| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.server.deviceidle; |
| 18 |
|
| 19 |
import android.bluetooth.BluetoothAdapter; |
| 20 |
import android.bluetooth.BluetoothDevice; |
| 21 |
import android.bluetooth.BluetoothManager; |
| 22 |
import android.bluetooth.BluetoothProfile; |
| 23 |
import android.content.BroadcastReceiver; |
| 24 |
import android.content.Context; |
| 25 |
import android.content.Intent; |
| 26 |
import android.content.IntentFilter; |
| 27 |
import android.os.Handler; |
| 28 |
import android.os.Message; |
| 29 |
|
| 30 |
import com.android.internal.annotations.GuardedBy; |
| 31 |
import com.android.internal.annotations.VisibleForTesting; |
| 32 |
import com.android.server.DeviceIdleInternal; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class BluetoothConstraint implements IDeviceIdleConstraint { |
| 40 |
private static final String TAG = BluetoothConstraint.class.getSimpleName(); |
| 41 |
private static final long INACTIVITY_TIMEOUT_MS = 20 * 60 * 1000L; |
| 42 |
|
| 43 |
private final Context mContext; |
| 44 |
private final Handler mHandler; |
| 45 |
private final DeviceIdleInternal mLocalService; |
| 46 |
private final BluetoothManager mBluetoothManager; |
| 47 |
|
| 48 |
private volatile boolean mConnected = true; |
| 49 |
private volatile boolean mMonitoring = false; |
| 50 |
|
| 51 |
public BluetoothConstraint( |
| 52 |
Context context, Handler handler, DeviceIdleInternal localService) { |
| 53 |
mContext = context; |
| 54 |
mHandler = handler; |
| 55 |
mLocalService = localService; |
| 56 |
mBluetoothManager = mContext.getSystemService(BluetoothManager.class); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public synchronized void startMonitoring() { |
| 61 |
|
| 62 |
mConnected = true; |
| 63 |
mMonitoring = true; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
IntentFilter filter = new IntentFilter(); |
| 68 |
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); |
| 69 |
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); |
| 70 |
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); |
| 71 |
mContext.registerReceiver(mReceiver, filter); |
| 72 |
|
| 73 |
|
| 74 |
mHandler.sendMessageDelayed( |
| 75 |
Message.obtain(mHandler, mTimeoutCallback), INACTIVITY_TIMEOUT_MS); |
| 76 |
|
| 77 |
|
| 78 |
updateAndReportActiveLocked(); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public synchronized void stopMonitoring() { |
| 83 |
mContext.unregisterReceiver(mReceiver); |
| 84 |
mHandler.removeCallbacks(mTimeoutCallback); |
| 85 |
mMonitoring = false; |
| 86 |
} |
| 87 |
|
| 88 |
private synchronized void cancelMonitoringDueToTimeout() { |
| 89 |
if (mMonitoring) { |
| 90 |
mMonitoring = false; |
| 91 |
mLocalService.onConstraintStateChanged(this, false); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
@GuardedBy("this") |
| 100 |
private void updateAndReportActiveLocked() { |
| 101 |
final boolean connected = isBluetoothConnected(mBluetoothManager); |
| 102 |
if (connected != mConnected) { |
| 103 |
mConnected = connected; |
| 104 |
|
| 105 |
mLocalService.onConstraintStateChanged(this, mConnected); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
@VisibleForTesting |
| 113 |
static boolean isBluetoothConnected(BluetoothManager bluetoothManager) { |
| 114 |
BluetoothAdapter adapter = bluetoothManager.getAdapter(); |
| 115 |
if (adapter != null && adapter.isEnabled()) { |
| 116 |
return bluetoothManager.getConnectedDevices(BluetoothProfile.GATT).size() > 0; |
| 117 |
} |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
@VisibleForTesting |
| 125 |
final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 126 |
@Override |
| 127 |
public void onReceive(Context context, Intent intent) { |
| 128 |
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(intent.getAction())) { |
| 129 |
mLocalService.exitIdle("bluetooth"); |
| 130 |
} else { |
| 131 |
updateAndReportActiveLocked(); |
| 132 |
} |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
private final Runnable mTimeoutCallback = () -> cancelMonitoringDueToTimeout(); |
| 137 |
} |