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