public void setPrimaryClip(ClipData clip, String callingPackage) {
   synchronized (this) {
     if (clip != null && clip.getItemCount() <= 0) {
       throw new IllegalArgumentException("No items");
     }
     final int callingUid = Binder.getCallingUid();
     if (mAppOps.noteOp(AppOpsManager.OP_WRITE_CLIPBOARD, callingUid, callingPackage)
         != AppOpsManager.MODE_ALLOWED) {
       return;
     }
     checkDataOwnerLocked(clip, callingUid);
     clearActiveOwnersLocked();
     PerUserClipboard clipboard = getClipboard();
     clipboard.primaryClip = clip;
     final long ident = Binder.clearCallingIdentity();
     final int n = clipboard.primaryClipListeners.beginBroadcast();
     try {
       for (int i = 0; i < n; i++) {
         try {
           ListenerInfo li = (ListenerInfo) clipboard.primaryClipListeners.getBroadcastCookie(i);
           if (mAppOps.checkOpNoThrow(AppOpsManager.OP_READ_CLIPBOARD, li.mUid, li.mPackageName)
               == AppOpsManager.MODE_ALLOWED) {
             clipboard.primaryClipListeners.getBroadcastItem(i).dispatchPrimaryClipChanged();
           }
         } catch (RemoteException e) {
           // The RemoteCallbackList will take care of removing
           // the dead object for us.
         }
       }
     } finally {
       clipboard.primaryClipListeners.finishBroadcast();
       Binder.restoreCallingIdentity(ident);
     }
   }
 }
 public boolean canGetUsageStats() {
   if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
     return true;
   }
   AppOpsManager aom = (AppOpsManager) getSystemService(APP_OPS_SERVICE);
   int uid = android.os.Process.myUid();
   int mode = aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, uid, getPackageName());
   return mode == AppOpsManager.MODE_ALLOWED;
 }
 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 public static boolean usageAccessGranted(Context context) {
   AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
   int mode =
       appOps.checkOpNoThrow(
           AppOpsManager.OPSTR_GET_USAGE_STATS,
           android.os.Process.myUid(),
           context.getPackageName());
   return mode == AppOpsManager.MODE_ALLOWED;
 }
  /** Returns true if there currently exist active high power location requests. */
  private boolean areActiveHighPowerLocationRequests() {
    List<AppOpsManager.PackageOps> packages =
        mAppOpsManager.getPackagesForOps(mHighPowerRequestAppOpArray);
    // AppOpsManager can return null when there is no requested data.
    if (packages != null) {
      final int numPackages = packages.size();
      for (int packageInd = 0; packageInd < numPackages; packageInd++) {
        AppOpsManager.PackageOps packageOp = packages.get(packageInd);
        List<AppOpsManager.OpEntry> opEntries = packageOp.getOps();
        if (opEntries != null) {
          final int numOps = opEntries.size();
          for (int opInd = 0; opInd < numOps; opInd++) {
            AppOpsManager.OpEntry opEntry = opEntries.get(opInd);
            // AppOpsManager should only return OP_MONITOR_HIGH_POWER_LOCATION because
            // of the mHighPowerRequestAppOpArray filter, but checking defensively.
            if (opEntry.getOp() == AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION) {
              if (opEntry.isRunning()) {
                return true;
              }
            }
          }
        }
      }
    }

    return false;
  }
Example #5
0
 public static boolean needPermissionForBlocking(Context context) {
   try {
     PackageManager packageManager = context.getPackageManager();
     ApplicationInfo applicationInfo =
         packageManager.getApplicationInfo(context.getPackageName(), 0);
     AppOpsManager appOpsManager =
         (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
     int mode =
         appOpsManager.checkOpNoThrow(
             AppOpsManager.OPSTR_GET_USAGE_STATS,
             applicationInfo.uid,
             applicationInfo.packageName);
     return (mode != AppOpsManager.MODE_ALLOWED);
   } catch (PackageManager.NameNotFoundException e) {
     return true;
   }
 }
 public boolean hasPrimaryClip(String callingPackage) {
   synchronized (this) {
     if (mAppOps.checkOp(AppOpsManager.OP_READ_CLIPBOARD, Binder.getCallingUid(), callingPackage)
         != AppOpsManager.MODE_ALLOWED) {
       return false;
     }
     return getClipboard().primaryClip != null;
   }
 }
 public ClipDescription getPrimaryClipDescription(String callingPackage) {
   synchronized (this) {
     if (mAppOps.checkOp(AppOpsManager.OP_READ_CLIPBOARD, Binder.getCallingUid(), callingPackage)
         != AppOpsManager.MODE_ALLOWED) {
       return null;
     }
     PerUserClipboard clipboard = getClipboard();
     return clipboard.primaryClip != null ? clipboard.primaryClip.getDescription() : null;
   }
 }
 public ClipData getPrimaryClip(String pkg) {
   synchronized (this) {
     if (mAppOps.noteOp(AppOpsManager.OP_READ_CLIPBOARD, Binder.getCallingUid(), pkg)
         != AppOpsManager.MODE_ALLOWED) {
       return null;
     }
     addActiveOwnerLocked(Binder.getCallingUid(), pkg);
     return getClipboard().primaryClip;
   }
 }
 public boolean hasClipboardText(String callingPackage) {
   synchronized (this) {
     if (mAppOps.checkOp(AppOpsManager.OP_READ_CLIPBOARD, Binder.getCallingUid(), callingPackage)
         != AppOpsManager.MODE_ALLOWED) {
       return false;
     }
     PerUserClipboard clipboard = getClipboard();
     if (clipboard.primaryClip != null) {
       CharSequence text = clipboard.primaryClip.getItemAt(0).getText();
       return text != null && text.length() > 0;
     }
     return false;
   }
 }