2013-06-25 04:02:30 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
2017-03-15 23:53:15 +00:00
|
|
|
import android.app.Dialog;
|
2013-06-25 04:02:30 +00:00
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
2014-06-29 03:40:57 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
2015-05-20 21:36:30 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2013-06-25 04:02:30 +00:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2013-06-25 04:02:30 +00:00
|
|
|
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
2017-03-15 23:53:15 +00:00
|
|
|
import org.thoughtcrime.securesms.database.PlaintextBackupExporter;
|
2013-06-25 04:02:30 +00:00
|
|
|
import org.thoughtcrime.securesms.database.PlaintextBackupImporter;
|
|
|
|
import org.thoughtcrime.securesms.service.ApplicationMigrationService;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
2017-03-15 23:53:15 +00:00
|
|
|
public class ImportExportFragment extends Fragment {
|
2013-06-25 04:02:30 +00:00
|
|
|
|
|
|
|
private static final int SUCCESS = 0;
|
|
|
|
private static final int NO_SD_CARD = 1;
|
|
|
|
private static final int ERROR_IO = 2;
|
|
|
|
|
2017-03-15 23:53:15 +00:00
|
|
|
private MasterSecret masterSecret;
|
2013-06-25 04:02:30 +00:00
|
|
|
private ProgressDialog progressDialog;
|
|
|
|
|
2017-03-15 23:53:15 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle bundle) {
|
|
|
|
super.onCreate(bundle);
|
|
|
|
this.masterSecret = getArguments().getParcelable("master_secret");
|
2013-06-25 04:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
|
2017-03-15 23:53:15 +00:00
|
|
|
View layout = inflater.inflate(R.layout.import_export_fragment, container, false);
|
2013-06-25 04:02:30 +00:00
|
|
|
View importSmsView = layout.findViewById(R.id.import_sms );
|
|
|
|
View importPlaintextView = layout.findViewById(R.id.import_plaintext_backup);
|
2017-03-15 23:53:15 +00:00
|
|
|
View exportPlaintextView = layout.findViewById(R.id.export_plaintext_backup);
|
2013-06-25 04:02:30 +00:00
|
|
|
|
|
|
|
importSmsView.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
handleImportSms();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-03-15 23:53:15 +00:00
|
|
|
importPlaintextView.setOnClickListener(new View.OnClickListener() {
|
2013-06-25 04:02:30 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-03-15 23:53:15 +00:00
|
|
|
handleImportPlaintextBackup();
|
2013-06-25 04:02:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-03-15 23:53:15 +00:00
|
|
|
exportPlaintextView.setOnClickListener(new View.OnClickListener() {
|
2013-06-25 04:02:30 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-03-15 23:53:15 +00:00
|
|
|
handleExportPlaintextBackup();
|
2013-06-25 04:02:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
|
|
|
|
if (progressDialog != null && progressDialog.isShowing()) {
|
|
|
|
progressDialog.dismiss();
|
|
|
|
progressDialog = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleImportSms() {
|
2015-05-20 21:36:30 +00:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
2015-03-24 12:44:22 +00:00
|
|
|
builder.setIconAttribute(R.attr.dialog_info_icon);
|
2013-06-25 04:02:30 +00:00
|
|
|
builder.setTitle(getActivity().getString(R.string.ImportFragment_import_system_sms_database));
|
|
|
|
builder.setMessage(getActivity().getString(R.string.ImportFragment_this_will_import_messages_from_the_system));
|
|
|
|
builder.setPositiveButton(getActivity().getString(R.string.ImportFragment_import), new AlertDialog.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
Intent intent = new Intent(getActivity(), ApplicationMigrationService.class);
|
|
|
|
intent.setAction(ApplicationMigrationService.MIGRATE_DATABASE);
|
|
|
|
intent.putExtra("master_secret", masterSecret);
|
|
|
|
getActivity().startService(intent);
|
|
|
|
|
|
|
|
Intent nextIntent = new Intent(getActivity(), ConversationListActivity.class);
|
|
|
|
|
|
|
|
Intent activityIntent = new Intent(getActivity(), DatabaseMigrationActivity.class);
|
|
|
|
activityIntent.putExtra("next_intent", nextIntent);
|
|
|
|
getActivity().startActivity(activityIntent);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.setNegativeButton(getActivity().getString(R.string.ImportFragment_cancel), null);
|
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
2017-03-15 23:53:15 +00:00
|
|
|
private void handleImportPlaintextBackup() {
|
2015-05-20 21:36:30 +00:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
2015-03-24 12:44:22 +00:00
|
|
|
builder.setIconAttribute(R.attr.dialog_alert_icon);
|
2017-03-15 23:53:15 +00:00
|
|
|
builder.setTitle(getActivity().getString(R.string.ImportFragment_import_plaintext_backup));
|
|
|
|
builder.setMessage(getActivity().getString(R.string.ImportFragment_this_will_import_messages_from_a_plaintext_backup));
|
|
|
|
builder.setPositiveButton(getActivity().getString(R.string.ImportFragment_import), new AlertDialog.OnClickListener() {
|
2013-06-25 04:02:30 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2017-10-23 20:03:32 +00:00
|
|
|
new ImportPlaintextBackupTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2013-06-25 04:02:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.setNegativeButton(getActivity().getString(R.string.ImportFragment_cancel), null);
|
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
2017-03-15 23:53:15 +00:00
|
|
|
private void handleExportPlaintextBackup() {
|
2015-05-20 21:36:30 +00:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
2015-03-24 12:44:22 +00:00
|
|
|
builder.setIconAttribute(R.attr.dialog_alert_icon);
|
2017-03-15 23:53:15 +00:00
|
|
|
builder.setTitle(getActivity().getString(R.string.ExportFragment_export_plaintext_to_storage));
|
|
|
|
builder.setMessage(getActivity().getString(R.string.ExportFragment_warning_this_will_export_the_plaintext_contents));
|
|
|
|
builder.setPositiveButton(getActivity().getString(R.string.ExportFragment_export), new Dialog.OnClickListener() {
|
2013-06-25 04:02:30 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2017-10-23 20:03:32 +00:00
|
|
|
new ExportPlaintextTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2013-06-25 04:02:30 +00:00
|
|
|
}
|
|
|
|
});
|
2017-03-15 23:53:15 +00:00
|
|
|
builder.setNegativeButton(getActivity().getString(R.string.ExportFragment_cancel), null);
|
2013-06-25 04:02:30 +00:00
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ImportPlaintextBackupTask extends AsyncTask<Void, Void, Integer> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
|
|
|
progressDialog = ProgressDialog.show(getActivity(),
|
|
|
|
getActivity().getString(R.string.ImportFragment_importing),
|
|
|
|
getActivity().getString(R.string.ImportFragment_import_plaintext_backup_elipse),
|
|
|
|
true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void onPostExecute(Integer result) {
|
|
|
|
Context context = getActivity();
|
|
|
|
|
|
|
|
if (progressDialog != null)
|
|
|
|
progressDialog.dismiss();
|
|
|
|
|
|
|
|
if (context == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case NO_SD_CARD:
|
|
|
|
Toast.makeText(context,
|
|
|
|
context.getString(R.string.ImportFragment_no_plaintext_backup_found),
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case ERROR_IO:
|
|
|
|
Toast.makeText(context,
|
|
|
|
context.getString(R.string.ImportFragment_error_importing_backup),
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case SUCCESS:
|
|
|
|
Toast.makeText(context,
|
|
|
|
context.getString(R.string.ImportFragment_import_complete),
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Integer doInBackground(Void... params) {
|
|
|
|
try {
|
|
|
|
PlaintextBackupImporter.importPlaintextFromSd(getActivity(), masterSecret);
|
|
|
|
return SUCCESS;
|
|
|
|
} catch (NoExternalStorageException e) {
|
|
|
|
Log.w("ImportFragment", e);
|
|
|
|
return NO_SD_CARD;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("ImportFragment", e);
|
|
|
|
return ERROR_IO;
|
|
|
|
}
|
|
|
|
}
|
2017-03-15 23:53:15 +00:00
|
|
|
}
|
2013-06-25 04:02:30 +00:00
|
|
|
|
2017-03-15 23:53:15 +00:00
|
|
|
private class ExportPlaintextTask extends AsyncTask<Void, Void, Integer> {
|
|
|
|
private ProgressDialog dialog;
|
2013-06-25 04:02:30 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
2017-03-15 23:53:15 +00:00
|
|
|
dialog = ProgressDialog.show(getActivity(),
|
|
|
|
getActivity().getString(R.string.ExportFragment_exporting),
|
|
|
|
getActivity().getString(R.string.ExportFragment_exporting_plaintext_to_storage),
|
|
|
|
true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Integer doInBackground(Void... params) {
|
|
|
|
try {
|
|
|
|
PlaintextBackupExporter.exportPlaintextToSd(getActivity(), masterSecret);
|
|
|
|
return SUCCESS;
|
|
|
|
} catch (NoExternalStorageException e) {
|
|
|
|
Log.w("ExportFragment", e);
|
|
|
|
return NO_SD_CARD;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("ExportFragment", e);
|
|
|
|
return ERROR_IO;
|
|
|
|
}
|
2013-06-25 04:02:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 23:53:15 +00:00
|
|
|
@Override
|
2013-06-25 04:02:30 +00:00
|
|
|
protected void onPostExecute(Integer result) {
|
|
|
|
Context context = getActivity();
|
|
|
|
|
2017-03-15 23:53:15 +00:00
|
|
|
if (dialog != null)
|
|
|
|
dialog.dismiss();
|
2013-06-25 04:02:30 +00:00
|
|
|
|
|
|
|
if (context == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case NO_SD_CARD:
|
|
|
|
Toast.makeText(context,
|
2017-03-15 23:53:15 +00:00
|
|
|
context.getString(R.string.ExportFragment_error_unable_to_write_to_storage),
|
2013-06-25 04:02:30 +00:00
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case ERROR_IO:
|
|
|
|
Toast.makeText(context,
|
2017-03-15 23:53:15 +00:00
|
|
|
context.getString(R.string.ExportFragment_error_while_writing_to_storage),
|
2013-06-25 04:02:30 +00:00
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case SUCCESS:
|
|
|
|
Toast.makeText(context,
|
2017-03-15 23:53:15 +00:00
|
|
|
context.getString(R.string.ExportFragment_export_successful),
|
2013-06-25 04:02:30 +00:00
|
|
|
Toast.LENGTH_LONG).show();
|
2017-03-15 23:53:15 +00:00
|
|
|
break;
|
2013-06-25 04:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-15 23:53:15 +00:00
|
|
|
|
2013-06-25 04:02:30 +00:00
|
|
|
}
|