Skip to content

Commit

Permalink
GH-122 android: Cleanup Dialog plugin code (#123)
Browse files Browse the repository at this point in the history
### Platforms affected
Android

### Description
1. Organizing Notification imports.
2. Externalizing Action strings in private static finals.
3. Externalizing Beep timeout and wait time in private static finals.

### Testing
Test plugin and everything works fine.

- [x] I've run the tests to see all new and existing tests pass
- [x] Commit is prefixed with `(platform)` if this change only applies to one platform (e.g. `(android)`)
- [x] If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct [keyword to close issues using keywords](https://help.github.com/articles/closing-issues-using-keywords/))
  • Loading branch information
hazems authored and janpio committed Mar 5, 2019
1 parent d5f09ce commit 419a838
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions src/android/Notification.java
Expand Up @@ -18,15 +18,6 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
package org.apache.cordova.dialogs;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
Expand All @@ -39,6 +30,15 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.widget.EditText;
import android.widget.TextView;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
* This class provides access to notifications on the device.
Expand All @@ -52,6 +52,19 @@ public class Notification extends CordovaPlugin {

private static final String LOG_TAG = "Notification";

private static final String ACTION_BEEP = "beep";
private static final String ACTION_ALERT = "alert";
private static final String ACTION_CONFIRM = "confirm";
private static final String ACTION_PROMPT = "prompt";
private static final String ACTION_ACTIVITY_START = "activityStart";
private static final String ACTION_ACTIVITY_STOP = "activityStop";
private static final String ACTION_PROGRESS_START = "progressStart";
private static final String ACTION_PROGRESS_VALUE = "progressValue";
private static final String ACTION_PROGRESS_STOP = "progressStop";

private static final long BEEP_TIMEOUT = 5000;
private static final long BEEP_WAIT_TINE = 100;

public int confirmResult = -1;
public ProgressDialog spinnerDialog = null;
public ProgressDialog progressDialog = null;
Expand All @@ -77,36 +90,36 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
* crashing the app. Just return true here since false should only
* be returned in the event of an invalid action.
*/
if(this.cordova.getActivity().isFinishing()) return true;
if (this.cordova.getActivity().isFinishing()) return true;

if (action.equals("beep")) {
if (action.equals(ACTION_BEEP)) {
this.beep(args.getLong(0));
}
else if (action.equals("alert")) {
else if (action.equals(ACTION_ALERT)) {
this.alert(args.getString(0), args.getString(1), args.getString(2), callbackContext);
return true;
}
else if (action.equals("confirm")) {
else if (action.equals(ACTION_CONFIRM)) {
this.confirm(args.getString(0), args.getString(1), args.getJSONArray(2), callbackContext);
return true;
}
else if (action.equals("prompt")) {
else if (action.equals(ACTION_PROMPT)) {
this.prompt(args.getString(0), args.getString(1), args.getJSONArray(2), args.getString(3), callbackContext);
return true;
}
else if (action.equals("activityStart")) {
else if (action.equals(ACTION_ACTIVITY_START)) {
this.activityStart(args.getString(0), args.getString(1));
}
else if (action.equals("activityStop")) {
else if (action.equals(ACTION_ACTIVITY_STOP)) {
this.activityStop();
}
else if (action.equals("progressStart")) {
else if (action.equals(ACTION_PROGRESS_START)) {
this.progressStart(args.getString(0), args.getString(1));
}
else if (action.equals("progressValue")) {
else if (action.equals(ACTION_PROGRESS_VALUE)) {
this.progressValue(args.getInt(0));
}
else if (action.equals("progressStop")) {
else if (action.equals(ACTION_PROGRESS_STOP)) {
this.progressStop();
}
else {
Expand Down Expand Up @@ -137,11 +150,11 @@ public void run() {
if (notification != null) {
for (long i = 0; i < count; ++i) {
notification.play();
long timeout = 5000;
long timeout = BEEP_TIMEOUT;
while (notification.isPlaying() && (timeout > 0)) {
timeout = timeout - 100;
timeout = timeout - BEEP_WAIT_TINE;
try {
Thread.sleep(100);
Thread.sleep(BEEP_WAIT_TINE);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Expand All @@ -165,7 +178,7 @@ public synchronized void alert(final String message, final String title, final S
Runnable runnable = new Runnable() {
public void run() {

AlertDialog.Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
dlg.setMessage(message);
dlg.setTitle(title);
dlg.setCancelable(true);
Expand Down Expand Up @@ -205,7 +218,7 @@ public synchronized void confirm(final String message, final String title, final

Runnable runnable = new Runnable() {
public void run() {
AlertDialog.Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
dlg.setMessage(message);
dlg.setTitle(title);
dlg.setCancelable(true);
Expand Down Expand Up @@ -295,7 +308,7 @@ But for some android versions is not visible (for example 5.1.1).
int promptInputTextColor = resources.getColor(android.R.color.primary_text_light);
promptInput.setTextColor(promptInputTextColor);
promptInput.setText(defaultText);
AlertDialog.Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
dlg.setMessage(message);
dlg.setTitle(title);
dlg.setCancelable(true);
Expand Down Expand Up @@ -481,12 +494,12 @@ public synchronized void progressStop() {
}

@SuppressLint("NewApi")
private AlertDialog.Builder createDialog(CordovaInterface cordova) {
private Builder createDialog(CordovaInterface cordova) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
return new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
return new Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
} else {
return new AlertDialog.Builder(cordova.getActivity());
return new Builder(cordova.getActivity());
}
}

Expand Down

0 comments on commit 419a838

Please sign in to comment.