[Modify]移除友盟统计
This commit is contained in:
@@ -139,6 +139,8 @@ dependencies {
|
||||
|
||||
api 'io.github.razerdp:BasePopup:3.2.1'
|
||||
|
||||
api 'com.umeng.sdk:utdid:1.1.5.3'
|
||||
|
||||
}
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
@@ -23,11 +23,6 @@ public class ErBanService extends JobService {
|
||||
public boolean onStartJob(JobParameters params) {
|
||||
int jobId = params.getJobId();
|
||||
Logger.d(ResUtil.getString(R.string.xchat_android_library_service_erbanservice_01), jobId);
|
||||
|
||||
// StatisticManager.Instance().deleteLogFiles()
|
||||
// .subscribe(aBoolean -> {
|
||||
// Logger.i(ResUtil.getString(R.string.xchat_android_library_service_erbanservice_02) + (aBoolean ? ResUtil.getString(R.string.xchat_android_library_service_erbanservice_03) : ResUtil.getString(R.string.xchat_android_library_service_erbanservice_04)));
|
||||
// });
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,314 @@
|
||||
package com.yizhuan.xchat_android_library.common.util
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.ActivityManager
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.os.Process
|
||||
import android.provider.Settings
|
||||
import android.telephony.TelephonyManager
|
||||
import android.text.TextUtils
|
||||
import com.ut.device.UTDevice
|
||||
import com.yizhuan.xchat_android_library.common.application.BaseApp
|
||||
import java.io.RandomAccessFile
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* create by wushaocheng 2023/2/15
|
||||
* 获取设备id
|
||||
*/
|
||||
object DeviceUtil {
|
||||
private const val TAG = "DeviceUtils"
|
||||
private const val ANDROID_ID_KEY = "android_id"
|
||||
private const val cache_device_id = "cache_device_id"
|
||||
private var deviceId = ""
|
||||
private var mcc_mnc = ""
|
||||
private var mcc = ""
|
||||
private var mnc = ""
|
||||
@Volatile
|
||||
private var ANDROID_ID = ""
|
||||
|
||||
/**
|
||||
* 获取设备信息
|
||||
*/
|
||||
var deviceDesc = ""
|
||||
get() {
|
||||
if (!TextUtils.isEmpty(field)) {
|
||||
return field
|
||||
}
|
||||
field =
|
||||
String.format("%s-%s-%s", Build.MANUFACTURER, Build.MODEL, Build.VERSION.RELEASE)
|
||||
return field
|
||||
}
|
||||
private set
|
||||
private const val MB = (1024 * 1024).toLong()
|
||||
private var sLevelCache: LEVEL? = null
|
||||
private var sTotalMemory: Long = 0
|
||||
|
||||
/**
|
||||
* 获取设备等级,考虑到机身真实内存大小与参数规格里面的不太一致,排除系统可能占用的内存情况后,以下面方式来衡量:
|
||||
* 1.内存小于等于4GB,定为中低端机
|
||||
* 2.内存大于4GB,定为高端机
|
||||
*/
|
||||
fun getLevel(context: Context): LEVEL? {
|
||||
if (null != sLevelCache) {
|
||||
return sLevelCache
|
||||
}
|
||||
val totalMemory = getTotalMemory(context)
|
||||
sLevelCache = if (totalMemory > 4 * 1024 * MB) {
|
||||
//内存大于4GB,定为高端机
|
||||
LEVEL.HIGH
|
||||
} else {
|
||||
//内存小于等于4GB,定为中低端机
|
||||
LEVEL.LOW
|
||||
}
|
||||
return sLevelCache
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 当前进程id
|
||||
*/
|
||||
private val appId: Int
|
||||
get() = Process.myPid()
|
||||
|
||||
/**
|
||||
* 获取设备内存总大小
|
||||
*/
|
||||
private fun getTotalMemory(context: Context): Long {
|
||||
if (0L != sTotalMemory) {
|
||||
return sTotalMemory
|
||||
}
|
||||
val memInfo = ActivityManager.MemoryInfo()
|
||||
val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
||||
am.getMemoryInfo(memInfo)
|
||||
sTotalMemory = memInfo.totalMem
|
||||
return sTotalMemory
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前设备是否处于低内存状态
|
||||
*/
|
||||
fun isLowMemory(context: Context): Boolean {
|
||||
val memInfo = ActivityManager.MemoryInfo()
|
||||
val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
||||
am.getMemoryInfo(memInfo)
|
||||
return memInfo.lowMemory
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前进程剩余内存大小
|
||||
*/
|
||||
val appAvailableMemory: Long
|
||||
get() {
|
||||
val runtime = Runtime.getRuntime()
|
||||
return runtime.totalMemory() - runtime.freeMemory()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前设备CPU使用率
|
||||
*/
|
||||
val appCpuRate: Double
|
||||
get() {
|
||||
var cpuTime = 0L
|
||||
var appTime = 0L
|
||||
var cpuRate = 0.0
|
||||
var procStatFile: RandomAccessFile? = null
|
||||
var appStatFile: RandomAccessFile? = null
|
||||
try {
|
||||
procStatFile = RandomAccessFile("/proc/stat", "r")
|
||||
val procStatString = procStatFile.readLine()
|
||||
val procStats = procStatString.split(" ".toRegex()).dropLastWhile { it.isEmpty() }
|
||||
.toTypedArray()
|
||||
cpuTime =
|
||||
procStats[2].toLong() + procStats[3].toLong() + procStats[4].toLong() + procStats[5].toLong() + procStats[6].toLong() + procStats[7].toLong() + procStats[8].toLong()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
try {
|
||||
procStatFile?.close()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
try {
|
||||
appStatFile = RandomAccessFile("/proc/$appId/stat", "r")
|
||||
val appStatString = appStatFile.readLine()
|
||||
val appStats =
|
||||
appStatString.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
|
||||
appTime = appStats[13].toLong() + appStats[14].toLong()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
try {
|
||||
appStatFile?.close()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
if (0L != cpuTime) {
|
||||
cpuRate = appTime.toDouble() / cpuTime.toDouble() * 100.0
|
||||
}
|
||||
return cpuRate
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备id
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getDeviceId(context: Context?): String {
|
||||
if (!TextUtils.isEmpty(deviceId)) {
|
||||
return deviceId
|
||||
}
|
||||
deviceId = Config.getInstance(context).getString(cache_device_id, "")
|
||||
if (!TextUtils.isEmpty(deviceId)) {
|
||||
return deviceId
|
||||
}
|
||||
deviceId = UTDevice.getUtdid(context)
|
||||
if (TextUtils.isEmpty(deviceId)) {
|
||||
deviceId = handyDevicesId
|
||||
Logger.info(TAG, "use handy deviceId=$deviceId")
|
||||
} else {
|
||||
Logger.info(TAG, "use system deviceId=$deviceId")
|
||||
}
|
||||
//缓存上次取到的数据,避免有些机型每次打开APP都能拿到一个新的设备id
|
||||
Config.getInstance(context).putString(cache_device_id, deviceId)
|
||||
return deviceId
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 获取系统id
|
||||
*/
|
||||
@SuppressLint("HardwareIds")
|
||||
@JvmStatic
|
||||
fun getAndroidID(): String {
|
||||
if (!TextUtils.isEmpty(ANDROID_ID)) {
|
||||
Logger.info(TAG, "getAndroidID from cache, ANDROID_ID=$ANDROID_ID")
|
||||
return ANDROID_ID
|
||||
}
|
||||
ANDROID_ID = SPUtils.getString(ANDROID_ID_KEY, "")
|
||||
if (!TextUtils.isEmpty(ANDROID_ID)) {
|
||||
Logger.info(TAG, "getAndroidID from SP, ANDROID_ID=$ANDROID_ID")
|
||||
return ANDROID_ID
|
||||
}
|
||||
ANDROID_ID = Settings.Secure.getString(
|
||||
BaseApp.getContext().contentResolver,
|
||||
Settings.Secure.ANDROID_ID
|
||||
)
|
||||
Logger.info(TAG, "getAndroidID from system, ANDROID_ID=$ANDROID_ID")
|
||||
SPUtils.putString(ANDROID_ID_KEY, ANDROID_ID)
|
||||
return ANDROID_ID
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 手动组装设备id
|
||||
*/
|
||||
private val handyDevicesId: String
|
||||
get() {
|
||||
val devIDShort =
|
||||
"35" + Build.BOARD.length % 10 + Build.BRAND.length % 10 + Build.DEVICE.length % 10 + Build.MANUFACTURER.length % 10 + Build.MODEL.length % 10 + Build.PRODUCT.length % 10
|
||||
val serial = "serial"
|
||||
return UUID(devIDShort.hashCode().toLong(), serial.hashCode().toLong()).toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 移动设备国家代码(英语:Mobile Country Code,MCC)+移动设备网络代码(英语:Mobile Network Code,MNC)
|
||||
*/
|
||||
private fun getMCC_MNC(context: Context): String {
|
||||
if (!TextUtils.isEmpty(mcc_mnc)) {
|
||||
return mcc_mnc
|
||||
}
|
||||
try {
|
||||
val telManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
|
||||
mcc_mnc = telManager.simOperator
|
||||
if (!TextUtils.isEmpty(mcc_mnc)) {
|
||||
//双卡双待手机会返回[46001,46002] or [,46007] or [46007,]这种格式,
|
||||
//先取第一个,如果第一个为空且第二个不为空,则取第二个
|
||||
val s = mcc_mnc.split(",".toRegex()).dropLastWhile { it.isEmpty() }
|
||||
.toTypedArray()
|
||||
if (s.isNotEmpty() && !TextUtils.isEmpty(s[0])) {
|
||||
mcc_mnc = s[0]
|
||||
} else {
|
||||
if (s.size == 2 && !TextUtils.isEmpty(s[1])) {
|
||||
mcc_mnc = s[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
return mcc_mnc
|
||||
} catch (e: Exception) {
|
||||
Logger.error(TAG, e.message)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 移动设备国家代码(英语:Mobile Country Code,MCC),三位
|
||||
*/
|
||||
fun getMCC(context: Context): String {
|
||||
if (!TextUtils.isEmpty(mcc)) {
|
||||
return mcc
|
||||
}
|
||||
try {
|
||||
val mcc_mnc = getMCC_MNC(context)
|
||||
if (mcc_mnc.length > 2) {
|
||||
mcc = mcc_mnc.substring(0, 3)
|
||||
return mcc
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Logger.error(TAG, e.message)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 移动设备网络代码(英语:Mobile Network Code,MNC),两位或三位
|
||||
*/
|
||||
fun getMNC(context: Context): String {
|
||||
if (!TextUtils.isEmpty(mnc)) {
|
||||
return mnc
|
||||
}
|
||||
try {
|
||||
val mcc_mnc = getMCC_MNC(context)
|
||||
if (mcc_mnc.length > 3) {
|
||||
mnc = mcc_mnc.substring(3)
|
||||
return mnc
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Logger.error(TAG, e.message)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查指定包名的APP是否已安装了
|
||||
*
|
||||
* @param context 上下文对象
|
||||
* @param packageName 包名
|
||||
* @return 是否有安装指定包名的APP,true代表已安装,false代表未安装
|
||||
*/
|
||||
fun isAppInstalled(context: Context?, packageName: String): Boolean {
|
||||
return if (context == null) {
|
||||
false
|
||||
} else {
|
||||
var installed = false
|
||||
try {
|
||||
val pm = context.packageManager
|
||||
val info = pm.getApplicationInfo(packageName, 0)
|
||||
if (info != null) {
|
||||
installed = true
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Logger.error(TAG, "isAppInstalled packageName=$packageName", e)
|
||||
}
|
||||
installed
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备等级划分方案参考Matrix框架,
|
||||
* 参考:[...](https://github.com/Tencent/matrix/blob/master/matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/util/DeviceUtil.java)
|
||||
*/
|
||||
enum class LEVEL(var value: Int) {
|
||||
HIGH(2), LOW(1);
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user