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

Commit 590587cd authored by Shashank Mittal's avatar Shashank Mittal Committed by Roman Birg
Browse files

AppOps: Add Bluetooth enable control into AppOps

Check user permission before enabling bluetooth.

Change-Id: I9af341157df05104d3c5a52874538eefdbe14c4b
parent 93a5bde8
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -253,7 +253,9 @@ public class AppOpsManager {
    /** @hide Wifi state change **/
    public static final int OP_WIFI_CHANGE = 63;
    /** @hide */
    public static final int _NUM_OP = 64;
    public static final int OP_BLUETOOTH_CHANGE = 64;
    /** @hide */
    public static final int _NUM_OP = 65;

    /** Access to coarse location information. */
    public static final String OPSTR_COARSE_LOCATION = "android:coarse_location";
@@ -354,6 +356,8 @@ public class AppOpsManager {
    /** @hide **/
    private static final String OPSTR_WIFI_CHANGE =
            "android:wifi_change";
    private static final String OPSTR_BLUETOOTH_CHANGE =
            "android:bluetooth_change";

    /**
     * This maps each operation to the operation that serves as the
@@ -428,6 +432,7 @@ public class AppOpsManager {
            OP_TURN_SCREEN_ON,
            OP_GET_ACCOUNTS,
            OP_WIFI_CHANGE,
            OP_BLUETOOTH_CHANGE,
    };

    /**
@@ -499,6 +504,7 @@ public class AppOpsManager {
            null,
            OPSTR_GET_ACCOUNTS,
            OPSTR_WIFI_CHANGE,
            OPSTR_BLUETOOTH_CHANGE,
    };

    /**
@@ -570,6 +576,7 @@ public class AppOpsManager {
            "TURN_ON_SCREEN",
            "GET_ACCOUNTS",
            "WIFI_CHANGE",
            "BLUETOOTH_CHANGE",
    };

    /**
@@ -641,6 +648,7 @@ public class AppOpsManager {
            null, // no permission for turning the screen on
            Manifest.permission.GET_ACCOUNTS,
            Manifest.permission.CHANGE_WIFI_STATE,
            Manifest.permission.BLUETOOTH,
    };

    /**
@@ -713,6 +721,7 @@ public class AppOpsManager {
            null, // TURN_ON_SCREEN
            null, // GET_ACCOUNTS
            null, //WIFI_CHANGE
            null, //BLUETOOTH_CHANGE
    };

    /**
@@ -784,6 +793,7 @@ public class AppOpsManager {
            false, // TURN_ON_SCREEN
            false, // GET_ACCOUNTS
            false, // WIFI_CHANGE
            false, // BLUETOOTH_CHANGE
    };

    /**
@@ -854,6 +864,7 @@ public class AppOpsManager {
            AppOpsManager.MODE_ALLOWED,  // OP_TURN_ON_SCREEN
            AppOpsManager.MODE_ALLOWED,
            AppOpsManager.MODE_ALLOWED, // OP_WIFI_CHANGE
            AppOpsManager.MODE_ASK,     // OP_BLUETOOTH_CHANGE
    };

    /**
@@ -925,6 +936,7 @@ public class AppOpsManager {
            AppOpsManager.MODE_ALLOWED, // OP_TURN_ON_SCREEN
            AppOpsManager.MODE_ALLOWED, // OP_GET_ACCOUNTS
            AppOpsManager.MODE_ASK,     // OP_WIFI_CHANGE
            AppOpsManager.MODE_ASK,     // OP_BLUETOOTH_CHANGE
    };

    /**
@@ -995,6 +1007,7 @@ public class AppOpsManager {
        false,    // TURN_ON_SCREEN
        false,    // GET_ACCOUNTS
        true,     // OP_WIFI_CHANGE
        true,     // OP_BLUETOOTH_CHANGE
    };

    /**
@@ -1069,6 +1082,7 @@ public class AppOpsManager {
            false,
            false,
            false,     // OP_WIFI_CHANGE
            false,     // OP_BLUETOOTH_CHANGE
    };

    /**
+5 −2
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009-2015 The Android Open Source Project
 * Copyright (C) 2015 Samsung LSI
 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
 * Not a Contribution.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -23,6 +25,7 @@ import android.annotation.RequiresPermission;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SystemApi;
import android.app.ActivityThread;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
@@ -764,7 +767,7 @@ public final class BluetoothAdapter {
        try {
            if (DBG) Log.d(TAG, "Calling enableBLE");
            mManagerService.updateBleAppCount(mToken, true);
            return mManagerService.enable();
            return mManagerService.enable(ActivityThread.currentPackageName());
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
@@ -902,7 +905,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
@@ -34,7 +34,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
@@ -85,5 +85,6 @@
        <item>Trying to turn screen on</item>
        <item>Trying to get device accounts</item>
        <item>Trying to turn on/off Wi-Fi</item>
        <item>Trying to trun on/off Bluetooth</item>
    </string-array>
</resources>
+12 −1
Original line number Diff line number Diff line
/*
 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
 * Not a Contribution.
 *
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,6 +21,7 @@ package com.android.server;

import android.Manifest;
import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
@@ -619,7 +623,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");
@@ -633,6 +637,13 @@ 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;