michael@0: /* michael@0: Licensed to the Apache Software Foundation (ASF) under one michael@0: or more contributor license agreements. See the NOTICE file michael@0: distributed with this work for additional information michael@0: regarding copyright ownership. The ASF licenses this file michael@0: to you under the Apache License, Version 2.0 (the michael@0: "License"); you may not use this file except in compliance michael@0: with the License. You may obtain a copy of the License at michael@0: michael@0: http://www.apache.org/licenses/LICENSE-2.0 michael@0: michael@0: Unless required by applicable law or agreed to in writing, michael@0: software distributed under the License is distributed on an michael@0: "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: KIND, either express or implied. See the License for the michael@0: specific language governing permissions and limitations michael@0: under the License. michael@0: */ michael@0: package org.apache.cordova.device; michael@0: michael@0: import java.util.TimeZone; michael@0: michael@0: import org.apache.cordova.CordovaWebView; michael@0: import org.apache.cordova.CallbackContext; michael@0: import org.apache.cordova.CordovaPlugin; michael@0: import org.apache.cordova.CordovaInterface; michael@0: import org.json.JSONArray; michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: import android.provider.Settings; michael@0: michael@0: public class Device extends CordovaPlugin { michael@0: public static final String TAG = "Device"; michael@0: michael@0: public static String platform; // Device OS michael@0: public static String uuid; // Device UUID michael@0: michael@0: private static final String ANDROID_PLATFORM = "Android"; michael@0: private static final String AMAZON_PLATFORM = "amazon-fireos"; michael@0: private static final String AMAZON_DEVICE = "Amazon"; michael@0: michael@0: /** michael@0: * Constructor. michael@0: */ michael@0: public Device() { michael@0: } michael@0: michael@0: /** michael@0: * Sets the context of the Command. This can then be used to do things like michael@0: * get file paths associated with the Activity. michael@0: * michael@0: * @param cordova The context of the main Activity. michael@0: * @param webView The CordovaWebView Cordova is running in. michael@0: */ michael@0: public void initialize(CordovaInterface cordova, CordovaWebView webView) { michael@0: super.initialize(cordova, webView); michael@0: Device.uuid = getUuid(); michael@0: } michael@0: michael@0: /** michael@0: * Executes the request and returns PluginResult. michael@0: * michael@0: * @param action The action to execute. michael@0: * @param args JSONArry of arguments for the plugin. michael@0: * @param callbackContext The callback id used when calling back into JavaScript. michael@0: * @return True if the action was valid, false if not. michael@0: */ michael@0: public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { michael@0: if (action.equals("getDeviceInfo")) { michael@0: JSONObject r = new JSONObject(); michael@0: r.put("uuid", Device.uuid); michael@0: r.put("version", this.getOSVersion()); michael@0: r.put("platform", this.getPlatform()); michael@0: r.put("model", this.getModel()); michael@0: callbackContext.success(r); michael@0: } michael@0: else { michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: //-------------------------------------------------------------------------- michael@0: // LOCAL METHODS michael@0: //-------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * Get the OS name. michael@0: * michael@0: * @return michael@0: */ michael@0: public String getPlatform() { michael@0: String platform; michael@0: if (isAmazonDevice()) { michael@0: platform = AMAZON_PLATFORM; michael@0: } else { michael@0: platform = ANDROID_PLATFORM; michael@0: } michael@0: return platform; michael@0: } michael@0: michael@0: /** michael@0: * Get the device's Universally Unique Identifier (UUID). michael@0: * michael@0: * @return michael@0: */ michael@0: public String getUuid() { michael@0: String uuid = Settings.Secure.getString(this.cordova.getActivity().getContentResolver(), android.provider.Settings.Secure.ANDROID_ID); michael@0: return uuid; michael@0: } michael@0: michael@0: public String getModel() { michael@0: String model = android.os.Build.MODEL; michael@0: return model; michael@0: } michael@0: michael@0: public String getProductName() { michael@0: String productname = android.os.Build.PRODUCT; michael@0: return productname; michael@0: } michael@0: michael@0: /** michael@0: * Get the OS version. michael@0: * michael@0: * @return michael@0: */ michael@0: public String getOSVersion() { michael@0: String osversion = android.os.Build.VERSION.RELEASE; michael@0: return osversion; michael@0: } michael@0: michael@0: public String getSDKVersion() { michael@0: @SuppressWarnings("deprecation") michael@0: String sdkversion = android.os.Build.VERSION.SDK; michael@0: return sdkversion; michael@0: } michael@0: michael@0: public String getTimeZoneID() { michael@0: TimeZone tz = TimeZone.getDefault(); michael@0: return (tz.getID()); michael@0: } michael@0: michael@0: /** michael@0: * Function to check if the device is manufactured by Amazon michael@0: * michael@0: * @return michael@0: */ michael@0: public boolean isAmazonDevice() { michael@0: if (android.os.Build.MANUFACTURER.equals(AMAZON_DEVICE)) { michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: }