• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.developeroptions.development.gamedriver;
18 
19 import static com.android.car.developeroptions.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_DEFAULT;
20 import static com.android.car.developeroptions.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_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.PreferenceScreen;
30 
31 import com.android.car.developeroptions.core.BasePreferenceController;
32 import com.android.settingslib.core.lifecycle.LifecycleObserver;
33 import com.android.settingslib.core.lifecycle.events.OnStart;
34 import com.android.settingslib.core.lifecycle.events.OnStop;
35 import com.android.settingslib.widget.FooterPreference;
36 
37 /**
38  * Controller of footer preference for Game Driver.
39  */
40 public class GameDriverFooterPreferenceController extends BasePreferenceController
41         implements GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver,
42                    OnStart, OnStop {
43 
44     private final ContentResolver mContentResolver;
45     @VisibleForTesting
46     GameDriverContentObserver mGameDriverContentObserver;
47 
48     private FooterPreference mPreference;
49 
GameDriverFooterPreferenceController(Context context, String key)50     public GameDriverFooterPreferenceController(Context context, String key) {
51         super(context, key);
52         mContentResolver = context.getContentResolver();
53         mGameDriverContentObserver =
54                 new GameDriverContentObserver(new Handler(Looper.getMainLooper()), this);
55     }
56 
57     @Override
getAvailabilityStatus()58     public int getAvailabilityStatus() {
59         return Settings.Global.getInt(
60                        mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT)
61                         == GAME_DRIVER_OFF
62                 ? AVAILABLE_UNSEARCHABLE
63                 : CONDITIONALLY_UNAVAILABLE;
64     }
65 
66     @Override
displayPreference(PreferenceScreen screen)67     public void displayPreference(PreferenceScreen screen) {
68         super.displayPreference(screen);
69         mPreference = screen.findPreference(getPreferenceKey());
70     }
71 
72     @Override
onStart()73     public void onStart() {
74         mGameDriverContentObserver.register(mContentResolver);
75     }
76 
77     @Override
onStop()78     public void onStop() {
79         mGameDriverContentObserver.unregister(mContentResolver);
80     }
81 
82     @Override
onGameDriverContentChanged()83     public void onGameDriverContentChanged() {
84         updateState(mPreference);
85     }
86 }
87