Files
Xuewang365OSNeutral/app/src/main/java/com/xwad/os/utils/OpenApkUtils.java
tongtongstudio 7e16ba8813 version
fix:
update:修改包名
2025-11-28 20:50:52 +08:00

712 lines
25 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.xwad.os.utils;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xwad.os.bean.LessonJson;
import com.xwad.os.config.CommonConfig;
import com.xwad.os.jxw.JxwPackageConfig;
import com.xwad.os.manager.RemoteManager;
import com.hjq.toast.Toaster;
import com.tencent.mmkv.MMKV;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BiConsumer;
public class OpenApkUtils {
private static final String TAG = "OpenApkUtils";
@SuppressLint("StaticFieldLeak")
private static OpenApkUtils sInstance;
private Context mContext;
private ContentResolver mResolver;
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
private LessonJson mLessonJson;
private TimeUtils.ContralTime mContralTime;
private OpenApkUtils(Context context) {
if (context == null) {
throw new RuntimeException("Context is NULL");
}
this.mContext = context;
this.mResolver = context.getContentResolver();
refresh();
}
public static void init(Context context) {
if (sInstance == null) {
sInstance = new OpenApkUtils(context);
}
}
public static OpenApkUtils getInstance() {
if (sInstance == null) {
throw new IllegalStateException("You must be init OpenApkUtils first");
}
return sInstance;
}
private boolean checkActivation() {
if (!ApkUtils.isAvailable(mContext, "com.uiui.zy")) {
return true;
}
boolean is_activation = RemoteManager.getInstance().getSnIsActivation();
if (!is_activation) {
OpenApkUtils.getInstance().openZySn();
Toaster.show("请激活设备后使用");
}
return is_activation;
}
public boolean openPackageWithArgs(String packageName, String className, String name, String args) {
if (!ApkUtils.isAvailable(mContext, packageName)) {
Toaster.show("应用未安装");
return false;
}
ComponentName cn = new ComponentName(packageName, className);
Intent intent = new Intent();
intent.setComponent(cn);
intent.putExtra(name, args);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
try {
mContext.startActivity(intent);
return true;
} catch (Exception e) {
Log.e(TAG, "openPackageWithArgs: " + e.getMessage());
}
return false;
}
public boolean openPackageWithMultiArgs(String packageName, String className, HashMap<String, String> args) {
if (!ApkUtils.isAvailable(mContext, packageName)) {
Toaster.show("应用未安装");
return false;
}
ComponentName cn = new ComponentName(packageName, className);
Intent intent = new Intent();
intent.setComponent(cn);
if (args != null) {
args.forEach(new BiConsumer<String, String>() {
@Override
public void accept(String s, String s2) {
intent.putExtra(s, s2);
}
});
}
intent.putExtra("index", 1);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
try {
mContext.startActivity(intent);
return true;
} catch (Exception e) {
Log.e(TAG, "openPackageWithArgs: " + e.getMessage());
}
return false;
}
public void refresh() {
String jsonString = mMMKV.decodeString(CommonConfig.CLOUD_LESSON_SETTINGS_KEY);
if (!TextUtils.isEmpty(jsonString)) {
Gson gson = new Gson();
Type type = new TypeToken<LessonJson>() {
}.getType();
LessonJson lessonJson = gson.fromJson(jsonString, type);
if (lessonJson != null) {
this.mLessonJson = lessonJson;
TimeUtils.ContralTime contralTime = TimeUtils.getContralTime(mContext, lessonJson.getStart_time(), lessonJson.getEnd_time());
if (contralTime != null) {
mContralTime = contralTime;
}
}
}
}
/**
* @param pkg
* @return true为能打开
*/
public boolean isCloudLessonMod(String pkg) {
if (mJxwApps.contains(pkg)) {
return true;
}
if (mLessonJson == null)
return true;
if (mLessonJson.getIs_lesson() == 1) {
if (mContralTime == null) {
return false;
} else {
String pkgs = mLessonJson.getPkgs();
if (mContralTime.inControlTime()) {
return pkgs.contains(pkg);
} else {
return false;
}
}
} else {
return true;
}
}
private Set<String> mJxwApps = new HashSet<String>() {{
this.add("air.com.zhihuiyoujiao.flashplayer");
this.add("com.example.arithmeticformula");
this.add("com.example.elementcycleapp");
this.add("com.example.pianpangbushou");
this.add("com.iflytek.cyber.iot.show.core");
this.add("com.iflytek.speechcloud");
this.add("com.jxw.bihuamingcheng");
this.add("com.jxw.bishunguize");
this.add("com.jxw.characterlearning");
this.add("com.jxw.dmxcy");
this.add("com.jxw.englishsoundmark");
this.add("com.jxw.examsystem");
this.add("com.jxw.game");
this.add("com.jxw.gb.zwpg");
this.add("com.jxw.huiben");
this.add("com.jxw.zwpg");
this.add("com.jxw.handwrite");
this.add("com.jxw.jinfangyici");
this.add("com.jxw.jxwbook");
this.add("com.jxw.jxwcalculator");
this.add("com.jxw.laboratory");
this.add("com.jxw.learnchinesepinyin");
this.add("com.jxw.letterstudynew");
this.add("com.jxw.liancichengju");
this.add("com.jxw.mskt.video");
this.add("com.jxw.newyouer.video");
this.add("com.jxw.online_study");
this.add("com.jxw.question");
this.add("com.jxw.schultegrid");
this.add("com.jxw.singsound");
this.add("com.jxw.studydigital");
this.add("com.jxw.teacher.video");
this.add("com.jxw.wuweijidanci");
this.add("com.jxw.youer.video");
this.add("com.jxw.yuwenxiezuo");
this.add("com.jxw.yyhb");
this.add("com.jxw.zncd");
this.add("com.oirsdfg89.flg");
this.add("com.study.flashplayer");
this.add("com.tech.translate");
this.add("com.uiui.zybrowser");
this.add("com.uiui.zysn ");
this.add("com.jxw.launcher");
this.add("com.uiui.zyappstore");
this.add("com.uiui.zy");
this.add("com.uiui.zyos");
this.add("com.teclast.zyos");
this.add("com.teclast.zybrowser");
this.add("com.teclast.zyappstore");
this.add("com.teclast.zy");
}};
private void sendRuningApp(String packageName) {
boolean activation = ActivationUtil.getInstance().isActivation();
if (!activation) {
return;
}
AppUsedTimeUtils.getInstance().setAppPackageName(packageName);
AppUsedTimeUtils.getInstance().setStartTime(System.currentTimeMillis());
AppUsedTimeUtils.getInstance().sendRunningApp(() -> {
});
}
public void openApp(String packageName) {
if (!ApkUtils.isAvailable(mContext, packageName)) {
Toaster.show("应用未安装");
return;
}
if (isCloudLessonMod(packageName)) {
ApkUtils.openPackage(mContext, packageName);
sendRuningApp(packageName);
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public void openApp(String packageName, String className) {
if (!ApkUtils.isAvailable(mContext, packageName)) {
Toaster.show("应用未安装");
return;
}
if (isCloudLessonMod(packageName)) {
if ("cn.wps.note".equals(packageName)) {
ApkUtils.openPackage(mContext, packageName);
} else {
ApkUtils.openPackage(mContext, packageName, className);
}
sendRuningApp(packageName);
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public void openSyncVideo(String args) {
if (!checkActivation()) {
return;
}
String packageName = JxwPackageConfig.JXW_VIDEO_PACKAGE_NAME;
if (isCloudLessonMod(packageName)) {
if (openPackageWithArgs(packageName, JxwPackageConfig.JXW_VIDEO_CLASS_NAME, "StartArgs", args)) {
sendRuningApp(packageName);
}
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public void openSynchronousTutoring(String args) {
if (!checkActivation()) {
return;
}
String packageName = JxwPackageConfig.JXW_TUTORING_PACKAGE_NAME;
String className = JxwPackageConfig.JXW_TUTORING_CLASS_NAME;
if (isCloudLessonMod(packageName)) {
if (openPackageWithArgs(packageName, className, "StartArgs", args)) {
sendRuningApp(packageName);
}
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public void openSolidifiedData(String args) {
if (!checkActivation()) {
return;
}
String packageName = JxwPackageConfig.JXW_TUTORING_PACKAGE_NAME;
String className = JxwPackageConfig.JXW_RHETORIC_CLASS_NAME;
if (isCloudLessonMod(packageName)) {
if (openPackageWithArgs(packageName, className, "StartArgs", args)) {
sendRuningApp(packageName);
}
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public void openComposition() {
if (!checkActivation()) {
return;
}
String packageName = JxwPackageConfig.JXW_VIDEO_PACKAGE_NAME;
String className = JxwPackageConfig.JXW_COMPOSITION_CLASS_NAME;
if (isCloudLessonMod(packageName)) {
if (openPackageWithArgs(packageName, className, "StartArgs", "专区数据/专题精品课/语文阅读与写作/小学")) {
sendRuningApp(packageName);
}
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public void openQuality() {
if (!checkActivation()) {
return;
}
String packageName = JxwPackageConfig.JXW_VIDEO_PACKAGE_NAME;
String className = JxwPackageConfig.JXW_QUALITY_CLASS_NAME;
if (isCloudLessonMod(packageName)) {
if (openPackageWithArgs(packageName, className, "StartArgs", "中华文化")) {
sendRuningApp(packageName);
} else {
Toaster.show("打开失败:应用未安装");
}
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public void openWordLecture() {
if (!checkActivation()) {
return;
}
String packageName = JxwPackageConfig.JXW_VIDEO_PACKAGE_NAME;
String className = JxwPackageConfig.JXW_WORD_LECTURE_CLASS_NAME;
HashMap<String, String> map = new HashMap<>();
map.put("gradeName", "一年级");
map.put("pressName", "人教版");
if (isCloudLessonMod(packageName)) {
if (openPackageWithMultiArgs(packageName, className, map)) {
sendRuningApp(packageName);
} else {
Toaster.show("打开失败:应用未安装");
}
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public void openExamSprint() {
if (!checkActivation()) {
return;
}
String packageName = JxwPackageConfig.JXW_VIDEO_PACKAGE_NAME;
String className = JxwPackageConfig.JXW_EXAM_SPRINT_CLASS_NAME;
HashMap<String, String> map = new HashMap<>();
map.put("title", "中考第一轮复习");
map.put("subject", "数学");
if (isCloudLessonMod(packageName)) {
if (openPackageWithMultiArgs(packageName, className, map)) {
sendRuningApp(packageName);
} else {
Toaster.show("打开失败:应用未安装");
}
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public boolean isNewAiApp() {
PackageManager pm = mContext.getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo = pm.getPackageInfo(JxwPackageConfig.JXW_PRECISION_PACKAGE_NAME, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageInfo == null) {
return false;
}
// long appVersionCode;
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// appVersionCode = packageInfo.getLongVersionCode();
// } else {
// appVersionCode = packageInfo.versionCode;
// }
// if (appVersionCode > 10) {
// return false;
// } else {
// return true;
// }
String versionName = packageInfo.versionName;
Log.e(TAG, "isNewAiApp: versionName = " + versionName);
try {
BigDecimal version = new BigDecimal("20240527");
BigDecimal bigDecimal = new BigDecimal(versionName);
if (bigDecimal.compareTo(version) > 0) {
return true;
} else {
return false;
}
} catch (Exception e) {
Log.e(TAG, "isNewAiApp: " + e.getMessage());
return true;
}
}
public void openPrecision(String args) {
if (!checkActivation()) {
return;
}
String packageName = JxwPackageConfig.JXW_PRECISION_PACKAGE_NAME;
String className = JxwPackageConfig.JXW_PRECISION_CLASS_NAME_85;
if (isCloudLessonMod(packageName)) {
if (openPackageWithArgs(packageName, className, "tiku_func_type", args)) {
sendRuningApp(packageName);
} else {
Toaster.show("打开失败:应用未安装");
}
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public void openAiPrecision(int page, String subject) {
if (!checkActivation()) {
return;
}
String packageName = JxwPackageConfig.JXW_PRECISION_PACKAGE_NAME;
String className = JxwPackageConfig.JXW_PRECISION_CLASS_NAME_1;
if (isCloudLessonMod(packageName)) {
if (openAiWithArgs(packageName, className, page, subject)) {
sendRuningApp(packageName);
} else {
Toaster.show("打开失败:应用未安装");
}
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public static final String NAME_PRIMARY_SCHOOL = "小学";
public static final String NAME_MIDDLE_SCHOOL = "初中";
public static final String NAME_HIGH_SCHOOL = "高中";
/**
* page_navigate 写死传0
* <p>
* key:"page_navigate" 说明:
* 0主页
* 3收藏夹
* 4错题本
* 7考点学测
* 8一键诊断
* 组卷中心5
* 阶段测1
* 专项测2
* <p>
* appoint_subject_list 不需要传
* appoint_period_name 这个是学段,一定要传
* <p>
* public static final String NAME_PRIMARY_SCHOOL = "小学";
* public static final String NAME_MIDDLE_SCHOOL = "初中";
* public static final String NAME_HIGH_SCHOOL = "高中";
* <p>
* appoint_period_name字段的值是上面这三个之一
*
* @param packageName
* @param className
* @param page
* @param subject
* @return
*/
public boolean openAiWithArgs(String packageName, String className, int page, String subject) {
if (!ApkUtils.isAvailable(mContext, packageName)) {
Toaster.show("应用未安装");
return false;
}
String grade = mMMKV.decodeString(CommonConfig.APPOINT_PERIOD_NAME, NAME_PRIMARY_SCHOOL);
ComponentName cn = new ComponentName(packageName, className);
Intent intent = new Intent();
intent.setComponent(cn);
intent.putExtra("page_navigate", page);
if (!TextUtils.isEmpty(subject)) {
intent.putExtra("appoint_subject_name", subject);
}
intent.putExtra("appoint_period_name", grade);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
try {
mContext.startActivity(intent);
return true;
} catch (Exception e) {
Log.e(TAG, "openPackageWithArgs: " + e.getMessage());
}
return false;
}
public void openAppWithoutArgs(String packageName, String className) {
if (!checkActivation()) {
return;
}
if (isCloudLessonMod(packageName)) {
openApp(packageName, className);
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public void openLaboratory(String args) {
if (!checkActivation()) {
return;
}
String packageName = JxwPackageConfig.JXW_LABORATORY_PACKAGE_NAME;
String className = JxwPackageConfig.JXW_LABORATORY_CLASS_NAME;
if (isCloudLessonMod(packageName)) {
if (openPackageWithArgs(packageName, className, "StartArgs", args)) {
sendRuningApp(packageName);
}
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public void openLetter() {
if (!checkActivation()) {
return;
}
String packageName = JxwPackageConfig.JXW_LETTER_PACKAGE_NAME;
String className = JxwPackageConfig.JXW_LETTER_CLASS_NAME;
if (isCloudLessonMod(packageName)) {
if (openLetterApp(packageName, className)) {
sendRuningApp(packageName);
}
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public boolean openLetterApp(String packageName, String className) {
if (!ApkUtils.isAvailable(mContext, packageName)) {
Toaster.show("应用未安装");
return false;
}
ComponentName cn = new ComponentName(packageName, className);
Intent intent = new Intent();
intent.setComponent(cn);
intent.putExtra("isTopic", true);
intent.putExtra("isYYGJ", true);
intent.putExtra("url", "http://api4.jiumentongbu.com/api/jwfd/baseapp/url?filePath=专区数据/英语广角/幼儿启蒙/ABC字母/menu.json");
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
try {
mContext.startActivity(intent);
return true;
} catch (Exception e) {
Log.e(TAG, "openPackageWithArgs: " + e.getMessage());
}
return false;
}
public void openChineseWriting() {
if (!checkActivation()) {
return;
}
String packageName = JxwPackageConfig.JXW_WRITING_PACKAGE_NAME;
String className = JxwPackageConfig.JXW_WRITING_CLASS_NAME;
if (isCloudLessonMod(packageName)) {
if (openPackageWithArgs(packageName, className, "StartArgs", "小学")) {
sendRuningApp(packageName);
}
} else {
Toaster.show("专注模式只允许使用指定应用");
}
}
public void openARHuiben() {
Intent intent = new Intent();
intent.setClassName(JxwPackageConfig.JXW_HUIBEN_NAME, JxwPackageConfig.JXW_HUIBEN_CLASS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.putExtra("StartArgs", "keben");
mContext.startActivity(intent);
}
public void openZuowenpigai() {
Intent intent = new Intent();
intent.setClassName(JxwPackageConfig.JXW_ZWPG_NAME, JxwPackageConfig.JXW_ZWPG_CLASS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
mContext.startActivity(intent);
}
public void openZuowenpigai6() {
Intent intent = new Intent();
intent.setClassName(JxwPackageConfig.JXW_ZWPG_NAME_6, JxwPackageConfig.JXW_ZWPG_CLASS_6);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
mContext.startActivity(intent);
}
public boolean openZySn() {
if (!ApkUtils.isAvailable(mContext, "com.uiui.zy")) {
Toaster.show("应用未安装");
return false;
}
try {
PackageManager pm = mContext.getPackageManager();
PackageInfo packageInfo = pm.getPackageInfo("com.uiui.zy", 0);
long appVersionCode;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
appVersionCode = packageInfo.getLongVersionCode();
} else {
appVersionCode = packageInfo.versionCode;
}
if (appVersionCode <= 95) {
openApp("com.uiui.zy", "com.uiui.zy.activity.main.MainActivity");
} else {
openApp("com.uiui.zy", "com.uiui.zy.activity.splash.SplashActivity");
}
return true;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "onCreate: " + e.getMessage());
}
return false;
}
public void openJxwApp(String paramStr) {
if (TextUtils.isEmpty(paramStr)) {
Log.e(TAG, "openJxwApp: params is null");
return;
}
// 分割字符串(使用-1保留空元素处理连续逗号情况
String[] params = paramStr.split(",", -1);
if (params.length < 5) {
Log.e(TAG, "参数字符串格式错误,参数数量不足");
return;
}
// 提取各项参数
String packageName = params[0];
String className = params[1];
String extraStr = params[2];
String fourthParam = params[3];
String fifthParam = params[4];
try {
// 构建Intent
Intent intent = new Intent();
ComponentName componentName = new ComponentName(packageName, className);
intent.setComponent(componentName);
// 解析并添加Extra参数
if (extraStr != null && !extraStr.isEmpty()) {
String[] extraParts = extraStr.split(":", 3);
if (extraParts.length == 3) {
String type = extraParts[0];
String key = extraParts[1];
String value = extraParts[2];
// 根据类型添加Extra
switch (type.toLowerCase()) {
case "int":
intent.putExtra(key, Integer.parseInt(value));
break;
case "string":
intent.putExtra(key, value);
break;
case "boolean":
intent.putExtra(key, Boolean.parseBoolean(value));
break;
case "long":
intent.putExtra(key, Long.parseLong(value));
break;
// 可扩展支持其他类型float、double等
default:
Log.w(TAG, "不支持的Extra类型" + type);
intent.putExtra(key, value);
break;
}
} else {
Log.e(TAG, "Extra参数格式错误" + extraStr);
}
}
// 如需传递第四、五项参数,可根据需求添加
// intent.putExtra("fourth_param", fourthParam);
// intent.putExtra("fifth_param", fifthParam);
// 启动Activity需确保上下文是Activity或添加FLAG_ACTIVITY_NEW_TASK
// if (!(context instanceof android.app.Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// }
mContext.startActivity(intent);
} catch (NumberFormatException e) {
Log.e(TAG, "参数类型转换失败", e);
} catch (Exception e) {
Log.e(TAG, "启动Activity失败", e);
}
}
}