diff --git a/app/src/main/kotlin/com/fox2code/mmm/MainActivity.kt b/app/src/main/kotlin/com/fox2code/mmm/MainActivity.kt index 222ab39..db4f831 100644 --- a/app/src/main/kotlin/com/fox2code/mmm/MainActivity.kt +++ b/app/src/main/kotlin/com/fox2code/mmm/MainActivity.kt @@ -878,9 +878,6 @@ class MainActivity : AppCompatActivity(), OnRefreshListener, OverScrollHelper { } companion object { - fun getAppCompatActivity(activity: AppCompatActivity): AppCompatActivity { - return activity - } fun getAppCompatActivity(context: Context): AppCompatActivity { return context as AppCompatActivity diff --git a/app/src/main/kotlin/com/fox2code/mmm/NotificationType.kt b/app/src/main/kotlin/com/fox2code/mmm/NotificationType.kt index 8c293ce..f2922c0 100644 --- a/app/src/main/kotlin/com/fox2code/mmm/NotificationType.kt +++ b/app/src/main/kotlin/com/fox2code/mmm/NotificationType.kt @@ -10,7 +10,6 @@ package com.fox2code.mmm import android.content.Intent -import android.net.Uri import android.view.View import android.widget.Toast import androidx.annotation.AttrRes @@ -20,8 +19,9 @@ import com.fox2code.mmm.installer.InstallerInitializer import com.fox2code.mmm.module.ModuleViewListBuilder import com.fox2code.mmm.repo.RepoManager import com.fox2code.mmm.utils.IntentHelper +import com.fox2code.mmm.utils.IntentHelper.Companion.OnFileReceivedCallback +import com.fox2code.mmm.utils.io.Files import com.fox2code.mmm.utils.io.Files.Companion.patchModuleSimple -import com.fox2code.mmm.utils.io.Files.Companion.read import com.fox2code.mmm.utils.io.net.Http import com.google.android.material.dialog.MaterialAlertDialogBuilder import timber.log.Timber @@ -205,49 +205,68 @@ enum class NotificationType( val module = File( compatActivity.cacheDir, "installer" + File.separator + "module.zip" ) - IntentHelper.openFileTo(compatActivity, module) { d: File, u: Uri, s: Int -> - val companion = NotificationType.Companion - if (s == IntentHelper.RESPONSE_FILE) { - try { - if (companion.needPatch(d)) { - patchModuleSimple( - read(d), FileOutputStream(d) - ) - } - if (companion.needPatch(d)) { - if (d.exists() && !d.delete()) Timber.w("Failed to delete non module zip") + IntentHelper.openFileTo(compatActivity, module, object : + OnFileReceivedCallback { + + override fun onReceived( + target: File?, + uri: android.net.Uri?, + response: Int + ) { + val companion = NotificationType.Companion + if (response == IntentHelper.RESPONSE_FILE) { + try { + if (companion.needPatch(target)) { + patchModuleSimple( + Files.read(target), + FileOutputStream(target) + ) + } + if (companion.needPatch(target)) { + if (target?.exists() == true && !target.delete()) Timber.w("Failed to delete non module zip") + Toast.makeText( + compatActivity, + R.string.invalid_format, + Toast.LENGTH_SHORT + ).show() + } else { + IntentHelper.openInstaller( + compatActivity, + target?.absolutePath, + compatActivity.getString( + R.string.local_install_title + ), + null, + null, + false, + BuildConfig.DEBUG && // Use debug mode if no root + InstallerInitializer.peekMagiskPath() == null + ) + } + } catch (ignored: IOException) { + if (target?.exists() == true && !target.delete()) Timber.w("Failed to delete invalid module") Toast.makeText( - compatActivity, R.string.invalid_format, Toast.LENGTH_SHORT - ).show() - } else { - IntentHelper.openInstaller( compatActivity, - d.absolutePath, - compatActivity.getString( - R.string.local_install_title - ), - null, - null, - false, - BuildConfig.DEBUG && // Use debug mode if no root - InstallerInitializer.peekMagiskPath() == null - ) + R.string.invalid_format, + Toast.LENGTH_SHORT + ).show() } - } catch (ignored: IOException) { - if (d.exists() && !d.delete()) Timber.w("Failed to delete invalid module") - Toast.makeText( - compatActivity, R.string.invalid_format, Toast.LENGTH_SHORT - ).show() + } else if (response == IntentHelper.RESPONSE_URL) { + IntentHelper.openInstaller( + compatActivity, + uri.toString(), + compatActivity.getString( + R.string.remote_install_title + ), + null, + null, + false, + BuildConfig.DEBUG && // Use debug mode if no root + InstallerInitializer.peekMagiskPath() == null + ) } - } else if (s == IntentHelper.RESPONSE_URL) { - IntentHelper.openInstaller( - compatActivity, u.toString(), compatActivity.getString( - R.string.remote_install_title - ), null, null, false, BuildConfig.DEBUG && // Use debug mode if no root - InstallerInitializer.peekMagiskPath() == null - ) } - } + }) }, false ) { @@ -324,4 +343,5 @@ enum class NotificationType( return false } } + } diff --git a/app/src/main/kotlin/com/fox2code/mmm/utils/IntentHelper.kt b/app/src/main/kotlin/com/fox2code/mmm/utils/IntentHelper.kt index a804236..b5f1b09 100644 --- a/app/src/main/kotlin/com/fox2code/mmm/utils/IntentHelper.kt +++ b/app/src/main/kotlin/com/fox2code/mmm/utils/IntentHelper.kt @@ -23,7 +23,6 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityOptionsCompat import com.fox2code.mmm.BuildConfig import com.fox2code.mmm.Constants -import com.fox2code.mmm.MainActivity import com.fox2code.mmm.MainApplication import com.fox2code.mmm.R import com.fox2code.mmm.XHooks.Companion.getConfigIntent @@ -368,7 +367,7 @@ enum class IntentHelper {; if ((destination == null) || (destination.parentFile.also { destinationFolder = it } == null) || (!destinationFolder?.mkdirs()!! && !destinationFolder!!.isDirectory)) { - Timber.w("dest null.for open") + Timber.w("dest null for open") callback.onReceived(destination, null, RESPONSE_ERROR) return } @@ -428,26 +427,5 @@ enum class IntentHelper {; } getContent.launch("application/zip") } - - fun openFileTo(compatActivity: AppCompatActivity, module: File, function: (File, Uri, Int) -> Unit) { - openFileTo(compatActivity, module, object : OnFileReceivedCallback { - override fun onReceived(target: File?, uri: Uri?, response: Int) { - if (response == RESPONSE_ERROR) { - MainActivity.getAppCompatActivity(compatActivity).runOnUiThread { - Toast.makeText( - compatActivity, R.string.no_file_provided, Toast.LENGTH_SHORT - ).show() - } - } else { - try { - function(target!!, uri!!, response) - } catch (e: Exception) { - Timber.e(e) - compatActivity.finish() - } - } - } - }) - } } } \ No newline at end of file