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

Commit 9207f1b9 authored by Shashank Mittal's avatar Shashank Mittal
Browse files

AppOps: Add Bluetooth enable control into AppOps

Check user permission before enabling bluetooth.

Change-Id: I9af341157df05104d3c5a52874538eefdbe14c4b
parent 85a66108
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -197,7 +197,9 @@ public class AppOpsManager {
    /** @hide */
    public static final int OP_WIFI_CHANGE = 43;
    /** @hide */
    public static final int _NUM_OP = 44;
    public static final int OP_BLUETOOTH_CHANGE = 44;
    /** @hide */
    public static final int _NUM_OP = 45;

    /** Access to coarse location information. */
    public static final String OPSTR_COARSE_LOCATION =
@@ -265,6 +267,7 @@ public class AppOpsManager {
            OP_COARSE_LOCATION,
            OP_COARSE_LOCATION,
            OP_WIFI_CHANGE,
            OP_BLUETOOTH_CHANGE,
    };

    /**
@@ -316,6 +319,7 @@ public class AppOpsManager {
            OPSTR_MONITOR_LOCATION,
            OPSTR_MONITOR_HIGH_POWER_LOCATION,
            null,
            null,
    };

    /**
@@ -367,6 +371,7 @@ public class AppOpsManager {
            "MONITOR_LOCATION",
            "MONITOR_HIGH_POWER_LOCATION",
            "WIFI_CHANGE",
            "BLUETOOTH_CHANGE",
    };

    /**
@@ -418,6 +423,7 @@ public class AppOpsManager {
            null, // no permission for generic location monitoring
            null, // no permission for high power location monitoring
            android.Manifest.permission.CHANGE_WIFI_STATE,
            android.Manifest.permission.BLUETOOTH,
    };

    /**
@@ -468,6 +474,7 @@ public class AppOpsManager {
            AppOpsManager.MODE_ALLOWED, // OP_MONITOR_LOCATION
            AppOpsManager.MODE_ALLOWED, // OP_MONITOR_HIGH_POWER_LOCATION
            AppOpsManager.MODE_ALLOWED, // OP_WIFI_CHANGE
            AppOpsManager.MODE_ALLOWED, // OP_BLUETOOTH_CHANGE
    };

    /**
@@ -518,6 +525,7 @@ public class AppOpsManager {
            AppOpsManager.MODE_ALLOWED, // OP_MONITOR_LOCATION
            AppOpsManager.MODE_ASK,     // OP_MONITOR_HIGH_POWER_LOCATION
            AppOpsManager.MODE_ASK,     // OP_WIFI_CHANGE
            AppOpsManager.MODE_ASK,     // OP_BLUETOOTH_CHANGE
    };


@@ -573,6 +581,7 @@ public class AppOpsManager {
            false,
            false,
            false,
            false,
    };

    private static HashMap<String, Integer> sOpStrToOp = new HashMap<String, Integer>();
+2 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ package android.bluetooth;

import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.app.ActivityThread;
import android.content.Context;
import android.os.Binder;
import android.os.IBinder;
@@ -518,7 +519,7 @@ public final class BluetoothAdapter {
            return true;
        }
        try {
            return mManagerService.enable();
            return mManagerService.enable(ActivityThread.currentPackageName());
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ interface IBluetoothManager
    void registerStateChangeCallback(in IBluetoothStateChangeCallback callback);
    void unregisterStateChangeCallback(in IBluetoothStateChangeCallback callback);
    boolean isEnabled();
    boolean enable();
    boolean enable(String callingPackage);
    boolean enableNoAutoConnect();
    boolean disable(boolean persist);
    IBluetoothGatt getBluetoothGatt();
+1 −0
Original line number Diff line number Diff line
@@ -1357,5 +1357,6 @@
        <item>Trying to access location</item>
        <item>Trying to access location</item>
        <item>Trying to turn on/off Wifi</item>
        <item>Trying to turn on/off bluetooth</item>
    </string-array>
</resources>
+8 −1
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.app.AppOpsManager;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
@@ -388,7 +389,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        return true;

    }
    public boolean enable() {
    public boolean enable(String callingPackage) {
        if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
            (!checkIfCallerIsForegroundUser())) {
            Log.w(TAG,"enable(): not allowed for non-active and non system user");
@@ -402,6 +403,12 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    " mBinding = " + mBinding);
        }

        AppOpsManager appOps = (AppOpsManager)mContext.getSystemService(Context.APP_OPS_SERVICE);
        int callingUid = Binder.getCallingUid();
        if (appOps.noteOp(AppOpsManager.OP_BLUETOOTH_CHANGE, callingUid, callingPackage) !=
            AppOpsManager.MODE_ALLOWED)
            return false;

        synchronized(mReceiver) {
            mQuietEnableExternal = false;
            mEnableExternal = true;