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
17package android.bluetooth.client.map;
18
19import java.io.IOException;
20import java.math.BigInteger;
21
22import javax.obex.ClientSession;
23import javax.obex.HeaderSet;
24import javax.obex.ResponseCodes;
25
26import android.bluetooth.client.map.BluetoothMasClient.CharsetType;
27import android.bluetooth.client.map.utils.ObexAppParameters;
28
29final class BluetoothMasRequestPushMessage extends BluetoothMasRequest {
30
31    private static final String TYPE = "x-bt/message";
32    private String mMsg;
33    private String mMsgHandle;
34
35    private BluetoothMasRequestPushMessage(String folder) {
36        mHeaderSet.setHeader(HeaderSet.TYPE, TYPE);
37        if (folder == null) {
38            folder = "";
39        }
40        mHeaderSet.setHeader(HeaderSet.NAME, folder);
41    }
42
43    public BluetoothMasRequestPushMessage(String folder, String msg, CharsetType charset,
44            boolean transparent, boolean retry) {
45        this(folder);
46        mMsg = msg;
47        ObexAppParameters oap = new ObexAppParameters();
48        oap.add(OAP_TAGID_TRANSPARENT, transparent ? TRANSPARENT_ON : TRANSPARENT_OFF);
49        oap.add(OAP_TAGID_RETRY, retry ? RETRY_ON : RETRY_OFF);
50        oap.add(OAP_TAGID_CHARSET, charset == CharsetType.NATIVE ? CHARSET_NATIVE : CHARSET_UTF8);
51        oap.addToHeaderSet(mHeaderSet);
52    }
53
54    @Override
55    protected void readResponseHeaders(HeaderSet headerset) {
56        try {
57            String handle = (String) headerset.getHeader(HeaderSet.NAME);
58            if (handle != null) {
59                /* just to validate */
60                new BigInteger(handle, 16);
61
62                mMsgHandle = handle;
63            }
64        } catch (NumberFormatException e) {
65            mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
66        } catch (IOException e) {
67            mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
68        }
69    }
70
71    public String getMsgHandle() {
72        return mMsgHandle;
73    }
74
75    @Override
76    public void execute(ClientSession session) throws IOException {
77        executePut(session, mMsg.getBytes());
78    }
79}
80