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        if (subjectLength < 0 || subjectLength > 255) {
45            throw new IllegalArgumentException("subjectLength should be [0..255]");
46        }
47
48        if (maxListCount < 0 || maxListCount > 65535) {
49            throw new IllegalArgumentException("maxListCount should be [0..65535]");
50        }
51
52        if (listStartOffset < 0 || listStartOffset > 65535) {
53            throw new IllegalArgumentException("listStartOffset should be [0..65535]");
54        }
55
56        mHeaderSet.setHeader(HeaderSet.TYPE, TYPE);
57
58        if (folderName == null) {
59            mHeaderSet.setHeader(HeaderSet.NAME, "");
60        } else {
61            mHeaderSet.setHeader(HeaderSet.NAME, folderName);
62        }
63
64        ObexAppParameters oap = new ObexAppParameters();
65
66        if (filter != null) {
67            if (filter.messageType != MessagesFilter.MESSAGE_TYPE_ALL) {
68                oap.add(OAP_TAGID_FILTER_MESSAGE_TYPE, filter.messageType);
69            }
70
71            if (filter.periodBegin != null) {
72                oap.add(OAP_TAGID_FILTER_PERIOD_BEGIN, filter.periodBegin);
73            }
74
75            if (filter.periodEnd != null) {
76                oap.add(OAP_TAGID_FILTER_PERIOD_END, filter.periodEnd);
77            }
78
79            if (filter.readStatus != MessagesFilter.READ_STATUS_ANY) {
80                oap.add(OAP_TAGID_FILTER_READ_STATUS, filter.readStatus);
81            }
82
83            if (filter.recipient != null) {
84                oap.add(OAP_TAGID_FILTER_RECIPIENT, filter.recipient);
85            }
86
87            if (filter.originator != null) {
88                oap.add(OAP_TAGID_FILTER_ORIGINATOR, filter.originator);
89            }
90
91            if (filter.priority != MessagesFilter.PRIORITY_ANY) {
92                oap.add(OAP_TAGID_FILTER_PRIORITY, filter.priority);
93            }
94        }
95
96        if (subjectLength != 0) {
97            oap.add(OAP_TAGID_SUBJECT_LENGTH, (byte) subjectLength);
98        }
99        /* Include parameterMask only when specific values are selected,
100         * to avoid IOT specific issue with no paramterMask header support.
101         */
102        if (parameters >  0 ) {
103            oap.add(OAP_TAGID_PARAMETER_MASK, parameters);
104        }
105        // Allow GetMessageListing for maxlistcount value 0 also.
106        if (maxListCount >= 0) {
107            oap.add(OAP_TAGID_MAX_LIST_COUNT, (short) maxListCount);
108        }
109
110        if (listStartOffset != 0) {
111            oap.add(OAP_TAGID_START_OFFSET, (short) listStartOffset);
112        }
113
114        oap.addToHeaderSet(mHeaderSet);
115    }
116
117    @Override
118    protected void readResponse(InputStream stream) {
119        mResponse = new BluetoothMapMessagesListing(stream);
120    }
121
122    @Override
123    protected void readResponseHeaders(HeaderSet headerset) {
124        ObexAppParameters oap = ObexAppParameters.fromHeaderSet(headerset);
125
126        mNewMessage = ((oap.getByte(OAP_TAGID_NEW_MESSAGE) & 0x01) == 1);
127
128        if (oap.exists(OAP_TAGID_MSE_TIME)) {
129            String mseTime = oap.getString(OAP_TAGID_MSE_TIME);
130            if(mseTime != null )
131               mServerTime = (new ObexTime(mseTime)).getTime();
132        }
133    }
134
135    public ArrayList<BluetoothMapMessage> getList() {
136        if (mResponse == null) {
137            return null;
138        }
139
140        return mResponse.getList();
141    }
142
143    public boolean getNewMessageStatus() {
144        return mNewMessage;
145    }
146
147    public Date getMseTime() {
148        return mServerTime;
149    }
150
151    @Override
152    public void execute(ClientSession session) throws IOException {
153        executeGet(session);
154    }
155}
156