Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce7c3a8d authored by Peter Visontay's avatar Peter Visontay Committed by Android (Google) Code Review
Browse files

Merge "Log an App Op when a package is requested to be deleted by a third...

Merge "Log an App Op when a package is requested to be deleted by a third party. (REQUEST_DELETE_PACKAGES)"
parents b6a3d34a 285ea8f0
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@
*/
package com.android.packageinstaller;

import static android.app.AppOpsManager.MODE_ALLOWED;

import static com.android.packageinstaller.PackageUtil.getMaxTargetSdkVersionForUid;

import android.Manifest;
@@ -23,6 +25,7 @@ import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityThread;
import android.app.AppGlobals;
import android.app.AppOpsManager;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentTransaction;
@@ -89,6 +92,25 @@ public class UninstallerActivity extends Activity {
        try {
            int callingUid = ActivityManager.getService().getLaunchedFromUid(getActivityToken());

            String callingPackage = getPackageNameForUid(callingUid);
            if (callingPackage == null) {
                Log.e(TAG, "Package not found for originating uid " + callingUid);
                setResult(Activity.RESULT_FIRST_USER);
                finish();
                return;
            } else {
                AppOpsManager appOpsManager = (AppOpsManager) getSystemService(
                        Context.APP_OPS_SERVICE);
                if (appOpsManager.noteOpNoThrow(
                        AppOpsManager.OPSTR_REQUEST_DELETE_PACKAGES, callingUid, callingPackage)
                        != MODE_ALLOWED) {
                    Log.e(TAG, "Install from uid " + callingUid + " disallowed by AppOps");
                    setResult(Activity.RESULT_FIRST_USER);
                    finish();
                    return;
                }
            }

            if (getMaxTargetSdkVersionForUid(this, callingUid)
                    >= Build.VERSION_CODES.P && AppGlobals.getPackageManager().checkUidPermission(
                    Manifest.permission.REQUEST_DELETE_PACKAGES, callingUid)
@@ -360,4 +382,12 @@ public class UninstallerActivity extends Activity {
            }
        }
    }

    private String getPackageNameForUid(int sourceUid) {
        String[] packagesForUid = getPackageManager().getPackagesForUid(sourceUid);
        if (packagesForUid == null) {
            return null;
        }
        return packagesForUid[0];
    }
}