| | |
| | | import androidx.annotation.NonNull |
| | | import androidx.lifecycle.MutableLiveData |
| | | import androidx.multidex.MultiDex |
| | | import androidx.work.Configuration |
| | | import androidx.work.WorkManager |
| | | import androidx.work.* |
| | | import com.example.firstapp.core.Core |
| | | import com.example.firstapp.database.repository.CodeRepository |
| | | import com.example.firstapp.database.repository.KeywordRepository |
| | | import com.example.firstapp.database.repository.MsgRepository |
| | | import com.example.firstapp.database.repository.ReminderRepository |
| | | import com.example.firstapp.database.service.RetrofitClient |
| | | import com.example.firstapp.receiver.CactusReceiver |
| | | import com.example.firstapp.utils.AppInfo |
| | | import com.example.firstapp.utils.FRONT_CHANNEL_ID |
| | | import com.example.firstapp.utils.FRONT_CHANNEL_NAME |
| | | import com.example.firstapp.utils.FRONT_NOTIFY_ID |
| | | import com.example.firstapp.utils.FRPC_LIB_VERSION |
| | | import com.example.firstapp.utils.Log |
| | | import com.example.firstapp.utils.PreferencesManager |
| | | import com.example.firstapp.utils.SettingUtils |
| | | import com.example.firstapp.service.ReminderWorker |
| | | import com.example.firstapp.utils.* |
| | | import com.example.firstapp.utils.tinker.TinkerLoadLibrary |
| | | import com.gyf.cactus.Cactus |
| | | import com.gyf.cactus.callback.CactusCallback |
| | |
| | | import java.io.FileWriter |
| | | import java.io.IOException |
| | | import java.text.SimpleDateFormat |
| | | import java.util.Date |
| | | import java.util.Locale |
| | | import java.util.* |
| | | |
| | | @Suppress("DEPRECATION") |
| | | class App : Application(), CactusCallback, Configuration.Provider by Core { |
| | | class App : Application(), CactusCallback, Configuration.Provider { |
| | | |
| | | val applicationScope = CoroutineScope(SupervisorJob()) |
| | | val database by lazy { AppDatabase.getInstance(this) } |
| | | val msgRepository by lazy { MsgRepository(database.msgDao()) } |
| | | val codeRepository by lazy { CodeRepository(database.codeDao()) } |
| | | val reminderRepository by lazy { ReminderRepository(database.reminderDao()) } |
| | | val keywordRepository by lazy { KeywordRepository(RetrofitClient.apiService,database.keywordDao()) } |
| | | |
| | | companion object { |
| | |
| | | } catch (ex: IOException) { |
| | | ex.printStackTrace() |
| | | } |
| | | //使用默认的处理方式让APP停止运行 |
| | | defaultHandler?.uncaughtException(thread, throwable) |
| | | } |
| | | |
| | |
| | | context = applicationContext |
| | | initLibs() |
| | | PreferencesManager.init(this) |
| | | //纯客户端模式 |
| | | if (SettingUtils.enablePureClientMode) return |
| | | |
| | | //初始化WorkManager |
| | | // WorkManager.initialize(this, Configuration.Builder().build()) |
| | | try { |
| | | if (SettingUtils.enablePureClientMode) return |
| | | } catch (e: Exception) { |
| | | // 捕获SettingUtils未初始化的异常 |
| | | Log.e(TAG, "SettingUtils未初始化: ${e.message}") |
| | | // 继续执行后续代码,不返回 |
| | | } |
| | | |
| | | // 初始化WorkManager |
| | | WorkManager.initialize(this, getWorkManagerConfiguration()) |
| | | |
| | | // 设置定时检查提醒的WorkManager |
| | | setupReminderWorker() |
| | | |
| | | //动态加载FrpcLib |
| | | val libPath = filesDir.absolutePath + "/libs" |
| | |
| | | @NonNull |
| | | override fun getWorkManagerConfiguration(): Configuration { |
| | | return Configuration.Builder() |
| | | .setMinimumLoggingLevel(android.util.Log.DEBUG) // 设置日志级别 |
| | | .setMinimumLoggingLevel(android.util.Log.DEBUG) |
| | | .build() |
| | | } |
| | | |
| | |
| | | Core.init(this) |
| | | Log.init(applicationContext) |
| | | // 配置文件初始化 |
| | | /* SharedPreference.init(applicationContext) |
| | | SharedPreference.init(applicationContext) |
| | | // X系列基础库初始化 |
| | | // XBasicLibInit.init(this) |
| | | // 初始化日志打印 |
| | | isDebug = SettingUtils.enableDebugMode |
| | | Log.init(applicationContext) |
| | | // 转发历史工具类初始化 |
| | | HistoryUtils.init(applicationContext) |
| | | // HistoryUtils.init(applicationContext) |
| | | // 版本更新初始化 |
| | | // XUpdateInit.init(this) |
| | | // 运营统计数据 |
| | | // UMengInit.init(this) |
| | | // 初始化语种切换框架 |
| | | MultiLanguages.init(this) |
| | | // MultiLanguages.init(this) |
| | | // 设置语种变化监听器 |
| | | MultiLanguages.setOnLanguageListener(object : OnLanguageListener { |
| | | override fun onAppLocaleChange(oldLocale: Locale, newLocale: Locale) { |
| | | // 注意:只有setAppLanguage时触发,clearAppLanguage时不触发 |
| | | Log.i(TAG, "监听到应用切换了语种,旧语种:$oldLocale,新语种:$newLocale") |
| | | switchLanguage(newLocale) |
| | | } |
| | | |
| | | override fun onSystemLocaleChange(oldLocale: Locale, newLocale: Locale) { |
| | | Log.i(TAG, "监听到系统切换了语种,旧语种:$oldLocale,新语种:$newLocale") |
| | | switchLanguage(newLocale) |
| | | *//*val isFlowSystem = SettingUtils.isFlowSystemLanguage //MultiLanguages.isSystemLanguage(context)取值不对,一直是false |
| | | Log.i(TAG, "监听到系统切换了语种,旧语种:$oldLocale,新语种:$newLocale,是否跟随系统:$isFlowSystem") |
| | | if (isFlowSystem) { |
| | | CommonUtils.switchLanguage(oldLocale, newLocale) |
| | | }*//* |
| | | } |
| | | }) |
| | | switchLanguage(MultiLanguages.getAppLanguage(this))*/ |
| | | // MultiLanguages.setOnLanguageListener(object : OnLanguageListener { |
| | | // override fun onAppLocaleChange(oldLocale: Locale, newLocale: Locale) { |
| | | // // 注意:只有setAppLanguage时触发,clearAppLanguage时不触发 |
| | | // Log.i(TAG, "监听到应用切换了语种,旧语种:$oldLocale,新语种:$newLocale") |
| | | // switchLanguage(newLocale) |
| | | // } |
| | | // |
| | | // override fun onSystemLocaleChange(oldLocale: Locale, newLocale: Locale) { |
| | | // Log.i(TAG, "监听到系统切换了语种,旧语种:$oldLocale,新语种:$newLocale") |
| | | // switchLanguage(newLocale) |
| | | // *//*val isFlowSystem = SettingUtils.isFlowSystemLanguage //MultiLanguages.isSystemLanguage(context)取值不对,一直是false |
| | | // Log.i(TAG, "监听到系统切换了语种,旧语种:$oldLocale,新语种:$newLocale,是否跟随系统:$isFlowSystem") |
| | | // if (isFlowSystem) { |
| | | // CommonUtils.switchLanguage(oldLocale, newLocale) |
| | | // }*//* |
| | | // } |
| | | // }) |
| | | // switchLanguage(MultiLanguages.getAppLanguage(this)) |
| | | } |
| | | |
| | | @SuppressLint("CheckResult") |
| | |
| | | // ) |
| | | // } |
| | | |
| | | /** |
| | | * 设置定时提醒Worker |
| | | * 配置为每天运行一次检查是否有新的提醒内容 |
| | | */ |
| | | private fun setupReminderWorker() { |
| | | Log.d(TAG, "设置定时提醒Worker") |
| | | |
| | | val constraints = Constraints.Builder() |
| | | .setRequiredNetworkType(NetworkType.CONNECTED) |
| | | .build() |
| | | |
| | | // 创建周期性工作请求 |
| | | val reminderWorkRequest = PeriodicWorkRequestBuilder<ReminderWorker>( |
| | | ReminderWorker.REPEAT_INTERVAL, |
| | | ReminderWorker.REPEAT_INTERVAL_TIME_UNIT |
| | | ) |
| | | .setConstraints(constraints) |
| | | .addTag("reminder_worker") |
| | | .build() |
| | | |
| | | // 使用 REPLACE 策略确保新配置生效 |
| | | WorkManager.getInstance(this).enqueueUniquePeriodicWork( |
| | | "reminder_work", |
| | | ExistingPeriodicWorkPolicy.REPLACE, |
| | | reminderWorkRequest |
| | | ) |
| | | } |
| | | |
| | | } |