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 android.content.Context;
19 import android.provider.Settings;
20 
21 import androidx.annotation.VisibleForTesting;
22 
23 import com.android.settings.R;
24 import com.android.settings.core.BasePreferenceController;
25 import com.android.settings.nfc.NfcPreferenceController;
26 
27 /**
28  * Controller that used to show which component is available
29  */
30 public class AdvancedConnectedDeviceController extends BasePreferenceController {
31 
32     private static final String DRIVING_MODE_SETTINGS_ENABLED =
33             "gearhead:driving_mode_settings_enabled";
34 
AdvancedConnectedDeviceController(Context context, String preferenceKey)35     public AdvancedConnectedDeviceController(Context context, String preferenceKey) {
36         super(context, preferenceKey);
37     }
38 
39     @Override
getAvailabilityStatus()40     public int getAvailabilityStatus() {
41         return AVAILABLE;
42     }
43 
44     @Override
getSummary()45     public CharSequence getSummary() {
46         return mContext.getText(getConnectedDevicesSummaryResourceId(mContext));
47     }
48 
49     /**
50      * Get Connected Devices summary that depend on {@link NfcPreferenceController} or
51      * diving mode are available
52      */
getConnectedDevicesSummaryResourceId(Context context)53     public static int getConnectedDevicesSummaryResourceId(Context context) {
54         final NfcPreferenceController nfcPreferenceController =
55                 new NfcPreferenceController(context, NfcPreferenceController.KEY_TOGGLE_NFC);
56 
57         return getConnectedDevicesSummaryResourceId(nfcPreferenceController,
58                 isDrivingModeAvailable(context));
59     }
60 
61     @VisibleForTesting
isDrivingModeAvailable(Context context)62     static boolean isDrivingModeAvailable(Context context) {
63         return Settings.System.
64                 getInt(context.getContentResolver(), DRIVING_MODE_SETTINGS_ENABLED, 0) == 1;
65     }
66 
67     @VisibleForTesting
getConnectedDevicesSummaryResourceId(NfcPreferenceController nfcPreferenceController, boolean isDrivingModeAvailable)68     static int getConnectedDevicesSummaryResourceId(NfcPreferenceController
69             nfcPreferenceController, boolean isDrivingModeAvailable) {
70         final int resId;
71 
72         if (nfcPreferenceController.isAvailable()) {
73             if (isDrivingModeAvailable) {
74                 // NFC available, driving mode available
75                 resId = R.string.connected_devices_dashboard_summary;
76             } else {
77                 // NFC available, driving mode not available
78                 resId = R.string.connected_devices_dashboard_no_driving_mode_summary;
79             }
80         } else {
81             if (isDrivingModeAvailable) {
82                 // NFC not available, driving mode available
83                 resId = R.string.connected_devices_dashboard_no_nfc_summary;
84             } else {
85                 // NFC not available, driving mode not available
86                 resId = R.string.connected_devices_dashboard_no_driving_mode_no_nfc_summary;
87             }
88         }
89 
90         return resId;
91     }
92 }
93