arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-device/src/android/Device.java
plugins/cordova-plugin-device/src/android/Device.javaRaw
  1/*
  2       Licensed to the Apache Software Foundation (ASF) under one
  3       or more contributor license agreements.  See the NOTICE file
  4       distributed with this work for additional information
  5       regarding copyright ownership.  The ASF licenses this file
  6       to you under the Apache License, Version 2.0 (the
  7       "License"); you may not use this file except in compliance
  8       with the License.  You may obtain a copy of the License at
  9
 10         http://www.apache.org/licenses/LICENSE-2.0
 11
 12       Unless required by applicable law or agreed to in writing,
 13       software distributed under the License is distributed on an
 14       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15       KIND, either express or implied.  See the License for the
 16       specific language governing permissions and limitations
 17       under the License.
 18*/
 19package org.apache.cordova.device;
 20
 21import java.util.TimeZone;
 22
 23import org.apache.cordova.CordovaWebView;
 24import org.apache.cordova.CallbackContext;
 25import org.apache.cordova.CordovaPlugin;
 26import org.apache.cordova.CordovaInterface;
 27import org.json.JSONArray;
 28import org.json.JSONException;
 29import org.json.JSONObject;
 30
 31import android.provider.Settings;
 32
 33public class Device extends CordovaPlugin {
 34    public static final String TAG = "Device";
 35
 36    public static String platform;                            // Device OS
 37    public static String uuid;                                // Device UUID
 38
 39    private static final String ANDROID_PLATFORM = "Android";
 40    private static final String AMAZON_PLATFORM = "amazon-fireos";
 41    private static final String AMAZON_DEVICE = "Amazon";
 42
 43    /**
 44     * Constructor.
 45     */
 46    public Device() {
 47    }
 48
 49    /**
 50     * Sets the context of the Command. This can then be used to do things like
 51     * get file paths associated with the Activity.
 52     *
 53     * @param cordova The context of the main Activity.
 54     * @param webView The CordovaWebView Cordova is running in.
 55     */
 56    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
 57        super.initialize(cordova, webView);
 58        Device.uuid = getUuid();
 59    }
 60
 61    /**
 62     * Executes the request and returns PluginResult.
 63     *
 64     * @param action            The action to execute.
 65     * @param args              JSONArry of arguments for the plugin.
 66     * @param callbackContext   The callback id used when calling back into JavaScript.
 67     * @return                  True if the action was valid, false if not.
 68     */
 69    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
 70        if ("getDeviceInfo".equals(action)) {
 71            JSONObject r = new JSONObject();
 72            r.put("uuid", Device.uuid);
 73            r.put("version", this.getOSVersion());
 74            r.put("platform", this.getPlatform());
 75            r.put("model", this.getModel());
 76            r.put("manufacturer", this.getManufacturer());
 77	        r.put("isVirtual", this.isVirtual());
 78            r.put("serial", this.getSerialNumber());
 79            callbackContext.success(r);
 80        }
 81        else {
 82            return false;
 83        }
 84        return true;
 85    }
 86
 87    //--------------------------------------------------------------------------
 88    // LOCAL METHODS
 89    //--------------------------------------------------------------------------
 90
 91    /**
 92     * Get the OS name.
 93     *
 94     * @return
 95     */
 96    public String getPlatform() {
 97        String platform;
 98        if (isAmazonDevice()) {
 99            platform = AMAZON_PLATFORM;
100        } else {
101            platform = ANDROID_PLATFORM;
102        }
103        return platform;
104    }
105
106    /**
107     * Get the device's Universally Unique Identifier (UUID).
108     *
109     * @return
110     */
111    public String getUuid() {
112        String uuid = Settings.Secure.getString(this.cordova.getActivity().getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
113        return uuid;
114    }
115
116    public String getModel() {
117        String model = android.os.Build.MODEL;
118        return model;
119    }
120
121    public String getProductName() {
122        String productname = android.os.Build.PRODUCT;
123        return productname;
124    }
125
126    public String getManufacturer() {
127        String manufacturer = android.os.Build.MANUFACTURER;
128        return manufacturer;
129    }
130
131    public String getSerialNumber() {
132        String serial = android.os.Build.SERIAL;
133        return serial;
134    }
135
136    /**
137     * Get the OS version.
138     *
139     * @return
140     */
141    public String getOSVersion() {
142        String osversion = android.os.Build.VERSION.RELEASE;
143        return osversion;
144    }
145
146    public String getSDKVersion() {
147        @SuppressWarnings("deprecation")
148        String sdkversion = android.os.Build.VERSION.SDK;
149        return sdkversion;
150    }
151
152    public String getTimeZoneID() {
153        TimeZone tz = TimeZone.getDefault();
154        return (tz.getID());
155    }
156
157    /**
158     * Function to check if the device is manufactured by Amazon
159     *
160     * @return
161     */
162    public boolean isAmazonDevice() {
163        if (android.os.Build.MANUFACTURER.equals(AMAZON_DEVICE)) {
164            return true;
165        }
166        return false;
167    }
168
169    public boolean isVirtual() {
170	return android.os.Build.FINGERPRINT.contains("generic") ||
171	    android.os.Build.PRODUCT.contains("sdk");
172    }
173
174}