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 android.bluetooth.client.map.BluetoothMasClient.MessagesFilter;
20import android.bluetooth.client.map.utils.ObexAppParameters;
21import android.bluetooth.client.map.utils.ObexTime;
22
23import java.io.IOException;
24import java.io.InputStream;
25import java.util.ArrayList;
26import java.util.Date;
27
28import javax.obex.ClientSession;
29import javax.obex.HeaderSet;
30
31final class BluetoothMasRequestGetMessagesListing extends BluetoothMasRequest {
32
33    private static final String TYPE = "x-bt/MAP-msg-listing";
34
35    private BluetoothMapMessagesListing mResponse = null;
36
37    private boolean mNewMessage = false;
38
39    private Date mServerTime = null;
40
41    public BluetoothMasRequestGetMessagesListing(String folderName, int parameters,
42            BluetoothMasClient.MessagesFilter filter, int subjectLength, int maxListCount,
43            int listStartOffset) {
44
45        if (subjectLength < 0 || subjectLength > 255) {
46            throw new IllegalArgumentException("subjectLength should be [0..255]");
47        }
48
49        if (maxListCount < 0 || maxListCount > 65535) {
50            throw new IllegalArgumentException("maxListCount should be [0..65535]");
51        }
52
53        if (listStartOffset < 0 || listStartOffset > 65535) {
54            throw new IllegalArgumentException("listStartOffset should be [0..65535]");
55        }
56
57        mHeaderSet.setHeader(HeaderSet.TYPE, TYPE);
58
59        if (folderName == null) {
60            mHeaderSet.setHeader(HeaderSet.NAME, "");
61        } else {
62            mHeaderSet.setHeader(HeaderSet.NAME, folderName);
63        }
64
65        ObexAppParameters oap = new ObexAppParameters();
66
67        if (filter != null) {
68            if (filter.messageType != MessagesFilter.MESSAGE_TYPE_ALL) {
69                oap.add(OAP_TAGID_FILTER_MESSAGE_TYPE, filter.messageType);
70            }
71
72            if (filter.periodBegin != null) {
73                oap.add(OAP_TAGID_FILTER_PERIOD_BEGIN, filter.periodBegin);
74            }
75
76            if (filter.periodEnd != null) {
77                oap.add(OAP_TAGID_FILTER_PERIOD_END, filter.periodEnd);
78            }
79
80            if (filter.readStatus != MessagesFilter.READ_STATUS_ANY) {
81                oap.add(OAP_TAGID_FILTER_READ_STATUS, filter.readStatus);
82            }
83
84            if (filter.recipient != null) {
85                oap.add(OAP_TAGID_FILTER_RECIPIENT, filter.recipient);
86            }
87
88            if (filter.originator != null) {
89                oap.add(OAP_TAGID_FILTER_ORIGINATOR, filter.originator);
90            }
91
92            if (filter.priority != MessagesFilter.PRIORITY_ANY) {
93                oap.add(OAP_TAGID_FILTER_PRIORITY, filter.priority);
94            }
95        }
96
97        if (subjectLength != 0) {
98            oap.add(OAP_TAGID_SUBJECT_LENGTH, (byte) subjectLength);
99        }
100
101        if (maxListCount != 0) {
102            oap.add(OAP_TAGID_MAX_LIST_COUNT, (short) maxListCount);
103        }
104
105        if (listStartOffset != 0) {
106            oap.add(OAP_TAGID_START_OFFSET, (short) listStartOffset);
107        }
108
109        oap.addToHeaderSet(mHeaderSet);
110    }
111
112    @Override
113    protected void readResponse(InputStream stream) {
114        mResponse = new BluetoothMapMessagesListing(stream);
115    }
116
117    @Override
118    protected void readResponseHeaders(HeaderSet headerset) {
119        ObexAppParameters oap = ObexAppParameters.fromHeaderSet(headerset);
120
121        mNewMessage = ((oap.getByte(OAP_TAGID_NEW_MESSAGE) & 0x01) == 1);
122
123        if (oap.exists(OAP_TAGID_MSE_TIME)) {
124            String mseTime = oap.getString(OAP_TAGID_MSE_TIME);
125
126            mServerTime = (new ObexTime(mseTime)).getTime();
127        }
128    }
129
130    public ArrayList<BluetoothMapMessage> getList() {
131        if (mResponse == null) {
132            return null;
133        }
134
135        return mResponse.getList();
136    }
137
138    public boolean getNewMessageStatus() {
139        return mNewMessage;
140    }
141
142    public Date getMseTime() {
143        return mServerTime;
144    }
145
146    @Override
147    public void execute(ClientSession session) throws IOException {
148        executeGet(session);
149    }
150}
151