1 /* 2 * Copyright (C) 2021 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.tv.settings.library.util; 18 19 import android.content.Context; 20 21 import com.android.tv.settings.library.PreferenceCompat; 22 import com.android.tv.settings.library.UIUpdateCallback; 23 import com.android.tv.settings.library.data.PreferenceCompatManager; 24 25 26 /** 27 * A controller that manages event for preference. 28 */ 29 public abstract class AbstractPreferenceController { 30 31 private static final String TAG = "AbstractPrefController"; 32 33 protected final Context mContext; 34 protected final UIUpdateCallback mUIUpdateCallback; 35 protected final int mStateIdentifier; 36 protected final PreferenceCompatManager mPreferenceCompatManager; 37 protected final String[] mKey; 38 protected final PreferenceCompat mPreferenceCompat; 39 AbstractPreferenceController(Context context, UIUpdateCallback callback, int stateIdentifier, PreferenceCompatManager preferenceCompatManager)40 public AbstractPreferenceController(Context context, UIUpdateCallback callback, 41 int stateIdentifier, PreferenceCompatManager preferenceCompatManager) { 42 mUIUpdateCallback = callback; 43 mContext = context; 44 mStateIdentifier = stateIdentifier; 45 mPreferenceCompatManager = preferenceCompatManager; 46 mKey = getPreferenceKey(); 47 mPreferenceCompat = mPreferenceCompatManager.getOrCreatePrefCompat(mKey); 48 } 49 AbstractPreferenceController(Context context, UIUpdateCallback callback, int stateIdentifier, PreferenceCompatManager preferenceCompatManager, String[] key)50 public AbstractPreferenceController(Context context, UIUpdateCallback callback, 51 int stateIdentifier, PreferenceCompatManager preferenceCompatManager, String[] key) { 52 mUIUpdateCallback = callback; 53 mContext = context; 54 mStateIdentifier = stateIdentifier; 55 mPreferenceCompatManager = preferenceCompatManager; 56 mKey = key; 57 mPreferenceCompat = mPreferenceCompatManager.getOrCreatePrefCompat(mKey); 58 } 59 60 /** 61 * @return whether the setting controlled by the controller is available. 62 */ isAvailable()63 public abstract boolean isAvailable(); 64 65 /** 66 * Initialize preference compat. 67 */ init()68 protected abstract void init(); 69 70 /** 71 * Initialize preference compat and notify to update UI. 72 */ initAndNotify()73 public void initAndNotify() { 74 init(); 75 notifyChange(); 76 } 77 78 /** 79 * Updates the current status of preference compat. 80 */ updateAndNotify()81 public void updateAndNotify() { 82 update(); 83 notifyChange(); 84 } 85 notifyChange()86 public void notifyChange() { 87 if (mPreferenceCompat != null) { 88 mUIUpdateCallback.notifyUpdate(mStateIdentifier, mPreferenceCompat); 89 } 90 } 91 92 /** 93 * Update preference compat. 94 */ update()95 protected abstract void update(); 96 97 98 /** 99 * Handle preference click. 100 * 101 * @param status status of new state 102 * @return whether the click is handled 103 */ handlePreferenceTreeClick(boolean status)104 public boolean handlePreferenceTreeClick(boolean status) { 105 return false; 106 } 107 108 /** 109 * Handle preference change. 110 * 111 * @param newValue new value of state 112 * @return whether the click is handled 113 */ handlePreferenceChange(Object newValue)114 public boolean handlePreferenceChange(Object newValue) { 115 return false; 116 } 117 getPrefCompat()118 public PreferenceCompat getPrefCompat() { 119 return mPreferenceCompat; 120 } 121 getPreferenceKey()122 public String[] getPreferenceKey() { 123 return mKey; 124 } 125 } 126