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