@@ -6,6 +6,7 @@ package com.fox2code.mmm.module
66import android.annotation.SuppressLint
77import android.content.DialogInterface
88import android.net.Uri
9+ import android.text.Html
910import android.text.Spanned
1011import android.widget.TextView
1112import android.widget.Toast
@@ -382,6 +383,154 @@ enum class ActionButtonType {
382383 ) { _: DialogInterface ? , _: Int -> }
383384 .create().show()
384385 }
386+ },
387+ REMOTE {
388+ @Suppress(" NAME_SHADOWING" )
389+ override fun doAction (button : Chip , moduleHolder : ModuleHolder ) {
390+ Timber .d(" doAction: remote module for %s" , moduleHolder.moduleInfo?.name ? : " null" )
391+ // that module is from remote repo
392+ val name: String? = if (moduleHolder.moduleInfo != null ) {
393+ moduleHolder.moduleInfo!! .name
394+ } else {
395+ moduleHolder.repoModule?.moduleInfo?.name
396+ }
397+ // positive button executes install logic and says reinstall. negative button does nothing
398+ TrackHelper .track().event(" remote_module" , name).with (INSTANCE !! .getTracker())
399+ val madb = MaterialAlertDialogBuilder (button.context)
400+ madb.setTitle(R .string.remote_module)
401+ val moduleInfo: ModuleInfo = if (moduleHolder.mainModuleInfo != null ) {
402+ moduleHolder.mainModuleInfo
403+ } else {
404+ moduleHolder.repoModule?.moduleInfo ? : return
405+ }
406+ var updateZipUrl = moduleHolder.updateZipUrl
407+ if (updateZipUrl.isNullOrEmpty()) {
408+ // try repoModule.zipUrl
409+ val repoModule = moduleHolder.repoModule
410+ if (repoModule?.zipUrl.isNullOrEmpty()) {
411+ Timber .e(" No repo update zip url for %s" , moduleInfo.name)
412+ } else {
413+ updateZipUrl = repoModule?.zipUrl
414+ }
415+ // next try localModuleInfo.updateZipUrl
416+ if (updateZipUrl.isNullOrEmpty()) {
417+ val localModuleInfo = moduleHolder.moduleInfo
418+ if (localModuleInfo != null ) {
419+ updateZipUrl = localModuleInfo.updateZipUrl
420+ } else {
421+ Timber .e(" No local update zip url for %s" , moduleInfo.name)
422+ }
423+ }
424+ if (updateZipUrl.isNullOrEmpty()) {
425+ Timber .e(" No update zip url at all for %s" , moduleInfo.name)
426+ // last ditch effort try moduleinfo
427+ updateZipUrl = moduleHolder.moduleInfo?.updateZipUrl
428+ }
429+ // LAST LAST ditch effort try localModuleInfo.remoteModuleInfo.zipUrl
430+ if (updateZipUrl.isNullOrEmpty()) {
431+ val localModuleInfo = moduleHolder.moduleInfo
432+ if (localModuleInfo != null ) {
433+ updateZipUrl = localModuleInfo.remoteModuleInfo?.zipUrl
434+ } else {
435+ Timber .e(" No local update zip url for %s" , moduleInfo.name)
436+ }
437+ }
438+ }
439+ if (! updateZipUrl.isNullOrEmpty()) {
440+ madb.setMessage(Html .fromHtml(button.context.getString(R .string.remote_message, name), Html .FROM_HTML_MODE_COMPACT ))
441+ madb.setPositiveButton(
442+ R .string.reinstall
443+ ) { _: DialogInterface ? , _: Int ->
444+ Timber .d(" Set moduleinfo to %s" , moduleInfo.name)
445+ val name: String? = if (moduleHolder.moduleInfo != null ) {
446+ moduleHolder.moduleInfo!! .name
447+ } else {
448+ moduleHolder.repoModule?.moduleInfo?.name
449+ }
450+ Timber .d(" doAction: remote module for %s" , name)
451+ TrackHelper .track().event(" view_update_install" , name)
452+ .with (INSTANCE !! .getTracker())
453+ // Androidacy manage the selection between download and install
454+ if (isAndroidacyLink(updateZipUrl)) {
455+ Timber .d(" Androidacy link detected" )
456+ openUrlAndroidacy(
457+ button.context,
458+ updateZipUrl,
459+ true ,
460+ moduleInfo.name,
461+ moduleInfo.config
462+ )
463+ return @setPositiveButton
464+ }
465+ val hasRoot = peekMagiskPath() != null && ! isShowcaseMode
466+ val builder = MaterialAlertDialogBuilder (button.context)
467+ builder.setTitle(moduleInfo.name).setCancelable(true )
468+ .setIcon(R .drawable.ic_baseline_extension_24)
469+ var desc: CharSequence? = moduleInfo.description
470+ var markwon: Markwon ? = null
471+ val localModuleInfo = moduleHolder.moduleInfo
472+ if (localModuleInfo != null && localModuleInfo.updateChangeLog.isNotEmpty()) {
473+ markwon = INSTANCE !! .getMarkwon()
474+ // Re-render each time in cse of config changes
475+ desc = markwon!! .toMarkdown(localModuleInfo.updateChangeLog)
476+ }
477+ if (desc.isNullOrEmpty()) {
478+ builder.setMessage(R .string.no_desc_found)
479+ } else {
480+ builder.setMessage(desc)
481+ }
482+ Timber .i(" URL: %s" , updateZipUrl)
483+ builder.setNegativeButton(R .string.download_module) { _: DialogInterface ? , _: Int ->
484+ openCustomTab(
485+ button.context,
486+ updateZipUrl
487+ )
488+ }
489+ if (hasRoot) {
490+ builder.setPositiveButton(if (moduleHolder.hasUpdate()) R .string.update_module else R .string.install_module) { _: DialogInterface ? , _: Int ->
491+ val updateZipChecksum = moduleHolder.updateZipChecksum
492+ openInstaller(
493+ button.context,
494+ updateZipUrl,
495+ moduleInfo.name,
496+ moduleInfo.config,
497+ updateZipChecksum,
498+ moduleInfo.mmtReborn
499+ )
500+ }
501+ }
502+ ExternalHelper .INSTANCE .injectButton(
503+ builder,
504+ { Uri .parse(updateZipUrl) },
505+ moduleHolder.updateZipRepo
506+ )
507+ val dim5dp = FoxDisplay .dpToPixel(5f )
508+ builder.setBackgroundInsetStart(dim5dp).setBackgroundInsetEnd(dim5dp)
509+ val alertDialog = builder.show()
510+ for (i in - 3 .. - 1 ) {
511+ val alertButton = alertDialog.getButton(i)
512+ if (alertButton != null && alertButton.paddingStart > dim5dp) {
513+ alertButton.setPadding(dim5dp, dim5dp, dim5dp, dim5dp)
514+ }
515+ }
516+ if (markwon != null ) {
517+ val messageView =
518+ alertDialog.window!! .findViewById<TextView >(android.R .id.message)
519+ markwon.setParsedMarkdown(messageView, (desc as Spanned ? )!! )
520+ }
521+ }
522+ } else {
523+ madb.setMessage(button.context.getString(R .string.remote_message_no_update, name))
524+ }
525+ madb.setNegativeButton(R .string.cancel, null )
526+ madb.create().show()
527+ }
528+
529+ override fun update (button : Chip , moduleHolder : ModuleHolder ) {
530+ val icon: Int = R .drawable.baseline_cloud_download_24
531+ button.chipIcon = button.context.getDrawable(icon)
532+ button.setText(R .string.online)
533+ }
385534 };
386535
387536 @DrawableRes
0 commit comments