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

Commit f4d535b2 authored by Joseph Pirozzo's avatar Joseph Pirozzo Committed by Sanket Agarwal
Browse files

PbapClientStateMachine legacyCode removal.

Eliminate dependencies and remove unused code after the state machine
redesign.

bug: 28249138
Change-Id: I95b362136609a8de033a057578c1b08bd46cced3
(cherry picked from commit 992274c189ae23ce454fa2136c1b164105006004)
parent 0033d1fd
Loading
Loading
Loading
Loading
+0 −143
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.pbapclient;

import com.android.vcard.VCardEntry;
import com.android.vcard.VCardEntry.EmailData;
import com.android.vcard.VCardEntry.NameData;
import com.android.vcard.VCardEntry.PhoneData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Entry representation of folder listing
 */
public class BluetoothPbapCard {

    public final String handle;

    //Name to be parsed (N)
    public final String N;
    public final String lastName;
    public final String firstName;
    public final String middleName;
    public final String prefix;
    public final String suffix;

    public BluetoothPbapCard(String handle, String name) {
        this.handle = handle;

        N = name;

        /*
         * format is as for vCard N field, so we have up to 5 tokens: LastName;
         * FirstName; MiddleName; Prefix; Suffix
         */
        String[] parsedName = name.split(";", 5);

        lastName = parsedName.length < 1 ? null : parsedName[0];
        firstName = parsedName.length < 2 ? null : parsedName[1];
        middleName = parsedName.length < 3 ? null : parsedName[2];
        prefix = parsedName.length < 4 ? null : parsedName[3];
        suffix = parsedName.length < 5 ? null : parsedName[4];
    }

    @Override
    public String toString() {
        JSONObject json = new JSONObject();

        try {
            json.put("handle", handle);
            json.put("N", N);
            json.put("lastName", lastName);
            json.put("firstName", firstName);
            json.put("middleName", middleName);
            json.put("prefix", prefix);
            json.put("suffix", suffix);
        } catch (JSONException e) {
            // do nothing
        }

        return json.toString();
    }

    static public String jsonifyVcardEntry(VCardEntry vcard) {
        JSONObject json = new JSONObject();

        try {
            NameData name = vcard.getNameData();
            json.put("formatted", name.getFormatted());
            json.put("family", name.getFamily());
            json.put("given", name.getGiven());
            json.put("middle", name.getMiddle());
            json.put("prefix", name.getPrefix());
            json.put("suffix", name.getSuffix());
        } catch (JSONException e) {
            // do nothing
        }

        try {
            JSONArray jsonPhones = new JSONArray();

            List<PhoneData> phones = vcard.getPhoneList();

            if (phones != null) {
                for (PhoneData phone : phones) {
                    JSONObject jsonPhone = new JSONObject();
                    jsonPhone.put("type", phone.getType());
                    jsonPhone.put("number", phone.getNumber());
                    jsonPhone.put("label", phone.getLabel());
                    jsonPhone.put("is_primary", phone.isPrimary());

                    jsonPhones.put(jsonPhone);
                }

                json.put("phones", jsonPhones);
            }
        } catch (JSONException e) {
            // do nothing
        }

        try {
            JSONArray jsonEmails = new JSONArray();

            List<EmailData> emails = vcard.getEmailList();

            if (emails != null) {
                for (EmailData email : emails) {
                    JSONObject jsonEmail = new JSONObject();
                    jsonEmail.put("type", email.getType());
                    jsonEmail.put("address", email.getAddress());
                    jsonEmail.put("label", email.getLabel());
                    jsonEmail.put("is_primary", email.isPrimary());

                    jsonEmails.put(jsonEmail);
                }

                json.put("emails", jsonEmails);
            }
        } catch (JSONException e) {
            // do nothing
        }

        return json.toString();
    }
}
+0 −857

File deleted.

Preview size limit exceeded, changes collapsed.

+0 −2
Original line number Diff line number Diff line
@@ -53,8 +53,6 @@ class BluetoothPbapObexAuthenticator implements Authenticator {
        mReplied = false;

        Log.d(TAG, "onAuthenticationChallenge: sending request");
        mCallback.obtainMessage(BluetoothPbapObexSession.OBEX_SESSION_AUTHENTICATION_REQUEST)
                .sendToTarget();

        synchronized (this) {
            while (!mReplied) {
+0 −253
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.pbapclient;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import java.io.IOException;
import java.lang.ref.WeakReference;

import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.ObexTransport;
import javax.obex.ResponseCodes;

final class BluetoothPbapObexSession {
    private static final boolean DBG = true;
    private static final String TAG = "BluetoothPbapObexSession";

    private static final byte[] PBAP_TARGET = new byte[] {
            0x79, 0x61, 0x35, (byte) 0xf0, (byte) 0xf0, (byte) 0xc5, 0x11, (byte) 0xd8, 0x09, 0x66,
            0x08, 0x00, 0x20, 0x0c, (byte) 0x9a, 0x66
    };

    final static int OBEX_SESSION_CONNECTED = 100;
    final static int OBEX_SESSION_FAILED = 101;
    final static int OBEX_SESSION_DISCONNECTED = 102;
    final static int OBEX_SESSION_REQUEST_COMPLETED = 103;
    final static int OBEX_SESSION_REQUEST_FAILED = 104;
    final static int OBEX_SESSION_AUTHENTICATION_REQUEST = 105;
    final static int OBEX_SESSION_AUTHENTICATION_TIMEOUT = 106;

    final static int MSG_CONNECT = 0;
    final static int MSG_REQUEST = 1;

    final static int CONNECTED = 0;
    final static int CONNECTING = 1;
    final static int DISCONNECTED = 2;

    private Handler mSessionHandler;
    private final ObexTransport mTransport;
    // private ObexClientThread mObexClientThread;
    private BluetoothPbapObexAuthenticator mAuth = null;
    private HandlerThread mThread;
    private Handler mHandler;
    private ClientSession mClientSession;

    private int mState = DISCONNECTED;

    public BluetoothPbapObexSession(ObexTransport transport) {
        mTransport = transport;
    }

    public synchronized boolean start(Handler handler) {
        Log.d(TAG, "start");

        if (mState == CONNECTED || mState == CONNECTING) {
            return false;
        }
        mState = CONNECTING;
        mSessionHandler = handler;

        mAuth = new BluetoothPbapObexAuthenticator(mSessionHandler);

        // Start the thread to process requests (see {@link schedule()}.
        mThread = new HandlerThread("BluetoothPbapObexSessionThread");
        mThread.start();
        mHandler = new ObexClientHandler(mThread.getLooper(), this);

        // Make connect call non blocking.
        boolean status = mHandler.sendMessage(mHandler.obtainMessage(MSG_CONNECT));
        if (!status) {
            mState = DISCONNECTED;
            return false;
        } else {
            return true;
        }
    }

    public void stop() {
        if (DBG) {
            Log.d(TAG, "stop");
        }

        // This will essentially stop the handler and ignore any inflight requests.
        mThread.quit();

        // We clean up the state here.
        disconnect(false /* no callback */);
    }

    public void abort() {
        stop();
    }

    public boolean schedule(BluetoothPbapRequest request) {
        if (DBG) {
            Log.d(TAG, "schedule() called with: " + request);
        }

        boolean status = mHandler.sendMessage(mHandler.obtainMessage(MSG_REQUEST, request));
        if (!status) {
            Log.e(TAG, "Adding messages failed, obex must be disconnected.");
            return false;
        }
        return true;
    }

    public int isConnected() {
        return mState;
    }

    private void connect() {
       if (DBG) {
          Log.d(TAG, "connect()");
       }

       boolean success = true;
       try {
          mClientSession = new ClientSession(mTransport);
          mClientSession.setAuthenticator(mAuth);
       } catch (IOException e) {
          Log.d(TAG, "connect() exception: " + e);
          success = false;
       }

       HeaderSet hs = new HeaderSet();
       hs.setHeader(HeaderSet.TARGET, PBAP_TARGET);
       try {
          hs = mClientSession.connect(hs);

          if (hs.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
              disconnect(true  /* callback */);
              success = false;
          }
       } catch (IOException e) {
          success = false;
       }

       synchronized (this) {
           if (success) {
              mSessionHandler.obtainMessage(OBEX_SESSION_CONNECTED).sendToTarget();
              mState = CONNECTED;
           } else {
              mSessionHandler.obtainMessage(OBEX_SESSION_DISCONNECTED).sendToTarget();
              mState = DISCONNECTED;
           }
       }
    }

    private synchronized void disconnect(boolean callback) {
        if (DBG) {
            Log.d(TAG, "disconnect()");
        }

        if (mState != DISCONNECTED) {
            return;
        }

        if (mClientSession != null) {
            try {
                mClientSession.disconnect(null);
                mClientSession.close();
            } catch (IOException e) {
            }
        }

        if (callback) {
            mSessionHandler.obtainMessage(OBEX_SESSION_DISCONNECTED).sendToTarget();
        }

        mState = DISCONNECTED;
    }

    private void executeRequest(BluetoothPbapRequest req) {
        try {
            req.execute(mClientSession);
        } catch (IOException e) {
            Log.e(TAG, "Error during executeRequest " + e);
            disconnect(true);
        }

        if (req.isSuccess()) {
            mSessionHandler.obtainMessage(OBEX_SESSION_REQUEST_COMPLETED, req).sendToTarget();
        } else {
            mSessionHandler.obtainMessage(OBEX_SESSION_REQUEST_FAILED, req).sendToTarget();
        }
    }

    public boolean setAuthReply(String key) {
        Log.d(TAG, "setAuthReply key=" + key);

        if (mAuth == null) {
            return false;
        }

        mAuth.setReply(key);

        return true;
    }

    private static class ObexClientHandler extends Handler {
        WeakReference<BluetoothPbapObexSession> mInst;

        ObexClientHandler(Looper looper, BluetoothPbapObexSession inst) {
            super(looper);
            mInst = new WeakReference<BluetoothPbapObexSession>(inst);
        }

        @Override
        public void handleMessage(Message msg) {
            BluetoothPbapObexSession inst = mInst.get();
            if (inst == null) {
                Log.e(TAG, "The instance class is no longer around; terminating.");
                return;
            }

            if (inst.isConnected() != CONNECTED && msg.what != MSG_CONNECT) {
                Log.w(TAG, "Cannot execute " + msg + " when not CONNECTED.");
                return;
            }

            switch (msg.what) {
                case MSG_CONNECT:
                    inst.connect();
                    break;
                case MSG_REQUEST:
                    inst.executeRequest((BluetoothPbapRequest) msg.obj);
                    break;
                default:
                    Log.e(TAG, "Unknwown message type: " + msg.what);
            }
        }
    }
}
+3 −3
Original line number Diff line number Diff line
@@ -63,9 +63,9 @@ final class BluetoothPbapRequestPullPhoneBook extends BluetoothPbapRequest {
        ObexAppParameters oap = new ObexAppParameters();

        /* make sure format is one of allowed values */
        if (format != BluetoothPbapClient.VCARD_TYPE_21
                && format != BluetoothPbapClient.VCARD_TYPE_30) {
            format = BluetoothPbapClient.VCARD_TYPE_21;
        if (format != PbapClientConnectionHandler.VCARD_TYPE_21
                && format != PbapClientConnectionHandler.VCARD_TYPE_30) {
            format = PbapClientConnectionHandler.VCARD_TYPE_21;
        }

        if (filter != 0) {
Loading