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

Commit f3b41aa0 authored by Jakub Pawlowski's avatar Jakub Pawlowski Committed by android-build-merger
Browse files

Merge "Simplify checkCaller" am: 2c1bee0f

am: dd66bd55

Change-Id: I15d268ea3ce0237dc33e838fb8297b47387acf25
parents 331b2d95 dd66bd55
Loading
Loading
Loading
Loading
+11 −22
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.bluetooth;

import android.app.ActivityManager;
import android.app.ActivityThread;
import android.app.AppOpsManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
@@ -221,8 +220,12 @@ public final class Utils {
        }
    }

    static int sSystemUiUid = UserHandle.USER_NULL;
    public static void setSystemUiUid(int uid) {
        Utils.sSystemUiUid = uid;
    }

    public static boolean checkCaller() {
        boolean ok;
        // Get the caller's user id then clear the calling identity
        // which will be restored in the finally clause.
        int callingUser = UserHandle.getCallingUserId();
@@ -232,28 +235,20 @@ public final class Utils {
        try {
            // With calling identity cleared the current user is the foreground user.
            int foregroundUser = ActivityManager.getCurrentUser();
            ok = (foregroundUser == callingUser);
            if (!ok) {
                // Always allow SystemUI/System access.
                final int systemUiUid = ActivityThread.getPackageManager()
                        .getPackageUid("com.android.systemui", PackageManager.MATCH_SYSTEM_ONLY,
                                UserHandle.USER_SYSTEM);
                ok = (systemUiUid == callingUid) || (Process.SYSTEM_UID == callingUid);
            }
            return (foregroundUser == callingUser) || (sSystemUiUid == callingUid)
                    || (Process.SYSTEM_UID == callingUid);
        } catch (Exception ex) {
            Log.e(TAG, "checkIfCallerIsSelfOrForegroundUser: Exception ex=" + ex);
            ok = false;
            return false;
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
        return ok;
    }

    public static boolean checkCallerAllowManagedProfiles(Context mContext) {
        if (mContext == null) {
            return checkCaller();
        }
        boolean ok;
        // Get the caller's user id and if it's a managed profile, get it's parents
        // id, then clear the calling identity
        // which will be restored in the finally clause.
@@ -266,21 +261,15 @@ public final class Utils {
            int parentUser = (ui != null) ? ui.id : UserHandle.USER_NULL;
            // With calling identity cleared the current user is the foreground user.
            int foregroundUser = ActivityManager.getCurrentUser();
            ok = (foregroundUser == callingUser) || (foregroundUser == parentUser);
            if (!ok) {
                // Always allow SystemUI/System access.
                final int systemUiUid = ActivityThread.getPackageManager()
                        .getPackageUid("com.android.systemui", PackageManager.MATCH_SYSTEM_ONLY,
                                UserHandle.USER_SYSTEM);
                ok = (systemUiUid == callingUid) || (Process.SYSTEM_UID == callingUid);
            }
            return (foregroundUser == callingUser) || (foregroundUser == parentUser)
                    || (sSystemUiUid == callingUid) || (Process.SYSTEM_UID == callingUid);
        } catch (Exception ex) {
            Log.e(TAG, "checkCallerAllowManagedProfiles: Exception ex=" + ex);
            ok = false;
            return false;
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
        return ok;
    }

    /**
+11 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.BatteryStats;
import android.os.Binder;
@@ -445,6 +446,16 @@ public class AdapterService extends Service {
                return null;
            }
        }.execute();

        try {
            int systemUiUid = getApplicationContext().getPackageManager().getPackageUidAsUser(
                    "com.android.systemui", PackageManager.MATCH_SYSTEM_ONLY,
                    UserHandle.USER_SYSTEM);
            Utils.setSystemUiUid(systemUiUid);
        } catch (PackageManager.NameNotFoundException e) {
            // Some platforms, such as wearables do not have a system ui.
            Log.w(TAG, "Unable to resolve SystemUI's UID.", e);
        }
    }

    @Override