BluetoothMapUtils.java revision e796818505a0b57b225c3aeffc53f580c1e0c66e
1/*
2* Copyright (C) 2013 Samsung System LSI
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7*      http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15package com.android.bluetooth.map;
16
17import android.util.Log;
18
19
20/**
21 * Various utility methods and generic defines that can be used throughout MAPS
22 */
23public class BluetoothMapUtils {
24
25    private static final String TAG = "MapUtils";
26    private static final boolean D = BluetoothMapService.DEBUG;
27    private static final boolean V = BluetoothMapService.VERBOSE;
28    /* We use the upper 4 bits for the type mask.
29     * TODO: When more types are needed, consider just using a number
30     *       in stead of a bit to indicate the message type. Then 4
31     *       bit can be use for 16 different message types.
32     */
33    private static final long HANDLE_TYPE_MASK            = (((long)0xf)<<56);
34    private static final long HANDLE_TYPE_MMS_MASK        = (((long)0x1)<<56);
35    private static final long HANDLE_TYPE_EMAIL_MASK      = (((long)0x2)<<56);
36    private static final long HANDLE_TYPE_SMS_GSM_MASK    = (((long)0x4)<<56);
37    private static final long HANDLE_TYPE_SMS_CDMA_MASK   = (((long)0x8)<<56);
38
39    /**
40     * This enum is used to convert from the bMessage type property to a type safe
41     * type. Hence do not change the names of the enum values.
42     */
43    public enum TYPE{
44        EMAIL,
45        SMS_GSM,
46        SMS_CDMA,
47        MMS
48    }
49
50    public static String getLongAsString(long v) {
51        char[] result = new char[16];
52        int v1 = (int) (v & 0xffffffff);
53        int v2 = (int) ((v>>32) & 0xffffffff);
54        int c;
55        for (int i = 0; i < 8; i++) {
56            c = v2 & 0x0f;
57            c += (c < 10) ? '0' : ('A'-10);
58            result[7 - i] = (char) c;
59            v2 >>= 4;
60            c = v1 & 0x0f;
61            c += (c < 10) ? '0' : ('A'-10);
62            result[15 - i] = (char)c;
63            v1 >>= 4;
64        }
65        return new String(result);
66    }
67
68    /**
69     * Convert a Content Provider handle and a Messagetype into a unique handle
70     * @param cpHandle content provider handle
71     * @param messageType message type (TYPE_MMS/TYPE_SMS_GSM/TYPE_SMS_CDMA/TYPE_EMAIL)
72     * @return String Formatted Map Handle
73     */
74    public static String getMapHandle(long cpHandle, TYPE messageType){
75        String mapHandle = "-1";
76        switch(messageType)
77        {
78
79            case MMS:
80                mapHandle = getLongAsString(cpHandle | HANDLE_TYPE_MMS_MASK);
81                break;
82            case SMS_GSM:
83                mapHandle = getLongAsString(cpHandle | HANDLE_TYPE_SMS_GSM_MASK);
84                break;
85            case SMS_CDMA:
86                mapHandle = getLongAsString(cpHandle | HANDLE_TYPE_SMS_CDMA_MASK);
87                break;
88            case EMAIL:
89                mapHandle = getLongAsString(cpHandle | HANDLE_TYPE_EMAIL_MASK);
90                break;
91                default:
92                    throw new IllegalArgumentException("Message type not supported");
93        }
94        return mapHandle;
95
96    }
97
98    /**
99     * Convert a handle string the the raw long representation, including the type bit.
100     * @param mapHandle the handle string
101     * @return the handle value
102     */
103    static public long getMsgHandleAsLong(String mapHandle){
104        return Long.parseLong(mapHandle, 16);
105    }
106    /**
107     * Convert a Map Handle into a content provider Handle
108     * @param mapHandle handle to convert from
109     * @return content provider handle without message type mask
110     */
111    static public long getCpHandle(String mapHandle)
112    {
113        long cpHandle = getMsgHandleAsLong(mapHandle);
114        if(D)Log.d(TAG,"-> MAP handle:"+mapHandle);
115        /* remove masks as the call should already know what type of message this handle is for */
116        cpHandle &= ~HANDLE_TYPE_MASK;
117        if(D)Log.d(TAG,"->CP handle:"+cpHandle);
118
119        return cpHandle;
120    }
121
122    /**
123     * Extract the message type from the handle.
124     * @param mapHandle
125     * @return
126     */
127    static public TYPE getMsgTypeFromHandle(String mapHandle) {
128        long cpHandle = getMsgHandleAsLong(mapHandle);
129
130        if((cpHandle & HANDLE_TYPE_MMS_MASK) != 0)
131            return TYPE.MMS;
132        if((cpHandle & HANDLE_TYPE_EMAIL_MASK) != 0)
133            return TYPE.EMAIL;
134        if((cpHandle & HANDLE_TYPE_SMS_GSM_MASK) != 0)
135            return TYPE.SMS_GSM;
136        if((cpHandle & HANDLE_TYPE_SMS_CDMA_MASK) != 0)
137            return TYPE.SMS_CDMA;
138
139        throw new IllegalArgumentException("Message type not found in handle string.");
140    }
141}
142
143