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

Commit d43da5a2 authored by Xin Li's avatar Xin Li
Browse files

Merge Android R

Bug: 168057903
Merged-In: Ice3e441cc9c0df8d0a6acc016bb74375e081bd67
Change-Id: I1d85742f594be2007c99841b290e502b6ede624e
parents 573f9d5f 3ed2bf0a
Loading
Loading
Loading
Loading
+127 −50
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SystemApi;
import android.app.ActivityThread;
import android.app.PropertyInvalidatedCache;
import android.bluetooth.BluetoothProfile.ConnectionPolicy;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.bluetooth.le.BluetoothLeScanner;
@@ -848,7 +849,7 @@ public final class BluetoothAdapter {
        synchronized (mLock) {
            if (sBluetoothLeScanner == null) {
                sBluetoothLeScanner = new BluetoothLeScanner(mManagerService, getOpPackageName(),
                        getFeatureId());
                        getAttributionTag());
            }
        }
        return sBluetoothLeScanner;
@@ -971,6 +972,55 @@ public final class BluetoothAdapter {
        return false;
    }

    private static final String BLUETOOTH_GET_STATE_CACHE_PROPERTY = "cache_key.bluetooth.get_state";

    private final PropertyInvalidatedCache<Void, Integer> mBluetoothGetStateCache =
            new PropertyInvalidatedCache<Void, Integer>(
                8, BLUETOOTH_GET_STATE_CACHE_PROPERTY) {
                @Override
                protected Integer recompute(Void query) {
                    try {
                        return mService.getState();
                    } catch (RemoteException e) {
                        throw e.rethrowFromSystemServer();
                    }
                }
            };

    /** @hide */
    public void disableBluetoothGetStateCache() {
        mBluetoothGetStateCache.disableLocal();
    }

    /** @hide */
    public static void invalidateBluetoothGetStateCache() {
        PropertyInvalidatedCache.invalidateCache(BLUETOOTH_GET_STATE_CACHE_PROPERTY);
    }

    /**
     * Fetch the current bluetooth state.  If the service is down, return
     * OFF.
     */
    @AdapterState
    private int getStateInternal() {
        int state = BluetoothAdapter.STATE_OFF;
        try {
            mServiceLock.readLock().lock();
            if (mService != null) {
                state = mBluetoothGetStateCache.query(null);
            }
        } catch (RuntimeException e) {
            if (e.getCause() instanceof RemoteException) {
                Log.e(TAG, "", e.getCause());
            } else {
                throw e;
            }
        } finally {
            mServiceLock.readLock().unlock();
        }
        return state;
    }

    /**
     * Get the current state of the local Bluetooth adapter.
     * <p>Possible return values are
@@ -984,18 +1034,7 @@ public final class BluetoothAdapter {
    @RequiresPermission(Manifest.permission.BLUETOOTH)
    @AdapterState
    public int getState() {
        int state = BluetoothAdapter.STATE_OFF;

        try {
            mServiceLock.readLock().lock();
            if (mService != null) {
                state = mService.getState();
            }
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        } finally {
            mServiceLock.readLock().unlock();
        }
        int state = getStateInternal();

        // Consider all internal states as OFF
        if (state == BluetoothAdapter.STATE_BLE_ON || state == BluetoothAdapter.STATE_BLE_TURNING_ON
@@ -1033,18 +1072,7 @@ public final class BluetoothAdapter {
    @UnsupportedAppUsage(publicAlternatives = "Use {@link #getState()} instead to determine "
            + "whether you can use BLE & BT classic.")
    public int getLeState() {
        int state = BluetoothAdapter.STATE_OFF;

        try {
            mServiceLock.readLock().lock();
            if (mService != null) {
                state = mService.getState();
            }
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        } finally {
            mServiceLock.readLock().unlock();
        }
        int state = getStateInternal();

        if (VDBG) {
            Log.d(TAG, "getLeState() returning " + BluetoothAdapter.nameForState(state));
@@ -1631,11 +1659,11 @@ public final class BluetoothAdapter {
        return ActivityThread.currentOpPackageName();
    }

    private String getFeatureId() {
    private String getAttributionTag() {
        // Workaround for legacy API for getting a BluetoothAdapter not
        // passing a context
        if (mContext != null) {
            return null;
            return mContext.getAttributionTag();
        }
        return null;
    }
@@ -1677,7 +1705,7 @@ public final class BluetoothAdapter {
        try {
            mServiceLock.readLock().lock();
            if (mService != null) {
                return mService.startDiscovery(getOpPackageName(), getFeatureId());
                return mService.startDiscovery(getOpPackageName(), getAttributionTag());
            }
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
@@ -1941,15 +1969,13 @@ public final class BluetoothAdapter {
        }
    }

    /**
     * Return true if offloaded filters are supported
     *
     * @return true if chipset supports on-chip filtering
     */
    public boolean isOffloadedFilteringSupported() {
        if (!getLeAccess()) {
            return false;
        }
    private static final String BLUETOOTH_FILTERING_CACHE_PROPERTY =
            "cache_key.bluetooth.is_offloaded_filtering_supported";
    private final PropertyInvalidatedCache<Void, Boolean> mBluetoothFilteringCache =
            new PropertyInvalidatedCache<Void, Boolean>(
                8, BLUETOOTH_FILTERING_CACHE_PROPERTY) {
                @Override
                protected Boolean recompute(Void query) {
                    try {
                        mServiceLock.readLock().lock();
                        if (mService != null) {
@@ -1961,6 +1987,30 @@ public final class BluetoothAdapter {
                        mServiceLock.readLock().unlock();
                    }
                    return false;

                }
            };

    /** @hide */
    public void disableIsOffloadedFilteringSupportedCache() {
        mBluetoothFilteringCache.disableLocal();
    }

    /** @hide */
    public static void invalidateIsOffloadedFilteringSupportedCache() {
        PropertyInvalidatedCache.invalidateCache(BLUETOOTH_FILTERING_CACHE_PROPERTY);
    }

    /**
     * Return true if offloaded filters are supported
     *
     * @return true if chipset supports on-chip filtering
     */
    public boolean isOffloadedFilteringSupported() {
        if (!getLeAccess()) {
            return false;
        }
        return mBluetoothFilteringCache.query(null);
    }

    /**
@@ -2332,6 +2382,43 @@ public final class BluetoothAdapter {
        return BluetoothAdapter.STATE_DISCONNECTED;
    }

    private static final String BLUETOOTH_PROFILE_CACHE_PROPERTY =
            "cache_key.bluetooth.get_profile_connection_state";
    private final PropertyInvalidatedCache<Integer, Integer>
            mGetProfileConnectionStateCache =
            new PropertyInvalidatedCache<Integer, Integer>(
                8, BLUETOOTH_PROFILE_CACHE_PROPERTY) {
                @Override
                protected Integer recompute(Integer query) {
                    try {
                        mServiceLock.readLock().lock();
                        if (mService != null) {
                            return mService.getProfileConnectionState(query);
                        }
                    } catch (RemoteException e) {
                        Log.e(TAG, "getProfileConnectionState:", e);
                    } finally {
                        mServiceLock.readLock().unlock();
                    }
                    return BluetoothProfile.STATE_DISCONNECTED;
                }
                @Override
                public String queryToString(Integer query) {
                    return String.format("getProfileConnectionState(profile=\"%d\")",
                                         query);
                }
            };

    /** @hide */
    public void disableGetProfileConnectionStateCache() {
        mGetProfileConnectionStateCache.disableLocal();
    }

    /** @hide */
    public static void invalidateGetProfileConnectionStateCache() {
        PropertyInvalidatedCache.invalidateCache(BLUETOOTH_PROFILE_CACHE_PROPERTY);
    }

    /**
     * Get the current connection state of a profile.
     * This function can be used to check whether the local Bluetooth adapter
@@ -2349,17 +2436,7 @@ public final class BluetoothAdapter {
        if (getState() != STATE_ON) {
            return BluetoothProfile.STATE_DISCONNECTED;
        }
        try {
            mServiceLock.readLock().lock();
            if (mService != null) {
                return mService.getProfileConnectionState(profile);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "getProfileConnectionState:", e);
        } finally {
            mServiceLock.readLock().unlock();
        }
        return BluetoothProfile.STATE_DISCONNECTED;
        return mGetProfileConnectionStateCache.query(new Integer(profile));
    }

    /**
+33 −21
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.app.PropertyInvalidatedCache;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.os.Handler;
@@ -1117,24 +1118,6 @@ public final class BluetoothDevice implements Parcelable {
        return false;
    }

    /**
     * Get the Bluetooth alias of the remote device.
     * If Alias is null, get the Bluetooth name instead.
     *
     * @return the Bluetooth alias, or null if no alias or there was a problem
     * @hide
     * @see #getAlias()
     * @see #getName()
     */
    @UnsupportedAppUsage(publicAlternatives = "Use {@link #getName()} instead.")
    public String getAliasName() {
        String name = getAlias();
        if (name == null) {
            name = getName();
        }
        return name;
    }

    /**
     * Get the most recent identified battery level of this Bluetooth device
     *
@@ -1324,6 +1307,31 @@ public final class BluetoothDevice implements Parcelable {
        return false;
    }

    private static final String BLUETOOTH_BONDING_CACHE_PROPERTY =
            "cache_key.bluetooth.get_bond_state";
    private final PropertyInvalidatedCache<BluetoothDevice, Integer> mBluetoothBondCache =
            new PropertyInvalidatedCache<BluetoothDevice, Integer>(
                8, BLUETOOTH_BONDING_CACHE_PROPERTY) {
                @Override
                protected Integer recompute(BluetoothDevice query) {
                    try {
                        return sService.getBondState(query);
                    } catch (RemoteException e) {
                        throw e.rethrowAsRuntimeException();
                    }
                }
            };

    /** @hide */
    public void disableBluetoothGetBondStateCache() {
        mBluetoothBondCache.disableLocal();
    }

    /** @hide */
    public static void invalidateBluetoothGetBondStateCache() {
        PropertyInvalidatedCache.invalidateCache(BLUETOOTH_BONDING_CACHE_PROPERTY);
    }

    /**
     * Get the bond state of the remote device.
     * <p>Possible values for the bond state are:
@@ -1341,9 +1349,13 @@ public final class BluetoothDevice implements Parcelable {
            return BOND_NONE;
        }
        try {
            return service.getBondState(this);
        } catch (RemoteException e) {
            return mBluetoothBondCache.query(this);
        } catch (RuntimeException e) {
            if (e.getCause() instanceof RemoteException) {
                Log.e(TAG, "", e);
            } else {
                throw e;
            }
        }
        return BOND_NONE;
    }
+1 −1
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ public final class BluetoothManager {
     * @hide
     */
    public BluetoothManager(Context context) {
        if (null == null) {
        if (context.getAttributionTag() == null) {
            context = context.getApplicationContext();
            if (context == null) {
                throw new IllegalArgumentException(
+138 −70
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.server;

import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY;
import static android.os.UserHandle.USER_SYSTEM;

import android.Manifest;
import android.app.ActivityManager;
import android.app.AppGlobals;
@@ -42,6 +45,7 @@ import android.content.ServiceConnection;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.content.pm.PackageManagerInternal;
import android.content.pm.UserInfo;
import android.database.ContentObserver;
import android.os.Binder;
@@ -66,15 +70,17 @@ import android.text.TextUtils;
import android.util.FeatureFlagUtils;
import android.util.Log;
import android.util.Slog;
import android.util.StatsLog;
import android.util.proto.ProtoOutputStream;

import com.android.internal.R;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.DumpUtils;
import com.android.internal.util.FrameworkStatsLog;
import com.android.server.pm.UserRestrictionsUtils;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Locale;
@@ -84,7 +90,6 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;


class BluetoothManagerService extends IBluetoothManager.Stub {
    private static final String TAG = "BluetoothManagerService";
    private static final boolean DBG = true;
@@ -201,6 +206,12 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    + " due to " + getEnableDisableReasonString(mReason) + " by " + mPackageName;
        }

        void dump(ProtoOutputStream proto) {
            proto.write(BluetoothManagerServiceDumpProto.ActiveLog.TIMESTAMP_MS, mTimestamp);
            proto.write(BluetoothManagerServiceDumpProto.ActiveLog.ENABLE, mEnable);
            proto.write(BluetoothManagerServiceDumpProto.ActiveLog.PACKAGE_NAME, mPackageName);
            proto.write(BluetoothManagerServiceDumpProto.ActiveLog.REASON, mReason);
        }
    }

    private final LinkedList<ActiveLog> mActiveLogs = new LinkedList<>();
@@ -254,10 +265,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    }

                    // DISALLOW_BLUETOOTH can only be set by DO or PO on the system user.
                    if (userId == UserHandle.USER_SYSTEM
                    if (userId == USER_SYSTEM
                            && UserRestrictionsUtils.restrictionsChanged(prevRestrictions,
                            newRestrictions, UserManager.DISALLOW_BLUETOOTH)) {
                        if (userId == UserHandle.USER_SYSTEM && newRestrictions.getBoolean(
                        if (userId == USER_SYSTEM && newRestrictions.getBoolean(
                                UserManager.DISALLOW_BLUETOOTH)) {
                            updateOppLauncherComponentState(userId, true); // Sharing disallowed
                            sendDisableMsg(BluetoothProtoEnums.ENABLE_DISABLE_REASON_DISALLOWED,
@@ -509,18 +520,18 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        }

        int systemUiUid = -1;
        try {
        // Check if device is configured with no home screen, which implies no SystemUI.
        boolean noHome = mContext.getResources().getBoolean(R.bool.config_noHomeScreen);
        if (!noHome) {
                systemUiUid = mContext.getPackageManager()
                        .getPackageUidAsUser("com.android.systemui", PackageManager.MATCH_SYSTEM_ONLY,
                                UserHandle.USER_SYSTEM);
            PackageManagerInternal pm = LocalServices.getService(PackageManagerInternal.class);
            systemUiUid = pm.getPackageUid(pm.getSystemUiServiceComponent().getPackageName(),
                    MATCH_SYSTEM_ONLY, USER_SYSTEM);
        }
        if (systemUiUid >= 0) {
            Slog.d(TAG, "Detected SystemUiUid: " + Integer.toString(systemUiUid));
        } catch (PackageManager.NameNotFoundException e) {
        } else {
            // Some platforms, such as wearables do not have a system ui.
            Slog.w(TAG, "Unable to resolve SystemUI's UID.", e);
            Slog.w(TAG, "Unable to resolve SystemUI's UID.");
        }
        mSystemUiUid = systemUiUid;
        DeviceConfig.addOnPropertiesChangedListener(
@@ -767,6 +778,35 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        return mIsHearingAidProfileSupported;
    }

    @Override
    /** @hide */
    public java.util.List<String> getSystemConfigEnabledProfilesForPackage(String packageName) {
        if (Binder.getCallingUid() != Process.BLUETOOTH_UID) {
            Slog.w(TAG, "getSystemConfigEnabledProfilesForPackage(): not allowed for non-bluetooth");
            return null;
        }

        SystemConfig systemConfig = SystemConfig.getInstance();
        if (systemConfig == null) {
            return null;
        }

        android.util.ArrayMap<String, Boolean> componentEnabledStates =
                systemConfig.getComponentsEnabledStates(packageName);
        if (componentEnabledStates == null) {
            return null;
        }

        ArrayList enabledProfiles = new ArrayList<String>();
        for (Map.Entry<String, Boolean> entry : componentEnabledStates.entrySet()) {
            if (entry.getValue()) {
                enabledProfiles.add(entry.getKey());
            }
        }

        return enabledProfiles;
    }

    // Monitor change of BLE scan only mode settings.
    private void registerForBleScanModeChange() {
        ContentObserver contentObserver = new ContentObserver(null) {
@@ -2481,9 +2521,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    new ActiveLog(reason, packageName, enable, System.currentTimeMillis()));
        }

        int state = enable ? StatsLog.BLUETOOTH_ENABLED_STATE_CHANGED__STATE__ENABLED :
                             StatsLog.BLUETOOTH_ENABLED_STATE_CHANGED__STATE__DISABLED;
        StatsLog.write_non_chained(StatsLog.BLUETOOTH_ENABLED_STATE_CHANGED,
        int state = enable ? FrameworkStatsLog.BLUETOOTH_ENABLED_STATE_CHANGED__STATE__ENABLED :
                             FrameworkStatsLog.BLUETOOTH_ENABLED_STATE_CHANGED__STATE__DISABLED;
        FrameworkStatsLog.write_non_chained(FrameworkStatsLog.BLUETOOTH_ENABLED_STATE_CHANGED,
                Binder.getCallingUid(), null, state, reason, packageName);
    }

@@ -2590,11 +2630,12 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) {
            return;
        }
        if ((args.length > 0) && args[0].startsWith("--proto")) {
            dumpProto(fd);
            return;
        }
        String errorMsg = null;

        boolean protoOut = (args.length > 0) && args[0].startsWith("--proto");

        if (!protoOut) {
        writer.println("Bluetooth Status");
        writer.println("  enabled: " + isEnabled());
        writer.println("  state: " + BluetoothAdapter.nameForState(mState));
@@ -2646,7 +2687,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
            args = new String[1];
            args[0] = "--print";
        }
        }

        if (mBluetoothBinder == null) {
            errorMsg = "Bluetooth Service not connected";
@@ -2658,14 +2698,42 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
            }
        }
        if (errorMsg != null) {
            // Silently return if we are extracting metrics in Protobuf format
            if (protoOut) {
                return;
            }
            writer.println(errorMsg);
        }
    }

    private void dumpProto(FileDescriptor fd) {
        final ProtoOutputStream proto = new ProtoOutputStream(fd);
        proto.write(BluetoothManagerServiceDumpProto.ENABLED, isEnabled());
        proto.write(BluetoothManagerServiceDumpProto.STATE, mState);
        proto.write(BluetoothManagerServiceDumpProto.STATE_NAME,
                BluetoothAdapter.nameForState(mState));
        proto.write(BluetoothManagerServiceDumpProto.ADDRESS, mAddress);
        proto.write(BluetoothManagerServiceDumpProto.NAME, mName);
        if (mEnable) {
            proto.write(BluetoothManagerServiceDumpProto.LAST_ENABLED_TIME_MS, mLastEnabledTime);
        }
        proto.write(BluetoothManagerServiceDumpProto.CURR_TIMESTAMP_MS,
                SystemClock.elapsedRealtime());
        for (ActiveLog log : mActiveLogs) {
            long token = proto.start(BluetoothManagerServiceDumpProto.ACTIVE_LOGS);
            log.dump(proto);
            proto.end(token);
        }
        proto.write(BluetoothManagerServiceDumpProto.NUM_CRASHES, mCrashes);
        proto.write(BluetoothManagerServiceDumpProto.CRASH_LOG_MAXED,
                mCrashes == CRASH_LOG_MAX_SIZE);
        for (Long time : mCrashTimestamps) {
            proto.write(BluetoothManagerServiceDumpProto.CRASH_TIMESTAMPS_MS, time);
        }
        proto.write(BluetoothManagerServiceDumpProto.NUM_BLE_APPS, mBleApps.size());
        for (ClientDeathRecipient app : mBleApps.values()) {
            proto.write(BluetoothManagerServiceDumpProto.BLE_APP_PACKAGE_NAMES,
                    app.getPackageName());
        }
        proto.flush();
    }

    private static String getEnableDisableReasonString(int reason) {
        switch (reason) {
            case BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST:
+2 −3
Original line number Diff line number Diff line
@@ -18,8 +18,7 @@ package com.android.server;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;

import com.android.internal.os.RoSystemProperties;
import android.os.UserManager;

class BluetoothService extends SystemService {
    private BluetoothManagerService mBluetoothManagerService;
@@ -47,7 +46,7 @@ class BluetoothService extends SystemService {
            publishBinderService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE,
                    mBluetoothManagerService);
        } else if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY &&
                !RoSystemProperties.MULTIUSER_HEADLESS_SYSTEM_USER) {
                !UserManager.isHeadlessSystemUserMode()) {
            initialize();
        }
    }