Skip to content

Commit

Permalink
app backup
Browse files Browse the repository at this point in the history
  • Loading branch information
WrBug committed Sep 12, 2024
1 parent 0ee2026 commit 8d88524
Show file tree
Hide file tree
Showing 30 changed files with 574 additions and 350 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ dependencies {
implementation 'gdut.bsx:share2:0.9.3'
kapt 'com.google.dagger:dagger-compiler:2.16'
implementation 'de.blox:graphview:0.5.0'
implementation "com.google.android.material:material:1.12.0"
implementation project(':basemoduleimport')
implementation project(":commonwidget")
implementation project(':ipc')
Expand Down
Binary file removed app/src/main/assets/zip.dex
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class DeveloperApplication : BaseApp() {
)
registerIpcServer()
BaseModule.init(this)
releaseAssetsFile()
registerLifecycle()
AppStatusRegister.init(this)
}
Expand Down Expand Up @@ -91,18 +90,4 @@ class DeveloperApplication : BaseApp() {

})
}

private fun releaseAssetsFile() {
doAsync {
val inputStream = BaseApp.instance.assets.open("zip.dex")
val file = File(BaseApp.instance.cacheDir, "zip.dex")
if (file.exists().not()) {
file.createNewFile()
}
val fileOutputStream = FileOutputStream(file)
fileOutputStream.write(inputStream.readBytes())
fileOutputStream.flush()
fileOutputStream.close()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.wrbug.developerhelper.model.entity

data class BackupAppInfo(
var backupApk: Boolean = false,
var backupData: Boolean = false,
var backupAndroidData: Boolean = false,
var versionName: String = "",
var versionCode: Int = 0,
var packageName: String = ""
)
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
package com.wrbug.developerhelper.ui.activity.hierachy

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.view.View
import android.view.ViewGroup
import androidx.annotation.Keep
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.wrbug.developerhelper.R
import com.wrbug.developerhelper.basecommon.uiThread
import com.wrbug.developerhelper.commonutil.entity.ApkInfo
import com.wrbug.developerhelper.commonutil.entity.TopActivityInfo
import com.wrbug.developerhelper.commonutil.shell.ShellManager
import com.wrbug.developerhelper.ui.decoration.SpaceItemDecoration
import com.wrbug.developerhelper.ui.widget.appdatainfoview.AppDataInfoView
import com.wrbug.developerhelper.ui.widget.appsettingview.AppSettingView
import com.wrbug.developerhelper.ui.widget.layoutinfoview.infopage.InfoAdapter
import com.wrbug.developerhelper.ui.widget.layoutinfoview.infopage.ItemInfo
import com.wrbug.developerhelper.util.EnforceUtils
import com.wrbug.developerhelper.commonutil.UiUtils
import com.wrbug.developerhelper.commonutil.addTo
import com.wrbug.developerhelper.commonutil.shell.Callback
import com.wrbug.developerhelper.ipc.processshare.manager.AppXposedProcessDataManager
import com.wrbug.developerhelper.ui.widget.layoutinfoview.infopage.LoadingItem
import com.wrbug.developerhelper.util.format
import com.wrbug.developerhelper.util.getString
import io.reactivex.rxjava3.disposables.CompositeDisposable
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import java.lang.StringBuilder
import java.util.ArrayList

@Keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ScrollView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentActivity
import com.wrbug.developerhelper.BuildConfig
import com.wrbug.developerhelper.R
import com.wrbug.developerhelper.basecommon.BaseActivity
Expand All @@ -19,6 +20,7 @@ import com.wrbug.developerhelper.commonutil.toUri
import com.wrbug.developerhelper.commonutil.zip
import com.wrbug.developerhelper.commonwidget.util.setOnRootCheckClickListener
import com.wrbug.developerhelper.commonwidget.util.visible
import com.wrbug.developerhelper.databinding.DialogBackupAppSelectBinding
import com.wrbug.developerhelper.databinding.ViewAppSettingBinding
import com.wrbug.developerhelper.mmkv.ConfigKv
import com.wrbug.developerhelper.mmkv.manager.MMKVManager
Expand Down Expand Up @@ -54,12 +56,13 @@ class AppSettingView : ScrollView {
}

private fun initListener() {
binding.backupApkBtn.setOnRootCheckClickListener {
doBackupApk()
}
binding.backupApkDataDirBtn.setOnRootCheckClickListener {
doBackupDataDir()
binding.backupAppBtn.setOnRootCheckClickListener {
showBackSelect()
// doBackupApk()
}
// binding.backupApkDataDirBtn.setOnRootCheckClickListener {
// doBackupDataDir()
// }
binding.restartAppBtn.setOnRootCheckClickListener {
doRestartApp()
}
Expand All @@ -74,6 +77,43 @@ class AppSettingView : ScrollView {
}
}

private fun showBackSelect() {
val selected = booleanArrayOf(false, false, false)
val binding = DialogBackupAppSelectBinding.inflate(LayoutInflater.from(context))
binding.cbApk.setOnCheckedChangeListener { _, isChecked ->
selected[0] = isChecked
}
binding.cbData.setOnCheckedChangeListener { _, isChecked ->
selected[1] = isChecked
}
binding.cbAndroidData.setOnCheckedChangeListener { _, isChecked ->
selected[2] = isChecked
}
AlertDialog.Builder(context)
.setTitle(R.string.backup_app_file)
.setView(binding.root)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(
R.string.ok
) { _, _ ->
doBackup(selected)
}.create().show()
}

private fun doBackup(selected: BooleanArray) {
val activity = context as? FragmentActivity ?: return
if (selected.find { it } == null) {
return
}
BackupAppDialog.show(
activity.supportFragmentManager,
apkInfo,
selected[0],
selected[1],
selected[2]
)
}

private fun doUninstallApp() {
apkInfo?.apply {
AppManagerUtils.uninstallApp(context, applicationInfo.packageName)
Expand Down Expand Up @@ -124,80 +164,6 @@ class AppSettingView : ScrollView {
}
}

private fun doBackupDataDir() {
if (checkRoot().not()) {
return
}
apkInfo?.apply {
val backupAppData = BackupUtils.backupAppData(applicationInfo.packageName)
if (backupAppData == null) {
showToast(context.getString(R.string.backup_failed))
return
}
if (context !is BaseActivity) {
showToast(context.getString(R.string.backup_success_msg))
return
}
showShareDataNotice(backupAppData)
}
}

private fun showShareDataNotice(backupAppData: File) {
showNotice(context.getString(R.string.backup_success_and_share_msg)) {
(context as BaseActivity).requestPermission(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
object : BaseActivity.PermissionCallback() {
override fun granted() {
val zipFile = File(
context.externalCacheDir, "${apkInfo?.getAppName() ?: ""}-data.zip"
)
backupAppData.zip(zipFile)
val uri = zipFile.toUri(context)
if (uri == null) {
showToast(context.getString(R.string.share_failed))
return
}
activityFinish()
Share2.Builder(context as Activity).setContentType(ShareContentType.FILE)
.setShareFileUri(uri).setOnActivityResult(10).build().shareBySystem()
}

})

}
}

private fun doBackupApk() {
if (checkRoot().not()) {
return
}
context.requestStoragePermission {
apkInfo?.apply {
val uri = BackupUtils.backupApk(
applicationInfo.packageName,
applicationInfo.publicSourceDir,
"${getAppName()}_${packageInfo.versionName}.apk"
)
if (uri == null) {
showToast(context.getString(R.string.backup_failed))
return@apply
}
if (context !is BaseActivity) {
showToast(context.getString(R.string.backup_success_msg))
return@apply
}
showShareApkDialog(uri)
}
}
}

private fun showShareApkDialog(uri: Uri) {
showNotice(context.getString(R.string.backup_success_and_share_msg)) {
activityFinish()
Share2.Builder(context as Activity).setContentType(ShareContentType.FILE)
.setShareFileUri(uri).setOnActivityResult(10).build().shareBySystem()
}
}

private fun checkRoot(): Boolean {
if (configKv.isOpenRoot().not()) {
showToast(context.getString(R.string.please_open_root))
Expand Down
Loading

0 comments on commit 8d88524

Please sign in to comment.