1 /* 2 * Copyright (C) 2020 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.accessories; 18 19 import android.content.ContentProviderClient; 20 import android.content.Context; 21 import android.net.Uri; 22 23 import com.android.tv.twopanelsettings.slices.SlicesConstants; 24 25 /** Util class for {@ConnectedDevicesSliceProvider} */ 26 public final class ConnectedDevicesSliceUtils { 27 28 static final String AUTHORITY = "com.android.tv.settings.accessories.sliceprovider"; 29 static final String GENERAL_PATH = "general"; 30 static final String BLUETOOTH_DEVICE_PATH = "device"; 31 static final String EXTRAS_DIRECTION = "extras_direction"; 32 static final String EXTRAS_SLICE_URI = "extras_slice_uri"; 33 static final String DIRECTION_BACK = "direction_back"; 34 public static final Uri GENERAL_SLICE_URI = 35 Uri.parse("content://" + AUTHORITY + "/" + GENERAL_PATH); 36 static final Uri BLUETOOTH_DEVICE_SLICE_URI = 37 Uri.parse("content://" + AUTHORITY + "/" + BLUETOOTH_DEVICE_PATH); 38 getDeviceAddr(Uri uri)39 static String getDeviceAddr(Uri uri) { 40 if (uri.getPathSegments().size() >= 2) { 41 return uri.getPathSegments().get(1).split(" ")[0]; 42 } 43 return null; 44 } 45 isGeneralPath(Uri uri)46 static boolean isGeneralPath(Uri uri) { 47 return GENERAL_PATH.equals(getFirstSegment(uri)); 48 } 49 isBluetoothDevicePath(Uri uri)50 static boolean isBluetoothDevicePath(Uri uri) { 51 return BLUETOOTH_DEVICE_PATH.equals(getFirstSegment(uri)); 52 } 53 54 /** Check if slice provider exists. */ isSliceProviderValid(Context context, String uri)55 static boolean isSliceProviderValid(Context context, String uri) { 56 if (uri == null) { 57 return false; 58 } 59 ContentProviderClient client = 60 context.getContentResolver().acquireContentProviderClient(Uri.parse(uri)); 61 if (client != null) { 62 client.close(); 63 return true; 64 } else { 65 return false; 66 } 67 } 68 getDeviceUri(String deviceAddr, String aliasName)69 static Uri getDeviceUri(String deviceAddr, String aliasName) { 70 return Uri.withAppendedPath( 71 BLUETOOTH_DEVICE_SLICE_URI, deviceAddr + " " + aliasName); 72 } 73 getFirstSegment(Uri uri)74 private static String getFirstSegment(Uri uri) { 75 if (uri.getPathSegments().size() > 0) { 76 return uri.getPathSegments().get(0); 77 } 78 return null; 79 } 80 notifyToGoBack(Context context, Uri uri)81 static void notifyToGoBack(Context context, Uri uri) { 82 Uri appendedUri = uri 83 .buildUpon().path("/" + SlicesConstants.PATH_STATUS) 84 .appendQueryParameter(SlicesConstants.PARAMETER_URI, uri.toString()) 85 .appendQueryParameter(SlicesConstants.PARAMETER_DIRECTION, SlicesConstants.BACKWARD) 86 .build(); 87 context.getContentResolver().notifyChange(appendedUri, null); 88 } 89 ConnectedDevicesSliceUtils()90 private ConnectedDevicesSliceUtils() { 91 // do not allow instantiation 92 } 93 } 94