Java Code Examples for android.content.Intent#ACTION_PACKAGE_ADDED

The following examples show how to use android.content.Intent#ACTION_PACKAGE_ADDED . These examples are extracted from open source projects. 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
static private ChangelogTask getEventTask(Context context, String packageName, String action) {
    App app = YalpStoreApplication.installedPackages.get(packageName);
    if (null == app) {
        app = new App();
        app.setPackageInfo(new PackageInfo());
        app.getPackageInfo().packageName = packageName;
    }
    ChangelogTask task = new ChangelogTask();
    task.setContext(context);
    task.setApp(app);
    switch (action) {
        case ACTION_PACKAGE_INSTALLATION_FAILED:
            task.setEventType(Event.TYPE.INSTALLATION, false);
            break;
        case Intent.ACTION_PACKAGE_FULLY_REMOVED:
        case Intent.ACTION_PACKAGE_REMOVED:
            task.setEventType(Event.TYPE.REMOVAL, true);
            break;
        case Intent.ACTION_PACKAGE_INSTALL:
        case Intent.ACTION_PACKAGE_ADDED:
        case Intent.ACTION_PACKAGE_REPLACED:
            task.setEventType(app.getInstalledVersionCode() > 0 ? Event.TYPE.UPDATE : Event.TYPE.INSTALLATION, true);
            break;
    }
    return task;
}
 
Example 2
@Override protected void onHandleIntent(Intent intent) {
  if (intent != null) {
    final String action = intent.getAction();
    final String packageName = intent.getData()
        .getEncodedSchemeSpecificPart();

    if (!TextUtils.equals(action, Intent.ACTION_PACKAGE_REPLACED) && intent.getBooleanExtra(
        Intent.EXTRA_REPLACING, false)) {
      // do nothing if its a replacement ongoing. we are only interested in
      // already replaced apps
      return;
    }

    switch (action) {
      case Intent.ACTION_PACKAGE_ADDED:
        onPackageAdded(packageName);
        break;
      case Intent.ACTION_PACKAGE_REPLACED:
        onPackageReplaced(packageName);
        break;
      case Intent.ACTION_PACKAGE_REMOVED:
        onPackageRemoved(packageName);
        break;
    }
  }
}
 
Example 3
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 Project: DevUtils   File: AppStateReceiver.java    License: Apache License 2.0 5 votes vote down vote up
@Override
    public void onReceive(Context context, Intent intent) {
        try {
            String action = intent.getAction();
            // 打印当前触发的广播
            LogPrintUtils.dTag(TAG, "onReceive Action: " + action);
            // 被操作应用包名
            String packageName = null;
            Uri uri = intent.getData();
            if (uri != null) {
//                packageName = uri.toString();
                packageName = uri.getEncodedSchemeSpecificPart();
            }
            // 判断类型
            switch (action) {
                case Intent.ACTION_PACKAGE_ADDED: // 应用安装
                    if (sListener != null) {
                        sListener.onAdded(packageName);
                    }
                    break;
                case Intent.ACTION_PACKAGE_REPLACED: // 应用更新
                    if (sListener != null) {
                        sListener.onReplaced(packageName);
                    }
                    break;
                case Intent.ACTION_PACKAGE_REMOVED: // 应用卸载
                    if (sListener != null) {
                        sListener.onRemoved(packageName);
                    }
                    break;
            }
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "onReceive");
        }
    }
 
Example 5
protected ShellSaiPackageInstaller(Context c) {
    super(c);

    mWorkerThread.start();
    mWorkerHandler = new Handler(mWorkerThread.getLooper());

    IntentFilter packageAddedFilter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    packageAddedFilter.addDataScheme("package");
    getContext().registerReceiver(mPackageInstalledBroadcastReceiver, packageAddedFilter, null, mWorkerHandler);
}
 
Example 6
public PackageIntentReceiver(MainLoader loader) {
    mLoader = loader;
    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");
    mLoader.getContext().registerReceiver(this, filter);
    // Register for events related to sdcard installation.
    IntentFilter sdFilter = new IntentFilter();
    sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
    sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
    filter.addAction(Intent.ACTION_LOCALE_CHANGED);
    mLoader.getContext().registerReceiver(this, sdFilter);
}
 
Example 7
@Override
public void onReceive(Context context, final Intent intent) {
    String status = null;
    ApplicationStatus applicationState;
    switch (intent.getAction()) {
        case Intent.ACTION_PACKAGE_ADDED:
            status = "added";
            break;
        case Intent.ACTION_PACKAGE_REMOVED:
            status = "removed";
            break;
        case Intent.ACTION_PACKAGE_REPLACED:
            status = "upgraded";
            break;
        case Intent.ACTION_PACKAGE_DATA_CLEARED:
            status = "dataCleared";
            break;
        default:
            Log.i(TAG, "Invalid intent received");
    }
    if (status != null) {
        String packageName = intent.getData().getEncodedSchemeSpecificPart();
        applicationState = new ApplicationStatus();
        applicationState.setState(status);
        applicationState.setPackageName(packageName);
        try {
            String appState = CommonUtils.toJSON(applicationState);
            publishEvent(appState, Constants.EventListeners.APPLICATION_STATE);
            if (Constants.DEBUG_MODE_ENABLED) {
                Log.d(TAG, appState);
            }
        } catch (AndroidAgentException e) {
            Log.e(TAG, "Could not convert to JSON");
        }
    }
}
 
Example 8
public ApplicationsReceiver(AppsLoader loader) {
    mLoader = loader;
    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");
    mLoader.getContext().registerReceiver(this, filter);
    IntentFilter sdFilter = new IntentFilter();
    sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
    sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
    mLoader.getContext().registerReceiver(this, sdFilter);
}
 
Example 9
Source Project: V.FlyoutTest   File: LoaderCustomSupport.java    License: MIT License 5 votes vote down vote up
public PackageIntentReceiver(AppListLoader loader) {
    mLoader = loader;
    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");
    mLoader.getContext().registerReceiver(this, filter);
    // Register for events related to sdcard installation.
    IntentFilter sdFilter = new IntentFilter();
    sdFilter.addAction(IntentCompat.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
    sdFilter.addAction(IntentCompat.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
    mLoader.getContext().registerReceiver(this, sdFilter);
}
 
Example 10
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction() == null) {
        return;
    }
    switch (intent.getAction()) {
        case Intent.ACTION_PACKAGE_ADDED:
            if (!MLPreferences.getPreferences(context).getBoolean(Common.NEW_APP_NOTIFICATION, true) ||
                    intent.getBooleanExtra(Intent.EXTRA_REPLACING, false) ||
                    intent.getData() == null || intent.getData().getSchemeSpecificPart().length() == 0) {
                return;
            }
            String newPackageName = intent.getData().getSchemeSpecificPart();
            if (context.getPackageManager().getLaunchIntentForPackage(newPackageName) != null) {
                NotificationHelper.postAppInstalledNotification(context, newPackageName);
            }
            break;
        case MAXLOCK_ACTION_LOCK_APP:
            String lockPackageName = intent.getStringExtra(EXTRA_PACKAGE_NAME);
            if (lockPackageName != null) {
                MLPreferences.getPrefsApps(context).edit().putBoolean(lockPackageName, true).apply();
                Toast.makeText(context, R.string.toast_lock_new_app_locked_successfully, Toast.LENGTH_SHORT).show();
            }
            NotificationManagerCompat.from(context).cancel(APP_INSTALLED_NOTIFICATION_ID);
            break;
        case MAXLOCK_ACTION_NEVER_SHOW_AGAIN:
            MLPreferences.getPreferences(context).edit().putBoolean(Common.NEW_APP_NOTIFICATION, false).apply();
            NotificationManagerCompat.from(context).cancel(APP_INSTALLED_NOTIFICATION_ID);
            break;
        case Intent.ACTION_PACKAGE_FULLY_REMOVED:
            if (intent.getData() != null && intent.getData().getSchemeSpecificPart().length() > 0) {
                MLPreferences.getPrefsApps(context).edit().remove(intent.getData().getSchemeSpecificPart()).apply();
            }
            break;
    }
}
 
Example 11
public PackageIntentReceiver(AppListLoader loader) {
    mLoader = loader;
    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");
    mLoader.getContext().registerReceiver(this, filter);
    // Register for events related to sdcard installation.
    IntentFilter sdFilter = new IntentFilter();
    sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
    sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
    mLoader.getContext().registerReceiver(this, sdFilter);
}
 
Example 12
public PackageIntentReceiver(SecretCodeLoader loader) {
    mLoader = loader;
    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");
    mLoader.getContext().registerReceiver(this, filter);
}
 
Example 13
public PackageIntentReceiver(AppListLoader loader) {
    mLoader = loader;
    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");
    mLoader.getContext().registerReceiver(this, filter);
    // Register for events related to sdcard installation.
    IntentFilter sdFilter = new IntentFilter();
    sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
    sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
    mLoader.getContext().registerReceiver(this, sdFilter);
}
 
Example 14
@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*/);
}
 
Example 15
@Override
public void onReceive(Context context, Intent intent) {
    final int userId  = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
    if (userId == UserHandle.USER_NULL) {
        Slog.w(TAG, "Intent broadcast does not contain user handle: " + intent);
        return;
    }

    final String action = intent.getAction();

    // This is normally called on Handler, so clearCallingIdentity() isn't needed,
    // but we still check it in unit tests.
    final long token = injectClearCallingIdentity();
    try {
        synchronized (mLock) {
            if (!isUserUnlockedL(userId)) {
                if (DEBUG) {
                    Slog.d(TAG, "Ignoring package broadcast " + action
                            + " for locked/stopped user " + userId);
                }
                return;
            }

            // Whenever we get one of those package broadcasts, or get
            // ACTION_PREFERRED_ACTIVITY_CHANGED, we purge the default launcher cache.
            final ShortcutUser user = getUserShortcutsLocked(userId);
            user.clearLauncher();
        }
        if (Intent.ACTION_PREFERRED_ACTIVITY_CHANGED.equals(action)) {
            // Nothing farther to do.
            return;
        }

        final Uri intentUri = intent.getData();
        final String packageName = (intentUri != null) ? intentUri.getSchemeSpecificPart()
                : null;
        if (packageName == null) {
            Slog.w(TAG, "Intent broadcast does not contain package name: " + intent);
            return;
        }

        final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);

        switch (action) {
            case Intent.ACTION_PACKAGE_ADDED:
                if (replacing) {
                    handlePackageUpdateFinished(packageName, userId);
                } else {
                    handlePackageAdded(packageName, userId);
                }
                break;
            case Intent.ACTION_PACKAGE_REMOVED:
                if (!replacing) {
                    handlePackageRemoved(packageName, userId);
                }
                break;
            case Intent.ACTION_PACKAGE_CHANGED:
                handlePackageChanged(packageName, userId);

                break;
            case Intent.ACTION_PACKAGE_DATA_CLEARED:
                handlePackageDataCleared(packageName, userId);
                break;
        }
    } catch (Exception e) {
        wtf("Exception in mPackageMonitor.onReceive", e);
    } finally {
        injectRestoreCallingIdentity(token);
    }
}
 
Example 16
protected ShellSAIPackageInstaller(Context c) {
    super(c);
    IntentFilter packageAddedFilter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    packageAddedFilter.addDataScheme("package");
    getContext().registerReceiver(mPackageInstalledBroadcastReceiver, packageAddedFilter);
}
 
Example 17
private void registerPackageInstallReceiver() {
    receiver = new BroadcastReceiver(){
        public void onReceive(Context context, Intent intent) {
            Log.d("Received intent: " + intent + " (" + intent.getExtras() + ")");
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
                long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                if (downloadId == enqueue) {
                    if (localFile.exists()) {
                        return;
                    }
                    Query query = new Query();
                    query.setFilterById(enqueue);
                    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {
                        hideProgress();
                        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
                        if (DownloadManager.STATUS_SUCCESSFUL == status) {
                            storeDownload(dm, downloadId);
                            installDownload();
                        } else {
                            int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
                            Toast.makeText(context,"Download failed (" + status + "): " + reason, Toast.LENGTH_LONG).show();
                        }
                    } else {
                        Toast.makeText(context,"Download diappeared!", Toast.LENGTH_LONG).show();
                    }
                    c.close();
                }
            } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
                if (intent.getData().toString().equals("package:org.ruboto.core")) {
                    Toast.makeText(context,"Ruboto Core is now installed.",Toast.LENGTH_LONG).show();
                    deleteFile(RUBOTO_APK);
                    if (receiver != null) {
                        unregisterReceiver(receiver);
                        receiver = null;
                    }
                    initJRuby(false);
                } else {
                    Toast.makeText(context,"Installed: " + intent.getData().toString(),Toast.LENGTH_LONG).show();
                }
            }
        }
        };
    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    filter.addDataScheme("package");
    registerReceiver(receiver, filter);
    IntentFilter download_filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    registerReceiver(receiver, download_filter);
}