1 /*
2  * Copyright 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.settings.development.graphicsdriver;
18 
19 import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableForAllAppsPreferenceController.UPDATABLE_DRIVER_DEFAULT;
20 import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableForAllAppsPreferenceController.UPDATABLE_DRIVER_OFF;
21 
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.os.Handler;
25 import android.os.Looper;
26 import android.provider.Settings;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.core.BasePreferenceController;
33 import com.android.settingslib.core.lifecycle.LifecycleObserver;
34 import com.android.settingslib.core.lifecycle.events.OnStart;
35 import com.android.settingslib.core.lifecycle.events.OnStop;
36 import com.android.settingslib.widget.TopIntroPreference;
37 
38 /**
39  * Controller of top info preference for Graphics Driver Preferences dashboard.
40  */
41 public class GraphicsDriverTopIntroPreferenceController extends BasePreferenceController
42         implements GraphicsDriverContentObserver.OnGraphicsDriverContentChangedListener,
43         LifecycleObserver, OnStart, OnStop {
44 
45     private final ContentResolver mContentResolver;
46     @VisibleForTesting
47     GraphicsDriverContentObserver mGraphicsDriverContentObserver;
48 
49     private TopIntroPreference mPreference;
50 
GraphicsDriverTopIntroPreferenceController(Context context, String key)51     public GraphicsDriverTopIntroPreferenceController(Context context, String key) {
52         super(context, key);
53         mContentResolver = context.getContentResolver();
54         mGraphicsDriverContentObserver =
55                 new GraphicsDriverContentObserver(new Handler(Looper.getMainLooper()), this);
56     }
57 
58     @Override
getAvailabilityStatus()59     public int getAvailabilityStatus() {
60         return Settings.Global.getInt(
61                 mContentResolver, Settings.Global.UPDATABLE_DRIVER_ALL_APPS,
62                 UPDATABLE_DRIVER_DEFAULT)
63                 == UPDATABLE_DRIVER_OFF
64                 ? AVAILABLE_UNSEARCHABLE
65                 : CONDITIONALLY_UNAVAILABLE;
66     }
67 
68     @Override
displayPreference(PreferenceScreen screen)69     public void displayPreference(PreferenceScreen screen) {
70         super.displayPreference(screen);
71         mPreference = screen.findPreference(getPreferenceKey());
72     }
73 
74     @Override
onStart()75     public void onStart() {
76         mGraphicsDriverContentObserver.register(mContentResolver);
77     }
78 
79     @Override
onStop()80     public void onStop() {
81         mGraphicsDriverContentObserver.unregister(mContentResolver);
82     }
83 
84     @Override
updateState(Preference preference)85     public void updateState(Preference preference) {
86         final TopIntroPreference topIntroPref = (TopIntroPreference) preference;
87         topIntroPref.setVisible(isAvailable());
88     }
89 
90     @Override
onGraphicsDriverContentChanged()91     public void onGraphicsDriverContentChanged() {
92         updateState(mPreference);
93     }
94 }
95