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.os.SystemProperties;
     21 import android.provider.Settings;
     22 import android.support.annotation.VisibleForTesting;
     23 import android.support.v14.preference.SwitchPreference;
     24 import android.support.v7.preference.Preference;
     25 
     26 import com.android.internal.app.LocalePicker;
     27 import com.android.settings.core.PreferenceControllerMixin;
     28 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
     29 
     30 public class RtlLayoutPreferenceController extends DeveloperOptionsPreferenceController implements
     31         Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
     32 
     33     private static final String FORCE_RTL_LAYOUT_KEY = "force_rtl_layout_all_locales";
     34 
     35     @VisibleForTesting
     36     static final int SETTING_VALUE_ON = 1;
     37     @VisibleForTesting
     38     static final int SETTING_VALUE_OFF = 0;
     39 
     40     public RtlLayoutPreferenceController(Context context) {
     41         super(context);
     42     }
     43 
     44     @Override
     45     public String getPreferenceKey() {
     46         return FORCE_RTL_LAYOUT_KEY;
     47     }
     48 
     49     @Override
     50     public boolean onPreferenceChange(Preference preference, Object newValue) {
     51         final boolean isEnabled = (Boolean) newValue;
     52         writeToForceRtlLayoutSetting(isEnabled);
     53         updateLocales();
     54         return true;
     55     }
     56 
     57     @Override
     58     public void updateState(Preference preference) {
     59         int rtlLayoutMode = Settings.Global.getInt(mContext.getContentResolver(),
     60                 Settings.Global.DEVELOPMENT_FORCE_RTL, SETTING_VALUE_OFF);
     61         ((SwitchPreference) mPreference).setChecked(rtlLayoutMode != SETTING_VALUE_OFF);
     62     }
     63 
     64     @Override
     65     protected void onDeveloperOptionsSwitchDisabled() {
     66         super.onDeveloperOptionsSwitchDisabled();
     67         writeToForceRtlLayoutSetting(false);
     68         updateLocales();
     69         ((SwitchPreference) mPreference).setChecked(false);
     70     }
     71 
     72     @VisibleForTesting
     73     void updateLocales() {
     74         LocalePicker.updateLocales(mContext.getResources().getConfiguration().getLocales());
     75     }
     76 
     77     private void writeToForceRtlLayoutSetting(boolean isEnabled) {
     78         Settings.Global.putInt(mContext.getContentResolver(),
     79                 Settings.Global.DEVELOPMENT_FORCE_RTL,
     80                 isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
     81         SystemProperties.set(Settings.Global.DEVELOPMENT_FORCE_RTL,
     82                 isEnabled ? Integer.toString(SETTING_VALUE_ON)
     83                         : Integer.toString(SETTING_VALUE_OFF));
     84     }
     85 }
     86