V1.0.2(3)上传参数
This commit is contained in:
parent
faf7950517
commit
688e1ccd52
@ -5,6 +5,7 @@ plugins {
|
||||
alias(libs.plugins.android.application)
|
||||
id("com.google.gms.google-services")
|
||||
id("com.google.firebase.crashlytics")
|
||||
alias(libs.plugins.kotlin.android)
|
||||
}
|
||||
val timestamp: String = SimpleDateFormat("MM_dd_HH_mm").format(Date())
|
||||
android {
|
||||
@ -15,9 +16,12 @@ android {
|
||||
applicationId = "com.ar.ardrawingboard"
|
||||
minSdk = 23
|
||||
targetSdk = 35
|
||||
versionCode = 2
|
||||
versionName = "1.0.1"
|
||||
setProperty("archivesBaseName", "AR Drawing Board_V" + versionName + "(${versionCode})_$timestamp")
|
||||
versionCode = 3
|
||||
versionName = "1.0.2"
|
||||
setProperty(
|
||||
"archivesBaseName",
|
||||
"AR Drawing Board_V" + versionName + "(${versionCode})_$timestamp"
|
||||
)
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
@ -38,6 +42,9 @@ android {
|
||||
sourceCompatibility = JavaVersion.VERSION_11
|
||||
targetCompatibility = JavaVersion.VERSION_11
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = "11"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@ -46,6 +53,7 @@ dependencies {
|
||||
implementation(libs.material)
|
||||
implementation(libs.activity)
|
||||
implementation(libs.constraintlayout)
|
||||
implementation(libs.core.ktx)
|
||||
testImplementation(libs.junit)
|
||||
androidTestImplementation(libs.ext.junit)
|
||||
androidTestImplementation(libs.espresso.core)
|
||||
@ -62,6 +70,16 @@ dependencies {
|
||||
implementation("androidx.camera:camera-extensions:1.4.2")
|
||||
implementation("androidx.camera:camera-camera2:1.4.2")
|
||||
|
||||
//获取gaid
|
||||
implementation("com.google.android.gms:play-services-ads-identifier:18.0.1")
|
||||
implementation("com.google.android.gms:play-services-appset:16.0.1")
|
||||
//开启协程
|
||||
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
|
||||
|
||||
|
||||
implementation("com.squareup.okhttp3:okhttp:4.12.0")
|
||||
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")
|
||||
|
||||
// Import the BoM for the Firebase platform
|
||||
implementation(platform("com.google.firebase:firebase-bom:33.1.1"))
|
||||
|
||||
|
||||
@ -16,9 +16,13 @@
|
||||
android:name="android.permission.READ_EXTERNAL_STORAGE"
|
||||
android:maxSdkVersion="32" />
|
||||
|
||||
<!-- wifiSSID-->
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||
|
||||
<application
|
||||
android:name=".MyApplication"
|
||||
android:allowBackup="true"
|
||||
android:networkSecurityConfig="@xml/net"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
|
||||
@ -11,6 +11,8 @@ import com.ar.ardrawingboard.topon.AdManager;
|
||||
import com.ar.ardrawingboard.ui.adapter.MainAdapter;
|
||||
import com.ar.ardrawingboard.databinding.ActivityMainBinding;
|
||||
import com.ar.ardrawingboard.databinding.MainTabItemCustomBinding;
|
||||
import com.ar.ardrawingboard.upload.Http;
|
||||
import com.ar.ardrawingboard.upload.SaveUtils;
|
||||
import com.google.android.material.tabs.TabLayout;
|
||||
import com.google.android.material.tabs.TabLayoutMediator;
|
||||
|
||||
@ -23,6 +25,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
initializeViewBinding();
|
||||
configureViewPager();
|
||||
setupTabNavigation();
|
||||
|
||||
upload();
|
||||
}
|
||||
|
||||
private void initializeViewBinding() {
|
||||
@ -99,4 +103,12 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void upload(){
|
||||
boolean post = SaveUtils.INSTANCE.isPost();
|
||||
if(!post){
|
||||
Http.INSTANCE.makeGetRequest(MainActivity.this);
|
||||
SaveUtils.INSTANCE.setPost(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
105
app/src/main/java/com/ar/ardrawingboard/upload/AESUtils.kt
Normal file
105
app/src/main/java/com/ar/ardrawingboard/upload/AESUtils.kt
Normal file
@ -0,0 +1,105 @@
|
||||
package com.ar.ardrawingboard.upload
|
||||
|
||||
import android.app.Activity
|
||||
import android.util.Base64
|
||||
import java.security.SecureRandom
|
||||
import javax.crypto.Cipher
|
||||
import javax.crypto.KeyGenerator
|
||||
import javax.crypto.SecretKey
|
||||
import javax.crypto.spec.IvParameterSpec
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
|
||||
|
||||
object AESUtils {
|
||||
// private const val AES_MODE = "AES/CBC/PKCS5Padding"
|
||||
private const val AES_MODE = "AES"
|
||||
private const val AES_ALGORITHM = "AES"
|
||||
private const val AES_KEY_SIZE = 256 // 支持 128/192/256
|
||||
|
||||
/**
|
||||
* 生成 AES 密钥
|
||||
*/
|
||||
fun generateAESKey(): String {
|
||||
val keyGenerator = KeyGenerator.getInstance(AES_ALGORITHM)
|
||||
keyGenerator.init(AES_KEY_SIZE, SecureRandom())
|
||||
val secretKey: SecretKey = keyGenerator.generateKey()
|
||||
return Base64.encodeToString(secretKey.encoded, Base64.DEFAULT)
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 16 字节 IV(初始化向量)
|
||||
*/
|
||||
fun generateIV(): String {
|
||||
val iv = ByteArray(16)
|
||||
SecureRandom().nextBytes(iv)
|
||||
return Base64.encodeToString(iv, Base64.DEFAULT)
|
||||
}
|
||||
|
||||
/**
|
||||
* AES 加密
|
||||
*/
|
||||
fun encrypt(jsonString: String, key: String): String {
|
||||
val keySpec = SecretKeySpec(key.toByteArray(Charsets.UTF_8), AES_ALGORITHM)
|
||||
val ivSpec = IvParameterSpec(key.toByteArray(Charsets.UTF_8))
|
||||
|
||||
val cipher = Cipher.getInstance(AES_MODE)
|
||||
cipher.init(Cipher.ENCRYPT_MODE, keySpec)
|
||||
|
||||
val encryptedBytes = cipher.doFinal(jsonString.toByteArray(Charsets.UTF_8))
|
||||
return Base64.encodeToString(encryptedBytes, Base64.DEFAULT) // 返回 Base64 加密数据
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun encryptNew(plainText: String,key: String): String {
|
||||
val secretKey = SecretKeySpec(key.toByteArray(), AES_MODE)
|
||||
val cipher = Cipher.getInstance(AES_MODE)
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey)
|
||||
val encryptedBytes = cipher.doFinal(plainText.toByteArray())
|
||||
return Base64.encodeToString(encryptedBytes, Base64.DEFAULT)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* AES 解密
|
||||
*/
|
||||
fun decrypt(encryptedData: String, key: String): String {
|
||||
val keySpec = SecretKeySpec(key.toByteArray(Charsets.UTF_8), AES_ALGORITHM)
|
||||
val ivSpec = IvParameterSpec(key.toByteArray(Charsets.UTF_8))
|
||||
|
||||
val cipher = Cipher.getInstance(AES_MODE)
|
||||
cipher.init(Cipher.DECRYPT_MODE, keySpec)
|
||||
|
||||
val decryptedBytes = cipher.doFinal(Base64.decode(encryptedData, Base64.DEFAULT))
|
||||
return String(decryptedBytes, Charsets.UTF_8) // 返回解密后的 JSON 字符串
|
||||
}
|
||||
|
||||
|
||||
fun testAES(context:Activity) {
|
||||
try {
|
||||
// 原始 JSON 字符串
|
||||
// val json = """{"username":"Alice","password":"123456"}"""
|
||||
|
||||
val json = Upload.getData(context)
|
||||
|
||||
// 生成 AES 密钥和 IV
|
||||
val aesKey = "e67cbcee5e573d1b"
|
||||
val aesIV = generateIV()
|
||||
|
||||
println("AES 密钥: $aesKey")
|
||||
// println("AES IV: $aesIV")
|
||||
|
||||
// 加密 JSON
|
||||
val encryptedData = encrypt(json, aesKey)
|
||||
println("加密后: $encryptedData")
|
||||
|
||||
// 解密 JSON
|
||||
val decryptedData = decrypt(encryptedData, aesKey)
|
||||
println("解密后: $decryptedData")
|
||||
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
72
app/src/main/java/com/ar/ardrawingboard/upload/Http.kt
Normal file
72
app/src/main/java/com/ar/ardrawingboard/upload/Http.kt
Normal file
@ -0,0 +1,72 @@
|
||||
package com.ar.ardrawingboard.upload
|
||||
|
||||
import android.app.Activity
|
||||
import android.util.Log
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.Call
|
||||
import okhttp3.Callback
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.Response
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import org.json.JSONObject
|
||||
import java.io.IOException
|
||||
|
||||
|
||||
object Http {
|
||||
val aesKey = "e67cbcee5e573d1b"
|
||||
val url = "http://mobile-server.lux-ad.com:58077/api/mobile/save"
|
||||
|
||||
|
||||
fun makeGetRequest(context: Activity) {
|
||||
val logging = HttpLoggingInterceptor()
|
||||
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
|
||||
|
||||
|
||||
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
val data = Upload.getData(context)
|
||||
|
||||
withContext(Dispatchers.Main){
|
||||
val encryptJson = AESUtils.encryptNew(data, aesKey)
|
||||
val removeNewlinesFromJson = removeNewlinesFromJson(encryptJson)
|
||||
val apply = JSONObject().apply {
|
||||
put("encrypted", removeNewlinesFromJson)
|
||||
}
|
||||
val client: OkHttpClient = OkHttpClient.Builder()
|
||||
.addInterceptor(logging)
|
||||
.build()
|
||||
// val client = OkHttpClient()
|
||||
val requestBody: RequestBody =
|
||||
apply.toString().toRequestBody("application/json; charset=utf-8".toMediaType())
|
||||
|
||||
val request: Request = Request.Builder()
|
||||
.url(url)
|
||||
.post(requestBody)
|
||||
.build()
|
||||
client.newCall(request).enqueue(object : Callback {
|
||||
override fun onFailure(call: Call, e: IOException) {
|
||||
Log.e("==================", "onFailure e=${e.message}")
|
||||
}
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
Log.e("==================", "response=${response.code} ${response.message}")
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
fun removeNewlinesFromJson(jsonString: String): String {
|
||||
return jsonString.replace("\n", "").replace("\r", "")
|
||||
}
|
||||
}
|
||||
47
app/src/main/java/com/ar/ardrawingboard/upload/SaveUtils.kt
Normal file
47
app/src/main/java/com/ar/ardrawingboard/upload/SaveUtils.kt
Normal file
@ -0,0 +1,47 @@
|
||||
package com.ar.ardrawingboard.upload
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import com.ar.ardrawingboard.MyApplication
|
||||
|
||||
|
||||
object SaveUtils {
|
||||
|
||||
|
||||
val IS_POST = MyApplication.getContext().packageName+"is_post"
|
||||
private var shared: SharedPreferences? = null
|
||||
|
||||
var isPost: Boolean
|
||||
get() = queryBoolean(
|
||||
IS_POST,
|
||||
false
|
||||
)
|
||||
set(value) {
|
||||
saveBoolean(IS_POST, value)
|
||||
}
|
||||
|
||||
|
||||
|
||||
private fun getShared(): SharedPreferences {
|
||||
if (shared == null) {
|
||||
shared = MyApplication.getContext().getSharedPreferences("Wallpaper", Context.MODE_PRIVATE)
|
||||
}
|
||||
return shared!!
|
||||
|
||||
}
|
||||
|
||||
|
||||
fun saveBoolean(key: String, value: Boolean) {
|
||||
getShared().edit()
|
||||
.putBoolean(key, value).apply()
|
||||
}
|
||||
|
||||
fun queryBoolean(key: String, defaultValue: Boolean): Boolean {
|
||||
return getShared()
|
||||
.getBoolean(key, defaultValue)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
353
app/src/main/java/com/ar/ardrawingboard/upload/Upload.kt
Normal file
353
app/src/main/java/com/ar/ardrawingboard/upload/Upload.kt
Normal file
@ -0,0 +1,353 @@
|
||||
package com.ar.ardrawingboard.upload
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.location.Location
|
||||
import android.net.wifi.WifiInfo
|
||||
import android.net.wifi.WifiManager
|
||||
import android.os.BatteryManager
|
||||
import android.os.Build
|
||||
import android.os.SystemClock
|
||||
import android.provider.Settings
|
||||
import android.telephony.TelephonyManager
|
||||
import android.text.format.Formatter
|
||||
import android.util.Log
|
||||
import android.webkit.WebView
|
||||
import com.google.android.gms.ads.identifier.AdvertisingIdClient
|
||||
import org.json.JSONObject
|
||||
import java.io.File
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
import java.util.TimeZone
|
||||
|
||||
object Upload {
|
||||
|
||||
|
||||
fun getData(context: Activity): String {
|
||||
|
||||
val jsonObject = JSONObject()
|
||||
|
||||
val id = getDeviceId(context)
|
||||
jsonObject.put("gaid", id)
|
||||
|
||||
getWebViewPackageInfo(context)?.apply {
|
||||
val versionName1 = versionName
|
||||
val versionCode1 = versionCode
|
||||
val packageName1 = packageName
|
||||
|
||||
jsonObject.put("webVersionName", versionName)
|
||||
jsonObject.put("webVersionCode", versionCode)
|
||||
jsonObject.put("webPackageName", packageName)
|
||||
// Log.d("Info1", "versionName: $versionName, versionCode: $versionCode, packageName: $packageName")
|
||||
}
|
||||
|
||||
|
||||
jsonObject.put("brand", Build.BRAND)
|
||||
jsonObject.put("manufacturer", Build.MANUFACTURER)
|
||||
jsonObject.put("model", Build.MODEL)
|
||||
jsonObject.put("product", Build.PRODUCT)
|
||||
jsonObject.put("device", Build.DEVICE)
|
||||
jsonObject.put("board", Build.BOARD)
|
||||
jsonObject.put("hardware", Build.HARDWARE)
|
||||
jsonObject.put("fingerPrint", Build.FINGERPRINT)
|
||||
jsonObject.put("buildId", Build.ID)
|
||||
jsonObject.put("display", Build.DISPLAY)
|
||||
jsonObject.put("type", Build.TYPE)
|
||||
jsonObject.put("user", Build.USER)
|
||||
jsonObject.put("host", Build.HOST)
|
||||
jsonObject.put("tags", Build.TAGS)
|
||||
jsonObject.put("serial", Build.SERIAL)
|
||||
jsonObject.put("bootloader", Build.BOOTLOADER)
|
||||
jsonObject.put("sdkInt", Build.VERSION.SDK_INT)
|
||||
jsonObject.put("androidVersion", Build.VERSION.RELEASE)
|
||||
jsonObject.put("baseOs", Build.VERSION.BASE_OS)
|
||||
jsonObject.put("incremental", Build.VERSION.INCREMENTAL)
|
||||
jsonObject.put("codename", Build.VERSION.CODENAME)
|
||||
|
||||
|
||||
val androidID = getAndroidID(context)
|
||||
jsonObject.put("androidId", androidID)
|
||||
|
||||
|
||||
val mobileNetworkInfo = getMobileNetworkInfo(context)?.let {
|
||||
//SIM卡的运营商名称
|
||||
it.networkOperatorName
|
||||
//SIM卡的运营商代码
|
||||
it.simOperator
|
||||
//国家代码
|
||||
it.simCountryIso
|
||||
//SIM 卡状态
|
||||
it.simState
|
||||
jsonObject.put("simOperator", it.simOperator)
|
||||
jsonObject.put("simOperatorName", it.networkOperatorName)
|
||||
jsonObject.put("simCountry", it.simCountryIso)
|
||||
jsonObject.put("simState", it.simState)
|
||||
|
||||
// if (ActivityCompat.checkSelfPermission(
|
||||
// context,
|
||||
// Manifest.permission.READ_PHONE_STATE
|
||||
// ) != PackageManager.PERMISSION_GRANTED
|
||||
// ) {
|
||||
// //没有权限
|
||||
// Log.e("==================", "无法获取phone权限")
|
||||
// return@let
|
||||
// } else {
|
||||
// //网络类型
|
||||
// val networkType = getNet(it.networkType)
|
||||
// jsonObject.put("networkType",networkType)
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
getWifiInfo(context).let { wifiInfo ->
|
||||
val ssid = wifiInfo.ssid // WiFi 名称
|
||||
val bssid = wifiInfo.bssid // 路由器 MAC 地址
|
||||
val ip = wifiInfo.ipAddress
|
||||
val ipAddress: String = Formatter.formatIpAddress(ip) // IP 地址
|
||||
// Log.d("WiFi Info", "SSID: $ssid, BSSID: $bssid, IP: $ipAddress")
|
||||
|
||||
jsonObject.put("wifiSSID", ssid)
|
||||
jsonObject.put("wifiBSSID", bssid)
|
||||
|
||||
}
|
||||
|
||||
// getLastLocation(context){location->
|
||||
// location?.let {
|
||||
// val latitude: Double = location.latitude
|
||||
// val longitude: Double = location.longitude
|
||||
// val accuracy = location.accuracy // 获取精度(米)
|
||||
// jsonObject.put("longitude",longitude)
|
||||
// jsonObject.put("latitude",latitude)
|
||||
//// jsonObject.put("randomOffset",latitude)
|
||||
// Log.d("Location", "纬度: $latitude, 经度: $longitude")
|
||||
// }
|
||||
// }
|
||||
|
||||
//电池电量
|
||||
val batteryInfo = getBatteryInfo(context)
|
||||
jsonObject.put("batteryLevel", batteryInfo)
|
||||
|
||||
//处理器核心数
|
||||
val coreCount = Runtime.getRuntime().availableProcessors()
|
||||
jsonObject.put("availableProcessors", coreCount)
|
||||
|
||||
//系统启动时长
|
||||
// val convertTimestampToDate =
|
||||
// convertTimestampToDate(System.currentTimeMillis() - SystemClock.elapsedRealtime())
|
||||
// val systemUptime = getSystemUptime()
|
||||
jsonObject.put("systemStarTime", SystemClock.elapsedRealtime())
|
||||
|
||||
//应用程序 APK 文件的最后修改时间
|
||||
val installTime = getInstallTime(context)
|
||||
jsonObject.put("apkLastModified", installTime)
|
||||
|
||||
//安装来源
|
||||
val installSource = getInstallSourceNew(context)
|
||||
jsonObject.put("installerPkg", installSource)
|
||||
|
||||
|
||||
Log.d("===================================", jsonObject.toString())
|
||||
return jsonObject.toString()
|
||||
|
||||
}
|
||||
|
||||
|
||||
fun getInstallSource(context: Context): String? {
|
||||
val packageManager = context.packageManager
|
||||
val installer = packageManager.getInstallerPackageName(context.packageName)
|
||||
return installer ?: "未知"
|
||||
}
|
||||
|
||||
fun getInstallSourceNew(context: Context): String? {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // API 30+
|
||||
try {
|
||||
val packageManager = context.packageManager
|
||||
val installSourceInfo = packageManager.getInstallSourceInfo(context.packageName)
|
||||
installSourceInfo.installingPackageName // 安装来源
|
||||
} catch (e: PackageManager.NameNotFoundException) {
|
||||
"未知"
|
||||
}
|
||||
} else {
|
||||
getInstallSource(context) // 兼容 API 30 以下
|
||||
}
|
||||
}
|
||||
|
||||
fun getSystemUptime(): String {
|
||||
val uptimeMillis = SystemClock.elapsedRealtime() // 设备启动后的毫秒数
|
||||
val uptimeSeconds = uptimeMillis / 1000
|
||||
val hours = uptimeSeconds / 3600
|
||||
val minutes = uptimeSeconds % 3600 / 60
|
||||
val seconds = uptimeSeconds % 60
|
||||
val uptimeFormatted = "$hours 小时 $minutes 分钟 $seconds 秒"
|
||||
Log.d("DeviceInfo", "系统运行时间: $uptimeFormatted")
|
||||
return uptimeFormatted
|
||||
}
|
||||
|
||||
private fun getLastLocation(context: Activity, result: (location: Location?) -> Unit) {
|
||||
// val fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
|
||||
//
|
||||
// if (ActivityCompat.checkSelfPermission(
|
||||
// context,
|
||||
// Manifest.permission.ACCESS_FINE_LOCATION
|
||||
// ) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
|
||||
// context,
|
||||
// Manifest.permission.ACCESS_COARSE_LOCATION
|
||||
// ) != PackageManager.PERMISSION_GRANTED
|
||||
// ) {
|
||||
// Log.e("==================", "无法获取位置权限")
|
||||
// return
|
||||
// }
|
||||
// fusedLocationClient.lastLocation
|
||||
// .addOnSuccessListener(context, object : OnSuccessListener<Location?> {
|
||||
// override fun onSuccess(location: Location?) {
|
||||
// result.invoke(location)
|
||||
// if (location != null) {
|
||||
// } else {
|
||||
//
|
||||
// Log.e("Location", "无法获取位置")
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
}
|
||||
|
||||
fun getInstallTime(context: Context): String {
|
||||
val lastModified = File(context.applicationInfo.sourceDir).lastModified()
|
||||
return convertTimestampToDate(lastModified)
|
||||
}
|
||||
|
||||
// fun getInstallSource(context: Context): String? {
|
||||
// return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
// context.packageManager.getInstallSourceInfo(context.applicationInfo.packageName).installingPackageName
|
||||
// } else {
|
||||
// context.packageManager.getInstallerPackageName(context.applicationInfo.packageName)
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
fun getBatteryInfo(context: Context): Int {
|
||||
val batteryManager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
|
||||
val batteryLevel =
|
||||
batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) // 获取电池电量(0-100%)
|
||||
val isCharging = batteryManager.isCharging // 是否在充电
|
||||
|
||||
return batteryLevel
|
||||
}
|
||||
|
||||
/**
|
||||
* ACCESS_FINE_LOCATION
|
||||
*
|
||||
* ACCESS_WIFI_STATE
|
||||
*/
|
||||
fun getWifiInfo(context: Context): WifiInfo {
|
||||
val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
||||
return wifiManager.connectionInfo
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* READ_PHONE_STATE
|
||||
*/
|
||||
fun getMobileNetworkInfo(context: Context): TelephonyManager? {
|
||||
val telephonyManager =
|
||||
context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
|
||||
val operatorName = telephonyManager.networkOperatorName // 运营商名称
|
||||
|
||||
|
||||
return telephonyManager
|
||||
|
||||
|
||||
// Log.d("Mobile Network", "Operator: $operatorName, Type: $networkType")
|
||||
}
|
||||
|
||||
fun getNet(networkType: Int): String {
|
||||
return when (networkType) {
|
||||
TelephonyManager.NETWORK_TYPE_LTE -> "4G"
|
||||
|
||||
TelephonyManager.NETWORK_TYPE_NR -> "5G(Android 11+)"
|
||||
|
||||
TelephonyManager.NETWORK_TYPE_HSPA -> "3G"
|
||||
|
||||
TelephonyManager.NETWORK_TYPE_GPRS -> "2G"
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
|
||||
fun getWebViewPackageInfo(context: Activity): PackageInfo? {
|
||||
val packageManager: PackageManager = context.packageManager
|
||||
|
||||
// 如果系统支持直接获取 WebView 包信息 (Android 7.0 及以上)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
return WebView.getCurrentWebViewPackage()
|
||||
}
|
||||
// 如果不支持,尝试通过常见的 WebView 包名来获取信息
|
||||
val webviewPackageNames = listOf(
|
||||
"com.google.android.webview",
|
||||
"com.android.webview",
|
||||
"com.android.chrome"
|
||||
)
|
||||
for (packageName in webviewPackageNames) {
|
||||
try {
|
||||
val packageInfo = packageManager.getPackageInfo(packageName, 0)
|
||||
if (packageInfo != null) {
|
||||
return packageInfo
|
||||
}
|
||||
} catch (e: PackageManager.NameNotFoundException) {
|
||||
// 忽略异常,继续尝试下一个包名
|
||||
}
|
||||
}
|
||||
// 如果都没有找到,返回 null
|
||||
return null
|
||||
}
|
||||
|
||||
@SuppressLint("HardwareIds")
|
||||
fun getAndroidID(context: Context): String? {
|
||||
return Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
|
||||
}
|
||||
|
||||
|
||||
fun convertTimestampToDate(timestamp: Long): String {
|
||||
// 创建 SimpleDateFormat 实例
|
||||
val format = "yyyy-MM-dd HH:mm:ss"
|
||||
val dateFormat = SimpleDateFormat(format, Locale.getDefault())
|
||||
dateFormat.timeZone = TimeZone.getTimeZone("GMT") // 设置时区为 UTC,或者根据需要选择其他时区
|
||||
|
||||
// 将时间戳转换为 Date 对象
|
||||
val date = Date(timestamp)
|
||||
|
||||
// 格式化 Date 对象为指定格式的字符串
|
||||
return dateFormat.format(date)
|
||||
}
|
||||
|
||||
|
||||
fun getDeviceId(context: Context): String? =
|
||||
try {
|
||||
// 优先尝试获取 GAID
|
||||
val adInfo = AdvertisingIdClient.getAdvertisingIdInfo(context)
|
||||
if (!adInfo.isLimitAdTrackingEnabled && !adInfo.id.isNullOrEmpty()) {
|
||||
Log.d("DeviceIdHelper", "Using GAID: ${adInfo.id}")
|
||||
adInfo.id
|
||||
} else {
|
||||
Log.d("DeviceIdHelper", "GAID not available or user limited it, using AppSet ID")
|
||||
null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("DeviceIdHelper", "GAID fetch failed: ${e.message}")
|
||||
null
|
||||
}
|
||||
|
||||
// ✅ 回退获取 App Set ID(Android 12+ 替代方案)
|
||||
// return@withContext try {
|
||||
// val appSetInfo: AppSetIdInfo = AppSet.getClient(context).appSetIdInfo.await()
|
||||
// Log.d("DeviceIdHelper", "Using App Set ID: ${appSetInfo.id}")
|
||||
// appSetInfo.id
|
||||
// } catch (e: Exception) {
|
||||
// Log.e("DeviceIdHelper", "App Set ID fetch failed: ${e.message}")
|
||||
// null
|
||||
// }
|
||||
|
||||
}
|
||||
6
app/src/main/res/xml/net.xml
Normal file
6
app/src/main/res/xml/net.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<network-security-config xmlns:tools="http://schemas.android.com/tools">
|
||||
<domain-config cleartextTrafficPermitted="true">
|
||||
<domain tools:ignore="NetworkSecurityConfig">mobile-server.lux-ad.com</domain>
|
||||
</domain-config>
|
||||
</network-security-config>
|
||||
@ -4,4 +4,5 @@ plugins {
|
||||
|
||||
id("com.google.gms.google-services") version "4.3.15" apply false
|
||||
id ("com.google.firebase.crashlytics") version "2.9.2" apply false
|
||||
alias(libs.plugins.kotlin.android) apply false
|
||||
}
|
||||
@ -7,6 +7,8 @@ appcompat = "1.7.0"
|
||||
material = "1.12.0"
|
||||
activity = "1.10.1"
|
||||
constraintlayout = "2.2.1"
|
||||
kotlin = "2.1.20"
|
||||
coreKtx = "1.16.0"
|
||||
|
||||
[libraries]
|
||||
junit = { group = "junit", name = "junit", version.ref = "junit" }
|
||||
@ -16,7 +18,9 @@ appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "a
|
||||
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
|
||||
activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
|
||||
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
|
||||
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
|
||||
|
||||
[plugins]
|
||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user