forked from purplecabbage/phonegap-plugins
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request purplecabbage#39 from ng4e/master
Native Android's Date and Time picker plugin
- Loading branch information
Showing
3 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/** | ||
* | ||
*/ | ||
package com.ngapplication.plugin; | ||
|
||
import java.util.Calendar; | ||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import android.app.DatePickerDialog; | ||
import android.app.DatePickerDialog.OnDateSetListener; | ||
import android.app.TimePickerDialog; | ||
import android.app.TimePickerDialog.OnTimeSetListener; | ||
import android.util.Log; | ||
import android.widget.DatePicker; | ||
import android.widget.TimePicker; | ||
|
||
import com.phonegap.api.PhonegapActivity; | ||
import com.phonegap.api.Plugin; | ||
import com.phonegap.api.PluginResult; | ||
import com.phonegap.api.PluginResult.Status; | ||
|
||
/** | ||
* @author ng4e | ||
* | ||
*/ | ||
public class DatePickerPlugin extends Plugin { | ||
|
||
private static final String ACTION_DATE = "date"; | ||
private static final String ACTION_TIME = "time"; | ||
|
||
/* | ||
* (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("DatePickerPlugin", "Plugin Called"); | ||
PluginResult result = null; | ||
|
||
if (ACTION_DATE.equalsIgnoreCase(action)) { | ||
Log.d("DatePickerPluginListener execute", ACTION_DATE); | ||
this.showDatePicker(callBackId); | ||
final PluginResult r = new PluginResult( | ||
PluginResult.Status.NO_RESULT); | ||
r.setKeepCallback(true); | ||
return r; | ||
|
||
} else if (ACTION_TIME.equalsIgnoreCase(action)) { | ||
Log.d("DatePickerPluginListener execute", ACTION_TIME); | ||
this.showTimePicker(callBackId); | ||
final PluginResult r = new PluginResult( | ||
PluginResult.Status.NO_RESULT); | ||
r.setKeepCallback(true); | ||
return r; | ||
|
||
} else { | ||
result = new PluginResult(Status.INVALID_ACTION); | ||
Log.d("DatePickerPlugin", "Invalid action : " + action + " passed"); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public synchronized void showTimePicker(final String callBackId) { | ||
final DatePickerPlugin datePickerPlugin = this; | ||
final PhonegapActivity currentCtx = ctx; | ||
|
||
final Runnable runnable = new Runnable() { | ||
|
||
public void run() { | ||
final TimePickerDialog tpd = new TimePickerDialog(currentCtx, | ||
new OnTimeSetListener() { | ||
|
||
public void onTimeSet(final TimePicker view, | ||
final int hourOfDay, final int minute) { | ||
final JSONObject userChoice = new JSONObject(); | ||
try { | ||
userChoice.put("hour", hourOfDay); | ||
userChoice.put("min", minute); | ||
} catch (final JSONException jsonEx) { | ||
Log.e("showDatePicker", | ||
"Got JSON Exception " | ||
+ jsonEx.getMessage()); | ||
datePickerPlugin.error(new PluginResult( | ||
Status.JSON_EXCEPTION), callBackId); | ||
} | ||
datePickerPlugin.success(new PluginResult( | ||
PluginResult.Status.OK, userChoice), | ||
callBackId); | ||
|
||
} | ||
}, 1, 1, true); | ||
|
||
tpd.show(); | ||
} | ||
}; | ||
ctx.runOnUiThread(runnable); | ||
|
||
} | ||
|
||
public synchronized void showDatePicker(final String callBackId) { | ||
|
||
final DatePickerPlugin datePickerPlugin = this; | ||
final PhonegapActivity currentCtx = ctx; | ||
final Calendar c = Calendar.getInstance(); | ||
final int mYear = c.get(Calendar.YEAR); | ||
final int mMonth = c.get(Calendar.MONTH); | ||
final int mDay = c.get(Calendar.DAY_OF_MONTH); | ||
|
||
final Runnable runnable = new Runnable() { | ||
|
||
public void run() { | ||
final DatePickerDialog dpd = new DatePickerDialog(currentCtx, | ||
new OnDateSetListener() { | ||
|
||
public void onDateSet(final DatePicker view, | ||
final int year, final int monthOfYear, | ||
final int dayOfMonth) { | ||
|
||
final JSONObject userChoice = new JSONObject(); | ||
|
||
try { | ||
userChoice.put("year", year); | ||
userChoice.put("month", monthOfYear); | ||
userChoice.put("day", dayOfMonth); | ||
} catch (final JSONException jsonEx) { | ||
Log.e("showDatePicker", | ||
"Got JSON Exception " | ||
+ jsonEx.getMessage()); | ||
datePickerPlugin.error(new PluginResult( | ||
Status.JSON_EXCEPTION), callBackId); | ||
} | ||
|
||
datePickerPlugin.success(new PluginResult( | ||
PluginResult.Status.OK, userChoice), | ||
callBackId); | ||
|
||
} | ||
}, mYear, mMonth, mDay); | ||
|
||
dpd.show(); | ||
} | ||
}; | ||
ctx.runOnUiThread(runnable); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
This plugin allows you to leave the PhoneGap webview and enter into the native android date and time picker. | ||
Once the user has selected time or date, they will be sent back into the PhoneGap webview with selected value available. | ||
|
||
|
||
How to use: | ||
|
||
Usage: | ||
|
||
window.plugins.datePickerPlugin.showDateOrTime(dataType,successCallback,errorCallback); | ||
|
||
dataType argument takes two value: | ||
'date' : if you need the datePicker | ||
'time' : if you need the timePicker | ||
|
||
For the date picker : Success returns an object with the parameters {day,month,year} filled with selected values. | ||
For the time picker : Success returns an object with the parameters {hour,min} filled with selected values. | ||
|
||
Example: | ||
|
||
document.querySelector("#mypickdatebutton").addEventListener("tap", function() { | ||
window.plugins.datePickerPlugin.showDateOrTime( | ||
'date', | ||
function(r){ | ||
document.getElementById("mydatetargetfield").value = r.day + "/" + r.month + "/" + r.year; | ||
}, | ||
function(e){console.log(e);} | ||
); | ||
}, false); | ||
|
||
document.querySelector("#mypickdatebutton").addEventListener("tap", function() { | ||
window.plugins.datePickerPlugin.showDateOrTime( | ||
'time', | ||
function(r){ | ||
document.getElementById("mytimetargetfield").value = r.hour + "h" + r.min; | ||
}, | ||
function(e){ | ||
console.log(e); | ||
} | ||
); | ||
}, false); | ||
|
||
|
||
|
||
For the current files to work, you'll need to create a package (folders) called com.ngapplication.plugin. | ||
You can change this to whatever you like, just update the datePickerPlugin.js and datePickerPlugin.java. | ||
|
||
datePickerPlugin.js should go in the asset folder and should be referenced in your index.html file. | ||
|
||
|
||
Limitations: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* | ||
* @return Object literal singleton instance of DatePicker | ||
*/ | ||
var DatePicker = function() { | ||
|
||
}; | ||
|
||
DatePicker.prototype.showDateOrTime = function(action,successCallback, failureCallback) { | ||
return PhoneGap.exec( | ||
successCallback, //Success callback from the plugin | ||
failureCallback, //Error callback from the plugin | ||
'DatePickerPlugin', //Tell PhoneGap to run "DatePickerPlugin" Plugin | ||
action, //Tell plugin, which action we want to perform | ||
[]); //Passing list of args to the plugin | ||
}; | ||
|
||
/** | ||
* Enregistre une nouvelle bibliothèque de fonctions | ||
* auprès de PhoneGap | ||
**/ | ||
|
||
PhoneGap.addConstructor(function() { | ||
//Register the javascript plugin with PhoneGap | ||
PhoneGap.addPlugin('datePickerPlugin', new DatePicker()); | ||
|
||
//Register the native class of plugin with PhoneGap | ||
PluginManager.addService("DatePickerPlugin", | ||
"com.ngapplication.plugin.DatePickerPlugin"); | ||
}); |