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             mVideoPreference = screen.findPreference(getVideoPrefKey());
46         }
47     }
48 
49     @Override
updateState(Preference preference)50     public void updateState(Preference preference) {
51         super.updateState(preference);
52         if (preference != null) {
53             // Different meanings of "Enabled" for the Preference and Controller.
54             preference.setEnabled(canHandleClicks());
55         }
56     }
57 
58     @Override
getSummary()59     public CharSequence getSummary() {
60         return mContext.getText(
61                 isChecked() ? R.string.gesture_setting_on : R.string.gesture_setting_off);
62     }
63 
64     @Override
onStart()65     public void onStart() {
66         if (mVideoPreference != null) {
67             mVideoPreference.onViewVisible();
68         }
69     }
70 
71     @Override
onStop()72     public void onStop() {
73         if (mVideoPreference != null) {
74             mVideoPreference.onViewInvisible();
75         }
76     }
77 
getVideoPrefKey()78     protected abstract String getVideoPrefKey();
79 
canHandleClicks()80     protected boolean canHandleClicks() {
81         return true;
82     }
83 }
84