MAPC: Fix following issues in map client code

  - Fix NPE for ObexTime and dateTimeformat.
      Handle possible NullPointerException while accessing
      map client ObexTime constructor utility.

      Send all parameters event, status and notification state to
      Test Client for setNotifcationRegistration event both success
      and failure case.

   - Fix GetMessagelisting Request for maxlistcount value 0.

      Include maxlistcount for value 0 in application parameters header
      while composing obex GetMessageListing Request to MAP Server.

   - Add maxListCount param in getfolderlisting.

      A special case, where MCE APP send the folderlisting
      with maxlistcount=0, startoffset=5, As DUT(MCE role)
      does not include maxListcount param in folderlsiting,
      when it's value set to 0 by MCE APP. So as per the spec,
      MSE assume  maxListCount = 1024 for folderlsiting & MSE
      response as the bad request, when startoffset is Out of
      range. Adding MaxListCount in folderListing to
      solve this issue, when it's value set to 0.

   - Handle exception for incorrect length value from MSE

      Handle possible exception while parsing GET Message response
      with incorrect LENGTH value fetched from MSE.

   - Include ParameterMask Header in GET only when specified.

      Include ParmaterMask in GET MessageLising Request only when
      specific values are selected from TestClient.
      ParameterMask is not a mandatory feild as per MAP1.1 Spec.
      excluding ParameterMask for default or value '0' fix MSE IOT
      issues that doesnot support ParameterMask Header.

Change-Id: I1bee3f512f877974bb8fd63f3b27dc3ce3ff8a13
diff --git a/src/android/bluetooth/client/map/BluetoothMapBmessageParser.java b/src/android/bluetooth/client/map/BluetoothMapBmessageParser.java
index fa3d817..ef75e7b 100644
--- a/src/android/bluetooth/client/map/BluetoothMapBmessageParser.java
+++ b/src/android/bluetooth/client/map/BluetoothMapBmessageParser.java
@@ -312,8 +312,16 @@
 
         prop = mParser.next(true);
 
-        if (prop != null && prop.equals(END_MSG)) {
-            mBmsg.mMessage = new String(data, 0, messageLen);
+        if (prop != null) {
+            if (prop.equals(END_MSG)) {
+                mBmsg.mMessage = new String(data, 0, messageLen);
+            } else {
+                /* Handle possible exception for incorrect LENGTH value
+                 * from MSE while parsing  GET Message response */
+                Log.e(TAG, "Prop Invalid: "+ prop.toString());
+                Log.e(TAG, "Possible Invalid LENGTH value");
+                throw expected(END_MSG);
+            }
         } else {
 
             data = null;
@@ -322,6 +330,11 @@
              * now we check if bMessage can be parsed if LENGTH is handled as
              * number of characters instead of number of bytes
              */
+            if (offset < 0 || offset > remng.length()) {
+                /* Handle possible exception for incorrect LENGTH value
+                 * from MSE while parsing  GET Message response */
+                throw new ParseException("Invalid LENGTH value", mParser.pos());
+            }
 
             Log.w(TAG, "byte LENGTH seems to be invalid, trying with char length");
 
diff --git a/src/android/bluetooth/client/map/BluetoothMapMessage.java b/src/android/bluetooth/client/map/BluetoothMapMessage.java
index 6c76bbe..5ce6c4b 100644
--- a/src/android/bluetooth/client/map/BluetoothMapMessage.java
+++ b/src/android/bluetooth/client/map/BluetoothMapMessage.java
@@ -91,8 +91,14 @@
         }
 
         mSubject = attrs.get("subject");
+        String dateTime = attrs.get("datetime");
+        //Handle possible NPE when not able to retreive datetime attribute
+        if(dateTime != null){
+            mDateTime = (new ObexTime(dateTime)).getTime();
+        } else {
+            mDateTime = null;
+        }
 
-        mDateTime = (new ObexTime(attrs.get("datetime"))).getTime();
 
         mSenderName = attrs.get("sender_name");
 
diff --git a/src/android/bluetooth/client/map/BluetoothMasClient.java b/src/android/bluetooth/client/map/BluetoothMasClient.java
index d6d2a1c..7f71693 100644
--- a/src/android/bluetooth/client/map/BluetoothMasClient.java
+++ b/src/android/bluetooth/client/map/BluetoothMasClient.java
@@ -453,13 +453,9 @@
     }
 
     private void sendToClient(int event, boolean success, Object param) {
-        if (success) {
-            mCallback.obtainMessage(event, STATUS_OK, mMas.getMasInstanceId(), param)
-                    .sendToTarget();
-        } else {
-            mCallback.obtainMessage(event, STATUS_FAILED, mMas.getMasInstanceId(), null)
-                    .sendToTarget();
-        }
+        // Send  event, status and notification state for both sucess and failure case.
+        mCallback.obtainMessage(event, success ? STATUS_OK : STATUS_FAILED, mMas.getMasInstanceId(),
+            param).sendToTarget();
     }
 
     private class SocketConnectThread extends Thread {
@@ -547,8 +543,11 @@
         }
 
         public void setPeriod(Date filterBegin, Date filterEnd) {
-            periodBegin = (new ObexTime(filterBegin)).toString();
-            periodEnd = (new ObexTime(filterEnd)).toString();
+        //Handle possible NPE for obexTime constructor utility
+            if(filterBegin != null )
+                periodBegin = (new ObexTime(filterBegin)).toString();
+            if(filterEnd != null)
+                periodEnd = (new ObexTime(filterEnd)).toString();
         }
 
         public void setReadStatus(byte readfilter) {
diff --git a/src/android/bluetooth/client/map/BluetoothMasRequest.java b/src/android/bluetooth/client/map/BluetoothMasRequest.java
index 658a344..0c9c29c 100644
--- a/src/android/bluetooth/client/map/BluetoothMasRequest.java
+++ b/src/android/bluetooth/client/map/BluetoothMasRequest.java
@@ -43,6 +43,7 @@
     protected static final byte OAP_TAGID_NEW_MESSAGE = 0x0d;
     protected static final byte OAP_TAGID_NOTIFICATION_STATUS = 0x0e;
     protected static final byte OAP_TAGID_MAS_INSTANCE_ID = 0x0f;
+    protected static final byte OAP_TAGID_PARAMETER_MASK = 0x10;
     protected static final byte OAP_TAGID_FOLDER_LISTING_SIZE = 0x11;
     protected static final byte OAP_TAGID_MESSAGES_LISTING_SIZE = 0x12;
     protected static final byte OAP_TAGID_SUBJECT_LENGTH = 0x13;
diff --git a/src/android/bluetooth/client/map/BluetoothMasRequestGetFolderListing.java b/src/android/bluetooth/client/map/BluetoothMasRequestGetFolderListing.java
index bd5a2dd..db22ada 100644
--- a/src/android/bluetooth/client/map/BluetoothMasRequestGetFolderListing.java
+++ b/src/android/bluetooth/client/map/BluetoothMasRequestGetFolderListing.java
@@ -43,8 +43,8 @@
         mHeaderSet.setHeader(HeaderSet.TYPE, TYPE);
 
         ObexAppParameters oap = new ObexAppParameters();
-
-        if (maxListCount > 0) {
+        // Allow GetFolderListing for maxListCount value 0 also.
+        if (maxListCount >= 0) {
             oap.add(OAP_TAGID_MAX_LIST_COUNT, (short) maxListCount);
         }
 
diff --git a/src/android/bluetooth/client/map/BluetoothMasRequestGetMessagesListing.java b/src/android/bluetooth/client/map/BluetoothMasRequestGetMessagesListing.java
index d5460f9..2ad167d 100644
--- a/src/android/bluetooth/client/map/BluetoothMasRequestGetMessagesListing.java
+++ b/src/android/bluetooth/client/map/BluetoothMasRequestGetMessagesListing.java
@@ -41,7 +41,6 @@
     public BluetoothMasRequestGetMessagesListing(String folderName, int parameters,
             BluetoothMasClient.MessagesFilter filter, int subjectLength, int maxListCount,
             int listStartOffset) {
-
         if (subjectLength < 0 || subjectLength > 255) {
             throw new IllegalArgumentException("subjectLength should be [0..255]");
         }
@@ -97,8 +96,14 @@
         if (subjectLength != 0) {
             oap.add(OAP_TAGID_SUBJECT_LENGTH, (byte) subjectLength);
         }
-
-        if (maxListCount != 0) {
+        /* Include parameterMask only when specific values are selected,
+         * to avoid IOT specific issue with no paramterMask header support.
+         */
+        if (parameters >  0 ) {
+            oap.add(OAP_TAGID_PARAMETER_MASK, parameters);
+        }
+        // Allow GetMessageListing for maxlistcount value 0 also.
+        if (maxListCount >= 0) {
             oap.add(OAP_TAGID_MAX_LIST_COUNT, (short) maxListCount);
         }
 
@@ -122,8 +127,8 @@
 
         if (oap.exists(OAP_TAGID_MSE_TIME)) {
             String mseTime = oap.getString(OAP_TAGID_MSE_TIME);
-
-            mServerTime = (new ObexTime(mseTime)).getTime();
+            if(mseTime != null )
+               mServerTime = (new ObexTime(mseTime)).getTime();
         }
     }