Android Open Source - cloudmine-android Device Identifier






From Project

Back to project page cloudmine-android.

License

The source code is released under:

Copyright (c) 2012 CloudMine LLC, http://cloudmine.me Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software")...

If you think the Android project cloudmine-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.cloudmine.api;
// www . jav  a  2 s . co  m
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.cloudmine.api.db.LocallySavableCMGeoPoint;
import com.cloudmine.api.persistance.ClassNameRegistry;
import com.cloudmine.api.rest.AndroidAsynchronousHttpClient;
import com.cloudmine.api.rest.AndroidBase64Encoder;
import com.cloudmine.api.rest.AndroidHeaderFactory;
import com.cloudmine.api.rest.HeaderFactory;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import java.util.UUID;

/**
 * An identifier that is unique per device per application. Uses SharedPreferences, so if the phone is
 * factory reset, a new identifier will be generated. Must be initialized before any calls to the
 * CloudMine api are made
 * <br>Copyright CloudMine LLC. All rights reserved<br> See LICENSE file included with SDK for details.
 */
public class DeviceIdentifier {

    public static final String UNIQUE_ID_KEY = "uniqueId";

    private static String uniqueId;


    /**
     * Retrieves the unique id for this application and device from the preferences. If this is the
     * first time the application has been run, a new unique id will be generated and saved in the
     * preferences
     * @param context the application context, accessable from an activity this.getApplicationContext()
     */
    public static void initialize(Context context) {
        //Not related to BaseDeviceIdentifier but we need to do the DI somewhere and this is as good as any
        LibrarySpecificClassCreator.setCreator(new LibrarySpecificClassCreator(new AndroidBase64Encoder(), new AndroidHeaderFactory(),
                new AndroidAsynchronousHttpClient()));
//                new VolleyAsynchronousHttpClient(context.getApplicationContext())));
        //Deserialize all geopoints as locally savable objects
        ClassNameRegistry.register(CMGeoPointInterface.GEOPOINT_CLASS, LocallySavableCMGeoPoint.class);
        ClassNameRegistry.register(JavaCMUser.CLASS_NAME, com.cloudmine.api.CMUser.class);
        SharedPreferences preferences = context.getSharedPreferences("CLOUDMINE_PREFERENCES", Context.MODE_PRIVATE);
        boolean isNotSet = !preferences.contains(UNIQUE_ID_KEY);
        if(isNotSet) {
            String uniqueId = generateUniqueDeviceIdentifier();
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(UNIQUE_ID_KEY, uniqueId);
            editor.commit();
        }
        uniqueId = preferences.getString(UNIQUE_ID_KEY, null); //null here so if we aren't getting the unique key, we fail hard
        if(uniqueId == null) {
            throw new RuntimeException("Unable to get unique id");
        } else {
            Log.e("CloudMine", "set unique id to " + uniqueId);
        }
    }

    /**
     * Get the unique identifier for this Device and application
     * @return the unique identifier
     * @throws RuntimeException if initialize has not been called
     */
    public static String getUniqueId() throws RuntimeException {
        if(uniqueId == null) {
            throw new RuntimeException("You must call BaseDeviceIdentifier.initialize before using the cloudmine api");
        }
        return uniqueId;
    }

    /**
     * Get the header that should be included with any requests to cloudmine
     * @return the header that should be included with any requests to cloudmine
     * @throws RuntimeException if initialize has not been called
     */
    public static Header getDeviceIdentifierHeader() throws RuntimeException {
        return new BasicHeader(HeaderFactory.DEVICE_HEADER_KEY, getUniqueId());
    }

    private static String generateUniqueDeviceIdentifier() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
}




Java Source Code List

com.cloudmine.api.AccessListController.java
com.cloudmine.api.BaseCMUser.java
com.cloudmine.api.BaseCacheableCMFile.java
com.cloudmine.api.CMAndroidSocial.java
com.cloudmine.api.CMCreditCard.java
com.cloudmine.api.CMSharedPreferencesConstants.java
com.cloudmine.api.CurrentHandlerHavers.java
com.cloudmine.api.DeviceIdentifier.java
com.cloudmine.api.HasHandler.java
com.cloudmine.api.LocallySavable.java
com.cloudmine.api.db.BaseLocallySavableCMAccessList.java
com.cloudmine.api.db.BaseLocallySavableCMObject.java
com.cloudmine.api.db.CMObjectDBOpenHelper.java
com.cloudmine.api.db.LocallySavableCMGeoPoint.java
com.cloudmine.api.db.RequestConstants.java
com.cloudmine.api.db.RequestDBObject.java
com.cloudmine.api.db.RequestDBOpenHelper.java
com.cloudmine.api.db.RequestPerformerService.java
com.cloudmine.api.gui.AuthenticationDialog.java
com.cloudmine.api.gui.VolleyAuthenticationDialog.java
com.cloudmine.api.loopj.AsyncHttpClient.java
com.cloudmine.api.loopj.AsyncHttpRequest.java
com.cloudmine.api.loopj.AsyncHttpResponseHandler.java
com.cloudmine.api.loopj.PersistentCookieStore.java
com.cloudmine.api.loopj.RequestParams.java
com.cloudmine.api.loopj.RetryHandler.java
com.cloudmine.api.loopj.SerializableCookie.java
com.cloudmine.api.loopj.SimpleMultipartEntity.java
com.cloudmine.api.rest.AndroidAsynchronousHttpClient.java
com.cloudmine.api.rest.AndroidBase64Encoder.java
com.cloudmine.api.rest.AndroidHeaderFactory.java
com.cloudmine.api.rest.AndroidUserCMWebService.java
com.cloudmine.api.rest.BaseAccessListCreateRequest.java
com.cloudmine.api.rest.BaseAccessListLoadRequest.java
com.cloudmine.api.rest.BaseAccessListModificationRequest.java
com.cloudmine.api.rest.BaseAddPaymentMethodRequest.java
com.cloudmine.api.rest.BaseChangeUserIdentifierRequest.java
com.cloudmine.api.rest.BaseChangeUserPasswordRequest.java
com.cloudmine.api.rest.BaseChargeCardRequest.java
com.cloudmine.api.rest.BaseFileCreationRequest.java
com.cloudmine.api.rest.BaseFileDeleteRequest.java
com.cloudmine.api.rest.BaseFileLoadRequest.java
com.cloudmine.api.rest.BaseImageLoadRequest.java
com.cloudmine.api.rest.BaseLoadPaymentMethodsRequest.java
com.cloudmine.api.rest.BaseLoadUserProfilesRequest.java
com.cloudmine.api.rest.BaseObjectDeleteRequest.java
com.cloudmine.api.rest.BaseObjectLoadRequest.java
com.cloudmine.api.rest.BaseObjectModificationRequest.java
com.cloudmine.api.rest.BaseProfileLoadRequest.java
com.cloudmine.api.rest.BaseProfileUpdateRequest.java
com.cloudmine.api.rest.BaseRemovePaymentMethodRequest.java
com.cloudmine.api.rest.BaseResetPasswordRequest.java
com.cloudmine.api.rest.BaseSnippetRequest.java
com.cloudmine.api.rest.BaseUserCreationRequest.java
com.cloudmine.api.rest.BaseUserLoginRequest.java
com.cloudmine.api.rest.BaseUserLogoutRequest.java
com.cloudmine.api.rest.CMImageLoader.java
com.cloudmine.api.rest.CMNetwork.java
com.cloudmine.api.rest.CloudMineRequest.java
com.cloudmine.api.rest.CompleteSocialLoginRequest.java
com.cloudmine.api.rest.DialogListener.java
com.cloudmine.api.rest.DiskBitmapCache.java
com.cloudmine.api.rest.ObjectLoadRequestBuilder.java
com.cloudmine.api.rest.OkHttpStack.java
com.cloudmine.api.rest.RequestBuilder.java
com.cloudmine.api.rest.SharedRequestQueueHolders.java
com.cloudmine.api.rest.VolleyAsynchronousHttpClient.java
com.cloudmine.api.rest.callbacks.AndroidCallback.java
com.cloudmine.api.rest.callbacks.PaymentResponseCallback.java
com.cloudmine.api.rest.response.PaymentResponse.java