• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 import androidx.preference.Preference;
22 import androidx.preference.PreferenceScreen;
23 
24 import com.android.settings.R;
25 import com.android.settings.core.TogglePreferenceController;
26 import com.android.settings.widget.VideoPreference;
27 import com.android.settingslib.core.lifecycle.LifecycleObserver;
28 import com.android.settingslib.core.lifecycle.events.OnStart;
29 import com.android.settingslib.core.lifecycle.events.OnStop;
30 
31 public abstract class GesturePreferenceController extends TogglePreferenceController
32         implements Preference.OnPreferenceChangeListener,
33         LifecycleObserver, OnStart, OnStop {
34 
35     private VideoPreference mVideoPreference;
36 
GesturePreferenceController(Context context, String key)37     public GesturePreferenceController(Context context, String key) {
38         super(context, key);
39     }
40 
41     @Override
displayPreference(PreferenceScreen screen)42     public void displayPreference(PreferenceScreen screen) {
43         super.displayPreference(screen);
44         if (isAvailable()) {
45             final Preference pref = screen.findPreference(getVideoPrefKey());
46             if (pref instanceof VideoPreference) {
47                 mVideoPreference = screen.findPreference(getVideoPrefKey());
48             }
49         }
50     }
51 
52     @Override
updateState(Preference preference)53     public void updateState(Preference preference) {
54         super.updateState(preference);
55         if (preference != null) {
56             // Different meanings of "Enabled" for the Preference and Controller.
57             preference.setEnabled(canHandleClicks());
58         }
59     }
60 
61     @Override
getSummary()62     public CharSequence getSummary() {
63         return mContext.getText(
64                 isChecked() ? R.string.gesture_setting_on : R.string.gesture_setting_off);
65     }
66 
67     @Override
onStart()68     public void onStart() {
69         if (mVideoPreference != null) {
70             mVideoPreference.onViewVisible();
71         }
72     }
73 
74     @Override
onStop()75     public void onStop() {
76         if (mVideoPreference != null) {
77             mVideoPreference.onViewInvisible();
78         }
79     }
80 
getVideoPrefKey()81     protected abstract String getVideoPrefKey();
82 
canHandleClicks()83     protected boolean canHandleClicks() {
84         return true;
85     }
86 }
87