• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.android.bluetooth.pbapclient;
18 
19 import android.util.Log;
20 
21 import java.io.IOException;
22 import java.io.InputStream;
23 
24 import javax.obex.ClientOperation;
25 import javax.obex.ClientSession;
26 import javax.obex.HeaderSet;
27 import javax.obex.ResponseCodes;
28 
29 abstract class BluetoothPbapRequest {
30 
31     private static final String TAG = "BluetoothPbapRequest";
32 
33     protected static final byte OAP_TAGID_ORDER = 0x01;
34     protected static final byte OAP_TAGID_SEARCH_VALUE = 0x02;
35     protected static final byte OAP_TAGID_SEARCH_ATTRIBUTE = 0x03;
36     protected static final byte OAP_TAGID_MAX_LIST_COUNT = 0x04;
37     protected static final byte OAP_TAGID_LIST_START_OFFSET = 0x05;
38     protected static final byte OAP_TAGID_FILTER = 0x06;
39     protected static final byte OAP_TAGID_FORMAT = 0x07;
40     protected static final byte OAP_TAGID_PHONEBOOK_SIZE = 0x08;
41     protected static final byte OAP_TAGID_NEW_MISSED_CALLS = 0x09;
42 
43     protected HeaderSet mHeaderSet;
44 
45     protected int mResponseCode;
46 
47     private boolean mAborted = false;
48 
49     private ClientOperation mOp = null;
50 
BluetoothPbapRequest()51     public BluetoothPbapRequest() {
52         mHeaderSet = new HeaderSet();
53     }
54 
isSuccess()55     final public boolean isSuccess() {
56         return (mResponseCode == ResponseCodes.OBEX_HTTP_OK);
57     }
58 
execute(ClientSession session)59     public void execute(ClientSession session) throws IOException {
60         Log.v(TAG, "execute");
61 
62         /* in case request is aborted before can be executed */
63         if (mAborted) {
64             mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
65             return;
66         }
67 
68         try {
69             mOp = (ClientOperation) session.get(mHeaderSet);
70 
71             /* make sure final flag for GET is used (PBAP spec 6.2.2) */
72             mOp.setGetFinalFlag(true);
73 
74             /*
75              * this will trigger ClientOperation to use non-buffered stream so
76              * we can abort operation
77              */
78             mOp.continueOperation(true, false);
79 
80             readResponseHeaders(mOp.getReceivedHeader());
81 
82             InputStream is = mOp.openInputStream();
83             readResponse(is);
84             is.close();
85 
86             mOp.close();
87 
88             mResponseCode = mOp.getResponseCode();
89 
90             Log.d(TAG, "mResponseCode=" + mResponseCode);
91 
92             checkResponseCode(mResponseCode);
93         } catch (IOException e) {
94             Log.e(TAG, "IOException occured when processing request", e);
95             mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
96 
97             throw e;
98         }
99     }
100 
abort()101     public void abort() {
102         mAborted = true;
103 
104         if (mOp != null) {
105             try {
106                 mOp.abort();
107             } catch (IOException e) {
108                 Log.e(TAG, "Exception occured when trying to abort", e);
109             }
110         }
111     }
112 
readResponse(InputStream stream)113     protected void readResponse(InputStream stream) throws IOException {
114         Log.v(TAG, "readResponse");
115 
116         /* nothing here by default */
117     }
118 
readResponseHeaders(HeaderSet headerset)119     protected void readResponseHeaders(HeaderSet headerset) {
120         Log.v(TAG, "readResponseHeaders");
121 
122         /* nothing here by dafault */
123     }
124 
checkResponseCode(int responseCode)125     protected void checkResponseCode(int responseCode) throws IOException {
126         Log.v(TAG, "checkResponseCode");
127 
128         /* nothing here by dafault */
129     }
130 }
131