Java Code Examples for android.content.pm.PackageManager#getServiceInfo()

The following examples show how to use android.content.pm.PackageManager#getServiceInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CheckUtils.java    From OPFPush with Apache License 2.0 6 votes vote down vote up
/**
 * Checks is a service has been described in the AndroidManifest.xml file.
 *
 * @param context The instance of {@link android.content.Context}.
 * @param service The checked service.
 */
@SuppressWarnings("PMD.PreserveStackTrace")
public static void checkService(@NonNull final Context context,
                                @NonNull final ComponentName service,
                                @Nullable final CheckManifestHandler checkManifestHandler) {
    final PackageManager packageManager = context.getPackageManager();
    try {
        packageManager.getServiceInfo(service, 0);
    } catch (PackageManager.NameNotFoundException e) {
        final String message = "Service " + service.getClassName()
                + " hasn't been declared in AndroidManifest.xml";

        if (checkManifestHandler == null) {
            throw new IllegalStateException(message);
        } else {
            checkManifestHandler.onCheckManifestError(message);
        }
    }
}
 
Example 2
Source File: ComponentDiscovery.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
private Bundle getMetadata(Context context) {
  try {
    PackageManager manager = context.getPackageManager();
    if (manager == null) {
      Log.w(TAG, "Context has no PackageManager.");
      return null;
    }
    ServiceInfo info =
        manager.getServiceInfo(
            new ComponentName(context, discoveryService), PackageManager.GET_META_DATA);
    if (info == null) {
      Log.w(TAG, discoveryService + " has no service info.");
      return null;
    }
    return info.metaData;
  } catch (PackageManager.NameNotFoundException e) {
    Log.w(TAG, "Application info not found.");
    return null;
  }
}
 
Example 3
Source File: MetadataBackendRegistry.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
private static Bundle getMetadata(Context context) {
  try {
    PackageManager manager = context.getPackageManager();
    if (manager == null) {
      Log.w(TAG, "Context has no PackageManager.");
      return null;
    }
    ServiceInfo info =
        manager.getServiceInfo(
            new ComponentName(context, TransportBackendDiscovery.class),
            PackageManager.GET_META_DATA);
    if (info == null) {
      Log.w(TAG, "TransportBackendDiscovery has no service info.");
      return null;
    }
    return info.metaData;
  } catch (PackageManager.NameNotFoundException e) {
    Log.w(TAG, "Application info not found.");
    return null;
  }
}
 
Example 4
Source File: RecognitionServiceManager.java    From AlexaAndroid with GNU General Public License v2.0 6 votes vote down vote up
public static Pair<String, String> getLabel(Context context, String comboAsString) {
    String recognizer = "[?]";
    String language = "[?]";
    String[] splits = TextUtils.split(comboAsString, SEPARATOR);
    if (splits.length > 0) {
        PackageManager pm = context.getPackageManager();
        ComponentName recognizerComponentName = ComponentName.unflattenFromString(splits[0]);
        if (recognizerComponentName != null) {
            try {
                ServiceInfo si = pm.getServiceInfo(recognizerComponentName, 0);
                recognizer = si.loadLabel(pm).toString();
            } catch (PackageManager.NameNotFoundException e) {
                // ignored
            }
        }
    }
    if (splits.length > 1) {
        language = makeLangLabel(splits[1]);
    }
    return new Pair<>(recognizer, language);
}
 
Example 5
Source File: RecognitionServiceManager.java    From AlexaAndroid with GNU General Public License v2.0 6 votes vote down vote up
public static Pair<String, String> getLabel(Context context, String comboAsString) {
    String recognizer = "[?]";
    String language = "[?]";
    String[] splits = TextUtils.split(comboAsString, SEPARATOR);
    if (splits.length > 0) {
        PackageManager pm = context.getPackageManager();
        ComponentName recognizerComponentName = ComponentName.unflattenFromString(splits[0]);
        if (recognizerComponentName != null) {
            try {
                ServiceInfo si = pm.getServiceInfo(recognizerComponentName, 0);
                recognizer = si.loadLabel(pm).toString();
            } catch (PackageManager.NameNotFoundException e) {
                // ignored
            }
        }
    }
    if (splits.length > 1) {
        language = makeLangLabel(splits[1]);
    }
    return new Pair<>(recognizer, language);
}
 
Example 6
Source File: RouterServiceValidator.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns the package name for the component name
 * @param cn
 * @param pm
 * @return
 */
private String appPackageForComponentName(ComponentName cn,PackageManager pm ){
	if(cn!=null && pm!=null){
		ServiceInfo info;
		try {
			info = pm.getServiceInfo(cn, 0);
			return info.applicationInfo.packageName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}
	return null;
	
}
 
Example 7
Source File: DevicePluginXmlUtil.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * 指定されたパッケージ名からサービスのコンポーネント情報を取得します.
 *
 * @param context コンテキスト
 * @param packageName パッケージ名
 * @return コンポーネント情報
 */
private static ServiceInfo getServiceInfo(final Context context, final String packageName) {
    try {
        PackageManager pkgMgr = context.getPackageManager();
        PackageInfo pkg = pkgMgr.getPackageInfo(packageName, PackageManager.GET_SERVICES);
        if (pkg != null) {
            ServiceInfo[] services = pkg.services;
            if (services != null) {
                for (int i = 0; i < services.length; i++) {
                    String pkgName = services[i].packageName;
                    String className = services[i].name;
                    ComponentName component = new ComponentName(pkgName, className);
                    ServiceInfo serviceInfo = pkgMgr.getServiceInfo(component, PackageManager.GET_META_DATA);
                    if (serviceInfo.metaData != null) {
                        Object xmlData = serviceInfo.metaData.get(PLUGIN_META_DATA);
                        if (xmlData instanceof Integer) {
                            XmlResourceParser xrp = serviceInfo.loadXmlMetaData(pkgMgr, PLUGIN_META_DATA);
                            if (xrp != null) {
                                return serviceInfo;
                            }
                        }
                    }
                }
            }
        }
        return null;
    } catch (NameNotFoundException e) {
        return null;
    }
}
 
Example 8
Source File: RecognitionServiceManager.java    From speechutils with Apache License 2.0 5 votes vote down vote up
public static Drawable getServiceIcon(Context context, ComponentName recognizerComponentName) {
    try {
        PackageManager pm = context.getPackageManager();
        ServiceInfo si = pm.getServiceInfo(recognizerComponentName, 0);
        return si.loadIcon(pm);
    } catch (PackageManager.NameNotFoundException e) {
        // ignored
    }
    return null;
}
 
Example 9
Source File: RecognitionServiceManager.java    From speechutils with Apache License 2.0 5 votes vote down vote up
public static ServiceInfo getServiceInfo(Context context, ComponentName recognizerComponentName) {
    try {
        PackageManager pm = context.getPackageManager();
        return pm.getServiceInfo(recognizerComponentName, PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        // ignored
    }
    return null;
}
 
Example 10
Source File: RecognitionServiceManager.java    From speechutils with Apache License 2.0 5 votes vote down vote up
public static String getServiceLabel(Context context, ComponentName recognizerComponentName) {
    try {
        PackageManager pm = context.getPackageManager();
        ServiceInfo si = pm.getServiceInfo(recognizerComponentName, 0);
        return si.loadLabel(pm).toString();
    } catch (PackageManager.NameNotFoundException e) {
        // ignored
    }
    return "[?]";
}
 
Example 11
Source File: RecognitionServiceManager.java    From AlexaAndroid with GNU General Public License v2.0 5 votes vote down vote up
public static String getServiceLabel(Context context, ComponentName recognizerComponentName) {
    String recognizer = "[?]";
    PackageManager pm = context.getPackageManager();
    if (recognizerComponentName != null) {
        try {
            ServiceInfo si = pm.getServiceInfo(recognizerComponentName, 0);
            recognizer = si.loadLabel(pm).toString();
        } catch (PackageManager.NameNotFoundException e) {
            // ignored
        }
    }
    return recognizer;
}
 
Example 12
Source File: RecognitionServiceManager.java    From AlexaAndroid with GNU General Public License v2.0 5 votes vote down vote up
public static String getServiceLabel(Context context, ComponentName recognizerComponentName) {
    String recognizer = "[?]";
    PackageManager pm = context.getPackageManager();
    if (recognizerComponentName != null) {
        try {
            ServiceInfo si = pm.getServiceInfo(recognizerComponentName, 0);
            recognizer = si.loadLabel(pm).toString();
        } catch (PackageManager.NameNotFoundException e) {
            // ignored
        }
    }
    return recognizer;
}
 
Example 13
Source File: MetaDataUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * Return the value of meta-data in service.
 *
 * @param clz The service class.
 * @param key The key of meta-data.
 * @return the value of meta-data in service
 */
public static String getMetaDataInService(@NonNull final Class<? extends Service> clz,
                                          @NonNull final String key) {
    String value = "";
    PackageManager pm = Utils.getApp().getPackageManager();
    ComponentName componentName = new ComponentName(Utils.getApp(), clz);
    try {
        ServiceInfo info = pm.getServiceInfo(componentName, PackageManager.GET_META_DATA);
        value = String.valueOf(info.metaData.get(key));
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return value;
}
 
Example 14
Source File: LeakCanaryInternals.java    From leakcanary-for-eclipse with MIT License 4 votes vote down vote up
public static boolean isInServiceProcess(Context context, Class<? extends Service> serviceClass) {
  PackageManager packageManager = context.getPackageManager();
  PackageInfo packageInfo;
  try {
    packageInfo = packageManager.getPackageInfo(context.getPackageName(), GET_SERVICES);
  } catch (Exception e) {
    Log.e("AndroidUtils", "Could not get package info for " + context.getPackageName(), e);
    return false;
  }
  String mainProcess = packageInfo.applicationInfo.processName;

  ComponentName component = new ComponentName(context, serviceClass);
  ServiceInfo serviceInfo;
  try {
    serviceInfo = packageManager.getServiceInfo(component, 0);
  } catch (PackageManager.NameNotFoundException ignored) {
    // Service is disabled.
    return false;
  }

  if (serviceInfo.processName.equals(mainProcess)) {
    Log.e("AndroidUtils",
        "Did not expect service " + serviceClass + " to run in main process " + mainProcess);
    // Technically we are in the service process, but we're not in the service dedicated process.
    return false;
  }

  int myPid = android.os.Process.myPid();
  ActivityManager activityManager =
      (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  ActivityManager.RunningAppProcessInfo myProcess = null;
  for (ActivityManager.RunningAppProcessInfo process : activityManager.getRunningAppProcesses()) {
    if (process.pid == myPid) {
      myProcess = process;
      break;
    }
  }
  if (myProcess == null) {
    Log.e("AndroidUtils", "Could not find running process for " + myPid);
    return false;
  }

  return myProcess.processName.equals(serviceInfo.processName);
}
 
Example 15
Source File: LeakCanaryInternals.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
public static boolean isInServiceProcess(Context context, Class<? extends Service> serviceClass) {
  PackageManager packageManager = context.getPackageManager();
  PackageInfo packageInfo;
  try {
    packageInfo = packageManager.getPackageInfo(context.getPackageName(), GET_SERVICES);
  } catch (Exception e) {
    CanaryLog.d(e, "Could not get package info for %s", context.getPackageName());
    return false;
  }
  String mainProcess = packageInfo.applicationInfo.processName;

  ComponentName component = new ComponentName(context, serviceClass);
  ServiceInfo serviceInfo;
  try {
    serviceInfo = packageManager.getServiceInfo(component, PackageManager.GET_DISABLED_COMPONENTS);
  } catch (PackageManager.NameNotFoundException ignored) {
    // Service is disabled.
    return false;
  }

  if (serviceInfo.processName.equals(mainProcess)) {
    CanaryLog.d("Did not expect service %s to run in main process %s", serviceClass, mainProcess);
    // Technically we are in the service process, but we're not in the service dedicated process.
    return false;
  }

  int myPid = android.os.Process.myPid();
  ActivityManager activityManager =
      (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  ActivityManager.RunningAppProcessInfo myProcess = null;
  List<ActivityManager.RunningAppProcessInfo> runningProcesses;
  try {
    runningProcesses = activityManager.getRunningAppProcesses();
  } catch (SecurityException exception) {
    // https://github.com/square/leakcanary/issues/948
    CanaryLog.d("Could not get running app processes %d", exception);
    return false;
  }
  if (runningProcesses != null) {
    for (ActivityManager.RunningAppProcessInfo process : runningProcesses) {
      if (process.pid == myPid) {
        myProcess = process;
        break;
      }
    }
  }
  if (myProcess == null) {
    CanaryLog.d("Could not find running process for %d", myPid);
    return false;
  }

  return myProcess.processName.equals(serviceInfo.processName);
}
 
Example 16
Source File: VoiceInteractionServiceInfo.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public VoiceInteractionServiceInfo(PackageManager pm, ComponentName comp)
        throws PackageManager.NameNotFoundException {
    this(pm, pm.getServiceInfo(comp, PackageManager.GET_META_DATA));
}
 
Example 17
Source File: DevicePluginManager.java    From DeviceConnect-Android with MIT License 3 votes vote down vote up
/**
 * 指定されたコンポーネントの ServiceInfo を取得します.
 * <p>
 * 一致する ServiceInfo が存在しない場合は null を返却します。
 * </p>
 * @param pkgMgr パッケージマネージャ
 * @param component コンポーネント
 * @return ServiceInfoのインスタンス
 */
private ServiceInfo getServiceInfo(final PackageManager pkgMgr, final ComponentName component) {
    try {
        return pkgMgr.getServiceInfo(component, PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        return null;
    }
}