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.bluetooth.BluetoothDevice; 20 import android.content.ContentProviderClient; 21 import android.content.Context; 22 import android.net.Uri; 23 import android.provider.Settings; 24 25 import com.android.tv.twopanelsettings.slices.SlicesConstants; 26 27 /** Util class for {@ConnectedDevicesSliceProvider} */ 28 public final class ConnectedDevicesSliceUtils { 29 30 static final String AUTHORITY = "com.android.tv.settings.accessories.sliceprovider"; 31 static final String GENERAL_PATH = "general"; 32 static final String BLUETOOTH_DEVICE_PATH = "device"; 33 static final String EXTRAS_DIRECTION = "extras_direction"; 34 static final String EXTRAS_SLICE_URI = "extras_slice_uri"; 35 static final String FIND_MY_REMOTE_PATH = "find_my_remote"; 36 static final String DIRECTION_BACK = "direction_back"; 37 public static final Uri GENERAL_SLICE_URI = 38 Uri.parse("content://" + AUTHORITY + "/" + GENERAL_PATH); 39 static final Uri BLUETOOTH_DEVICE_SLICE_URI = 40 Uri.parse("content://" + AUTHORITY + "/" + BLUETOOTH_DEVICE_PATH); 41 static final Uri FIND_MY_REMOTE_SLICE_URI = 42 Uri.parse("content://" + AUTHORITY + "/" + FIND_MY_REMOTE_PATH); 43 44 /** 45 * The {@link Settings.Global} integer setting name. 46 * 47 * <p>The settings tells whether the physical button integration for Find My Remote feature 48 * is enabled. Default value: 1. 49 */ 50 static final String FIND_MY_REMOTE_PHYSICAL_BUTTON_ENABLED_SETTING = 51 "find_my_remote_physical_button_enabled"; 52 getDeviceAddr(Uri uri)53 static String getDeviceAddr(Uri uri) { 54 if (uri.getPathSegments().size() >= 2) { 55 return uri.getPathSegments().get(1).split(" ")[0]; 56 } 57 return null; 58 } 59 isGeneralPath(Uri uri)60 static boolean isGeneralPath(Uri uri) { 61 return GENERAL_PATH.equals(getFirstSegment(uri)); 62 } 63 isBluetoothDevicePath(Uri uri)64 static boolean isBluetoothDevicePath(Uri uri) { 65 return BLUETOOTH_DEVICE_PATH.equals(getFirstSegment(uri)); 66 } 67 isFindMyRemotePath(Uri uri)68 static boolean isFindMyRemotePath(Uri uri) { 69 return FIND_MY_REMOTE_PATH.equals(getFirstSegment(uri)); 70 } 71 72 /** Check if slice provider exists. */ isSliceProviderValid(Context context, String uri)73 static boolean isSliceProviderValid(Context context, String uri) { 74 if (uri == null) { 75 return false; 76 } 77 ContentProviderClient client = 78 context.getContentResolver().acquireContentProviderClient(Uri.parse(uri)); 79 if (client != null) { 80 client.close(); 81 return true; 82 } else { 83 return false; 84 } 85 } 86 getDeviceUri(String deviceAddr, String aliasName)87 static Uri getDeviceUri(String deviceAddr, String aliasName) { 88 return Uri.withAppendedPath( 89 BLUETOOTH_DEVICE_SLICE_URI, deviceAddr + " " + aliasName); 90 } 91 getFirstSegment(Uri uri)92 private static String getFirstSegment(Uri uri) { 93 if (uri.getPathSegments().size() > 0) { 94 return uri.getPathSegments().get(0); 95 } 96 return null; 97 } 98 notifyToGoBack(Context context, Uri uri)99 static void notifyToGoBack(Context context, Uri uri) { 100 Uri appendedUri = uri 101 .buildUpon().path("/" + SlicesConstants.PATH_STATUS) 102 .appendQueryParameter(SlicesConstants.PARAMETER_URI, uri.toString()) 103 .appendQueryParameter(SlicesConstants.PARAMETER_DIRECTION, SlicesConstants.BACKWARD) 104 .build(); 105 context.getContentResolver().notifyChange(appendedUri, null); 106 } 107 notifyDeviceChanged(Context context, BluetoothDevice device)108 static void notifyDeviceChanged(Context context, BluetoothDevice device) { 109 if (device != null) { 110 context.getContentResolver().notifyChange( 111 getDeviceUri(device.getAddress(), device.getAlias()), null); 112 } 113 } 114 ConnectedDevicesSliceUtils()115 private ConnectedDevicesSliceUtils() { 116 // do not allow instantiation 117 } 118 isFindMyRemoteButtonEnabled(Context context)119 public static boolean isFindMyRemoteButtonEnabled(Context context) { 120 return Settings.Global.getInt(context.getContentResolver(), 121 FIND_MY_REMOTE_PHYSICAL_BUTTON_ENABLED_SETTING, 1) != 0; 122 } 123 setFindMyRemoteButtonEnabled(Context context, boolean enabled)124 static void setFindMyRemoteButtonEnabled(Context context, boolean enabled) { 125 Settings.Global.putInt(context.getContentResolver(), 126 FIND_MY_REMOTE_PHYSICAL_BUTTON_ENABLED_SETTING, 127 enabled ? 1 : 0); 128 } 129 } 130