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.settings.network;
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.UsbManager;
24 import android.net.ConnectivityManager;
25 import android.os.Environment;
26 import android.text.TextUtils;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.lifecycle.Lifecycle;
30 import androidx.lifecycle.OnLifecycleEvent;
31 
32 import com.android.settings.Utils;
33 
34 /**
35  * This controller helps to manage the switch state and visibility of USB tether switch
36  * preference.
37  *
38  */
39 public final class UsbTetherPreferenceController extends TetherBasePreferenceController {
40 
41     private static final String TAG = "UsbTetherPrefController";
42 
43     private boolean mUsbConnected;
44     private boolean mMassStorageActive;
45 
UsbTetherPreferenceController(Context context, String prefKey)46     public UsbTetherPreferenceController(Context context, String prefKey) {
47         super(context, prefKey);
48     }
49 
50     @OnLifecycleEvent(Lifecycle.Event.ON_START)
onStart()51     public void onStart() {
52         mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
53         IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE);
54         filter.addAction(Intent.ACTION_MEDIA_SHARED);
55         filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
56         mContext.registerReceiver(mUsbChangeReceiver, filter);
57     }
58 
59     @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
onStop()60     public void onStop() {
61         mContext.unregisterReceiver(mUsbChangeReceiver);
62     }
63 
64     @Override
shouldEnable()65     public boolean shouldEnable() {
66         return mUsbConnected && !mMassStorageActive;
67     }
68 
69     @Override
shouldShow()70     public boolean shouldShow() {
71         String[] usbRegexs = mCm.getTetherableUsbRegexs();
72         return  usbRegexs != null && usbRegexs.length != 0 && !Utils.isMonkeyRunning();
73     }
74 
75     @Override
getTetherType()76     public int getTetherType() {
77         return ConnectivityManager.TETHERING_USB;
78     }
79 
80     @VisibleForTesting
81     final BroadcastReceiver mUsbChangeReceiver = new BroadcastReceiver() {
82         @Override
83         public void onReceive(Context context, Intent intent) {
84             String action = intent.getAction();
85             if (TextUtils.equals(Intent.ACTION_MEDIA_SHARED, action)) {
86                 mMassStorageActive = true;
87             } else if (TextUtils.equals(Intent.ACTION_MEDIA_UNSHARED, action)) {
88                 mMassStorageActive = false;
89             } else if (TextUtils.equals(UsbManager.ACTION_USB_STATE, action)) {
90                 mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
91             }
92             updateState(mPreference);
93         }
94     };
95 }
96