Java tutorial
//package com.java2s; /** * AdFlakeUtil.java (AdFlakeSDK-Android) * * Copyright 2013 MADE GmbH - All Rights Reserved. * * Unauthorized copying of this file, via any medium is strictly prohibited * unless otherwise noted in the License section of this document header. * * @file AdFlakeUtil.java * @copyright 2013 MADE GmbH. All rights reserved. * @section License * 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 android.content.Context; import android.os.Build; import android.provider.Settings; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Locale; public class Main { /** * Gets the md5 hashed and upper-cased device id. * * @param context * the application context. * @return The encoded device id. */ public static String getEncodedDeviceId(Context context) { String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); String hashedId; if ((androidId == null) || isEmulator()) { hashedId = md5("emulator"); } else { hashedId = md5(androidId); } if (hashedId == null) { return null; } return hashedId.toUpperCase(Locale.US); } /** * Checks whether or not the running device is an emulator. * * @return Boolean indicating if the app is currently running in an * emulator. */ public static boolean isEmulator() { return (Build.BOARD.equals("unknown") && Build.DEVICE.equals("generic") && Build.BRAND.equals("generic")); } /** * Method for returning an md5 hash of a string. * * @param val * the string to hash. * @return A hex string representing the md5 hash of the input. */ private static String md5(String val) { String result = null; if ((val != null) && (val.length() > 0)) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(val.getBytes(), 0, val.length()); result = String.format("%032X", new BigInteger(1, md5.digest())); } catch (NoSuchAlgorithmException nsae) { result = val.substring(0, 32); } } return result; } }