it.scsoft.tiuuid.TitaniumUUIDModule.java Source code

Java tutorial

Introduction

Here is the source code for it.scsoft.tiuuid.TitaniumUUIDModule.java

Source

/**
 * This file was auto-generated by the Titanium Module SDK helper for Android
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 *
 */
package it.scsoft.tiuuid;

import java.util.UUID;
import java.math.BigInteger;
import java.nio.charset.Charset;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;

import org.appcelerator.titanium.TiApplication;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;

import android.content.Context;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.content.pm.PackageManager;
import android.Manifest;
import android.support.v4.content.ContextCompat;
import android.util.Base64;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Kroll.module(name = "TitaniumUUID", id = "it.scsoft.tiuuid")
public class TitaniumUUIDModule extends KrollModule {

    // Standard Debugging variables
    private static final String LCAT = "TitaniumUUIDModule";
    private static final boolean DBG = TiConfig.LOGD;

    // You can define constants with @Kroll.constant, for example:
    // @Kroll.constant public static final String EXTERNAL_NAME = value;

    public TitaniumUUIDModule() {
        super();
    }

    @Kroll.onAppCreate
    public static void onAppCreate(TiApplication app) {
        Log.d(LCAT, "inside onAppCreate");
        // put module init code that needs to run when the application is created
    }

    // Methods
    @Kroll.method
    public String uuidForDevice() {

        // Serial
        String serial = android.os.Build.SERIAL;

        // IMEI
        String imei = "";
        if (ContextCompat.checkSelfPermission(TiApplication.getInstance().getApplicationContext(),
                Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            TelephonyManager telephonyManager = (TelephonyManager) TiApplication.getInstance()
                    .getSystemService(Context.TELEPHONY_SERVICE);
            imei = telephonyManager.getDeviceId();
        }

        // AndroidID
        String androidID = Secure.getString(TiApplication.getInstance().getContentResolver(), Secure.ANDROID_ID);

        // uuid
        String uuid = serial + imei + androidID;

        // MD5 hash via http://stackoverflow.com/questions/3934331/how-to-hash-a-string-in-android
        String hash = "";
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
            digest.update(uuid.getBytes(Charset.forName("US-ASCII")), 0, uuid.length());
            byte[] magnitude = digest.digest();
            BigInteger bi = new BigInteger(1, magnitude);
            hash = String.format("%0" + (magnitude.length << 1) + "x", bi);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return hash;
    }
}