1 /* 2 * Copyright (C) 2024 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 package com.android.tv.settings.util 17 18 import android.app.slice.SliceManager 19 import android.content.Context 20 import android.net.Uri 21 import android.util.Log 22 import kotlinx.coroutines.Dispatchers 23 import kotlinx.coroutines.withContext 24 25 object SliceUtilsKt { 26 private const val TAG = "SliceUtilsKt" 27 /** 28 * Checks if the slice is available in the background if needed 29 * 30 * @param context Current context of the app 31 * @param uri Settings slice uri 32 * @param topLevelSettingsSliceUri Top level settings slice uri, if null, use provided uri to 33 * deduce top level settings slice uri. 34 * @return returns true if slice is enabled, false otherwise 35 */ 36 @JvmStatic isSettingsSliceEnablednull37 suspend fun isSettingsSliceEnabled(context: Context, uri: String?, 38 topLevelSettingsSliceUri: String?): Boolean { 39 if (uri == null) { 40 return false 41 } 42 val sliceManager = context.getSystemService(SliceManager::class.java) 43 ?: return false 44 return withContext(Dispatchers.IO) { 45 val topLevelSettingsSlice = if (topLevelSettingsSliceUri == null) { 46 Uri.parse(uri).buildUpon().path("/").build() 47 } else { 48 Uri.parse(ResourcesUtil.getString(context, topLevelSettingsSliceUri)) 49 } 50 val enabledSlicesUri = sliceManager.getSliceDescendants(topLevelSettingsSlice) 51 for (sliceUri in enabledSlicesUri) { 52 Log.i(TAG, "Enabled slice: $sliceUri") 53 if (sliceUri.toString() == uri) { 54 return@withContext true 55 } 56 } 57 return@withContext false 58 } 59 } 60 }