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