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