Java Code Examples for android.app.Application#getPackageManager()

The following examples show how to use android.app.Application#getPackageManager() . 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: SharedPreferencesUtils.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for getting a boolean value from the apps manifest
 *
 * @param preference the preference key.
 * @return whether the preference has been set or not
 */
public boolean isManifestSet(String preference) {
  Application application = (Application) firebaseApp.getApplicationContext();
  // Check if there's metadata in the manifest setting the auto-init state.
  try {
    PackageManager packageManager = application.getPackageManager();
    if (packageManager != null) {
      ApplicationInfo applicationInfo =
          packageManager.getApplicationInfo(
              application.getPackageName(), PackageManager.GET_META_DATA);
      return applicationInfo != null
          && applicationInfo.metaData != null
          && applicationInfo.metaData.containsKey(preference);
    }
  } catch (PackageManager.NameNotFoundException e) {
    // This shouldn't happen since it's this app's package. However, if it does, we want to fall
    // through to the default, and avoid throwing an exception

  }
  return false;
}
 
Example 2
Source File: SharedPreferencesUtils.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for getting a boolean value from the apps stored preferences. Falls back to
 * checking for a manifest preference before returning the default value.
 *
 * @param preference the manifest preference key.
 * @param defaultValue the default value to return if the key is not found.
 * @return the value stored or the default if the stored value is not found.
 */
public boolean getBooleanManifestValue(String preference, boolean defaultValue) {
  Application application = (Application) firebaseApp.getApplicationContext();
  // Check if there's metadata in the manifest setting the auto-init state.
  try {
    PackageManager packageManager = application.getPackageManager();
    if (packageManager != null) {
      ApplicationInfo applicationInfo =
          packageManager.getApplicationInfo(
              application.getPackageName(), PackageManager.GET_META_DATA);
      if (applicationInfo != null
          && applicationInfo.metaData != null
          && applicationInfo.metaData.containsKey(preference)) {
        return applicationInfo.metaData.getBoolean(preference);
      }
    }
  } catch (PackageManager.NameNotFoundException e) {
    // This shouldn't happen since it's this app's package. However, if it does, we want to fall
    // through to the default, and avoid throwing an exception
  }

  // Return the default
  return defaultValue;
}
 
Example 3
Source File: RemoteConnection.java    From CC with Apache License 2.0 6 votes vote down vote up
/**
 * 获取当前设备上安装的可供跨app调用组件的App列表
 * @return 包名集合
 */
public static List<String> scanComponentApps() {
    Application application = CC.getApplication();
    String curPkg = application.getPackageName();
    PackageManager pm = application.getPackageManager();
    // 查询所有已经安装的应用程序
    Intent intent = new Intent("action.com.billy.cc.connection");
    List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
    List<String> packageNames = new ArrayList<>();
    for (ResolveInfo info : list) {
        ActivityInfo activityInfo = info.activityInfo;
        String packageName = activityInfo.packageName;
        if (curPkg.equals(packageName)) {
            continue;
        }
        if (tryWakeup(packageName)) {
            packageNames.add(packageName);
        }
    }
    return packageNames;
}
 
Example 4
Source File: ManifestValidator.java    From clevertap-android-sdk with MIT License 6 votes vote down vote up
@SuppressWarnings("SameParameterValue")
private static void validateActivityInManifest(Application application, Class activityClass) throws PackageManager.NameNotFoundException {
    PackageManager pm = application.getPackageManager();
    String packageName = application.getPackageName();

    PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
    ActivityInfo[] activities = packageInfo.activities;
    String activityClassName = activityClass.getName();
    for (ActivityInfo activityInfo : activities) {
        if (activityInfo.name.equals(activityClassName)) {
            Logger.i(activityClassName.replaceFirst("com.clevertap.android.sdk.", "") + " is present");
            return;
        }
    }
    Logger.i(activityClassName.replaceFirst("com.clevertap.android.sdk.", "") + " not present");
}
 
Example 5
Source File: AndroidHack.java    From AtlasForAndroid with MIT License 6 votes vote down vote up
public static Object createNewLoadedApk(Application application, Object obj) {
    try {
        Method declaredMethod;
        ApplicationInfo applicationInfo = application.getPackageManager().getApplicationInfo(application.getPackageName(), 1152);
        application.getPackageManager();
        Resources resources = application.getResources();
        if (resources instanceof DelegateResources) {
            declaredMethod = resources.getClass().getSuperclass().getDeclaredMethod("getCompatibilityInfo", new Class[0]);
        } else {
            declaredMethod = resources.getClass().getDeclaredMethod("getCompatibilityInfo", new Class[0]);
        }
        declaredMethod.setAccessible(true);
        Class cls = Class.forName("android.content.res.CompatibilityInfo");
        Object invoke = declaredMethod.invoke(application.getResources(), new Object[0]);
        Method declaredMethod2 = AtlasHacks.ActivityThread.getmClass().getDeclaredMethod("getPackageInfoNoCheck", new Class[]{ApplicationInfo.class, cls});
        declaredMethod2.setAccessible(true);
        invoke = declaredMethod2.invoke(obj, new Object[]{applicationInfo, invoke});
        _mLoadedApk = invoke;
        return invoke;
    } catch (Throwable e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
 
Example 6
Source File: AppUtil.java    From SimpleProject with MIT License 5 votes vote down vote up
/**
 * 根据包名检查APP是否已安装
 * @param context
 * @param packageName
 * @return
 */
public static boolean appIsInstalled(Application context, String packageName) {
	PackageManager pm = context.getPackageManager();
	List<PackageInfo> packageList = pm.getInstalledPackages(0);
	for (PackageInfo appInfo : packageList) {
		if (appInfo.packageName.equals(packageName)) {
			return true;
		}
	}

	return false;
}
 
Example 7
Source File: AndroidHack.java    From ACDD with MIT License 5 votes vote down vote up
public static Object createNewLoadedApk(Application application, Object obj) {
    try {
        Method declaredMethod;
        ApplicationInfo applicationInfo = application.getPackageManager()
                .getApplicationInfo(application.getPackageName(), 1152);
        application.getPackageManager();
        Resources resources = application.getResources();
        if (resources instanceof DelegateResources) {
            declaredMethod = resources
                    .getClass()
                    .getSuperclass()
                    .getDeclaredMethod("getCompatibilityInfo");
        } else {
            declaredMethod = resources.getClass().getDeclaredMethod(
                    "getCompatibilityInfo");
        }
        declaredMethod.setAccessible(true);
        Class cls = Class.forName("android.content.res.CompatibilityInfo");
        Object invoke = declaredMethod.invoke(application.getResources()
        );
        Method declaredMethod2 = OpenAtlasHacks.ActivityThread.getmClass()
                .getDeclaredMethod("getPackageInfoNoCheck",
                        ApplicationInfo.class, cls);
        declaredMethod2.setAccessible(true);
        invoke = declaredMethod2.invoke(obj, applicationInfo, invoke);
        _mLoadedApk = invoke;
        return invoke;
    } catch (Throwable e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: ManifestValidator.java    From clevertap-android-sdk with MIT License 5 votes vote down vote up
private static void validateReceiverInManifest(Application application, String receiverClassName) throws PackageManager.NameNotFoundException {
    PackageManager pm = application.getPackageManager();
    String packageName = application.getPackageName();

    PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_RECEIVERS);
    ActivityInfo[] receivers = packageInfo.receivers;

    for (ActivityInfo activityInfo : receivers) {
        if (activityInfo.name.equals(receiverClassName)) {
            Logger.i(receiverClassName.replaceFirst("com.clevertap.android.sdk.", "") + " is present");
            return;
        }
    }
    Logger.i(receiverClassName.replaceFirst("com.clevertap.android.sdk.", "") + " not present");
}
 
Example 9
Source File: ManifestValidator.java    From clevertap-android-sdk with MIT License 5 votes vote down vote up
private static void validateServiceInManifest(Application application, String serviceClassName) throws PackageManager.NameNotFoundException {
    PackageManager pm = application.getPackageManager();
    String packageName = application.getPackageName();

    PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_SERVICES);
    ServiceInfo[] services = packageInfo.services;
    for (ServiceInfo serviceInfo : services) {
        if (serviceInfo.name.equals(serviceClassName)) {
            Logger.i(serviceClassName.replaceFirst("com.clevertap.android.sdk.", "") + " is present");
            return;
        }
    }
    Logger.i(serviceClassName.replaceFirst("com.clevertap.android.sdk.", "") + " not present");
}
 
Example 10
Source File: WebViewFactory.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private static Context getWebViewContextAndSetProvider() throws MissingWebViewPackageException {
    Application initialApplication = AppGlobals.getInitialApplication();
    try {
        WebViewProviderResponse response = null;
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW,
                "WebViewUpdateService.waitForAndGetProvider()");
        try {
            response = getUpdateService().waitForAndGetProvider();
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
        if (response.status != LIBLOAD_SUCCESS
                && response.status != LIBLOAD_FAILED_WAITING_FOR_RELRO) {
            throw new MissingWebViewPackageException("Failed to load WebView provider: "
                    + getWebViewPreparationErrorReason(response.status));
        }
        // Register to be killed before fetching package info - so that we will be
        // killed if the package info goes out-of-date.
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "ActivityManager.addPackageDependency()");
        try {
            ActivityManager.getService().addPackageDependency(
                    response.packageInfo.packageName);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
        // Fetch package info and verify it against the chosen package
        PackageInfo newPackageInfo = null;
        PackageManager pm = initialApplication.getPackageManager();
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "PackageManager.getPackageInfo()");
        try {
            newPackageInfo = pm.getPackageInfo(
                response.packageInfo.packageName,
                PackageManager.GET_SHARED_LIBRARY_FILES
                | PackageManager.MATCH_DEBUG_TRIAGED_MISSING
                // Make sure that we fetch the current provider even if its not
                // installed for the current user
                | PackageManager.MATCH_UNINSTALLED_PACKAGES
                // Fetch signatures for verification
                | PackageManager.GET_SIGNATURES
                // Get meta-data for meta data flag verification
                | PackageManager.GET_META_DATA);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }

        // Validate the newly fetched package info, throws MissingWebViewPackageException on
        // failure
        verifyPackageInfo(response.packageInfo, newPackageInfo);

        ApplicationInfo ai = newPackageInfo.applicationInfo;
        fixupStubApplicationInfo(ai, pm);

        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW,
                "initialApplication.createApplicationContext");
        try {
            // Construct an app context to load the Java code into the current app.
            Context webViewContext = initialApplication.createApplicationContext(
                    ai,
                    Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
            sPackageInfo = newPackageInfo;
            return webViewContext;
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
    } catch (RemoteException | PackageManager.NameNotFoundException e) {
        throw new MissingWebViewPackageException("Failed to load WebView provider: " + e);
    }
}
 
Example 11
Source File: ResourceUtils.java    From Dagger-2-Example with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param packageName
 * @return
 * @throws PackageManager.NameNotFoundException
 */
protected static Resources getResources(Application application, String packageName) throws PackageManager.NameNotFoundException {
    PackageManager pm = application.getPackageManager();
    return pm.getResourcesForApplication(packageName);
}