OSDN Git Service

Merge "Fix search indexing for encryption_and_credential page" into oc-dr1-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / gestures / GesturePreferenceController.java
1 /*
2  * Copyright (C) 2016 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.gestures;
18
19 import android.content.Context;
20 import android.support.v7.preference.Preference;
21 import android.support.v7.preference.PreferenceScreen;
22 import android.support.v7.preference.TwoStatePreference;
23
24 import com.android.settings.R;
25 import com.android.settings.core.PreferenceController;
26 import com.android.settings.widget.VideoPreference;
27 import com.android.settingslib.core.lifecycle.Lifecycle;
28 import com.android.settingslib.core.lifecycle.LifecycleObserver;
29 import com.android.settingslib.core.lifecycle.events.OnStart;
30 import com.android.settingslib.core.lifecycle.events.OnStop;
31
32 public abstract class GesturePreferenceController extends PreferenceController
33         implements Preference.OnPreferenceChangeListener, LifecycleObserver, OnStart, OnStop {
34
35     private VideoPreference mVideoPreference;
36
37     public GesturePreferenceController(Context context, Lifecycle lifecycle) {
38         super(context);
39         if (lifecycle != null) {
40             lifecycle.addObserver(this);
41         }
42     }
43
44     @Override
45     public void displayPreference(PreferenceScreen screen) {
46         super.displayPreference(screen);
47         if (isAvailable()) {
48             mVideoPreference = (VideoPreference) screen.findPreference(getVideoPrefKey());
49         }
50     }
51
52     @Override
53     public void updateState(Preference preference) {
54         super.updateState(preference);
55         final boolean isEnabled = isSwitchPrefEnabled();
56         if (preference != null) {
57             if (preference instanceof TwoStatePreference) {
58                 ((TwoStatePreference) preference).setChecked(isEnabled);
59             } else {
60                 preference.setSummary(isEnabled
61                         ? R.string.gesture_setting_on
62                         : R.string.gesture_setting_off);
63             }
64             // Different meanings of "Enabled" for the Preference and Controller.
65             preference.setEnabled(canHandleClicks());
66         }
67     }
68
69     @Override
70     public void onStop() {
71         if (mVideoPreference != null) {
72             mVideoPreference.onViewInvisible();
73         }
74     }
75
76     @Override
77     public void onStart() {
78         if (mVideoPreference != null) {
79             mVideoPreference.onViewVisible();
80         }
81     }
82
83     protected abstract String getVideoPrefKey();
84
85     protected abstract boolean isSwitchPrefEnabled();
86
87     protected boolean canHandleClicks() {
88         return true;
89     }
90 }