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

The following examples show how to use android.content.pm.PackageManager#resolveContentProvider() . 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: FontsContract.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/** @hide */
@VisibleForTesting
public static @Nullable ProviderInfo getProvider(
        PackageManager packageManager, FontRequest request) throws NameNotFoundException {
    String providerAuthority = request.getProviderAuthority();
    ProviderInfo info = packageManager.resolveContentProvider(providerAuthority, 0);
    if (info == null) {
        throw new NameNotFoundException("No package found for authority: " + providerAuthority);
    }

    if (!info.packageName.equals(request.getProviderPackage())) {
        throw new NameNotFoundException("Found content provider " + providerAuthority
                + ", but package was not " + request.getProviderPackage());
    }
    // Trust system apps without signature checks
    if (info.applicationInfo.isSystemApp()) {
        return info;
    }

    List<byte[]> signatures;
    PackageInfo packageInfo = packageManager.getPackageInfo(info.packageName,
            PackageManager.GET_SIGNATURES);
    signatures = convertToByteArrayList(packageInfo.signatures);
    Collections.sort(signatures, sByteArrayComparator);

    List<List<byte[]>> requestCertificatesList = request.getCertificates();
    for (int i = 0; i < requestCertificatesList.size(); ++i) {
        // Make a copy so we can sort it without modifying the incoming data.
        List<byte[]> requestSignatures = new ArrayList<>(requestCertificatesList.get(i));
        Collections.sort(requestSignatures, sByteArrayComparator);
        if (equalsByteArrayList(signatures, requestSignatures)) {
            return info;
        }
    }
    return null;
}
 
Example 2
Source File: WhitelistCheck.java    From flutter_whatsapp_stickers with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static boolean isWhitelistedFromProvider(@NonNull Context context, @NonNull String identifier, String whatsappPackageName) {
    final PackageManager packageManager = context.getPackageManager();

    if (isPackageInstalled(whatsappPackageName, packageManager)) {
        final String whatsAppProviderAuthority = whatsappPackageName + CONTENT_PROVIDER;
        final ProviderInfo providerInfo = packageManager.resolveContentProvider(whatsAppProviderAuthority, PackageManager.GET_META_DATA);

        // provider is not there. The WhatsApp app may be an old version.
        if (providerInfo == null) {
            return false;
        }

        final Uri queryUri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
                .authority(whatsAppProviderAuthority)
                .appendPath(QUERY_PATH)
                .appendQueryParameter(AUTHORITY_QUERY_PARAM, WhatsAppStickersPlugin.getContentProviderAuthority(context))
                .appendQueryParameter(IDENTIFIER_QUERY_PARAM, identifier).build();

        try (final Cursor cursor = context.getContentResolver().query(queryUri, null, null, null, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                final int whiteListResult = cursor.getInt(cursor.getColumnIndexOrThrow(QUERY_RESULT_COLUMN_NAME));

                return whiteListResult == 1;
            }
        }
    } else {
        // if app is not installed, then don't need to take into its whitelist info into account.
        return true;
    }
    return false;
}
 
Example 3
Source File: Validate.java    From kognitivo with Apache License 2.0 5 votes vote down vote up
public static void hasContentProvider(Context context) {
    Validate.notNull(context, "context");
    String appId = Validate.hasAppID();
    PackageManager pm = context.getPackageManager();
    if (pm != null) {
        String providerName = CONTENT_PROVIDER_BASE + appId;
        if (pm.resolveContentProvider(providerName, 0) == null) {
            throw new IllegalStateException(
                    String.format(CONTENT_PROVIDER_NOT_FOUND_REASON, providerName));
        }
    }
}
 
Example 4
Source File: IconUtils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static Drawable loadPackageIcon(Context context, String authority, int icon) {
    if (icon != 0) {
        if (authority != null) {
            final PackageManager pm = context.getPackageManager();
            final ProviderInfo info = pm.resolveContentProvider(authority, 0);
            if (info != null) {
                return pm.getDrawable(info.packageName, icon, info.applicationInfo);
            }
        } else {
            return ContextCompat.getDrawable(context, icon);
        }
    }
    return null;
}
 
Example 5
Source File: IconUtils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static Drawable loadPackageIcon(Context context, String authority, int icon) {
    if (icon != 0) {
        if (authority != null) {
            final PackageManager pm = context.getPackageManager();
            final ProviderInfo info = pm.resolveContentProvider(authority, 0);
            if (info != null) {
                return pm.getDrawable(info.packageName, icon, info.applicationInfo);
            }
        } else {
            return ContextCompat.getDrawable(context, icon);
        }
    }
    return null;
}
 
Example 6
Source File: IconUtils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static Drawable loadPackageIcon(Context context, String authority, int icon) {
    if (icon != 0) {
        if (authority != null) {
            final PackageManager pm = context.getPackageManager();
            final ProviderInfo info = pm.resolveContentProvider(authority, 0);
            if (info != null) {
                return pm.getDrawable(info.packageName, icon, info.applicationInfo);
            }
        } else {
            return ContextCompat.getDrawable(context, icon);
        }
    }
    return null;
}
 
Example 7
Source File: AddContentApi.java    From ankihelper with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the AnkiDroid package name that the API will communicate with.
 * This can be used to check that a supported version of AnkiDroid is installed,
 * or to get the application label and icon, etc.
 * @param context a Context that can be used to get the PackageManager
 * @return packageId of AnkiDroid if a supported version is not installed, otherwise null
 */
public static String getAnkiDroidPackageName(Context context) {
    PackageManager manager = context.getPackageManager();
    ProviderInfo pi = manager.resolveContentProvider(FlashCardsContract.AUTHORITY, 0);
    if (pi != null) {
        return pi.packageName;
    } else {
        return null;
    }
}
 
Example 8
Source File: SystemAppsManager.java    From island with Apache License 2.0 5 votes vote down vote up
/** @param flags intersection of {@code PackageManager.ComponentInfoFlags} and {@code PackageManager.ResolveInfoFlags}. */
@OwnerUser @ProfileUser public static Set<String> detectCriticalSystemPackages(final PackageManager pm, final int flags) {
	final Stopwatch stopwatch = Performances.startUptimeStopwatch();

	final Set<String> critical_sys_pkgs = new HashSet<>(sCriticalSystemPkgs);

	// Detect package names for critical intent actions, as an addition to the white-list of well-known ones.
	for (final Intent intent : sCriticalActivityIntents) {
		@SuppressLint("WrongConstant") final ResolveInfo info = pm.resolveActivity(intent, MATCH_DEFAULT_ONLY | Hacks.MATCH_ANY_USER_AND_UNINSTALLED);
		if (info == null || (info.activityInfo.applicationInfo.flags & FLAG_SYSTEM) == 0) continue;
		final String pkg = info.activityInfo.packageName;
		Log.i(TAG, "Critical package for " + intent + ": " + pkg);
		critical_sys_pkgs.add(pkg);
	}
	Performances.check(stopwatch, 1, "CriticalActivities");

	// Detect package names for critical content providers, as an addition to the white-list of well-known ones.
	for (final String authority : sCriticalContentAuthorities) {
		if (authority == null) continue;		// Nullable for version-specific authorities
		final ProviderInfo provider = pm.resolveContentProvider(authority, flags);
		if (provider == null || (provider.applicationInfo.flags & FLAG_SYSTEM) == 0 || (provider.flags & FLAG_SINGLE_USER) != 0) continue;
		Log.i(TAG, "Critical package for authority \"" + authority + "\": " + provider.packageName);
		critical_sys_pkgs.add(provider.packageName);
	}
	Performances.check(stopwatch, 1, "CriticalProviders");

	return critical_sys_pkgs;
}
 
Example 9
Source File: ExternalStorageProviderProxy.java    From island with Apache License 2.0 5 votes vote down vote up
static @Nullable ProviderInfo findTargetProvider(final Context context) {
	final PackageManager pm = context.getPackageManager();
	final ProviderInfo provider = pm.resolveContentProvider(TARGET_AUTHORITY, 0);
	final String error_event = "esp_error";
	if (provider != null) {
		if (! provider.exported) Analytics.$().event(error_event).with(ITEM_CATEGORY, "not exported").send();
		else if (! MANAGE_DOCUMENTS.equals(provider.readPermission))
			Analytics.$().event(error_event).with(ITEM_CATEGORY, "permission").with(ITEM_ID, provider.readPermission).send();
		else return provider;
	}

	// In case authority cannot be found, try enumerating documents providers in package of its well-known name. (Fixed authority name is unnecessary for documents provider)
	final List<ResolveInfo> resolves = pm.queryIntentContentProviders(new Intent(PROVIDER_INTERFACE).setPackage(TARGET_PACKAGE), 0);
	if (resolves == null || resolves.isEmpty()) {
		Log.e(TAG, "Documents provider for external storage is missing.");
		return null;
	}
	final ResolveInfo first_resolve = resolves.get(0);
	if (resolves.size() > 1) {
		Log.w(TAG, "Potential providers: " + resolves);
		for (final ResolveInfo resolve : resolves) {
			final String authority = resolve.providerInfo.authority;
			final String info = resolve.toString();
			Analytics.$().event(error_event).with(ITEM_CATEGORY, "indistinguishable").with(ITEM_ID, authority + ":" + info).send();
			Log.w(TAG, ">> " + authority + ": " + info);
		}
	} else Analytics.$().event(error_event).with(ITEM_CATEGORY, "authority").with(ITEM_ID, first_resolve.providerInfo.authority).send();

	return first_resolve.providerInfo;
}
 
Example 10
Source File: SearchableInfo.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor
 * 
 * Given a ComponentName, get the searchability info
 * and build a local copy of it.  Use the factory, not this.
 * 
 * @param activityContext runtime context for the activity that the searchable info is about.
 * @param attr The attribute set we found in the XML file, contains the values that are used to
 * construct the object.
 * @param cName The component name of the searchable activity
 * @throws IllegalArgumentException if the searchability info is invalid or insufficient
 */
private SearchableInfo(Context activityContext, AttributeSet attr, final ComponentName cName) {
    mSearchActivity = cName;
    
    TypedArray a = activityContext.obtainStyledAttributes(attr,
            com.android.internal.R.styleable.Searchable);
    mSearchMode = a.getInt(com.android.internal.R.styleable.Searchable_searchMode, 0);
    mLabelId = a.getResourceId(com.android.internal.R.styleable.Searchable_label, 0);
    mHintId = a.getResourceId(com.android.internal.R.styleable.Searchable_hint, 0);
    mIconId = a.getResourceId(com.android.internal.R.styleable.Searchable_icon, 0);
    mSearchButtonText = a.getResourceId(
            com.android.internal.R.styleable.Searchable_searchButtonText, 0);
    mSearchInputType = a.getInt(com.android.internal.R.styleable.Searchable_inputType, 
            InputType.TYPE_CLASS_TEXT |
            InputType.TYPE_TEXT_VARIATION_NORMAL);
    mSearchImeOptions = a.getInt(com.android.internal.R.styleable.Searchable_imeOptions, 
            EditorInfo.IME_ACTION_GO);
    mIncludeInGlobalSearch = a.getBoolean(
            com.android.internal.R.styleable.Searchable_includeInGlobalSearch, false);
    mQueryAfterZeroResults = a.getBoolean(
            com.android.internal.R.styleable.Searchable_queryAfterZeroResults, false);
    mAutoUrlDetect = a.getBoolean(
            com.android.internal.R.styleable.Searchable_autoUrlDetect, false);

    mSettingsDescriptionId = a.getResourceId(
            com.android.internal.R.styleable.Searchable_searchSettingsDescription, 0);
    mSuggestAuthority = a.getString(
            com.android.internal.R.styleable.Searchable_searchSuggestAuthority);
    mSuggestPath = a.getString(
            com.android.internal.R.styleable.Searchable_searchSuggestPath);
    mSuggestSelection = a.getString(
            com.android.internal.R.styleable.Searchable_searchSuggestSelection);
    mSuggestIntentAction = a.getString(
            com.android.internal.R.styleable.Searchable_searchSuggestIntentAction);
    mSuggestIntentData = a.getString(
            com.android.internal.R.styleable.Searchable_searchSuggestIntentData);
    mSuggestThreshold = a.getInt(
            com.android.internal.R.styleable.Searchable_searchSuggestThreshold, 0);

    mVoiceSearchMode = 
        a.getInt(com.android.internal.R.styleable.Searchable_voiceSearchMode, 0);
    // TODO this didn't work - came back zero from YouTube
    mVoiceLanguageModeId = 
        a.getResourceId(com.android.internal.R.styleable.Searchable_voiceLanguageModel, 0);
    mVoicePromptTextId = 
        a.getResourceId(com.android.internal.R.styleable.Searchable_voicePromptText, 0);
    mVoiceLanguageId = 
        a.getResourceId(com.android.internal.R.styleable.Searchable_voiceLanguage, 0);
    mVoiceMaxResults = 
        a.getInt(com.android.internal.R.styleable.Searchable_voiceMaxResults, 0);

    a.recycle();

    // get package info for suggestions provider (if any)
    String suggestProviderPackage = null;
    if (mSuggestAuthority != null) {
        PackageManager pm = activityContext.getPackageManager();
        ProviderInfo pi = pm.resolveContentProvider(mSuggestAuthority,
                PackageManager.MATCH_DEBUG_TRIAGED_MISSING);
        if (pi != null) {
            suggestProviderPackage = pi.packageName;
        }
    }
    mSuggestProviderPackage = suggestProviderPackage;

    // for now, implement some form of rules - minimal data
    if (mLabelId == 0) {
        throw new IllegalArgumentException("Search label must be a resource reference.");
    }
}
 
Example 11
Source File: APEZProvider.java    From play-apk-expansion with Apache License 2.0 4 votes vote down vote up
private boolean initIfNecessary() {
    if (!mInit) {
        Context ctx = getContext();
        PackageManager pm = ctx.getPackageManager();
        ProviderInfo pi = pm.resolveContentProvider(getAuthority(),
                PackageManager.GET_META_DATA);
        PackageInfo packInfo;
        try {
            packInfo = pm.getPackageInfo(ctx.getPackageName(), 0);
        } catch (NameNotFoundException e1) {
            e1.printStackTrace();
            return false;
        }
        int patchFileVersion;
        int mainFileVersion;
        int appVersionCode = packInfo.versionCode;
        String[] resourceFiles = null;
        if (null != pi.metaData) {
            mainFileVersion = pi.metaData.getInt("mainVersion", appVersionCode);
            patchFileVersion = pi.metaData.getInt("patchVersion", appVersionCode);
            String mainFileName = pi.metaData.getString("mainFilename");
            if (null != mainFileName) {
                String patchFileName = pi.metaData.getString("patchFilename");
                if (null != patchFileName) {
                    resourceFiles = new String[] {
                            mainFileName, patchFileName
                    };
                } else {
                    resourceFiles = new String[] {
                            mainFileName
                    };
                }
            }
        } else {
            mainFileVersion = patchFileVersion = appVersionCode;
        }
        try {
            if (null == resourceFiles) {
                mAPKExtensionFile = APKExpansionSupport.getAPKExpansionZipFile(ctx,
                        mainFileVersion, patchFileVersion);
            } else {
                mAPKExtensionFile = APKExpansionSupport.getResourceZipFile(resourceFiles);
            }
            mInit = true;
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}
 
Example 12
Source File: APEZProvider.java    From glide-support with The Unlicense 4 votes vote down vote up
private boolean initIfNecessary() {
    if ( !mInit ) {
           Context ctx = getContext();
           PackageManager pm = ctx.getPackageManager();
           ProviderInfo pi = pm.resolveContentProvider(getAuthority(), PackageManager.GET_META_DATA);
           PackageInfo packInfo;
           try {
               packInfo = pm.getPackageInfo(ctx.getPackageName(), 0);
           } catch (NameNotFoundException e1) {
               e1.printStackTrace();
               return false;
           }
           int patchFileVersion;
           int mainFileVersion;
           int appVersionCode = packInfo.versionCode;
           String[] resourceFiles = null;
           if ( null != pi.metaData ) {
               mainFileVersion = pi.metaData.getInt("mainVersion", appVersionCode);
               patchFileVersion = pi.metaData.getInt("patchVersion", appVersionCode);
            String mainFileName = pi.metaData.getString("mainFilename");
            mainFileName = mainFileName != null? mainFileName : NO_FILE;
            if ( NO_FILE != mainFileName ) {
                String patchFileName = pi.metaData.getString("patchFilename");
                patchFileName = patchFileName != null? patchFileName : NO_FILE;
	            if ( NO_FILE != patchFileName ) {
                       resourceFiles = new String[] { mainFileName, patchFileName };
                   } else {
                       resourceFiles = new String[] { mainFileName };
                   }
               }
           } else {
               mainFileVersion = patchFileVersion = appVersionCode;
           }
           try {
               if ( null == resourceFiles ) {
                   mAPKExtensionFile = APKExpansionSupport.getAPKExpansionZipFile(ctx, mainFileVersion, patchFileVersion);
               } else {
                   mAPKExtensionFile = APKExpansionSupport.getResourceZipFile(resourceFiles);
               }
               mInit = true;
               return true;
           } catch (IOException e) {
               e.printStackTrace();                
           }
    }
       return false;	    
}
 
Example 13
Source File: APEZProvider.java    From Alite with GNU General Public License v3.0 4 votes vote down vote up
private boolean initIfNecessary() {
    if ( !mInit ) {
           Context ctx = getContext();
           PackageManager pm = ctx.getPackageManager();
           ProviderInfo pi = pm.resolveContentProvider(getAuthority(), PackageManager.GET_META_DATA);
           PackageInfo packInfo;
           try {
               packInfo = pm.getPackageInfo(ctx.getPackageName(), 0);
           } catch (NameNotFoundException e1) {
               e1.printStackTrace();
               return false;
           }
           int patchFileVersion;
           int mainFileVersion;
           int appVersionCode = packInfo.versionCode;
           String[] resourceFiles = null;
           if ( null != pi.metaData ) {
               mainFileVersion = pi.metaData.getInt("mainVersion", appVersionCode);
               patchFileVersion = pi.metaData.getInt("patchVersion", appVersionCode);
               String mainFileName = pi.metaData.getString("mainFilename", NO_FILE);
               if ( NO_FILE != mainFileName ) {
                   String patchFileName = pi.metaData.getString("patchFilename", NO_FILE);
                   if ( NO_FILE != patchFileName ) {
                       resourceFiles = new String[] { mainFileName, patchFileName };
                   } else {
                       resourceFiles = new String[] { mainFileName };
                   }
               }
           } else {
               mainFileVersion = patchFileVersion = appVersionCode;
           }
           try {
               if ( null == resourceFiles ) {
                   mAPKExtensionFile = APKExpansionSupport.getAPKExpansionZipFile(ctx, mainFileVersion, patchFileVersion);
               } else {
                   mAPKExtensionFile = APKExpansionSupport.getResourceZipFile(resourceFiles);
               }
               mInit = true;
               return true;
           } catch (IOException e) {
               e.printStackTrace();                
           }
    }
       return false;	    
}
 
Example 14
Source File: APEZProvider.java    From react-native-video with MIT License 4 votes vote down vote up
private boolean initIfNecessary() {
    if ( !mInit ) {
           Context ctx = getContext();
           PackageManager pm = ctx.getPackageManager();
           ProviderInfo pi = pm.resolveContentProvider(getAuthority(), PackageManager.GET_META_DATA);
           PackageInfo packInfo;
           try {
               packInfo = pm.getPackageInfo(ctx.getPackageName(), 0);
           } catch (NameNotFoundException e1) {
               e1.printStackTrace();
               return false;
           }
           int patchFileVersion;
           int mainFileVersion;
           int appVersionCode = packInfo.versionCode;
           String[] resourceFiles = null;
           if ( null != pi.metaData ) {
               mainFileVersion = pi.metaData.getInt("mainVersion", appVersionCode);
               patchFileVersion = pi.metaData.getInt("patchVersion", appVersionCode);
               String mainFileName = pi.metaData.getString("mainFilename", NO_FILE);
               if ( NO_FILE != mainFileName ) {
                   String patchFileName = pi.metaData.getString("patchFilename", NO_FILE);
                   if ( NO_FILE != patchFileName ) {
                       resourceFiles = new String[] { mainFileName, patchFileName };
                   } else {
                       resourceFiles = new String[] { mainFileName };
                   }
               }
           } else {
               mainFileVersion = patchFileVersion = appVersionCode;
           }
           try {
               if ( null == resourceFiles ) {
                   mAPKExtensionFile = APKExpansionSupport.getAPKExpansionZipFile(ctx, mainFileVersion, patchFileVersion);
               } else {
                   mAPKExtensionFile = APKExpansionSupport.getResourceZipFile(resourceFiles);
               }
               mInit = true;
               return true;
           } catch (IOException e) {
               e.printStackTrace();                
           }
    }
       return false;	    
}