private final void addActiveOwnerLocked(int uid, String pkg) {
   final IPackageManager pm = AppGlobals.getPackageManager();
   final int targetUserHandle = UserHandle.getCallingUserId();
   final long oldIdentity = Binder.clearCallingIdentity();
   try {
     PackageInfo pi = pm.getPackageInfo(pkg, 0, targetUserHandle);
     if (pi == null) {
       throw new IllegalArgumentException("Unknown package " + pkg);
     }
     if (!UserHandle.isSameApp(pi.applicationInfo.uid, uid)) {
       throw new SecurityException("Calling uid " + uid + " does not own package " + pkg);
     }
   } catch (RemoteException e) {
     // Can't happen; the package manager is in the same process
   } finally {
     Binder.restoreCallingIdentity(oldIdentity);
   }
   PerUserClipboard clipboard = getClipboard();
   if (clipboard.primaryClip != null && !clipboard.activePermissionOwners.contains(pkg)) {
     final int N = clipboard.primaryClip.getItemCount();
     for (int i = 0; i < N; i++) {
       grantItemLocked(clipboard.primaryClip.getItemAt(i), pkg);
     }
     clipboard.activePermissionOwners.add(pkg);
   }
 }
    @Override
    public UsageEvents queryEvents(long beginTime, long endTime, String callingPackage) {
      if (!hasPermission(callingPackage)) {
        return null;
      }

      final int userId = UserHandle.getCallingUserId();
      final long token = Binder.clearCallingIdentity();
      try {
        return UsageStatsService.this.queryEvents(userId, beginTime, endTime);
      } finally {
        Binder.restoreCallingIdentity(token);
      }
    }
    @Override
    public ParceledListSlice<UsageStats> queryUsageStats(
        int bucketType, long beginTime, long endTime, String callingPackage) {
      if (!hasPermission(callingPackage)) {
        return null;
      }

      final int userId = UserHandle.getCallingUserId();
      final long token = Binder.clearCallingIdentity();
      try {
        final List<UsageStats> results =
            UsageStatsService.this.queryUsageStats(userId, bucketType, beginTime, endTime);
        if (results != null) {
          return new ParceledListSlice<>(results);
        }
      } finally {
        Binder.restoreCallingIdentity(token);
      }
      return null;
    }
Пример #4
0
  static void shutdownInner(final Context context, boolean confirm) {
    // ensure that only one thread is trying to power down.
    // any additional calls are just returned
    synchronized (sIsStartedGuard) {
      if (sIsStarted) {
        Log.d(TAG, "Request to shutdown already running, returning.");
        return;
      }
    }

    final int titleResourceId;
    final int resourceId;

    Log.d(TAG, "Notifying thread to start shutdown");

    if (mRebootSafeMode) {
      titleResourceId = com.android.internal.R.string.reboot_safemode_title;
      resourceId = com.android.internal.R.string.reboot_safemode_confirm;
    } else if (mReboot) {
      titleResourceId = com.android.internal.R.string.reboot_system;
      resourceId = com.android.internal.R.string.reboot_confirm;
    } else {

      final int longPressBehavior =
          context
              .getResources()
              .getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior);

      titleResourceId = com.android.internal.R.string.power_off;
      if (longPressBehavior == 2) {
        resourceId = com.android.internal.R.string.shutdown_confirm_question;
      } else {
        resourceId = com.android.internal.R.string.shutdown_confirm;
      }

      Log.d(TAG, "longPressBehavior=" + longPressBehavior);
    }

    if (confirm) {
      final CloseDialogReceiver closer = new CloseDialogReceiver(context);
      if (sConfirmDialog != null) {
        sConfirmDialog.dismiss();
        sConfirmDialog = null;
      }
      if (mReboot && !mRebootSafeMode) {
        // Determine if primary user is logged in
        boolean isPrimary = UserHandle.getCallingUserId() == UserHandle.USER_OWNER;

        // See if the advanced reboot menu is enabled (only if primary user) and check the keyguard
        // state
        boolean advancedReboot = isPrimary ? advancedRebootEnabled(context) : false;
        KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        boolean locked = km.inKeyguardRestrictedInputMode();

        if (advancedReboot && !locked) {
          // Include options in power menu for rebooting into recovery or bootloader
          sConfirmDialog =
              new AlertDialog.Builder(context)
                  .setTitle(titleResourceId)
                  .setSingleChoiceItems(
                      com.android.internal.R.array.shutdown_reboot_options,
                      0,
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                          if (which < 0) return;

                          String actions[] =
                              context
                                  .getResources()
                                  .getStringArray(
                                      com.android.internal.R.array.shutdown_reboot_actions);

                          if (actions != null && which < actions.length)
                            mRebootReason = actions[which];
                        }
                      })
                  .setPositiveButton(
                      com.android.internal.R.string.yes,
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                          mReboot = true;
                          beginShutdownSequence(context);
                        }
                      })
                  .setNegativeButton(
                      com.android.internal.R.string.no,
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                          mReboot = false;
                          dialog.cancel();
                        }
                      })
                  .create();
          sConfirmDialog.setOnKeyListener(
              new DialogInterface.OnKeyListener() {
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                  if (keyCode == KeyEvent.KEYCODE_BACK) {
                    mReboot = false;
                    dialog.cancel();
                  }
                  return true;
                }
              });
        }
      }

      if (sConfirmDialog == null) {
        sConfirmDialog =
            new AlertDialog.Builder(context)
                .setTitle(titleResourceId)
                .setMessage(resourceId)
                .setPositiveButton(
                    com.android.internal.R.string.yes,
                    new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int which) {
                        beginShutdownSequence(context);
                      }
                    })
                .setNegativeButton(com.android.internal.R.string.no, null)
                .create();
      }

      closer.dialog = sConfirmDialog;
      sConfirmDialog.setOnDismissListener(closer);
      sConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
      sConfirmDialog.show();

    } else {
      beginShutdownSequence(context);
    }
  }
 private PerUserClipboard getClipboard() {
   return getClipboard(UserHandle.getCallingUserId());
 }