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.core.lifecycle.Lifecycle; 27 import com.android.settings.core.lifecycle.LifecycleObserver; 28 import com.android.settings.core.lifecycle.events.OnStart; 29 import com.android.settings.core.lifecycle.events.OnStop; 30 import com.android.settings.widget.VideoPreference; 31 32 public abstract class GesturePreferenceController extends PreferenceController 33 implements Preference.OnPreferenceChangeListener, LifecycleObserver, OnStart, OnStop { 34 35 private VideoPreference mVideoPreference; 36 GesturePreferenceController(Context context, Lifecycle lifecycle)37 public GesturePreferenceController(Context context, Lifecycle lifecycle) { 38 super(context); 39 if (lifecycle != null) { 40 lifecycle.addObserver(this); 41 } 42 } 43 44 @Override displayPreference(PreferenceScreen screen)45 public void displayPreference(PreferenceScreen screen) { 46 super.displayPreference(screen); 47 if (isAvailable()) { 48 mVideoPreference = (VideoPreference) screen.findPreference(getVideoPrefKey()); 49 } 50 } 51 52 @Override updateState(Preference preference)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 } 65 } 66 67 @Override onStop()68 public void onStop() { 69 if (mVideoPreference != null) { 70 mVideoPreference.onViewInvisible(); 71 } 72 } 73 74 @Override onStart()75 public void onStart() { 76 if (mVideoPreference != null) { 77 mVideoPreference.onViewVisible(); 78 } 79 } 80 getVideoPrefKey()81 protected abstract String getVideoPrefKey(); 82 isSwitchPrefEnabled()83 protected abstract boolean isSwitchPrefEnabled(); 84 } 85