Home | History | Annotate | Download | only in messenger
      1 /*
      2  * Copyright (C) 2017 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.car.messenger;
     18 
     19 import android.bluetooth.BluetoothDevice;
     20 import android.bluetooth.BluetoothMapClient;
     21 import android.content.Intent;
     22 import android.support.annotation.Nullable;
     23 
     24 /**
     25  * Represents a message obtained via MAP service from a connected Bluetooth device.
     26  */
     27 class MapMessage {
     28     private BluetoothDevice mDevice;
     29     private String mHandle;
     30     private long mReceivedTimeMs;
     31     private String mSenderName;
     32     @Nullable
     33     private String mSenderContactUri;
     34     private String mText;
     35 
     36     /**
     37      * Constructs Message from {@code intent} that was received from MAP service via
     38      * {@link BluetoothMapClient#ACTION_MESSAGE_RECEIVED} broadcast.
     39      *
     40      * @param intent Intent received from MAP service.
     41      * @return Message constructed from extras in {@code intent}.
     42      * @throws IllegalArgumentException If {@code intent} is missing any required fields.
     43      */
     44     public static MapMessage parseFrom(Intent intent) {
     45         BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
     46         String handle = intent.getStringExtra(BluetoothMapClient.EXTRA_MESSAGE_HANDLE);
     47         String senderContactUri = intent.getStringExtra(
     48                 BluetoothMapClient.EXTRA_SENDER_CONTACT_URI);
     49         String senderContactName = intent.getStringExtra(
     50                 BluetoothMapClient.EXTRA_SENDER_CONTACT_NAME);
     51         String text = intent.getStringExtra(android.content.Intent.EXTRA_TEXT);
     52         return new MapMessage(device, handle, System.currentTimeMillis(), senderContactName,
     53                 senderContactUri, text);
     54     }
     55 
     56     private MapMessage(BluetoothDevice device,
     57             String handle,
     58             long receivedTimeMs,
     59             String senderName,
     60             @Nullable String senderContactUri,
     61             String text) {
     62         boolean missingDevice = (device == null);
     63         boolean missingHandle = (handle == null);
     64         boolean missingSenderName = (senderName == null);
     65         boolean missingText = (text == null);
     66         if (missingDevice || missingHandle || missingText) {
     67             StringBuilder builder = new StringBuilder("Missing required fields:");
     68             if (missingDevice) {
     69                 builder.append(" device");
     70             }
     71             if (missingHandle) {
     72                 builder.append(" handle");
     73             }
     74             if (missingSenderName) {
     75                 builder.append(" senderName");
     76             }
     77             if (missingText) {
     78                 builder.append(" text");
     79             }
     80             throw new IllegalArgumentException(builder.toString());
     81         }
     82         mDevice = device;
     83         mHandle = handle;
     84         mReceivedTimeMs = receivedTimeMs;
     85         mText = text;
     86         mSenderContactUri = senderContactUri;
     87         mSenderName = senderName;
     88     }
     89 
     90     public BluetoothDevice getDevice() {
     91         return mDevice;
     92     }
     93 
     94     /**
     95      * @return Unique handle for this message. NOTE: The handle is only required to be unique for
     96      *      the lifetime of a single MAP session.
     97      */
     98     public String getHandle() {
     99         return mHandle;
    100     }
    101 
    102     /**
    103      * @return Milliseconds since epoch at which this message notification was received on the head-
    104      *      unit.
    105      */
    106     public long getReceivedTimeMs() {
    107         return mReceivedTimeMs;
    108     }
    109 
    110     /**
    111      * @return Contact name as obtained from the device. If contact is in the device's address-book,
    112      *       this is typically the contact name. Otherwise it will be the phone number.
    113      */
    114     public String getSenderName() {
    115         return mSenderName;
    116     }
    117 
    118     /**
    119      * @return Sender phone number available as a URI string. iPhone's don't provide these.
    120      */
    121     @Nullable
    122     public String getSenderContactUri() {
    123         return mSenderContactUri;
    124     }
    125 
    126     /**
    127      * @return Actual content of the message.
    128      */
    129     public String getText() {
    130         return mText;
    131     }
    132 
    133     @Override
    134     public String toString() {
    135         return "MapMessage{" +
    136                 "mDevice=" + mDevice +
    137                 ", mHandle='" + mHandle + '\'' +
    138                 ", mReceivedTimeMs=" + mReceivedTimeMs +
    139                 ", mText='" + mText + '\'' +
    140                 ", mSenderContactUri='" + mSenderContactUri + '\'' +
    141                 ", mSenderName='" + mSenderName + '\'' +
    142                 '}';
    143     }
    144 }
    145