Joseph Pirozzo | caa94c6 | 2016-10-06 09:29:43 -0700 | [diff] [blame^] | 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.google.android.car.kitchensink.bluetooth; |
| 18 | |
| 19 | import android.app.PendingIntent; |
| 20 | import android.bluetooth.BluetoothAdapter; |
| 21 | import android.bluetooth.BluetoothDevice; |
| 22 | import android.bluetooth.BluetoothMapClient; |
| 23 | import android.bluetooth.BluetoothProfile; |
| 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.net.Uri; |
| 29 | import android.os.Bundle; |
| 30 | import android.support.annotation.Nullable; |
| 31 | import android.support.v4.app.Fragment; |
| 32 | import android.util.Log; |
| 33 | import android.view.LayoutInflater; |
| 34 | import android.view.View; |
| 35 | import android.view.ViewGroup; |
| 36 | import android.widget.Button; |
| 37 | import android.widget.CheckBox; |
| 38 | import android.widget.TextView; |
| 39 | |
| 40 | import com.google.android.car.kitchensink.R; |
| 41 | |
| 42 | import java.util.List; |
| 43 | |
| 44 | public class MapMceTestFragment extends Fragment { |
| 45 | static final String MESSAGE_TO_SEND = "Im Busy Driving"; |
| 46 | private static final String TAG = "CAR.BLUETOOTH.KS"; |
| 47 | BluetoothMapClient mMapProfile; |
| 48 | BluetoothAdapter mBluetoothAdapter; |
| 49 | TextView mMessage; |
| 50 | TextView mOriginator; |
| 51 | CheckBox mSent; |
| 52 | CheckBox mDelivered; |
| 53 | TextView mBluetoothDevice; |
| 54 | PendingIntent mSentIntent; |
| 55 | PendingIntent mDeliveredIntent; |
| 56 | NotificationReceiver mTransmissionStatusReceiver; |
| 57 | Object mLock = new Object(); |
| 58 | private Intent mSendIntent; |
| 59 | private Intent mDeliveryIntent; |
| 60 | |
| 61 | @Override |
| 62 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, |
| 63 | @Nullable Bundle savedInstanceState) { |
| 64 | View v = inflater.inflate(R.layout.sms_received, container, false); |
| 65 | |
| 66 | Button reply = (Button) v.findViewById(R.id.reply); |
| 67 | Button checkMessages = (Button) v.findViewById(R.id.check_messages); |
| 68 | mBluetoothDevice = (TextView) v.findViewById(R.id.bluetoothDevice); |
| 69 | mOriginator = (TextView) v.findViewById(R.id.messageOriginator); |
| 70 | mSent = (CheckBox) v.findViewById(R.id.sent_checkbox); |
| 71 | mDelivered = (CheckBox) v.findViewById(R.id.delivered_checkbox); |
| 72 | mSendIntent = new Intent(BluetoothMapClient.ACTION_MESSAGE_SENT_SUCCESSFULLY); |
| 73 | mDeliveryIntent = new Intent(BluetoothMapClient.ACTION_MESSAGE_DELIVERED_SUCCESSFULLY); |
| 74 | mMessage = (TextView) v.findViewById(R.id.messageContent); |
| 75 | |
| 76 | //TODO add manual entry option for phone number |
| 77 | reply.setOnClickListener(new View.OnClickListener() { |
| 78 | @Override |
| 79 | public void onClick(View view) { |
| 80 | sendMessage(new Uri[]{Uri.parse(mOriginator.getText().toString())}, |
| 81 | MESSAGE_TO_SEND); |
| 82 | } |
| 83 | }); |
| 84 | |
| 85 | checkMessages.setOnClickListener(new View.OnClickListener() { |
| 86 | @Override |
| 87 | public void onClick(View view) { |
| 88 | getMessages(); |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | mTransmissionStatusReceiver = new NotificationReceiver(); |
| 93 | return v; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onResume() { |
| 98 | super.onResume(); |
| 99 | |
| 100 | mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 101 | mBluetoothAdapter.getProfileProxy(getContext(), new MapServiceListener(), |
| 102 | BluetoothProfile.MAP_CLIENT); |
| 103 | |
| 104 | IntentFilter intentFilter = new IntentFilter(); |
| 105 | intentFilter.addAction(BluetoothMapClient.ACTION_MESSAGE_SENT_SUCCESSFULLY); |
| 106 | intentFilter.addAction(BluetoothMapClient.ACTION_MESSAGE_DELIVERED_SUCCESSFULLY); |
| 107 | intentFilter.addAction(BluetoothMapClient.ACTION_MESSAGE_RECEIVED); |
| 108 | getContext().registerReceiver(mTransmissionStatusReceiver, intentFilter); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void onPause() { |
| 113 | super.onPause(); |
| 114 | getContext().unregisterReceiver(mTransmissionStatusReceiver); |
| 115 | } |
| 116 | |
| 117 | private void getMessages() { |
| 118 | synchronized (mLock) { |
| 119 | if (mMapProfile != null) { |
| 120 | Log.d(TAG, "Getting Messages"); |
| 121 | mMapProfile.getUnreadMessages(mBluetoothAdapter.getRemoteDevice( |
| 122 | mBluetoothDevice.getText().toString())); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | private void sendMessage(Uri[] recipients, String message) { |
| 128 | synchronized (mLock) { |
| 129 | mSent.setChecked(false); |
| 130 | mDelivered.setChecked(false); |
| 131 | if (mMapProfile != null) { |
| 132 | Log.d(TAG, "Sending reply"); |
| 133 | if (recipients == null) { |
| 134 | Log.d(TAG, "Recipients is null"); |
| 135 | return; |
| 136 | } |
| 137 | if (mBluetoothDevice == null) { |
| 138 | Log.d(TAG, "BluetoothDevice is null"); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | mSentIntent = PendingIntent.getBroadcast(getContext(), 0, mSendIntent, |
| 143 | PendingIntent.FLAG_ONE_SHOT); |
| 144 | mDeliveredIntent = PendingIntent.getBroadcast(getContext(), 0, mDeliveryIntent, |
| 145 | PendingIntent.FLAG_ONE_SHOT); |
| 146 | mMapProfile.sendMessage( |
| 147 | mBluetoothAdapter.getRemoteDevice(mBluetoothDevice.getText().toString()), |
| 148 | recipients, message, mSentIntent, mDeliveredIntent); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | class MapServiceListener implements BluetoothProfile.ServiceListener { |
| 154 | @Override |
| 155 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
| 156 | synchronized (mLock) { |
| 157 | mMapProfile = (BluetoothMapClient) proxy; |
| 158 | List<BluetoothDevice> connectedDevices = proxy.getConnectedDevices(); |
| 159 | if (connectedDevices.size() > 0) { |
| 160 | mBluetoothDevice.setText(connectedDevices.get(0).getAddress()); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void onServiceDisconnected(int profile) { |
| 167 | synchronized (mLock) { |
| 168 | mMapProfile = null; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | private class NotificationReceiver extends BroadcastReceiver { |
| 174 | @Override |
| 175 | public void onReceive(Context context, Intent intent) { |
| 176 | String action = intent.getAction(); |
| 177 | synchronized (mLock) { |
| 178 | if (action.equals(BluetoothMapClient.ACTION_MESSAGE_SENT_SUCCESSFULLY)) { |
| 179 | mSent.setChecked(true); |
| 180 | } else if (action.equals( |
| 181 | BluetoothMapClient.ACTION_MESSAGE_DELIVERED_SUCCESSFULLY)) { |
| 182 | mDelivered.setChecked(true); |
| 183 | } else if (action.equals(BluetoothMapClient.ACTION_MESSAGE_RECEIVED)) { |
| 184 | String[] recipients = intent.getStringArrayExtra(android.provider |
| 185 | .ContactsContract.Intents.EXTRA_RECIPIENT_CONTACT_URI); |
| 186 | StringBuilder stringBuilder = new StringBuilder(); |
| 187 | if (recipients != null) { |
| 188 | for (String s : recipients) { |
| 189 | stringBuilder.append(s); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | mMessage.setText(intent.getStringExtra(android.content.Intent.EXTRA_TEXT)); |
| 194 | mOriginator.setText(stringBuilder.toString()); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |