public class OpenUDID_manager implements ServiceConnection {
final static String PREF_KEY = "openudid";
final static String PREFS_NAME = "openudid_prefs";
private final Context mContext; //Application context
private List<ResolveInfo> mMatchingIntents; //List of available OpenUDID Intents
private final Map<String, Integer> mReceivedOpenUDIDs; //Map of OpenUDIDs found so far
private final SharedPreferences mPreferences; //Preferences to store the OpenUDID
private final Random mRandom;
private OpenUDID_manager(Context context) {
mPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
mContext = context;
mRandom = new Random();
mReceivedOpenUDIDs = new HashMap<String, Integer>();
}
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
//Get the OpenUDID from the remote service
try {
//Send a random number to the service
android.os.Parcel data = android.os.Parcel.obtain();
data.writeInt(mRandom.nextInt());
android.os.Parcel reply = android.os.Parcel.obtain();
service.transact(1, android.os.Parcel.obtain(), reply, 0);
if (data.readInt() == reply.readInt()) //Check if the service returns us this number
{
final String _openUDID = reply.readString();
if (_openUDID != null) { //if valid OpenUDID, save it
if (mReceivedOpenUDIDs.containsKey(_openUDID)) mReceivedOpenUDIDs.put(_openUDID, mReceivedOpenUDIDs.get(_openUDID) + 1);
else mReceivedOpenUDIDs.put(_openUDID, 1);
}
}
} catch (RemoteException ignored) {}
mContext.unbindService(this);
startService();
}
@Override
public void onServiceDisconnected(ComponentName className) {}
@SuppressLint("ApplySharedPref")
private void storeOpenUDID() {
mPreferences.edit().putString(PREF_KEY, OpenUDID).commit();
}
/*
* Generate a new OpenUDID
*/
private void generateOpenUDID() {
//Try to get the ANDROID_ID
OpenUDID = Secure.getString(mContext.getContentResolver(), Secure.ANDROID_ID);
if (OpenUDID == null || OpenUDID.equals("9774d56d682e549c") || OpenUDID.length() < 15 ) {
//if ANDROID_ID is null, or it's equals to the GalaxyTab generic ANDROID_ID or bad, generates a new one
final SecureRandom random = new SecureRandom();
OpenUDID = new BigInteger(64, random).toString(16);
}
}
/*
* Start the oldest service
*/
private void startService() {
if (mMatchingIntents.size() > 0) { //There are some Intents untested
final ServiceInfo servInfo = mMatchingIntents.get(0).serviceInfo;
final Intent i = new Intent();
i.setComponent(new ComponentName(servInfo.applicationInfo.packageName, servInfo.name));
mMatchingIntents.remove(0);
try { // try added by Lionscribe
mContext.bindService(i, this, Context.BIND_AUTO_CREATE);
}
catch (SecurityException e) {
startService(); // ignore this one, and start next one
}
} else { //No more service to test
getMostFrequentOpenUDID(); //Choose the most frequent
if (OpenUDID == null) //No OpenUDID was chosen, generate one
generateOpenUDID();
storeOpenUDID();//Store it locally
mInitialized = true;
}
}
private void getMostFrequentOpenUDID() {
if (!mReceivedOpenUDIDs.isEmpty()) {
final TreeMap<String,Integer> sorted_OpenUDIDS = new TreeMap(new ValueComparator());
sorted_OpenUDIDS.putAll(mReceivedOpenUDIDs);
OpenUDID = sorted_OpenUDIDS.firstKey();
}
}
private static String OpenUDID = null;
private static boolean mInitialized = false;
public static String getDeviceID() {
return OpenUDID_manager.isInitialized() ? OpenUDID_manager.getOpenUDID() : "";
}
/**
* The Method to call to get OpenUDID
* @return the OpenUDID
*/
private static String getOpenUDID() {
return OpenUDID;
}
/**
* The Method to call to get OpenUDID
* @return the OpenUDID
*/
private static boolean isInitialized() {
return mInitialized;
}
/**
* The Method the call at the setMode of your app
* @param context you current context
*/
public static void sync(Context context) {
//Initialise the Manager
OpenUDID_manager manager = new OpenUDID_manager(context);
//Try to get the openudid from local preferences
OpenUDID = manager.mPreferences.getString(PREF_KEY, null);
if (OpenUDID == null) //Not found
{
//Get the list of all OpenUDID services available (including itself)
manager.mMatchingIntents = context.getPackageManager().queryIntentServices(new Intent("org.OpenUDID.GETUDID"), 0);
if (manager.mMatchingIntents != null)
//Start services one by one
manager.startService();
} else {//Got it, you can now call getOpenUDID()
mInitialized = true;
}
}
/*
* Used to sort the OpenUDIDs collected by occurrence
*/
private class ValueComparator implements Comparator {
public int compare(Object a, Object b) {
if(mReceivedOpenUDIDs.get(a) < mReceivedOpenUDIDs.get(b)) {
return 1;
} else if(mReceivedOpenUDIDs.get(a) == mReceivedOpenUDIDs.get(b)) {
return 0;
} else {
return -1;
}
}
}
}
public class OpenUDID_service extends Service {
@Override
public IBinder onBind(Intent arg0) {
return new Binder() {
@Override
public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) {
final SharedPreferences preferences = getSharedPreferences(OpenUDID_manager.PREFS_NAME, Context.MODE_PRIVATE);
reply.writeInt(data.readInt()); //Return to the sender the input random number
reply.writeString(preferences.getString(OpenUDID_manager.PREF_KEY, null));
return true;
}
};
}
}
Добавить в манифест
<service android:name=".util.openUDID.OpenUDID_service">
<intent-filter>
<action android:name="org.openudid.GETUDID" />
</intent-filter>
</service>
Так получаем udid
String udid = OpenUDID_manager.getDeviceID();