• 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 import android.os.Bundle;
21 import android.support.annotation.VisibleForTesting;
22 import android.support.v7.preference.Preference;
23 import android.support.v7.preference.PreferenceScreen;
24 import android.support.v7.preference.TwoStatePreference;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.PreferenceControllerMixin;
28 import com.android.settings.widget.VideoPreference;
29 import com.android.settingslib.core.AbstractPreferenceController;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 import com.android.settingslib.core.lifecycle.LifecycleObserver;
32 import com.android.settingslib.core.lifecycle.events.OnCreate;
33 import com.android.settingslib.core.lifecycle.events.OnPause;
34 import com.android.settingslib.core.lifecycle.events.OnResume;
35 import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState;
36 
37 public abstract class GesturePreferenceController extends AbstractPreferenceController
38         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
39         LifecycleObserver, OnResume, OnPause, OnCreate, OnSaveInstanceState  {
40 
41     @VisibleForTesting
42     static final String KEY_VIDEO_PAUSED = "key_video_paused";
43 
44     private VideoPreference mVideoPreference;
45     @VisibleForTesting
46     boolean mVideoPaused;
47 
GesturePreferenceController(Context context, Lifecycle lifecycle)48     public GesturePreferenceController(Context context, Lifecycle lifecycle) {
49         super(context);
50         if (lifecycle != null) {
51             lifecycle.addObserver(this);
52         }
53     }
54 
55     @Override
displayPreference(PreferenceScreen screen)56     public void displayPreference(PreferenceScreen screen) {
57         super.displayPreference(screen);
58         if (isAvailable()) {
59             mVideoPreference = (VideoPreference) screen.findPreference(getVideoPrefKey());
60         }
61     }
62 
63     @Override
updateState(Preference preference)64     public void updateState(Preference preference) {
65         super.updateState(preference);
66         final boolean isEnabled = isSwitchPrefEnabled();
67         if (preference != null) {
68             if (preference instanceof TwoStatePreference) {
69                 ((TwoStatePreference) preference).setChecked(isEnabled);
70             } else {
71                 preference.setSummary(isEnabled
72                         ? R.string.gesture_setting_on
73                         : R.string.gesture_setting_off);
74             }
75             // Different meanings of "Enabled" for the Preference and Controller.
76             preference.setEnabled(canHandleClicks());
77         }
78     }
79 
80     @Override
onCreate(Bundle savedInstanceState)81     public void onCreate(Bundle savedInstanceState) {
82         if (savedInstanceState != null) {
83             mVideoPaused = savedInstanceState.getBoolean(KEY_VIDEO_PAUSED, false);
84         }
85     }
86 
87     @Override
onSaveInstanceState(Bundle outState)88     public void onSaveInstanceState(Bundle outState) {
89         outState.putBoolean(KEY_VIDEO_PAUSED, mVideoPaused);
90     }
91 
92     @Override
onPause()93     public void onPause() {
94         if (mVideoPreference != null) {
95             mVideoPaused = mVideoPreference.isVideoPaused();
96             mVideoPreference.onViewInvisible();
97         }
98     }
99 
100     @Override
onResume()101     public void onResume() {
102         if (mVideoPreference != null) {
103             mVideoPreference.onViewVisible(mVideoPaused);
104         }
105     }
106 
getVideoPrefKey()107     protected abstract String getVideoPrefKey();
108 
isSwitchPrefEnabled()109     protected abstract boolean isSwitchPrefEnabled();
110 
canHandleClicks()111     protected boolean canHandleClicks() {
112         return true;
113     }
114 }
115