1 /*
2  * Copyright (C) 2019 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.car.settings.applications.assist;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.database.ContentObserver;
23 import android.net.Uri;
24 import android.os.Handler;
25 import android.os.Looper;
26 import android.os.UserHandle;
27 import android.provider.Settings;
28 
29 import androidx.preference.TwoStatePreference;
30 
31 import com.android.car.settings.common.FragmentController;
32 import com.android.car.settings.common.PreferenceController;
33 import com.android.internal.app.AssistUtils;
34 
35 import java.util.List;
36 
37 /** Common logic for preference controllers that configure the assistant's behavior. */
38 public abstract class AssistConfigBasePreferenceController extends
39         PreferenceController<TwoStatePreference> {
40 
41     private final SettingObserver mSettingObserver;
42     private final AssistUtils mAssistUtils;
43 
AssistConfigBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44     public AssistConfigBasePreferenceController(Context context, String preferenceKey,
45             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
46         super(context, preferenceKey, fragmentController, uxRestrictions);
47         mAssistUtils = new AssistUtils(context);
48         mSettingObserver = new SettingObserver(getSettingUris(), this::refreshUi);
49     }
50 
51     @Override
getPreferenceType()52     protected Class<TwoStatePreference> getPreferenceType() {
53         return TwoStatePreference.class;
54     }
55 
56     @Override
getAvailabilityStatus()57     protected int getAvailabilityStatus() {
58         return mAssistUtils.getAssistComponentForUser(
59                 UserHandle.myUserId()) != null ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
60     }
61 
62     @Override
onStartInternal()63     protected void onStartInternal() {
64         mSettingObserver.register(getContext().getContentResolver(), true);
65     }
66 
67     @Override
onStopInternal()68     protected void onStopInternal() {
69         mSettingObserver.register(getContext().getContentResolver(), false);
70     }
71 
72     /** Gets the Setting Uris that should be observed */
getSettingUris()73     protected abstract List<Uri> getSettingUris();
74 
75     /**
76      * Creates an observer that listens for changes to {@link Settings.Secure#ASSISTANT} as well as
77      * any other URI defined by {@link #getSettingUris()}.
78      */
79     private static class SettingObserver extends ContentObserver {
80 
81         private static final Uri ASSIST_URI = Settings.Secure.getUriFor(Settings.Secure.ASSISTANT);
82         private final List<Uri> mUriList;
83         private final Runnable mSettingChangeListener;
84 
SettingObserver(List<Uri> uriList, Runnable settingChangeListener)85         SettingObserver(List<Uri> uriList, Runnable settingChangeListener) {
86             super(new Handler(Looper.getMainLooper()));
87             mUriList = uriList;
88             mSettingChangeListener = settingChangeListener;
89         }
90 
91         /** Registers or unregisters this observer to the given content resolver. */
register(ContentResolver cr, boolean register)92         void register(ContentResolver cr, boolean register) {
93             if (register) {
94                 cr.registerContentObserver(ASSIST_URI, /* notifyForDescendants= */ false,
95                         /* observer= */ this);
96                 if (mUriList != null) {
97                     for (Uri uri : mUriList) {
98                         cr.registerContentObserver(uri, /* notifyForDescendants= */ false,
99                                 /* observer= */ this);
100                     }
101                 }
102             } else {
103                 cr.unregisterContentObserver(this);
104             }
105         }
106 
107         @Override
onChange(boolean selfChange, Uri uri)108         public void onChange(boolean selfChange, Uri uri) {
109             super.onChange(selfChange, uri);
110 
111             if (shouldUpdatePreference(uri)) {
112                 mSettingChangeListener.run();
113             }
114         }
115 
shouldUpdatePreference(Uri uri)116         private boolean shouldUpdatePreference(Uri uri) {
117             return ASSIST_URI.equals(uri) || (mUriList != null && mUriList.contains(uri));
118         }
119     }
120 }
121