open backup files on view action

This commit is contained in:
Daniel Gultsch 2019-07-17 11:32:56 +02:00
parent 603e1b35a5
commit cb1feab350
3 changed files with 47 additions and 20 deletions

View file

@ -23,7 +23,20 @@
<activity <activity
android:name=".ui.ImportBackupActivity" android:name=".ui.ImportBackupActivity"
android:label="@string/restore_backup" android:label="@string/restore_backup"
android:launchMode="singleTask" /> android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/vnd.conversations.backup" />
<data android:scheme="content" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/vnd.conversations.backup" />
<data android:scheme="file" />
</intent-filter>
</activity>
</application> </application>
</manifest> </manifest>

View file

@ -28,7 +28,6 @@ import eu.siacs.conversations.databinding.ActivityImportBackupBinding;
import eu.siacs.conversations.databinding.DialogEnterPasswordBinding; import eu.siacs.conversations.databinding.DialogEnterPasswordBinding;
import eu.siacs.conversations.services.ImportBackupService; import eu.siacs.conversations.services.ImportBackupService;
import eu.siacs.conversations.ui.adapter.BackupFileAdapter; import eu.siacs.conversations.ui.adapter.BackupFileAdapter;
import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
import eu.siacs.conversations.utils.ThemeHelper; import eu.siacs.conversations.utils.ThemeHelper;
public class ImportBackupActivity extends ActionBarActivity implements ServiceConnection, ImportBackupService.OnBackupFilesLoaded, BackupFileAdapter.OnItemClickedListener, ImportBackupService.OnBackupProcessed { public class ImportBackupActivity extends ActionBarActivity implements ServiceConnection, ImportBackupService.OnBackupFilesLoaded, BackupFileAdapter.OnItemClickedListener, ImportBackupService.OnBackupProcessed {
@ -49,7 +48,7 @@ public class ImportBackupActivity extends ActionBarActivity implements ServiceCo
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_import_backup); binding = DataBindingUtil.setContentView(this, R.layout.activity_import_backup);
setSupportActionBar((Toolbar) binding.toolbar); setSupportActionBar((Toolbar) binding.toolbar);
configureActionBar(getSupportActionBar()); setLoadingState(savedInstanceState != null && savedInstanceState.getBoolean("loading_state", false));
this.backupFileAdapter = new BackupFileAdapter(); this.backupFileAdapter = new BackupFileAdapter();
this.binding.list.setAdapter(this.backupFileAdapter); this.binding.list.setAdapter(this.backupFileAdapter);
this.backupFileAdapter.setOnItemClickedListener(this); this.backupFileAdapter.setOnItemClickedListener(this);
@ -63,6 +62,12 @@ public class ImportBackupActivity extends ActionBarActivity implements ServiceCo
return true; return true;
} }
@Override
public void onSaveInstanceState(Bundle bundle) {
bundle.putBoolean("loading_state", this.mLoadingState);
super.onSaveInstanceState(bundle);
}
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
@ -72,6 +77,13 @@ public class ImportBackupActivity extends ActionBarActivity implements ServiceCo
} else { } else {
bindService(new Intent(this, ImportBackupService.class), this, Context.BIND_AUTO_CREATE); bindService(new Intent(this, ImportBackupService.class), this, Context.BIND_AUTO_CREATE);
} }
final Intent intent = getIntent();
if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction()) && !this.mLoadingState) {
Uri uri = intent.getData();
if (uri != null) {
openBackupFileFromUri(uri, true);
}
}
} }
@Override @Override
@ -104,26 +116,30 @@ public class ImportBackupActivity extends ActionBarActivity implements ServiceCo
@Override @Override
public void onClick(final ImportBackupService.BackupFile backupFile) { public void onClick(final ImportBackupService.BackupFile backupFile) {
showEnterPasswordDialog(backupFile); showEnterPasswordDialog(backupFile, false);
} }
private void openBackupFileFromUri(final Uri uri) { private void openBackupFileFromUri(final Uri uri, final boolean finishOnCancel) {
try { try {
final ImportBackupService.BackupFile backupFile = ImportBackupService.BackupFile.read(this, uri); final ImportBackupService.BackupFile backupFile = ImportBackupService.BackupFile.read(this, uri);
showEnterPasswordDialog(backupFile); showEnterPasswordDialog(backupFile, finishOnCancel);
} catch (IOException e) { } catch (IOException e) {
Snackbar.make(binding.coordinator, R.string.not_a_backup_file, Snackbar.LENGTH_LONG).show(); Snackbar.make(binding.coordinator, R.string.not_a_backup_file, Snackbar.LENGTH_LONG).show();
} }
} }
private void showEnterPasswordDialog(final ImportBackupService.BackupFile backupFile) { private void showEnterPasswordDialog(final ImportBackupService.BackupFile backupFile, final boolean finishOnCancel) {
final DialogEnterPasswordBinding enterPasswordBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.dialog_enter_password, null, false); final DialogEnterPasswordBinding enterPasswordBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.dialog_enter_password, null, false);
Log.d(Config.LOGTAG, "attempting to import " + backupFile.getUri()); Log.d(Config.LOGTAG, "attempting to import " + backupFile.getUri());
enterPasswordBinding.explain.setText(getString(R.string.enter_password_to_restore, backupFile.getHeader().getJid().toString())); enterPasswordBinding.explain.setText(getString(R.string.enter_password_to_restore, backupFile.getHeader().getJid().toString()));
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(enterPasswordBinding.getRoot()); builder.setView(enterPasswordBinding.getRoot());
builder.setTitle(R.string.enter_password); builder.setTitle(R.string.enter_password);
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, (dialog, which) -> {
if (finishOnCancel) {
finish();
}
});
builder.setPositiveButton(R.string.restore, (dialog, which) -> { builder.setPositiveButton(R.string.restore, (dialog, which) -> {
final String password = enterPasswordBinding.accountPassword.getEditableText().toString(); final String password = enterPasswordBinding.accountPassword.getEditableText().toString();
final Uri uri = backupFile.getUri(); final Uri uri = backupFile.getUri();
@ -156,7 +172,7 @@ public class ImportBackupActivity extends ActionBarActivity implements ServiceCo
public void onActivityResult(int requestCode, int resultCode, Intent intent) { public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
if (requestCode == 0xbac) { if (requestCode == 0xbac) {
openBackupFileFromUri(intent.getData()); openBackupFileFromUri(intent.getData(), false);
} }
} }
} }
@ -197,16 +213,15 @@ public class ImportBackupActivity extends ActionBarActivity implements ServiceCo
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { if (item.getItemId() == R.id.action_open_backup_file) {
case R.id.action_open_backup_file: Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*");
intent.setType("*/*"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false); }
} intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addCategory(Intent.CATEGORY_OPENABLE); startActivityForResult(Intent.createChooser(intent, getString(R.string.open_backup)), 0xbac);
startActivityForResult(Intent.createChooser(intent, getString(R.string.open_backup)), 0xbac); return true;
return true;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }

View file

@ -92,7 +92,6 @@
android:theme="@style/SplashTheme"> android:theme="@style/SplashTheme">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>