[Arakhnę-Dev] [420] * Improve the serial number or the UUID for the Android operating system. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 420
Author: galland
Date: 2013-04-09 13:53:27 +0200 (Tue, 09 Apr 2013)
Log Message:
-----------
* Improve the serial number or the UUID for the Android operating system.
Modified Paths:
--------------
trunk/arakhneVmutils/arakhneVmutils-java/src/main/java/org/arakhne/vmutil/OperatingSystemAndroidWrapper.java
trunk/arakhneVmutils/arakhneVmutils-java/src/main/java/org/arakhne/vmutil/OperatingSystemDiskUtilWrapper.java
trunk/arakhneVmutils/arakhneVmutils-java/src/main/java/org/arakhne/vmutil/OperatingSystemUDevWrapper.java
Modified: trunk/arakhneVmutils/arakhneVmutils-java/src/main/java/org/arakhne/vmutil/OperatingSystemAndroidWrapper.java
===================================================================
--- trunk/arakhneVmutils/arakhneVmutils-java/src/main/java/org/arakhne/vmutil/OperatingSystemAndroidWrapper.java 2013-04-08 20:02:47 UTC (rev 419)
+++ trunk/arakhneVmutils/arakhneVmutils-java/src/main/java/org/arakhne/vmutil/OperatingSystemAndroidWrapper.java 2013-04-09 11:53:27 UTC (rev 420)
@@ -23,6 +23,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.util.UUID;
/**
* Wrapper to the Android functions.
@@ -39,6 +40,9 @@
*/
class OperatingSystemAndroidWrapper extends AbstractOperatingSystemWrapper {
+ private static final String PREFS_FILE = "device_id.xml"; //$NON-NLS-1$
+ private static final String PREFS_DEVICE_ID = "device_id"; //$NON-NLS-1$
+
/**
*/
public OperatingSystemAndroidWrapper() {
@@ -49,20 +53,95 @@
*/
@Override
public String getOSSerialNumber(boolean enableSuperUser, boolean enableGUI) {
- String serial;
+ String serial = null;
try {
- Class<?> secureClass = Android.getSystemSettingsClass();
+ Object androidContext = Android.getContext();
+ if (androidContext!=null) {
+ Class<?> contextClass = androidContext.getClass();
+ Method m;
+ Object v;
- Method getStringMethod = secureClass.getMethod("getString", Android.getContextResolverClass(), String.class); //$NON-NLS-1$
+ // Read the prefs
+ try {
+ m = contextClass.getMethod("getSharedPreferences", String.class, int.class); //$NON-NLS-1$
+ Object prefs = m.invoke(androidContext, PREFS_FILE, 0);
+ assert(prefs!=null);
- Field androidIdField = secureClass.getField("ANDROID_ID"); //$NON-NLS-1$
- Object androidId = androidIdField.get(null);
+ try {
- serial = (String)getStringMethod.invoke(null, Android.getContextResolver(), androidId);
+ m = prefs.getClass().getMethod("getString", String.class, String .class); //$NON-NLS-1$
+ v = m.invoke(prefs, PREFS_DEVICE_ID, null);
+ if (v!=null) {
+ serial = v.toString();
+ }
+ }
+ catch (Throwable _) {
+ serial = null;
+ }
+
+ // Get the id from the Android constant.
+ try {
+ if (serial==null) {
+ Class<?> secureClass = Android.getSecureSettingsClass();
+
+ Method getStringMethod = secureClass.getMethod("getString", Android.getContextResolverClass(), String.class); //$NON-NLS-1$
+
+ Field androidIdField = secureClass.getField("ANDROID_ID"); //$NON-NLS-1$
+ Object androidId = androidIdField.get(null);
+
+ serial = (String)getStringMethod.invoke(null, Android.getContextResolver(), androidId);
+
+ // Use the Android ID unless it's broken, in which case fallback on deviceId,
+ // unless it's not available, then fallback on the phone id.
+ if ("9774d56d682e549c".equalsIgnoreCase(serial)) { //$NON-NLS-1$
+ // This id is known as the broken id on 2.2 devices.
+ serial = null;
+ }
+ }
+ }
+ catch (Throwable _) {
+ serial = null;
+ }
+
+ // Read the phone id
+ try {
+ if (serial==null) {
+ m = contextClass.getMethod("getSystemService", String.class); //$NON-NLS-1$
+ Field field = contextClass.getField("TELEPHONY_SERVICE"); //$NON-NLS-1$
+ Object telephonyManager = m.invoke(androidContext, field.get(null));
+ if (telephonyManager!=null) {
+ m = telephonyManager.getClass().getMethod("getDeviceId"); //$NON-NLS-1$
+ Object rawSerial = m.invoke(telephonyManager);
+ if (rawSerial!=null) {
+ serial = rawSerial.toString();
+ }
+ }
+ }
+ }
+ catch (Throwable _) {
+ serial = null;
+ }
+
+ // Compute a random id and put it in the prefs.
+ if (serial==null) {
+ serial = UUID.randomUUID().toString();
+ m = prefs.getClass().getMethod("edit"); //$NON-NLS-1$
+ v = m.invoke(prefs);
+ m = v.getClass().getMethod("putString", String.class, String.class); //$NON-NLS-1$
+ v = m.invoke(v, PREFS_DEVICE_ID, serial);
+ m = v.getClass().getMethod("commit"); //$NON-NLS-1$
+ m.invoke(v);
+ }
+ }
+ catch (Throwable _) {
+ serial = null;
+ }
+ }
}
catch (Throwable _) {
serial = null;
}
+
return serial;
}
@@ -70,21 +149,23 @@
*/
@Override
public String getOSUUID(boolean enableSuperUser, boolean enableGUI) {
- String serial;
- try {
- Class<?> secureClass = Android.getSecureSettingsClass();
+ String serial = getOSSerialNumber(enableSuperUser, enableGUI);
+ if (serial!=null) {
+ try {
+ return UUID.fromString(serial).toString();
+ }
+ catch(Throwable _) {
+ //
+ }
- Method getStringMethod = secureClass.getMethod("getString", Android.getContextResolverClass(), String.class); //$NON-NLS-1$
-
- Field androidIdField = secureClass.getField("ANDROID_ID"); //$NON-NLS-1$
- Object androidId = androidIdField.get(null);
-
- serial = (String)getStringMethod.invoke(null, Android.getContextResolver(), androidId);
+ try {
+ return UUID.nameUUIDFromBytes(serial.getBytes()).toString();
+ }
+ catch(Throwable _) {
+ //
+ }
}
- catch (Throwable _) {
- serial = null;
- }
- return serial;
+ return null;
}
/**
Modified: trunk/arakhneVmutils/arakhneVmutils-java/src/main/java/org/arakhne/vmutil/OperatingSystemDiskUtilWrapper.java
===================================================================
--- trunk/arakhneVmutils/arakhneVmutils-java/src/main/java/org/arakhne/vmutil/OperatingSystemDiskUtilWrapper.java 2013-04-08 20:02:47 UTC (rev 419)
+++ trunk/arakhneVmutils/arakhneVmutils-java/src/main/java/org/arakhne/vmutil/OperatingSystemDiskUtilWrapper.java 2013-04-09 11:53:27 UTC (rev 420)
@@ -1,5 +1,5 @@
/*
- * $Id$
+ * $Id$
*
* Copyright (C) 2011 Stephane GALLAND.
*
Modified: trunk/arakhneVmutils/arakhneVmutils-java/src/main/java/org/arakhne/vmutil/OperatingSystemUDevWrapper.java
===================================================================
--- trunk/arakhneVmutils/arakhneVmutils-java/src/main/java/org/arakhne/vmutil/OperatingSystemUDevWrapper.java 2013-04-08 20:02:47 UTC (rev 419)
+++ trunk/arakhneVmutils/arakhneVmutils-java/src/main/java/org/arakhne/vmutil/OperatingSystemUDevWrapper.java 2013-04-09 11:53:27 UTC (rev 420)
@@ -64,10 +64,12 @@
if (f.exists()) {
return runUdev(f, "ID_SERIAL"); //$NON-NLS-1$
}
+
f = new File("/dev/hda"); //$NON-NLS-1$
if (f.exists()) {
return runUdev(f, "ID_SERIAL"); //$NON-NLS-1$
}
+
return null;
}
@@ -80,10 +82,12 @@
if (f.exists()) {
return runUdev(f, "ID_SERIAL_SHORT"); //$NON-NLS-1$
}
+
f = new File("/dev/hda"); //$NON-NLS-1$
if (f.exists()) {
return runUdev(f, "ID_SERIAL_SHORT"); //$NON-NLS-1$
}
+
return null;
}