@Override
 public void onReceive(Context context, Intent intent) {
   final String action = intent.getAction();
   if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
       || Intent.ACTION_PACKAGE_REMOVED.equals(action)
       || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
     Log.i(TAG, "action: " + action);
     final String packageName = intent.getData().getSchemeSpecificPart();
     final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
     int op = PackageUpdatedTask.OP_NONE;
     if (packageName == null || packageName.length() == 0) {
       // they sent us a bad intent
       return;
     }
     if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
       op = PackageUpdatedTask.OP_UPDATE;
     } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
       if (!replacing) {
         op = PackageUpdatedTask.OP_REMOVE;
       }
     } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
       if (!replacing) {
         op = PackageUpdatedTask.OP_ADD;
       } else {
         op = PackageUpdatedTask.OP_UPDATE;
       }
     }
     mHandler.post(new PackageUpdatedTask(op, new String[] {packageName}));
   } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
     Log.i(TAG, "ACTION_EXTERNAL_APPLICATIONS_AVAILABLE");
     String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
     int op = PackageUpdatedTask.OP_ADD;
     mHandler.post(new PackageUpdatedTask(op, packages));
   } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
     Log.i(TAG, "ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE");
     String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
     int op = PackageUpdatedTask.OP_REMOVE;
     mHandler.post(new PackageUpdatedTask(op, packages));
   } else if (Intent.ACTION_TIME_TICK.equals(action)
       || Intent.ACTION_TIME_CHANGED.equals(action)
       || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
     timeChanged();
   } else if (FavoritesModel.ACTION_FAVOTITE_UPDATE.equals(action)) {
     Log.i(TAG, "ACTION_FAVOTITE_UPDATE");
     FavoritesData.saveFavoritesToDatabase(mContext);
     FavoritesData.sort();
   }
 }
 @Override
 public void onReceive(Context context, Intent intent) {
   final String action = intent.getAction();
   if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
     mBatteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
     mBatteryStatus =
         intent.getIntExtra(
             BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
   } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)
       || Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
     setupNavigationItems();
   } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
     // Give up on screen off. what's the point in pie controls if you don't see them?
     if (isShowing()) {
       mPieContainer.exit();
     }
   }
 }
Пример #3
0
  @Override
  public void onReceive(Context context, Intent intent) {
    mChangeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
    if (mChangeUserId == UserHandle.USER_NULL) {
      throw new IllegalArgumentException(
          "Intent broadcast does not contain user handle: " + intent);
    }
    onBeginPackageChanges();

    mDisappearingPackages = mAppearingPackages = null;
    mSomePackagesChanged = false;

    String action = intent.getAction();
    if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
      String pkg = getPackageName(intent);
      int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
      // We consider something to have changed regardless of whether
      // this is just an update, because the update is now finished
      // and the contents of the package may have changed.
      mSomePackagesChanged = true;
      if (pkg != null) {
        mAppearingPackages = mTempArray;
        mTempArray[0] = pkg;
        if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
          mModifiedPackages = mTempArray;
          mChangeType = PACKAGE_UPDATING;
          onPackageUpdateFinished(pkg, uid);
          onPackageModified(pkg);
        } else {
          mChangeType = PACKAGE_PERMANENT_CHANGE;
          onPackageAdded(pkg, uid);
        }
        onPackageAppeared(pkg, mChangeType);
        if (mChangeType == PACKAGE_UPDATING) {
          synchronized (mUpdatingPackages) {
            mUpdatingPackages.remove(pkg);
          }
        }
      }
    } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
      String pkg = getPackageName(intent);
      int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
      if (pkg != null) {
        mDisappearingPackages = mTempArray;
        mTempArray[0] = pkg;
        if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
          mChangeType = PACKAGE_UPDATING;
          synchronized (mUpdatingPackages) {
            // not used for now
            // mUpdatingPackages.add(pkg);
          }
          onPackageUpdateStarted(pkg, uid);
        } else {
          mChangeType = PACKAGE_PERMANENT_CHANGE;
          // We only consider something to have changed if this is
          // not a replace; for a replace, we just need to consider
          // it when it is re-added.
          mSomePackagesChanged = true;
          onPackageRemoved(pkg, uid);
          if (intent.getBooleanExtra(Intent.EXTRA_REMOVED_FOR_ALL_USERS, false)) {
            onPackageRemovedAllUsers(pkg, uid);
          }
        }
        onPackageDisappeared(pkg, mChangeType);
      }
    } else if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
      String pkg = getPackageName(intent);
      int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
      String[] components = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST);
      if (pkg != null) {
        mModifiedPackages = mTempArray;
        mTempArray[0] = pkg;
        mChangeType = PACKAGE_PERMANENT_CHANGE;
        if (onPackageChanged(pkg, uid, components)) {
          mSomePackagesChanged = true;
        }
        onPackageModified(pkg);
      }
    } else if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) {
      mDisappearingPackages = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
      mChangeType = PACKAGE_TEMPORARY_CHANGE;
      boolean canRestart =
          onHandleForceStop(
              intent, mDisappearingPackages, intent.getIntExtra(Intent.EXTRA_UID, 0), false);
      if (canRestart) setResultCode(Activity.RESULT_OK);
    } else if (Intent.ACTION_PACKAGE_RESTARTED.equals(action)) {
      mDisappearingPackages = new String[] {getPackageName(intent)};
      mChangeType = PACKAGE_TEMPORARY_CHANGE;
      onHandleForceStop(
          intent, mDisappearingPackages, intent.getIntExtra(Intent.EXTRA_UID, 0), true);
    } else if (Intent.ACTION_UID_REMOVED.equals(action)) {
      onUidRemoved(intent.getIntExtra(Intent.EXTRA_UID, 0));
    } else if (Intent.ACTION_USER_STOPPED.equals(action)) {
      if (intent.hasExtra(Intent.EXTRA_USER_HANDLE)) {
        onHandleUserStop(intent, intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
      }
    } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
      String[] pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
      mAppearingPackages = pkgList;
      mChangeType = PACKAGE_TEMPORARY_CHANGE;
      mSomePackagesChanged = true;
      if (pkgList != null) {
        onPackagesAvailable(pkgList);
        for (int i = 0; i < pkgList.length; i++) {
          onPackageAppeared(pkgList[i], PACKAGE_TEMPORARY_CHANGE);
        }
      }
    } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
      String[] pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
      mDisappearingPackages = pkgList;
      mChangeType = PACKAGE_TEMPORARY_CHANGE;
      mSomePackagesChanged = true;
      if (pkgList != null) {
        onPackagesUnavailable(pkgList);
        for (int i = 0; i < pkgList.length; i++) {
          onPackageDisappeared(pkgList[i], PACKAGE_TEMPORARY_CHANGE);
        }
      }
    }

    if (mSomePackagesChanged) {
      onSomePackagesChanged();
    }

    onFinishPackageChanges();
    mChangeUserId = UserHandle.USER_NULL;
  }
    public void onReceive(Context context, Intent intent) {
      final String action = intent.getAction();
      final UserHandleCompat user = UserHandleCompat.myUserHandle();

      if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
          || Intent.ACTION_PACKAGE_REMOVED.equals(action)
          || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
        final String packageName = intent.getData().getSchemeSpecificPart();
        final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);

        if (packageName == null || packageName.length() == 0) {
          // they sent us a bad intent
          return;
        }
        if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
          for (OnAppsChangedCallbackCompat callback : getCallbacks()) {
            callback.onPackageChanged(packageName, user);
          }
        } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
          if (!replacing) {
            for (OnAppsChangedCallbackCompat callback : getCallbacks()) {
              callback.onPackageRemoved(packageName, user);
            }
          }
          // else, we are replacing the package, so a PACKAGE_ADDED will be sent
          // later, we will update the package at this time
        } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
          if (!replacing) {
            for (OnAppsChangedCallbackCompat callback : getCallbacks()) {
              callback.onPackageAdded(packageName, user);
            }
          } else {
            for (OnAppsChangedCallbackCompat callback : getCallbacks()) {
              callback.onPackageChanged(packageName, user);
            }
          }
        }
      } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
        // EXTRA_REPLACING is available Kitkat onwards. For lower devices, it is broadcasted
        // when moving a package or mounting/un-mounting external storage. Assume that
        // it is a replacing operation.
        final boolean replacing =
            intent.getBooleanExtra(
                Intent.EXTRA_REPLACING, Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT);
        String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
        for (OnAppsChangedCallbackCompat callback : getCallbacks()) {
          callback.onPackagesAvailable(packages, user, replacing);
        }
      } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
        // This intent is broadcasted when moving a package or mounting/un-mounting
        // external storage.
        // However on Kitkat this is also sent when a package is being updated, and
        // contains an extra Intent.EXTRA_REPLACING=true for that case.
        // Using false as default for Intent.EXTRA_REPLACING gives correct value on
        // lower devices as the intent is not sent when the app is updating/replacing.
        final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
        String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
        for (OnAppsChangedCallbackCompat callback : getCallbacks()) {
          callback.onPackagesUnavailable(packages, user, replacing);
        }
      }
    }