Exemple #1
0
  /**
   * Check if ads should be hidden.
   *
   * @param context {@link Context}
   * @return true if ads should be hidden
   */
  public static boolean hideAds(final Context context) {
    PackageManager pm = context.getPackageManager();
    Intent donationCheck = new Intent(DONATOR_BROADCAST_CHECK);
    ResolveInfo ri = pm.resolveService(donationCheck, 0);
    if (ri == null) {
      return false;
    } else {
      ComponentName cn = new ComponentName(ri.serviceInfo.packageName, ri.serviceInfo.name);
      int i = pm.getComponentEnabledSetting(cn);
      if (i == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
          || i == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT && ri.serviceInfo.enabled) {
        int match = pm.checkSignatures(context.getPackageName(), ri.serviceInfo.packageName);
        if (match != PackageManager.SIGNATURE_MATCH) {
          Log.e(TAG, "signatures do not match: " + match);
          return false;
        }
        donationCheck.setComponent(cn);
      } else {
        Log.w(TAG, ri.serviceInfo.packageName + ": " + ri.serviceInfo.enabled);
        return false;
      }
    }

    double r = Math.random();
    if (r < CHECK_DONATOR_LIC) {
      // verify donator license
      ComponentName cn = context.startService(donationCheck);
      return cn != null;
    } else {
      return true;
    }
  }
  /**
   * Goes through all apps that handle VIEW intents and have a warmup service. Picks the one chosen
   * by the user if there is one, otherwise makes a best effort to return a valid package name.
   *
   * <p>This is <strong>not</strong> threadsafe.
   *
   * @param context {@link Context} to use for accessing {@link PackageManager}.
   * @return The package name recommended to use for connecting to custom tabs related components.
   */
  public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null) return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
      defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
      Intent serviceIntent = new Intent();
      serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
      serviceIntent.setPackage(info.activityInfo.packageName);
      if (pm.resolveService(serviceIntent, 0) != null) {
        packagesSupportingCustomTabs.add(info.activityInfo.packageName);
      }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
      sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
      sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
        && !hasSpecializedHandlerIntents(context, activityIntent)
        && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
      sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
      sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
      sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
      sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
      sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
  }