Bug 674725 - Part O - Receiving SMS: Android backend. r=cjones

This commit is contained in:
Mounir Lamouri 2011-11-24 14:10:20 +01:00
parent cdeea80ab9
commit ff4a7f0cf3
7 changed files with 89 additions and 0 deletions

View File

@ -64,6 +64,7 @@ EXPORTS_mozilla/dom/sms = \
SmsServiceFactory.h \ SmsServiceFactory.h \
Constants.h \ Constants.h \
Types.h \ Types.h \
SmsMessage.h \
$(NULL) $(NULL)
CPPSRCS = \ CPPSRCS = \

View File

@ -23,6 +23,7 @@
<!-- WebSMS --> <!-- WebSMS -->
<uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-feature android:name="android.hardware.location" android:required="false"/> <uses-feature android:name="android.hardware.location" android:required="false"/>
<uses-feature android:name="android.hardware.location.gps" android:required="false"/> <uses-feature android:name="android.hardware.location.gps" android:required="false"/>

View File

@ -88,6 +88,7 @@ abstract public class GeckoApp
private IntentFilter mConnectivityFilter; private IntentFilter mConnectivityFilter;
private BroadcastReceiver mConnectivityReceiver; private BroadcastReceiver mConnectivityReceiver;
private BroadcastReceiver mBatteryReceiver; private BroadcastReceiver mBatteryReceiver;
private BroadcastReceiver mSmsReceiver;
enum LaunchState {PreLaunch, Launching, WaitForDebugger, enum LaunchState {PreLaunch, Launching, WaitForDebugger,
Launched, GeckoRunning, GeckoExiting}; Launched, GeckoRunning, GeckoExiting};
@ -413,6 +414,11 @@ abstract public class GeckoApp
mBatteryReceiver = new GeckoBatteryManager(); mBatteryReceiver = new GeckoBatteryManager();
registerReceiver(mBatteryReceiver, batteryFilter); registerReceiver(mBatteryReceiver, batteryFilter);
IntentFilter smsFilter = new IntentFilter();
smsFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
mSmsReceiver = new GeckoSmsManager();
registerReceiver(mSmsReceiver, smsFilter);
if (!checkAndSetLaunchState(LaunchState.PreLaunch, if (!checkAndSetLaunchState(LaunchState.PreLaunch,
LaunchState.Launching)) LaunchState.Launching))
return; return;
@ -565,6 +571,7 @@ abstract public class GeckoApp
public void onDestroy() public void onDestroy()
{ {
Log.i(LOG_FILE_NAME, "destroy"); Log.i(LOG_FILE_NAME, "destroy");
// Tell Gecko to shutting down; we'll end up calling System.exit() // Tell Gecko to shutting down; we'll end up calling System.exit()
// in onXreExit. // in onXreExit.
if (isFinishing()) if (isFinishing())
@ -572,6 +579,7 @@ abstract public class GeckoApp
super.onDestroy(); super.onDestroy();
unregisterReceiver(mSmsReceiver);
unregisterReceiver(mBatteryReceiver); unregisterReceiver(mBatteryReceiver);
} }

View File

@ -118,6 +118,8 @@ public class GeckoAppShell
public static native void notifyBatteryChange(double aLevel, boolean aCharging, double aRemainingTime); public static native void notifyBatteryChange(double aLevel, boolean aCharging, double aRemainingTime);
public static native void notifySmsReceived(String aSender, String aBody, long aTimestamp);
// A looper thread, accessed by GeckoAppShell.getHandler // A looper thread, accessed by GeckoAppShell.getHandler
private static class LooperThread extends Thread { private static class LooperThread extends Thread {
public SynchronousQueue<Handler> mHandlerQueue = public SynchronousQueue<Handler> mHandlerQueue =

View File

@ -41,12 +41,47 @@ import java.util.ArrayList;
import android.util.Log; import android.util.Log;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.os.Bundle;
import android.telephony.SmsManager; import android.telephony.SmsManager;
import android.telephony.SmsMessage;
public class GeckoSmsManager public class GeckoSmsManager
extends BroadcastReceiver
{ {
final static int kMaxMessageSize = 160; final static int kMaxMessageSize = 160;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
// TODO: Try to find the receiver number to be able to populate
// SmsMessage.receiver.
// TODO: Get the id and the date from the stock app saved message.
// Using the stock app saved message require us to wait for it to
// be saved which can lead to race conditions.
Bundle bundle = intent.getExtras();
if (bundle == null) {
return;
}
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i=0; i<pdus.length; ++i) {
SmsMessage msg = SmsMessage.createFromPdu((byte[])pdus[i]);
GeckoAppShell.notifySmsReceived(msg.getDisplayOriginatingAddress(),
msg.getDisplayMessageBody(),
System.currentTimeMillis());
}
}
}
public static int getNumberOfMessagesForText(String aText) { public static int getNumberOfMessagesForText(String aText) {
return SmsManager.getDefault().divideMessage(aText).size(); return SmsManager.getDefault().divideMessage(aText).size();
} }

View File

@ -299,6 +299,7 @@ SHELL_WRAPPER1(cameraCallbackBridge, jbyteArray)
SHELL_WRAPPER1(notifyUriVisited, jstring) SHELL_WRAPPER1(notifyUriVisited, jstring)
SHELL_WRAPPER3(notifyBatteryChange, jdouble, jboolean, jdouble); SHELL_WRAPPER3(notifyBatteryChange, jdouble, jboolean, jdouble);
SHELL_WRAPPER1_WITH_RETURN(canCreateFixupURI, bool, jstring); SHELL_WRAPPER1_WITH_RETURN(canCreateFixupURI, bool, jstring);
SHELL_WRAPPER3(notifySmsReceived, jstring, jstring, jlong);
static void * xul_handle = NULL; static void * xul_handle = NULL;
static time_t apk_mtime = 0; static time_t apk_mtime = 0;
@ -704,6 +705,7 @@ loadLibs(const char *apkName)
GETFUNC(notifyUriVisited); GETFUNC(notifyUriVisited);
GETFUNC(notifyBatteryChange); GETFUNC(notifyBatteryChange);
GETFUNC(canCreateFixupURI); GETFUNC(canCreateFixupURI);
GETFUNC(notifySmsReceived);
#undef GETFUNC #undef GETFUNC
sStartupTimeline = (uint64_t *)__wrap_dlsym(xul_handle, "_ZN7mozilla15StartupTimeline16sStartupTimelineE"); sStartupTimeline = (uint64_t *)__wrap_dlsym(xul_handle, "_ZN7mozilla15StartupTimeline16sStartupTimelineE");
gettimeofday(&t1, 0); gettimeofday(&t1, 0);

View File

@ -62,8 +62,13 @@
#include "nsExceptionHandler.h" #include "nsExceptionHandler.h"
#endif #endif
#include "mozilla/dom/sms/SmsMessage.h"
#include "mozilla/dom/sms/Constants.h"
#include "mozilla/dom/sms/Types.h"
#include "mozilla/dom/sms/PSms.h"
using namespace mozilla; using namespace mozilla;
using namespace mozilla::dom::sms;
/* Forward declare all the JNI methods as extern "C" */ /* Forward declare all the JNI methods as extern "C" */
@ -83,6 +88,7 @@ extern "C" {
NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_notifyUriVisited(JNIEnv *, jclass, jstring uri); NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_notifyUriVisited(JNIEnv *, jclass, jstring uri);
NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_notifyBatteryChange(JNIEnv* jenv, jclass, jdouble, jboolean, jdouble); NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_notifyBatteryChange(JNIEnv* jenv, jclass, jdouble, jboolean, jdouble);
NS_EXPORT bool JNICALL Java_org_mozilla_gecko_GeckoAppShell_canCreateFixupURI(JNIEnv* jenv, jclass, jstring text); NS_EXPORT bool JNICALL Java_org_mozilla_gecko_GeckoAppShell_canCreateFixupURI(JNIEnv* jenv, jclass, jstring text);
NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_notifySmsReceived(JNIEnv* jenv, jclass, jstring, jstring, jlong);
} }
@ -251,3 +257,37 @@ Java_org_mozilla_gecko_GeckoAppShell_canCreateFixupURI(JNIEnv* jenv, jclass, jst
return AndroidBridge::Bridge()->CanCreateFixupURI(uriString); return AndroidBridge::Bridge()->CanCreateFixupURI(uriString);
} }
NS_EXPORT void JNICALL
Java_org_mozilla_gecko_GeckoAppShell_notifySmsReceived(JNIEnv* jenv, jclass,
jstring aSender,
jstring aBody,
jlong aTimestamp)
{
class NotifySmsReceivedRunnable : public nsRunnable {
public:
NotifySmsReceivedRunnable(const SmsMessageData& aMessageData)\
: mMessageData(aMessageData)
{}
NS_IMETHODIMP Run() {
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
if (!obs) {
return NS_OK;
}
nsCOMPtr<nsIDOMMozSmsMessage> message = new SmsMessage(mMessageData);
obs->NotifyObservers(message, kSmsReceivedObserverTopic, nsnull);
return NS_OK;
}
private:
SmsMessageData mMessageData;
};
SmsMessageData message(0, eDeliveryState_Received, nsJNIString(aSender, jenv), EmptyString(),
nsJNIString(aBody, jenv), aTimestamp);
nsCOMPtr<nsIRunnable> runnable = new NotifySmsReceivedRunnable(message);
NS_DispatchToMainThread(runnable);
}