private Intent getMediaProjectionIntent(int uid, String packageName, boolean permanentGrant)
     throws RemoteException {
   IMediaProjection projection =
       mService.createProjection(
           uid, packageName, MediaProjectionManager.TYPE_SCREEN_CAPTURE, permanentGrant);
   Intent intent = new Intent();
   intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder());
   return intent;
 }
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    mPackageName = getCallingPackage();
    IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE);
    mService = IMediaProjectionManager.Stub.asInterface(b);

    if (mPackageName == null) {
      finish();
      return;
    }

    PackageManager packageManager = getPackageManager();
    ApplicationInfo aInfo;
    try {
      aInfo = packageManager.getApplicationInfo(mPackageName, 0);
      mUid = aInfo.uid;
    } catch (PackageManager.NameNotFoundException e) {
      Log.e(TAG, "unable to look up package name", e);
      finish();
      return;
    }

    try {
      if (mService.hasProjectionPermission(mUid, mPackageName)) {
        setResult(
            RESULT_OK, getMediaProjectionIntent(mUid, mPackageName, false /*permanentGrant*/));
        finish();
        return;
      }
    } catch (RemoteException e) {
      Log.e(TAG, "Error checking projection permissions", e);
      finish();
      return;
    }

    String appName = aInfo.loadLabel(packageManager).toString();

    mDialog =
        new AlertDialog.Builder(this)
            .setIcon(aInfo.loadIcon(packageManager))
            .setMessage(getString(R.string.media_projection_dialog_text, appName))
            .setPositiveButton(R.string.media_projection_action_text, this)
            .setNegativeButton(android.R.string.cancel, this)
            .setView(R.layout.remember_permission_checkbox)
            .setOnCancelListener(this)
            .create();

    mDialog.create();

    ((CheckBox) mDialog.findViewById(R.id.remember)).setOnCheckedChangeListener(this);
    mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

    mDialog.show();
  }