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

Commit 21fbd606 authored by Andrew Cheng's avatar Andrew Cheng Committed by Joseph Pirozzo
Browse files

Delete call logs when calls are made without PBAP

PbapClientConnectionHandler has code to remove calllogs when PBAP
disconnects. However, if PBAP was never connected/enabled in the first
place, and calls are made over HFP, these calllogs will not be removed
when the device disconnects. This CL ensures calllogs are still removed
in this case.

Bug: 143723278

Test: (1) Verifying call logs are removed: Connect phone to carkit
without enabling PBAP (e.g., don't allow permissions during pairing).
Place a phone call over Bluetooth. Examine the calllog database
directly: "sqlite3 calllog.db .dump” to verify call entry is present.
Disconnect Bluetooth from the phone. Examine database again to verify
entry is deleted. Repeat by disconnecting from the carkit, as well as
disconnecting only HFP. (2) Verifying only HFP call logs are removed:
Connect phone to carkit, with PBAP enabled this time. Place a phone call
over HFP. Disable HFP profile, leaving PBAP enabled. Verify the
HFP-entry is removed from calllog.db, while the PBAP-entries are still
present.

Change-Id: I305b323194f7732967db9a086d2c8a689798e64d
Merged-In: I305b323194f7732967db9a086d2c8a689798e64d
(cherry picked from commit e3c349a1)
parent 313b13a2
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -19,9 +19,11 @@ package com.android.bluetooth.pbapclient;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadsetClient;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.IBluetoothPbapClient;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
@@ -31,6 +33,7 @@ import android.util.Log;
import com.android.bluetooth.R;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.hfpclient.connserv.HfpClientConnectionService;
import com.android.bluetooth.sdp.SdpManager;

import java.util.ArrayList;
@@ -71,6 +74,9 @@ public class PbapClientService extends ProfileService {
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        // delay initial download until after the user is unlocked to add an account.
        filter.addAction(Intent.ACTION_USER_UNLOCKED);
        // To remove call logs when PBAP was never connected while calls were made,
        // we also listen for HFP to become disconnected.
        filter.addAction(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED);
        try {
            registerReceiver(mPbapBroadcastReceiver, filter);
        } catch (Exception e) {
@@ -128,6 +134,21 @@ public class PbapClientService extends ProfileService {
        }
    }

    private void removeHfpCallLog(String accountName, Context context) {
        if (DBG) Log.d(TAG, "Removing call logs from " + accountName);
        // Delete call logs belonging to accountName==BD_ADDR that also match
        // component name "hfpclient".
        ComponentName componentName = new ComponentName(context, HfpClientConnectionService.class);
        String selectionFilter = CallLog.Calls.PHONE_ACCOUNT_ID + "=? AND "
                + CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME + "=?";
        String[] selectionArgs = new String[]{accountName, componentName.flattenToString()};
        try {
            getContentResolver().delete(CallLog.Calls.CONTENT_URI, selectionFilter, selectionArgs);
        } catch (IllegalArgumentException e) {
            Log.w(TAG, "Call Logs could not be deleted, they may not exist yet.");
        }
    }

    private void registerSdpRecord() {
        SdpManager sdpManager = SdpManager.getDefaultManager();
        if (sdpManager == null) {
@@ -171,6 +192,21 @@ public class PbapClientService extends ProfileService {
                for (PbapClientStateMachine stateMachine : mPbapClientStateMachineMap.values()) {
                    stateMachine.resumeDownload();
                }
            } else if (action.equals(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED)) {
                // PbapClientConnectionHandler has code to remove calllogs when PBAP disconnects.
                // However, if PBAP was never connected/enabled in the first place, and calls are
                // made over HFP, these calllogs will not be removed when the device disconnects.
                // This code ensures callogs are still removed in this case.
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);

                if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                    if (DBG) {
                        Log.d(TAG, "Received intent to disconnect HFP with " + device);
                    }
                    // HFP client stores entries in calllog.db by BD_ADDR and component name
                    removeHfpCallLog(device.getAddress(), context);
                }
            }
        }
    }