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 */
14package com.android.settings.display;
15
16import android.content.Context;
17import android.support.v7.preference.Preference;
18import android.support.v7.preference.TwoStatePreference;
19
20import com.android.internal.logging.nano.MetricsProto;
21import com.android.internal.view.RotationPolicy;
22import com.android.settings.core.PreferenceControllerMixin;
23import com.android.settings.core.instrumentation.MetricsFeatureProvider;
24import com.android.settings.overlay.FeatureFactory;
25import com.android.settingslib.core.AbstractPreferenceController;
26import com.android.settingslib.core.lifecycle.Lifecycle;
27import com.android.settingslib.core.lifecycle.LifecycleObserver;
28import com.android.settingslib.core.lifecycle.events.OnPause;
29import com.android.settingslib.core.lifecycle.events.OnResume;
30
31public class AutoRotatePreferenceController extends AbstractPreferenceController implements
32        PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver,
33        OnResume, OnPause {
34
35    private static final String KEY_AUTO_ROTATE = "auto_rotate";
36    private final MetricsFeatureProvider mMetricsFeatureProvider;
37    private TwoStatePreference mPreference;
38    private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
39
40    public AutoRotatePreferenceController(Context context, Lifecycle lifecycle) {
41        super(context);
42        mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
43        if (lifecycle != null) {
44            lifecycle.addObserver(this);
45        }
46    }
47
48    @Override
49    public String getPreferenceKey() {
50        return KEY_AUTO_ROTATE;
51    }
52
53    @Override
54    public void updateState(Preference preference) {
55        mPreference = (TwoStatePreference) preference;
56        updatePreference();
57    }
58
59    @Override
60    public boolean isAvailable() {
61        return RotationPolicy.isRotationLockToggleVisible(mContext);
62    }
63
64    @Override
65    public boolean onPreferenceChange(Preference preference, Object newValue) {
66        final boolean locked = !(boolean) newValue;
67        mMetricsFeatureProvider.action(mContext, MetricsProto.MetricsEvent.ACTION_ROTATION_LOCK,
68                locked);
69        RotationPolicy.setRotationLock(mContext, locked);
70        return true;
71    }
72
73    @Override
74    public void onResume() {
75        if (mRotationPolicyListener == null) {
76            mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
77                @Override
78                public void onChange() {
79                    updatePreference();
80                }
81            };
82        }
83        RotationPolicy.registerRotationPolicyListener(mContext,
84                mRotationPolicyListener);
85    }
86
87    @Override
88    public void onPause() {
89        if (mRotationPolicyListener != null) {
90            RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
91        }
92    }
93
94    private void updatePreference() {
95        if (mPreference == null) {
96            return;
97        }
98        mPreference.setChecked(!RotationPolicy.isRotationLocked(mContext));
99    }
100}
101