/**
   * Searches the given package for a resource to use to replace the Drawable on the target with the
   * given resource id
   *
   * @param component of the .apk that contains the resource
   * @param name of the metadata in the .apk
   * @param existingResId the resource id of the target to search for
   * @return true if found in the given package and replaced at least one target Drawables
   */
  public boolean replaceTargetDrawablesIfPresent(
      ComponentName component, String name, int existingResId) {
    if (existingResId == 0) return false;

    boolean replaced = false;
    if (component != null) {
      try {
        PackageManager packageManager = mContext.getPackageManager();
        // Look for the search icon specified in the activity meta-data
        Bundle metaData =
            packageManager.getActivityInfo(component, PackageManager.GET_META_DATA).metaData;
        if (metaData != null) {
          int iconResId = metaData.getInt(name);
          if (iconResId != 0) {
            Resources res = packageManager.getResourcesForActivity(component);
            replaced = replaceTargetDrawables(res, existingResId, iconResId);
          }
        }
      } catch (NameNotFoundException e) {
        Log.w(
            TAG, "Failed to swap drawable; " + component.flattenToShortString() + " not found", e);
      } catch (Resources.NotFoundException nfe) {
        Log.w(TAG, "Failed to swap drawable from " + component.flattenToShortString(), nfe);
      }
    }
    if (!replaced) {
      // Restore the original drawable
      replaceTargetDrawables(mContext.getResources(), existingResId, existingResId);
    }
    return replaced;
  }