1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.dialer.storage;
18 
19 import android.Manifest;
20 import android.bluetooth.BluetoothAdapter;
21 import android.bluetooth.BluetoothDevice;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 
27 import androidx.annotation.Nullable;
28 import androidx.core.app.ActivityCompat;
29 
30 import java.util.Collections;
31 import java.util.Set;
32 
33 import javax.inject.Inject;
34 
35 import dagger.hilt.android.AndroidEntryPoint;
36 
37 /**
38  * Broadcast receiver that monitors the bluetooth device unpair event and removes entries for
39  * devices that has been unpaired.
40  */
41 @AndroidEntryPoint(BroadcastReceiver.class)
42 public class BluetoothBondedListReceiver extends Hilt_BluetoothBondedListReceiver {
43     @Inject FavoriteNumberRepository mFavoriteNumberRepository;
44     @Nullable BluetoothAdapter mBluetoothAdapter;
45 
46     @Override
onReceive(Context context, Intent intent)47     public void onReceive(Context context, Intent intent) {
48         super.onReceive(context, intent);
49         if (intent.getAction() != BluetoothDevice.ACTION_BOND_STATE_CHANGED) {
50             return;
51         }
52         if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT)
53                 != PackageManager.PERMISSION_GRANTED) {
54             return;
55         }
56 
57         if (intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE)
58                 == BluetoothDevice.BOND_NONE) {
59             Set<BluetoothDevice> pairedDevices = mBluetoothAdapter == null ? Collections.emptySet()
60                     : mBluetoothAdapter.getBondedDevices();
61             mFavoriteNumberRepository.cleanup(pairedDevices);
62         }
63     }
64 }
65