1 /* 2 * Copyright (C) 2014 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.bluetooth; 18 19 import android.text.TextUtils; 20 21 import java.util.regex.Matcher; 22 import java.util.regex.Pattern; 23 24 /** 25 * Helper functions and constants for reading the information passed via the Bluetooth 26 * name of the hub. 27 */ 28 public class BluetoothNameUtils { 29 /* 30 * matches string that 31 * - may or may not start with a one- or two-digit number followed by a space 32 * - a string surrounded by quotes 33 * - a string surrounded by parentheses 34 */ 35 private static final Pattern NAME_PATTERN = Pattern.compile( 36 "\"([0-9]{0,3}) ?(.*)\" \\((.*)\\)", Pattern.CASE_INSENSITIVE); 37 private static final Pattern COLOR_PATTERN = Pattern.compile( 38 "#([0-9a-f]{6})-#([0-9a-f]{6})(p?)(t?)(.*)", Pattern.CASE_INSENSITIVE); 39 40 /** 41 * Decode the setup type integer from the Bluetooth device name. 42 * @param bluetoothName 43 * @return The integer value of the setup code, or -1 if no code is present. 44 */ getSetupType(String bluetoothName)45 public static int getSetupType(String bluetoothName) { 46 Matcher matcher = NAME_PATTERN.matcher(bluetoothName); 47 if (!matcher.matches()) { 48 return -1; 49 } 50 51 String typeStr = matcher.group(1); 52 53 if (typeStr != null) { 54 try { 55 return Integer.parseInt(typeStr); 56 } catch (NumberFormatException e) { 57 return -1; 58 } 59 } else { 60 return -1; 61 } 62 } 63 64 /** 65 * Decode the LED configuration contained in the input string. 66 * @param bluetoothName 67 * @return The LedConfiguration or none if one can not be parsed from the string. 68 */ getColorConfiguration(String bluetoothName)69 public static LedConfiguration getColorConfiguration(String bluetoothName) { 70 Matcher matcher = NAME_PATTERN.matcher(bluetoothName); 71 if (!matcher.matches()) { 72 return null; 73 } 74 75 final String cs = matcher.group(3); 76 if (TextUtils.isEmpty(cs)) { 77 return null; 78 } else { 79 final Matcher cm = COLOR_PATTERN.matcher(cs); 80 if (!cm.matches()) { 81 return null; 82 } 83 LedConfiguration config = new LedConfiguration( 84 0xff000000 | Integer.parseInt(cm.group(1), 16), 85 0xff000000 | Integer.parseInt(cm.group(2), 16), 86 "p".equals(cm.group(3))); 87 config.isTransient = "t".equals(cm.group(4)); 88 return config; 89 } 90 } 91 92 /** 93 * Check if the name matches the expected format for a hub Bluetooth name. 94 * @param name 95 * @return true if the pattern matches, false if it doesn't. 96 */ isValidName(String name)97 public static boolean isValidName(String name) { 98 Matcher matcher = NAME_PATTERN.matcher(name); 99 return matcher.matches(); 100 } 101 BluetoothNameUtils()102 private BluetoothNameUtils() { 103 // DO NOT INSTANTIATE 104 } 105 } 106