| 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.FileInputStream; |
| 24 | import java.io.FileOutputStream; |
| 25 | import java.io.IOException; |
| 26 | import java.text.DecimalFormat; |
| 27 | import java.util.Calendar; |
| 28 | |
| 29 | import android.content.Intent; |
| 30 | import android.os.Bundle; |
| 31 | import android.os.Environment; |
| 32 | import android.preference.Preference; |
| 33 | import android.preference.PreferenceActivity; |
| 34 | import android.preference.PreferenceScreen; |
| 35 | import android.util.Log; |
| 36 | import android.widget.Toast; |
| 37 | |
| 38 | public class SettingsActivity extends PreferenceActivity |
| 39 | { |
| 40 | private DecimalFormat twoDigits = new DecimalFormat("00"); |
| 41 | private File backupDirectory; |
| 42 | |
| 43 | @Override |
| 44 | protected void onCreate(Bundle savedInstanceState) |
| 45 | { |
| 46 | super.onCreate(savedInstanceState); |
| 47 | addPreferencesFromResource(R.xml.settings); |
| 48 | |
| 49 | // TODO offer list of backup locations as preference |
| 50 | // at least suggested location and somewhere on ext. sdcard |
| 51 | backupDirectory = new File( |
| 52 | Environment.getExternalStorageDirectory(), |
| 53 | "/Android/data/kdk.android.simplydo/files/"); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, |
| 58 | Preference preference) |
| 59 | { |
| 60 | Log.v(L.TAG, "onPreferenceTreeClick() for key " + preference.getKey()); |
| 61 | |
| 62 | if("backupDb".equals(preference.getKey())) |
| 63 | { |
| 64 | // TODO dialog: This will backup you current SimplyDo database. You |
| 65 | // can access and manage these backups through mass storage |
| 66 | // access to you device. Make backup now to the file .... |
| 67 | backupDbSelected(); |
| 68 | } |
| 69 | else if("restoreDb".equals(preference.getKey())) |
| 70 | { |
| 71 | Intent restoreActivity = new Intent(getBaseContext(), RestoreActivity.class); |
| 72 | startActivity(restoreActivity); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | // the android docs give no clues as to what the returned bool does |
| 77 | return super.onPreferenceTreeClick(preferenceScreen, preference); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | private void backupDbSelected() |
| 82 | { |
| 83 | Calendar cal = Calendar.getInstance(); |
| 84 | int seconds = (cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE)) * 60 + cal.get(Calendar.SECOND); |
| 85 | String filename = |
| 86 | "SimplyDo_" + |
| 87 | cal.get(Calendar.YEAR) + |
| 88 | twoDigits.format(cal.get(Calendar.MONTH) + 1) + |
| 89 | twoDigits.format(cal.get(Calendar.DAY_OF_MONTH)) + "_" + |
| 90 | seconds + ".simplydo"; |
| 91 | |
| 92 | String state = Environment.getExternalStorageState(); |
| 93 | |
| 94 | final File dbFile = getDatabasePath(DataManager.DATABASE_NAME); |
| 95 | |
| 96 | if (Environment.MEDIA_MOUNTED.equals(state)) |
| 97 | { |
| 98 | // We can read and write the media |
| 99 | backupDirectory.mkdirs(); |
| 100 | final File outFile = new File(backupDirectory, filename); |
| 101 | |
| 102 | Log.d(L.TAG, "Backing up to " + outFile.getAbsolutePath() + "\n" + dbFile.getAbsolutePath()); |
| 103 | |
| 104 | copyDb(dbFile, outFile); |
| 105 | } |
| 106 | else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) |
| 107 | { |
| 108 | // We can only read the media and we need to write |
| 109 | Toast t = Toast.makeText(this, R.string.settingsMediaReadOnly, Toast.LENGTH_LONG); |
| 110 | t.show(); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | // Something else is wrong. |
| 115 | Toast t = Toast.makeText(this, R.string.settingsMediaNoMount, Toast.LENGTH_LONG); |
| 116 | t.show(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | private void copyDb(File src, File dst) |
| 122 | { |
| 123 | try |
| 124 | { |
| 125 | SimplyDoActivity.getInstance().getDataVeiwer().flush(); |
| 126 | |
| 127 | fileCopy(src, dst); |
| 128 | String backedUpTo = String.format(getString(R.string.settingsBackedUp), dst.getName()); |
| 129 | Toast t = Toast.makeText(this, backedUpTo, Toast.LENGTH_SHORT); |
| 130 | t.show(); |
| 131 | } |
| 132 | catch (Exception e) |
| 133 | { |
| 134 | Log.d(L.TAG, "Failed to copy files: " + e.getMessage(), e); |
| 135 | String backUpFailed = String.format(getString(R.string.settingsBackUpFailed), e.getMessage()); |
| 136 | Toast t = Toast.makeText(this, backUpFailed, Toast.LENGTH_SHORT); |
| 137 | t.show(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Copies the specified source file to the destination file |
| 143 | * location. |
| 144 | * @param src Source file |
| 145 | * @param dst Destination file location |
| 146 | * @throws IOException On error. |
| 147 | */ |
| 148 | public static void fileCopy(File src, File dst) throws IOException |
| 149 | { |
| 150 | FileInputStream srcStream = new FileInputStream(src); |
| 151 | FileOutputStream dstStream = new FileOutputStream(dst); |
| 152 | |
| 153 | byte[] buffer = new byte[1024]; |
| 154 | |
| 155 | int bytesRead = srcStream.read(buffer); |
| 156 | while(bytesRead > 0) |
| 157 | { |
| 158 | dstStream.write(buffer, 0, bytesRead); |
| 159 | bytesRead = srcStream.read(buffer); |
| 160 | } |
| 161 | |
| 162 | dstStream.close(); |
| 163 | srcStream.close(); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | } |