|
| 1 | +package org.dodgybits.android.shuffle.activity; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.OutputStream; |
| 7 | +import java.text.SimpleDateFormat; |
| 8 | +import java.util.Date; |
| 9 | + |
| 10 | +import org.dodgybits.android.shuffle.R; |
| 11 | +import org.dodgybits.android.shuffle.model.Context; |
| 12 | +import org.dodgybits.android.shuffle.model.Project; |
| 13 | +import org.dodgybits.android.shuffle.model.Task; |
| 14 | +import org.dodgybits.android.shuffle.provider.Shuffle; |
| 15 | +import org.dodgybits.android.shuffle.util.BindingUtils; |
| 16 | +import org.dodgybits.shuffle.dto.ShuffleProtos.Catalogue; |
| 17 | +import org.dodgybits.shuffle.dto.ShuffleProtos.Catalogue.Builder; |
| 18 | + |
| 19 | +import android.app.Activity; |
| 20 | +import android.database.Cursor; |
| 21 | +import android.os.Bundle; |
| 22 | +import android.os.Environment; |
| 23 | +import android.text.TextUtils; |
| 24 | +import android.util.Log; |
| 25 | +import android.view.View; |
| 26 | +import android.widget.EditText; |
| 27 | +import android.widget.Toast; |
| 28 | + |
| 29 | +public class PreferencesCreateBackupActivity extends Activity |
| 30 | + implements View.OnClickListener { |
| 31 | + private static final String cTag = "PreferencesCreateBackupActivity"; |
| 32 | + |
| 33 | + private EditText mFilenameWidget; |
| 34 | + |
| 35 | + @Override |
| 36 | + protected void onCreate(Bundle icicle) { |
| 37 | + Log.d(cTag, "onCreate+"); |
| 38 | + super.onCreate(icicle); |
| 39 | + setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT); |
| 40 | + |
| 41 | + setContentView(R.layout.backup_create); |
| 42 | + |
| 43 | + mFilenameWidget = (EditText) findViewById(R.id.filename); |
| 44 | + Date today = new Date(); |
| 45 | + SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); |
| 46 | + String defaultText = "shuffle-" + formatter.format(today) + ".bak"; |
| 47 | + mFilenameWidget.setText(defaultText); |
| 48 | + |
| 49 | + addSavePanelListeners(); |
| 50 | + } |
| 51 | + |
| 52 | + public void onClick(View v) { |
| 53 | + switch (v.getId()) { |
| 54 | + |
| 55 | + case R.id.saveButton: |
| 56 | + createBackup(); |
| 57 | + break; |
| 58 | + |
| 59 | + case R.id.discardButton: |
| 60 | + finish(); |
| 61 | + break; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private void addSavePanelListeners() { |
| 66 | + // Setup the bottom buttons |
| 67 | + View view = findViewById(R.id.saveButton); |
| 68 | + view.setOnClickListener(this); |
| 69 | + view = findViewById(R.id.discardButton); |
| 70 | + view.setOnClickListener(this); |
| 71 | + } |
| 72 | + |
| 73 | + private void createBackup() { |
| 74 | + String filename = mFilenameWidget.getText().toString(); |
| 75 | + if (TextUtils.isEmpty(filename)) { |
| 76 | + // TODO show alert |
| 77 | + Log.e(cTag, "Filename was empty"); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + String storage_state = Environment.getExternalStorageState(); |
| 82 | + if (! Environment.MEDIA_MOUNTED.equals(storage_state)) { |
| 83 | + // TODO show alert |
| 84 | + Log.e(cTag, "Media is not mounted: " + storage_state); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + File dir = Environment.getExternalStorageDirectory(); |
| 89 | + File backupFile = new File(dir, filename); |
| 90 | + try { |
| 91 | + backupFile.createNewFile(); |
| 92 | + FileOutputStream out = new FileOutputStream(backupFile); |
| 93 | + writeBackup(out); |
| 94 | + } catch (IOException ioe) { |
| 95 | + // TODO show alert |
| 96 | + Log.e(cTag, "Failed to create backup " + ioe.getMessage()); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + String text = getResources().getString(R.string.toast_backup_saved, filename); |
| 101 | + Toast.makeText(this, text, Toast.LENGTH_LONG).show(); |
| 102 | + finish(); |
| 103 | + } |
| 104 | + |
| 105 | + private void writeBackup(OutputStream out) throws IOException { |
| 106 | + // TODO display progress |
| 107 | + Builder builder = Catalogue.newBuilder(); |
| 108 | + |
| 109 | + // write contexts |
| 110 | + Cursor contextCursor = getContentResolver().query( |
| 111 | + Shuffle.Contexts.CONTENT_URI, Shuffle.Contexts.cFullProjection, |
| 112 | + null, null, null); |
| 113 | + while (contextCursor.moveToNext()) { |
| 114 | + Context context = BindingUtils.readContext(contextCursor, getResources()); |
| 115 | + builder.addContext(context.toDto()); |
| 116 | + } |
| 117 | + contextCursor.close(); |
| 118 | + |
| 119 | + // write projects |
| 120 | + Cursor projectCursor = getContentResolver().query( |
| 121 | + Shuffle.Projects.CONTENT_URI, Shuffle.Projects.cFullProjection, |
| 122 | + null, null, null); |
| 123 | + while (projectCursor.moveToNext()) { |
| 124 | + Project project = BindingUtils.readProject(projectCursor); |
| 125 | + builder.addProject(project.toDto()); |
| 126 | + } |
| 127 | + projectCursor.close(); |
| 128 | + |
| 129 | + // write tasks |
| 130 | + Cursor taskCursor = getContentResolver().query( |
| 131 | + Shuffle.Tasks.CONTENT_URI, Shuffle.Tasks.cFullProjection, |
| 132 | + null, null, null); |
| 133 | + while (taskCursor.moveToNext()) { |
| 134 | + Task task = BindingUtils.readTask(taskCursor, getResources()); |
| 135 | + builder.addTask(task.toDto()); |
| 136 | + } |
| 137 | + taskCursor.close(); |
| 138 | + |
| 139 | + builder.build().writeTo(out); |
| 140 | + out.close(); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments