All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hadoopz.MyDroidLib.util.IDroidUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2018 jw362j.
 *
 * 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 com.hadoopz.MyDroidLib.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import java.util.List;
import android.telephony.TelephonyManager;
import android.content.pm.Signature;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author jw362j
 */
public class IDroidUtils {

    public static boolean isPackageExists(Context context, String packageName) {
        if (context == null || TextUtils.isEmpty(packageName)) {
            return false;
        }
        PackageManager pm = context.getPackageManager();
        List packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
            if (packageName.equals(packageInfo.packageName)) {
                return true;
            }
        }
        return false;
    }

    public interface AppPackageWatcher {

        boolean onPackage(ApplicationInfo applicationInfo);
    }

    public static void loadAllPackage(Context context, AppPackageWatcher packageWatcher) {
        if (context == null || packageWatcher == null) {
            return;
        }
        PackageManager pm = context.getPackageManager();
        List appInfos = pm.getInstalledApplications(0);

        for (ApplicationInfo packageInfo : appInfos) {
            if (packageWatcher.onPackage(packageInfo)) {
                break;
            }
        }
    }

    public static boolean ishasSimCard(Context context) {
        TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        int simState = telMgr.getSimState();
        boolean result = true;
        switch (simState) {
            case TelephonyManager.SIM_STATE_ABSENT:
                result = false; // NO SIM
                break;
            case TelephonyManager.SIM_STATE_UNKNOWN:
                result = false;// NO SIM
                break;
        }
        //Log.d(TAG, result ? "has SIM" : "NO SIM");
        return result;
    }

    private static final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5',
        '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    private static String toHexString(byte[] bData) {
        StringBuilder sb = new StringBuilder(bData.length * 2);
        for (byte aBData : bData) {
            sb.append(HEX_DIGITS[(aBData & 0xf0) >>> 4]);
            sb.append(HEX_DIGITS[aBData & 0x0f]);
        }
        return sb.toString();
    }

    public static String getApkSHA256FingurePrint(Context context) {
        try {
            Signature[] signatures = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures;

            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            if (signatures != null) {
                for (Signature s : signatures) {
                    digest.update(s.toByteArray());
                }
            }
            return toHexString(digest.digest());
        } catch (PackageManager.NameNotFoundException ex) {
            return null;
        } catch (NoSuchAlgorithmException ex) {
            return null;
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy