package com.uiui.zyos.utils; import android.annotation.SuppressLint; import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.provider.Settings; import android.text.TextUtils; import android.util.Log; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.tencent.mmkv.MMKV; import com.uiui.zyos.bean.LessonJson; import com.uiui.zyos.config.CommonConfig; import com.uiui.zyos.jxw.JxwPackageConfig; import java.lang.reflect.Type; public class OpenApkUtils { private static final String TAG = OpenApkUtils.class.getSimpleName(); @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() { int is_activation = Settings.Global.getInt(mContext.getContentResolver(), CommonConfig.UIUI_ACTIVATION_KEY, 0); if (is_activation != 1) { ApkUtils.openPackage(mContext, "com.uiui.zysn"); ToastUtil.show("请激活设备后使用"); } return is_activation == 1; } public boolean openPackageWithArgs(String packageName, String className, String name, String args) { if (!ApkUtils.isAvailable(mContext, packageName)) { ToastUtil.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 void refresh() { String jsonString = mMMKV.decodeString(CommonConfig.CLOUD_LESSON_SETTINGS_KEY); if (!TextUtils.isEmpty(jsonString)) { Gson gson = new Gson(); Type type = new TypeToken() { }.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 (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 void sendRuningApp(String packageName) { AppUsedTimeUtils.getInstance().setAppPackageName(packageName); AppUsedTimeUtils.getInstance().setStartTime(System.currentTimeMillis()); AppUsedTimeUtils.getInstance().sendRunningApp(() -> { }); } public void openApp(String packageName) { if (!ApkUtils.isAvailable(mContext, packageName)) { ToastUtil.show("应用未安装"); return; } if (isCloudLessonMod(packageName)) { ApkUtils.openPackage(mContext, packageName); sendRuningApp(packageName); } else { ToastUtil.show("网课模式只允许使用指定应用"); } } public void openApp(String packageName, String className) { if (!ApkUtils.isAvailable(mContext, packageName)) { ToastUtil.show("应用未安装"); return; } if (isCloudLessonMod(packageName)) { ApkUtils.openPackage(mContext, packageName, className); sendRuningApp(packageName); } else { ToastUtil.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 { ToastUtil.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 { ToastUtil.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 { ToastUtil.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 { ToastUtil.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 { ToastUtil.show("网课模式只允许使用指定应用"); } } public void openPrecision(String args) { if (!checkActivation()) { return; } String packageName = JxwPackageConfig.JXW_PRECISION_PACKAGE_NAME; String className = JxwPackageConfig.JXW_PRECISION_CLASS_NAME; if (isCloudLessonMod(packageName)) { if (openPackageWithArgs(packageName, className, "tiku_func_type", args)) { sendRuningApp(packageName); } } else { ToastUtil.show("网课模式只允许使用指定应用"); } } public void openAppWithoutArgs(String packageName, String className) { if (!checkActivation()) { return; } if (isCloudLessonMod(packageName)) { openApp(packageName, className); } else { ToastUtil.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 { ToastUtil.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 { ToastUtil.show("网课模式只允许使用指定应用"); } } public boolean openLetterApp(String packageName, String className) { if (!ApkUtils.isAvailable(mContext, packageName)) { ToastUtil.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_LABORATORY_PACKAGE_NAME; String className = JxwPackageConfig.JXW_writing_CLASS_NAME; if (isCloudLessonMod(packageName)) { if (openPackageWithArgs(packageName, className, "StartArgs", "小学")) { sendRuningApp(packageName); } } else { ToastUtil.show("网课模式只允许使用指定应用"); } } }