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

Commit f15281ea authored by Sanket Agarwal's avatar Sanket Agarwal Committed by Gerrit Code Review
Browse files

Merge "Connect PbapClient Profile"

parents c3caeade 71e44fe2
Loading
Loading
Loading
Loading
+7 −11
Original line number Diff line number Diff line
@@ -69,18 +69,22 @@ class PbapClientConnectionHandler extends Handler {
    private final BluetoothAdapter mAdapter;
    private final BluetoothDevice mDevice;
    private ClientSession mObexSession;
    private Context mContext;
    private BluetoothPbapObexAuthenticator mAuth = null;
    private final PbapClientStateMachine mPbapClientStateMachine;
    private boolean mAccountCreated;

    PbapClientConnectionHandler(Looper looper, PbapClientStateMachine stateMachine,
    PbapClientConnectionHandler(Looper looper, Context context, PbapClientStateMachine stateMachine,
            BluetoothDevice device) {
        super(looper);
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mDevice = device;
        mContext = context;
        mPbapClientStateMachine = stateMachine;
        mAuth = new BluetoothPbapObexAuthenticator(this);
        mAccountManager = AccountManager.get(mPbapClientStateMachine.getContext());
        mAccount = new Account(mDevice.getAddress(), mContext.getString(
                R.string.pbap_account_type));
    }

    @Override
@@ -94,10 +98,6 @@ class PbapClientConnectionHandler extends Handler {
                    /* To establish a connection first open a socket, establish a OBEX Transport
                     * abstraction, establish a Bluetooth Authenticator, and finally attempt to
                     * connect via an OBEX session */
                    mAccount = new Account(mDevice.getAddress(),
                            mPbapClientStateMachine.getContext()
                            .getString(R.string.pbap_account_type));

                    mSocket = mDevice.createRfcommSocketToServiceRecord(
                            BluetoothUuid.PBAP_PSE.getUuid());
                    mSocket.connect();
@@ -139,21 +139,17 @@ class PbapClientConnectionHandler extends Handler {
                    Log.w(TAG,"DISCONNECT Failure " + e.toString());
                }
                removeAccount(mAccount);
                mPbapClientStateMachine.getContext().getContentResolver()
                mContext.getContentResolver()
                        .delete(CallLog.Calls.CONTENT_URI, null, null);
                mPbapClientStateMachine.obtainMessage(
                        PbapClientStateMachine.MSG_CONNECTION_CLOSED).sendToTarget();
                break;

            case MSG_DOWNLOAD:
                if (mAccountCreated == true) {
                    // If the account exists download has already completed, don't try again.
                    return;
                }
                try {
                    mAccountCreated = addAccount(mAccount);
                    if (mAccountCreated == false) {
                        Log.d(TAG,"Account creation failed, normal durring startup");
                        Log.e(TAG,"Account creation failed.");
                        return;
                    }
                    BluetoothPbapRequestPullPhoneBook request =
+3 −3
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ public class PbapClientService extends ProfileService {
        } catch (Exception e) {
            Log.w(TAG,"Unable to register pbapclient receiver", e);
        }
        mPbapClientStateMachine = new PbapClientStateMachine(this);
        mPbapClientStateMachine = new PbapClientStateMachine(this, this);
        setPbapClientService(this);
        mPbapClientStateMachine.start();
        return true;
+15 −5
Original line number Diff line number Diff line
@@ -48,9 +48,10 @@ import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothPbapClient;
import android.content.Context;
import android.content.Intent;
import android.os.HandlerThread;
import android.os.Message;
import android.os.Process;
import android.os.HandlerThread;
import android.os.UserManager;
import android.provider.CallLog;
import android.util.Log;

@@ -93,17 +94,22 @@ final class PbapClientStateMachine extends StateMachine {

    // mCurrentDevice may only be changed in Disconnected State.
    private BluetoothDevice mCurrentDevice = null;
    private PbapClientService mService;
    private Context mContext;
    private PbapClientConnectionHandler mConnectionHandler;
    private HandlerThread mHandlerThread = null;
    private UserManager mUserManager = null;

    // mMostRecentState maintains previous state for broadcasting transitions.
    private int mMostRecentState = BluetoothProfile.STATE_DISCONNECTED;

    PbapClientStateMachine(Context context) {
    PbapClientStateMachine(PbapClientService svc, Context context) {
        super(TAG);

        mService = svc;
        mContext = context;
        mLock = new Object();
        mUserManager = UserManager.get(mContext);
        mDisconnected = new Disconnected();
        mConnecting = new Connecting();
        mDisconnecting = new Disconnecting();
@@ -182,7 +188,7 @@ final class PbapClientStateMachine extends StateMachine {
                    Process.THREAD_PRIORITY_BACKGROUND);
            mHandlerThread.start();
            mConnectionHandler = new PbapClientConnectionHandler(mHandlerThread.getLooper(),
                    PbapClientStateMachine.this, mCurrentDevice);
                mContext, PbapClientStateMachine.this, mCurrentDevice);
            mConnectionHandler.obtainMessage(PbapClientConnectionHandler.MSG_CONNECT)
                    .sendToTarget();
            sendMessageDelayed(MSG_CONNECT_TIMEOUT, CONNECT_TIMEOUT);
@@ -274,9 +280,11 @@ final class PbapClientStateMachine extends StateMachine {
            onConnectionStateChanged(mCurrentDevice, mMostRecentState,
                    BluetoothProfile.STATE_CONNECTED);
            mMostRecentState = BluetoothProfile.STATE_CONNECTED;
            if (mUserManager.isUserUnlocked()) {
                mConnectionHandler.obtainMessage(PbapClientConnectionHandler.MSG_DOWNLOAD)
                        .sendToTarget();
            }
        }

        @Override
        public boolean processMessage(Message message) {
@@ -319,6 +327,8 @@ final class PbapClientStateMachine extends StateMachine {
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
        mContext.sendBroadcast(intent, ProfileService.BLUETOOTH_PERM);
        mService.notifyProfileConnectionStateChanged(device, BluetoothProfile.PBAP_CLIENT, state,
                prevState);
    }

    public void connect(BluetoothDevice device) {