Exemple #1
0
 /**
  * @param context
  * @return
  */
 public static String getApplicationName(final Context context) {
   String name = null;
   try {
     final PackageManager packagemanager = context.getPackageManager();
     final PackageInfo packageinfo = packagemanager.getPackageInfo(context.getPackageName(), 0);
     name =
         packageinfo.applicationInfo.loadLabel(packagemanager).toString()
             + "("
             + packageinfo.packageName
             + ")";
   } catch (Exception x) {
     DBG.m(x);
   }
   return name;
 }
  String[] getPluginDirectories() {

    ArrayList<String> directories = new ArrayList<String>();
    PackageManager pm = this.mAppContext.getPackageManager();
    List<ResolveInfo> plugins =
        pm.queryIntentServices(
            new Intent(PLUGIN_ACTION), PackageManager.GET_SERVICES | PackageManager.GET_META_DATA);

    synchronized (mPackageInfoCache) {

      // clear the list of existing packageInfo objects
      mPackageInfoCache.clear();

      for (ResolveInfo info : plugins) {

        // retrieve the plugin's service information
        ServiceInfo serviceInfo = info.serviceInfo;
        if (serviceInfo == null) {
          Log.w(LOGTAG, "Ignore bad plugin");
          continue;
        }

        Log.w(LOGTAG, "Loading plugin: " + serviceInfo.packageName);

        // retrieve information from the plugin's manifest
        PackageInfo pkgInfo;
        try {
          pkgInfo =
              pm.getPackageInfo(
                  serviceInfo.packageName,
                  PackageManager.GET_PERMISSIONS | PackageManager.GET_SIGNATURES);
        } catch (Exception e) {
          Log.w(LOGTAG, "Can't find plugin: " + serviceInfo.packageName);
          continue;
        }
        if (pkgInfo == null) {
          Log.w(
              LOGTAG,
              "Loading plugin: "
                  + serviceInfo.packageName
                  + ". Could not load package information.");
          continue;
        }

        /*
         * find the location of the plugin's shared library. The default
         * is to assume the app is either a user installed app or an
         * updated system app. In both of these cases the library is
         * stored in the app's data directory.
         */
        String directory = pkgInfo.applicationInfo.dataDir + "/lib";
        final int appFlags = pkgInfo.applicationInfo.flags;
        final int updatedSystemFlags =
            ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
        // preloaded system app with no user updates
        if ((appFlags & updatedSystemFlags) == ApplicationInfo.FLAG_SYSTEM) {
          directory = PLUGIN_SYSTEM_LIB + pkgInfo.packageName;
        }

        // check if the plugin has the required permissions
        String permissions[] = pkgInfo.requestedPermissions;
        if (permissions == null) {
          Log.w(
              LOGTAG,
              "Loading plugin: "
                  + serviceInfo.packageName
                  + ". Does not have required permission.");
          continue;
        }
        boolean permissionOk = false;
        for (String permit : permissions) {
          if (PLUGIN_PERMISSION.equals(permit)) {
            permissionOk = true;
            break;
          }
        }
        if (!permissionOk) {
          Log.w(
              LOGTAG,
              "Loading plugin: "
                  + serviceInfo.packageName
                  + ". Does not have required permission (2).");
          continue;
        }

        // check to ensure the plugin is properly signed
        Signature signatures[] = pkgInfo.signatures;
        if (signatures == null) {
          Log.w(LOGTAG, "Loading plugin: " + serviceInfo.packageName + ". Not signed.");
          continue;
        }

        // determine the type of plugin from the manifest
        if (serviceInfo.metaData == null) {
          Log.e(LOGTAG, "The plugin '" + serviceInfo.name + "' has no type defined");
          continue;
        }

        String pluginType = serviceInfo.metaData.getString(PLUGIN_TYPE);
        if (!TYPE_NATIVE.equals(pluginType)) {
          Log.e(LOGTAG, "Unrecognized plugin type: " + pluginType);
          continue;
        }

        try {
          Class<?> cls = getPluginClass(serviceInfo.packageName, serviceInfo.name);

          // TODO implement any requirements of the plugin class here!
          boolean classFound = true;

          if (!classFound) {
            Log.e(
                LOGTAG,
                "The plugin's class' "
                    + serviceInfo.name
                    + "' does not extend the appropriate class.");
            continue;
          }

        } catch (NameNotFoundException e) {
          Log.e(LOGTAG, "Can't find plugin: " + serviceInfo.packageName);
          continue;
        } catch (ClassNotFoundException e) {
          Log.e(LOGTAG, "Can't find plugin's class: " + serviceInfo.name);
          continue;
        }

        // if all checks have passed then make the plugin available
        mPackageInfoCache.add(pkgInfo);
        directories.add(directory);
      }
    }

    return directories.toArray(new String[directories.size()]);
  }