package com.jiahaoliuliu.avoidduplicatedinstallations;

import android.content.Context;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.TextView;

import java.util.UUID;

import com.jiahaoliuliu.avoidduplicatedinstallations.Preferences.StringId;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private Context mContext;

    // Views
    private TextView mUuidTextView;

    private volatile UUID mUuid;
    private Object lock = new Object();
    private Preferences mPreferences;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = this;
        mPreferences = new Preferences(mContext);

        // link the views
        mUuidTextView = (TextView) findViewById(R.id.uuid_text_view);

        // Set the values
        mUuidTextView.setText(getUuid().toString());
        Log.v(TAG, "The uuid is " + mUuidTextView.getText().toString());
    }

    public UUID getUuid() {
        if (mUuid == null) {
            generateUUID();
        }

        return mUuid;
    }

    /**
     * Based on the code in StackOverFlow:
     * http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id Returns a
     * unique UUID for the current android device. As with all UUIDs, this unique ID is
     * "very highly likely" to be unique across all Android devices. Much more so than ANDROID_ID
     * is.
     *
     * The UUID is generated by using ANDROID_ID as the base key if appropriate, falling back on
     * TelephonyManager.getDeviceID() if ANDROID_ID is known to be incorrect, and finally falling
     * back on a random UUID that's persisted to SharedPreferences if getDeviceID() does not return
     * a usable value.
     *
     * In some rare circumstances, this ID may change. In particular, if the device is factory reset
     * a new device ID may be generated. In addition, if a user upgrades their phone from certain
     * buggy implementations of Android 2.2 to a newer, non-buggy version of Android, the device ID
     * may change. Or, if a user uninstalls your app on a device that has neither a proper Android
     * ID nor a Device ID, this ID may change on reinstallation.
     *
     * Note that if the code falls back on using TelephonyManager.getDeviceId(), the resulting ID
     * will NOT change after a factory reset. Something to be aware of.
     *
     * Works around a bug in Android 2.2 for many devices when using ANDROID_ID directly.
     *
     * @see http://code.google.com/p/android/issues/detail?id=10603
     *
     * @return a UUID that may be used to uniquely identify your device for most purposes.
     */
    private void generateUUID() {
        // If the uuid has not been generated before
        synchronized (lock) {
            // It could be that the UUID has been generated by other threads
            // that has the lock. Check it again
            if (mUuid == null) {
                final String id = mPreferences.get(StringId.UUID);
                if (id != null) {
                    // Use the ids previously computed and stored in the
                    // prefs file
                    mUuid = UUID.fromString(id);
                } else {
                    final String androidId = Settings.Secure.getString(
                            getContentResolver(), Settings.Secure.ANDROID_ID);
                    // Use the Android ID unless it's broken, in which case
                    // fallback on deviceId,
                    // unless it's not available, then fallback on a random
                    // number which we store to a prefs file
                    if (!"9774d56d682e549c".equals(androidId)) {
                        mUuid = UUID.nameUUIDFromBytes(androidId.getBytes());
                    } else {
                        final String deviceId = ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
                        mUuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes())
                                : UUID.randomUUID();
                    }
                    // Write the value out to the prefs file
                    mPreferences.set(StringId.UUID, mUuid.toString());
                }
            }
        }
    }

}

@@ -389,16 +389,52 @@
 tringId;
+%0Aimport com.parse.ParseInstallation;
 %0A%0Apublic
@@ -535,16 +535,104 @@
 tivity%22;
+%0A    private static final String PARSE_INSTALLATION_TABLE_COLUMN_UNIQUE_ID = %22uniqueId%22;
 %0A%0A    pr
@@ -1275,42 +1275,284 @@
 %22 + 
-m
+get
 Uuid
-TextView.getText().toString()
+().toString());%0A%0A        // Update the installation id%0A        ParseInstallation installation = ParseInstallation.getCurrentInstallation();%0A        installation.put(PARSE_INSTALLATION_TABLE_COLUMN_UNIQUE_ID, getUuid().toString());%0A        installation.saveInBackground(
 );%0A 
@@ -4893,11 +4893,10 @@
 %7D%0A    %7D%0A
-%0A
 %7D%0A

Create the column UniqueId for each installation