1 /* 2 * Copyright (C) 2022 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.net.Uri; 21 22 import androidx.lifecycle.Lifecycle; 23 import androidx.lifecycle.LifecycleObserver; 24 import androidx.lifecycle.OnLifecycleEvent; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.settings.R; 29 import com.android.settings.core.BasePreferenceController; 30 import com.android.settingslib.widget.IllustrationPreference; 31 32 /** Configures the behaviour of long press power illustration. */ 33 public class LongPressPowerIllustrationPreferenceController extends BasePreferenceController 34 implements PowerMenuSettingsUtils.SettingsStateCallback, LifecycleObserver { 35 36 private IllustrationPreference mIllustrationPreference; 37 private final PowerMenuSettingsUtils mUtils; 38 LongPressPowerIllustrationPreferenceController(Context context, String key)39 public LongPressPowerIllustrationPreferenceController(Context context, String key) { 40 super(context, key); 41 mUtils = new PowerMenuSettingsUtils(context); 42 } 43 44 @Override getAvailabilityStatus()45 public int getAvailabilityStatus() { 46 return AVAILABLE; 47 } 48 49 @Override displayPreference(PreferenceScreen screen)50 public void displayPreference(PreferenceScreen screen) { 51 super.displayPreference(screen); 52 mIllustrationPreference = screen.findPreference(getPreferenceKey()); 53 } 54 55 @Override updateState(Preference preference)56 public void updateState(Preference preference) { 57 super.updateState(preference); 58 59 ((IllustrationPreference) preference) 60 .setLottieAnimationResId( 61 PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext) 62 ? R.raw.lottie_long_press_power_for_assistant 63 : R.raw.lottie_long_press_power_for_power_menu); 64 } 65 66 @Override onChange(Uri uri)67 public void onChange(Uri uri) { 68 if (mIllustrationPreference != null) { 69 updateState(mIllustrationPreference); 70 } 71 } 72 73 /** @OnLifecycleEvent(Lifecycle.Event.ON_START) */ 74 @OnLifecycleEvent(Lifecycle.Event.ON_START) onStart()75 public void onStart() { 76 mUtils.registerObserver(this); 77 } 78 79 /** @OnLifecycleEvent(Lifecycle.Event.ON_STOP) */ 80 @OnLifecycleEvent(Lifecycle.Event.ON_STOP) onStop()81 public void onStop() { 82 mUtils.unregisterObserver(); 83 } 84 } 85