Android Open Source - mclib Identity Helper






From Project

Back to project page mclib.

License

The source code is released under:

Apache License, Version 2.0 FoundationProjectsPeopleGet InvolvedDownloadSupport ApacheHome ? Licenses Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITION...

If you think the Android project mclib 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

/*
   Copyright 2012 Mikhail Chabanov/*from  w  ww .  jav  a 2 s. com*/

   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.
 */
package mc.lib.identity;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedList;

import mc.lib.log.Log;
import mc.lib.regexp.Validator;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;

public class IdentityHelper
{
    private static final String LOGTAG = IdentityHelper.class.getSimpleName();

    /**
     * 
     * @param context
     * @return andriod device ID
     */
    public static String getAndroidId(Context context)
    {
        return android.provider.Settings.System.getString(context.getContentResolver(), Secure.ANDROID_ID);
    }

    /**
     * 
     * @param context
     * @return IMEI
     */
    public static String getIMEI(Context context)
    {
        return ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    }

    /**
     * 
     * @param context
     * @return Sim Card ID
     */
    public static String getIMSI(Context context)
    {
        return ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getSimSerialNumber();
    }

    public static Account[] getAccounts(Context context)
    {
        return AccountManager.get(context).getAccounts();
    }

    public static Account[] getEmailAccounts(Context context)
    {
        LinkedList<Account> res = new LinkedList<Account>();
        for(Account a : getAccounts(context))
        {
            if(Validator.validEMail(a.name))
            {
                res.add(a);
            }
        }
        return res.toArray(new Account[res.size()]);
    }

    public static Account[] getGoogleAccounts(Context context)
    {
        return AccountManager.get(context).getAccountsByType("com.google");
    }

    public static String getMD5Hash(String src)
    {
        return toHexString(getMD5HashAsBytes(src));
    }

    public static byte[] getMD5HashAsBytes(String src)
    {
        try
        {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(src.getBytes());
            return md.digest();
        }
        catch(NoSuchAlgorithmException e)
        {
            Log.e(LOGTAG, "This device doesn't support MD5", e);
        }
        return null;
    }

    public static String getSHA256Hash(String src)
    {
        return toHexString(getSHA256HashAsBytes(src));
    }

    public static byte[] getSHA256HashAsBytes(String src)
    {
        try
        {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(src.getBytes());
            return md.digest();
        }
        catch(NoSuchAlgorithmException e)
        {
            Log.e(LOGTAG, "This device doesn't support SHA-256", e);
        }
        return null;
    }

    private static String toHexString(byte[] bytes)
    {
        StringBuffer res = new StringBuffer();
        for(int i = 0; i < bytes.length; i++)
        {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if(hex.length() == 1)
            {
                res.append("0");
            }
            res.append(hex);
        }
        return res.toString();
    }
}




Java Source Code List

mc.lib.archives.ArchivesHelper.java
mc.lib.assets.AssetsHelper.java
mc.lib.audio.AudioSource.java
mc.lib.audio.IAudioPlayer.java
mc.lib.audio.MediaAudioPlayer.java
mc.lib.files.FileHelper.java
mc.lib.files.FileReadingHelper.java
mc.lib.files.PermissionHelper.java
mc.lib.identity.IdentityHelper.java
mc.lib.image.BitmapTools.java
mc.lib.image.ImageHelper.java
mc.lib.image.InputStreamFullSkip.java
mc.lib.interfaces.OnCompleteListener.java
mc.lib.interfaces.OnProgressListener.java
mc.lib.interfaces.OnStartListener.java
mc.lib.io.KeyboardHelper.java
mc.lib.io.ToastHelper.java
mc.lib.location.LocationHelper.java
mc.lib.location.OnLocationChangeListener.java
mc.lib.log.Log.java
mc.lib.network.AsyncFileDownloader.java
mc.lib.network.AsyncNetworkHelper.java
mc.lib.network.EmailHelper.java
mc.lib.network.NetworkHelper.java
mc.lib.regexp.PopularRegexp.java
mc.lib.regexp.Validator.java
mc.lib.screen.ScreenHelper.java
mc.lib.stream.AdvancedStreamHelper.java
mc.lib.stream.StreamHelper.java
mc.lib.video.BasicVideoPlayer.java
mc.lib.video.IVideoPlayer.java
mc.lib.video.VideoViewPlayer.java