Android Device Information Get getDeviceUniqueIdentificator( @Nonnull Context context, @Nonnull String appId)

Here you can find the source of getDeviceUniqueIdentificator( @Nonnull Context context, @Nonnull String appId)

Description

Generates an unique, stable UID that identifies the device where the user is currently logged.

License

Apache License

Parameter

Parameter Description
context A Context to retrieve data from the TelephonyManager and ContentResolver
appId An unique and possibly secret app ID to "secure" the generated value

Return

An alphanumeric (+ hyphens) string

Declaration

@Nonnull
public static String getDeviceUniqueIdentificator(
        @Nonnull Context context, @Nonnull String appId) 

Method Source Code

//package com.java2s;
/*/*  w  w  w. j a v a2 s  .  co m*/
 * Copyright 2013 Luluvise Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.UUID;

import javax.annotation.Nonnull;

import android.annotation.TargetApi;

import android.content.Context;

import android.content.pm.PackageManager;

import android.os.Build;

import android.telephony.TelephonyManager;

public class Main {
    /**
     * Generates an unique, stable UID that identifies the device where the user
     * is currently logged.
     * 
     * @param context
     *            A {@link Context} to retrieve data from the
     *            {@link TelephonyManager} and {@link ContentResolver}
     * @param appId
     *            An unique and possibly secret app ID to "secure" the generated
     *            value
     * @return An alphanumeric (+ hyphens) {@link UUID} string
     */
    @Nonnull
    public static String getDeviceUniqueIdentificator(
            @Nonnull Context context, @Nonnull String appId) {
        String telephonyId = null;
        String androidId = null;

        if (hasTelephony(context)) {
            final TelephonyManager telephony = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            telephonyId = telephony.getDeviceId();
        }
        if (telephonyId == null) {
            telephonyId = "";
        }

        if (Build.VERSION.SDK_INT >= 9) { // Build.VERSION_CODES.GINGERBREAD
            androidId = getAndroidBuildSerial();
        } else {
            androidId = android.provider.Settings.Secure.getString(
                    context.getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);
        }
        // if ("9774d56d682e549c".equals(androidId)) { // broken Android ID
        // TODO: fallback
        // }
        if (androidId == null) {
            androidId = "";
        }

        UUID deviceUuid = new UUID(androidId.hashCode(),
                ((long) telephonyId.hashCode() << 32) | appId.hashCode());

        return deviceUuid.toString();
    }

    /**
     * Checks if the device has the {@link PackageManager#FEATURE_TELEPHONY}.
     * 
     * @param context
     * @return true if the telephony is available, false otherwise
     */
    public static boolean hasTelephony(@Nonnull Context context) {
        PackageManager manager = context.getPackageManager();
        return manager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
    }

    /**
     * Returns the {@link #android.os.Build.SERIAL} string.<br>
     * Not guaranteed to be unique and not null, only available from API 9.
     */
    @TargetApi(9)
    private static String getAndroidBuildSerial() {
        return android.os.Build.SERIAL;
    }
}

Related

  1. getDeviceType(Context ctx)
  2. getDeviceModel()
  3. getDeviceCpuCores()
  4. deviceBeRoot()
  5. getDeviceCpuCores()
  6. get_device_id(Context ctx)