private static boolean isPermissionEnforced(String permission) {
   try {
     return ActivityThread.getPackageManager().isPermissionEnforced(permission);
   } catch (RemoteException e) {
     throw new RuntimeException("Problem talking with PackageManager", e);
   }
 }
 private static void setPermissionEnforced(Context context, String permission, boolean enforced) {
   try {
     // TODO: offload to background thread
     ActivityThread.getPackageManager().setPermissionEnforced(READ_EXTERNAL_STORAGE, enforced);
   } catch (RemoteException e) {
     throw new RuntimeException("Problem talking with PackageManager", e);
   }
 }
    boolean updateService(Context context, ActivityManager.RunningServiceInfo service) {
      final PackageManager pm = context.getPackageManager();

      boolean changed = false;
      ServiceItem si = mServices.get(service.service);
      if (si == null) {
        changed = true;
        si = new ServiceItem(mUserId);
        si.mRunningService = service;
        try {
          si.mServiceInfo =
              ActivityThread.getPackageManager()
                  .getServiceInfo(
                      service.service,
                      PackageManager.GET_UNINSTALLED_PACKAGES,
                      UserHandle.getUserId(service.uid));

          if (si.mServiceInfo == null) {
            Log.d("RunningService", "getServiceInfo returned null for: " + service.service);
            return false;
          }
        } catch (RemoteException e) {
        }
        si.mDisplayLabel =
            makeLabel(pm, si.mRunningService.service.getClassName(), si.mServiceInfo);
        mLabel = mDisplayLabel != null ? mDisplayLabel.toString() : null;
        si.mPackageInfo = si.mServiceInfo.applicationInfo;
        mServices.put(service.service, si);
      }
      si.mCurSeq = mCurSeq;
      si.mRunningService = service;
      long activeSince = service.restarting == 0 ? service.activeSince : -1;
      if (si.mActiveSince != activeSince) {
        si.mActiveSince = activeSince;
        changed = true;
      }
      if (service.clientPackage != null && service.clientLabel != 0) {
        if (si.mShownAsStarted) {
          si.mShownAsStarted = false;
          changed = true;
        }
        try {
          Resources clientr = pm.getResourcesForApplication(service.clientPackage);
          String label = clientr.getString(service.clientLabel);
          si.mDescription = context.getResources().getString(R.string.service_client_name, label);
        } catch (PackageManager.NameNotFoundException e) {
          si.mDescription = null;
        }
      } else {
        if (!si.mShownAsStarted) {
          si.mShownAsStarted = true;
          changed = true;
        }
        si.mDescription = context.getResources().getString(R.string.service_started_by_app);
      }

      return changed;
    }
Exemplo n.º 4
0
 /**
  * Helper to check if this device has FEATURE_NFC, but without using a context. Equivalent to
  * context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)
  */
 private static boolean hasNfcFeature() {
   IPackageManager pm = ActivityThread.getPackageManager();
   if (pm == null) {
     Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
     return false;
   }
   try {
     return pm.hasSystemFeature(PackageManager.FEATURE_NFC);
   } catch (RemoteException e) {
     Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
     return false;
   }
 }
  boolean handleAppCrashLocked(
      ProcessRecord app,
      String reason,
      String shortMsg,
      String longMsg,
      String stackTrace,
      AppErrorDialog.Data data) {
    long now = SystemClock.uptimeMillis();

    Long crashTime;
    Long crashTimePersistent;
    if (!app.isolated) {
      crashTime = mProcessCrashTimes.get(app.info.processName, app.uid);
      crashTimePersistent = mProcessCrashTimesPersistent.get(app.info.processName, app.uid);
    } else {
      crashTime = crashTimePersistent = null;
    }
    if (crashTime != null && now < crashTime + ProcessList.MIN_CRASH_INTERVAL) {
      // This process loses!
      Slog.w(TAG, "Process " + app.info.processName + " has crashed too many times: killing!");
      EventLog.writeEvent(
          EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, app.userId, app.info.processName, app.uid);
      mService.mStackSupervisor.handleAppCrashLocked(app);
      if (!app.persistent) {
        // We don't want to start this process again until the user
        // explicitly does so...  but for persistent process, we really
        // need to keep it running.  If a persistent process is actually
        // repeatedly crashing, then badness for everyone.
        EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.userId, app.uid, app.info.processName);
        if (!app.isolated) {
          // XXX We don't have a way to mark isolated processes
          // as bad, since they don't have a peristent identity.
          mBadProcesses.put(
              app.info.processName,
              app.uid,
              new BadProcessInfo(now, shortMsg, longMsg, stackTrace));
          mProcessCrashTimes.remove(app.info.processName, app.uid);
        }
        app.bad = true;
        app.removed = true;
        // Don't let services in this process be restarted and potentially
        // annoy the user repeatedly.  Unless it is persistent, since those
        // processes run critical code.
        mService.removeProcessLocked(app, false, false, "crash");
        mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
        return false;
      }
      mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
    } else {
      TaskRecord affectedTask =
          mService.mStackSupervisor.finishTopRunningActivityLocked(app, reason);
      if (data != null) {
        data.task = affectedTask;
      }
      if (data != null
          && crashTimePersistent != null
          && now < crashTimePersistent + ProcessList.MIN_CRASH_INTERVAL) {
        data.repeating = true;
      }
    }

    // Bump up the crash count of any services currently running in the proc.
    for (int i = app.services.size() - 1; i >= 0; i--) {
      // Any services running in the application need to be placed
      // back in the pending list.
      ServiceRecord sr = app.services.valueAt(i);
      sr.crashCount++;
    }

    // If the crashing process is what we consider to be the "home process" and it has been
    // replaced by a third-party app, clear the package preferred activities from packages
    // with a home activity running in the process to prevent a repeatedly crashing app
    // from blocking the user to manually clear the list.
    final ArrayList<ActivityRecord> activities = app.activities;
    if (app == mService.mHomeProcess
        && activities.size() > 0
        && (mService.mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
      for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
        final ActivityRecord r = activities.get(activityNdx);
        if (r.isHomeActivity()) {
          Log.i(TAG, "Clearing package preferred activities from " + r.packageName);
          try {
            ActivityThread.getPackageManager().clearPackagePreferredActivities(r.packageName);
          } catch (RemoteException c) {
            // pm is in same process, this will never happen.
          }
        }
      }
    }

    if (!app.isolated) {
      // XXX Can't keep track of crash times for isolated processes,
      // because they don't have a perisistent identity.
      mProcessCrashTimes.put(app.info.processName, app.uid, now);
      mProcessCrashTimesPersistent.put(app.info.processName, app.uid, now);
    }

    if (app.crashHandler != null) mService.mHandler.post(app.crashHandler);
    return true;
  }
  public void init() {
    final Context context = getContext();

    final UserInfo currentUser;
    try {
      currentUser = ActivityManagerNative.getDefault().getCurrentUser();
    } catch (RemoteException e) {
      throw new RuntimeException("Failed to get current user");
    }

    final List<UserInfo> otherUsers = getUsersExcluding(currentUser);
    final boolean showUsers = mVolume == null && otherUsers.size() > 0;

    mUsageBarPreference = new UsageBarPreference(context);
    mUsageBarPreference.setOrder(ORDER_USAGE_BAR);
    addPreference(mUsageBarPreference);

    mItemTotal = buildItem(R.string.memory_size, 0);
    mItemAvailable = buildItem(R.string.memory_available, R.color.memory_avail);
    addPreference(mItemTotal);
    addPreference(mItemAvailable);

    mItemApps = buildItem(R.string.memory_apps_usage, R.color.memory_apps_usage);
    mItemDcim = buildItem(R.string.memory_dcim_usage, R.color.memory_dcim);
    mItemMusic = buildItem(R.string.memory_music_usage, R.color.memory_music);
    mItemDownloads = buildItem(R.string.memory_downloads_usage, R.color.memory_downloads);
    mItemCache = buildItem(R.string.memory_media_cache_usage, R.color.memory_cache);
    mItemMisc = buildItem(R.string.memory_media_misc_usage, R.color.memory_misc);

    mItemCache.setKey(KEY_CACHE);

    final boolean showDetails = mVolume == null || mVolume.isPrimary();
    if (showDetails) {
      if (showUsers) {
        addPreference(new PreferenceHeader(context, currentUser.name));
      }

      addPreference(mItemApps);
      addPreference(mItemDcim);
      addPreference(mItemMusic);
      addPreference(mItemDownloads);
      addPreference(mItemCache);
      addPreference(mItemMisc);

      if (showUsers) {
        addPreference(new PreferenceHeader(context, R.string.storage_other_users));

        int count = 0;
        for (UserInfo info : otherUsers) {
          final int colorRes =
              count++ % 2 == 0 ? R.color.memory_user_light : R.color.memory_user_dark;
          final StorageItemPreference userPref =
              new StorageItemPreference(getContext(), info.name, colorRes, info.id);
          mItemUsers.add(userPref);
          addPreference(userPref);
        }
      }
    }

    final boolean isRemovable = mVolume != null ? mVolume.isRemovable() : false;
    if (isRemovable) {
      mMountTogglePreference = new Preference(context);
      mMountTogglePreference.setTitle(R.string.sd_eject);
      mMountTogglePreference.setSummary(R.string.sd_eject_summary);
      addPreference(mMountTogglePreference);
    }

    // Only allow formatting of primary physical storage
    // TODO: enable for non-primary volumes once MTP is fixed
    final boolean allowFormat = mVolume != null ? mVolume.isPrimary() : false;
    if (allowFormat) {
      mFormatPreference = new Preference(context);
      mFormatPreference.setTitle(R.string.sd_format);
      mFormatPreference.setSummary(R.string.sd_format_summary);
      addPreference(mFormatPreference);
    }

    final IPackageManager pm = ActivityThread.getPackageManager();
    try {
      if (pm.isStorageLow()) {
        mStorageLow = new Preference(context);
        mStorageLow.setOrder(ORDER_STORAGE_LOW);
        mStorageLow.setTitle(R.string.storage_low_title);
        mStorageLow.setSummary(R.string.storage_low_summary);
        addPreference(mStorageLow);
      } else if (mStorageLow != null) {
        removePreference(mStorageLow);
        mStorageLow = null;
      }
    } catch (RemoteException e) {
    }
  }