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.settings.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 com.android.tv.settings.R;
26 
27 import java.util.Collection;
28 
29 /** Utility class for slice **/
30 public final class SliceUtils {
31     private static final String TAG = "SliceUtils";
32 
33     public static final String PATH_SLICE_FRAGMENT =
34             "com.android.tv.twopanelsettings.slices.SliceFragment";
35     /**
36      * Check if slice provider exists.
37      */
isSliceProviderValid(Context context, String uri)38     public static boolean isSliceProviderValid(Context context, String uri) {
39         if (uri == null) {
40             return false;
41         }
42         ContentProviderClient client =
43                 context.getContentResolver().acquireContentProviderClient(Uri.parse(uri));
44         if (client != null) {
45             client.close();
46             return true;
47         } else {
48             return false;
49         }
50     }
51 
52     /**
53      * Checks if the slice is enabled
54      * @param context Current context of the app
55      * @param uri Settings slice uri
56      * @return returns true if slice is enabled, false otherwise
57      */
isSettingsSliceEnabled(Context context, String uri)58     public static boolean isSettingsSliceEnabled(Context context, String uri) {
59         if (uri == null) {
60             return false;
61         }
62         final SliceManager sliceManager = context.getSystemService(SliceManager.class);
63         if (sliceManager == null) {
64             return false;
65         }
66         try {
67             final Collection<Uri> enabledSlicesUri = sliceManager.getSliceDescendants(
68                     Uri.parse(context.getString(R.string.top_level_settings_slice_uri)));
69             if (enabledSlicesUri != null) {
70                 for (final Uri sliceUri : enabledSlicesUri) {
71                     Log.i(TAG, "Enabled slice: " + sliceUri);
72                     if (sliceUri.toString().equals(uri)) {
73                         return true;
74                     }
75                 }
76             }
77         } catch (NullPointerException nullPointerException) {
78             Log.e(TAG, "Unable to get slice descendants", nullPointerException);
79         }
80         return false;
81     }
82 }
83