Skip to content

Commit 3a46bea

Browse files
rework local install 1/?
Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent 569e14e commit 3a46bea

3 files changed

Lines changed: 61 additions & 66 deletions

File tree

app/src/main/kotlin/com/fox2code/mmm/MainActivity.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,6 @@ class MainActivity : AppCompatActivity(), OnRefreshListener, OverScrollHelper {
878878
}
879879

880880
companion object {
881-
fun getAppCompatActivity(activity: AppCompatActivity): AppCompatActivity {
882-
return activity
883-
}
884881

885882
fun getAppCompatActivity(context: Context): AppCompatActivity {
886883
return context as AppCompatActivity

app/src/main/kotlin/com/fox2code/mmm/NotificationType.kt

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package com.fox2code.mmm
1111

1212
import android.content.Intent
13-
import android.net.Uri
1413
import android.view.View
1514
import android.widget.Toast
1615
import androidx.annotation.AttrRes
@@ -20,8 +19,9 @@ import com.fox2code.mmm.installer.InstallerInitializer
2019
import com.fox2code.mmm.module.ModuleViewListBuilder
2120
import com.fox2code.mmm.repo.RepoManager
2221
import com.fox2code.mmm.utils.IntentHelper
22+
import com.fox2code.mmm.utils.IntentHelper.Companion.OnFileReceivedCallback
23+
import com.fox2code.mmm.utils.io.Files
2324
import com.fox2code.mmm.utils.io.Files.Companion.patchModuleSimple
24-
import com.fox2code.mmm.utils.io.Files.Companion.read
2525
import com.fox2code.mmm.utils.io.net.Http
2626
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2727
import 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
}

app/src/main/kotlin/com/fox2code/mmm/utils/IntentHelper.kt

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import androidx.appcompat.app.AppCompatActivity
2323
import androidx.core.app.ActivityOptionsCompat
2424
import com.fox2code.mmm.BuildConfig
2525
import com.fox2code.mmm.Constants
26-
import com.fox2code.mmm.MainActivity
2726
import com.fox2code.mmm.MainApplication
2827
import com.fox2code.mmm.R
2928
import com.fox2code.mmm.XHooks.Companion.getConfigIntent
@@ -368,7 +367,7 @@ enum class IntentHelper {;
368367
if ((destination == null) || (destination.parentFile.also {
369368
destinationFolder = it
370369
} == null) || (!destinationFolder?.mkdirs()!! && !destinationFolder!!.isDirectory)) {
371-
Timber.w("dest null.for open")
370+
Timber.w("dest null for open")
372371
callback.onReceived(destination, null, RESPONSE_ERROR)
373372
return
374373
}
@@ -428,26 +427,5 @@ enum class IntentHelper {;
428427
}
429428
getContent.launch("application/zip")
430429
}
431-
432-
fun openFileTo(compatActivity: AppCompatActivity, module: File, function: (File, Uri, Int) -> Unit) {
433-
openFileTo(compatActivity, module, object : OnFileReceivedCallback {
434-
override fun onReceived(target: File?, uri: Uri?, response: Int) {
435-
if (response == RESPONSE_ERROR) {
436-
MainActivity.getAppCompatActivity(compatActivity).runOnUiThread {
437-
Toast.makeText(
438-
compatActivity, R.string.no_file_provided, Toast.LENGTH_SHORT
439-
).show()
440-
}
441-
} else {
442-
try {
443-
function(target!!, uri!!, response)
444-
} catch (e: Exception) {
445-
Timber.e(e)
446-
compatActivity.finish()
447-
}
448-
}
449-
}
450-
})
451-
}
452430
}
453431
}

0 commit comments

Comments
 (0)