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.content.Context; 20 import android.content.pm.ProviderInfo; 21 import android.net.Uri; 22 import android.text.TextUtils; 23 import android.util.Log; 24 25 import androidx.annotation.Nullable; 26 import androidx.preference.Preference; 27 28 import com.android.tv.settings.overlay.FlavorUtils; 29 import com.android.tv.twopanelsettings.slices.SlicePreference; 30 31 /** Utility class for slice **/ 32 public final class SliceUtils { 33 private static final String TAG = "SliceUtils"; 34 35 public static final String PATH_SLICE_FRAGMENT = 36 "com.android.tv.twopanelsettings.slices.SliceFragment"; 37 38 /** 39 * Check if slice provider exists. 40 */ isSliceProviderValid(Context context, String stringUri)41 public static boolean isSliceProviderValid(Context context, String stringUri) { 42 if (TextUtils.isEmpty(stringUri)) { 43 return false; 44 } 45 Uri uri = Uri.parse(stringUri); 46 ProviderInfo providerInfo = 47 context.getPackageManager() 48 .resolveContentProvider(uri.getAuthority(), /* flags= */ 0); 49 if (providerInfo == null) { 50 Log.i(TAG, "Slice Provider not found for: " + stringUri); 51 return false; 52 } 53 return true; 54 } 55 maybeUseSlice(@ullable Preference preference, @Nullable SlicePreference slicePreference)56 public static boolean maybeUseSlice(@Nullable Preference preference, 57 @Nullable SlicePreference slicePreference) { 58 boolean usingSlice = slicePreference != null 59 && FlavorUtils.isTwoPanel(slicePreference.getContext()) 60 && SliceUtilsKt.isSettingsSliceEnabledSync(slicePreference.getContext(), 61 slicePreference.getUri(), /* topLevelSettingsSliceUri = */ null); 62 if (slicePreference != null) { 63 slicePreference.setVisible(usingSlice); 64 } 65 66 if (preference != null) { 67 preference.setVisible(!usingSlice); 68 } 69 70 return usingSlice; 71 } 72 } 73