Android Open Source - AndroidPlugin Plugin Host Application






From Project

Back to project page AndroidPlugin.

License

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.

Java Source Code

/*
 * 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);
  }

}




Java Source Code List

android.widget.ViewStub.java
androidx.plmgrdemo.MainActivity.java
androidx.plmgrdemo.PlugListViewAdapter.java
androidx.pluginmgr.ActivityClassGenerator.java
androidx.pluginmgr.ActivityOverider.java
androidx.pluginmgr.FileUtil.java
androidx.pluginmgr.FrameworkClassLoader.java
androidx.pluginmgr.LayoutInflaterWrapper.java
androidx.pluginmgr.PlugInfo.java
androidx.pluginmgr.PluginActivityLifeCycleCallback.java
androidx.pluginmgr.PluginClassLoader.java
androidx.pluginmgr.PluginContextWrapper.java
androidx.pluginmgr.PluginManager.java
androidx.pluginmgr.PluginManifestUtil.java
androidx.pluginmgr.ReflectionUtils.java
androidx.pluginmgr.XmlManifestReader.java
com.limemobile.app.demo.pluginclienta.ClientABindService.java
com.limemobile.app.demo.pluginclienta.ClientAStartedService.java
com.limemobile.app.demo.pluginclienta.MainActivity.java
com.limemobile.app.demo.pluginclienta.TestButton.java
com.limemobile.app.demo.pluginclienta.TestFragmentActivity.java
com.limemobile.app.demo.pluginclienta.TestFragment.java
com.limemobile.app.demo.pluginclientb.ClientBStartedService.java
com.limemobile.app.demo.pluginclientb.MainActivity.java
com.limemobile.app.demo.pluginhost.HostBindService.java
com.limemobile.app.demo.pluginhost.HostStartedService.java
com.limemobile.app.demo.pluginhost.MainActivity.java
com.limemobile.app.demo.pluginhost.MyApplication.java
com.limemobile.app.demo.pluginhost.TestHostClass.java
com.limemobile.app.plugin.IPluginActivity.java
com.limemobile.app.plugin.IPluginContentProvider.java
com.limemobile.app.plugin.IPluginService.java
com.limemobile.app.plugin.PluginClientActivity.java
com.limemobile.app.plugin.PluginClientFragmentActivity.java
com.limemobile.app.plugin.PluginClientService.java
com.limemobile.app.plugin.PluginHostApplication.java
com.limemobile.app.plugin.PluginHostContentProvider.java
com.limemobile.app.plugin.PluginHostDelegateActivity.java
com.limemobile.app.plugin.PluginHostDelegateContentProvider.java
com.limemobile.app.plugin.PluginHostDelegateFragmentActivity.java
com.limemobile.app.plugin.PluginHostDelegateService.java
com.limemobile.app.plugin.internal.IPluginActivityDelegate.java
com.limemobile.app.plugin.internal.IPluginContentProviderDelegate.java
com.limemobile.app.plugin.internal.IPluginServiceDelegate.java
com.limemobile.app.plugin.internal.PluginClientDexClassLoader.java
com.limemobile.app.plugin.internal.PluginClientInfo.java
com.limemobile.app.plugin.internal.PluginClientManager.java
com.limemobile.app.plugin.internal.PluginDelegateActivityImpl.java
com.limemobile.app.plugin.internal.PluginDelegateContentProviderImpl.java
com.limemobile.app.plugin.internal.PluginDelegateServiceImpl.java
com.limemobile.app.plugin.internal.ReflectFieldAccessor.java