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

Commit 56dabca2 authored by Sal Savage's avatar Sal Savage Committed by Gerrit Code Review
Browse files

Merge "Refactor HFP HF logging to be unguarded" into main

parents 204f9965 d0396272
Loading
Loading
Loading
Loading
+19 −42
Original line number Diff line number Diff line
@@ -62,8 +62,7 @@ import java.util.UUID;
 * Bluetooth application.
 */
public class HeadsetClientService extends ProfileService {
    private static final boolean DBG = true;
    private static final String TAG = "HeadsetClientService";
    private static final String TAG = HeadsetClientService.class.getSimpleName();

    // This is also used as a lock for shared data in {@link HeadsetClientService}
    @GuardedBy("mStateMachineMap")
@@ -101,9 +100,7 @@ public class HeadsetClientService extends ProfileService {
    @Override
    public void start() {
        synchronized (mStartStopLock) {
            if (DBG) {
            Log.d(TAG, "start()");
            }
            if (getHeadsetClientService() != null) {
                throw new IllegalStateException("start() called twice");
            }
@@ -202,20 +199,16 @@ public class HeadsetClientService extends ProfileService {
            // ({@link HeadsetClientStateMachine#SET_SPEAKER_VOLUME} in
            // {@link HeadsetClientStateMachine} for details.
            if (action.equals(AudioManager.ACTION_VOLUME_CHANGED)) {
                if (DBG) {
                Log.d(TAG, "Volume changed for stream: " + intent.getIntExtra(
                        AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1));
                }
                int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1);
                if (streamType == AudioManager.STREAM_VOICE_CALL) {
                    int streamValue =
                            intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, -1);
                    int hfVol = HeadsetClientStateMachine.amToHfVol(streamValue);
                    if (DBG) {
                    Log.d(TAG,
                            "Setting volume to audio manager: " + streamValue + " hands free: "
                                    + hfVol);
                    }
                    mAudioManager.setHfpVolume(hfVol);
                    synchronized (mStateMachineMap) {
                        for (HeadsetClientStateMachine sm : mStateMachineMap.values()) {
@@ -235,10 +228,8 @@ public class HeadsetClientService extends ProfileService {
                }
                mLastBatteryLevel = batteryLevel;

                if (DBG) {
                Log.d(TAG,
                        "Send battery level update BIEV(2," + batteryLevel + ") command");
                }

                synchronized (mStateMachineMap) {
                    for (HeadsetClientStateMachine sm : mStateMachineMap.values()) {
@@ -724,16 +715,12 @@ public class HeadsetClientService extends ProfileService {
    /** Set a {@link HeadsetClientService} instance. */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    public static synchronized void setHeadsetClientService(HeadsetClientService instance) {
        if (DBG) {
        Log.d(TAG, "setHeadsetClientService(): set to: " + instance);
        }
        sHeadsetClientService = instance;
    }

    public boolean connect(BluetoothDevice device) {
        if (DBG) {
        Log.d(TAG, "connect " + device);
        }
        if (getConnectionPolicy(device) == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
            Log.w(TAG, "Connection not allowed: <" + device.getAddress()
                    + "> is CONNECTION_POLICY_FORBIDDEN");
@@ -837,9 +824,7 @@ public class HeadsetClientService extends ProfileService {
     * @return true if connectionPolicy is set, false on error
     */
    public boolean setConnectionPolicy(BluetoothDevice device, int connectionPolicy) {
        if (DBG) {
        Log.d(TAG, "Saved connectionPolicy " + device + " = " + connectionPolicy);
        }

        if (!mDatabaseManager.setProfileConnectionPolicy(device, BluetoothProfile.HEADSET_CLIENT,
                  connectionPolicy)) {
@@ -1037,11 +1022,9 @@ public class HeadsetClientService extends ProfileService {
                    continue;
                }
                int connectionState = entry.getValue().getConnectionState(entry.getKey());
                if (DBG) {
                Log.d(TAG,
                        "Accepting a call on device " + device + ". Possibly disconnecting on "
                                + entry.getValue());
                }
                if (connectionState == BluetoothProfile.STATE_CONNECTED) {
                    entry.getValue()
                            .obtainMessage(HeadsetClientStateMachine.TERMINATE_CALL)
@@ -1306,9 +1289,7 @@ public class HeadsetClientService extends ProfileService {
        }

        if (sm != null) {
            if (DBG) {
            Log.d(TAG, "Found SM for device " + device);
            }
        } else if (isConnectionEvent) {
            // The only time a new state machine should be created when none was found is for
            // connection events.
@@ -1338,9 +1319,7 @@ public class HeadsetClientService extends ProfileService {
        synchronized (mStateMachineMap) {
            HeadsetClientStateMachine sm = mStateMachineMap.get(device);
            if (sm != null) {
                if (DBG) {
                Log.d(TAG, "allocateStateMachine: SM already exists for device " + device);
                }
                return sm;
            }

@@ -1369,10 +1348,8 @@ public class HeadsetClientService extends ProfileService {
                if (entry.getValue() != null) {
                    int audioState = entry.getValue().getAudioState(entry.getKey());
                    if (audioState == HeadsetClientHalConstants.AUDIO_STATE_CONNECTED) {
                        if (DBG) {
                        Log.d(TAG, "Device " + entry.getKey() + " audio state " + audioState
                                + " Connected");
                        }
                        return true;
                    }
                }
+150 −142

File changed.

Preview size limit exceeded, changes collapsed.

+24 −36
Original line number Diff line number Diff line
@@ -28,8 +28,7 @@ import java.util.Objects;
import java.util.UUID;

public class HfpClientConnection extends Connection {
    private static final String TAG = "HfpClientConnection";
    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
    private static final String TAG = HfpClientConnection.class.getSimpleName();

    private static final String EVENT_SCO_CONNECT = "com.android.bluetooth.hfpclient.SCO_CONNECT";
    private static final String EVENT_SCO_DISCONNECT =
@@ -69,7 +68,7 @@ public class HfpClientConnection extends Connection {
        mCurrentCall = mServiceInterface.dial(mDevice, number.getSchemeSpecificPart());
        if (mCurrentCall == null) {
            close(DisconnectCause.ERROR);
            Log.e(TAG, "Failed to create the call, dial failed.");
            error("Failed to create the call, dial failed.");
            return;
        }

@@ -117,7 +116,7 @@ public class HfpClientConnection extends Connection {

    public void updateCall(HfpClientCall call) {
        if (call == null) {
            Log.e(TAG, "Updating call to a null value.");
            error("Updating call to a null value.");
            return;
        }
        mCurrentCall = call;
@@ -127,9 +126,7 @@ public class HfpClientConnection extends Connection {
        HfpClientConference conference = (HfpClientConference) getConference();
        int state = mCurrentCall.getState();

        if (DBG) {
            Log.d(TAG, "Got call state change to " + state);
        }
        debug("Got call state change to " + state);
        switch (state) {
            case HfpClientCall.CALL_STATE_ACTIVE:
                setActive();
@@ -163,19 +160,17 @@ public class HfpClientConnection extends Connection {
                }
                break;
            default:
                Log.wtf(TAG, "Unexpected phone state " + state);
                Log.wtf(TAG, "[" + mDevice + "]Unexpected phone state " + state);
        }
        mPreviousCallState = state;
    }

    public synchronized void close(int cause) {
        if (DBG) {
            Log.d(TAG, "Closing call " + mCurrentCall + "state: " + mClosed);
        }
        debug("Closing call " + mCurrentCall + "state: " + mClosed);
        if (mClosed) {
            return;
        }
        Log.d(TAG, "Setting " + mCurrentCall + " to disconnected " + getTelecomCallId());
        debug("Setting " + mCurrentCall + " to disconnected " + getTelecomCallId());
        setDisconnected(new DisconnectCause(cause));

        mClosed = true;
@@ -194,9 +189,7 @@ public class HfpClientConnection extends Connection {

    @Override
    public synchronized void onPlayDtmfTone(char c) {
        if (DBG) {
            Log.d(TAG, "onPlayDtmfTone " + c + " " + mCurrentCall);
        }
        debug("onPlayDtmfTone " + c + " " + mCurrentCall);
        if (!mClosed) {
            mServiceInterface.sendDTMF(mDevice, (byte) c);
        }
@@ -204,9 +197,7 @@ public class HfpClientConnection extends Connection {

    @Override
    public synchronized void onDisconnect() {
        if (DBG) {
            Log.d(TAG, "onDisconnect call: " + mCurrentCall + " state: " + mClosed);
        }
        debug("onDisconnect call: " + mCurrentCall + " state: " + mClosed);
        // The call is not closed so we should send a terminate here.
        if (!mClosed) {
            mServiceInterface.terminateCall(mDevice, mCurrentCall);
@@ -217,17 +208,13 @@ public class HfpClientConnection extends Connection {

    @Override
    public void onAbort() {
        if (DBG) {
            Log.d(TAG, "onAbort " + mCurrentCall);
        }
        debug("onAbort " + mCurrentCall);
        onDisconnect();
    }

    @Override
    public synchronized void onHold() {
        if (DBG) {
            Log.d(TAG, "onHold " + mCurrentCall);
        }
        debug("onHold " + mCurrentCall);
        if (!mClosed) {
            mServiceInterface.holdCall(mDevice);
        }
@@ -239,9 +226,7 @@ public class HfpClientConnection extends Connection {
            Log.w(TAG, "Ignoring unhold; call hold on the foreground call");
            return;
        }
        if (DBG) {
            Log.d(TAG, "onUnhold " + mCurrentCall);
        }
        debug("onUnhold " + mCurrentCall);
        if (!mClosed) {
            mServiceInterface.acceptCall(mDevice, HeadsetClientServiceInterface.CALL_ACCEPT_HOLD);
        }
@@ -249,9 +234,7 @@ public class HfpClientConnection extends Connection {

    @Override
    public synchronized void onAnswer() {
        if (DBG) {
            Log.d(TAG, "onAnswer " + mCurrentCall);
        }
        debug("onAnswer " + mCurrentCall);
        if (!mClosed) {
            mServiceInterface.acceptCall(mDevice, HeadsetClientServiceInterface.CALL_ACCEPT_NONE);
        }
@@ -259,9 +242,7 @@ public class HfpClientConnection extends Connection {

    @Override
    public synchronized void onReject() {
        if (DBG) {
            Log.d(TAG, "onReject " + mCurrentCall);
        }
        debug("onReject " + mCurrentCall);
        if (!mClosed) {
            mServiceInterface.rejectCall(mDevice);
        }
@@ -269,9 +250,7 @@ public class HfpClientConnection extends Connection {

    @Override
    public void onCallEvent(String event, Bundle extras) {
        if (DBG) {
            Log.d(TAG, "onCallEvent(" + event + ", " + extras + ")");
        }
        debug("onCallEvent(" + event + ", " + extras + ")");
        if (mClosed) {
            return;
        }
@@ -299,4 +278,13 @@ public class HfpClientConnection extends Connection {
        return "HfpClientConnection{" + getAddress() + "," + stateToString(getState()) + ","
                + mCurrentCall + "}";
    }

    private void debug(String message) {
        Log.d(TAG, "[" + mDevice + "]: " + message);
    }

    private void error(String message) {

        Log.e(TAG, "[" + mDevice + "]: " + message);
    }
}
+12 −33
Original line number Diff line number Diff line
@@ -42,8 +42,7 @@ import java.util.Map;
import java.util.Objects;

public class HfpClientConnectionService extends ConnectionService {
    private static final String TAG = "HfpClientConnService";
    private static final boolean DBG = true;
    private static final String TAG = HfpClientConnectionService.class.getSimpleName();

    public static final String HFP_SCHEME = "hfpc";

@@ -128,18 +127,14 @@ public class HfpClientConnectionService extends ConnectionService {
    private void onConnectionStateChangedInternal(BluetoothDevice device, int newState,
            int oldState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            if (DBG) {
            Log.d(TAG, "Established connection with " + device);
            }

            HfpClientDeviceBlock block = createBlockForDevice(device);
            if (block == null) {
                Log.w(TAG, "Block already exists for device= " + device + ", ignoring.");
            }
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            if (DBG) {
            Log.d(TAG, "Disconnecting from " + device);
            }

            // Disconnect any inflight calls from the connection service.
            synchronized (HfpClientConnectionService.this) {
@@ -197,9 +192,7 @@ public class HfpClientConnectionService extends ConnectionService {
    @Override
    public void onCreate() {
        super.onCreate();
        if (DBG) {
        Log.d(TAG, "onCreate");
        }
        mTelecomManager = getSystemService(TelecomManager.class);
        if (mTelecomManager != null) mTelecomManager.clearPhoneAccounts();

@@ -215,9 +208,7 @@ public class HfpClientConnectionService extends ConnectionService {

    @Override
    public void onDestroy() {
        if (DBG) {
        Log.d(TAG, "onDestroy called");
        }

        // Unregister the phone account. This should ideally happen when disconnection ensues but in
        // case the service crashes we may need to force clean.
@@ -236,9 +227,7 @@ public class HfpClientConnectionService extends ConnectionService {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (DBG) {
        Log.d(TAG, "onStartCommand " + intent);
        }
        // In order to make sure that the service is sticky (recovers from errors when HFP
        // connection is still active) and to stop it we need a special intent since stopService
        // only recreates it.
@@ -259,10 +248,8 @@ public class HfpClientConnectionService extends ConnectionService {
    @Override
    public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerAccount,
            ConnectionRequest request) {
        if (DBG) {
        Log.d(TAG,
                "onCreateIncomingConnection " + connectionManagerAccount + " req: " + request);
        }

        HfpClientDeviceBlock block = findBlockForHandle(connectionManagerAccount);
        if (block == null) {
@@ -282,9 +269,7 @@ public class HfpClientConnectionService extends ConnectionService {
    @Override
    public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerAccount,
            ConnectionRequest request) {
        if (DBG) {
        Log.d(TAG, "onCreateOutgoingConnection " + connectionManagerAccount);
        }
        HfpClientDeviceBlock block = findBlockForHandle(connectionManagerAccount);
        if (block == null) {
            Log.w(TAG, "HfpClient does not support having a connection manager");
@@ -300,9 +285,7 @@ public class HfpClientConnectionService extends ConnectionService {
    @Override
    public Connection onCreateUnknownConnection(PhoneAccountHandle connectionManagerAccount,
            ConnectionRequest request) {
        if (DBG) {
        Log.d(TAG, "onCreateUnknownConnection " + connectionManagerAccount);
        }
        HfpClientDeviceBlock block = findBlockForHandle(connectionManagerAccount);
        if (block == null) {
            Log.w(TAG, "HfpClient does not support having a connection manager");
@@ -319,9 +302,7 @@ public class HfpClientConnectionService extends ConnectionService {

    @Override
    public void onConference(Connection connection1, Connection connection2) {
        if (DBG) {
        Log.d(TAG, "onConference " + connection1 + " " + connection2);
        }

        BluetoothDevice bd1 = ((HfpClientConnection) connection1).getDevice();
        BluetoothDevice bd2 = ((HfpClientConnection) connection2).getDevice();
@@ -395,9 +376,7 @@ public class HfpClientConnectionService extends ConnectionService {
                        .setSupportedUriSchemes(Arrays.asList(PhoneAccount.SCHEME_TEL))
                        .setCapabilities(capabilities)
                        .build();
        if (DBG) {
        Log.d(TAG, "phoneaccount: " + account);
        }
        return account;
    }

+4 −7
Original line number Diff line number Diff line
@@ -38,8 +38,7 @@ 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 TAG = "HfpClientDeviceBlock";
    private static final boolean DBG = false;
    private static final String TAG = HfpClientDeviceBlock.class.getSimpleName();

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

@@ -383,16 +382,14 @@ public class HfpClientDeviceBlock {
    // Per-Device logging

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

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

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