3131import android .content .pm .ResolveInfo ;
3232import android .net .Uri ;
3333import android .os .Build ;
34+ import android .os .Bundle ;
3435import android .os .Handler ;
3536import android .util .Log ;
3637import android .util .Singleton ;
3738
3839import com .didi .virtualapk .delegate .ActivityManagerProxy ;
3940import com .didi .virtualapk .delegate .IContentProviderProxy ;
41+ import com .didi .virtualapk .delegate .RemoteContentProvider ;
4042import com .didi .virtualapk .internal .ComponentsHandler ;
4143import com .didi .virtualapk .internal .LoadedPlugin ;
42- import com .didi .virtualapk .internal .PluginContentResolver ;
4344import com .didi .virtualapk .internal .VAInstrumentation ;
44- import com .didi .virtualapk .utils .PluginUtil ;
45+ import com .didi .virtualapk .internal . utils .PluginUtil ;
4546import com .didi .virtualapk .utils .Reflector ;
4647import com .didi .virtualapk .utils .RunUtil ;
4748
@@ -66,14 +67,15 @@ public class PluginManager {
6667 private static volatile PluginManager sInstance = null ;
6768
6869 // Context of host app
69- private Context mContext ;
70- private ComponentsHandler mComponentsHandler ;
71- private Map <String , LoadedPlugin > mPlugins = new ConcurrentHashMap <>();
72- private final List <Callback > mCallbacks = new ArrayList <>();
70+ protected final Context mContext ;
71+ protected final Application mApplication ;
72+ protected ComponentsHandler mComponentsHandler ;
73+ protected final Map <String , LoadedPlugin > mPlugins = new ConcurrentHashMap <>();
74+ protected final List <Callback > mCallbacks = new ArrayList <>();
7375
74- private VAInstrumentation mInstrumentation ; // Hooked instrumentation
75- private IActivityManager mActivityManager ; // Hooked IActivityManager binder
76- private IContentProvider mIContentProvider ; // Hooked IContentProvider binder
76+ protected VAInstrumentation mInstrumentation ; // Hooked instrumentation
77+ protected IActivityManager mActivityManager ; // Hooked IActivityManager binder
78+ protected IContentProvider mIContentProvider ; // Hooked IContentProvider binder
7779
7880 public static PluginManager getInstance (Context base ) {
7981 if (sInstance == null ) {
@@ -89,14 +91,22 @@ public static PluginManager getInstance(Context base) {
8991
9092 private static PluginManager createInstance (Context context ) {
9193 try {
92- String factoryClass = context .getPackageManager ()
93- .getApplicationInfo (context .getPackageName (), PackageManager .GET_META_DATA )
94- .metaData .getString ("VA_FACTORY" );
94+ Bundle metaData = context .getPackageManager ()
95+ .getApplicationInfo (context .getPackageName (), PackageManager .GET_META_DATA )
96+ .metaData ;
97+
98+ if (metaData == null ) {
99+ return new PluginManager (context );
100+ }
101+
102+ String factoryClass = metaData .getString ("VA_FACTORY" );
103+
95104 if (factoryClass == null ) {
96105 return new PluginManager (context );
97106 }
98107
99108 PluginManager pluginManager = Reflector .on (factoryClass ).method ("create" , Context .class ).call (context );
109+
100110 if (pluginManager != null ) {
101111 Log .d (TAG , "Created a instance of " + pluginManager .getClass ());
102112 return pluginManager ;
@@ -110,17 +120,25 @@ private static PluginManager createInstance(Context context) {
110120 }
111121
112122 protected PluginManager (Context context ) {
113- Context app = context . getApplicationContext ();
114- if ( app == null ) {
115- this .mContext = context ;
123+ if ( context instanceof Application ) {
124+ this . mApplication = ( Application ) context ;
125+ this .mContext = mApplication . getBaseContext () ;
116126 } else {
117- this .mContext = ((Application )app ).getBaseContext ();
127+ final Context app = context .getApplicationContext ();
128+ if (app == null ) {
129+ this .mContext = context ;
130+ this .mApplication = ActivityThread .currentApplication ();
131+ } else {
132+ this .mApplication = (Application ) app ;
133+ this .mContext = mApplication .getBaseContext ();
134+ }
118135 }
119- prepare ();
136+
137+ mComponentsHandler = createComponentsHandler ();
138+ hookCurrentProcess ();
120139 }
121140
122- private void prepare () {
123- mComponentsHandler = createComponentsHandler ();
141+ protected void hookCurrentProcess () {
124142 hookInstrumentationAndHandler ();
125143 hookSystemServices ();
126144 hookDataBindingUtil ();
@@ -138,6 +156,10 @@ public void run() {
138156 protected void doInWorkThread () {
139157 }
140158
159+ public Application getHostApplication () {
160+ return this .mApplication ;
161+ }
162+
141163 protected ComponentsHandler createComponentsHandler () {
142164 return new ComponentsHandler (this );
143165 }
@@ -208,10 +230,10 @@ protected void hookInstrumentationAndHandler() {
208230 try {
209231 ActivityThread activityThread = ActivityThread .currentActivityThread ();
210232 Instrumentation baseInstrumentation = activityThread .getInstrumentation ();
211- if (baseInstrumentation .getClass ().getName ().contains ("lbe" )) {
212- // reject executing in paralell space, for example, lbe.
213- System .exit (0 );
214- }
233+ // if (baseInstrumentation.getClass().getName().contains("lbe")) {
234+ // // reject executing in paralell space, for example, lbe.
235+ // System.exit(0);
236+ // }
215237
216238 final VAInstrumentation instrumentation = createInstrumentation (baseInstrumentation );
217239
@@ -225,14 +247,14 @@ protected void hookInstrumentationAndHandler() {
225247 }
226248
227249 protected void hookIContentProviderAsNeeded () {
228- Uri uri = Uri .parse (PluginContentResolver .getUri (mContext ));
250+ Uri uri = Uri .parse (RemoteContentProvider .getUri (mContext ));
229251 mContext .getContentResolver ().call (uri , "wakeup" , null , null );
230252 try {
231253 Field authority = null ;
232- Field mProvider = null ;
254+ Field provider = null ;
233255 ActivityThread activityThread = ActivityThread .currentActivityThread ();
234- Map mProviderMap = Reflector .with (activityThread ).field ("mProviderMap" ).get ();
235- Iterator iter = mProviderMap .entrySet ().iterator ();
256+ Map providerMap = Reflector .with (activityThread ).field ("mProviderMap" ).get ();
257+ Iterator iter = providerMap .entrySet ().iterator ();
236258 while (iter .hasNext ()) {
237259 Map .Entry entry = (Map .Entry ) iter .next ();
238260 Object key = entry .getKey ();
@@ -247,12 +269,12 @@ protected void hookIContentProviderAsNeeded() {
247269 }
248270 auth = (String ) authority .get (key );
249271 }
250- if (auth .equals (PluginContentResolver .getAuthority (mContext ))) {
251- if (mProvider == null ) {
252- mProvider = val .getClass ().getDeclaredField ("mProvider" );
253- mProvider .setAccessible (true );
272+ if (auth .equals (RemoteContentProvider .getAuthority (mContext ))) {
273+ if (provider == null ) {
274+ provider = val .getClass ().getDeclaredField ("mProvider" );
275+ provider .setAccessible (true );
254276 }
255- IContentProvider rawProvider = (IContentProvider ) mProvider .get (val );
277+ IContentProvider rawProvider = (IContentProvider ) provider .get (val );
256278 IContentProvider proxy = IContentProviderProxy .newInstance (mContext , rawProvider );
257279 mIContentProvider = proxy ;
258280 Log .d (TAG , "hookIContentProvider succeed : " + mIContentProvider );
@@ -281,17 +303,16 @@ public void loadPlugin(File apk) throws Exception {
281303 }
282304
283305 LoadedPlugin plugin = createLoadedPlugin (apk );
284- if (null != plugin ) {
285- this .mPlugins .put (plugin .getPackageName (), plugin );
286- synchronized (mCallbacks ) {
287- for (int i = 0 ; i < mCallbacks .size (); i ++) {
288- mCallbacks .get (i ).onAddedLoadedPlugin (plugin );
289- }
306+
307+ if (null == plugin ) {
308+ throw new RuntimeException ("Can't load plugin which is invalid: " + apk .getAbsolutePath ());
309+ }
310+
311+ this .mPlugins .put (plugin .getPackageName (), plugin );
312+ synchronized (mCallbacks ) {
313+ for (int i = 0 ; i < mCallbacks .size (); i ++) {
314+ mCallbacks .get (i ).onAddedLoadedPlugin (plugin );
290315 }
291- // try to invoke plugin's application
292- plugin .invokeApplication ();
293- } else {
294- throw new RuntimeException ("Can't load plugin which is invalid: " + apk .getAbsolutePath ());
295316 }
296317 }
297318
0 commit comments