1 /*
2  * Copyright (C) 2017 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 package com.android.settings.connecteddevice.usb;
17 
18 import android.content.BroadcastReceiver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.IntentFilter;
22 import android.hardware.usb.UsbManager;
23 import android.hardware.usb.UsbPortStatus;
24 
25 import com.android.settingslib.core.lifecycle.LifecycleObserver;
26 import com.android.settingslib.core.lifecycle.events.OnPause;
27 import com.android.settingslib.core.lifecycle.events.OnResume;
28 
29 /**
30  * Receiver to receive usb update and use {@link UsbConnectionListener} to invoke callback
31  */
32 public class UsbConnectionBroadcastReceiver extends BroadcastReceiver implements LifecycleObserver,
33         OnResume, OnPause {
34     private Context mContext;
35     private UsbConnectionListener mUsbConnectionListener;
36     private boolean mListeningToUsbEvents;
37     private UsbBackend mUsbBackend;
38 
39     private boolean mConnected;
40     private long mFunctions;
41     private int mDataRole;
42     private int mPowerRole;
43 
UsbConnectionBroadcastReceiver(Context context, UsbConnectionListener usbConnectionListener, UsbBackend backend)44     public UsbConnectionBroadcastReceiver(Context context,
45             UsbConnectionListener usbConnectionListener, UsbBackend backend) {
46         mContext = context;
47         mUsbConnectionListener = usbConnectionListener;
48         mUsbBackend = backend;
49 
50         mFunctions = UsbManager.FUNCTION_NONE;
51         mDataRole = UsbPortStatus.DATA_ROLE_NONE;
52         mPowerRole = UsbPortStatus.POWER_ROLE_NONE;
53     }
54 
55     @Override
onReceive(Context context, Intent intent)56     public void onReceive(Context context, Intent intent) {
57         if (UsbManager.ACTION_USB_STATE.equals(intent.getAction())) {
58             mConnected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED)
59                     || intent.getExtras().getBoolean(UsbManager.USB_HOST_CONNECTED);
60             long functions = UsbManager.FUNCTION_NONE;
61             if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_MTP)
62                     && intent.getExtras().getBoolean(UsbManager.USB_DATA_UNLOCKED, false)) {
63                 functions |= UsbManager.FUNCTION_MTP;
64             }
65             if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_PTP)
66                     && intent.getExtras().getBoolean(UsbManager.USB_DATA_UNLOCKED, false)) {
67                 functions |= UsbManager.FUNCTION_PTP;
68             }
69             if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_MIDI)) {
70                 functions |= UsbManager.FUNCTION_MIDI;
71             }
72             if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_RNDIS)) {
73                 functions |= UsbManager.FUNCTION_RNDIS;
74             }
75             mFunctions = functions;
76             mDataRole = mUsbBackend.getDataRole();
77             mPowerRole = mUsbBackend.getPowerRole();
78         } else if (UsbManager.ACTION_USB_PORT_CHANGED.equals(intent.getAction())) {
79             UsbPortStatus portStatus = intent.getExtras()
80                     .getParcelable(UsbManager.EXTRA_PORT_STATUS);
81             if (portStatus != null) {
82                 mDataRole = portStatus.getCurrentDataRole();
83                 mPowerRole = portStatus.getCurrentPowerRole();
84             }
85         }
86         if (mUsbConnectionListener != null) {
87             mUsbConnectionListener.onUsbConnectionChanged(mConnected, mFunctions, mPowerRole,
88                     mDataRole);
89         }
90     }
91 
register()92     public void register() {
93         if (!mListeningToUsbEvents) {
94             mConnected = false;
95             final IntentFilter intentFilter = new IntentFilter();
96             intentFilter.addAction(UsbManager.ACTION_USB_STATE);
97             intentFilter.addAction(UsbManager.ACTION_USB_PORT_CHANGED);
98             final Intent intent = mContext.registerReceiver(this, intentFilter);
99             // TODO b/77240599 use an api instead of sticky intent
100             if (intent != null) {
101                 onReceive(mContext, intent);
102             }
103             mListeningToUsbEvents = true;
104         }
105     }
106 
unregister()107     public void unregister() {
108         if (mListeningToUsbEvents) {
109             mContext.unregisterReceiver(this);
110             mListeningToUsbEvents = false;
111         }
112     }
113 
isConnected()114     public boolean isConnected() {
115         return mConnected;
116     }
117 
118     @Override
onResume()119     public void onResume() {
120         register();
121     }
122 
123     @Override
onPause()124     public void onPause() {
125         unregister();
126     }
127 
128     /**
129      * Interface definition for a callback to be invoked when usb connection is changed.
130      */
131     interface UsbConnectionListener {
onUsbConnectionChanged(boolean connected, long functions, int powerRole, int dataRole)132         void onUsbConnectionChanged(boolean connected, long functions, int powerRole, int dataRole);
133     }
134 }
135