Files
Xuewang365OSNeutral/app/src/main/java/com/xwad/os/utils/OpenApkUtils.java
tongtongstudio ed285c05ee version:
fix:
update:跳转优化,资源替换
2025-12-16 14:37:15 +08:00

852 lines
31 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.app.Activity;
import android.app.Dialog;
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 android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.toast.Toaster;
import com.tencent.mmkv.MMKV;
import com.xwad.os.R;
import com.xwad.os.bean.AppInfo;
import com.xwad.os.bean.BaseResponse;
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.xwad.os.network.NetInterfaceManager;
import com.xwad.os.service.DownloadService;
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;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
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);
return bigDecimal.compareTo(version) > 0;
} 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;
}
/**
* 根据参数字符串打开对应页面
*
* @param paramStr 参数字符串(格式:包名,类名,extra参数,第四项,第五项)
*/
public void openJxwApp(Activity activity, String paramStr) {
if (TextUtils.isEmpty(paramStr)) {
Log.e(TAG, "context为空或参数字符串为空");
return;
}
// 1. 按逗号分割整体参数(保留空项,避免丢失字段)
String[] totalParts = paramStr.split(",", -1);
if (totalParts.length < 2) { // 至少需要包名+类名
Log.e(TAG, "参数字符串格式错误:" + paramStr);
return;
}
// 提取各项基础参数
String packageName = totalParts[0]; // 第一项:包名
String className = totalParts[1]; // 第二项:类名
String extraParamStr = totalParts.length > 2 ? totalParts[2] : ""; // 第三项Intent Extra参数
String fourthParam = totalParts.length > 3 ? totalParts[3] : ""; // 第四项(自定义)
String appName = totalParts.length > 4 ? totalParts[4] : ""; // 第五项(自定义)
Log.d(TAG, "包名:" + packageName + "\n类名" + className
+ "\n第四项" + fourthParam + "\n应用名" + appName);
if (!ApkUtils.isAvailable(mContext, packageName)) {
showDownloadDialog(activity, packageName, appName);
return;
}
if (TextUtils.isEmpty(className)) {
openApp(packageName);
return;
}
// 2. 构建Intent并设置Component包名+类名)
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName component = new ComponentName(packageName, className);
intent.setComponent(component);
// 3. 解析Extra参数支持多组用#分隔;每组格式:类型:key:值)
if (!TextUtils.isEmpty(extraParamStr)) {
String[] extraGroups = extraParamStr.split("#");
for (String extraGroup : extraGroups) {
if (TextUtils.isEmpty(extraGroup)) continue;
// 按冒号分割最多分3段避免值中包含冒号
String[] extraParts = extraGroup.split(":", 3);
if (extraParts.length != 3) {
Log.w(TAG, "Extra参数格式错误" + extraGroup);
continue;
}
String type = extraParts[0]; // 类型int/String等
String key = extraParts[1]; // Extra的key
String value = extraParts[2]; // Extra的value
// 根据类型设置Extra
setExtraByType(intent, type, key, value);
}
}
// 4. 启动Activity处理异常
try {
activity.startActivity(intent);
} catch (Exception e) {
if (e instanceof android.content.ActivityNotFoundException) {
Log.e(TAG, "未找到目标Activity" + packageName + "/" + className);
Toaster.show("打开失败,请升级应用到最新版本");
} else if (e instanceof SecurityException) {
Log.e(TAG, "启动Activity权限不足");
} else {
Log.e(TAG, "启动Activity失败" + e.getMessage());
}
e.printStackTrace();
}
}
/**
* 根据类型为Intent设置Extra参数
*
* @param intent 目标Intent
* @param type 参数类型int/String/boolean等
* @param key Extra的key
* @param value Extra的value字符串
*/
private static void setExtraByType(Intent intent, String type, String key, String value) {
if (TextUtils.isEmpty(type) || TextUtils.isEmpty(key)) return;
switch (type.toLowerCase()) {
case "int":
try {
int intValue = Integer.parseInt(value);
intent.putExtra(key, intValue);
} catch (NumberFormatException e) {
Log.w(TAG, "int类型转换失败" + value);
}
break;
case "string":
intent.putExtra(key, value);
break;
case "boolean":
try {
boolean boolValue = Boolean.parseBoolean(value);
intent.putExtra(key, boolValue);
} catch (Exception e) {
Log.w(TAG, "boolean类型转换失败" + value);
}
break;
case "long":
try {
long longValue = Long.parseLong(value);
intent.putExtra(key, longValue);
} catch (NumberFormatException e) {
Log.w(TAG, "long类型转换失败" + value);
}
break;
// 可扩展其他类型float/double等
default:
Log.w(TAG, "不支持的参数类型:" + type);
break;
}
}
public void showDownloadDialog(Activity context, String pkg, String appName) {
Dialog dialog = new Dialog(context, R.style.ActionSheet);
Window window = dialog.getWindow();
View inflate = ((LayoutInflater) context.getSystemService("layout_inflater")).inflate(R.layout.download_dialog, null);
TextView textView = inflate.findViewById(R.id.download_cancel);
TextView textView2 = inflate.findViewById(R.id.tag_title);
if (!TextUtils.isEmpty(appName)) {
textView2.setText("未安装\"" + appName + "\"\n下载后可继续使用!");
}
TextView textView3 = inflate.findViewById(R.id.download_ok);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
textView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getAppInfo(pkg);
Intent intent = new Intent(mContext, DownloadService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mContext.startForegroundService(intent);
} else {
mContext.startService(intent);
}
dialog.dismiss();
}
});
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.x = 0;
attributes.y = 0;
attributes.gravity = 17;
dialog.onWindowAttributesChanged(attributes);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(inflate);
window.setDimAmount(0.6f);
dialog.show();
window.setAttributes(attributes);
}
public void getAppInfo(String pkg) {
NetInterfaceManager.getInstance().getAdminAppObservable(pkg)
.subscribe(new Observer<BaseResponse<AppInfo>>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.e("getAppInfo", "onSubscribe: ");
}
@Override
public void onNext(@NonNull BaseResponse<AppInfo> baseResponse) {
Log.e("getAppInfo", "onNext: " + baseResponse);
if (baseResponse.code == 200) {
AppInfo appInfo = baseResponse.data;
FileUtil.ariaDownload(mContext, appInfo.getApp_url(), appInfo);
} else {
Toaster.showLong("没有找到应用信息,请联系客服");
}
}
@Override
public void onError(@NonNull Throwable e) {
Log.e("getAppInfo", "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Log.e("getAppInfo", "onComplete: ");
}
});
}
}