Hemant Gupta | 192d793 | 2014-04-08 16:04:13 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 android.bluetooth.client.map; |
| 18 | |
| 19 | import android.os.Handler; |
| 20 | import android.os.Process; |
| 21 | import android.util.Log; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | |
| 25 | import javax.obex.ClientSession; |
| 26 | import javax.obex.HeaderSet; |
| 27 | import javax.obex.ObexTransport; |
Nitin Shivpure | e5b6f5c | 2014-11-21 18:44:35 +0530 | [diff] [blame] | 28 | import javax.obex.ObexHelper; |
Hemant Gupta | 192d793 | 2014-04-08 16:04:13 +0530 | [diff] [blame] | 29 | import javax.obex.ResponseCodes; |
| 30 | |
| 31 | class BluetoothMasObexClientSession { |
| 32 | private static final String TAG = "BluetoothMasObexClientSession"; |
| 33 | |
| 34 | private static final byte[] MAS_TARGET = new byte[] { |
| 35 | (byte) 0xbb, 0x58, 0x2b, 0x40, 0x42, 0x0c, 0x11, (byte) 0xdb, (byte) 0xb0, (byte) 0xde, |
| 36 | 0x08, 0x00, 0x20, 0x0c, (byte) 0x9a, 0x66 |
| 37 | }; |
| 38 | |
| 39 | static final int MSG_OBEX_CONNECTED = 100; |
| 40 | static final int MSG_OBEX_DISCONNECTED = 101; |
| 41 | static final int MSG_REQUEST_COMPLETED = 102; |
| 42 | |
| 43 | private final ObexTransport mTransport; |
| 44 | |
| 45 | private final Handler mSessionHandler; |
| 46 | |
| 47 | private ClientThread mClientThread; |
| 48 | |
| 49 | private volatile boolean mInterrupted; |
| 50 | |
| 51 | private class ClientThread extends Thread { |
| 52 | private final ObexTransport mTransport; |
| 53 | |
| 54 | private ClientSession mSession; |
| 55 | |
| 56 | private BluetoothMasRequest mRequest; |
| 57 | |
| 58 | private boolean mConnected; |
| 59 | |
| 60 | public ClientThread(ObexTransport transport) { |
| 61 | super("MAS ClientThread"); |
| 62 | |
| 63 | mTransport = transport; |
| 64 | mConnected = false; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void run() { |
| 69 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 70 | |
| 71 | connect(); |
| 72 | |
| 73 | if (mConnected) { |
| 74 | mSessionHandler.obtainMessage(MSG_OBEX_CONNECTED).sendToTarget(); |
| 75 | } else { |
| 76 | mSessionHandler.obtainMessage(MSG_OBEX_DISCONNECTED).sendToTarget(); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | while (!mInterrupted) { |
| 81 | synchronized (this) { |
| 82 | if (mRequest == null) { |
| 83 | try { |
| 84 | this.wait(); |
| 85 | } catch (InterruptedException e) { |
| 86 | mInterrupted = true; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (!mInterrupted && mRequest != null) { |
| 92 | try { |
| 93 | mRequest.execute(mSession); |
| 94 | } catch (IOException e) { |
| 95 | // this will "disconnect" to cleanup |
| 96 | mInterrupted = true; |
| 97 | } |
| 98 | |
| 99 | BluetoothMasRequest oldReq = mRequest; |
| 100 | mRequest = null; |
| 101 | |
| 102 | mSessionHandler.obtainMessage(MSG_REQUEST_COMPLETED, oldReq).sendToTarget(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | disconnect(); |
| 107 | |
| 108 | mSessionHandler.obtainMessage(MSG_OBEX_DISCONNECTED).sendToTarget(); |
| 109 | } |
| 110 | |
| 111 | private void connect() { |
| 112 | try { |
Nitin Shivpure | e5b6f5c | 2014-11-21 18:44:35 +0530 | [diff] [blame] | 113 | Log.w(TAG, "connect:"); |
Hemant Gupta | 192d793 | 2014-04-08 16:04:13 +0530 | [diff] [blame] | 114 | mSession = new ClientSession(mTransport); |
| 115 | |
| 116 | HeaderSet headerset = new HeaderSet(); |
| 117 | headerset.setHeader(HeaderSet.TARGET, MAS_TARGET); |
| 118 | |
| 119 | headerset = mSession.connect(headerset); |
| 120 | |
| 121 | if (headerset.getResponseCode() == ResponseCodes.OBEX_HTTP_OK) { |
| 122 | mConnected = true; |
| 123 | } else { |
| 124 | disconnect(); |
| 125 | } |
| 126 | } catch (IOException e) { |
Nitin Shivpure | e5b6f5c | 2014-11-21 18:44:35 +0530 | [diff] [blame] | 127 | Log.w(TAG, "handled connect exception: ", e); |
Hemant Gupta | 192d793 | 2014-04-08 16:04:13 +0530 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | private void disconnect() { |
Nitin Shivpure | e5b6f5c | 2014-11-21 18:44:35 +0530 | [diff] [blame] | 132 | Log.w(TAG, "disconnect: "); |
Hemant Gupta | 192d793 | 2014-04-08 16:04:13 +0530 | [diff] [blame] | 133 | try { |
| 134 | mSession.disconnect(null); |
| 135 | } catch (IOException e) { |
| 136 | } |
| 137 | |
| 138 | try { |
| 139 | mSession.close(); |
| 140 | } catch (IOException e) { |
Nitin Shivpure | e5b6f5c | 2014-11-21 18:44:35 +0530 | [diff] [blame] | 141 | Log.w(TAG, "handled disconnect exception:", e); |
Hemant Gupta | 192d793 | 2014-04-08 16:04:13 +0530 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | mConnected = false; |
| 145 | } |
| 146 | |
| 147 | public synchronized boolean schedule(BluetoothMasRequest request) { |
| 148 | if (mRequest != null) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | mRequest = request; |
| 153 | notify(); |
| 154 | |
| 155 | return true; |
| 156 | } |
Nitin Shivpure | 614d2e0 | 2015-09-28 12:43:46 +0530 | [diff] [blame] | 157 | |
| 158 | private void shutdown() { |
| 159 | Log.w(TAG, "shutdown "); |
| 160 | mInterrupted = true; |
| 161 | interrupt(); |
| 162 | } |
Hemant Gupta | 192d793 | 2014-04-08 16:04:13 +0530 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | public BluetoothMasObexClientSession(ObexTransport transport, Handler handler) { |
| 166 | mTransport = transport; |
| 167 | mSessionHandler = handler; |
| 168 | } |
| 169 | |
| 170 | public void start() { |
| 171 | if (mClientThread == null) { |
| 172 | mClientThread = new ClientThread(mTransport); |
| 173 | mClientThread.start(); |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | public void stop() { |
| 179 | if (mClientThread != null) { |
Nitin Shivpure | 614d2e0 | 2015-09-28 12:43:46 +0530 | [diff] [blame] | 180 | mClientThread.shutdown(); |
Hemant Gupta | 192d793 | 2014-04-08 16:04:13 +0530 | [diff] [blame] | 181 | |
Nitin Shivpure | ee991e9 | 2015-11-03 17:29:41 +0530 | [diff] [blame] | 182 | Thread t = new Thread(new Runnable() { |
| 183 | public void run () { |
| 184 | Log.d(TAG, "Spawning a new thread for stopping obex session"); |
Hemant Gupta | 192d793 | 2014-04-08 16:04:13 +0530 | [diff] [blame] | 185 | try { |
| 186 | mClientThread.join(); |
| 187 | mClientThread = null; |
| 188 | } catch (InterruptedException e) { |
| 189 | Log.w(TAG, "Interrupted while waiting for thread to join"); |
| 190 | } |
| 191 | } |
Nitin Shivpure | ee991e9 | 2015-11-03 17:29:41 +0530 | [diff] [blame] | 192 | }); |
| 193 | t.start(); |
| 194 | Log.d(TAG, "Exiting from the stopping thread"); |
Hemant Gupta | 192d793 | 2014-04-08 16:04:13 +0530 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | public boolean makeRequest(BluetoothMasRequest request) { |
| 199 | if (mClientThread == null) { |
| 200 | return false; |
| 201 | } |
| 202 | |
Nitin Shivpure | e5b6f5c | 2014-11-21 18:44:35 +0530 | [diff] [blame] | 203 | if (((BluetoothMapTransport)mTransport).isSrmSupported()) { |
| 204 | Log.d(TAG, "Client is srm capable"); |
| 205 | if (request instanceof BluetoothMasRequestGetFolderListing || |
| 206 | request instanceof BluetoothMasRequestGetFolderListingSize || |
| 207 | request instanceof BluetoothMasRequestGetMessagesListing || |
| 208 | request instanceof BluetoothMasRequestGetMessage || |
| 209 | request instanceof BluetoothMasRequestPushMessage || |
| 210 | request instanceof BluetoothMasRequestGetMessagesListingSize) { |
| 211 | mClientThread.mSession.setLocalSrmStatus(true); |
| 212 | } |
| 213 | |
| 214 | if (request instanceof BluetoothMasRequestSetMessageStatus || |
| 215 | request instanceof BluetoothMasRequestUpdateInbox|| |
| 216 | request instanceof BluetoothMasRequestSetNotificationRegistration) { |
| 217 | mClientThread.mSession.setLocalSrmStatus(false); |
| 218 | } |
| 219 | } else { |
| 220 | Log.d(TAG, "Client is not srm capable"); |
| 221 | } |
| 222 | |
Hemant Gupta | 192d793 | 2014-04-08 16:04:13 +0530 | [diff] [blame] | 223 | return mClientThread.schedule(request); |
| 224 | } |
| 225 | } |