Java Code Examples for android.content.IntentFilter#addDataSchemeSpecificPart()

The following examples show how to use android.content.IntentFilter#addDataSchemeSpecificPart() . 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: Vpn.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void maybeRegisterPackageChangeReceiverLocked(String packageName) {
    // Unregister IntentFilter listening for previous always-on package change
    unregisterPackageChangeReceiverLocked();

    if (!isNullOrLegacyVpn(packageName)) {
        mIsPackageIntentReceiverRegistered = true;

        IntentFilter intentFilter = new IntentFilter();
        // Protected intent can only be sent by system. No permission required in register.
        intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        intentFilter.addDataScheme("package");
        intentFilter.addDataSchemeSpecificPart(packageName, PatternMatcher.PATTERN_LITERAL);
        mContext.registerReceiverAsUser(
                mPackageIntentReceiver, UserHandle.of(mUserHandle), intentFilter, null, null);
    }
}
 
Example 2
Source File: PackageTrackerIntentHelperImpl.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(String updaterAppPackageName, String dataAppPackageName,
        PackageTracker packageTracker) {
    mUpdaterAppPackageName = updaterAppPackageName;

    // Register for events of interest.

    // The intent filter that triggers when package update events happen that indicate there may
    // be work to do.
    IntentFilter packageIntentFilter = new IntentFilter();

    packageIntentFilter.addDataScheme("package");
    packageIntentFilter.addDataSchemeSpecificPart(
            updaterAppPackageName, PatternMatcher.PATTERN_LITERAL);
    packageIntentFilter.addDataSchemeSpecificPart(
            dataAppPackageName, PatternMatcher.PATTERN_LITERAL);

    // ACTION_PACKAGE_ADDED is fired when a package is upgraded or downgraded (in addition to
    // ACTION_PACKAGE_REMOVED and ACTION_PACKAGE_REPLACED). A system/priv-app can never be
    // removed entirely so we do not need to trigger on ACTION_PACKAGE_REMOVED or
    // ACTION_PACKAGE_FULLY_REMOVED.
    packageIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);

    // ACTION_PACKAGE_CHANGED is used when a package is disabled / re-enabled. It is not
    // strictly necessary to trigger on this but it won't hurt anything and may catch some cases
    // where a package has changed while disabled.
    // Note: ACTION_PACKAGE_CHANGED is not fired when updating a suspended app, but
    // ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and ACTION_PACKAGE_REPLACED are (and the app
    // is left in an unsuspended state after this).
    packageIntentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);

    // We do not register for ACTION_PACKAGE_RESTARTED because it doesn't imply an update.
    // We do not register for ACTION_PACKAGE_DATA_CLEARED because the updater / data apps are
    // not expected to need local data.

    Receiver packageUpdateReceiver = new Receiver(packageTracker);
    mContext.registerReceiverAsUser(
            packageUpdateReceiver, UserHandle.SYSTEM, packageIntentFilter,
            null /* broadcastPermission */, null /* default handler */);
}
 
Example 3
Source File: MediaUpdateService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void registerBroadcastReceiver() {
    BroadcastReceiver updateReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_SYSTEM)
                        != UserHandle.USER_SYSTEM) {
                    // Ignore broadcast for non system users. We don't want to update system
                    // service multiple times.
                    return;
                }
                switch (intent.getAction()) {
                    case Intent.ACTION_PACKAGE_REMOVED:
                        if (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)) {
                            // The existing package is updated. Will be handled with the
                            // following ACTION_PACKAGE_ADDED case.
                            return;
                        }
                        packageStateChanged();
                        break;
                    case Intent.ACTION_PACKAGE_CHANGED:
                        packageStateChanged();
                        break;
                    case Intent.ACTION_PACKAGE_ADDED:
                        packageStateChanged();
                        break;
                }
            }
    };
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");
    filter.addDataSchemeSpecificPart(MEDIA_UPDATE_PACKAGE_NAME, PatternMatcher.PATTERN_LITERAL);

    getContext().registerReceiverAsUser(updateReceiver, UserHandle.ALL, filter,
            null /* broadcast permission */, null /* handler */);
}
 
Example 4
Source File: WebViewUpdateService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public void onStart() {
    mWebViewUpdatedReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
                switch (intent.getAction()) {
                    case Intent.ACTION_PACKAGE_REMOVED:
                        // When a package is replaced we will receive two intents, one
                        // representing the removal of the old package and one representing the
                        // addition of the new package.
                        // In the case where we receive an intent to remove the old version of
                        // the package that is being replaced we early-out here so that we don't
                        // run the update-logic twice.
                        if (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)) return;
                        mImpl.packageStateChanged(packageNameFromIntent(intent),
                                PACKAGE_REMOVED, userId);
                        break;
                    case Intent.ACTION_PACKAGE_CHANGED:
                        // Ensure that we only heed PACKAGE_CHANGED intents if they change an
                        // entire package, not just a component
                        if (entirePackageChanged(intent)) {
                            mImpl.packageStateChanged(packageNameFromIntent(intent),
                                    PACKAGE_CHANGED, userId);
                        }
                        break;
                    case Intent.ACTION_PACKAGE_ADDED:
                        mImpl.packageStateChanged(packageNameFromIntent(intent),
                                (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)
                                 ? PACKAGE_ADDED_REPLACED : PACKAGE_ADDED), userId);
                        break;
                    case Intent.ACTION_USER_STARTED:
                        mImpl.handleNewUser(userId);
                        break;
                    case Intent.ACTION_USER_REMOVED:
                        mImpl.handleUserRemoved(userId);
                        break;
                }
            }
    };
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");
    // Make sure we only receive intents for WebView packages from our config file.
    for (WebViewProviderInfo provider : mImpl.getWebViewPackages()) {
        filter.addDataSchemeSpecificPart(provider.packageName, PatternMatcher.PATTERN_LITERAL);
    }

    getContext().registerReceiverAsUser(mWebViewUpdatedReceiver, UserHandle.ALL, filter,
            null /* broadcast permission */, null /* handler */);

    IntentFilter userAddedFilter = new IntentFilter();
    userAddedFilter.addAction(Intent.ACTION_USER_STARTED);
    userAddedFilter.addAction(Intent.ACTION_USER_REMOVED);
    getContext().registerReceiverAsUser(mWebViewUpdatedReceiver, UserHandle.ALL,
            userAddedFilter, null /* broadcast permission */, null /* handler */);

    publishBinderService("webviewupdate", new BinderService(), true /*allowIsolated*/);
}