This commit is contained in:
2021-12-03 17:46:59 +08:00
commit 6c2eb5a1a3
287 changed files with 3854 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
package com.uiui.os.utils;
import android.content.Context;
import android.util.Log;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
public class AmapManager {
private static AmapManager sInstance;
private Context mContext;
public static AMapLocationClient locationClient = null;
private String TAG = AmapManager.class.getSimpleName();
private AmapManager(Context context) {
this.mContext = context;
}
public static void init(Context context) {
if (sInstance == null) {
sInstance = new AmapManager(context);
}
}
public static AmapManager getInstance() {
if (sInstance == null) {
throw new IllegalStateException("You must be init AmapManager first");
}
return sInstance;
}
public AMapLocationClient getLocationClient() {
if (null == locationClient) {
initAmap();
}
return locationClient;
}
public void initAmap() {
locationClient = new AMapLocationClient(mContext);
AMapLocationClientOption option = new AMapLocationClientOption();
option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
option.setNeedAddress(true);
//获取一次定位结果:
//该方法默认为false。
option.setOnceLocation(true);
//获取最近3s内精度最高的一次定位结果
//设置setOnceLocationLatest(boolean b)接口为true启动定位时SDK会返回最近3s内精度最高的一次定位结果。
// 如果设置其为truesetOnceLocation(boolean b)接口也会被设置为true反之不会默认为false。
option.setOnceLocationLatest(true);
locationClient.setLocationOption(option);
//设置定位模式为AMapLocationMode.Hight_Accuracy高精度模式。
//设置定位监听
locationClient.setLocationListener(new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
//errCode等于0代表定位成功其他的为定位失败具体的可以参照官网定位错误码说明
if (aMapLocation.getErrorCode() == 0) {
Log.e(TAG, "onLocationChanged: " + "定位成功");
Log.e(TAG, "onLocationChanged: " + aMapLocation.getAddress());
} else {
//定位失败
Log.e(TAG, "onLocationChanged: " + "定位失败");
}
}
});
//设置场景模式后最好调用一次stop再调用start以保证场景模式生效
locationClient.stopLocation();
locationClient.startLocation();
Log.e(TAG, "initAmap: " + "startLocation");
}
}

View File

@@ -0,0 +1,141 @@
package com.uiui.os.utils;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;
import android.util.Log;
import com.uiui.os.BuildConfig;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ApkUtils {
private static String[] excludePackageName = {BuildConfig.APPLICATION_ID};
public static ArrayList<ApplicationInfo> queryFilterAppInfo(Context context) {
PackageManager pm = context.getPackageManager();
// 查询所有已经安装的应用程序
List<ApplicationInfo> appInfos = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);// GET_UNINSTALLED_PACKAGES代表已删除但还有安装目录的
ArrayList<ApplicationInfo> applicationInfos = new ArrayList<>();
// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// 通过getPackageManager()的queryIntentActivities方法遍历,得到所有能打开的app的packageName
List<ResolveInfo> resolveinfoList = pm.queryIntentActivities(resolveIntent, 0);
Set<String> allowPackages = new HashSet();
for (ResolveInfo resolveInfo : resolveinfoList) {
allowPackages.add(resolveInfo.activityInfo.packageName);
}
for (ApplicationInfo app : appInfos) {
// if((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0)//通过flag排除系统应用会将电话、短信也排除掉
// {
// applicationInfos.add(app);
// }
// if(app.uid > 10000){//通过uid排除系统应用在一些手机上效果不好
// applicationInfos.add(app);
// }
if (allowPackages.contains(app.packageName) && !Arrays.asList(excludePackageName).contains(app.packageName)) {
// if (allowPackages.contains(app.packageName)) {
applicationInfos.add(app);
}
}
return applicationInfos;
}
/**
* 获取第三方应用
* @param context
* @return
*/
public static List<String> queryFilterAppList(Context context) {
PackageManager pm = context.getPackageManager();
// 查询所有已经安装的应用程序
List<ApplicationInfo> appInfos = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);// GET_UNINSTALLED_PACKAGES代表已删除但还有安装目录的
List<String> applicationInfos = new ArrayList<>();
for (ApplicationInfo app : appInfos) {
// Log.e("queryFilterAppInfo", String.valueOf(app.flags));
// Log.e("queryFilterAppInfo", String.valueOf((app.flags & mask)));
if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
//通过flag排除系统应用会将电话、短信也排除掉
} else {
applicationInfos.add(app.packageName);
Log.e("queryFilterAppInfo", app.packageName);
}
}
return applicationInfos;
}
public static void openApp(Context context, String packageName) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
if (intent != null) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
public static Intent getAppOpenIntentByPackageName(Context context,String packageName){
//Activity完整名
String mainAct = null;
//根据包名寻找
PackageManager pkgMag = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED|Intent.FLAG_ACTIVITY_NEW_TASK);
List<ResolveInfo> list = pkgMag.queryIntentActivities(intent,
PackageManager.GET_ACTIVITIES);
for (int i = 0; i < list.size(); i++) {
ResolveInfo info = list.get(i);
if (info.activityInfo.packageName.equals(packageName)) {
mainAct = info.activityInfo.name;
break;
}
}
if (TextUtils.isEmpty(mainAct)) {
return null;
}
intent.setComponent(new ComponentName(packageName, mainAct));
return intent;
}
public static Context getPackageContext(Context context, String packageName) {
Context pkgContext = null;
if (context.getPackageName().equals(packageName)) {
pkgContext = context;
} else {
// 创建第三方应用的上下文环境
try {
pkgContext = context.createPackageContext(packageName,
Context.CONTEXT_IGNORE_SECURITY
| Context.CONTEXT_INCLUDE_CODE);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
return pkgContext;
}
public static boolean openPackage(Context context, String packageName) {
Context pkgContext = getPackageContext(context, packageName);
Intent intent = getAppOpenIntentByPackageName(context, packageName);
if (pkgContext != null && intent != null) {
pkgContext.startActivity(intent);
return true;
}
return false;
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright (C) 2012 www.amsoft.cn
*
* 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.uiui.os.utils;
import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;
import java.io.BufferedReader;
import java.io.FileReader;
public class AppUtil {
private static String TAG = AppUtil.class.getSimpleName();
/**
* 描述:获取可用内存.
*
* @param context
* @return
*/
public static long getAvailMemory(Context context) {
// 获取android当前可用内存大小
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
// 当前系统可用内存 ,将获得的内存大小规格化
return memoryInfo.availMem;
}
/**
* @param context
* @return 可用的内存大小
*/
public static long getFreeMemory(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
long freeMem = memoryInfo.totalMem - memoryInfo.availMem;
// Log.e("getHardware", "getFreeMemory: " + freeMem);
return freeMem;
}
/**
* 描述:总内存.
*
* @param context
* @return
*/
public static long getTotalMemory(Context context) {
// 系统内存信息文件
String file = "/proc/meminfo";
String memInfo;
String[] strs;
long memory = 0;
try {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader, 8192);
// 读取meminfo第一行系统内存大小
memInfo = bufferedReader.readLine();
strs = memInfo.split("\\s+");
for (String str : strs) {
Log.e(TAG, "getTotalMemory: " + str + "\t");
}
// 获得系统总内存单位KB
memory = Integer.valueOf(strs[1]).intValue();
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
// Byte转位KB或MB
return memory * 1024;
}
}

View File

@@ -0,0 +1,72 @@
package com.uiui.os.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.AdaptiveIconDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import java.io.ByteArrayOutputStream;
public class BitmapUtils {
public static Bitmap Bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
public static byte[] Bitmap2Bytes(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
return data;
}
/**
* Drawable转换成一个Bitmap
*
* @param drawable drawable对象
* @return
*/
public static final Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
public static Bitmap drawableToBitamp(Drawable drawable) {
Bitmap bitmap;
BitmapDrawable bd = (BitmapDrawable) drawable;
bitmap = bd.getBitmap();
return bitmap;
}
public static Bitmap getIconBitmap(Context context, Drawable drawable) {
try {
if (drawable == null) {
return null;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} else {
return ((BitmapDrawable) drawable).getBitmap();
}
} catch (Exception e) {
return null;
}
}
}

View File

@@ -0,0 +1,61 @@
package com.uiui.os.utils;
import java.util.ArrayList;
import java.util.List;
public class IconUtils {
public static List<String> appClassNameList = new ArrayList<String>() {
{
this.add("com.android.appstore");//应用市场
this.add("com.android.browser");//浏览器
this.add("com.android.calculator2");//计算器
this.add("com.android.calendar");//日历
this.add("com.android.camera");//相机
this.add("com.android.camera2");//相机
this.add("com.android.contacts");//通讯录
this.add("com.android.deskclock");//时钟
this.add("com.android.dialer");//电话
this.add("com.android.dialer");//电话
this.add("com.android.gallery3d");//图库
this.add("com.android.mms.ui");//信息
this.add("com.android.music");//音乐
this.add("com.android.providers.downloads.ui");//下载
this.add("com.android.quicksearchbox");//搜索
this.add("com.android.settings");//设置
this.add("com.android.soundrecorder");//录音机
this.add("com.android.stk.StkMain");//sim卡
this.add("com.android.vdieo");//视频
this.add("com.mediatek.filemanager");//文件管理
this.add("com.android.documentsui");//下载
this.add("com.mediatek.fmradio");//收音机
this.add("com.android.fmradio");//收音机
this.add("com.android.email");//电子邮件
}
};
public static List<String> appIconList = new ArrayList<String>() {{
this.add("com_android_appstore");
this.add("com_android_browser");
this.add("com_android_calculator2");
this.add("com_android_calendar");
this.add("com_android_camera");
this.add("com_android_camera");
this.add("com_android_contacts");
this.add("com_android_deskclock");
this.add("com_android_dialer");
this.add("com_android_dialer");
this.add("com_android_gallery3d_app");
this.add("com_android_mms_ui");
this.add("com_android_music");
this.add("com_android_providers_downloads_ui");
this.add("com_android_quicksearchbox");
this.add("com_android_settings");
this.add("com_android_soundrecorder");
this.add("com_android_stk_stkmain");
this.add("com_android_vdieo");
this.add("com_mediatek_filemanager");
this.add("com_mediatek_filemanager");
this.add("com_mediatek_fmradio");
this.add("com_mediatek_fmradio");//收音机
this.add("com_android_email");
}};
}

View File

@@ -0,0 +1,26 @@
package com.uiui.os.utils;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;
public class Utils {
/**
* 获取电量
*
* @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);
}
}
}