|
| 1 | +package com.stevesoltys.backup.activity.backup; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.backup.BackupProgress; |
| 5 | +import android.widget.PopupWindow; |
| 6 | +import android.widget.ProgressBar; |
| 7 | +import android.widget.TextView; |
| 8 | +import android.widget.Toast; |
| 9 | + |
| 10 | +import com.stevesoltys.backup.R; |
| 11 | +import com.stevesoltys.backup.session.backup.BackupResult; |
| 12 | +import com.stevesoltys.backup.session.backup.BackupSession; |
| 13 | +import com.stevesoltys.backup.session.backup.BackupSessionObserver; |
| 14 | +import com.stevesoltys.backup.transport.ConfigurableBackupTransport; |
| 15 | +import com.stevesoltys.backup.transport.ConfigurableBackupTransportService; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Steve Soltys |
| 19 | + */ |
| 20 | +class BackupObserver implements BackupSessionObserver { |
| 21 | + |
| 22 | + private final Activity context; |
| 23 | + |
| 24 | + private final PopupWindow popupWindow; |
| 25 | + |
| 26 | + BackupObserver(Activity context, PopupWindow popupWindow) { |
| 27 | + this.context = context; |
| 28 | + this.popupWindow = popupWindow; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void backupPackageStarted(BackupSession backupSession, String packageName, BackupProgress backupProgress) { |
| 33 | + context.runOnUiThread(() -> { |
| 34 | + |
| 35 | + TextView textView = popupWindow.getContentView().findViewById(R.id.popup_text_view); |
| 36 | + |
| 37 | + if (textView != null) { |
| 38 | + textView.setText(packageName); |
| 39 | + } |
| 40 | + |
| 41 | + ProgressBar progressBar = popupWindow.getContentView().findViewById(R.id.popup_progress_bar); |
| 42 | + |
| 43 | + if (progressBar != null) { |
| 44 | + progressBar.setMax((int) backupProgress.bytesExpected); |
| 45 | + progressBar.setProgress((int) backupProgress.bytesTransferred); |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void backupPackageCompleted(BackupSession backupSession, String packageName, BackupResult result) { |
| 52 | + context.runOnUiThread(() -> { |
| 53 | + |
| 54 | + TextView textView = popupWindow.getContentView().findViewById(R.id.popup_text_view); |
| 55 | + |
| 56 | + if (textView != null) { |
| 57 | + textView.setText(packageName); |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void backupSessionCompleted(BackupSession backupSession, BackupResult backupResult) { |
| 64 | + ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); |
| 65 | + |
| 66 | + if (backupTransport.getRestoreComponent() == null || backupTransport.getBackupComponent() == null) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + backupTransport.setBackupComponent(null); |
| 71 | + backupTransport.setRestoreComponent(null); |
| 72 | + |
| 73 | + context.runOnUiThread(() -> { |
| 74 | + if (backupResult == BackupResult.SUCCESS) { |
| 75 | + Toast.makeText(context, R.string.backup_success, Toast.LENGTH_LONG).show(); |
| 76 | + |
| 77 | + } else if (backupResult == BackupResult.CANCELLED) { |
| 78 | + Toast.makeText(context, R.string.backup_cancelled, Toast.LENGTH_LONG).show(); |
| 79 | + |
| 80 | + } else { |
| 81 | + Toast.makeText(context, R.string.backup_failure, Toast.LENGTH_LONG).show(); |
| 82 | + } |
| 83 | + |
| 84 | + popupWindow.dismiss(); |
| 85 | + context.finish(); |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
0 commit comments