Back to project page AndroidPlugin.
The source code is released under:
MIT License
If you think the Android project AndroidPlugin listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2014 achellies/* w w w. j a v a 2 s .c o m*/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.limemobile.app.plugin; import java.util.Collection; import java.util.Iterator; import java.util.List; import android.app.Application; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.pm.ServiceInfo; import android.os.Bundle; import com.limemobile.app.plugin.internal.PluginClientInfo; import com.limemobile.app.plugin.internal.PluginClientManager; import com.limemobile.app.plugin.internal.ReflectFieldAccessor; /** * PluginHost?Application??????PluginHostApplication? * ????????PluginClient??????????Process???????loadPluginClients()??????PluginClient * * @author achellies * */ public class PluginHostApplication extends Application { @Override public void onCreate() { super.onCreate(); try { /** * ??Android???????????Dalvik????Android?????????????APK???classes. * dex??????ClassLoader * ?????????AndroidManifest.xml??????????????Activity???????UI? * * Android??dalvik.system.DexClassLoader??????????Java??????? * ???????Activity??????? * ????????ClassLoader?Application.mBase.mPackageInfo.mClassLoader? * ??????????apk????jar??????????plugin client???????jar? * * http://www.trinea.cn/android/java-loader-common-class/ * * @see TODO ?????? */ Context baseContext = new ReflectFieldAccessor<Context>(this, "mBase").get(); Object packageInfo = new ReflectFieldAccessor<Object>(baseContext, "mPackageInfo").get(); ReflectFieldAccessor<ClassLoader> sClassLoader = new ReflectFieldAccessor<ClassLoader>( packageInfo, "mClassLoader"); ClassLoader classLoader = sClassLoader.get(); PluginClientManager.sharedInstance(baseContext) .setPluginHostGlobalClassLoader(classLoader); } catch (Exception e) { e.printStackTrace(); } loadPluginClients(); } /** * ?????plugin * client,?????Application????????PluginManager??PluginClient????????????????Process * Activity??Service?????????????Process?????AndroidManifest.xml??? * android:process=":remote" */ protected void loadPluginClients() { } // TODO ??client?Application // Context???????Activity?Service????????????????????????????????ugly @Override public void startActivity(Intent intent) { List<ResolveInfo> resolveInfos = getPackageManager() .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); if (resolveInfos != null && !resolveInfos.isEmpty()) { super.startActivity(intent); } else { ComponentName componentName = intent.getComponent(); if (componentName == null) { throw new NullPointerException("Not Support Null ComponentName"); } Collection<PluginClientInfo> clients = PluginClientManager .sharedInstance(this).getPluginClients(); if (!clients.isEmpty()) { Iterator<PluginClientInfo> iterator = clients.iterator(); while (iterator.hasNext()) { PluginClientInfo clientInfo = iterator.next(); ActivityInfo[] activities = clientInfo.mClientPackageInfo.activities; if (activities != null) { for (ActivityInfo activityInfo : activities) { if (activityInfo.name.equals(componentName .getClassName())) { intent.setPackage(clientInfo.mPackageName); } } } } } PluginClientManager.sharedInstance(this) .startActivity(this, intent); } } @Override public void startActivity(Intent intent, Bundle options) { List<ResolveInfo> resolveInfos = getPackageManager() .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); if (resolveInfos != null && !resolveInfos.isEmpty()) { super.startActivity(intent, options); } else { ComponentName componentName = intent.getComponent(); if (componentName == null) { throw new NullPointerException("Not Support Null ComponentName"); } Collection<PluginClientInfo> clients = PluginClientManager .sharedInstance(this).getPluginClients(); if (!clients.isEmpty()) { Iterator<PluginClientInfo> iterator = clients.iterator(); while (iterator.hasNext()) { PluginClientInfo clientInfo = iterator.next(); ActivityInfo[] activities = clientInfo.mClientPackageInfo.activities; if (activities != null) { for (ActivityInfo activityInfo : activities) { if (activityInfo.name.equals(componentName .getClassName())) { intent.setPackage(clientInfo.mPackageName); } } } } } PluginClientManager.sharedInstance(this).startActivity(this, intent, options); } } @Override public ComponentName startService(Intent service) { List<ResolveInfo> resolveInfos = getPackageManager() .queryIntentServices(service, PackageManager.MATCH_DEFAULT_ONLY); if (resolveInfos != null && !resolveInfos.isEmpty()) { return super.startService(service); } else { ComponentName componentName = service.getComponent(); if (componentName == null) { throw new NullPointerException("Not Support Null ComponentName"); } Collection<PluginClientInfo> clients = PluginClientManager .sharedInstance(this).getPluginClients(); if (!clients.isEmpty()) { Iterator<PluginClientInfo> iterator = clients.iterator(); while (iterator.hasNext()) { PluginClientInfo clientInfo = iterator.next(); ServiceInfo[] services = clientInfo.mClientPackageInfo.services; if (services != null) { for (ServiceInfo serviceInfo : services) { if (serviceInfo.name.equals(componentName .getClassName())) { service.setPackage(clientInfo.mPackageName); } } } } } } return PluginClientManager.sharedInstance(this).startService(this, service); } @Override public boolean stopService(Intent service) { List<ResolveInfo> resolveInfos = getPackageManager() .queryIntentServices(service, PackageManager.MATCH_DEFAULT_ONLY); if (resolveInfos != null && !resolveInfos.isEmpty()) { return super.stopService(service); } else { ComponentName componentName = service.getComponent(); if (componentName == null) { throw new NullPointerException("Not Support Null ComponentName"); } Collection<PluginClientInfo> clients = PluginClientManager .sharedInstance(this).getPluginClients(); if (!clients.isEmpty()) { Iterator<PluginClientInfo> iterator = clients.iterator(); while (iterator.hasNext()) { PluginClientInfo clientInfo = iterator.next(); ServiceInfo[] services = clientInfo.mClientPackageInfo.services; if (services != null) { for (ServiceInfo serviceInfo : services) { if (serviceInfo.name.equals(componentName .getClassName())) { service.setPackage(clientInfo.mPackageName); } } } } } } return PluginClientManager.sharedInstance(this).stopService(this, service); } }