Advertisement
Guest User

Untitled

a guest
Dec 9th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.14 KB | None | 0 0
  1. import android.provider.Settings.Secure;
  2.  
  3. private String android_id = Secure.getString(getContext().getContentResolver(),
  4. Secure.ANDROID_ID);
  5.  
  6. final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
  7.  
  8. final String tmDevice, tmSerial, androidId;
  9. tmDevice = "" + tm.getDeviceId();
  10. tmSerial = "" + tm.getSimSerialNumber();
  11. androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
  12.  
  13. UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
  14. String deviceId = deviceUuid.toString();
  15.  
  16. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  17.  
  18. import android.content.Context;
  19. import android.content.SharedPreferences;
  20. import android.provider.Settings.Secure;
  21. import android.telephony.TelephonyManager;
  22.  
  23. import java.io.UnsupportedEncodingException;
  24. import java.util.UUID;
  25.  
  26. public class DeviceUuidFactory {
  27.  
  28. protected static final String PREFS_FILE = "device_id.xml";
  29. protected static final String PREFS_DEVICE_ID = "device_id";
  30. protected volatile static UUID uuid;
  31.  
  32. public DeviceUuidFactory(Context context) {
  33. if (uuid == null) {
  34. synchronized (DeviceUuidFactory.class) {
  35. if (uuid == null) {
  36. final SharedPreferences prefs = context
  37. .getSharedPreferences(PREFS_FILE, 0);
  38. final String id = prefs.getString(PREFS_DEVICE_ID, null);
  39. if (id != null) {
  40. // Use the ids previously computed and stored in the
  41. // prefs file
  42. uuid = UUID.fromString(id);
  43. } else {
  44. final String androidId = Secure.getString(
  45. context.getContentResolver(), Secure.ANDROID_ID);
  46. // Use the Android ID unless it's broken, in which case
  47. // fallback on deviceId,
  48. // unless it's not available, then fallback on a random
  49. // number which we store to a prefs file
  50. try {
  51. if (!"9774d56d682e549c".equals(androidId)) {
  52. uuid = UUID.nameUUIDFromBytes(androidId
  53. .getBytes("utf8"));
  54. } else {
  55. final String deviceId = (
  56. (TelephonyManager) context
  57. .getSystemService(Context.TELEPHONY_SERVICE))
  58. .getDeviceId();
  59. uuid = deviceId != null ? UUID
  60. .nameUUIDFromBytes(deviceId
  61. .getBytes("utf8")) : UUID
  62. .randomUUID();
  63. }
  64. } catch (UnsupportedEncodingException e) {
  65. throw new RuntimeException(e);
  66. }
  67. // Write the value out to the prefs file
  68. prefs.edit()
  69. .putString(PREFS_DEVICE_ID, uuid.toString())
  70. .commit();
  71. }
  72. }
  73. }
  74. }
  75. }
  76.  
  77. /**
  78. * Returns a unique UUID for the current android device. As with all UUIDs,
  79. * this unique ID is "very highly likely" to be unique across all Android
  80. * devices. Much more so than ANDROID_ID is.
  81. *
  82. * The UUID is generated by using ANDROID_ID as the base key if appropriate,
  83. * falling back on TelephonyManager.getDeviceID() if ANDROID_ID is known to
  84. * be incorrect, and finally falling back on a random UUID that's persisted
  85. * to SharedPreferences if getDeviceID() does not return a usable value.
  86. *
  87. * In some rare circumstances, this ID may change. In particular, if the
  88. * device is factory reset a new device ID may be generated. In addition, if
  89. * a user upgrades their phone from certain buggy implementations of Android
  90. * 2.2 to a newer, non-buggy version of Android, the device ID may change.
  91. * Or, if a user uninstalls your app on a device that has neither a proper
  92. * Android ID nor a Device ID, this ID may change on reinstallation.
  93. *
  94. * Note that if the code falls back on using TelephonyManager.getDeviceId(),
  95. * the resulting ID will NOT change after a factory reset. Something to be
  96. * aware of.
  97. *
  98. * Works around a bug in Android 2.2 for many devices when using ANDROID_ID
  99. * directly.
  100. *
  101. * @see http://code.google.com/p/android/issues/detail?id=10603
  102. *
  103. * @return a UUID that may be used to uniquely identify your device for most
  104. * purposes.
  105. */
  106. public UUID getDeviceUuid() {
  107. return uuid;
  108. }
  109. }
  110.  
  111. private static String uniqueID = null;
  112. private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
  113.  
  114. public synchronized static String id(Context context) {
  115. if (uniqueID == null) {
  116. SharedPreferences sharedPrefs = context.getSharedPreferences(
  117. PREF_UNIQUE_ID, Context.MODE_PRIVATE);
  118. uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
  119. if (uniqueID == null) {
  120. uniqueID = UUID.randomUUID().toString();
  121. Editor editor = sharedPrefs.edit();
  122. editor.putString(PREF_UNIQUE_ID, uniqueID);
  123. editor.commit();
  124. }
  125. }
  126. return uniqueID;
  127. }
  128.  
  129. WifiManager wm = (WifiManager)Ctxt.getSystemService(Context.WIFI_SERVICE);
  130. return wm.getConnectionInfo().getMacAddress();
  131.  
  132. String serial = null;
  133.  
  134. try {
  135. Class<?> c = Class.forName("android.os.SystemProperties");
  136. Method get = c.getMethod("get", String.class);
  137. serial = (String) get.invoke(c, "ro.serialno");
  138. } catch (Exception ignored) {
  139. }
  140.  
  141. String m_szDevIDShort = "35" + //we make this look like a valid IMEI
  142. Build.BOARD.length()%10+ Build.BRAND.length()%10 +
  143. Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 +
  144. Build.DISPLAY.length()%10 + Build.HOST.length()%10 +
  145. Build.ID.length()%10 + Build.MANUFACTURER.length()%10 +
  146. Build.MODEL.length()%10 + Build.PRODUCT.length()%10 +
  147. Build.TAGS.length()%10 + Build.TYPE.length()%10 +
  148. Build.USER.length()%10 ; //13 digits
  149.  
  150. public class Installation {
  151. private static String sID = null;
  152. private static final String INSTALLATION = "INSTALLATION";
  153.  
  154. public synchronized static String id(Context context) {
  155. if (sID == null) {
  156. File installation = new File(context.getFilesDir(), INSTALLATION);
  157. try {
  158. if (!installation.exists())
  159. writeInstallationFile(installation);
  160. sID = readInstallationFile(installation);
  161. } catch (Exception e) {
  162. throw new RuntimeException(e);
  163. }
  164. }
  165. return sID;
  166. }
  167.  
  168. private static String readInstallationFile(File installation) throws IOException {
  169. RandomAccessFile f = new RandomAccessFile(installation, "r");
  170. byte[] bytes = new byte[(int) f.length()];
  171. f.readFully(bytes);
  172. f.close();
  173. return new String(bytes);
  174. }
  175.  
  176. private static void writeInstallationFile(File installation) throws IOException {
  177. FileOutputStream out = new FileOutputStream(installation);
  178. String id = UUID.randomUUID().toString();
  179. out.write(id.getBytes());
  180. out.close();
  181. }
  182. }
  183.  
  184. String deviceId = Settings.System.getString(getContentResolver(),Settings.System.ANDROID_ID);
  185.  
  186. <application android:label="MyApplication"
  187. android:backupAgent="MyBackupAgent">
  188. ...
  189. <meta-data android:name="com.google.android.backup.api_key"
  190. android:value="your_backup_service_key" />
  191. </application>
  192.  
  193. public class MyBackupAgent extends BackupAgentHelper {
  194. // The name of the SharedPreferences file
  195. static final String PREFS = "user_preferences";
  196.  
  197. // A key to uniquely identify the set of backup data
  198. static final String PREFS_BACKUP_KEY = "prefs";
  199.  
  200. // Allocate a helper and add it to the backup agent
  201. @Override
  202. public void onCreate() {
  203. SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
  204. addHelper(PREFS_BACKUP_KEY, helper);
  205. }
  206. }
  207.  
  208. BackupManager backupManager = new BackupManager(context);
  209.  
  210. public static String getUserID(Context context) {
  211. private static String uniqueID = null;
  212. private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
  213. if (uniqueID == null) {
  214. SharedPreferences sharedPrefs = context.getSharedPreferences(
  215. MyBackupAgent.PREFS, Context.MODE_PRIVATE);
  216. uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
  217. if (uniqueID == null) {
  218. uniqueID = UUID.randomUUID().toString();
  219. Editor editor = sharedPrefs.edit();
  220. editor.putString(PREF_UNIQUE_ID, uniqueID);
  221. editor.commit();
  222.  
  223. //backup the changes
  224. BackupManager mBackupManager = new BackupManager(context);
  225. mBackupManager.dataChanged();
  226. }
  227. }
  228.  
  229. return uniqueID;
  230. }
  231.  
  232. if API => 9:(95.4% of devices)
  233.  
  234. return unique ID containing serial id(rooted phones may be different)
  235.  
  236. else
  237.  
  238. return unique ID of Build information(may overlap data - API < 9)
  239.  
  240. /**
  241. * Return Pseudo Unique ID
  242. * @return ID
  243. */
  244. public static String getUniquePsuedoID()
  245. {
  246. // IF all else fails, if the user does is lower than API 9(lower
  247. // than Gingerbread), has reset their phone or 'Secure.ANDROID_ID'
  248. // returns 'null', then simply the ID returned will be soley based
  249. // off their Android device information. This is where the collisions
  250. // can happen.
  251. // Thanks http://www.pocketmagic.net/?p=1662!
  252. // Try not to use DISPLAY, HOST or ID - these items could change
  253. // If there are collisions, there will be overlapping data
  254. String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10) + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10) + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10);
  255.  
  256. // Thanks to @Roman SL!
  257. // http://stackoverflow.com/a/4789483/950427
  258. // Only devices with API >= 9 have android.os.Build.SERIAL
  259. // http://developer.android.com/reference/android/os/Build.html#SERIAL
  260. // If a user upgrades software or roots their phone, there will be a duplicate entry
  261. String serial = null;
  262. try
  263. {
  264. serial = android.os.Build.class.getField("SERIAL").toString();
  265.  
  266. // go ahead and return the serial for api => 9
  267. return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
  268. }
  269. catch (Exception e)
  270. {
  271. // String needs to be initialized
  272. serial = "serial"; // some value
  273. }
  274.  
  275. // Thanks @Joe!
  276. // http://stackoverflow.com/a/2853253/950427
  277. // Finally, combine the values we have found by using the UUID class to create a unique identifier
  278. return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
  279. }
  280.  
  281. deviceId = Secure.getString(this.getContext().getContentResolver(), Secure.ANDROID_ID);
  282.  
  283. final TelephonyManager tm = (TelephonyManager) getBaseContext()
  284. .getSystemService(SplashActivity.TELEPHONY_SERVICE);
  285. final String tmDevice, tmSerial, androidId;
  286. tmDevice = "" + tm.getDeviceId();
  287. Log.v("DeviceIMEI", "" + tmDevice);
  288. tmSerial = "" + tm.getSimSerialNumber();
  289. Log.v("GSM devices Serial Number[simcard] ", "" + tmSerial);
  290. androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(),
  291. android.provider.Settings.Secure.ANDROID_ID);
  292. Log.v("androidId CDMA devices", "" + androidId);
  293. UUID deviceUuid = new UUID(androidId.hashCode(),
  294. ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
  295. String deviceId = deviceUuid.toString();
  296. Log.v("deviceIdUUID universally unique identifier", "" + deviceId);
  297. String deviceModelName = android.os.Build.MODEL;
  298. Log.v("Model Name", "" + deviceModelName);
  299. String deviceUSER = android.os.Build.USER;
  300. Log.v("Name USER", "" + deviceUSER);
  301. String devicePRODUCT = android.os.Build.PRODUCT;
  302. Log.v("PRODUCT", "" + devicePRODUCT);
  303. String deviceHARDWARE = android.os.Build.HARDWARE;
  304. Log.v("HARDWARE", "" + deviceHARDWARE);
  305. String deviceBRAND = android.os.Build.BRAND;
  306. Log.v("BRAND", "" + deviceBRAND);
  307. String myVersion = android.os.Build.VERSION.RELEASE;
  308. Log.v("VERSION.RELEASE", "" + myVersion);
  309. int sdkVersion = android.os.Build.VERSION.SDK_INT;
  310. Log.v("VERSION.SDK_INT", "" + sdkVersion);
  311.  
  312. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  313.  
  314. user@creep:~$ adb shell ls -l /sys/class/android_usb/android0/iSerial
  315. -rw-r--r-- root root 4096 2013-01-10 21:08 iSerial
  316. user@creep:~$ adb shell cat /sys/class/android_usb/android0/iSerial
  317. 0A3CXXXXXXXXXX5
  318.  
  319. import android.Manifest.permission;
  320. import android.bluetooth.BluetoothAdapter;
  321. import android.content.Context;
  322. import android.content.pm.PackageManager;
  323. import android.net.wifi.WifiManager;
  324. import android.provider.Settings.Secure;
  325. import android.telephony.TelephonyManager;
  326. import android.util.Log;
  327.  
  328. // TODO : hash
  329. public final class DeviceIdentifier {
  330.  
  331. private DeviceIdentifier() {}
  332.  
  333. /** @see http://code.google.com/p/android/issues/detail?id=10603 */
  334. private static final String ANDROID_ID_BUG_MSG = "The device suffers from "
  335. + "the Android ID bug - its ID is the emulator ID : "
  336. + IDs.BUGGY_ANDROID_ID;
  337. private static volatile String uuid; // volatile needed - see EJ item 71
  338. // need lazy initialization to get a context
  339.  
  340. /**
  341. * Returns a unique identifier for this device. The first (in the order the
  342. * enums constants as defined in the IDs enum) non null identifier is
  343. * returned or a DeviceIDException is thrown. A DeviceIDException is also
  344. * thrown if ignoreBuggyAndroidID is false and the device has the Android ID
  345. * bug
  346. *
  347. * @param ctx
  348. * an Android constant (to retrieve system services)
  349. * @param ignoreBuggyAndroidID
  350. * if false, on a device with the android ID bug, the buggy
  351. * android ID is not returned instead a DeviceIDException is
  352. * thrown
  353. * @return a *device* ID - null is never returned, instead a
  354. * DeviceIDException is thrown
  355. * @throws DeviceIDException
  356. * if none of the enum methods manages to return a device ID
  357. */
  358. public static String getDeviceIdentifier(Context ctx,
  359. boolean ignoreBuggyAndroidID) throws DeviceIDException {
  360. String result = uuid;
  361. if (result == null) {
  362. synchronized (DeviceIdentifier.class) {
  363. result = uuid;
  364. if (result == null) {
  365. for (IDs id : IDs.values()) {
  366. try {
  367. result = uuid = id.getId(ctx);
  368. } catch (DeviceIDNotUniqueException e) {
  369. if (!ignoreBuggyAndroidID)
  370. throw new DeviceIDException(e);
  371. }
  372. if (result != null) return result;
  373. }
  374. throw new DeviceIDException();
  375. }
  376. }
  377. }
  378. return result;
  379. }
  380.  
  381. private static enum IDs {
  382. TELEPHONY_ID {
  383.  
  384. @Override
  385. String getId(Context ctx) {
  386. // TODO : add a SIM based mechanism ? tm.getSimSerialNumber();
  387. final TelephonyManager tm = (TelephonyManager) ctx
  388. .getSystemService(Context.TELEPHONY_SERVICE);
  389. if (tm == null) {
  390. w("Telephony Manager not available");
  391. return null;
  392. }
  393. assertPermission(ctx, permission.READ_PHONE_STATE);
  394. return tm.getDeviceId();
  395. }
  396. },
  397. ANDROID_ID {
  398.  
  399. @Override
  400. String getId(Context ctx) throws DeviceIDException {
  401. // no permission needed !
  402. final String andoidId = Secure.getString(
  403. ctx.getContentResolver(),
  404. android.provider.Settings.Secure.ANDROID_ID);
  405. if (BUGGY_ANDROID_ID.equals(andoidId)) {
  406. e(ANDROID_ID_BUG_MSG);
  407. throw new DeviceIDNotUniqueException();
  408. }
  409. return andoidId;
  410. }
  411. },
  412. WIFI_MAC {
  413.  
  414. @Override
  415. String getId(Context ctx) {
  416. WifiManager wm = (WifiManager) ctx
  417. .getSystemService(Context.WIFI_SERVICE);
  418. if (wm == null) {
  419. w("Wifi Manager not available");
  420. return null;
  421. }
  422. assertPermission(ctx, permission.ACCESS_WIFI_STATE); // I guess
  423. // getMacAddress() has no java doc !!!
  424. return wm.getConnectionInfo().getMacAddress();
  425. }
  426. },
  427. BLUETOOTH_MAC {
  428.  
  429. @Override
  430. String getId(Context ctx) {
  431. BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
  432. if (ba == null) {
  433. w("Bluetooth Adapter not available");
  434. return null;
  435. }
  436. assertPermission(ctx, permission.BLUETOOTH);
  437. return ba.getAddress();
  438. }
  439. }
  440. // TODO PSEUDO_ID
  441. // http://www.pocketmagic.net/2011/02/android-unique-device-id/
  442. ;
  443.  
  444. static final String BUGGY_ANDROID_ID = "9774d56d682e549c";
  445. private final static String TAG = IDs.class.getSimpleName();
  446.  
  447. abstract String getId(Context ctx) throws DeviceIDException;
  448.  
  449. private static void w(String msg) {
  450. Log.w(TAG, msg);
  451. }
  452.  
  453. private static void e(String msg) {
  454. Log.e(TAG, msg);
  455. }
  456. }
  457.  
  458. private static void assertPermission(Context ctx, String perm) {
  459. final int checkPermission = ctx.getPackageManager().checkPermission(
  460. perm, ctx.getPackageName());
  461. if (checkPermission != PackageManager.PERMISSION_GRANTED) {
  462. throw new SecurityException("Permission " + perm + " is required");
  463. }
  464. }
  465.  
  466. // =========================================================================
  467. // Exceptions
  468. // =========================================================================
  469. public static class DeviceIDException extends Exception {
  470.  
  471. private static final long serialVersionUID = -8083699995384519417L;
  472. private static final String NO_ANDROID_ID = "Could not retrieve a "
  473. + "device ID";
  474.  
  475. public DeviceIDException(Throwable throwable) {
  476. super(NO_ANDROID_ID, throwable);
  477. }
  478.  
  479. public DeviceIDException(String detailMessage) {
  480. super(detailMessage);
  481. }
  482.  
  483. public DeviceIDException() {
  484. super(NO_ANDROID_ID);
  485. }
  486. }
  487.  
  488. public static final class DeviceIDNotUniqueException extends
  489. DeviceIDException {
  490.  
  491. private static final long serialVersionUID = -8940090896069484955L;
  492.  
  493. public DeviceIDNotUniqueException() {
  494. super(ANDROID_ID_BUG_MSG);
  495. }
  496. }
  497. }
  498.  
  499. String identifier = null;
  500. TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE));
  501. if (tm != null)
  502. identifier = tm.getDeviceId();
  503. if (identifier == null || identifier .length() == 0)
  504. identifier = Secure.getString(activity.getContentResolver(),Secure.ANDROID_ID);
  505.  
  506. public static String getDeviceId(Context ctx)
  507. {
  508. TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
  509.  
  510. String tmDevice = tm.getDeviceId();
  511. String androidId = Secure.getString(ctx.getContentResolver(), Secure.ANDROID_ID);
  512. String serial = null;
  513. if(Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) serial = Build.SERIAL;
  514.  
  515. if(tmDevice != null) return "01" + tmDevice;
  516. if(androidId != null) return "02" + androidId;
  517. if(serial != null) return "03" + serial;
  518. // other alternatives (i.e. Wi-Fi MAC, Bluetooth MAC, etc.)
  519.  
  520. return null;
  521. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement