• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.library.util;
18 
19 import android.app.slice.SliceManager;
20 import android.content.ContentProviderClient;
21 import android.content.Context;
22 import android.net.Uri;
23 import android.util.Log;
24 
25 import java.util.Collection;
26 
27 /** Utility class for slice **/
28 public final class SliceUtils {
29     private static final String TAG = "SliceUtils";
30 
31     public static final String PATH_SLICE_FRAGMENT =
32             "com.android.tv.twopanelsettings.slices.SliceFragment";
33 
34     /**
35      * Check if slice provider exists.
36      */
isSliceProviderValid(Context context, String uri)37     public static boolean isSliceProviderValid(Context context, String uri) {
38         if (uri == null) {
39             return false;
40         }
41         ContentProviderClient client =
42                 context.getContentResolver().acquireContentProviderClient(Uri.parse(uri));
43         if (client != null) {
44             client.close();
45             return true;
46         } else {
47             return false;
48         }
49     }
50 
51     /**
52      * Checks if the slice is enabled
53      *
54      * @param context                  Current context of the app
55      * @param uri                      Settings slice uri
56      * @param topLevelSettingsSliceUri Top level settings slice uri, if null, use provided uri to
57      *                                 deduce top level settings slice uri.
58      * @return returns true if slice is enabled, false otherwise
59      */
isSettingsSliceEnabled(Context context, String uri, String topLevelSettingsSliceUri)60     public static boolean isSettingsSliceEnabled(Context context, String uri,
61             String topLevelSettingsSliceUri) {
62         if (uri == null) {
63             return false;
64         }
65         final SliceManager sliceManager = context.getSystemService(SliceManager.class);
66         if (sliceManager == null) {
67             return false;
68         }
69         try {
70             Uri topLevelSettingsSlice = topLevelSettingsSliceUri == null
71                     ? Uri.parse(uri).buildUpon().path("/").build()
72                     : Uri.parse(ResourcesUtil.getString(context, topLevelSettingsSliceUri));
73             final Collection<Uri> enabledSlicesUri = sliceManager
74                     .getSliceDescendants(topLevelSettingsSlice);
75             if (enabledSlicesUri != null) {
76                 for (final Uri sliceUri : enabledSlicesUri) {
77                     Log.i(TAG, "Enabled slice: " + sliceUri);
78                     if (sliceUri.toString().equals(uri)) {
79                         return true;
80                     }
81                 }
82             }
83         } catch (NullPointerException nullPointerException) {
84             Log.e(TAG, "Unable to get slice descendants", nullPointerException);
85         }
86         return false;
87     }
88 }
89