1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package de.bjusystems.vdrmanager.backup;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import de.bjusystems.vdrmanager.data.db.DBAccess;
/**
* Handler for writing or reading single-file backups.
*
* @author Rodrigo Damazio
*/
class ExternalFileBackup {
// Filename format - in UTC
private static final SimpleDateFormat BACKUP_FILENAME_FORMAT = new SimpleDateFormat(
"'backup-'yyyy-MM-dd_HH-mm-ss'.zip'");
static {
BACKUP_FILENAME_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
}
private static final String BACKUPS_SUBDIR = "backups";
private static final int BACKUP_FORMAT_VERSION = 1;
private static final String ZIP_ENTRY_NAME = "backup.mybillingbuddy.v"
+ BACKUP_FORMAT_VERSION;
private static final int COMPRESSION_LEVEL = 8;
private final Context context;
public ExternalFileBackup(Context context) {
this.context = context;
}
/**
* Returns whether the backups directory is (or can be made) available.
*
* @param create
* whether to try creating the directory if it doesn't exist
*/
public boolean isBackupsDirectoryAvailable(boolean create) {
return getBackupsDirectory(create) != null;
}
/**
* Returns the backup directory, or null if not available.
*
* @param create
* whether to try creating the directory if it doesn't exist
*/
private File getBackupsDirectory(boolean create) {
String dirName = FileUtils.buildExternalDirectoryPath(BACKUPS_SUBDIR);
final File dir = new File(dirName);
Log.d(Constants.TAG, "Dir: " + dir.getAbsolutePath());
if (create) {
// Try to create - if that fails, return null
return FileUtils.ensureDirectoryExists(dir) ? dir : null;
} else {
// Return it if it already exists, otherwise return null
return dir.isDirectory() ? dir : null;
}
}
/**
* Returns a list of available backups to be restored.
*/
public Date[] getAvailableBackups() {
File dir = getBackupsDirectory(false);
if (dir == null) {
return null;
}
String[] fileNames = dir.list();
List<Date> backupDates = new ArrayList<Date>(fileNames.length);
for (int i = 0; i < fileNames.length; i++) {
String fileName = fileNames[i];
try {
backupDates.add(BACKUP_FILENAME_FORMAT.parse(fileName));
} catch (ParseException e) {
// Not a backup file, ignore
}
}
return backupDates.toArray(new Date[backupDates.size()]);
}
/**
* Writes the backup to the default file.
*/
public void writeToDefaultFile() throws IOException {
writeToFile(getFileForDate(new Date()));
}
/**
* Restores the backup from the given date.
*/
public void restoreFromDate(Date when) throws IOException {
restoreFromFile(getFileForDate(when));
}
public void restoreFromFile(String path ) throws IOException {
restoreFromFile(new File(path));
}
/**
* Produces the proper file descriptor for the given backup date.
*/
private File getFileForDate(Date when) {
File dir = getBackupsDirectory(false);
String fileName = BACKUP_FILENAME_FORMAT.format(when);
File file = new File(dir, fileName);
return file;
}
/**
* Synchronously writes a backup to the given file.
*/
private void writeToFile(File outputFile) throws IOException {
Log.d(Constants.TAG,
"Writing backup to file " + outputFile.getAbsolutePath());
// Create all the auxiliary classes that will do the writing
//DatabaseDumper trackDumper = new DatabaseDumper(
// BackupColumns.TRACKS_BACKUP_COLUMNS,
//BackupColumns.TRACKS_BACKUP_COLUMN_TYPES, false);
//DatabaseDumper waypointDumper = new DatabaseDumper(
// BackupColumns.WAYPOINTS_BACKUP_COLUMNS,
//BackupColumns.WAYPOINTS_BACKUP_COLUMN_TYPES, false);
//DatabaseDumper pointDumper = new DatabaseDumper(
// BackupColumns.POINTS_BACKUP_COLUMNS,
//BackupColumns.POINTS_BACKUP_COLUMN_TYPES, false);
// Open the target for writing
FileOutputStream outputStream = new FileOutputStream(outputFile);
ZipOutputStream compressedStream = new ZipOutputStream(outputStream);
compressedStream.setLevel(COMPRESSION_LEVEL);
compressedStream.putNextEntry(new ZipEntry(ZIP_ENTRY_NAME));
DataOutputStream outWriter = new DataOutputStream(compressedStream);
try {
// Dump the entire contents of each table
// ContentResolver contentResolver = context.getContentResolver();
// Cursor tracksCursor = contentResolver.query(
// TracksColumns.CONTENT_URI, null, null, null, null);
// try {
// trackDumper.writeAllRows(tracksCursor, outWriter);
// } finally {
// tracksCursor.close();
// }
//
// Cursor waypointsCursor = contentResolver.query(
// WaypointsColumns.CONTENT_URI, null, null, null, null);
// try {
// waypointDumper.writeAllRows(waypointsCursor, outWriter);
// } finally {
// waypointsCursor.close();
// }
//
// Cursor pointsCursor = contentResolver.query(
// TrackPointsColumns.CONTENT_URI, null, null, null, null);
// try {
// pointDumper.writeAllRows(pointsCursor, outWriter);
// } finally {
// pointsCursor.close();
// }
// Dump preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
PreferenceBackupHelper preferencesHelper = new PreferenceBackupHelper();
preferencesHelper.exportPreferences(preferences, outWriter);
File f = new File(DBAccess.getDataBaseFile());
if(f.exists()){
compressedStream.putNextEntry(new ZipEntry(DBAccess.DATABASE_NAME));
IOUtils.copy(new FileInputStream(DBAccess.getDataBaseFile()), outWriter);
}
} catch (IOException e) {
// We tried to delete the partially created file, but do nothing
// if that also fails.
if (!outputFile.delete()) {
Log.w(Constants.TAG,
"Failed to delete file " + outputFile.getAbsolutePath());
}
throw e;
} finally {
compressedStream.closeEntry();
compressedStream.close();
}
}
/**
* Synchronously restores the backup from the given file.
*/
private void restoreFromFile(File inputFile) throws IOException {
Log.d(Constants.TAG,
"Restoring from file " + inputFile.getAbsolutePath());
ZipFile zipFile = new ZipFile(inputFile, ZipFile.OPEN_READ);
ZipEntry zipEntry = zipFile.getEntry(ZIP_ENTRY_NAME);
if (zipEntry == null) {
throw new IOException("Invalid backup ZIP file");
}
InputStream compressedStream = zipFile.getInputStream(zipEntry);
DataInputStream reader = new DataInputStream(compressedStream);
try {
// Restore preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
PreferenceBackupHelper preferencesHelper = new PreferenceBackupHelper();
preferencesHelper.importPreferences(reader, preferences);
zipEntry = zipFile.getEntry(DBAccess.DATABASE_NAME);
if (zipEntry != null) {
IOUtils.copy(zipFile.getInputStream(zipEntry), new FileOutputStream(DBAccess.getDataBaseFile()));
deleteJournal(DBAccess.getDataBaseFile());
}
} finally {
compressedStream.close();
zipFile.close();
}
}
private static void deleteJournal(String db){
if(db == null){
return;
}
new File(db+"-journal").delete();
}
}
|