1010package com.fox2code.mmm
1111
1212import android.content.Intent
13- import android.net.Uri
1413import android.view.View
1514import android.widget.Toast
1615import androidx.annotation.AttrRes
@@ -20,8 +19,9 @@ import com.fox2code.mmm.installer.InstallerInitializer
2019import com.fox2code.mmm.module.ModuleViewListBuilder
2120import com.fox2code.mmm.repo.RepoManager
2221import com.fox2code.mmm.utils.IntentHelper
22+ import com.fox2code.mmm.utils.IntentHelper.Companion.OnFileReceivedCallback
23+ import com.fox2code.mmm.utils.io.Files
2324import com.fox2code.mmm.utils.io.Files.Companion.patchModuleSimple
24- import com.fox2code.mmm.utils.io.Files.Companion.read
2525import com.fox2code.mmm.utils.io.net.Http
2626import com.google.android.material.dialog.MaterialAlertDialogBuilder
2727import timber.log.Timber
@@ -205,49 +205,68 @@ enum class NotificationType(
205205 val module = File (
206206 compatActivity.cacheDir, " installer" + File .separator + " module.zip"
207207 )
208- IntentHelper .openFileTo(compatActivity, module) { d: File , u: Uri , s: Int ->
209- val companion = NotificationType .Companion
210- if (s == IntentHelper .RESPONSE_FILE ) {
211- try {
212- if (companion .needPatch(d)) {
213- patchModuleSimple(
214- read(d), FileOutputStream (d)
215- )
216- }
217- if (companion .needPatch(d)) {
218- if (d.exists() && ! d.delete()) Timber .w(" Failed to delete non module zip" )
208+ IntentHelper .openFileTo(compatActivity, module, object :
209+ OnFileReceivedCallback {
210+
211+ override fun onReceived (
212+ target : File ? ,
213+ uri : android.net.Uri ? ,
214+ response : Int
215+ ) {
216+ val companion = NotificationType .Companion
217+ if (response == IntentHelper .RESPONSE_FILE ) {
218+ try {
219+ if (companion .needPatch(target)) {
220+ patchModuleSimple(
221+ Files .read(target),
222+ FileOutputStream (target)
223+ )
224+ }
225+ if (companion .needPatch(target)) {
226+ if (target?.exists() == true && ! target.delete()) Timber .w(" Failed to delete non module zip" )
227+ Toast .makeText(
228+ compatActivity,
229+ R .string.invalid_format,
230+ Toast .LENGTH_SHORT
231+ ).show()
232+ } else {
233+ IntentHelper .openInstaller(
234+ compatActivity,
235+ target?.absolutePath,
236+ compatActivity.getString(
237+ R .string.local_install_title
238+ ),
239+ null ,
240+ null ,
241+ false ,
242+ BuildConfig .DEBUG && // Use debug mode if no root
243+ InstallerInitializer .peekMagiskPath() == null
244+ )
245+ }
246+ } catch (ignored: IOException ) {
247+ if (target?.exists() == true && ! target.delete()) Timber .w(" Failed to delete invalid module" )
219248 Toast .makeText(
220- compatActivity, R .string.invalid_format, Toast .LENGTH_SHORT
221- ).show()
222- } else {
223- IntentHelper .openInstaller(
224249 compatActivity,
225- d.absolutePath,
226- compatActivity.getString(
227- R .string.local_install_title
228- ),
229- null ,
230- null ,
231- false ,
232- BuildConfig .DEBUG && // Use debug mode if no root
233- InstallerInitializer .peekMagiskPath() == null
234- )
250+ R .string.invalid_format,
251+ Toast .LENGTH_SHORT
252+ ).show()
235253 }
236- } catch (ignored: IOException ) {
237- if (d.exists() && ! d.delete()) Timber .w(" Failed to delete invalid module" )
238- Toast .makeText(
239- compatActivity, R .string.invalid_format, Toast .LENGTH_SHORT
240- ).show()
254+ } else if (response == IntentHelper .RESPONSE_URL ) {
255+ IntentHelper .openInstaller(
256+ compatActivity,
257+ uri.toString(),
258+ compatActivity.getString(
259+ R .string.remote_install_title
260+ ),
261+ null ,
262+ null ,
263+ false ,
264+ BuildConfig .DEBUG && // Use debug mode if no root
265+ InstallerInitializer .peekMagiskPath() == null
266+ )
241267 }
242- } else if (s == IntentHelper .RESPONSE_URL ) {
243- IntentHelper .openInstaller(
244- compatActivity, u.toString(), compatActivity.getString(
245- R .string.remote_install_title
246- ), null , null , false , BuildConfig .DEBUG && // Use debug mode if no root
247- InstallerInitializer .peekMagiskPath() == null
248- )
249268 }
250- }
269+ })
251270 },
252271 false
253272 ) {
@@ -324,4 +343,5 @@ enum class NotificationType(
324343 return false
325344 }
326345 }
346+
327347}
0 commit comments