• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.tv.twopanelsettings.slices;
18 
19 import android.content.Context;
20 import android.net.Uri;
21 import android.util.ArrayMap;
22 import android.util.Log;
23 
24 import androidx.lifecycle.Observer;
25 
26 import com.android.tv.twopanelsettings.slices.PreferenceSliceLiveData.SliceLiveDataImpl;
27 import com.android.tv.twopanelsettings.slices.base.SliceManager;
28 import com.android.tv.twopanelsettings.slices.compat.Slice;
29 
30 
31 /**
32  * Ensure the SliceLiveData with same uri is created only once across the activity.
33  */
34 public class ContextSingleton {
35     private static final String TAG = "TvSettingsContext";
36     private static ContextSingleton sInstance;
37     private ArrayMap<Uri, SliceLiveDataImpl> mSliceMap;
38     private boolean mGivenFullSliceAccess;
39 
40     /**
41      * Get the instance.
42      */
getInstance()43     public static ContextSingleton getInstance() {
44         if (sInstance == null) {
45             sInstance = new ContextSingleton();
46         }
47         return sInstance;
48     }
49 
ContextSingleton()50     private ContextSingleton() {
51         mSliceMap = new ArrayMap<>();
52         mGivenFullSliceAccess = false;
53     }
54 
55     /**
56      * Get the corresponding SliceLiveData based on the uri.
57      */
getSliceLiveData(Context context, Uri uri)58     public SliceLiveDataImpl getSliceLiveData(Context context, Uri uri) {
59         if (!mSliceMap.containsKey(uri)) {
60             mSliceMap.put(uri, PreferenceSliceLiveData.fromUri(context, uri));
61         }
62 
63         return mSliceMap.get(uri);
64     }
65 
66     /**
67      * Register slice live data observer directly.
68      */
addSliceObserver(Context context, Uri uri, Observer<Slice> observer)69     public void addSliceObserver(Context context, Uri uri, Observer<Slice> observer) {
70         getSliceLiveData(context, uri).observeForever(observer);
71     }
72 
73     /**
74      * Unregister slice live data observer directly.
75      */
removeSliceObserver(Context context, Uri uri, Observer<Slice> observer)76     public void removeSliceObserver(Context context, Uri uri, Observer<Slice> observer) {
77         getSliceLiveData(context, uri).removeObserver(observer);
78     }
79 
80     /**
81      *  Grant full access to current package.
82      */
grantFullAccess(Context ctx, Uri uri)83     public void grantFullAccess(Context ctx, Uri uri) {
84         if (!mGivenFullSliceAccess) {
85             String currentPackageName = ctx.getApplicationContext().getPackageName();
86             // Uri cannot be null here as SliceManagerService calls notifyChange(uri, null) in
87             // grantPermissionFromUser.
88             SliceManager.from(ctx).grantPermissionFromUser(
89                     uri, currentPackageName, true);
90             mGivenFullSliceAccess = true;
91         }
92     }
93 
94     /**
95      *  Grant full access to specific package.
96      */
grantFullAccess(Context ctx, String uri, String packageName)97     public void grantFullAccess(Context ctx, String uri, String packageName) {
98         try {
99             SliceManager.from(ctx).grantPermissionFromUser(
100                     Uri.parse(uri), packageName, true);
101         } catch (Exception e) {
102             Log.e(TAG, "Cannot grant full access to " + packageName + " " + e);
103         }
104     }
105 }
106