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

Commit 92cc6eba authored by Automerger Merge Worker's avatar Automerger Merge Worker
Browse files

Merge "Refactor BluetoothPbap APIs used by Settings" am: a33dfe1e am: 7231799d

Change-Id: I6167bbc3c0cf1a725816dfb4b6ccfd0997fdc65c
parents 61f1ecb2 7231799d
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -1418,6 +1418,11 @@ package android.bluetooth {
    field public static final int REMOTE_PANU_ROLE = 2; // 0x2
  }
  public class BluetoothPbap implements android.bluetooth.BluetoothProfile {
    method public int getConnectionState(@Nullable android.bluetooth.BluetoothDevice);
    field public static final String ACTION_CONNECTION_STATE_CHANGED = "android.bluetooth.pbap.profile.action.CONNECTION_STATE_CHANGED";
  }
  public interface BluetoothProfile {
    field public static final int CONNECTION_POLICY_ALLOWED = 100; // 0x64
    field public static final int CONNECTION_POLICY_FORBIDDEN = 0; // 0x0
+51 −44
Original line number Diff line number Diff line
@@ -16,7 +16,10 @@

package android.bluetooth;

import android.annotation.Nullable;
import android.annotation.SdkConstant;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.annotation.UnsupportedAppUsage;
import android.content.ComponentName;
import android.content.Context;
@@ -54,6 +57,7 @@ import java.util.List;
 *
 * @hide
 */
@SystemApi
public class BluetoothPbap implements BluetoothProfile {

    private static final String TAG = "BluetoothPbap";
@@ -75,7 +79,11 @@ public class BluetoothPbap implements BluetoothProfile {
     *  {@link BluetoothProfile#STATE_DISCONNECTING}.
     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to
     * receive.
     *
     * @hide
     */
    @SuppressLint("ActionValue")
    @SystemApi
    @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String ACTION_CONNECTION_STATE_CHANGED =
            "android.bluetooth.pbap.profile.action.CONNECTION_STATE_CHANGED";
@@ -85,33 +93,16 @@ public class BluetoothPbap implements BluetoothProfile {
    private ServiceListener mServiceListener;
    private BluetoothAdapter mAdapter;

    /** @hide */
    public static final int RESULT_FAILURE = 0;
    /** @hide */
    public static final int RESULT_SUCCESS = 1;
    /** Connection canceled before completion. */
    public static final int RESULT_CANCELED = 2;

    /**
     * An interface for notifying Bluetooth PCE IPC clients when they have
     * been connected to the BluetoothPbap service.
     */
    public interface ServiceListener {
    /**
         * Called to notify the client when this proxy object has been
         * connected to the BluetoothPbap service. Clients must wait for
         * this callback before making IPC calls on the BluetoothPbap
         * service.
         */
        public void onServiceConnected(BluetoothPbap proxy);

        /**
         * Called to notify the client that this proxy object has been
         * disconnected from the BluetoothPbap service. Clients must not
         * make IPC calls on the BluetoothPbap service after this callback.
         * This callback will currently only occur if the application hosting
         * the BluetoothPbap service, but may be called more often in future.
     * Connection canceled before completion.
     *
     * @hide
     */
        public void onServiceDisconnected();
    }
    public static final int RESULT_CANCELED = 2;

    private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
            new IBluetoothStateChangeCallback.Stub() {
@@ -127,6 +118,8 @@ public class BluetoothPbap implements BluetoothProfile {

    /**
     * Create a BluetoothPbap proxy object.
     *
     * @hide
     */
    public BluetoothPbap(Context context, ServiceListener l) {
        mContext = context;
@@ -181,6 +174,7 @@ public class BluetoothPbap implements BluetoothProfile {
        }
    }

    /** @hide */
    protected void finalize() throws Throwable {
        try {
            close();
@@ -194,6 +188,8 @@ public class BluetoothPbap implements BluetoothProfile {
     * Other public functions of BluetoothPbap will return default error
     * results once close() has been called. Multiple invocations of close()
     * are ok.
     *
     * @hide
     */
    public synchronized void close() {
        IBluetoothManager mgr = mAdapter.getBluetoothManager();
@@ -210,6 +206,8 @@ public class BluetoothPbap implements BluetoothProfile {

    /**
     * {@inheritDoc}
     *
     * @hide
     */
    @Override
    public List<BluetoothDevice> getConnectedDevices() {
@@ -229,17 +227,22 @@ public class BluetoothPbap implements BluetoothProfile {

    /**
     * {@inheritDoc}
     *
     * @hide
     */
    @SystemApi
    @Override
    public int getConnectionState(BluetoothDevice device) {
    public int getConnectionState(@Nullable BluetoothDevice device) {
        log("getConnectionState: device=" + device);
        try {
            final IBluetoothPbap service = mService;
            if (service != null && isEnabled() && isValidDevice(device)) {
                return service.getConnectionState(device);
            }
            if (service == null) {
                Log.w(TAG, "Proxy not attached to service");
            return BluetoothProfile.STATE_DISCONNECTED;
            }
        try {
            return service.getConnectionState(device);
            return BluetoothProfile.STATE_DISCONNECTED;
        } catch (RemoteException e) {
            Log.e(TAG, e.toString());
        }
@@ -248,6 +251,8 @@ public class BluetoothPbap implements BluetoothProfile {

    /**
     * {@inheritDoc}
     *
     * @hide
     */
    @Override
    public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
@@ -265,23 +270,13 @@ public class BluetoothPbap implements BluetoothProfile {
        return new ArrayList<BluetoothDevice>();
    }

    /**
     * Returns true if the specified Bluetooth device is connected (does not
     * include connecting). Returns false if not connected, or if this proxy
     * object is not currently connected to the Pbap service.
     */
    // TODO: This is currently being used by SettingsLib and internal app.
    public boolean isConnected(BluetoothDevice device) {
        return getConnectionState(device) == BluetoothAdapter.STATE_CONNECTED;
    }

    /**
     * Disconnects the current Pbap client (PCE). Currently this call blocks,
     * it may soon be made asynchronous. Returns false if this proxy object is
     * not currently connected to the Pbap service.
     *
     * @hide
     */
    // TODO: This is currently being used by SettingsLib and will be used in the future.
    // TODO: Must specify target device. Implement this in the service.
    @UnsupportedAppUsage
    public boolean disconnect(BluetoothDevice device) {
        log("disconnect()");
@@ -304,7 +299,7 @@ public class BluetoothPbap implements BluetoothProfile {
            log("Proxy object connected");
            mService = IBluetoothPbap.Stub.asInterface(service);
            if (mServiceListener != null) {
                mServiceListener.onServiceConnected(BluetoothPbap.this);
                mServiceListener.onServiceConnected(BluetoothProfile.PBAP, BluetoothPbap.this);
            }
        }

@@ -312,11 +307,23 @@ public class BluetoothPbap implements BluetoothProfile {
            log("Proxy object disconnected");
            doUnbind();
            if (mServiceListener != null) {
                mServiceListener.onServiceDisconnected();
                mServiceListener.onServiceDisconnected(BluetoothProfile.PBAP);
            }
        }
    };

    private boolean isEnabled() {
        if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true;
        return false;
    }

    private boolean isValidDevice(BluetoothDevice device) {
        if (device == null) return false;

        if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
        return false;
    }

    private static void log(String msg) {
        if (DBG) {
            Log.d(TAG, msg);
+12 −12
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settingslib.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothPbap;
@@ -52,14 +53,16 @@ public class PbapServerProfile implements LocalBluetoothProfile {

    // These callbacks run on the main thread.
    private final class PbapServiceListener
            implements BluetoothPbap.ServiceListener {
            implements BluetoothProfile.ServiceListener {

        public void onServiceConnected(BluetoothPbap proxy) {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            mService = (BluetoothPbap) proxy;
            mIsProfileReady=true;
        }

        public void onServiceDisconnected() {
        @Override
        public void onServiceDisconnected(int profile) {
            mIsProfileReady=false;
        }
    }
@@ -74,7 +77,8 @@ public class PbapServerProfile implements LocalBluetoothProfile {
    }

    PbapServerProfile(Context context) {
        BluetoothPbap pbap = new BluetoothPbap(context, new PbapServiceListener());
        BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, new PbapServiceListener(),
                BluetoothProfile.PBAP);
    }

    public boolean accessProfileEnabled() {
@@ -97,13 +101,8 @@ public class PbapServerProfile implements LocalBluetoothProfile {
    }

    public int getConnectionStatus(BluetoothDevice device) {
        if (mService == null) {
            return BluetoothProfile.STATE_DISCONNECTED;
        }
        if (mService.isConnected(device))
            return BluetoothProfile.STATE_CONNECTED;
        else
            return BluetoothProfile.STATE_DISCONNECTED;
        if (mService == null) return BluetoothProfile.STATE_DISCONNECTED;
        return mService.getConnectionState(device);
    }

    public boolean isPreferred(BluetoothDevice device) {
@@ -142,7 +141,8 @@ public class PbapServerProfile implements LocalBluetoothProfile {
        Log.d(TAG, "finalize()");
        if (mService != null) {
            try {
                mService.close();
                BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.PBAP,
                        mService);
                mService = null;
            }catch (Throwable t) {
                Log.w(TAG, "Error cleaning up PBAP proxy", t);