Home | History | Annotate | Download | only in development
      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 
     17 package com.android.settings.development;
     18 
     19 import android.content.Context;
     20 import android.provider.Settings;
     21 import android.support.v7.preference.ListPreference;
     22 import android.support.v7.preference.Preference;
     23 import android.text.TextUtils;
     24 
     25 import com.android.settings.R;
     26 import com.android.settings.core.PreferenceControllerMixin;
     27 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
     28 
     29 public class SecondaryDisplayPreferenceController extends DeveloperOptionsPreferenceController
     30         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
     31 
     32     private static final String OVERLAY_DISPLAY_DEVICES_KEY = "overlay_display_devices";
     33 
     34     private final String[] mListValues;
     35     private final String[] mListSummaries;
     36 
     37     public SecondaryDisplayPreferenceController(Context context) {
     38         super(context);
     39 
     40         mListValues = context.getResources().getStringArray(R.array.overlay_display_devices_values);
     41         mListSummaries = context.getResources().getStringArray(
     42                 R.array.overlay_display_devices_entries);
     43     }
     44 
     45     @Override
     46     public String getPreferenceKey() {
     47         return OVERLAY_DISPLAY_DEVICES_KEY;
     48     }
     49 
     50     @Override
     51     public boolean onPreferenceChange(Preference preference, Object newValue) {
     52         writeSecondaryDisplayDevicesOption(newValue.toString());
     53         return true;
     54     }
     55 
     56     @Override
     57     public void updateState(Preference preference) {
     58         updateSecondaryDisplayDevicesOptions();
     59     }
     60 
     61     @Override
     62     protected void onDeveloperOptionsSwitchDisabled() {
     63         super.onDeveloperOptionsSwitchDisabled();
     64         writeSecondaryDisplayDevicesOption(null);
     65     }
     66 
     67     private void updateSecondaryDisplayDevicesOptions() {
     68         final String value = Settings.Global.getString(mContext.getContentResolver(),
     69                 Settings.Global.OVERLAY_DISPLAY_DEVICES);
     70         int index = 0; // default
     71         for (int i = 0; i < mListValues.length; i++) {
     72             if (TextUtils.equals(value, mListValues[i])) {
     73                 index = i;
     74                 break;
     75             }
     76         }
     77         final ListPreference listPreference = (ListPreference) mPreference;
     78         listPreference.setValue(mListValues[index]);
     79         listPreference.setSummary(mListSummaries[index]);
     80     }
     81 
     82     private void writeSecondaryDisplayDevicesOption(String newValue) {
     83         Settings.Global.putString(mContext.getContentResolver(),
     84                 Settings.Global.OVERLAY_DISPLAY_DEVICES, newValue);
     85         updateSecondaryDisplayDevicesOptions();
     86     }
     87 }
     88