Skip to content

Commit

Permalink
added JavaScript Event trigger for Android, fixed some indentation, a…
Browse files Browse the repository at this point in the history
…dded webview static getter method to plugin class, added permission to plugin.xml
  • Loading branch information
aogilvie committed Dec 19, 2013
1 parent 9fd8b7f commit c6a68cb
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public void onReceive(Context context, Intent intent) {
try {
notificationId = Integer.parseInt(bundle.getString(NOTIFICATION_ID));
} catch (Exception e) {
Log.d("AlarmReceiver", "Unable to process alarm with id: " + bundle.getString(NOTIFICATION_ID));
Log.e("AlarmReceiver", "Unable to process alarm with id: " + bundle.getString(NOTIFICATION_ID));
Log.e("LocalNotification", "Unable to process alarm with id: " + bundle.getString(NOTIFICATION_ID));
}

// Construct the notification and notificationManager objects
Expand All @@ -64,5 +65,9 @@ public void onReceive(Context context, Intent intent) {
*/
notificationMgr.notify(notificationId, notification);

// Notify JavaScript
// NOTE: It is very difficult to know if our app is in the background or not, we will send active: true
// the most important thing is to inform JavaScript that the event was triggered.
LocalNotification.getCordovaWebView().sendJavascript("cordova.fireDocumentEvent('receivedLocalNotification', { active : true, notificationId : " + notificationId + " })");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,39 @@ public class AlarmRestoreOnBoot extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
final String pluginName = LocalNotification.TAG;
// Obtain alarm details form Shared Preferences
final SharedPreferences alarmSettings = context.getSharedPreferences(pluginName, Context.MODE_PRIVATE);
final Map<String, ?> allAlarms = alarmSettings.getAll();
final Set<String> alarmIds = allAlarms.keySet();

/*
* For each alarm, parse its alarm options and register is again with
* the Alarm Manager
*/
for (String alarmId : alarmIds) {
try {
final AlarmHelper alarm = new AlarmHelper(context);
final JSONArray alarmDetails = new JSONArray(alarmSettings.getString(alarmId, ""));
final AlarmOptions options = new AlarmOptions();

options.parseOptions(alarmDetails);

final String title = options.getAlarmTitle();
final String subTitle = options.getAlarmSubTitle();
final String ticker = options.getAlarmTicker();
final String id = options.getNotificationId();
final Long cal = options.getCal().getTimeInMillis();

alarm.addAlarm(title, subTitle, ticker, id, cal);

} catch (JSONException e) {
Log.d(pluginName,
"AlarmRestoreOnBoot: Error while restoring alarm details after reboot: " + e.toString());
}

Log.d(pluginName, "AlarmRestoreOnBoot: Successfully restored alarms upon reboot");
}
final String pluginName = LocalNotification.TAG;

// Obtain alarm details form Shared Preferences
final SharedPreferences alarmSettings = context.getSharedPreferences(pluginName, Context.MODE_PRIVATE);
final Map<String, ?> allAlarms = alarmSettings.getAll();
final Set<String> alarmIds = allAlarms.keySet();

/*
* For each alarm, parse its alarm options and register is again with
* the Alarm Manager
*/
for (String alarmId : alarmIds) {
try {
final AlarmHelper alarm = new AlarmHelper(context);
final JSONArray alarmDetails = new JSONArray(alarmSettings.getString(alarmId, ""));
final AlarmOptions options = new AlarmOptions();

options.parseOptions(alarmDetails);

final String title = options.getAlarmTitle();
final String subTitle = options.getAlarmSubTitle();
final String ticker = options.getAlarmTicker();
final String id = options.getNotificationId();
final Long cal = options.getCal().getTimeInMillis();

alarm.addAlarm(title, subTitle, ticker, id, cal);

} catch (JSONException e) {
Log.d(pluginName,
"AlarmRestoreOnBoot: Error while restoring alarm details after reboot: " + e.toString());
}

Log.d(pluginName, "AlarmRestoreOnBoot: Successfully restored alarms upon reboot");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;

Expand All @@ -22,19 +23,26 @@
public class LocalNotification extends CordovaPlugin {

public static final String TAG = "LocalNotification";
public static CordovaWebView _webview;

/**
* Delegate object that does the actual alarm registration. Is reused by the
* AlarmRestoreOnBoot class.
*/
private AlarmHelper alarm = null;

@Override
public void initialize(org.apache.cordova.CordovaInterface cordova, org.apache.cordova.CordovaWebView webView) {
// Keep a pointer to the WebView so we can emit JS Event when getting a notification
_webview = webView;
super.initialize(cordova, webView);
}

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
// public PluginResult execute(String action, JSONArray optionsArr, String callBackId) {
alarm = new AlarmHelper(cordova.getActivity().getApplicationContext());
Log.d(TAG, "Plugin execute called with action: " + action);

Log.d(TAG, "Plugin execute called with action: " + action);
alarm = new AlarmHelper(cordova.getActivity().getApplicationContext());

/*
* Determine which action of the plugin needs to be invoked
*/
Expand Down Expand Up @@ -81,8 +89,6 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
*
* @param callbackContext
* Callback context of the request from Cordova
* @param repeatDaily
* If true, the alarm interval will be set to one day.
* @param alarmTitle
* The title of the alarm as shown in the Android notification
* panel
Expand Down Expand Up @@ -158,6 +164,10 @@ public Boolean cancelAllNotifications(CallbackContext callbackContext) {
}
}

public static CordovaWebView getCordovaWebView() {
return _webview;
}

/**
* Persist the information of this alarm to the Android Shared Preferences.
* This will allow the application to restore the alarm upon device reboot.
Expand Down
19 changes: 2 additions & 17 deletions example/android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,8 @@
android:anyDensity="true"
/>

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />


<application android:icon="@drawable/icon" android:label="@string/app_name"
android:hardwareAccelerated="true"
Expand All @@ -58,7 +43,7 @@
</intent-filter>
</activity>
<receiver android:name="jp.wizcorp.phonegap.plugin.localNotification.AlarmReceiver" ></receiver>
<receiver android:name="jp.wizcrop.phonegap.plugin.localNotification.AlarmRestoreOnBoot" >
<receiver android:name="jp.wizcorp.phonegap.plugin.localNotification.AlarmRestoreOnBoot" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public void onReceive(Context context, Intent intent) {
try {
notificationId = Integer.parseInt(bundle.getString(NOTIFICATION_ID));
} catch (Exception e) {
Log.d("AlarmReceiver", "Unable to process alarm with id: " + bundle.getString(NOTIFICATION_ID));
Log.e("AlarmReceiver", "Unable to process alarm with id: " + bundle.getString(NOTIFICATION_ID));
Log.e("LocalNotification", "Unable to process alarm with id: " + bundle.getString(NOTIFICATION_ID));
}

// Construct the notification and notificationManager objects
Expand All @@ -64,5 +65,9 @@ public void onReceive(Context context, Intent intent) {
*/
notificationMgr.notify(notificationId, notification);

// Notify JavaScript
// NOTE: It is very difficult to know if our app is in the background or not, we will send active: true
// the most important thing is to inform JavaScript that the event was triggered.
LocalNotification.getCordovaWebView().sendJavascript("cordova.fireDocumentEvent('receivedLocalNotification', { active : true, notificationId : " + notificationId + " })");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,39 @@ public class AlarmRestoreOnBoot extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
final String pluginName = LocalNotification.TAG;
// Obtain alarm details form Shared Preferences
final SharedPreferences alarmSettings = context.getSharedPreferences(pluginName, Context.MODE_PRIVATE);
final Map<String, ?> allAlarms = alarmSettings.getAll();
final Set<String> alarmIds = allAlarms.keySet();

/*
* For each alarm, parse its alarm options and register is again with
* the Alarm Manager
*/
for (String alarmId : alarmIds) {
try {
final AlarmHelper alarm = new AlarmHelper(context);
final JSONArray alarmDetails = new JSONArray(alarmSettings.getString(alarmId, ""));
final AlarmOptions options = new AlarmOptions();

options.parseOptions(alarmDetails);

final String title = options.getAlarmTitle();
final String subTitle = options.getAlarmSubTitle();
final String ticker = options.getAlarmTicker();
final String id = options.getNotificationId();
final Long cal = options.getCal().getTimeInMillis();

alarm.addAlarm(title, subTitle, ticker, id, cal);

} catch (JSONException e) {
Log.d(pluginName,
"AlarmRestoreOnBoot: Error while restoring alarm details after reboot: " + e.toString());
}

Log.d(pluginName, "AlarmRestoreOnBoot: Successfully restored alarms upon reboot");
}
final String pluginName = LocalNotification.TAG;

// Obtain alarm details form Shared Preferences
final SharedPreferences alarmSettings = context.getSharedPreferences(pluginName, Context.MODE_PRIVATE);
final Map<String, ?> allAlarms = alarmSettings.getAll();
final Set<String> alarmIds = allAlarms.keySet();

/*
* For each alarm, parse its alarm options and register is again with
* the Alarm Manager
*/
for (String alarmId : alarmIds) {
try {
final AlarmHelper alarm = new AlarmHelper(context);
final JSONArray alarmDetails = new JSONArray(alarmSettings.getString(alarmId, ""));
final AlarmOptions options = new AlarmOptions();

options.parseOptions(alarmDetails);

final String title = options.getAlarmTitle();
final String subTitle = options.getAlarmSubTitle();
final String ticker = options.getAlarmTicker();
final String id = options.getNotificationId();
final Long cal = options.getCal().getTimeInMillis();

alarm.addAlarm(title, subTitle, ticker, id, cal);

} catch (JSONException e) {
Log.d(pluginName,
"AlarmRestoreOnBoot: Error while restoring alarm details after reboot: " + e.toString());
}

Log.d(pluginName, "AlarmRestoreOnBoot: Successfully restored alarms upon reboot");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;

Expand All @@ -22,19 +23,26 @@
public class LocalNotification extends CordovaPlugin {

public static final String TAG = "LocalNotification";
public static CordovaWebView _webview;

/**
* Delegate object that does the actual alarm registration. Is reused by the
* AlarmRestoreOnBoot class.
*/
private AlarmHelper alarm = null;

@Override
public void initialize(org.apache.cordova.CordovaInterface cordova, org.apache.cordova.CordovaWebView webView) {
// Keep a pointer to the WebView so we can emit JS Event when getting a notification
_webview = webView;
super.initialize(cordova, webView);
}

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
// public PluginResult execute(String action, JSONArray optionsArr, String callBackId) {
alarm = new AlarmHelper(cordova.getActivity().getApplicationContext());
Log.d(TAG, "Plugin execute called with action: " + action);

Log.d(TAG, "Plugin execute called with action: " + action);
alarm = new AlarmHelper(cordova.getActivity().getApplicationContext());

/*
* Determine which action of the plugin needs to be invoked
*/
Expand Down Expand Up @@ -81,8 +89,6 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
*
* @param callbackContext
* Callback context of the request from Cordova
* @param repeatDaily
* If true, the alarm interval will be set to one day.
* @param alarmTitle
* The title of the alarm as shown in the Android notification
* panel
Expand Down Expand Up @@ -158,6 +164,10 @@ public Boolean cancelAllNotifications(CallbackContext callbackContext) {
}
}

public static CordovaWebView getCordovaWebView() {
return _webview;
}

/**
* Persist the information of this alarm to the Android Shared Preferences.
* This will allow the application to restore the alarm upon device reboot.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Licensed to the Apache Software Foundation (ASF) under one

package org.apache.cordova.example;

import android.app.Activity;
import android.os.Bundle;
import org.apache.cordova.*;

Expand Down
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
</config-file>

Expand Down

0 comments on commit c6a68cb

Please sign in to comment.