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