package com.hdl.widget; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.support.annotation.NonNull; import android.util.Log; import java.util.List; /** * Created by JLChen on 2019/9/23 */ public final class HDLUtlisXM { public static final int DEFAULT_OFFLINE_COLOR = 0xC8E9E9EC; public static String MAuthority_NAME = "com.hdl.widgetxm.fileprovider"; public static final String CROP_TYPE_KEY = "Type"; public static final String CROP_NAME_KEY = "FileName"; public static final String CROP_RATIO_X_KEY = "ImageRatioX"; public static final String CROP_RATIO_Y_KEY = "ImageRatioY"; public static final String CROP_OUTPUT_Y_KEY = "ImageUotputY"; private HDLUtlisXM() { throw new UnsupportedOperationException("u can't instantiate me..."); } /** * dp转px * * @param dpValue dp值 * @return px值 */ public static int dp2px(Context context, final float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * px转dp * * @param pxValue px值 * @return dp值 */ public static int px2dp(Context context, final float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * Relaunch the application. */ public static void relaunchApp(Context mContext) { relaunchApp(mContext, false); } /** * Relaunch the application. * * @param isKillProcess True to kill the process, false otherwise. */ public static void relaunchApp(Context mContext, final boolean isKillProcess) { Intent intent = getLaunchAppIntent(mContext, mContext.getPackageName(), true); if (intent == null) { Log.e("AppUtils", "Didn't exist launcher activity."); return; } intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK ); mContext.startActivity(intent); if (!isKillProcess) return; android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); } private static Intent getLaunchAppIntent(Context mContext, final String packageName) { return getLaunchAppIntent(mContext, packageName, false); } private static Intent getLaunchAppIntent(Context mContext, final String packageName, final boolean isNewTask) { String launcherActivity = getLauncherActivity(mContext, packageName); if (!launcherActivity.isEmpty()) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); ComponentName cn = new ComponentName(packageName, launcherActivity); intent.setComponent(cn); return isNewTask ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) : intent; } return null; } private static String getLauncherActivity(Context mContext, @NonNull final String pkg) { Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setPackage(pkg); PackageManager pm = mContext.getPackageManager(); List info = pm.queryIntentActivities(intent, 0); int size = info.size(); if (size == 0) return ""; for (int i = 0; i < size; i++) { ResolveInfo ri = info.get(i); if (ri.activityInfo.processName.equals(pkg)) { return ri.activityInfo.name; } } return info.get(0).activityInfo.name; } // /** // * 生成二维码,默认大小为500*500 // * // * @param url 需要生成二维码的网址,也可以是文字 // * @return bitmap // */ // public static Bitmap createQRCode(String url) { // return createQRCode(url, 500); // } // // /** // * 生成二维码,默认大小为500*500 // * // * @param url 需要生成二维码的网址,也可以是文字 // * @param size 需要生成二维码的大小() // * @return bitmap // */ // public static Bitmap createQRCode(String url, int size) { // try { // Hashtable hints = new Hashtable<>(); // hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // BitMatrix bitMatrix = new QRCodeWriter().encode(url, // BarcodeFormat.QR_CODE, size, size, hints); // int[] pixels = new int[size * size]; // for (int y = 0; y < size; y++) { // for (int x = 0; x < size; x++) { // if (bitMatrix.get(x, y)) { // pixels[y * size + x] = 0xff000000; // } else { // pixels[y * size + x] = 0xffffffff; // } // // } // } // Bitmap bitmap = Bitmap.createBitmap(size, size, // Bitmap.Config.ARGB_8888); // bitmap.setPixels(pixels, 0, size, 0, 0, size, size); // return bitmap; // } catch (WriterException e) { // e.printStackTrace(); // return null; // } // } // // /** // * @param url 需要生成二维码的文字、网址等 // * @param size 需要生成二维码的大小() // * @param mBitmap logo文件 // * @return bitmap // */ // private static int IMAGE_HALFWIDTH = 50;//宽度值,影响中间图片大小 // // public static Bitmap createQRCodeWithLogo(String url, Bitmap mBitmap) { // return createQRCodeWithLogo(url, 500, mBitmap); // } // // public static Bitmap createQRCodeWithLogo(String url, int size, Bitmap mBitmap) { // try { // IMAGE_HALFWIDTH = size / 10; // Hashtable hints = new Hashtable<>(); // hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // /* // * 设置容错级别,默认为ErrorCorrectionLevel.L // * 因为中间加入logo所以建议你把容错级别调至H,否则可能会出现识别不了 // */ // hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // BitMatrix bitMatrix = new QRCodeWriter().encode(url, // BarcodeFormat.QR_CODE, size, size, hints); // // int width = bitMatrix.getWidth();//矩阵高度 // int height = bitMatrix.getHeight();//矩阵宽度 // int halfW = width / 2; // int halfH = height / 2; // // Matrix m = new Matrix(); // float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth(); // float sy = (float) 2 * IMAGE_HALFWIDTH // / mBitmap.getHeight(); // m.setScale(sx, sy); // //设置缩放信息 // //将logo图片按martix设置的信息缩放 // mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, // mBitmap.getWidth(), mBitmap.getHeight(), m, false); // // int[] pixels = new int[size * size]; // for (int y = 0; y < size; y++) { // for (int x = 0; x < size; x++) { // if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH // && y > halfH - IMAGE_HALFWIDTH // && y < halfH + IMAGE_HALFWIDTH) { // //该位置用于存放图片信息 // //记录图片每个像素信息 // pixels[y * width + x] = mBitmap.getPixel(x - halfW // + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH); // } else { // if (bitMatrix.get(x, y)) { // pixels[y * size + x] = 0xff000000; // } else { // pixels[y * size + x] = 0xffffffff; // } // } // } // } // Bitmap bitmap = Bitmap.createBitmap(size, size, // Bitmap.Config.ARGB_8888); // bitmap.setPixels(pixels, 0, size, 0, 0, size, size); // return bitmap; // } catch (WriterException e) { // e.printStackTrace(); // return null; // } // } }