Bug 674725 - Part I - Implement mozSms.getNumberOfMessagesForText(). r=smaug,cjones
authorMounir Lamouri <mounir.lamouri@gmail.com>
Fri, 25 Nov 2011 10:48:51 +0100
changeset 81573 fcf8b53367112da414349d8eaf36eee47f1c613b
parent 81572 d94567fea9de50d19f1d4610c025cd7b1d80d34f
child 81574 423c6548b52253327ed6c617b1bb25712d202aa5
push idunknown
push userunknown
push dateunknown
reviewerssmaug, cjones
bugs674725
milestone11.0a1
Bug 674725 - Part I - Implement mozSms.getNumberOfMessagesForText(). r=smaug,cjones
dom/sms/interfaces/nsIDOMSmsManager.idl
dom/sms/interfaces/nsISmsService.idl
dom/sms/src/SmsManager.cpp
dom/sms/src/android/SmsService.cpp
dom/sms/src/fallback/SmsService.cpp
dom/sms/src/ipc/PSms.ipdl
dom/sms/src/ipc/SmsIPCService.cpp
dom/sms/src/ipc/SmsParent.cpp
dom/sms/src/ipc/SmsParent.h
embedding/android/GeckoAppShell.java
embedding/android/GeckoSmsManager.java
embedding/android/Makefile.in
widget/src/android/AndroidBridge.cpp
widget/src/android/AndroidBridge.h
--- a/dom/sms/interfaces/nsIDOMSmsManager.idl
+++ b/dom/sms/interfaces/nsIDOMSmsManager.idl
@@ -31,12 +31,13 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsISupports.idl"
 
-[scriptable, function, uuid(562eade6-6dee-4c0a-bee9-bfc7f2bcadbe)]
+[scriptable, function, uuid(acff4eca-ae08-4c4a-bc6d-34a960134314)]
 interface nsIDOMMozSmsManager : nsISupports
 {
+  unsigned short getNumberOfMessagesForText(in DOMString text);
 };
--- a/dom/sms/interfaces/nsISmsService.idl
+++ b/dom/sms/interfaces/nsISmsService.idl
@@ -40,9 +40,10 @@
 #define NS_SMSSERVICE_CID { 0xbada3cb8, 0xa568, 0x4dff, { 0xb5, 0x43, 0x52, 0xbb, 0xb3, 0x14, 0x31, 0x21 } }
 #define SMSSERVICE_CONTRACTID "@mozilla.org/sms/smsservice;1"
 %}
 
 [scriptable, function, uuid(abc95960-c689-4985-a226-7c96d27e2cd8)]
 interface nsISmsService : nsISupports
 {
   boolean hasSupport();
+  unsigned short getNumberOfMessagesForText(in DOMString text);
 };
--- a/dom/sms/src/SmsManager.cpp
+++ b/dom/sms/src/SmsManager.cpp
@@ -32,27 +32,39 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "SmsManager.h"
 #include "nsIDOMClassInfo.h"
+#include "nsISmsService.h"
 
 DOMCI_DATA(MozSmsManager, mozilla::dom::sms::SmsManager)
 
 namespace mozilla {
 namespace dom {
 namespace sms {
 
 NS_INTERFACE_MAP_BEGIN(SmsManager)
   NS_INTERFACE_MAP_ENTRY(nsIDOMMozSmsManager)
   NS_INTERFACE_MAP_ENTRY(nsISupports)
   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozSmsManager)
 NS_INTERFACE_MAP_END
 
 NS_IMPL_ADDREF(SmsManager)
 NS_IMPL_RELEASE(SmsManager)
 
+NS_IMETHODIMP
+SmsManager::GetNumberOfMessagesForText(const nsAString& aText, PRUint16* aResult)
+{
+  nsCOMPtr<nsISmsService> smsService = do_GetService(SMSSERVICE_CONTRACTID);
+  NS_ENSURE_TRUE(smsService, NS_OK);
+
+  smsService->GetNumberOfMessagesForText(aText, aResult);
+
+  return NS_OK;
+}
+
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/android/SmsService.cpp
+++ b/dom/sms/src/android/SmsService.cpp
@@ -31,25 +31,38 @@
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "SmsService.h"
+#include "AndroidBridge.h"
 
 namespace mozilla {
 namespace dom {
 namespace sms {
 
 NS_IMPL_ISUPPORTS1(SmsService, nsISmsService)
 
 NS_IMETHODIMP
 SmsService::HasSupport(bool* aHasSupport)
 {
   *aHasSupport = true;
   return NS_OK;
 }
 
+NS_IMETHODIMP
+SmsService::GetNumberOfMessagesForText(const nsAString& aText, PRUint16* aResult)
+{
+  if (!AndroidBridge::Bridge()) {
+    *aResult = 0;
+    return NS_OK;
+  }
+
+  *aResult = AndroidBridge::Bridge()->GetNumberOfMessagesForText(aText);
+  return NS_OK;
+}
+
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/fallback/SmsService.cpp
+++ b/dom/sms/src/fallback/SmsService.cpp
@@ -45,11 +45,19 @@ NS_IMPL_ISUPPORTS1(SmsService, nsISmsSer
 
 NS_IMETHODIMP
 SmsService::HasSupport(bool* aHasSupport)
 {
   *aHasSupport = false;
   return NS_OK;
 }
 
+NS_IMETHODIMP
+SmsService::GetNumberOfMessagesForText(const nsAString& aText, PRUint16* aResult)
+{
+  NS_ERROR("We should not be here!");
+  *aResult = 0;
+  return NS_OK;
+}
+
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/ipc/PSms.ipdl
+++ b/dom/sms/src/ipc/PSms.ipdl
@@ -45,14 +45,17 @@ namespace sms {
 
 sync protocol PSms {
     manager PContent;
 
 parent:
     sync HasSupport()
         returns (bool aHasSupport);
 
+    sync GetNumberOfMessagesForText(nsString aText)
+        returns (PRUint16 aNumber);
+
     __delete__();
 };
 
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/ipc/SmsIPCService.cpp
+++ b/dom/sms/src/ipc/SmsIPCService.cpp
@@ -61,11 +61,19 @@ SmsIPCService::GetSmsChild()
 NS_IMETHODIMP
 SmsIPCService::HasSupport(bool* aHasSupport)
 {
   GetSmsChild()->SendHasSupport(aHasSupport);
 
   return NS_OK;
 }
 
+NS_IMETHODIMP
+SmsIPCService::GetNumberOfMessagesForText(const nsAString& aText, PRUint16* aResult)
+{
+  GetSmsChild()->SendGetNumberOfMessagesForText(nsString(aText), aResult);
+
+  return NS_OK;
+}
+
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/ipc/SmsParent.cpp
+++ b/dom/sms/src/ipc/SmsParent.cpp
@@ -49,11 +49,23 @@ SmsParent::RecvHasSupport(bool* aHasSupp
 
   nsCOMPtr<nsISmsService> smsService = do_GetService(SMSSERVICE_CONTRACTID);
   NS_ENSURE_TRUE(smsService, true);
 
   smsService->HasSupport(aHasSupport);
   return true;
 }
 
+bool
+SmsParent::RecvGetNumberOfMessagesForText(const nsString& aText, PRUint16* aResult)
+{
+  *aResult = 0;
+
+  nsCOMPtr<nsISmsService> smsService = do_GetService(SMSSERVICE_CONTRACTID);
+  NS_ENSURE_TRUE(smsService, true);
+
+  smsService->GetNumberOfMessagesForText(aText, aResult);
+  return true;
+}
+
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/ipc/SmsParent.h
+++ b/dom/sms/src/ipc/SmsParent.h
@@ -42,15 +42,16 @@
 
 namespace mozilla {
 namespace dom {
 namespace sms {
 
 class SmsParent : public PSmsParent
 {
   NS_OVERRIDE virtual bool RecvHasSupport(bool* aHasSupport);
+  NS_OVERRIDE virtual bool RecvGetNumberOfMessagesForText(const nsString& aText, PRUint16* aResult);
 };
 
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_sms_SmsParent_h
--- a/embedding/android/GeckoAppShell.java
+++ b/embedding/android/GeckoAppShell.java
@@ -1643,9 +1643,13 @@ public class GeckoAppShell
 
     public static void disableBatteryNotifications() {
         GeckoBatteryManager.disableNotifications();
     }
 
     public static double[] getCurrentBatteryInformation() {
         return GeckoBatteryManager.getCurrentInformation();
     }
+
+    public static int getNumberOfMessagesForText(String aText) {
+        return GeckoSmsManager.getNumberOfMessagesForText(aText);
+    }
 }
new file mode 100644
--- /dev/null
+++ b/embedding/android/GeckoSmsManager.java
@@ -0,0 +1,51 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Mozilla Android code.
+ *
+ * The Initial Developer of the Original Code is Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2011
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+package org.mozilla.gecko;
+
+import java.util.ArrayList;
+
+import android.util.Log;
+
+import android.telephony.SmsManager;
+
+public class GeckoSmsManager
+{
+  public static int getNumberOfMessagesForText(String aText) {
+    return SmsManager.getDefault().divideMessage(aText).size();
+  }
+}
--- a/embedding/android/Makefile.in
+++ b/embedding/android/Makefile.in
@@ -50,16 +50,17 @@ JAVAFILES = \
   GeckoAppShell.java \
   GeckoConnectivityReceiver.java \
   GeckoEvent.java \
   GeckoSurfaceView.java \
   GeckoInputConnection.java \
   AlertNotification.java \
   SurfaceInfo.java \
   GeckoBatteryManager.java \
+  GeckoSmsManager.java \
   $(NULL)
 
 PROCESSEDJAVAFILES = \
   App.java \
   Restarter.java \
   NotificationHandler.java \
   LauncherShortcuts.java \
   $(NULL)
--- a/widget/src/android/AndroidBridge.cpp
+++ b/widget/src/android/AndroidBridge.cpp
@@ -164,16 +164,18 @@ AndroidBridge::Init(JNIEnv *jEnv,
     jDisableBatteryNotifications = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "disableBatteryNotifications", "()V");
     jGetCurrentBatteryInformation = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "getCurrentBatteryInformation", "()[D");
 
     jGetAccessibilityEnabled = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "getAccessibilityEnabled", "()Z");
     jHandleGeckoMessage = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "handleGeckoMessage", "(Ljava/lang/String;)Ljava/lang/String;");
     jCheckUriVisited = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "checkUriVisited", "(Ljava/lang/String;)V");
     jMarkUriVisited = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "markUriVisited", "(Ljava/lang/String;)V");
 
+    jNumberOfMessages = (jmethodID) jEnv->GetStaticMethodID(jGeckoAppShellClass, "getNumberOfMessagesForText", "(Ljava/lang/String;)I");
+
     jEGLContextClass = (jclass) jEnv->NewGlobalRef(jEnv->FindClass("javax/microedition/khronos/egl/EGLContext"));
     jEGL10Class = (jclass) jEnv->NewGlobalRef(jEnv->FindClass("javax/microedition/khronos/egl/EGL10"));
     jEGLSurfaceImplClass = (jclass) jEnv->NewGlobalRef(jEnv->FindClass("com/google/android/gles_jni/EGLSurfaceImpl"));
     jEGLContextImplClass = (jclass) jEnv->NewGlobalRef(jEnv->FindClass("com/google/android/gles_jni/EGLContextImpl"));
     jEGLConfigImplClass = (jclass) jEnv->NewGlobalRef(jEnv->FindClass("com/google/android/gles_jni/EGLConfigImpl"));
     jEGLDisplayImplClass = (jclass) jEnv->NewGlobalRef(jEnv->FindClass("com/google/android/gles_jni/EGLDisplayImpl"));
 
     InitAndroidJavaWrappers(jEnv);
@@ -1352,16 +1354,26 @@ AndroidBridge::MarkURIVisited(const nsAS
 void AndroidBridge::EmitGeckoAccessibilityEvent (PRInt32 eventType, const nsAString& role, const nsAString& text, const nsAString& description, bool enabled, bool checked, bool password) {
     AutoLocalJNIFrame jniFrame;
     jstring jstrRole = mJNIEnv->NewString(nsPromiseFlatString(role).get(), role.Length());
     jstring jstrText = mJNIEnv->NewString(nsPromiseFlatString(text).get(), text.Length());
     jstring jstrDescription = mJNIEnv->NewString(nsPromiseFlatString(description).get(), description.Length());
     mJNIEnv->CallStaticVoidMethod(mGeckoAppShellClass, jEmitGeckoAccessibilityEvent, eventType, jstrRole, jstrText, jstrDescription, enabled, checked, password);
 }
 
+PRUint16
+AndroidBridge::GetNumberOfMessagesForText(const nsAString& aText)
+{
+    ALOG_BRIDGE("AndroidBridge::GetNumberOfMessagesForText");
+
+    AutoLocalJNIFrame jniFrame;
+    jstring jText = GetJNIForThread()->NewString(PromiseFlatString(aText).get(), aText.Length());
+    return GetJNIForThread()->CallStaticIntMethod(mGeckoAppShellClass, jNumberOfMessages, jText);
+}
+
 void *
 AndroidBridge::LockBitmap(jobject bitmap)
 {
     int err;
     void *buf;
 
     if ((err = AndroidBitmap_lockPixels(JNI(), bitmap, &buf)) != 0) {
         ALOG_BRIDGE("AndroidBitmap_lockPixels failed! (error %d)", err);
--- a/widget/src/android/AndroidBridge.h
+++ b/widget/src/android/AndroidBridge.h
@@ -313,16 +313,18 @@ public:
     bool InitCamera(const nsCString& contentType, PRUint32 camera, PRUint32 *width, PRUint32 *height, PRUint32 *fps);
 
     void CloseCamera();
 
     void EnableBatteryNotifications();
     void DisableBatteryNotifications();
     void GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo);
 
+    PRUint16 GetNumberOfMessagesForText(const nsAString& aText);
+
 protected:
     static AndroidBridge *sBridge;
 
     // the global JavaVM
     JavaVM *mJavaVM;
 
     // the JNIEnv for the main thread
     JNIEnv *mJNIEnv;
@@ -396,16 +398,17 @@ protected:
     jmethodID jEnableBatteryNotifications;
     jmethodID jDisableBatteryNotifications;
     jmethodID jGetCurrentBatteryInformation;
     jmethodID jGetAccessibilityEnabled;
     jmethodID jHandleGeckoMessage;
     jmethodID jCheckUriVisited;
     jmethodID jMarkUriVisited;
     jmethodID jEmitGeckoAccessibilityEvent;
+    jmethodID jNumberOfMessages;
 
     // stuff we need for CallEglCreateWindowSurface
     jclass jEGLSurfaceImplClass;
     jclass jEGLContextImplClass;
     jclass jEGLConfigImplClass;
     jclass jEGLDisplayImplClass;
     jclass jEGLContextClass;
     jclass jEGL10Class;