From ff1abc10811dbb25bc6f8815587a769215cf7406 Mon Sep 17 00:00:00 2001 From: nikita-yfh Date: Sat, 26 Mar 2022 01:41:00 +0300 Subject: [PATCH] Add backup support --- app/build.gradle | 4 +- .../main/java/ru/nikita/adb/ADBActivity.java | 51 +++++++++++++++++-- app/src/main/java/ru/nikita/adb/ADBTask.java | 24 +++++++++ app/src/main/res/layout/adb_activity.xml | 16 ++++++ app/src/main/res/layout/backup.xml | 37 ++++++++++++++ app/src/main/res/values-ru/strings.xml | 7 +++ app/src/main/res/values/strings.xml | 7 +++ 7 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 app/src/main/res/layout/backup.xml diff --git a/app/build.gradle b/app/build.gradle index 03674b7..d2fa563 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,8 +7,8 @@ android { applicationId "ru.nikita.adb" minSdkVersion 12 targetSdkVersion 20 - versionCode 3 - versionName "1.3" + versionCode 4 + versionName "1.4" } buildTypes { release { diff --git a/app/src/main/java/ru/nikita/adb/ADBActivity.java b/app/src/main/java/ru/nikita/adb/ADBActivity.java index 8bad7c5..2a7ecb2 100644 --- a/app/src/main/java/ru/nikita/adb/ADBActivity.java +++ b/app/src/main/java/ru/nikita/adb/ADBActivity.java @@ -10,6 +10,7 @@ import android.app.Activity; import android.app.AlertDialog; import android.widget.EditText; +import android.widget.Switch; import android.text.InputType; import android.content.DialogInterface; import android.content.Intent; @@ -24,6 +25,7 @@ public class ADBActivity extends DevicesActivity { private static final int APP_INSTALL_FILE=1; + private static final int RESTORE=2; @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.adb_activity); @@ -151,10 +153,13 @@ public void appManager(View view){ @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK && data != null){ - if(requestCode == APP_INSTALL_FILE){ - String filePath = data.getData().getPath(); + String filePath = data.getData().getPath(); + if(requestCode == APP_INSTALL_FILE) new ADBTask(text,binary).installAppFromFile(device,filePath); - } + else if(requestCode == RESTORE) + new ADBTask(text,binary).restore(device,filePath); + + } super.onActivityResult(requestCode, resultCode, data); } @@ -184,8 +189,46 @@ public void onClick(DialogInterface dialog, int which) { builder.show(); } + public void backup(View view){ + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.backup); + + View layout = getLayoutInflater().inflate(R.layout.backup, null); + builder.setView(layout); + + final Switch backupAPK = (Switch)layout.findViewById(R.id.backup_apk); + final Switch backupUserApps = (Switch)layout.findViewById(R.id.backup_user_apps); + final Switch backupSystemApps = (Switch)layout.findViewById(R.id.backup_system_apps); + final Switch backupData = (Switch)layout.findViewById(R.id.backup_data); + final Switch backupCache = (Switch)layout.findViewById(R.id.backup_cache); + + builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + new ADBTask(text,binary).backup(device, + backupAPK.isChecked(), + backupUserApps.isChecked(), + backupSystemApps.isChecked(), + backupData.isChecked(), + backupCache.isChecked()); + } + }); + builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + + builder.show(); + } + public void restore(View view){ + Intent intent = new Intent(this, FileManagerActivity.class); + startActivityForResult(intent, RESTORE); + } + public void executeCommand(View view){ EditText command = (EditText)findViewById(R.id.command); - new ADBTask(text,binary).execute(device, command.getText().toString()); + new ADBTask(text,binary).shell(device, command.getText().toString()); } } diff --git a/app/src/main/java/ru/nikita/adb/ADBTask.java b/app/src/main/java/ru/nikita/adb/ADBTask.java index 233dd86..a2d164c 100644 --- a/app/src/main/java/ru/nikita/adb/ADBTask.java +++ b/app/src/main/java/ru/nikita/adb/ADBTask.java @@ -2,7 +2,10 @@ import ru.nikita.adb.Task; import ru.nikita.adb.Device; +import java.io.File; import java.lang.String; +import java.util.Calendar; +import java.text.SimpleDateFormat; import android.widget.TextView; class ADBTask extends Task{ @@ -18,6 +21,27 @@ public void reboot(Device device, String arg){ public void installAppFromFile(final Device device, String fileName){ execute(device,"install '"+fileName+"'"); } + public void backup(final Device device, boolean apk, boolean userApps, + boolean systemApps, boolean data, boolean cache) { + new File("/sdcard/ADB").mkdir(); + String args = new String(); + if(apk) + args += "-apk "; + if(data) + args += "-shsred "; + if(cache) + args += "-obb "; + if(userApps) + args += "-all "; + if(!systemApps) + args += "-nosystem "; + String fileName = "/sdcard/ADB/backup_"+new SimpleDateFormat("ddMMyy_hhmmss").format(Calendar.getInstance().getTime())+".ab"; + + execute(device,"backup -f '"+fileName+"' "+args); + } + public void restore(final Device device, String fileName){ + execute(device,"restore '"+fileName+"'"); + } public void connectDevice(String ip, String port){ execute(String.format("connect %s:%s", ip, port)); } diff --git a/app/src/main/res/layout/adb_activity.xml b/app/src/main/res/layout/adb_activity.xml index c682987..e7253df 100644 --- a/app/src/main/res/layout/adb_activity.xml +++ b/app/src/main/res/layout/adb_activity.xml @@ -127,6 +127,22 @@ android:layout_height="wrap_content" android:onClick="appManager" android:text="@string/app_manager" /> + +