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.util; 18 19 import android.app.slice.SliceManager; 20 import android.content.Context; 21 import android.content.pm.ProviderInfo; 22 import android.net.Uri; 23 import android.text.TextUtils; 24 import android.util.Log; 25 26 import java.util.Collection; 27 28 import kotlin.coroutines.Continuation; 29 30 /** Utility class for slice **/ 31 public final class SliceUtils { 32 private static final String TAG = "SliceUtils"; 33 34 public static final String PATH_SLICE_FRAGMENT = 35 "com.android.tv.twopanelsettings.slices.SliceFragment"; 36 37 /** 38 * Check if slice provider exists. 39 */ isSliceProviderValid(Context context, String stringUri)40 public static boolean isSliceProviderValid(Context context, String stringUri) { 41 if (TextUtils.isEmpty(stringUri)) { 42 return false; 43 } 44 Uri uri = Uri.parse(stringUri); 45 ProviderInfo providerInfo = 46 context.getPackageManager() 47 .resolveContentProvider(uri.getAuthority(), /* flags= */ 0); 48 if (providerInfo == null) { 49 Log.i(TAG, "Slice Provider not found for: " + stringUri); 50 return false; 51 } 52 return true; 53 } 54 55 /** 56 * Checks if the slice is enabled 57 * 58 * @param context Current context of the app 59 * @param uri Settings slice uri 60 * @param topLevelSettingsSliceUri Top level settings slice uri, if null, use provided uri to 61 * deduce top level settings slice uri. 62 * @return returns true if slice is enabled, false otherwise 63 * @deprecated use {@link SliceUtilsKt#isSettingsSliceEnabled} instead. 64 */ 65 @Deprecated isSettingsSliceEnabled(Context context, String uri, String topLevelSettingsSliceUri)66 public static boolean isSettingsSliceEnabled(Context context, String uri, 67 String topLevelSettingsSliceUri) { 68 if (uri == null) { 69 return false; 70 } 71 final SliceManager sliceManager = context.getSystemService(SliceManager.class); 72 if (sliceManager == null) { 73 return false; 74 } 75 try { 76 Uri topLevelSettingsSlice = topLevelSettingsSliceUri == null 77 ? Uri.parse(uri).buildUpon().path("/").build() 78 : Uri.parse(ResourcesUtil.getString(context, topLevelSettingsSliceUri)); 79 final Collection<Uri> enabledSlicesUri = sliceManager 80 .getSliceDescendants(topLevelSettingsSlice); 81 if (enabledSlicesUri != null) { 82 for (final Uri sliceUri : enabledSlicesUri) { 83 Log.i(TAG, "Enabled slice: " + sliceUri); 84 if (sliceUri.toString().equals(uri)) { 85 return true; 86 } 87 } 88 } 89 } catch (NullPointerException nullPointerException) { 90 Log.e(TAG, "Unable to get slice descendants", nullPointerException); 91 } 92 return false; 93 } 94 } 95