1 /*
2  * Copyright (C) 2023 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.settings.connecteddevice.stylus;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.hardware.usb.UsbDevice;
24 import android.hardware.usb.UsbManager;
25 
26 /** Broadcast receiver for styluses connected via USB **/
27 public class UsbStylusBroadcastReceiver extends BroadcastReceiver {
28     private Context mContext;
29     private UsbStylusConnectionListener mUsbConnectionListener;
30     private boolean mListeningToUsbEvents;
31 
UsbStylusBroadcastReceiver(Context context, UsbStylusConnectionListener usbConnectionListener)32     public UsbStylusBroadcastReceiver(Context context,
33             UsbStylusConnectionListener usbConnectionListener) {
34         mContext = context;
35         mUsbConnectionListener = usbConnectionListener;
36     }
37 
38     /** Registers the receiver. **/
register()39     public void register() {
40         if (!mListeningToUsbEvents) {
41             final IntentFilter intentFilter = new IntentFilter();
42             intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
43             intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
44             intentFilter.addAction(UsbManager.ACTION_USB_STATE);
45             final Intent intent = mContext.registerReceiver(this, intentFilter);
46             if (intent != null) {
47                 onReceive(mContext, intent);
48             }
49             mListeningToUsbEvents = true;
50         }
51     }
52 
53     /** Unregisters the receiver. **/
unregister()54     public void unregister() {
55         if (mListeningToUsbEvents) {
56             mContext.unregisterReceiver(this);
57             mListeningToUsbEvents = false;
58         }
59     }
60 
61     @Override
onReceive(Context context, Intent intent)62     public void onReceive(Context context, Intent intent) {
63         UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE, UsbDevice.class);
64         if (StylusUsbFirmwareController.hasUsbStylusFirmwareUpdateFeature(usbDevice)) {
65             mUsbConnectionListener.onUsbStylusConnectionChanged(usbDevice,
66                     intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED));
67         }
68     }
69 
70     /**
71      * Interface definition for a callback to be invoked when stylus usb connection is changed.
72      */
73     interface UsbStylusConnectionListener {
onUsbStylusConnectionChanged(UsbDevice device, boolean connected)74         void onUsbStylusConnectionChanged(UsbDevice device, boolean connected);
75     }
76 }
77