Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2016 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.bluetooth.util;
     18 
     19 import java.util.ArrayList;
     20 import java.util.List;
     21 
     22 /**
     23  * Centralized Bluetooth Interoperability workaround utilities and database.
     24  * This is the Java version. An analagous native version can be found
     25  * in /system/bt/devices/include/interop_database.h.
     26  */
     27 public class Interop {
     28 
     29   /**
     30    * Simple interop entry consisting of a workarond id (see below)
     31    * and a (partial or complete) Bluetooth device address string
     32    * to match against.
     33    */
     34   private static class Entry {
     35     String address;
     36     int workaround_id;
     37 
     38     public Entry(int workaround_id, String address) {
     39       this.workaround_id = workaround_id;
     40       this.address = address;
     41     }
     42   }
     43 
     44   /**
     45    * The actual "database" of interop entries.
     46    */
     47   private static List<Entry> entries = null;
     48 
     49   /**
     50    * Workaround ID for deivces which do not accept non-ASCII
     51    * characters in SMS messages.
     52    */
     53   public static final int INTEROP_MAP_ASCIIONLY = 1;
     54 
     55   /**
     56    * Initializes the interop datbase with the relevant workaround
     57    * entries.
     58    * When adding entries, please provide a description for each
     59    * device as to what problem the workaround addresses.
     60    */
     61   private static void lazyInitInteropDatabase() {
     62     if (entries != null) return;
     63     entries = new ArrayList<Entry>();
     64 
     65     /** Mercedes Benz NTG 4.5 does not handle non-ASCII characters in SMS */
     66     entries.add(new Entry(INTEROP_MAP_ASCIIONLY, "00:26:e8"));
     67   }
     68 
     69   /**
     70    * Checks wheter a given device identified by |address| is a match
     71    * for a given workaround identified by |workaround_id|.
     72    * Return true if the address matches, false otherwise.
     73    */
     74   public static boolean matchByAddress(int workaround_id, String address) {
     75     if (address == null || address.isEmpty()) return false;
     76 
     77     lazyInitInteropDatabase();
     78     for (Entry entry : entries) {
     79       if (entry.workaround_id == workaround_id &&
     80           entry.address.startsWith(address.toLowerCase())) {
     81         return true;
     82       }
     83     }
     84 
     85     return false;
     86   }
     87 }
     88