rework local install 1/?

Signed-off-by: androidacy-user <opensource@androidacy.com>
pull/89/head
androidacy-user 2 years ago
parent 569e14e9f0
commit 3a46bea884

@ -878,9 +878,6 @@ class MainActivity : AppCompatActivity(), OnRefreshListener, OverScrollHelper {
} }
companion object { companion object {
fun getAppCompatActivity(activity: AppCompatActivity): AppCompatActivity {
return activity
}
fun getAppCompatActivity(context: Context): AppCompatActivity { fun getAppCompatActivity(context: Context): AppCompatActivity {
return context as AppCompatActivity return context as AppCompatActivity

@ -10,7 +10,6 @@
package com.fox2code.mmm package com.fox2code.mmm
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.view.View import android.view.View
import android.widget.Toast import android.widget.Toast
import androidx.annotation.AttrRes import androidx.annotation.AttrRes
@ -20,8 +19,9 @@ import com.fox2code.mmm.installer.InstallerInitializer
import com.fox2code.mmm.module.ModuleViewListBuilder import com.fox2code.mmm.module.ModuleViewListBuilder
import com.fox2code.mmm.repo.RepoManager import com.fox2code.mmm.repo.RepoManager
import com.fox2code.mmm.utils.IntentHelper 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.patchModuleSimple
import com.fox2code.mmm.utils.io.Files.Companion.read
import com.fox2code.mmm.utils.io.net.Http import com.fox2code.mmm.utils.io.net.Http
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import timber.log.Timber import timber.log.Timber
@ -205,49 +205,68 @@ enum class NotificationType(
val module = File( val module = File(
compatActivity.cacheDir, "installer" + File.separator + "module.zip" compatActivity.cacheDir, "installer" + File.separator + "module.zip"
) )
IntentHelper.openFileTo(compatActivity, module) { d: File, u: Uri, s: Int -> IntentHelper.openFileTo(compatActivity, module, object :
val companion = NotificationType.Companion OnFileReceivedCallback {
if (s == IntentHelper.RESPONSE_FILE) {
try { override fun onReceived(
if (companion.needPatch(d)) { target: File?,
patchModuleSimple( uri: android.net.Uri?,
read(d), FileOutputStream(d) response: Int
) ) {
} val companion = NotificationType.Companion
if (companion.needPatch(d)) { if (response == IntentHelper.RESPONSE_FILE) {
if (d.exists() && !d.delete()) Timber.w("Failed to delete non module zip") 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( Toast.makeText(
compatActivity, R.string.invalid_format, Toast.LENGTH_SHORT
).show()
} else {
IntentHelper.openInstaller(
compatActivity, compatActivity,
d.absolutePath, R.string.invalid_format,
compatActivity.getString( Toast.LENGTH_SHORT
R.string.local_install_title ).show()
),
null,
null,
false,
BuildConfig.DEBUG && // Use debug mode if no root
InstallerInitializer.peekMagiskPath() == null
)
} }
} catch (ignored: IOException) { } else if (response == IntentHelper.RESPONSE_URL) {
if (d.exists() && !d.delete()) Timber.w("Failed to delete invalid module") IntentHelper.openInstaller(
Toast.makeText( compatActivity,
compatActivity, R.string.invalid_format, Toast.LENGTH_SHORT uri.toString(),
).show() 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 false
) { ) {
@ -324,4 +343,5 @@ enum class NotificationType(
return false return false
} }
} }
} }

@ -23,7 +23,6 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat import androidx.core.app.ActivityOptionsCompat
import com.fox2code.mmm.BuildConfig import com.fox2code.mmm.BuildConfig
import com.fox2code.mmm.Constants import com.fox2code.mmm.Constants
import com.fox2code.mmm.MainActivity
import com.fox2code.mmm.MainApplication import com.fox2code.mmm.MainApplication
import com.fox2code.mmm.R import com.fox2code.mmm.R
import com.fox2code.mmm.XHooks.Companion.getConfigIntent import com.fox2code.mmm.XHooks.Companion.getConfigIntent
@ -368,7 +367,7 @@ enum class IntentHelper {;
if ((destination == null) || (destination.parentFile.also { if ((destination == null) || (destination.parentFile.also {
destinationFolder = it destinationFolder = it
} == null) || (!destinationFolder?.mkdirs()!! && !destinationFolder!!.isDirectory)) { } == null) || (!destinationFolder?.mkdirs()!! && !destinationFolder!!.isDirectory)) {
Timber.w("dest null.for open") Timber.w("dest null for open")
callback.onReceived(destination, null, RESPONSE_ERROR) callback.onReceived(destination, null, RESPONSE_ERROR)
return return
} }
@ -428,26 +427,5 @@ enum class IntentHelper {;
} }
getContent.launch("application/zip") 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()
}
}
}
})
}
} }
} }
Loading…
Cancel
Save