1 /*
2  * Copyright 2018 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;
17 
18 import static com.android.settingslib.drawer.TileUtils.IA_SETTINGS_ACTION;
19 
20 import android.content.Context;
21 import android.content.Intent;
22 import android.provider.Settings;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.BasePreferenceController;
28 import com.android.settings.nfc.NfcPreferenceController;
29 
30 /**
31  * Controller that used to show which component is available
32  */
33 public class AdvancedConnectedDeviceController extends BasePreferenceController {
34 
35     private static final String DRIVING_MODE_SETTINGS_ENABLED =
36             "gearhead:driving_mode_settings_enabled";
37     private static final String GEARHEAD_PACKAGE = "com.google.android.projection.gearhead";
38 
AdvancedConnectedDeviceController(Context context, String preferenceKey)39     public AdvancedConnectedDeviceController(Context context, String preferenceKey) {
40         super(context, preferenceKey);
41     }
42 
43     @Override
getAvailabilityStatus()44     public int getAvailabilityStatus() {
45         return AVAILABLE;
46     }
47 
48     @Override
getSummary()49     public CharSequence getSummary() {
50         return mContext.getText(getConnectedDevicesSummaryResourceId(mContext));
51     }
52 
53     /**
54      * Get Connected Devices summary that depend on {@link NfcPreferenceController} or
55      * diving mode are available
56      */
getConnectedDevicesSummaryResourceId(Context context)57     public static int getConnectedDevicesSummaryResourceId(Context context) {
58         final NfcPreferenceController nfcPreferenceController =
59                 new NfcPreferenceController(context, NfcPreferenceController.KEY_TOGGLE_NFC);
60 
61         return getConnectedDevicesSummaryResourceId(nfcPreferenceController,
62                 isDrivingModeAvailable(context), isAndroidAutoSettingAvailable(context));
63     }
64 
65     @VisibleForTesting
isDrivingModeAvailable(Context context)66     static boolean isDrivingModeAvailable(Context context) {
67         return Settings.System.
68                 getInt(context.getContentResolver(), DRIVING_MODE_SETTINGS_ENABLED, 0) == 1;
69     }
70 
71     @VisibleForTesting
isAndroidAutoSettingAvailable(Context context)72     static boolean isAndroidAutoSettingAvailable(Context context) {
73         final Intent intent = new Intent(IA_SETTINGS_ACTION);
74         intent.setPackage(GEARHEAD_PACKAGE);
75         return intent.resolveActivity(context.getPackageManager()) != null;
76     }
77 
78     @VisibleForTesting
getConnectedDevicesSummaryResourceId(NfcPreferenceController nfcPreferenceController, boolean isDrivingModeAvailable, boolean isAndroidAutoAvailable)79     static int getConnectedDevicesSummaryResourceId(NfcPreferenceController
80             nfcPreferenceController,
81             boolean isDrivingModeAvailable,
82             boolean isAndroidAutoAvailable) {
83         final int resId;
84 
85         if (isAndroidAutoAvailable) {
86             if (nfcPreferenceController.isAvailable()) {
87                 if (isDrivingModeAvailable) {
88                     // NFC available, driving mode available
89                     resId = R.string.connected_devices_dashboard_android_auto_summary;
90                 } else {
91                     // NFC available, driving mode not available
92                     resId =
93                         R.string.connected_devices_dashboard_android_auto_no_driving_mode_summary;
94                 }
95             } else {
96                 if (isDrivingModeAvailable) {
97                     // NFC not available, driving mode available
98                     resId = R.string.connected_devices_dashboard_android_auto_no_nfc_summary;
99                 } else {
100                     // NFC not available, driving mode not available
101                     resId =
102                         R.string.connected_devices_dashboard_android_auto_no_nfc_no_driving_mode;
103                 }
104             }
105         } else {
106             if (nfcPreferenceController.isAvailable()) {
107                 if (isDrivingModeAvailable) {
108                     // NFC available, driving mode available
109                     resId = R.string.connected_devices_dashboard_summary;
110                 } else {
111                     // NFC available, driving mode not available
112                     resId = R.string.connected_devices_dashboard_no_driving_mode_summary;
113                 }
114             } else {
115                 if (isDrivingModeAvailable) {
116                     // NFC not available, driving mode available
117                     resId = R.string.connected_devices_dashboard_no_nfc_summary;
118                 } else {
119                     // NFC not available, driving mode not available
120                     resId = R.string.connected_devices_dashboard_no_driving_mode_no_nfc_summary;
121                 }
122             }
123         }
124 
125         return resId;
126     }
127 }
128