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