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

Commit cc500fa5 authored by Sal Savage's avatar Sal Savage
Browse files

Update per-instance tag patterns to use the message and not tag

Tag: #refactor
Flag: EXEMPT, logging only change
Bug: 315046089
Test: atest HfpclientDeviceBlockTest.java
Test: atest BluetoothMapMasInstanceTest.java
Change-Id: I4ee9fdb4fb4656d78d90cff33543ce7ecaae76d2
parent de2fca6a
Loading
Loading
Loading
Loading
+25 −43
Original line number Diff line number Diff line
@@ -47,9 +47,8 @@ import java.util.concurrent.atomic.AtomicInteger;
 * In both cases the {@link ObexServerSockets} object have terminated, and a new must be created.
 */
public class ObexServerSockets {
    private final String mTag;
    private static final String STAG = "ObexServerSockets";
    private static final boolean D = true; // TODO: set to false!
    private static final String TAG = "ObexServerSockets";
    private static final boolean D = true; // TODO: set this to false!

    private final IObexConnectionHandler mConHandler;
    /* The wrapped sockets */
@@ -66,7 +65,6 @@ public class ObexServerSockets {
        mConHandler = conHandler;
        mRfcommSocket = rfcommSocket;
        mL2capSocket = l2capSocket;
        mTag = "ObexServerSockets" + sInstanceCounter.getAndIncrement();
    }

    /**
@@ -113,7 +111,7 @@ public class ObexServerSockets {
    private static ObexServerSockets create(IObexConnectionHandler validator, int rfcommChannel,
            int l2capPsm, boolean isSecure) {
        if (D) {
            Log.d(STAG, "create(rfcomm = " + rfcommChannel + ", l2capPsm = " + l2capPsm + ")");
            Log.d(TAG, "create(rfcomm = " + rfcommChannel + ", l2capPsm = " + l2capPsm + ")");
        }
        BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
        if (bt == null) {
@@ -142,10 +140,10 @@ public class ObexServerSockets {
                    }
                }
            } catch (IOException e) {
                Log.e(STAG, "Error create ServerSockets ", e);
                Log.e(TAG, "Error create ServerSockets ", e);
                initSocketOK = false;
            } catch (SecurityException e) {
                Log.e(STAG, "Error create ServerSockets ", e);
                Log.e(TAG, "Error create ServerSockets ", e);
                initSocketOK = false;
                break;
            }
@@ -154,16 +152,16 @@ public class ObexServerSockets {
                int state = bt.getState();
                if ((state != BluetoothAdapter.STATE_TURNING_ON) && (state
                        != BluetoothAdapter.STATE_ON)) {
                    Log.w(STAG, "initServerSockets failed as BT is (being) turned off");
                    Log.w(TAG, "initServerSockets failed as BT is (being) turned off");
                    break;
                }
                try {
                    if (D) {
                        Log.v(STAG, "waiting 300 ms...");
                        Log.v(TAG, "waiting 300 ms...");
                    }
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    Log.e(STAG, "create() was interrupted");
                    Log.e(TAG, "create() was interrupted");
                }
            } else {
                break;
@@ -172,13 +170,13 @@ public class ObexServerSockets {

        if (initSocketOK) {
            if (D) {
                Log.d(STAG, "Succeed to create listening sockets ");
                Log.d(TAG, "Succeed to create listening sockets ");
            }
            ObexServerSockets sockets = new ObexServerSockets(validator, rfcommSocket, l2capSocket);
            sockets.startAccept();
            return sockets;
        } else {
            Log.e(STAG, "Error to create listening socket after " + CREATE_RETRY_TIME + " try");
            Log.e(TAG, "Error to create listening socket after " + CREATE_RETRY_TIME + " try");
            return null;
        }
    }
@@ -207,9 +205,7 @@ public class ObexServerSockets {
     * the {@link IObexConnectionValidator#onConnect()}, at which point both threads will exit.
     */
    private void startAccept() {
        if (D) {
            Log.d(mTag, "startAccept()");
        }
        Log.d(TAG, "startAccept()");

        mRfcommThread = new SocketAcceptThread(mRfcommSocket);
        mRfcommThread.start();
@@ -225,9 +221,7 @@ public class ObexServerSockets {
     * @return true if the connection is accepted, false otherwise.
     */
    private synchronized boolean onConnect(BluetoothDevice device, BluetoothSocket conSocket) {
        if (D) {
            Log.d(mTag, "onConnect() socket: " + conSocket);
        }
        Log.d(TAG, "onConnect() socket: " + conSocket);
        return mConHandler.onConnect(device, conSocket);
    }

@@ -238,7 +232,7 @@ public class ObexServerSockets {
        shutdown(false);
        BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
        if ((mAdapter != null) && (mAdapter.getState() == BluetoothAdapter.STATE_ON)) {
            Log.d(mTag, "onAcceptFailed() calling shutdown...");
            Log.d(TAG, "onAcceptFailed() calling shutdown...");
            mConHandler.onAcceptFailed();
        }
    }
@@ -249,9 +243,7 @@ public class ObexServerSockets {
     * has ended execution
     */
    public synchronized void shutdown(boolean block) {
        if (D) {
            Log.d(mTag, "shutdown(block = " + block + ")");
        }
        Log.d(TAG, "shutdown(block = " + block + ")");
        if (mRfcommThread != null) {
            mRfcommThread.shutdown();
        }
@@ -270,7 +262,7 @@ public class ObexServerSockets {
                        mL2capThread = null;
                    }
                } catch (InterruptedException e) {
                    Log.i(mTag, "shutdown() interrupted, continue waiting...", e);
                    Log.i(TAG, "shutdown() interrupted, continue waiting...", e);
                }
            }
        } else {
@@ -314,28 +306,24 @@ public class ObexServerSockets {
                    BluetoothDevice device;

                    try {
                        if (D) {
                            Log.d(mTag, "Accepting socket connection...");
                        }
                        Log.d(TAG, "Accepting socket connection...");

                        connSocket = mServerSocket.accept();
                        if (D) {
                            Log.d(mTag, "Accepted socket connection from: " + mServerSocket);
                        }
                        Log.d(TAG, "Accepted socket connection from: " + mServerSocket);

                        if (connSocket == null) {
                            // TODO: Do we need a max error count, to avoid spinning?
                            Log.w(mTag, "connSocket is null - reattempt accept");
                            Log.w(TAG, "connSocket is null - reattempt accept");
                            continue;
                        }
                        device = connSocket.getRemoteDevice();

                        if (device == null) {
                            Log.i(mTag, "getRemoteDevice() = null - reattempt accept");
                            Log.i(TAG, "getRemoteDevice() = null - reattempt accept");
                            try {
                                connSocket.close();
                            } catch (IOException e) {
                                Log.w(mTag, "Error closing the socket. ignoring...", e);
                                Log.w(TAG, "Error closing the socket. ignoring...", e);
                            }
                            continue;
                        }
@@ -348,7 +336,7 @@ public class ObexServerSockets {
                            /* Close connection if we already have a connection with another device
                             * by responding to the OBEX connect request.
                             */
                            Log.i(mTag, "RemoteDevice is invalid - creating ObexRejectServer.");
                            Log.i(TAG, "RemoteDevice is invalid - creating ObexRejectServer.");
                            BluetoothObexTransport obexTrans =
                                    new BluetoothObexTransport(connSocket);
                            // Create and detach a selfdestructing ServerSession to respond to any
@@ -364,16 +352,14 @@ public class ObexServerSockets {
                        if (mStopped) {
                            // Expected exception because of shutdown.
                        } else {
                            Log.w(mTag, "Accept exception for " + mServerSocket, ex);
                            Log.w(TAG, "Accept exception for " + mServerSocket, ex);
                            ObexServerSockets.this.onAcceptFailed();
                        }
                        mStopped = true;
                    }
                } // End while()
            } finally {
                if (D) {
                    Log.d(mTag, "AcceptThread ended for: " + mServerSocket);
                }
                Log.d(TAG, "AcceptThread ended for: " + mServerSocket);
            }
        }

@@ -391,18 +377,14 @@ public class ObexServerSockets {
                try {
                    mServerSocket.close();
                } catch (IOException e) {
                    if (D) {
                        Log.d(mTag, "Exception while thread shutdown:", e);
                    }
                    Log.w(TAG, "Exception while thread shutdown:", e);
                }
            }
            // If called from another thread, interrupt the thread
            if (!Thread.currentThread().equals(this)) {
                // TODO: Will this interrupt the thread if it is blocked in synchronized?
                // Else: change to use InterruptableLock
                if (D) {
                    Log.d(mTag, "shutdown called from another thread - interrupt().");
                }
                Log.d(TAG, "shutdown called from another thread - interrupt().");
                interrupt();
            }
        }
+39 −51
Original line number Diff line number Diff line
@@ -38,10 +38,11 @@ import java.util.UUID;
// Lifecycle of a Device Block is managed entirely by the Service which creates it. In essence it
// has only the active state otherwise the block should be GCed.
public class HfpClientDeviceBlock {
    private static final String KEY_SCO_STATE = "com.android.bluetooth.hfpclient.SCO_STATE";
    private static final String TAG = "HfpClientDeviceBlock";
    private static final boolean DBG = false;

    private final String mTAG;
    private static final String KEY_SCO_STATE = "com.android.bluetooth.hfpclient.SCO_STATE";

    private final BluetoothDevice mDevice;
    private final PhoneAccount mPhoneAccount;
    private final Map<UUID, HfpClientConnection> mConnections = new HashMap<>();
@@ -56,7 +57,6 @@ public class HfpClientDeviceBlock {
        mDevice = device;
        mConnServ = connServ;
        mServiceInterface = serviceInterface;
        mTAG = "HfpClientDeviceBlock." + mDevice;
        mPhoneAccount = mConnServ.createAccount(device);
        mTelecomManager = mConnServ.getSystemService(TelecomManager.class);

@@ -66,20 +66,16 @@ public class HfpClientDeviceBlock {
        mTelecomManager.setUserSelectedOutgoingPhoneAccount(mPhoneAccount.getAccountHandle());

        mScoState = getScoStateFromDevice(device);
        if (DBG) {
            Log.d(mTAG, "SCO state = " + mScoState);
        }
        debug("SCO state = " + mScoState);


        List<HfpClientCall> calls = mServiceInterface.getCurrentCalls(mDevice);
        if (DBG) {
            Log.d(mTAG, "Got calls " + calls);
        }
        debug("Got calls " + calls);
        if (calls == null) {
            // We can get null as a return if we are not connected. Hence there may
            // be a race in getting the broadcast and HFP Client getting
            // disconnected before broadcast gets delivered.
            Log.w(mTAG, "Got connected but calls were null, ignoring the broadcast");
            warn("Got connected but calls were null, ignoring the broadcast");
            return;
        }

@@ -106,7 +102,7 @@ public class HfpClientDeviceBlock {
            connection.onAdded();
            return connection;
        } else {
            Log.e(mTAG, "Call " + callUuid + " ignored: connection does not exist");
            error("Call " + callUuid + " ignored: connection does not exist");
            return null;
        }
    }
@@ -120,9 +116,7 @@ public class HfpClientDeviceBlock {
    }

    synchronized void onAudioStateChange(int newState, int oldState) {
        if (DBG) {
            Log.d(mTAG, "Call audio state changed " + oldState + " -> " + newState);
        }
        debug("Call audio state changed " + oldState + " -> " + newState);
        mScoState.putInt(KEY_SCO_STATE, newState);

        for (HfpClientConnection connection : mConnections.values()) {
@@ -140,7 +134,7 @@ public class HfpClientDeviceBlock {
            connection.onAdded();
            return connection;
        } else {
            Log.e(mTAG, "Call " + callUuid + " ignored: connection does not exist");
            error("Call " + callUuid + " ignored: connection does not exist");
            return null;
        }
    }
@@ -164,16 +158,14 @@ public class HfpClientDeviceBlock {
    // Remove existing calls and the phone account associated, the object will get garbage
    // collected soon
    synchronized void cleanup() {
        Log.d(mTAG, "Resetting state for device " + mDevice);
        debug("Resetting state for device " + mDevice);
        disconnectAll();
        mTelecomManager.unregisterPhoneAccount(mPhoneAccount.getAccountHandle());
    }

    // Handle call change
    synchronized void handleCall(HfpClientCall call) {
        if (DBG) {
            Log.d(mTAG, "Got call " + call.toString());
        }
        debug("Got call " + call.toString());

        HfpClientConnection connection = findConnectionKey(call);

@@ -217,9 +209,7 @@ public class HfpClientDeviceBlock {
                mTelecomManager.addNewIncomingCall(mPhoneAccount.getAccountHandle(), b);
            }
        } else if (call.getState() == HfpClientCall.CALL_STATE_TERMINATED) {
            if (DBG) {
                Log.d(mTAG, "Removing call " + call);
            }
            debug("Removing call " + call);
            mConnections.remove(call.getUUID());
        }

@@ -228,9 +218,7 @@ public class HfpClientDeviceBlock {

    // Find the connection specified by the key, also update the key with ID if present.
    private synchronized HfpClientConnection findConnectionKey(HfpClientCall call) {
        if (DBG) {
            Log.d(mTAG, "findConnectionKey local key set " + mConnections.toString());
        }
        debug("findConnectionKey local key set " + mConnections.toString());
        return mConnections.get(call.getUUID());
    }

@@ -250,9 +238,7 @@ public class HfpClientDeviceBlock {

    private boolean isDisconnectingToActive(HfpClientConnection prevConn,
            HfpClientCall newCall) {
        if (DBG) {
            Log.d(mTAG, "prevConn " + prevConn.isClosing() + " new call " + newCall.getState());
        }
        debug("prevConn " + prevConn.isClosing() + " new call " + newCall.getState());
        if (prevConn.isClosing() && prevConn.getCall().getState() != newCall.getState()
                && newCall.getState() != HfpClientCall.CALL_STATE_TERMINATED) {
            return true;
@@ -263,21 +249,18 @@ public class HfpClientDeviceBlock {
    private synchronized HfpClientConnection buildConnection(HfpClientCall call,
            Uri number) {
        if (call == null && number == null) {
            Log.e(mTAG, "Both call and number cannot be null.");
            error("Both call and number cannot be null.");
            return null;
        }

        if (DBG) {
            Log.d(mTAG, "Creating connection on " + mDevice + " for " + call + "/" + number);
        }
        debug("Creating connection on " + mDevice + " for " + call + "/" + number);

        HfpClientConnection connection = (call != null
                ? new HfpClientConnection(mDevice, call, mConnServ, mServiceInterface)
                : new HfpClientConnection(mDevice, number, mConnServ, mServiceInterface));
        connection.setExtras(mScoState);
        if (DBG) {
            Log.d(mTAG, "Connection extras = " + connection.getExtras().toString());
        }

        debug("Connection extras = " + connection.getExtras().toString());

        if (connection.getState() != Connection.STATE_DISCONNECTED) {
            mConnections.put(connection.getUUID(), connection);
@@ -289,19 +272,14 @@ public class HfpClientDeviceBlock {
    // Updates any conferencable connections.
    private void updateConferenceableConnections() {
        boolean addConf = false;
        if (DBG) {
            Log.d(mTAG, "Existing connections: " + mConnections + " existing conference "
                    + mConference);
        }
        debug("Existing connections: " + mConnections + " existing conference " + mConference);

        // If we have an existing conference call then loop through all connections and update any
        // connections that may have switched from conference -> non-conference.
        if (mConference != null) {
            for (Connection confConn : mConference.getConnections()) {
                if (!((HfpClientConnection) confConn).inConference()) {
                    if (DBG) {
                        Log.d(mTAG, "Removing connection " + confConn + " from conference.");
                    }
                    debug("Removing connection " + confConn + " from conference.");
                    mConference.removeConnection(confConn);
                }
            }
@@ -319,9 +297,7 @@ public class HfpClientDeviceBlock {
                    mConference.setExtras(mScoState);
                }
                if (mConference.addConnection(otherConn)) {
                    if (DBG) {
                        Log.d(mTAG, "Adding connection " + otherConn + " to conference.");
                    }
                    debug("Adding connection " + otherConn + " to conference.");
                    addConf = true;
                }
            }
@@ -329,9 +305,7 @@ public class HfpClientDeviceBlock {

        // If we have no connections in the conference we should simply end it.
        if (mConference != null && mConference.getConnections().size() == 0) {
            if (DBG) {
                Log.d(mTAG, "Conference has no connection, destroying");
            }
            debug("Conference has no connection, destroying");
            mConference.setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
            mConference.destroy();
            mConference = null;
@@ -339,9 +313,7 @@ public class HfpClientDeviceBlock {

        // If we have a valid conference and not previously added then add it.
        if (mConference != null && addConf) {
            if (DBG) {
                Log.d(mTAG, "Adding conference to stack.");
            }
            debug("Adding conference to stack.");
            mConnServ.addConference(mConference);
        }
    }
@@ -407,4 +379,20 @@ public class HfpClientDeviceBlock {
        }

    }

    // Per-Device logging

    public void debug(String message) {
        if (DBG) {
            Log.d(TAG, "[device=" + mDevice + "] " + message);
        }
    }

    public void warn(String message) {
        Log.w(TAG, "[device=" + mDevice + "] " + message);
    }

    public void error(String message) {
        Log.e(TAG, "[device=" + mDevice + "] " + message);
    }
}
+45 −54

File changed.

Preview size limit exceeded, changes collapsed.

+0 −8
Original line number Diff line number Diff line
@@ -60,14 +60,6 @@ public class BluetoothMapMasInstanceTest {
                TEST_PROVIDER_AUTHORITY, TEST_DRAWABLE, TEST_TYPE, TEST_UCI, TEST_UCI_PREFIX);
    }

    @Test
    public void constructor_withNoParameters() {
        BluetoothMapMasInstance instance = new BluetoothMapMasInstance();

        assertThat(instance.mTag).isEqualTo(
                "BluetoothMapMasInstance" + (BluetoothMapMasInstance.sInstanceCounter - 1));
    }

    @Test
    public void toString_returnsInfo() {
        BluetoothMapMasInstance instance = new BluetoothMapMasInstance(mMapService, mContext,