• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_ACCESSORY)) {
76                 functions |= UsbManager.FUNCTION_ACCESSORY;
77             }
78             mFunctions = functions;
79             mDataRole = mUsbBackend.getDataRole();
80             mPowerRole = mUsbBackend.getPowerRole();
81         } else if (UsbManager.ACTION_USB_PORT_CHANGED.equals(intent.getAction())) {
82             UsbPortStatus portStatus = intent.getExtras()
83                     .getParcelable(UsbManager.EXTRA_PORT_STATUS);
84             if (portStatus != null) {
85                 mDataRole = portStatus.getCurrentDataRole();
86                 mPowerRole = portStatus.getCurrentPowerRole();
87             }
88         }
89         if (mUsbConnectionListener != null) {
90             mUsbConnectionListener.onUsbConnectionChanged(mConnected, mFunctions, mPowerRole,
91                     mDataRole);
92         }
93     }
94 
register()95     public void register() {
96         if (!mListeningToUsbEvents) {
97             mConnected = false;
98             final IntentFilter intentFilter = new IntentFilter();
99             intentFilter.addAction(UsbManager.ACTION_USB_STATE);
100             intentFilter.addAction(UsbManager.ACTION_USB_PORT_CHANGED);
101             final Intent intent = mContext.registerReceiver(this, intentFilter);
102             // TODO b/77240599 use an api instead of sticky intent
103             if (intent != null) {
104                 onReceive(mContext, intent);
105             }
106             mListeningToUsbEvents = true;
107         }
108     }
109 
unregister()110     public void unregister() {
111         if (mListeningToUsbEvents) {
112             mContext.unregisterReceiver(this);
113             mListeningToUsbEvents = false;
114         }
115     }
116 
isConnected()117     public boolean isConnected() {
118         return mConnected;
119     }
120 
121     @Override
onResume()122     public void onResume() {
123         register();
124     }
125 
126     @Override
onPause()127     public void onPause() {
128         unregister();
129     }
130 
131     /**
132      * Interface definition for a callback to be invoked when usb connection is changed.
133      */
134     interface UsbConnectionListener {
onUsbConnectionChanged(boolean connected, long functions, int powerRole, int dataRole)135         void onUsbConnectionChanged(boolean connected, long functions, int powerRole, int dataRole);
136     }
137 }
138