Repository navigation
Expand file tree
/
Copy pathFragmentLoader.java
More file actions
130 lines (111 loc) · 3.53 KB
/
Copy pathFragmentLoader.java
File metadata and controls
130 lines (111 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.dianping.example.fragmentloader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;
import dalvik.system.DexClassLoader;
public class FragmentLoader extends Activity {
private AssetManager asm;
private Resources res;
private Theme thm;
private ClassLoader cl;
@Override
protected void onCreate(Bundle savedInstanceState) {
if ("com.dianping.intent.action.LOAD_FRAGMENT".equals(getIntent()
.getAction())) {
// we need to setup environment before super.onCreate
try {
String path = getIntent().getStringExtra("path");
InputStream ins = MyApplication.instance().getAssets()
.open(path);
byte[] bytes = new byte[ins.available()];
ins.read(bytes);
ins.close();
File f = new File(MyApplication.instance().getFilesDir(), "dex");
f.mkdir();
f = new File(f, "FL_" + Integer.toHexString(path.hashCode())
+ ".apk");
FileOutputStream fos = new FileOutputStream(f);
fos.write(bytes);
fos.close();
File fo = new File(MyApplication.instance().getFilesDir(),
"dexout");
fo.mkdir();
DexClassLoader dcl = new DexClassLoader(f.getAbsolutePath(),
fo.getAbsolutePath(), null, super.getClassLoader());
cl = dcl;
try {
AssetManager am = (AssetManager) AssetManager.class
.newInstance();
am.getClass().getMethod("addAssetPath", String.class)
.invoke(am, f.getAbsolutePath());
asm = am;
} catch (Exception e) {
throw new RuntimeException(e);
}
Resources superRes = super.getResources();
res = new Resources(asm, superRes.getDisplayMetrics(),
superRes.getConfiguration());
thm = res.newTheme();
thm.setTo(super.getTheme());
} catch (Exception e) {
e.printStackTrace();
}
}
super.onCreate(savedInstanceState);
FrameLayout rootView = new FrameLayout(this);
rootView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
rootView.setId(android.R.id.primary);
setContentView(rootView);
if (savedInstanceState != null)
return;
if ("com.dianping.intent.action.LOAD_FRAGMENT".equals(getIntent()
.getAction())) {
try {
String fragmentClass = getIntent().getStringExtra("class");
Fragment f = (Fragment) getClassLoader().loadClass(
fragmentClass).newInstance();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(android.R.id.primary, f);
ft.commit();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
} else {
Fragment f = new ListApkFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(android.R.id.primary, f);
ft.commit();
}
}
@Override
public AssetManager getAssets() {
return asm == null ? super.getAssets() : asm;
}
@Override
public Resources getResources() {
return res == null ? super.getResources() : res;
}
@Override
public Theme getTheme() {
return thm == null ? super.getTheme() : thm;
}
@Override
public ClassLoader getClassLoader() {
return cl == null ? super.getClassLoader() : cl;
}
}