grape/app/src/main/java/com/android/grape/util/ServiceUtils.kt

192 lines
5.6 KiB
Kotlin

package com.android.grape.util
import android.app.IMikRom
import android.content.Context
import android.os.Environment
import android.os.IBinder
import android.util.Log
import com.android.grape.util.ContextUtils.getBaseFilesDir
import com.android.grape.util.FileUtils.forceMakeDir
import java.io.File
import java.io.FileOutputStream
import java.io.FileWriter
import java.io.IOException
object ServiceUtils {
private var iMikRom: IMikRom? = null
fun getiMikRom(): IMikRom? {
if (iMikRom == null) {
try {
val localClass = Class.forName("android.os.ServiceManager")
val getService = localClass.getMethod(
"getService", *arrayOf<Class<*>>(
String::class.java
)
)
if (getService != null) {
val objResult = getService.invoke(localClass, *arrayOf<Any>("mikrom"))
if (objResult != null) {
val binder = objResult as IBinder
iMikRom = IMikRom.Stub.asInterface(binder)
}
}
} catch (e: Exception) {
Log.d("MikManager", e.message!!)
e.printStackTrace()
}
}
return iMikRom
}
/**
* 文件读取
* @param path
* @return
*/
fun readFile(path: String?): String {
var retMsg = ""
val iMikRom: IMikRom? = getiMikRom()
if (iMikRom != null) {
try {
retMsg = iMikRom.readFile(path)
} catch (eee: Exception) {
eee.printStackTrace()
}
}
return retMsg
}
/**
* 文件保存
* @param path
* @param data
*/
fun writeFile(path: String?, data: String?) {
val iMikRom: IMikRom? = getiMikRom()
if (iMikRom != null) {
try {
iMikRom.writeFile(path, data)
} catch (eee: Exception) {
eee.printStackTrace()
}
}
}
fun shellExec(cmd: String?): String {
var retMsg = ""
val iMikRom: IMikRom? = getiMikRom()
if (iMikRom != null) {
try {
retMsg = iMikRom.shellExec(cmd)
} catch (eee: Exception) {
eee.printStackTrace()
}
}
return retMsg
}
/**
* 判断App是否已经开启注入
* @param callingPkg
* @return
*/
fun isEnableApp(callingPkg: String): Boolean {
var retMsg = false
val iMikRom: IMikRom? = getiMikRom()
if (iMikRom != null) {
try {
retMsg = iMikRom.isEnableApp(callingPkg)
} catch (eee: Exception) {
eee.printStackTrace()
}
}
return retMsg
}
/**
* 设置App是否需要打开注入
* @param pkgName
* @param isEnable
*/
fun setEnableApp(pkgName: String, isEnable: Boolean) {
val iMikRom: IMikRom? = getiMikRom()
if (iMikRom != null) {
try {
iMikRom.setEnableApp(pkgName, isEnable)
} catch (eee: Exception) {
eee.printStackTrace()
}
}
}
fun writeFile1(context: Context?, dirName: String, fileName: String, contents: String) {
val fs = File("$dirName/$fileName")
forceMakeDir(fs)
var outputStream: FileOutputStream? = null
try {
outputStream = FileOutputStream(fs)
outputStream.write(contents.toByteArray())
outputStream.flush()
outputStream.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
fun writeFile(context: Context, dirName: String, fileName: String, contents: String) {
val fs = File(getBaseFilesDir(context) + "/" + dirName + "/" + fileName)
forceMakeDir(fs)
var outputStream: FileOutputStream? = null
try {
outputStream = FileOutputStream(fs)
outputStream.write(contents.toByteArray())
outputStream.flush()
outputStream.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
fun WriteFile(fileName: String?, content: String?) {
try {
val writer = FileWriter(fileName, false)
writer.write(content)
writer.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
@Throws(IOException::class)
private fun writeFileToSDCard(message: String) {
// 比如可以将一个文件作为普通的文档存储,那么先获取系统默认的文档存放根目录
val parent_path = Environment.getExternalStorageDirectory()
// String AppPath = "/data/data/"+ Util.getRecordPackageName() + "/";
val AppPath = "/data/data/sperixlabs.proxyme/"
val dir = File(AppPath)
if (!dir.exists()) {
dir.mkdirs()
}
val file = File(AppPath + "device.txt")
if (file.exists()) {
file.delete()
}
try {
file.createNewFile()
} catch (e: IOException) {
e.printStackTrace()
}
val writer = FileWriter(AppPath + "device.txt", true)
writer.write(message)
writer.close()
Log.d("文件写入", "成功")
}
}