2013-06-25 04:02:30 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
2014-06-29 03:40:57 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
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;
|
|
|
|
import org.thoughtcrime.securesms.database.PlaintextBackupExporter;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Dialogs;
|
2013-06-25 04:02:30 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
2014-06-29 03:40:57 +00:00
|
|
|
public class ExportFragment 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 IO_ERROR = 2;
|
|
|
|
|
|
|
|
private MasterSecret masterSecret;
|
|
|
|
|
|
|
|
public void setMasterSecret(MasterSecret masterSecret) {
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
|
|
|
|
View layout = inflater.inflate(R.layout.export_fragment, container, false);
|
2014-06-22 23:29:05 +00:00
|
|
|
// View exportEncryptedView = layout.findViewById(R.id.export_encrypted_backup);
|
2013-06-25 04:02:30 +00:00
|
|
|
View exportPlaintextView = layout.findViewById(R.id.export_plaintext_backup);
|
|
|
|
|
2014-06-22 23:29:05 +00:00
|
|
|
// exportEncryptedView.setOnClickListener(new View.OnClickListener() {
|
|
|
|
// @Override
|
|
|
|
// public void onClick(View v) {
|
|
|
|
// handleExportEncryptedBackup();
|
|
|
|
// }
|
|
|
|
// });
|
2013-06-25 04:02:30 +00:00
|
|
|
|
|
|
|
exportPlaintextView.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
handleExportPlaintextBackup();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2014-06-22 23:29:05 +00:00
|
|
|
// private void handleExportEncryptedBackup() {
|
|
|
|
// AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
|
|
// builder.setIcon(Dialogs.resolveIcon(getActivity(), R.attr.dialog_info_icon));
|
|
|
|
// builder.setTitle(getActivity().getString(R.string.ExportFragment_export_to_sd_card));
|
|
|
|
// builder.setMessage(getActivity().getString(R.string.ExportFragment_this_will_export_your_encrypted_keys_settings_and_messages));
|
|
|
|
// builder.setPositiveButton(getActivity().getString(R.string.ExportFragment_export), new Dialog.OnClickListener() {
|
|
|
|
// @Override
|
|
|
|
// public void onClick(DialogInterface dialog, int which) {
|
|
|
|
// new ExportEncryptedTask().execute();
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// builder.setNegativeButton(getActivity().getString(R.string.ExportFragment_cancel), null);
|
|
|
|
// builder.show();
|
|
|
|
// }
|
2013-06-25 04:02:30 +00:00
|
|
|
|
|
|
|
private void handleExportPlaintextBackup() {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
2014-03-01 00:32:00 +00:00
|
|
|
builder.setIcon(Dialogs.resolveIcon(getActivity(), R.attr.dialog_alert_icon));
|
2013-06-25 04:02:30 +00:00
|
|
|
builder.setTitle(getActivity().getString(R.string.ExportFragment_export_plaintext_to_sd_card));
|
|
|
|
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() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
new ExportPlaintextTask().execute();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.setNegativeButton(getActivity().getString(R.string.ExportFragment_cancel), null);
|
|
|
|
builder.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ExportPlaintextTask extends AsyncTask<Void, Void, Integer> {
|
|
|
|
private ProgressDialog dialog;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
|
|
|
dialog = ProgressDialog.show(getActivity(),
|
|
|
|
getActivity().getString(R.string.ExportFragment_exporting),
|
|
|
|
getActivity().getString(R.string.ExportFragment_exporting_plaintext_to_sd_card),
|
|
|
|
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 IO_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Integer result) {
|
|
|
|
Context context = getActivity();
|
|
|
|
|
|
|
|
if (dialog != null)
|
|
|
|
dialog.dismiss();
|
|
|
|
|
|
|
|
if (context == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case NO_SD_CARD:
|
|
|
|
Toast.makeText(context,
|
|
|
|
context.getString(R.string.ExportFragment_error_unable_to_write_to_sd_card),
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case IO_ERROR:
|
|
|
|
Toast.makeText(context,
|
|
|
|
context.getString(R.string.ExportFragment_error_while_writing_to_sd_card),
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case SUCCESS:
|
|
|
|
Toast.makeText(context,
|
|
|
|
context.getString(R.string.ExportFragment_success),
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-22 23:29:05 +00:00
|
|
|
// private class ExportEncryptedTask extends AsyncTask<Void, Void, Integer> {
|
|
|
|
// private ProgressDialog dialog;
|
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// protected void onPreExecute() {
|
|
|
|
// dialog = ProgressDialog.show(getActivity(),
|
|
|
|
// getActivity().getString(R.string.ExportFragment_exporting),
|
|
|
|
// getActivity().getString(R.string.ExportFragment_exporting_keys_settings_and_messages),
|
|
|
|
// true, false);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// protected void onPostExecute(Integer result) {
|
|
|
|
// Context context = getActivity();
|
|
|
|
//
|
|
|
|
// if (dialog != null) dialog.dismiss();
|
|
|
|
//
|
|
|
|
// if (context == null) return;
|
|
|
|
//
|
|
|
|
// switch (result) {
|
|
|
|
// case NO_SD_CARD:
|
|
|
|
// Toast.makeText(context,
|
|
|
|
// context.getString(R.string.ExportFragment_error_unable_to_write_to_sd_card),
|
|
|
|
// Toast.LENGTH_LONG).show();
|
|
|
|
// break;
|
|
|
|
// case IO_ERROR:
|
|
|
|
// Toast.makeText(context,
|
|
|
|
// context.getString(R.string.ExportFragment_error_while_writing_to_sd_card),
|
|
|
|
// Toast.LENGTH_LONG).show();
|
|
|
|
// break;
|
|
|
|
// case SUCCESS:
|
|
|
|
// Toast.makeText(context,
|
|
|
|
// context.getString(R.string.ExportFragment_success),
|
|
|
|
// Toast.LENGTH_LONG).show();
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// protected Integer doInBackground(Void... params) {
|
|
|
|
// try {
|
|
|
|
// EncryptedBackupExporter.exportToSd(getActivity());
|
|
|
|
// return SUCCESS;
|
|
|
|
// } catch (NoExternalStorageException e) {
|
|
|
|
// Log.w("ExportFragment", e);
|
|
|
|
// return NO_SD_CARD;
|
|
|
|
// } catch (IOException e) {
|
|
|
|
// Log.w("ExportFragment", e);
|
|
|
|
// return IO_ERROR;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2013-06-25 04:02:30 +00:00
|
|
|
}
|