mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-22 00:32:55 +08:00
Change Device JS object to include only platform, uuid, version, and phonegap properties as defined in API, and modify Device Java class to implement plugin interface.
This commit is contained in:
parent
c050e00b8f
commit
705b8f6874
@ -1,59 +1,60 @@
|
|||||||
/**
|
/**
|
||||||
* this represents the mobile device, and provides properties for inspecting the model, version, UUID of the
|
* This represents the mobile device, and provides properties for inspecting the model, version, UUID of the
|
||||||
* phone, etc.
|
* phone, etc.
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function Device() {
|
function Device() {
|
||||||
this.available = PhoneGap.available;
|
this.available = PhoneGap.available;
|
||||||
this.platform = null;
|
this.platform = null;
|
||||||
this.version = null;
|
this.version = null;
|
||||||
this.name = null;
|
this.name = null;
|
||||||
this.gap = null;
|
this.uuid = null;
|
||||||
this.uuid = null;
|
this.phonegap = null;
|
||||||
try {
|
|
||||||
if (window.DroidGap) {
|
var me = this;
|
||||||
this.available = true;
|
PhoneGap.execAsync(
|
||||||
this.uuid = window.DroidGap.getUuid();
|
function(info) {
|
||||||
this.version = window.DroidGap.getOSVersion();
|
me.available = true;
|
||||||
this.gapVersion = window.DroidGap.getVersion();
|
me.platform = info.platform;
|
||||||
this.platform = window.DroidGap.getPlatform();
|
me.version = info.version;
|
||||||
this.name = window.DroidGap.getProductName();
|
me.uuid = info.uuid;
|
||||||
this.line1Number = window.DroidGap.getLine1Number();
|
me.phonegap = info.phonegap;
|
||||||
this.deviceId = window.DroidGap.getDeviceId();
|
},
|
||||||
this.simSerialNumber = window.DroidGap.getSimSerialNumber();
|
function(e) {
|
||||||
this.subscriberId = window.DroidGap.getSubscriberId();
|
me.available = false;
|
||||||
}
|
console.log("Error initializing PhoneGap: " + e);
|
||||||
} catch(e) {
|
alert("Error initializing PhoneGap: "+e);
|
||||||
this.available = false;
|
},
|
||||||
}
|
"Device", "getDeviceInfo", []);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* You must explicitly override the back button.
|
* This is only for Android.
|
||||||
|
*
|
||||||
|
* You must explicitly override the back button.
|
||||||
*/
|
*/
|
||||||
|
Device.prototype.overrideBackButton = function() {
|
||||||
Device.prototype.overrideBackButton = function()
|
BackButton.override();
|
||||||
{
|
|
||||||
BackButton.override();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
* This is only for Android.
|
||||||
|
*
|
||||||
* This resets the back button to the default behaviour
|
* This resets the back button to the default behaviour
|
||||||
*/
|
*/
|
||||||
|
Device.prototype.resetBackButton = function() {
|
||||||
Device.prototype.resetBackButton = function()
|
BackButton.reset();
|
||||||
{
|
|
||||||
BackButton.reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
* This is only for Android.
|
||||||
|
*
|
||||||
* This terminates the activity!
|
* This terminates the activity!
|
||||||
*/
|
*/
|
||||||
Device.prototype.exitApp = function()
|
Device.prototype.exitApp = function() {
|
||||||
{
|
BackButton.exitApp();
|
||||||
BackButton.exitApp();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PhoneGap.addConstructor(function() {
|
PhoneGap.addConstructor(function() {
|
||||||
navigator.device = window.device = new Device();
|
navigator.device = window.device = new Device();
|
||||||
});
|
});
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
* This class provides access to notifications on the device.
|
* This class provides access to notifications on the device.
|
||||||
*/
|
*/
|
||||||
function Notification() {
|
function Notification() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,7 +33,7 @@ Notification.prototype.activityStop = function() {
|
|||||||
* @param {String} colour The colour of the light.
|
* @param {String} colour The colour of the light.
|
||||||
*/
|
*/
|
||||||
Notification.prototype.blink = function(count, colour) {
|
Notification.prototype.blink = function(count, colour) {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,16 +41,17 @@ Notification.prototype.blink = function(count, colour) {
|
|||||||
* @param {Integer} mills The number of milliseconds to vibrate for.
|
* @param {Integer} mills The number of milliseconds to vibrate for.
|
||||||
*/
|
*/
|
||||||
Notification.prototype.vibrate = function(mills) {
|
Notification.prototype.vibrate = function(mills) {
|
||||||
|
PhoneGap.execAsync(null, null, "Device", "vibrate", [mills]);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Causes the device to beep.
|
* Causes the device to beep.
|
||||||
|
* On Android, the default notification ringtone is played.
|
||||||
|
*
|
||||||
* @param {Integer} count The number of beeps.
|
* @param {Integer} count The number of beeps.
|
||||||
* @param {Integer} volume The volume of the beep.
|
|
||||||
*/
|
*/
|
||||||
Notification.prototype.beep = function(count, volume) {
|
Notification.prototype.beep = function(count) {
|
||||||
|
PhoneGap.execAsync(null, null, "Device", "beep", [count]);
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: of course on Blackberry and Android there notifications in the UI as well
|
// TODO: of course on Blackberry and Android there notifications in the UI as well
|
||||||
@ -60,18 +60,3 @@ PhoneGap.addConstructor(function() {
|
|||||||
if (typeof navigator.notification == "undefined") navigator.notification = new Notification();
|
if (typeof navigator.notification == "undefined") navigator.notification = new Notification();
|
||||||
});
|
});
|
||||||
|
|
||||||
Notification.prototype.vibrate = function(mills)
|
|
||||||
{
|
|
||||||
DroidGap.vibrate(mills);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* On the Android, we don't beep, we notify you with your
|
|
||||||
* notification! We shouldn't keep hammering on this, and should
|
|
||||||
* review what we want beep to do.
|
|
||||||
*/
|
|
||||||
|
|
||||||
Notification.prototype.beep = function(count, volume)
|
|
||||||
{
|
|
||||||
DroidGap.beep(count);
|
|
||||||
}
|
|
||||||
|
@ -24,7 +24,15 @@ package com.phonegap;
|
|||||||
|
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import com.phonegap.api.Plugin;
|
||||||
|
import com.phonegap.api.PluginResult;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Vibrator;
|
import android.os.Vibrator;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
@ -33,76 +41,210 @@ import android.webkit.WebView;
|
|||||||
import android.media.Ringtone;
|
import android.media.Ringtone;
|
||||||
import android.media.RingtoneManager;
|
import android.media.RingtoneManager;
|
||||||
|
|
||||||
public class Device{
|
public class Device implements Plugin {
|
||||||
|
|
||||||
private static final String LOG_TAG = "PhoneGap";
|
public static String phonegapVersion = "pre-0.92 EDGE"; // PhoneGap version
|
||||||
/*
|
public static String platform = "Android"; // Device OS
|
||||||
* UUID, version and availability
|
public static String uuid; // Device UUID
|
||||||
*/
|
private DroidGap ctx; // DroidGap object
|
||||||
public boolean droid = true;
|
@SuppressWarnings("unused")
|
||||||
public static String version = "0.91";
|
private WebView webView; // Webview object
|
||||||
public static String platform = "Android";
|
|
||||||
public static String uuid;
|
|
||||||
private Context mCtx;
|
|
||||||
private WebView mAppView;
|
|
||||||
AudioPlayer audio;
|
|
||||||
|
|
||||||
public Device(WebView appView, Context ctx) {
|
/**
|
||||||
this.mCtx = ctx;
|
* Constructor.
|
||||||
this.mAppView = appView;
|
*/
|
||||||
uuid = getUuid();
|
public Device() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void beep(long pattern)
|
/**
|
||||||
{
|
* Sets the context of the Command. This can then be used to do things like
|
||||||
|
* get file paths associated with the Activity.
|
||||||
|
*
|
||||||
|
* @param ctx The context of the main Activity.
|
||||||
|
*/
|
||||||
|
public void setContext(DroidGap ctx) {
|
||||||
|
this.ctx = ctx;
|
||||||
|
Device.uuid = getUuid();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the main View of the application, this is the WebView within which
|
||||||
|
* a PhoneGap app runs.
|
||||||
|
*
|
||||||
|
* @param webView The PhoneGap WebView
|
||||||
|
*/
|
||||||
|
public void setView(WebView webView) {
|
||||||
|
this.webView = webView;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the request and returns CommandResult.
|
||||||
|
*
|
||||||
|
* @param action The command to execute.
|
||||||
|
* @param args JSONArry of arguments for the command.
|
||||||
|
* @return A CommandResult object with a status and message.
|
||||||
|
*/
|
||||||
|
public PluginResult execute(String action, JSONArray args) {
|
||||||
|
PluginResult.Status status = PluginResult.Status.OK;
|
||||||
|
String result = "";
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (action.equals("getDeviceInfo")) {
|
||||||
|
JSONObject r = new JSONObject();
|
||||||
|
r.put("uuid", Device.uuid);
|
||||||
|
r.put("version", this.getOSVersion());
|
||||||
|
r.put("platform", Device.platform);
|
||||||
|
r.put("name", this.getProductName());
|
||||||
|
r.put("phonegap", Device.phonegapVersion);
|
||||||
|
//JSONObject pg = new JSONObject();
|
||||||
|
//pg.put("version", Device.phonegapVersion);
|
||||||
|
//r.put("phonegap", pg);
|
||||||
|
return new PluginResult(status, r);
|
||||||
|
}
|
||||||
|
else if (action.equals("beep")) {
|
||||||
|
this.beep(args.getLong(0));
|
||||||
|
}
|
||||||
|
else if (action.equals("vibrate")) {
|
||||||
|
this.vibrate(args.getLong(0));
|
||||||
|
}
|
||||||
|
return new PluginResult(status, result);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identifies if action to be executed returns a value and should be run synchronously.
|
||||||
|
*
|
||||||
|
* @param action The action to execute
|
||||||
|
* @return T=returns value
|
||||||
|
*/
|
||||||
|
public boolean isSynch(String action) {
|
||||||
|
if (action.equals("getDeviceInfo")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the system is about to start resuming a previous activity.
|
||||||
|
*/
|
||||||
|
public void onPause() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the activity will start interacting with the user.
|
||||||
|
*/
|
||||||
|
public void onResume() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the activity is to be shut down.
|
||||||
|
* Stop listener.
|
||||||
|
*/
|
||||||
|
public void onDestroy() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when an activity you launched exits, giving you the requestCode you started it with,
|
||||||
|
* the resultCode it returned, and any additional data from it.
|
||||||
|
*
|
||||||
|
* @param requestCode The request code originally supplied to startActivityForResult(),
|
||||||
|
* allowing you to identify who this result came from.
|
||||||
|
* @param resultCode The integer result code returned by the child activity through its setResult().
|
||||||
|
* @param data An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
|
||||||
|
*/
|
||||||
|
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------
|
||||||
|
// LOCAL METHODS
|
||||||
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Beep plays the default notification ringtone.
|
||||||
|
*
|
||||||
|
* @param count Number of times to play notification
|
||||||
|
*/
|
||||||
|
public void beep(long count) {
|
||||||
Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
||||||
Ringtone notification = RingtoneManager.getRingtone(mCtx, ringtone);
|
Ringtone notification = RingtoneManager.getRingtone(this.ctx, ringtone);
|
||||||
if (notification != null) { // This will be the case when the phone is set to silent for example
|
|
||||||
for (long i = 0; i < pattern; ++i)
|
// If phone is not set to silent mode
|
||||||
{
|
if (notification != null) {
|
||||||
|
for (long i = 0; i < count; ++i) {
|
||||||
notification.play();
|
notification.play();
|
||||||
|
long timeout = 5000;
|
||||||
|
while (notification.isPlaying() && (timeout > 0)) {
|
||||||
|
timeout = timeout - 100;
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void vibrate(long pattern){
|
/**
|
||||||
|
* Vibrates the device for the specified amount of time.
|
||||||
|
*
|
||||||
|
* @param time Time to vibrate in ms.
|
||||||
|
*/
|
||||||
|
public void vibrate(long time){
|
||||||
// Start the vibration, 0 defaults to half a second.
|
// Start the vibration, 0 defaults to half a second.
|
||||||
if (pattern == 0)
|
if (time == 0) {
|
||||||
pattern = 500;
|
time = 500;
|
||||||
Vibrator vibrator = (Vibrator) mCtx.getSystemService(Context.VIBRATOR_SERVICE);
|
}
|
||||||
vibrator.vibrate(pattern);
|
Vibrator vibrator = (Vibrator) this.ctx.getSystemService(Context.VIBRATOR_SERVICE);
|
||||||
|
vibrator.vibrate(time);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPlatform()
|
/**
|
||||||
{
|
* Get the OS name.
|
||||||
return this.platform;
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getPlatform() {
|
||||||
|
return Device.platform;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUuid()
|
/**
|
||||||
{
|
* Get the device's Universally Unique Identifier (UUID).
|
||||||
//TelephonyManager operator = (TelephonyManager) mCtx.getSystemService(Context.TELEPHONY_SERVICE);
|
*
|
||||||
//String uuid = operator.getDeviceId();
|
* @return
|
||||||
String uuid = Settings.Secure.getString(mCtx.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
|
*/
|
||||||
|
public String getUuid() {
|
||||||
|
String uuid = Settings.Secure.getString(this.ctx.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the PhoneGap version.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getPhonegapVersion() {
|
||||||
|
return Device.phonegapVersion;
|
||||||
|
}
|
||||||
|
|
||||||
public String getLine1Number(){
|
public String getLine1Number(){
|
||||||
TelephonyManager operator = (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
|
TelephonyManager operator = (TelephonyManager)this.ctx.getSystemService(Context.TELEPHONY_SERVICE);
|
||||||
return operator.getLine1Number();
|
return operator.getLine1Number();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDeviceId(){
|
public String getDeviceId(){
|
||||||
TelephonyManager operator = (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
|
TelephonyManager operator = (TelephonyManager)this.ctx.getSystemService(Context.TELEPHONY_SERVICE);
|
||||||
return operator.getDeviceId();
|
return operator.getDeviceId();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSimSerialNumber(){
|
public String getSimSerialNumber(){
|
||||||
TelephonyManager operator = (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
|
TelephonyManager operator = (TelephonyManager)this.ctx.getSystemService(Context.TELEPHONY_SERVICE);
|
||||||
return operator.getSimSerialNumber();
|
return operator.getSimSerialNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSubscriberId(){
|
public String getSubscriberId(){
|
||||||
TelephonyManager operator = (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
|
TelephonyManager operator = (TelephonyManager)this.ctx.getSystemService(Context.TELEPHONY_SERVICE);
|
||||||
return operator.getSubscriberId();
|
return operator.getSubscriberId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,21 +258,23 @@ public class Device{
|
|||||||
String productname = android.os.Build.PRODUCT;
|
String productname = android.os.Build.PRODUCT;
|
||||||
return productname;
|
return productname;
|
||||||
}
|
}
|
||||||
public String getOSVersion()
|
|
||||||
{
|
/**
|
||||||
|
* Get the OS version.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getOSVersion() {
|
||||||
String osversion = android.os.Build.VERSION.RELEASE;
|
String osversion = android.os.Build.VERSION.RELEASE;
|
||||||
return osversion;
|
return osversion;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSDKVersion()
|
public String getSDKVersion()
|
||||||
{
|
{
|
||||||
String sdkversion = android.os.Build.VERSION.SDK;
|
String sdkversion = android.os.Build.VERSION.SDK;
|
||||||
return sdkversion;
|
return sdkversion;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getVersion()
|
|
||||||
{
|
|
||||||
return version;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTimeZoneID() {
|
public String getTimeZoneID() {
|
||||||
TimeZone tz = TimeZone.getDefault();
|
TimeZone tz = TimeZone.getDefault();
|
||||||
|
@ -83,7 +83,6 @@ public class DroidGap extends Activity {
|
|||||||
protected Boolean loadInWebView = false;
|
protected Boolean loadInWebView = false;
|
||||||
private LinearLayout root;
|
private LinearLayout root;
|
||||||
|
|
||||||
private Device gap;
|
|
||||||
private FileUtils fs;
|
private FileUtils fs;
|
||||||
private BrowserKey mKey;
|
private BrowserKey mKey;
|
||||||
public CallbackServer callbackServer;
|
public CallbackServer callbackServer;
|
||||||
@ -270,13 +269,11 @@ public class DroidGap extends Activity {
|
|||||||
private void bindBrowser(WebView appView) {
|
private void bindBrowser(WebView appView) {
|
||||||
this.callbackServer = new CallbackServer();
|
this.callbackServer = new CallbackServer();
|
||||||
this.pluginManager = new PluginManager(appView, this);
|
this.pluginManager = new PluginManager(appView, this);
|
||||||
this.gap = new Device(appView, this);
|
|
||||||
this.fs = new FileUtils(appView, this);
|
this.fs = new FileUtils(appView, this);
|
||||||
this.mKey = new BrowserKey(appView, this);
|
this.mKey = new BrowserKey(appView, this);
|
||||||
|
|
||||||
// This creates the new javascript interfaces for PhoneGap
|
// This creates the new javascript interfaces for PhoneGap
|
||||||
appView.addJavascriptInterface(this.pluginManager, "PluginManager");
|
appView.addJavascriptInterface(this.pluginManager, "PluginManager");
|
||||||
appView.addJavascriptInterface(this.gap, "DroidGap");
|
|
||||||
|
|
||||||
appView.addJavascriptInterface(this.fs, "FileUtil");
|
appView.addJavascriptInterface(this.fs, "FileUtil");
|
||||||
appView.addJavascriptInterface(this.mKey, "BackButton");
|
appView.addJavascriptInterface(this.mKey, "BackButton");
|
||||||
@ -295,6 +292,7 @@ public class DroidGap extends Activity {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.addService("Device", "com.phonegap.Device");
|
||||||
this.addService("Accelerometer", "com.phonegap.AccelListener");
|
this.addService("Accelerometer", "com.phonegap.AccelListener");
|
||||||
this.addService("Compass", "com.phonegap.CompassListener");
|
this.addService("Compass", "com.phonegap.CompassListener");
|
||||||
this.addService("Media", "com.phonegap.AudioHandler");
|
this.addService("Media", "com.phonegap.AudioHandler");
|
||||||
|
Loading…
Reference in New Issue
Block a user