| 1 | /* |
| 2 | * Copyright (C) 2013 Keith Kildare |
| 3 | * |
| 4 | * This file is part of SimplyDo. |
| 5 | * |
| 6 | * SimplyDo is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * SimplyDo is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with SimplyDo. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | package kdk.android.simplydo; |
| 21 | |
| 22 | import java.io.File; |
| 23 | import java.io.FilenameFilter; |
| 24 | import java.util.Comparator; |
| 25 | |
| 26 | import android.app.AlertDialog; |
| 27 | import android.app.Dialog; |
| 28 | import android.app.ListActivity; |
| 29 | import android.content.DialogInterface; |
| 30 | import android.database.sqlite.SQLiteDatabase; |
| 31 | import android.os.Bundle; |
| 32 | import android.os.Environment; |
| 33 | import android.util.Log; |
| 34 | import android.view.View; |
| 35 | import android.widget.ArrayAdapter; |
| 36 | import android.widget.ListView; |
| 37 | import android.widget.Toast; |
| 38 | |
| 39 | public class RestoreActivity extends ListActivity |
| 40 | { |
| 41 | private static final int DIALOG_RESTORE_WARN = 300; |
| 42 | private static final String EXTENSION = ".simplydo"; |
| 43 | |
| 44 | private ArrayAdapter<NameOnlyFile> adapter; |
| 45 | private AlertDialog.Builder restoreWarningBuilder; |
| 46 | private NameOnlyFile restoreFile; |
| 47 | private FilenameFilter restoreFilenameFilter; |
| 48 | private Comparator<NameOnlyFile> comparator; |
| 49 | |
| 50 | public RestoreActivity() |
| 51 | { |
| 52 | restoreFilenameFilter = new FilenameFilter() { |
| 53 | @Override |
| 54 | public boolean accept(File dir, String filename) |
| 55 | { |
| 56 | return filename.endsWith(EXTENSION); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | comparator = new Comparator<NameOnlyFile>() { |
| 61 | @Override |
| 62 | public int compare(NameOnlyFile object1, NameOnlyFile object2) |
| 63 | { |
| 64 | return object2.toString().compareTo(object1.toString()); |
| 65 | } |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | protected void onCreate(Bundle savedInstanceState) |
| 71 | { |
| 72 | super.onCreate(savedInstanceState); |
| 73 | |
| 74 | Log.v(L.TAG, "RestoreActivity.onCreate() called"); |
| 75 | |
| 76 | adapter = new ArrayAdapter<NameOnlyFile>(this, R.layout.restore_entry, R.id.RestoreName); |
| 77 | |
| 78 | refresh(); |
| 79 | |
| 80 | setListAdapter(adapter); |
| 81 | |
| 82 | restoreWarningBuilder = new AlertDialog.Builder(this); |
| 83 | restoreWarningBuilder.setMessage(R.string.restoreWarnMessage) |
| 84 | .setCancelable(true) |
| 85 | .setTitle(R.string.restoreWarnTitle) |
| 86 | .setPositiveButton(R.string.restoreWarnPositive, new DialogInterface.OnClickListener() { |
| 87 | public void onClick(DialogInterface dialog, int id) { |
| 88 | doRestore(); |
| 89 | dialog.cancel(); |
| 90 | } |
| 91 | }) |
| 92 | .setNegativeButton(R.string.restoreWarnNegative, new DialogInterface.OnClickListener() { |
| 93 | public void onClick(DialogInterface dialog, int id) { |
| 94 | dialog.cancel(); |
| 95 | } |
| 96 | }); |
| 97 | |
| 98 | } |
| 99 | |
| 100 | |
| 101 | @Override |
| 102 | protected Dialog onCreateDialog(int id) |
| 103 | { |
| 104 | Log.v(L.TAG, "RestoreActivity.onCreateDialog() called"); |
| 105 | |
| 106 | switch(id) |
| 107 | { |
| 108 | case DIALOG_RESTORE_WARN: |
| 109 | { |
| 110 | AlertDialog dialog = restoreWarningBuilder.create(); |
| 111 | return dialog; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return super.onCreateDialog(id); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | protected void onListItemClick(ListView l, View v, int position, long id) |
| 120 | { |
| 121 | super.onListItemClick(l, v, position, id); |
| 122 | |
| 123 | Log.i(L.TAG, "RestoreActivity.onListItemClick()"); |
| 124 | |
| 125 | restoreFile = adapter.getItem(position); |
| 126 | |
| 127 | try |
| 128 | { |
| 129 | // test restore file |
| 130 | SQLiteDatabase db = SQLiteDatabase.openDatabase( |
| 131 | restoreFile.file.getPath(), null, SQLiteDatabase.OPEN_READONLY); |
| 132 | db.close(); |
| 133 | |
| 134 | // Dialog: This will overwrite the existing items, continue? |
| 135 | showDialog(DIALOG_RESTORE_WARN); |
| 136 | } |
| 137 | catch(Exception e) |
| 138 | { |
| 139 | Log.e(L.TAG, "Error testing user selected restore DB", e); |
| 140 | Toast t = Toast.makeText(this, R.string.restoreToastInvalidDB, Toast.LENGTH_LONG); |
| 141 | t.show(); |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | |
| 146 | |
| 147 | private void refresh() |
| 148 | { |
| 149 | File backupDirectory = new File( |
| 150 | Environment.getExternalStorageDirectory(), |
| 151 | "/Android/data/kdk.android.simplydo/files/"); |
| 152 | adapter.clear(); |
| 153 | if(backupDirectory.isDirectory()) |
| 154 | { |
| 155 | File[] files = backupDirectory.listFiles(restoreFilenameFilter); |
| 156 | for(File f : files) |
| 157 | { |
| 158 | adapter.add(new NameOnlyFile(f)); |
| 159 | } |
| 160 | adapter.sort(comparator); |
| 161 | } |
| 162 | adapter.notifyDataSetChanged(); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | private void doRestore() |
| 167 | { |
| 168 | Log.i(L.TAG, "RestoreActivity.doRestore() called"); |
| 169 | |
| 170 | // Flush the database update queue |
| 171 | SimplyDoActivity.getInstance().getDataVeiwer().flush(); |
| 172 | |
| 173 | String state = Environment.getExternalStorageState(); |
| 174 | if (!Environment.MEDIA_MOUNTED.equals(state)) |
| 175 | { |
| 176 | Toast.makeText( |
| 177 | this, |
| 178 | R.string.restoreToastMountProblem, |
| 179 | Toast.LENGTH_LONG |
| 180 | ).show(); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | // backup old file |
| 185 | File dbFile = getDatabasePath(DataManager.DATABASE_NAME); |
| 186 | File dbBakFile = getDatabasePath(DataManager.DATABASE_NAME + ".bak"); |
| 187 | boolean moved = dbFile.renameTo(dbBakFile); |
| 188 | if(!moved) |
| 189 | { |
| 190 | Toast.makeText( |
| 191 | this, |
| 192 | R.string.restoreToastUnableToMove, |
| 193 | Toast.LENGTH_LONG |
| 194 | ).show(); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | try |
| 199 | { |
| 200 | // copy new file into place |
| 201 | SettingsActivity.fileCopy(restoreFile.file, dbFile); |
| 202 | |
| 203 | // delete backup |
| 204 | dbBakFile.delete(); |
| 205 | } |
| 206 | catch (Exception e) |
| 207 | { |
| 208 | // put the old database back |
| 209 | dbFile.delete(); |
| 210 | dbBakFile.renameTo(dbFile); |
| 211 | |
| 212 | Log.e(L.TAG, "Failed to copy restore database into place", e); |
| 213 | |
| 214 | Toast.makeText( |
| 215 | this, |
| 216 | R.string.restoreToastCopyFailed, |
| 217 | Toast.LENGTH_LONG |
| 218 | ).show(); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | SimplyDoActivity.getInstance().getDataVeiwer().invalidateCache(); |
| 223 | SimplyDoActivity.getInstance().cacheInvalidated(); |
| 224 | |
| 225 | Toast.makeText( |
| 226 | this, |
| 227 | R.string.restoreToastRestoreFinished, |
| 228 | Toast.LENGTH_LONG |
| 229 | ).show(); |
| 230 | |
| 231 | finish(); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | private static class NameOnlyFile |
| 236 | { |
| 237 | public File file; |
| 238 | |
| 239 | public NameOnlyFile(File f) |
| 240 | { |
| 241 | file = f; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public String toString() |
| 246 | { |
| 247 | String name = file.getName(); |
| 248 | return name.substring(0, name.length() - EXTENSION.length()); |
| 249 | } |
| 250 | } |
| 251 | } |