• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.inputmethod.latin.settings;
18 
19 import android.os.Build;
20 import android.preference.CheckBoxPreference;
21 import android.preference.Preference;
22 import android.preference.PreferenceGroup;
23 import android.preference.SwitchPreference;
24 
25 import java.util.ArrayList;
26 
27 public class TwoStatePreferenceHelper {
28     private static final String EMPTY_TEXT = "";
29 
TwoStatePreferenceHelper()30     private TwoStatePreferenceHelper() {
31         // This utility class is not publicly instantiable.
32     }
33 
replaceCheckBoxPreferencesBySwitchPreferences(final PreferenceGroup group)34     public static void replaceCheckBoxPreferencesBySwitchPreferences(final PreferenceGroup group) {
35         // The keyboard settings keeps using a CheckBoxPreference on KitKat or previous.
36         if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
37             return;
38         }
39         // The keyboard settings starts using a SwitchPreference without switch on/off text on
40         // API versions newer than KitKat.
41         replaceAllCheckBoxPreferencesBySwitchPreferences(group);
42     }
43 
replaceAllCheckBoxPreferencesBySwitchPreferences( final PreferenceGroup group)44     private static void replaceAllCheckBoxPreferencesBySwitchPreferences(
45             final PreferenceGroup group) {
46         final ArrayList<Preference> preferences = new ArrayList<>();
47         final int count = group.getPreferenceCount();
48         for (int index = 0; index < count; index++) {
49             preferences.add(group.getPreference(index));
50         }
51         group.removeAll();
52         for (int index = 0; index < count; index++) {
53             final Preference preference = preferences.get(index);
54             if (preference instanceof CheckBoxPreference) {
55                 addSwitchPreferenceBasedOnCheckBoxPreference((CheckBoxPreference)preference, group);
56             } else {
57                 group.addPreference(preference);
58                 if (preference instanceof PreferenceGroup) {
59                     replaceAllCheckBoxPreferencesBySwitchPreferences((PreferenceGroup)preference);
60                 }
61             }
62         }
63     }
64 
addSwitchPreferenceBasedOnCheckBoxPreference(final CheckBoxPreference checkBox, final PreferenceGroup group)65     static void addSwitchPreferenceBasedOnCheckBoxPreference(final CheckBoxPreference checkBox,
66             final PreferenceGroup group) {
67         final SwitchPreference switchPref = new SwitchPreference(checkBox.getContext());
68         switchPref.setTitle(checkBox.getTitle());
69         switchPref.setKey(checkBox.getKey());
70         switchPref.setOrder(checkBox.getOrder());
71         switchPref.setPersistent(checkBox.isPersistent());
72         switchPref.setEnabled(checkBox.isEnabled());
73         switchPref.setChecked(checkBox.isChecked());
74         switchPref.setSummary(checkBox.getSummary());
75         switchPref.setSummaryOn(checkBox.getSummaryOn());
76         switchPref.setSummaryOff(checkBox.getSummaryOff());
77         switchPref.setSwitchTextOn(EMPTY_TEXT);
78         switchPref.setSwitchTextOff(EMPTY_TEXT);
79         group.addPreference(switchPref);
80         switchPref.setDependency(checkBox.getDependency());
81     }
82 }
83