1 /* 2 * Copyright (C) 2017 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.applications.assist; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.net.Uri; 22 import android.os.UserHandle; 23 import android.provider.Settings; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceScreen; 28 import androidx.preference.TwoStatePreference; 29 30 import com.android.internal.app.AssistUtils; 31 import com.android.settings.core.PreferenceControllerMixin; 32 import com.android.settingslib.core.AbstractPreferenceController; 33 import com.android.settingslib.core.lifecycle.Lifecycle; 34 import com.android.settingslib.core.lifecycle.LifecycleObserver; 35 import com.android.settingslib.core.lifecycle.events.OnPause; 36 import com.android.settingslib.core.lifecycle.events.OnResume; 37 38 import java.util.Arrays; 39 import java.util.List; 40 41 public class AssistFlashScreenPreferenceController extends AbstractPreferenceController 42 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener, 43 LifecycleObserver, OnResume, OnPause { 44 45 private static final String KEY_FLASH = "flash"; 46 47 private final AssistUtils mAssistUtils; 48 private final SettingObserver mSettingObserver; 49 private PreferenceScreen mScreen; 50 private Preference mPreference; 51 AssistFlashScreenPreferenceController(Context context, Lifecycle lifecycle)52 public AssistFlashScreenPreferenceController(Context context, Lifecycle lifecycle) { 53 super(context); 54 mAssistUtils = new AssistUtils(context); 55 mSettingObserver = new SettingObserver(); 56 57 if (lifecycle != null) { 58 lifecycle.addObserver(this); 59 } 60 } 61 62 @Override isAvailable()63 public boolean isAvailable() { 64 return getCurrentAssist() != null && allowDisablingAssistDisclosure(); 65 } 66 67 @Override getPreferenceKey()68 public String getPreferenceKey() { 69 return KEY_FLASH; 70 } 71 72 @Override displayPreference(PreferenceScreen screen)73 public void displayPreference(PreferenceScreen screen) { 74 mScreen = screen; 75 mPreference = screen.findPreference(getPreferenceKey()); 76 super.displayPreference(screen); 77 } 78 79 @Override onResume()80 public void onResume() { 81 mSettingObserver.register(mContext.getContentResolver(), true); 82 updatePreference(); 83 } 84 85 @Override updateState(Preference preference)86 public void updateState(Preference preference) { 87 updatePreference(); 88 } 89 90 @Override onPause()91 public void onPause() { 92 mSettingObserver.register(mContext.getContentResolver(), false); 93 } 94 95 @Override onPreferenceChange(Preference preference, Object newValue)96 public boolean onPreferenceChange(Preference preference, Object newValue) { 97 Settings.Secure.putInt(mContext.getContentResolver(), 98 Settings.Secure.ASSIST_DISCLOSURE_ENABLED, 99 (boolean) newValue ? 1 : 0); 100 return true; 101 } 102 updatePreference()103 private void updatePreference() { 104 if (mPreference == null || !(mPreference instanceof TwoStatePreference)) { 105 return; 106 } 107 if (isAvailable()) { 108 if (mScreen.findPreference(getPreferenceKey()) == null) { 109 // add it if it's not on scree 110 mScreen.addPreference(mPreference); 111 } 112 } else { 113 mScreen.removePreference(mPreference); 114 } 115 ComponentName assistant = getCurrentAssist(); 116 117 boolean isContextChecked = AssistContextPreferenceController.isChecked(mContext); 118 119 mPreference.setEnabled(isContextChecked && isPreInstalledAssistant(assistant)); 120 ((TwoStatePreference) mPreference).setChecked(willShowFlash(assistant)); 121 } 122 123 @VisibleForTesting willShowFlash(ComponentName assistant)124 boolean willShowFlash(ComponentName assistant) { 125 return AssistUtils.shouldDisclose(mContext, assistant); 126 } 127 128 @VisibleForTesting isPreInstalledAssistant(ComponentName assistant)129 boolean isPreInstalledAssistant(ComponentName assistant) { 130 return AssistUtils.isPreinstalledAssistant(mContext, assistant); 131 } 132 133 @VisibleForTesting allowDisablingAssistDisclosure()134 boolean allowDisablingAssistDisclosure() { 135 return AssistUtils.allowDisablingAssistDisclosure(mContext); 136 } 137 getCurrentAssist()138 private ComponentName getCurrentAssist() { 139 return mAssistUtils.getAssistComponentForUser(UserHandle.myUserId()); 140 } 141 142 class SettingObserver extends AssistSettingObserver { 143 144 private final Uri URI = 145 Settings.Secure.getUriFor(Settings.Secure.ASSIST_DISCLOSURE_ENABLED); 146 private final Uri CONTEXT_URI = 147 Settings.Secure.getUriFor(Settings.Secure.ASSIST_STRUCTURE_ENABLED); 148 149 @Override getSettingUris()150 protected List<Uri> getSettingUris() { 151 return Arrays.asList(URI, CONTEXT_URI); 152 } 153 154 @Override onSettingChange()155 public void onSettingChange() { 156 updatePreference(); 157 } 158 } 159 160 161 } 162