1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 package com.android.settings.display; 15 16 import android.content.Context; 17 import android.content.res.Configuration; 18 import android.support.v7.preference.DropDownPreference; 19 import android.support.v7.preference.Preference; 20 21 import com.android.internal.logging.nano.MetricsProto; 22 import com.android.internal.view.RotationPolicy; 23 import com.android.settings.R; 24 import com.android.settings.core.PreferenceController; 25 import com.android.settings.core.instrumentation.MetricsFeatureProvider; 26 import com.android.settings.overlay.FeatureFactory; 27 28 public class AutoRotatePreferenceController extends PreferenceController implements 29 Preference.OnPreferenceChangeListener { 30 31 private static final String KEY_AUTO_ROTATE = "auto_rotate"; 32 private final MetricsFeatureProvider mMetricsFeatureProvider; 33 34 public AutoRotatePreferenceController(Context context) { 35 super(context); 36 mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); 37 } 38 39 @Override 40 public String getPreferenceKey() { 41 return KEY_AUTO_ROTATE; 42 } 43 44 @Override 45 public void updateState(Preference preference) { 46 final DropDownPreference rotatePreference = (DropDownPreference) preference; 47 final int rotateLockedResourceId; 48 preference.setSummary("%s"); 49 // The following block sets the string used when rotation is locked. 50 // If the device locks specifically to portrait or landscape (rather than current 51 // rotation), then we use a different string to include this information. 52 if (allowAllRotations()) { 53 rotateLockedResourceId = R.string.display_auto_rotate_stay_in_current; 54 } else { 55 if (RotationPolicy.getRotationLockOrientation(mContext) 56 == Configuration.ORIENTATION_PORTRAIT) { 57 rotateLockedResourceId = R.string.display_auto_rotate_stay_in_portrait; 58 } else { 59 rotateLockedResourceId = R.string.display_auto_rotate_stay_in_landscape; 60 } 61 } 62 rotatePreference.setEntries(new CharSequence[]{ 63 mContext.getString(R.string.display_auto_rotate_rotate), 64 mContext.getString(rotateLockedResourceId), 65 }); 66 rotatePreference.setEntryValues(new CharSequence[]{"0", "1"}); 67 rotatePreference.setValueIndex(RotationPolicy.isRotationLocked(mContext) ? 68 1 : 0); 69 } 70 71 @Override 72 public boolean isAvailable() { 73 return RotationPolicy.isRotationLockToggleVisible(mContext); 74 } 75 76 private boolean allowAllRotations() { 77 return mContext.getResources().getBoolean( 78 com.android.internal.R.bool.config_allowAllRotations); 79 } 80 81 @Override 82 public boolean onPreferenceChange(Preference preference, Object newValue) { 83 final boolean locked = Integer.parseInt((String) newValue) != 0; 84 mMetricsFeatureProvider.action(mContext, MetricsProto.MetricsEvent.ACTION_ROTATION_LOCK, 85 locked); 86 RotationPolicy.setRotationLock(mContext, locked); 87 return true; 88 } 89 } 90