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
17
18/**
19 * Various utility methods and generic defines that can be used throughout MAPS
20 */
21public class BluetoothMapUtils {
22
23    private static final String TAG = "MapUtils";
24    private static final boolean V = BluetoothMapService.VERBOSE;
25    /* We use the upper 5 bits for the type mask - avoid using the top bit, since it
26     * indicates a negative value, hence corrupting the formatter when converting to
27     * type String. (I really miss the unsigned type in Java:))
28     */
29    private static final long HANDLE_TYPE_MASK            = 0xf<<59;
30    private static final long HANDLE_TYPE_MMS_MASK        = 0x1<<59;
31    private static final long HANDLE_TYPE_EMAIL_MASK      = 0x2<<59;
32    private static final long HANDLE_TYPE_SMS_GSM_MASK    = 0x4<<59;
33    private static final long HANDLE_TYPE_SMS_CDMA_MASK   = 0x8<<59;
34
35    /**
36     * This enum is used to convert from the bMessage type property to a type safe
37     * type. Hence do not change the names of the enum values.
38     */
39    public enum TYPE{
40        EMAIL,
41        SMS_GSM,
42        SMS_CDMA,
43        MMS
44    }
45
46    /**
47     * Convert a Content Provider handle and a Messagetype into a unique handle
48     * @param cpHandle content provider handle
49     * @param messageType message type (TYPE_MMS/TYPE_SMS_GSM/TYPE_SMS_CDMA/TYPE_EMAIL)
50     * @return String Formatted Map Handle
51     */
52    static public String getMapHandle(long cpHandle, TYPE messageType){
53        String mapHandle = "-1";
54        switch(messageType)
55        {
56            case MMS:
57                mapHandle = String.format("%016X",(cpHandle | HANDLE_TYPE_MMS_MASK));
58                break;
59            case SMS_GSM:
60                mapHandle = String.format("%016X",cpHandle | HANDLE_TYPE_SMS_GSM_MASK);
61                break;
62            case SMS_CDMA:
63                mapHandle = String.format("%016X",cpHandle | HANDLE_TYPE_SMS_CDMA_MASK);
64                break;
65            case EMAIL:
66                mapHandle = String.format("%016X",(cpHandle | HANDLE_TYPE_EMAIL_MASK)); //TODO correct when email support is implemented
67                break;
68                default:
69                    throw new IllegalArgumentException("Message type not supported");
70        }
71        return mapHandle;
72
73    }
74
75    /**
76     * Convert a handle string the the raw long representation, including the type bit.
77     * @param mapHandle the handle string
78     * @return the handle value
79     */
80    static public long getMsgHandleAsLong(String mapHandle){
81        return Long.parseLong(mapHandle, 16);
82    }
83    /**
84     * Convert a Map Handle into a content provider Handle
85     * @param mapHandle handle to convert from
86     * @return content provider handle without message type mask
87     */
88    static public long getCpHandle(String mapHandle)
89    {
90        long cpHandle = getMsgHandleAsLong(mapHandle);
91        /* remove masks as the call should already know what type of message this handle is for */
92        cpHandle &= ~HANDLE_TYPE_MASK;
93        return cpHandle;
94    }
95
96    /**
97     * Extract the message type from the handle.
98     * @param mapHandle
99     * @return
100     */
101    static public TYPE getMsgTypeFromHandle(String mapHandle) {
102        long cpHandle = getMsgHandleAsLong(mapHandle);
103
104        if((cpHandle & HANDLE_TYPE_MMS_MASK) != 0)
105            return TYPE.MMS;
106        if((cpHandle & HANDLE_TYPE_EMAIL_MASK) != 0)
107            return TYPE.EMAIL;
108        if((cpHandle & HANDLE_TYPE_SMS_GSM_MASK) != 0)
109            return TYPE.SMS_GSM;
110        if((cpHandle & HANDLE_TYPE_SMS_CDMA_MASK) != 0)
111            return TYPE.SMS_CDMA;
112
113        throw new IllegalArgumentException("Message type not found in handle string.");
114    }
115}
116
117