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