Touchgui/plugins/org.apache.cordova.device/src/android/Device.java

changeset 0
e8ccd40d0ef6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Touchgui/plugins/org.apache.cordova.device/src/android/Device.java	Thu Jun 04 14:50:33 2015 +0200
     1.3 @@ -0,0 +1,156 @@
     1.4 +/*
     1.5 +       Licensed to the Apache Software Foundation (ASF) under one
     1.6 +       or more contributor license agreements.  See the NOTICE file
     1.7 +       distributed with this work for additional information
     1.8 +       regarding copyright ownership.  The ASF licenses this file
     1.9 +       to you under the Apache License, Version 2.0 (the
    1.10 +       "License"); you may not use this file except in compliance
    1.11 +       with the License.  You may obtain a copy of the License at
    1.12 +
    1.13 +         http://www.apache.org/licenses/LICENSE-2.0
    1.14 +
    1.15 +       Unless required by applicable law or agreed to in writing,
    1.16 +       software distributed under the License is distributed on an
    1.17 +       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.18 +       KIND, either express or implied.  See the License for the
    1.19 +       specific language governing permissions and limitations
    1.20 +       under the License.
    1.21 +*/
    1.22 +package org.apache.cordova.device;
    1.23 +
    1.24 +import java.util.TimeZone;
    1.25 +
    1.26 +import org.apache.cordova.CordovaWebView;
    1.27 +import org.apache.cordova.CallbackContext;
    1.28 +import org.apache.cordova.CordovaPlugin;
    1.29 +import org.apache.cordova.CordovaInterface;
    1.30 +import org.json.JSONArray;
    1.31 +import org.json.JSONException;
    1.32 +import org.json.JSONObject;
    1.33 +
    1.34 +import android.provider.Settings;
    1.35 +
    1.36 +public class Device extends CordovaPlugin {
    1.37 +    public static final String TAG = "Device";
    1.38 +
    1.39 +    public static String platform;                            // Device OS
    1.40 +    public static String uuid;                                // Device UUID
    1.41 +
    1.42 +    private static final String ANDROID_PLATFORM = "Android";
    1.43 +    private static final String AMAZON_PLATFORM = "amazon-fireos";
    1.44 +    private static final String AMAZON_DEVICE = "Amazon";
    1.45 +
    1.46 +    /**
    1.47 +     * Constructor.
    1.48 +     */
    1.49 +    public Device() {
    1.50 +    }
    1.51 +
    1.52 +    /**
    1.53 +     * Sets the context of the Command. This can then be used to do things like
    1.54 +     * get file paths associated with the Activity.
    1.55 +     *
    1.56 +     * @param cordova The context of the main Activity.
    1.57 +     * @param webView The CordovaWebView Cordova is running in.
    1.58 +     */
    1.59 +    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    1.60 +        super.initialize(cordova, webView);
    1.61 +        Device.uuid = getUuid();
    1.62 +    }
    1.63 +
    1.64 +    /**
    1.65 +     * Executes the request and returns PluginResult.
    1.66 +     *
    1.67 +     * @param action            The action to execute.
    1.68 +     * @param args              JSONArry of arguments for the plugin.
    1.69 +     * @param callbackContext   The callback id used when calling back into JavaScript.
    1.70 +     * @return                  True if the action was valid, false if not.
    1.71 +     */
    1.72 +    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    1.73 +        if (action.equals("getDeviceInfo")) {
    1.74 +            JSONObject r = new JSONObject();
    1.75 +            r.put("uuid", Device.uuid);
    1.76 +            r.put("version", this.getOSVersion());
    1.77 +            r.put("platform", this.getPlatform());
    1.78 +            r.put("model", this.getModel());
    1.79 +            callbackContext.success(r);
    1.80 +        }
    1.81 +        else {
    1.82 +            return false;
    1.83 +        }
    1.84 +        return true;
    1.85 +    }
    1.86 +
    1.87 +    //--------------------------------------------------------------------------
    1.88 +    // LOCAL METHODS
    1.89 +    //--------------------------------------------------------------------------
    1.90 +
    1.91 +    /**
    1.92 +     * Get the OS name.
    1.93 +     * 
    1.94 +     * @return
    1.95 +     */
    1.96 +    public String getPlatform() {
    1.97 +        String platform;
    1.98 +        if (isAmazonDevice()) {
    1.99 +            platform = AMAZON_PLATFORM;
   1.100 +        } else {
   1.101 +            platform = ANDROID_PLATFORM;
   1.102 +        }
   1.103 +        return platform;
   1.104 +    }
   1.105 +
   1.106 +    /**
   1.107 +     * Get the device's Universally Unique Identifier (UUID).
   1.108 +     *
   1.109 +     * @return
   1.110 +     */
   1.111 +    public String getUuid() {
   1.112 +        String uuid = Settings.Secure.getString(this.cordova.getActivity().getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
   1.113 +        return uuid;
   1.114 +    }
   1.115 +
   1.116 +    public String getModel() {
   1.117 +        String model = android.os.Build.MODEL;
   1.118 +        return model;
   1.119 +    }
   1.120 +
   1.121 +    public String getProductName() {
   1.122 +        String productname = android.os.Build.PRODUCT;
   1.123 +        return productname;
   1.124 +    }
   1.125 +
   1.126 +    /**
   1.127 +     * Get the OS version.
   1.128 +     *
   1.129 +     * @return
   1.130 +     */
   1.131 +    public String getOSVersion() {
   1.132 +        String osversion = android.os.Build.VERSION.RELEASE;
   1.133 +        return osversion;
   1.134 +    }
   1.135 +
   1.136 +    public String getSDKVersion() {
   1.137 +        @SuppressWarnings("deprecation")
   1.138 +        String sdkversion = android.os.Build.VERSION.SDK;
   1.139 +        return sdkversion;
   1.140 +    }
   1.141 +
   1.142 +    public String getTimeZoneID() {
   1.143 +        TimeZone tz = TimeZone.getDefault();
   1.144 +        return (tz.getID());
   1.145 +    }
   1.146 +
   1.147 +    /**
   1.148 +     * Function to check if the device is manufactured by Amazon
   1.149 +     * 
   1.150 +     * @return
   1.151 +     */
   1.152 +    public boolean isAmazonDevice() {
   1.153 +        if (android.os.Build.MANUFACTURER.equals(AMAZON_DEVICE)) {
   1.154 +            return true;
   1.155 +        }
   1.156 +        return false;
   1.157 +    }
   1.158 +
   1.159 +}

mercurial