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

changeset 0
e8ccd40d0ef6
equal deleted inserted replaced
-1:000000000000 0:8c7d2dd838a9
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 */
19 package org.apache.cordova.device;
20
21 import java.util.TimeZone;
22
23 import org.apache.cordova.CordovaWebView;
24 import org.apache.cordova.CallbackContext;
25 import org.apache.cordova.CordovaPlugin;
26 import org.apache.cordova.CordovaInterface;
27 import org.json.JSONArray;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30
31 import android.provider.Settings;
32
33 public 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 (action.equals("getDeviceInfo")) {
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 callbackContext.success(r);
77 }
78 else {
79 return false;
80 }
81 return true;
82 }
83
84 //--------------------------------------------------------------------------
85 // LOCAL METHODS
86 //--------------------------------------------------------------------------
87
88 /**
89 * Get the OS name.
90 *
91 * @return
92 */
93 public String getPlatform() {
94 String platform;
95 if (isAmazonDevice()) {
96 platform = AMAZON_PLATFORM;
97 } else {
98 platform = ANDROID_PLATFORM;
99 }
100 return platform;
101 }
102
103 /**
104 * Get the device's Universally Unique Identifier (UUID).
105 *
106 * @return
107 */
108 public String getUuid() {
109 String uuid = Settings.Secure.getString(this.cordova.getActivity().getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
110 return uuid;
111 }
112
113 public String getModel() {
114 String model = android.os.Build.MODEL;
115 return model;
116 }
117
118 public String getProductName() {
119 String productname = android.os.Build.PRODUCT;
120 return productname;
121 }
122
123 /**
124 * Get the OS version.
125 *
126 * @return
127 */
128 public String getOSVersion() {
129 String osversion = android.os.Build.VERSION.RELEASE;
130 return osversion;
131 }
132
133 public String getSDKVersion() {
134 @SuppressWarnings("deprecation")
135 String sdkversion = android.os.Build.VERSION.SDK;
136 return sdkversion;
137 }
138
139 public String getTimeZoneID() {
140 TimeZone tz = TimeZone.getDefault();
141 return (tz.getID());
142 }
143
144 /**
145 * Function to check if the device is manufactured by Amazon
146 *
147 * @return
148 */
149 public boolean isAmazonDevice() {
150 if (android.os.Build.MANUFACTURER.equals(AMAZON_DEVICE)) {
151 return true;
152 }
153 return false;
154 }
155
156 }

mercurial