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

Commit 0f42037e authored by fredc's avatar fredc Committed by Android (Google) Code Review
Browse files

Non persistent adapter service

Change-Id: Ib13d5c77416e58161df0e04d7a15ec0dddbde8b5

Conflicts:

	core/java/android/bluetooth/BluetoothInputDevice.java

Conflicts:

	core/java/com/android/internal/app/ShutdownThread.java
	services/java/com/android/server/SystemServer.java

Conflicts:

	services/java/com/android/server/SystemServer.java
	services/java/com/android/server/pm/ShutdownThread.java
parent 919a4c62
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -93,6 +93,9 @@ LOCAL_SRC_FILES += \
	core/java/android/bluetooth/IBluetoothHealth.aidl \
	core/java/android/bluetooth/IBluetoothHealthCallback.aidl \
	core/java/android/bluetooth/IBluetoothInputDevice.aidl \
	core/java/android/bluetooth/IBluetoothPan.aidl \
	core/java/android/bluetooth/IBluetoothManager.aidl \
	core/java/android/bluetooth/IBluetoothManagerCallback.aidl \
	core/java/android/bluetooth/IBluetoothPbap.aidl \
	core/java/android/bluetooth/IBluetoothStateChangeCallback.aidl \
	core/java/android/content/IClipboard.aidl \
+2 −0
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ package android {
    field public static final java.lang.String MODIFY_PHONE_STATE = "android.permission.MODIFY_PHONE_STATE";
    field public static final java.lang.String MOUNT_FORMAT_FILESYSTEMS = "android.permission.MOUNT_FORMAT_FILESYSTEMS";
    field public static final java.lang.String MOUNT_UNMOUNT_FILESYSTEMS = "android.permission.MOUNT_UNMOUNT_FILESYSTEMS";
    field public static final java.lang.String NET_TUNNELING = "android.permission.NET_TUNNELING";
    field public static final java.lang.String NFC = "android.permission.NFC";
    field public static final deprecated java.lang.String PERSISTENT_ACTIVITY = "android.permission.PERSISTENT_ACTIVITY";
    field public static final java.lang.String PROCESS_OUTGOING_CALLS = "android.permission.PROCESS_OUTGOING_CALLS";
@@ -4438,6 +4439,7 @@ package android.appwidget {
package android.bluetooth {
  public final class BluetoothA2dp implements android.bluetooth.BluetoothProfile {
    method public void finalize();
    method public java.util.List<android.bluetooth.BluetoothDevice> getConnectedDevices();
    method public int getConnectionState(android.bluetooth.BluetoothDevice);
    method public java.util.List<android.bluetooth.BluetoothDevice> getDevicesMatchingConnectionStates(int[]);
+62 −1
Original line number Diff line number Diff line
@@ -108,6 +108,36 @@ public final class BluetoothA2dp implements BluetoothProfile {
    private IBluetoothA2dp mService;
    private BluetoothAdapter mAdapter;

    final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
            new IBluetoothStateChangeCallback.Stub() {
                public void onBluetoothStateChange(boolean up) {
                    if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
                    if (!up) {
                        if (DBG) Log.d(TAG,"Unbinding service...");
                        synchronized (mConnection) {
                            try {
                                mService = null;
                                mContext.unbindService(mConnection);
                            } catch (Exception re) {
                                Log.e(TAG,"",re);
                            }
                        }
                    } else {
                        synchronized (mConnection) {
                            try {
                                if (mService == null) {
                                    if (DBG) Log.d(TAG,"Binding service...");
                                    if (!mContext.bindService(new Intent(IBluetoothA2dp.class.getName()), mConnection, 0)) {
                                        Log.e(TAG, "Could not bind to Bluetooth A2DP Service");
                                    }
                                }
                            } catch (Exception re) {
                                Log.e(TAG,"",re);
                            }
                        }
                    }
                }
        };
    /**
     * Create a BluetoothA2dp proxy object for interacting with the local
     * Bluetooth A2DP service.
@@ -117,6 +147,15 @@ public final class BluetoothA2dp implements BluetoothProfile {
        mContext = context;
        mServiceListener = l;
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        IBluetoothManager mgr = mAdapter.getBluetoothManager();
        if (mgr != null) {
            try {
                mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
            } catch (RemoteException e) {
                Log.e(TAG,"",e);
            }
        }

        if (!context.bindService(new Intent(IBluetoothA2dp.class.getName()), mConnection, 0)) {
            Log.e(TAG, "Could not bind to Bluetooth A2DP Service");
        }
@@ -124,8 +163,30 @@ public final class BluetoothA2dp implements BluetoothProfile {

    /*package*/ void close() {
        mServiceListener = null;
        IBluetoothManager mgr = mAdapter.getBluetoothManager();
        if (mgr != null) {
            try {
                mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
            } catch (Exception e) {
                Log.e(TAG,"",e);
            }
        }

        synchronized (mConnection) {
            if (mService != null) {
                try {
                    mService = null;
                    mContext.unbindService(mConnection);
                } catch (Exception re) {
                    Log.e(TAG,"",re);
                }
            }
        }
    }

    public void finalize() {
        close();
    }
    /**
     * Initiate connection to a profile of the remote bluetooth device.
     *
@@ -260,7 +321,7 @@ public final class BluetoothA2dp implements BluetoothProfile {
     * Set priority of the profile
     *
     * <p> The device should already be paired.
     *  Priority can be one of {@link #PRIORITY_ON} or
     *  Priority can be one of {@link #PRIORITY_ON} orgetBluetoothManager
     * {@link #PRIORITY_OFF},
     *
     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
+117 −30
Original line number Diff line number Diff line
@@ -73,7 +73,7 @@ import java.util.UUID;
 */
public final class BluetoothAdapter {
    private static final String TAG = "BluetoothAdapter";
    private static final boolean DBG = false;
    private static final boolean DBG = true;

    /**
     * Sentinel error value for this class. Guaranteed to not equal any other
@@ -343,7 +343,7 @@ public final class BluetoothAdapter {
    public static final int STATE_DISCONNECTING = 3;

    /** @hide */
    public static final String BLUETOOTH_SERVICE = "bluetooth";
    public static final String BLUETOOTH_MANAGER_SERVICE = "bluetooth_manager";

    private static final int ADDRESS_LENGTH = 17;

@@ -353,7 +353,8 @@ public final class BluetoothAdapter {
     */
    private static BluetoothAdapter sAdapter;

    private final IBluetooth mService;
    private final IBluetoothManager mManagerService;
    private IBluetooth mService;

    private Handler mServiceRecordHandler;

@@ -367,10 +368,10 @@ public final class BluetoothAdapter {
     */
    public static synchronized BluetoothAdapter getDefaultAdapter() {
        if (sAdapter == null) {
            IBinder b = ServiceManager.getService("bluetooth");
            IBinder b = ServiceManager.getService(BLUETOOTH_MANAGER_SERVICE);
            if (b != null) {
                IBluetooth service = IBluetooth.Stub.asInterface(b);
                sAdapter = new BluetoothAdapter(service);
                IBluetoothManager managerService = IBluetoothManager.Stub.asInterface(b);
                sAdapter = new BluetoothAdapter(managerService);
            } else {
                Log.e(TAG, "Bluetooth binder is null");
            }
@@ -381,11 +382,15 @@ public final class BluetoothAdapter {
    /**
     * Use {@link #getDefaultAdapter} to get the BluetoothAdapter instance.
     */
    BluetoothAdapter(IBluetooth service) {
        if (service == null) {
            throw new IllegalArgumentException("service is null");
    BluetoothAdapter(IBluetoothManager managerService) {

        if (managerService == null) {
            throw new IllegalArgumentException("bluetooth manager service is null");
        }
        mService = service;
        try {
            mService = managerService.registerAdapter(mManagerCallback);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        mManagerService = managerService;
        mServiceRecordHandler = null;
    }

@@ -402,6 +407,10 @@ public final class BluetoothAdapter {
     * @throws IllegalArgumentException if address is invalid
     */
    public BluetoothDevice getRemoteDevice(String address) {
        if (mService == null) {
            Log.e(TAG, "BT not enabled. Cannot create Remote Device");
            return null;
        }
        return new BluetoothDevice(address);
    }

@@ -433,8 +442,11 @@ public final class BluetoothAdapter {
     * @return true if the local adapter is turned on
     */
    public boolean isEnabled() {

        try {
            return mService.isEnabled();
            synchronized(mManagerCallback) {
                if (mService != null) return mService.isEnabled();
            }
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
@@ -451,9 +463,15 @@ public final class BluetoothAdapter {
     * @return current state of Bluetooth adapter
     */
    public int getState() {
        if (mService == null) return STATE_OFF;
        try {
            synchronized(mManagerCallback) {
                if (mService != null)
                {
                    return mService.getState();
                }
                // TODO(BT) there might be a small gap during STATE_TURNING_ON that
                //          mService is null, handle that case
            }
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return STATE_OFF;
    }
@@ -486,8 +504,13 @@ public final class BluetoothAdapter {
     *         immediate error
     */
    public boolean enable() {

        boolean enabled = false;
        try {
            return mService.enable();
            enabled = mManagerService.enable();
            if (enabled) {
                // TODO(BT)
            }
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
@@ -518,7 +541,7 @@ public final class BluetoothAdapter {
     */
    public boolean disable() {
        try {
            return mService.disable(true);
            return mManagerService.disable(true);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
@@ -534,8 +557,9 @@ public final class BluetoothAdapter {
     * @hide
     */
    public boolean disable(boolean persist) {

        try {
            return mService.disable(persist);
            return mManagerService.disable(persist);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
@@ -549,7 +573,7 @@ public final class BluetoothAdapter {
     */
    public String getAddress() {
        try {
            return mService.getAddress();
            return mManagerService.getAddress();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }
@@ -563,7 +587,7 @@ public final class BluetoothAdapter {
     */
    public String getName() {
        try {
            return mService.getName();
            return mManagerService.getName();
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }
@@ -579,7 +603,9 @@ public final class BluetoothAdapter {
    public ParcelUuid[] getUuids() {
        if (getState() != STATE_ON) return null;
        try {
            return mService.getUuids();
            synchronized(mManagerCallback) {
                if (mService != null) return mService.getUuids();
            }
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }
@@ -602,7 +628,9 @@ public final class BluetoothAdapter {
    public boolean setName(String name) {
        if (getState() != STATE_ON) return false;
        try {
            return mService.setName(name);
            synchronized(mManagerCallback) {
                if (mService != null) return mService.setName(name);
            }
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
@@ -626,7 +654,9 @@ public final class BluetoothAdapter {
    public int getScanMode() {
        if (getState() != STATE_ON) return SCAN_MODE_NONE;
        try {
            return mService.getScanMode();
            synchronized(mManagerCallback) {
                if (mService != null) return mService.getScanMode();
            }
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return SCAN_MODE_NONE;
    }
@@ -662,7 +692,9 @@ public final class BluetoothAdapter {
    public boolean setScanMode(int mode, int duration) {
        if (getState() != STATE_ON) return false;
        try {
            return mService.setScanMode(mode, duration);
            synchronized(mManagerCallback) {
                if (mService != null) return mService.setScanMode(mode, duration);
            }
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
@@ -678,7 +710,9 @@ public final class BluetoothAdapter {
    public int getDiscoverableTimeout() {
        if (getState() != STATE_ON) return -1;
        try {
            return mService.getDiscoverableTimeout();
            synchronized(mManagerCallback) {
                if (mService != null) return mService.getDiscoverableTimeout();
            }
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return -1;
    }
@@ -687,7 +721,9 @@ public final class BluetoothAdapter {
    public void setDiscoverableTimeout(int timeout) {
        if (getState() != STATE_ON) return;
        try {
            mService.setDiscoverableTimeout(timeout);
            synchronized(mManagerCallback) {
                if (mService != null) mService.setDiscoverableTimeout(timeout);
            }
        } catch (RemoteException e) {Log.e(TAG, "", e);}
    }

@@ -724,7 +760,9 @@ public final class BluetoothAdapter {
    public boolean startDiscovery() {
        if (getState() != STATE_ON) return false;
        try {
            return mService.startDiscovery();
            synchronized(mManagerCallback) {
                if (mService != null) return mService.startDiscovery();
            }
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
@@ -749,7 +787,9 @@ public final class BluetoothAdapter {
    public boolean cancelDiscovery() {
        if (getState() != STATE_ON) return false;
        try {
            return mService.cancelDiscovery();
            synchronized(mManagerCallback) {
                if (mService != null) return mService.cancelDiscovery();
            }
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
@@ -776,7 +816,9 @@ public final class BluetoothAdapter {
    public boolean isDiscovering() {
        if (getState() != STATE_ON) return false;
        try {
            return mService.isDiscovering();
            synchronized(mManagerCallback) {
                if (mService != null ) return mService.isDiscovering();
            }
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
@@ -797,7 +839,10 @@ public final class BluetoothAdapter {
            return toDeviceSet(new BluetoothDevice[0]);
        }
        try {
            return toDeviceSet(mService.getBondedDevices());
            synchronized(mManagerCallback) {
                if (mService != null) return toDeviceSet(mService.getBondedDevices());
            }
            return toDeviceSet(new BluetoothDevice[0]);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }
@@ -818,7 +863,9 @@ public final class BluetoothAdapter {
    public int getConnectionState() {
        if (getState() != STATE_ON) return BluetoothAdapter.STATE_DISCONNECTED;
        try {
            return mService.getAdapterConnectionState();
            synchronized(mManagerCallback) {
                if (mService != null) return mService.getAdapterConnectionState();
            }
        } catch (RemoteException e) {Log.e(TAG, "getConnectionState:", e);}
        return BluetoothAdapter.STATE_DISCONNECTED;
    }
@@ -841,7 +888,9 @@ public final class BluetoothAdapter {
    public int getProfileConnectionState(int profile) {
        if (getState() != STATE_ON) return BluetoothProfile.STATE_DISCONNECTED;
        try {
            return mService.getProfileConnectionState(profile);
            synchronized(mManagerCallback) {
                if (mService != null) return mService.getProfileConnectionState(profile);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "getProfileConnectionState:", e);
        }
@@ -1160,6 +1209,23 @@ public final class BluetoothAdapter {
        }
    }

    final private IBluetoothManagerCallback mManagerCallback =
        new IBluetoothManagerCallback.Stub() {
            public void onBluetoothServiceUp(IBluetooth bluetoothService) {
                if (DBG) Log.d(TAG, "onBluetoothServiceUp: " + bluetoothService);
                synchronized (mManagerCallback) {
                    mService = bluetoothService;
                }
            }

            public void onBluetoothServiceDown() {
                if (DBG) Log.d(TAG, "onBluetoothServiceDown: " + mService);
                synchronized (mManagerCallback) {
                    mService = null;
                }
            }
    };

    /**
     * Enable the Bluetooth Adapter, but don't auto-connect devices
     * and don't persist state. Only for use by system applications.
@@ -1245,6 +1311,17 @@ public final class BluetoothAdapter {
        return Collections.unmodifiableSet(deviceSet);
    }

    protected void finalize() throws Throwable {
        try {
            mManagerService.unregisterAdapter(mManagerCallback);
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        } finally {
            super.finalize();
        }
    }


    /**
     * Validate a String Bluetooth address, such as "00:43:A8:23:10:F0"
     * <p>Alphabetic characters must be uppercase to be valid.
@@ -1275,4 +1352,14 @@ public final class BluetoothAdapter {
        }
        return true;
    }

    /*package*/ IBluetoothManager getBluetoothManager() {
            return mManagerService;
    }

    /*package*/ IBluetooth getBluetoothService() {
        synchronized (mManagerCallback) {
            return mService;
        }
    }
}
+52 −5
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ import java.util.UUID;
 */
public final class BluetoothDevice implements Parcelable {
    private static final String TAG = "BluetoothDevice";
    private static final boolean DBG = true;

    /**
     * Sentinel error value for this class. Guaranteed to not equal any other
@@ -483,11 +484,8 @@ public final class BluetoothDevice implements Parcelable {
    /*package*/ static IBluetooth getService() {
        synchronized (BluetoothDevice.class) {
            if (sService == null) {
                IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE);
                if (b == null) {
                    throw new RuntimeException("Bluetooth service not available");
                }
                sService = IBluetooth.Stub.asInterface(b);
                BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
                sService = adapter.getBluetoothService();
            }
        }
        return sService;
@@ -561,6 +559,7 @@ public final class BluetoothDevice implements Parcelable {
     * @return Bluetooth hardware address as string
     */
    public String getAddress() {
        if (DBG) Log.d(TAG, "mAddress: " + mAddress);
        return mAddress;
    }

@@ -575,6 +574,10 @@ public final class BluetoothDevice implements Parcelable {
     * @return the Bluetooth name, or null if there was a problem.
     */
    public String getName() {
        if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot get Remote Device name");
            return null;
        }
        try {
            return sService.getRemoteName(this);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
@@ -589,6 +592,10 @@ public final class BluetoothDevice implements Parcelable {
     * @hide
     */
    public String getAlias() {
        if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot get Remote Device Alias");
            return null;
        }
        try {
            return sService.getRemoteAlias(this);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
@@ -606,6 +613,10 @@ public final class BluetoothDevice implements Parcelable {
     * @hide
     */
    public boolean setAlias(String alias) {
        if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot set Remote Device name");
            return false;
        }
        try {
            return sService.setRemoteAlias(this, alias);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
@@ -642,6 +653,10 @@ public final class BluetoothDevice implements Parcelable {
     * @hide
     */
    public boolean createBond() {
        if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device");
            return false;
        }
        try {
            return sService.createBond(this);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
@@ -706,6 +721,10 @@ public final class BluetoothDevice implements Parcelable {
     * @hide
     */
    public boolean cancelBondProcess() {
        if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond");
            return false;
        }
        try {
            return sService.cancelBondProcess(this);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
@@ -723,6 +742,10 @@ public final class BluetoothDevice implements Parcelable {
     * @hide
     */
    public boolean removeBond() {
        if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond");
            return false;
        }
        try {
            return sService.removeBond(this);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
@@ -740,6 +763,10 @@ public final class BluetoothDevice implements Parcelable {
     * @return the bond state
     */
    public int getBondState() {
        if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot get bond state");
            return BOND_NONE;
        }
        try {
            return sService.getBondState(this);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
@@ -753,6 +780,10 @@ public final class BluetoothDevice implements Parcelable {
     * @return Bluetooth class object, or null on error
     */
    public BluetoothClass getBluetoothClass() {
        if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class");
            return null;
        }
        try {
            int classInt = sService.getRemoteClass(this);
            if (classInt == BluetoothClass.ERROR) return null;
@@ -807,6 +838,10 @@ public final class BluetoothDevice implements Parcelable {
     *         or null on error
     */
     public ParcelUuid[] getUuids() {
         if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot get remote device Uuids");
             return null;
         }
        try {
            return sService.getRemoteUuids(this);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
@@ -847,6 +882,10 @@ public final class BluetoothDevice implements Parcelable {

    /** @hide */
    public boolean setPin(byte[] pin) {
        if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot set Remote Device pin");
            return false;
        }
        try {
            return sService.setPin(this, true, pin.length, pin);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
@@ -865,6 +904,10 @@ public final class BluetoothDevice implements Parcelable {

    /** @hide */
    public boolean setPairingConfirmation(boolean confirm) {
        if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot set pairing confirmation");
            return false;
        }
        try {
            return sService.setPairingConfirmation(this, confirm);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
@@ -883,6 +926,10 @@ public final class BluetoothDevice implements Parcelable {

    /** @hide */
    public boolean cancelPairingUserInput() {
        if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot create pairing user input");
            return false;
        }
        try {
            return sService.cancelBondProcess(this);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
Loading