Skip to content

Commit

Permalink
Merge pull request phonegap#1112 from jwark/remove_plugin_deprecations
Browse files Browse the repository at this point in the history
Fix Android SoftKeyboard & DatePicker Plugin deprecations
  • Loading branch information
devgeeks committed Apr 18, 2013
2 parents 8358157 + e7f9828 commit 253540d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 51 deletions.
62 changes: 24 additions & 38 deletions Android/DatePicker/DatePickerPlugin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package com.phonegap.plugin;
package com.phonegap.plugins;

import java.util.Calendar;
import java.util.Date;
Expand All @@ -19,10 +19,8 @@
import android.widget.DatePicker;
import android.widget.TimePicker;


import org.apache.cordova.DroidGap;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;

/**
* @author ng4e
Expand All @@ -31,35 +29,24 @@
* Rewrote plugin so it it similar to the iOS datepicker plugin and it
* accepts prefilled dates and time
*/
public class DatePickerPlugin extends Plugin {
public class DatePickerPlugin extends CordovaPlugin {

private static final String ACTION_DATE = "date";
private static final String ACTION_TIME = "time";
private final String pluginName = "DatePickerPlugin";

/*
* (non-Javadoc)
*
* @see com.phonegap.api.Plugin#execute(java.lang.String,
* org.json.JSONArray, java.lang.String)
*/
@Override
public PluginResult execute(final String action, final JSONArray data, final String callBackId) {
Log.d(pluginName, "DatePicker called with options: " + data);
PluginResult result = null;

this.show(data, callBackId);
result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);

return result;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
Log.d(pluginName, "DatePicker called with options: " + args);

return this.show(args, callbackContext);
}

public synchronized void show(final JSONArray data, final String callBackId) {
final DatePickerPlugin datePickerPlugin = this;
final Context currentCtx = cordova.getActivity();
public synchronized boolean show(final JSONArray data, final CallbackContext callbackContext) {
final Calendar c = Calendar.getInstance();
final Runnable runnable;
final Context currentCtx = cordova.getActivity();
final DatePickerPlugin datePickerPlugin = this;

String action = "date";

Expand Down Expand Up @@ -98,7 +85,7 @@ public synchronized void show(final JSONArray data, final String callBackId) {
if (ACTION_TIME.equalsIgnoreCase(action)) {
runnable = new Runnable() {
public void run() {
final TimeSetListener timeSetListener = new TimeSetListener(datePickerPlugin, callBackId);
final TimeSetListener timeSetListener = new TimeSetListener(datePickerPlugin, callbackContext);
final TimePickerDialog timeDialog = new TimePickerDialog(currentCtx, timeSetListener, mHour,
mMinutes, true);
timeDialog.show();
Expand All @@ -108,7 +95,7 @@ public void run() {
} else if (ACTION_DATE.equalsIgnoreCase(action)) {
runnable = new Runnable() {
public void run() {
final DateSetListener dateSetListener = new DateSetListener(datePickerPlugin, callBackId);
final DateSetListener dateSetListener = new DateSetListener(datePickerPlugin, callbackContext);
final DatePickerDialog dateDialog = new DatePickerDialog(currentCtx, dateSetListener, mYear,
mMonth, mDay);
dateDialog.show();
Expand All @@ -117,38 +104,38 @@ public void run() {

} else {
Log.d(pluginName, "Unknown action. Only 'date' or 'time' are valid actions");
return;
return false;
}

cordova.getActivity().runOnUiThread(runnable);
return true;
}

private final class DateSetListener implements OnDateSetListener {
private final DatePickerPlugin datePickerPlugin;
private final String callBackId;
private final CallbackContext callbackContext;

private DateSetListener(DatePickerPlugin datePickerPlugin, String callBackId) {
private DateSetListener(DatePickerPlugin datePickerPlugin, CallbackContext callbackContext) {
this.datePickerPlugin = datePickerPlugin;
this.callBackId = callBackId;
this.callbackContext = callbackContext;
}

/**
* Return a string containing the date in the format YYYY/MM/DD
*/
public void onDateSet(final DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) {
String returnDate = year + "/" + (monthOfYear + 1) + "/" + dayOfMonth;
datePickerPlugin.success(new PluginResult(PluginResult.Status.OK, returnDate), callBackId);

callbackContext.success(returnDate);
}
}

private final class TimeSetListener implements OnTimeSetListener {
private final DatePickerPlugin datePickerPlugin;
private final String callBackId;
private final CallbackContext callbackContext;

private TimeSetListener(DatePickerPlugin datePickerPlugin, String callBackId) {
private TimeSetListener(DatePickerPlugin datePickerPlugin, CallbackContext callbackContext) {
this.datePickerPlugin = datePickerPlugin;
this.callBackId = callBackId;
this.callbackContext = callbackContext;
}

/**
Expand All @@ -160,8 +147,7 @@ public void onTimeSet(final TimePicker view, final int hourOfDay, final int minu
date.setHours(hourOfDay);
date.setMinutes(minute);

datePickerPlugin.success(new PluginResult(PluginResult.Status.OK, date.toLocaleString()), callBackId);

callbackContext.success(date.toLocaleString());
}
}

Expand Down
29 changes: 16 additions & 13 deletions Android/SoftKeyboard/SoftKeyBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
import android.content.Context;
import android.view.inputmethod.InputMethodManager;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;

public class SoftKeyBoard extends Plugin {
public class SoftKeyBoard extends CordovaPlugin {

public SoftKeyBoard() {
}

public void showKeyBoard() {
InputMethodManager mgr = (InputMethodManager) this.ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodManager mgr = (InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(webView, InputMethodManager.SHOW_IMPLICIT);

((InputMethodManager) this.ctx.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(webView, 0);
((InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(webView, 0);
}

public void hideKeyBoard() {
InputMethodManager mgr = (InputMethodManager) this.ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodManager mgr = (InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(webView.getWindowToken(), 0);
}

Expand All @@ -31,21 +31,24 @@ public boolean isKeyBoardShowing() {
return (100 < heightDiff); // if more than 100 pixels, its probably a keyboard...
}

public PluginResult execute(String action, JSONArray args, String callbackId) {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
if (action.equals("show")) {
this.showKeyBoard();
return new PluginResult(PluginResult.Status.OK, "done");
callbackContext.success("done");
return true;
}
else if (action.equals("hide")) {
this.hideKeyBoard();
return new PluginResult(PluginResult.Status.OK);
callbackContext.success();
return true;
}
else if (action.equals("isShowing")) {
return new PluginResult(PluginResult.Status.OK, this.isKeyBoardShowing());
else if (action.equals("isShowing")) {
callbackContext.success(Boolean.toString(this.isKeyBoardShowing()));
return true;
}
else {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
return false;
}
}
}

0 comments on commit 253540d

Please sign in to comment.