388 lines
16 KiB
Java
388 lines
16 KiB
Java
package com.xxpatx.os.utils;
|
||
|
||
import android.annotation.SuppressLint;
|
||
import android.app.role.RoleManager;
|
||
import android.content.ComponentName;
|
||
import android.content.Context;
|
||
import android.content.ContextWrapper;
|
||
import android.content.Intent;
|
||
import android.content.IntentFilter;
|
||
import android.content.pm.PackageManager;
|
||
import android.content.pm.ResolveInfo;
|
||
import android.content.res.Configuration;
|
||
import android.graphics.Bitmap;
|
||
import android.graphics.BitmapFactory;
|
||
import android.graphics.Canvas;
|
||
import android.graphics.Paint;
|
||
import android.graphics.PorterDuff;
|
||
import android.graphics.PorterDuffXfermode;
|
||
import android.net.Uri;
|
||
import android.os.BatteryManager;
|
||
import android.os.Build;
|
||
import android.os.Environment;
|
||
import android.os.UserHandle;
|
||
import android.telecom.PhoneAccountHandle;
|
||
import android.telecom.TelecomManager;
|
||
import android.telephony.TelephonyManager;
|
||
import android.text.TextUtils;
|
||
import android.util.Log;
|
||
|
||
import androidx.core.content.ContextCompat;
|
||
|
||
import com.xxpatx.os.BuildConfig;
|
||
import com.xxpatx.os.R;
|
||
|
||
import java.io.File;
|
||
import java.lang.reflect.Method;
|
||
import java.util.List;
|
||
import java.util.concurrent.Executor;
|
||
import java.util.function.Consumer;
|
||
|
||
public class Utils {
|
||
private static final String TAG = "Utils";
|
||
|
||
/**
|
||
* 获取设备序列号
|
||
*
|
||
* @return
|
||
*/
|
||
@SuppressLint("MissingPermission")
|
||
public static String getSerial() {
|
||
String serial = "unknow";
|
||
try {
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {//9.0+
|
||
serial = Build.getSerial();
|
||
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {//8.0+
|
||
serial = Build.SERIAL;
|
||
} else {//8.0-
|
||
Class<?> c = Class.forName("android.os.SystemProperties");
|
||
Method get = c.getMethod("get", String.class);
|
||
serial = (String) get.invoke(c, "ro.serialno");
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
Log.e("e", "读取设备序列号异常:" + e.toString());
|
||
}
|
||
if (BuildConfig.DEBUG) {
|
||
// return "QNG2DKB00463";
|
||
}
|
||
return serial;
|
||
}
|
||
|
||
public static String getDeviceSN() {
|
||
String serial = null;
|
||
try {
|
||
Class<?> c = Class.forName("android.os.SystemProperties");
|
||
Method get = c.getMethod("get", String.class);
|
||
serial = (String) get.invoke(c, "persist.sys.hrSerial");
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
return serial;
|
||
}
|
||
|
||
/**
|
||
* 获取电量
|
||
*
|
||
* @param mContext
|
||
* @return
|
||
*/
|
||
synchronized public static int getBatteryLevel(Context mContext) {
|
||
if (Build.VERSION.SDK_INT >= 21) {
|
||
return ((BatteryManager) mContext.getSystemService(Context.BATTERY_SERVICE)).getIntProperty(4);
|
||
} else {
|
||
Intent intent = (new ContextWrapper(mContext)).registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
|
||
return intent.getIntExtra("level", -1) * 100 / intent.getIntExtra("scale", -1);
|
||
}
|
||
}
|
||
|
||
public static String getDownLoadPath(Context context) {
|
||
String path = ContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_DOWNLOADS)[0].getAbsolutePath();
|
||
return path + File.separator;
|
||
}
|
||
|
||
public static String getFileNamefromURL(String url) {
|
||
int position = url.lastIndexOf("/");
|
||
return url.substring(position + 1);
|
||
}
|
||
|
||
public static void openLauncher3(Context context) {
|
||
setDefaultDesktop(context, Launcher3, Launcher3Class);
|
||
// ApkUtils.openPackage(mContext, Launcher3);
|
||
gotoLauncher(context);
|
||
}
|
||
|
||
public static void gotoLauncher(Context context) {
|
||
Intent i = new Intent(Intent.ACTION_MAIN);
|
||
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //android123提示如果是服务里调用,必须加入new task标识
|
||
i.addCategory(Intent.CATEGORY_HOME);
|
||
context.startActivity(i);
|
||
}
|
||
|
||
private static String Launcher3 = "com.android.launcher3";
|
||
private static String Launcher3Class = "com.android.launcher3.Launcher";
|
||
|
||
public static void setDefaultDesktop(Context context, String pkg, String className) {
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||
setRoleHolderAsUser(context, pkg);
|
||
Log.e(TAG, "setDefaultDesktop: setRoleHolderAsUser");
|
||
} else {
|
||
//爱华设置,暂时屏蔽
|
||
// setDefaultLauncher(mContext, pkg, className);
|
||
Log.e(TAG, "setDefaultDesktop: setDefaultLauncher");
|
||
}
|
||
// String oldDesktop = (String) SPUtils.get(mContext, "default_launcher", "");
|
||
// if (Objects.equals(oldDesktop, pkg)) {
|
||
// Log.e(TAG, "setDefaultDesktop: " + "数据一致");
|
||
// return;
|
||
// }
|
||
Intent intent = new Intent("setDefaultLauncher");
|
||
intent.putExtra("package", pkg);
|
||
intent.putExtra("className", className);
|
||
// if (JGYUtils.getInstance().checkAppPlatform() == MTKPlatform) {
|
||
Log.e(TAG, "setDefaultDesktop: MTK");
|
||
//爱华定制
|
||
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.AoleReceiver"));
|
||
// TODO: 2022/7/6 有问题
|
||
setDefaultLauncher(context, "com.android.transfer", "com.android.transfer.OldMainActivity");
|
||
// SystemProperties.set("persist.sys.launcher.pkgname", pkg);
|
||
// SystemProperties.set("persist.sys.launcher.classname", className);
|
||
// }
|
||
intent.setPackage("com.android.settings");
|
||
context.sendBroadcast(intent);
|
||
// ApkUtils.openPackage(mContext, pkg);
|
||
Log.e(TAG, "setDefaultDesktop: " + pkg + ":" + className);
|
||
// Log.e(TAG, "setDefaultDesktop: " + "persist.sys.launcher.pkgname = " + SystemProperties.get("persist.sys.launcher.pkgname"));
|
||
// Log.e(TAG, "setDefaultDesktop: " + "persist.sys.launcher.classname = " + SystemProperties.get("persist.sys.launcher.classname"));
|
||
}
|
||
|
||
public static void setDefaultLauncher(Context context, String defPackageName, String defClassName) {
|
||
try {
|
||
if (!TextUtils.isEmpty(defPackageName) && !TextUtils.isEmpty(defClassName)) {
|
||
IntentFilter filter = new IntentFilter();
|
||
filter.addAction("android.intent.action.MAIN");
|
||
filter.addCategory("android.intent.category.HOME");
|
||
filter.addCategory("android.intent.category.DEFAULT");
|
||
Intent intent = new Intent(Intent.ACTION_MAIN);
|
||
intent.addCategory(Intent.CATEGORY_HOME);
|
||
// 返回给定条件的所有ResolveInfo对象(本质上是Activity)
|
||
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
|
||
int bestMatch = 0;
|
||
final int size = list.size();
|
||
ComponentName[] set = new ComponentName[size];
|
||
for (int i = 0; i < size; i++) {
|
||
ResolveInfo ri = list.get(i);
|
||
set[i] = new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name);
|
||
if (ri.match > bestMatch) {
|
||
bestMatch = ri.match;
|
||
}
|
||
}
|
||
ComponentName preActivity = new ComponentName(defPackageName, defClassName);
|
||
context.getPackageManager().addPreferredActivity(filter, bestMatch, set, preActivity);
|
||
}
|
||
} catch (java.lang.SecurityException e) {
|
||
e.printStackTrace();
|
||
Log.e(TAG, "setDefaultLauncher: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
|
||
public static void setRoleHolderAsUser(Context context, String packageName) {
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||
String roleName = "android.app.role.HOME";
|
||
boolean add = true;
|
||
int flags = 0;
|
||
UserHandle user = android.os.Process.myUserHandle();
|
||
Log.i("settingssssssstemf", (add ? "Adding" : "Removing") + " package as role holder, role: "
|
||
+ roleName + ", package: " + packageName);
|
||
// if (JGYUtils.getInstance().checkAppPlatform() != JGYUtils.MTKPlatform) {
|
||
RoleManager roleManager = context.getSystemService(RoleManager.class);
|
||
Executor executor = context.getMainExecutor();
|
||
Consumer<Boolean> callback = successful -> {
|
||
if (successful) {
|
||
Log.d("settingssssssstemf", "Package " + (add ? "added" : "removed")
|
||
+ " as role holder, role: " + roleName + ", package: " + packageName);
|
||
} else {
|
||
Log.d("settingssssssstemf", "Failed to " + (add ? "add" : "remove")
|
||
+ " package as role holder, role: " + roleName + ", package: "
|
||
+ packageName);
|
||
}
|
||
};
|
||
// roleManager.addRoleHolderAsUser(roleName, packageName, flags, user, executor, callback);
|
||
Log.i("settingssssssstemf", "addRoleHolderAsUser done");
|
||
// }
|
||
}
|
||
}
|
||
|
||
public static Bitmap getRoundedBitmap(Bitmap mBitmap, Context context) {
|
||
Bitmap bgBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);
|
||
|
||
Bitmap mask = BitmapFactory.decodeResource(context.getResources(), R.drawable.mask);
|
||
int width = mask.getWidth();
|
||
int height = mask.getHeight();
|
||
Bitmap bitmapScale = Bitmap.createScaledBitmap(mBitmap, width, height, true);
|
||
bitmapScale.setDensity(context.getResources().getDisplayMetrics().densityDpi);
|
||
// Palette p = Palette.from(mBitmap).generate();
|
||
// Palette.Swatch vibrant = p.getVibrantSwatch();//有活力的
|
||
// int color = vibrant.getRgb(); //样本中的像素数量
|
||
|
||
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||
Canvas canvas = new Canvas();
|
||
Paint paint = new Paint();
|
||
|
||
canvas.setBitmap(result);
|
||
// canvas.drawColor(color);
|
||
canvas.drawBitmap(mask, 0, 0, paint);
|
||
// paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
||
canvas.drawBitmap(bitmapScale, 0, 0, paint);
|
||
// return result;
|
||
|
||
Bitmap result2 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||
Canvas canvas2 = new Canvas();
|
||
Paint paint2 = new Paint();
|
||
canvas2.setBitmap(result2);
|
||
canvas2.drawBitmap(mask, 0, 0, paint2);
|
||
paint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
||
canvas2.drawBitmap(result, 0, 0, paint2);
|
||
return result2;
|
||
|
||
|
||
// Canvas mCanvas = new Canvas();
|
||
// mCanvas.setBitmap(bgBitmap);
|
||
// Paint mPaint = new Paint();
|
||
// RectF mRectM = new RectF(scaleM, scaleM, mBitmap.getWidth() - scaleM, mBitmap.getHeight() - scaleM); //设置剪裁圆角的区域
|
||
// Rect mRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
|
||
// RectF mRectF = mRectM;
|
||
//
|
||
// float roundPx = 15; //圆角半径
|
||
// mPaint.setAntiAlias(true);
|
||
// //Log.d("wy"+TAG,"mBitmap.getWidth()="+mBitmap.getWidth()+", mBitmap.getHeight()="+mBitmap.getHeight());
|
||
// mCanvas.drawRoundRect(mRectF, roundPx, roundPx, mPaint);
|
||
// mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
||
// mCanvas.drawBitmap(mBitmap, mRect, mRect, mPaint);
|
||
// return bgBitmap;
|
||
}
|
||
|
||
/**
|
||
* 获取系统配置信息
|
||
*
|
||
* @param key
|
||
* @param defaultValue
|
||
* @return
|
||
*/
|
||
public static String getProperty(String key, String defaultValue) {
|
||
String value = defaultValue;
|
||
try {
|
||
Class<?> c = Class.forName("android.os.SystemProperties");
|
||
Method get = c.getMethod("get", String.class, String.class);
|
||
value = (String) (get.invoke(c, key, "unknown"));
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
} finally {
|
||
return value;
|
||
}
|
||
}
|
||
|
||
public static boolean isTablet(Context context) {
|
||
// return isPad();
|
||
boolean isTablet = (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
|
||
Log.e(TAG, "isTablet: " + isTablet);
|
||
return isTablet;
|
||
}
|
||
|
||
|
||
public static boolean isPad() {
|
||
boolean result = false;
|
||
String mDeviceType = getProperty("ro.build.characteristics", "default");
|
||
Log.e(TAG, "isPad: " + mDeviceType);
|
||
if (mDeviceType != null && mDeviceType.equalsIgnoreCase("tablet")) {
|
||
result = true;
|
||
}
|
||
Log.d(TAG, "isPad:" + result);
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 判断是否包含SIM卡
|
||
*
|
||
* @return 状态
|
||
*/
|
||
public static boolean hasSimCard(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; // 没有SIM卡
|
||
break;
|
||
case TelephonyManager.SIM_STATE_UNKNOWN:
|
||
result = false;
|
||
break;
|
||
}
|
||
Log.d("try", result ? "有SIM卡" : "无SIM卡");
|
||
return result;
|
||
}
|
||
|
||
public static boolean isMultiSim(Context context) {
|
||
boolean result = false;
|
||
TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
|
||
if (telecomManager != null) {
|
||
List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts();
|
||
result = phoneAccountHandleList.size() >= 2;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static void call(Context context, int id, String telNum) {
|
||
TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
|
||
if (telecomManager != null) {
|
||
List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts();
|
||
Intent intent = new Intent();
|
||
intent.setAction(Intent.ACTION_CALL);
|
||
intent.setData(Uri.parse("tel:" + telNum));
|
||
intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandleList.get(id));
|
||
context.startActivity(intent);
|
||
}
|
||
}
|
||
|
||
@SuppressLint("HardwareIds")
|
||
public static String getIMEI(Context context, int slotIndex) {
|
||
String imei;
|
||
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||
imei = tm.getImei(slotIndex);
|
||
} else {
|
||
imei = tm.getDeviceId(slotIndex);
|
||
}
|
||
if (TextUtils.isEmpty(imei)) {
|
||
return "暂无数据";
|
||
}
|
||
return imei;
|
||
}
|
||
|
||
public static String getEmid(Context context) {
|
||
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||
String meid = telephonyManager.getDeviceId();
|
||
return meid;
|
||
}
|
||
|
||
public static String getCacheDir(Context context) {
|
||
String cachePath;
|
||
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|
||
|| !Environment.isExternalStorageRemovable()) {
|
||
if (context.getExternalCacheDir() != null) {
|
||
cachePath = context.getExternalCacheDir().getPath();
|
||
} else if (context.getExternalFilesDir("cache") != null) {
|
||
cachePath = context.getExternalFilesDir("cache").getPath();
|
||
} else {
|
||
cachePath = context.getCacheDir().getPath();
|
||
}
|
||
} else {
|
||
cachePath = context.getCacheDir().getPath();
|
||
}
|
||
return cachePath;
|
||
}
|
||
|
||
}
|