1 /*
2  * Copyright (C) 2020 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.tv.settings.widget;
18 
19 import android.content.Context;
20 import android.database.ContentObserver;
21 import android.database.Cursor;
22 import android.net.Uri;
23 
24 import androidx.lifecycle.MutableLiveData;
25 
26 import com.android.settingslib.utils.ThreadUtils;
27 
28 public class PreferenceContentProviderLiveData extends MutableLiveData<Boolean> {
29     private static final String AUTHORITY = "com.google.android.apps.tv.settings.contentprovider";
30     private static final String PATH_PREFERENCE = "preference";
31     private static final String PREFERENCE_DATA_PROVIDER_PATH =
32             "content://" + AUTHORITY + "/" + PATH_PREFERENCE;
33 
34     private final ContentObserver mContentObserver = new ContentObserver(null) {
35         @Override
36         public void onChange(boolean selfChange) {
37             postGetVisibilityStatus();
38         }
39     };
40 
41     private final Context mContext;
42     private final Uri mUri;
43     private final String mKey;
44     private boolean mRegistered = false;
45     private static final String VISIBLE = "visible";
46 
PreferenceContentProviderLiveData(String key, Context context)47     public PreferenceContentProviderLiveData(String key, Context context) {
48         mKey = key;
49         mUri = Uri.parse(PREFERENCE_DATA_PROVIDER_PATH + "/" + key);
50         mContext = context;
51     }
52 
53     @Override
onActive()54     protected void onActive() {
55         postGetVisibilityStatus();
56         if (!mRegistered) {
57             mContext.getContentResolver().registerContentObserver(mUri, false,
58                     mContentObserver);
59             mRegistered = true;
60         }
61     }
62 
63     @Override
onInactive()64     protected void onInactive() {
65         mContext.getContentResolver().unregisterContentObserver(mContentObserver);
66         mRegistered = false;
67     }
68 
getKey()69     public String getKey() {
70         return mKey;
71     }
72 
postGetVisibilityStatus()73     private void postGetVisibilityStatus() {
74         ThreadUtils.postOnBackgroundThread(() -> {
75             postValue(Boolean.valueOf(getVisibilityStatus()));
76         });
77     }
78 
getVisibilityStatus()79     private boolean getVisibilityStatus() {
80         try {
81             Cursor cursor = mContext.getContentResolver().query(mUri, null, null, null);
82             if (cursor != null && cursor.getCount() != 0) {
83                 cursor.moveToFirst();
84                 int visible = cursor.getInt(cursor.getColumnIndex(VISIBLE));
85                 return visible == 1;
86             }
87         } catch (IllegalArgumentException | NullPointerException e) {
88             return true;
89         }
90         return true;
91     }
92 }
93