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