Skip to content

Commit

Permalink
[add] release 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vitvm committed Nov 3, 2024
1 parent 2bcdf7c commit 1de1dd9
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 201 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/pic/catcher/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.lu.magic.util.log.SimpleLogger;
import com.pic.catcher.base.CustomLifecycle;
import com.pic.catcher.base.CustomLifecycleOwner;
import com.pic.catcher.config.AppConfigUtil;
import com.pic.catcher.ui.JsonMenuManager;

import org.jetbrains.annotations.NotNull;
Expand All @@ -21,7 +22,7 @@ public void onCreate() {
super.onCreate();

Companion.setInstance(this);
JsonMenuManager.Companion.updateMenuListFromRemote(this);
AppConfigUtil.Companion.init();
}

@NonNull
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/pic/catcher/base/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @author Mingyueyixi
* @date 2024/9/28 19:25
* @description
* @description BaseActivity
*/
public class BaseActivity extends Activity implements CustomLifecycleOwner{
private CustomLifecycleOwnerDelegate mCustomLifecycleOwnerDelegate = new CustomLifecycleOwnerDelegate();
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/pic/catcher/base/CustomLifecycle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.pic.catcher.base;
/**
* @author Mingyueyixi
* @date 2024/9/28 19:39
* @description
* @description 为了获得极简的代码量,不引入androidx life 库,自行实现类似风格的api
*/
interface CustomLifecycle {

Expand All @@ -20,6 +20,7 @@ interface CustomLifecycle {
fun onLifeStateChanged(source: CustomLifecycleOwner, state: State)
}


interface CustomLifecycleOwner {
fun getCurrentState(): CustomLifecycle.State
fun addObserver(life: CustomLifecycle)
Expand Down
16 changes: 10 additions & 6 deletions app/src/main/java/com/pic/catcher/config/AppConfig.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
package com.pic.catcher.config

import androidx.annotation.Keep
import com.pic.catcher.ui.JsonMenuManager
import org.json.JSONObject


class AppConfig(
var mainUi: MainUi?
var mainUi: MainUi?,
var version: Int,
) {
constructor() : this(null)
constructor() : this(null, 0)

companion object {
fun fromJson(json: JSONObject?): AppConfig? {
return if (json == null) null else AppConfig(
MainUi.fromJson(json.optJSONObject("mainUi"))
MainUi.fromJson(json.optJSONObject("mainUi")),
json.optInt("version")
)
}
}
}

class MainUi(
var donateCard: DonateCard?,
var moduleCard: ModuleCard?
var moduleCard: ModuleCard?,
var menuList: List<MenuBean>?
) {
companion object {
fun fromJson(json: JSONObject?): MainUi? {
return if (json == null) null else MainUi(
DonateCard.fromJson(json.optJSONObject("donateCard")),
ModuleCard.fromJson(json.optJSONObject("moduleCard"))
ModuleCard.fromJson(json.optJSONObject("moduleCard")),
MenuBean.fromJsonArray(json.optJSONArray("menuList"))
)
}
}
Expand Down
123 changes: 95 additions & 28 deletions app/src/main/java/com/pic/catcher/config/AppConfigUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.pic.catcher.config

import android.net.Uri
import com.lu.magic.util.AppUtil

import com.lu.magic.util.log.LogUtil
import com.pic.catcher.R
import com.pic.catcher.util.TimeExpiredCalculator
import com.pic.catcher.util.http.HttpConnectUtil
import com.pic.catcher.R
import org.json.JSONArray
import org.json.JSONObject
import java.io.File

Expand All @@ -23,55 +23,65 @@ class AppConfigUtil {
//5分钟过期时间
val releaseNoteExpiredSetting by lazy { TimeExpiredCalculator(5 * 60 * 1000L) }

fun init() {
load()
checkRemoteUpdate()
}


fun load() {
val file = getLocalFile(configFilePath)
var rawBin: ByteArray
val rawConfig: JSONObject = AppUtil.getContext().resources.openRawResource(R.raw.app_config).use {
rawBin = it.readBytes()
JSONObject(rawBin.toString(Charsets.UTF_8))
}
val localConfig: JSONObject? = try {
if (file.exists()) JSONObject(file.readText(Charsets.UTF_8)) else null
} catch (e: Exception) {
LogUtil.e("load local config fail, del it", e)
file.delete()
null
}
val finalJson = localConfig ?: rawConfig
if (localConfig == null) {
saveLocalFile(file, rawBin)
} else {
if (rawConfig.optInt("version", 0) > localConfig.optInt("version", 0)) {
saveLocalFile(file, rawBin)
}
}
config = AppConfig.fromJson(finalJson) ?: config
}


// 不通过构造函数创建对象
// UnsafeAllocator.INSTANCE.newInstance(AppConfig::class.java)
fun load(callBack: ((config: AppConfig, fromRemote: Boolean) -> Unit)? = null) {
fun checkRemoteUpdate(callBack: ((config: AppConfig, fromRemote: Boolean) -> Unit)? = null) {
val rawUrl = "$githubMainUrl/$configFilePath"
//例如分支v1.12, 写法url编码,且前缀加@v:@vv1.12%2Fdev
val cdnUrl = "$cdnMainUrl/$configFilePath"
val file = getLocalFile(configFilePath)

val file = File(AppUtil.getContext().filesDir, configFilePath)
if (!file.exists()) {
try {
file.parentFile?.mkdirs()
} catch (e: Exception) {
}
}
HttpConnectUtil.get(rawUrl, HttpConnectUtil.noCacheHttpHeader) { raw ->
if (raw.error != null || raw.code != 200) {
LogUtil.d("request raw fail, $rawUrl", raw)
HttpConnectUtil.get(cdnUrl, HttpConnectUtil.noCacheHttpHeader) { cdn ->
if (cdn.error == null && cdn.code == 200) {
parseConfig(cdn.body)
saveLocalFile(file, cdn.body)
callBack?.invoke(config, true)
} else {
LogUtil.d("request cdn fail, $cdnUrl", cdn)
//本地读取
parseLocalConfig(file)
callBack?.invoke(config, false)
}
}
} else {
parseConfig(raw.body)
saveLocalFile(file, raw.body)
callBack?.invoke(config, true)
}
}
}

private fun parseLocalConfig(file: File) {
file.readBytes().let {
parseConfig(it)
}
}

private fun parseConfig(data: ByteArray) {
val jsonText = data.toString(Charsets.UTF_8)
AppConfig.fromJson(JSONObject(jsonText))?.let {
config = it
}
}

private fun saveLocalFile(file: File, data: ByteArray) {
file.outputStream().use {
Expand Down Expand Up @@ -118,7 +128,64 @@ class AppConfigUtil {
}

private fun getLocalFile(relativePath: String): File {
return File(AppUtil.getContext().filesDir, relativePath)
return File(AppUtil.getContext().filesDir, relativePath).also {
it.parentFile?.mkdirs()
}
}
}
}


class MenuBean(
var groupId: Int = 0,
var itemId: Int = 0,
var order: Int = 0,
var title: String? = "",
var link: String? = "",
var appLink: AppLink? = null,
/**最低支持app版本,小于则不显示*/
var since: Int = 0
) {
companion object {
fun fromJson(json: JSONObject?): MenuBean {
return if (json == null) MenuBean() else MenuBean(
json.optInt("groupId"),
json.optInt("itemId"),
json.optInt("order"),
json.optString("title"),
json.optString("link"),
json.optJSONObject("appLink")?.let { AppLink.fromJson(it) },
json.optInt("since")
)
}

fun fromJsonArray(jsonArray: JSONArray?): ArrayList<MenuBean>? {
if (jsonArray == null) {
return null
}
val result = ArrayList<MenuBean>()
for (i in 0 until jsonArray.length()) {
result.add(MenuBean.fromJson(jsonArray.optJSONObject(i)))
}
return result
}
}
}


class AppLink(var links: Array<String?>? = null, var priority: Int = 0) {
companion object {
fun fromJson(json: JSONObject?): AppLink? {
return if (json == null) null else AppLink(
json.optJSONArray("links")?.let {
val result = ArrayList<String?>()
for (i in 0 until it.length()) {
result.add(it.optString(i))
}
result.toTypedArray()
},
json.optInt("priority")
)
}
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/pic/catcher/plugin/OKHttpPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ private void handleHookAndroidOkHttp(Context context, XC_LoadPackage.LoadPackage
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Object response = XposedHelpers2.getObjectField(param.thisObject, "userResponse");

if (response == null) {
return;
}
// LogUtil.d("response", response);
String contentType = (String) XposedHelpers2.callMethod(response, "header", "Content-Type");
if (TextUtils.isEmpty(contentType)) {
LogUtil.d("content-type is empty");
// if (Regexs.PIC_URL.matcher(url))
return;
}
String guessFileEx = MimeTypeMap.getSingleton().getExtensionFromMimeType(contentType);
Expand Down
Loading

0 comments on commit 1de1dd9

Please sign in to comment.