Simple Notepad Application for Android OS
Revision | c103a2a1b60ab43e839932ee9c9999c4bf3ebcee (tree) |
---|---|
Time | 2014-07-12 16:15:55 |
Author | Masahiko, SAWAI <say@user...> |
Commiter | Masahiko, SAWAI |
Database backup process is now as a service
@@ -239,6 +239,8 @@ | ||
239 | 239 | |
240 | 240 | <service android:name=".provider.NoteDBOptimizer" /> |
241 | 241 | |
242 | + <service android:name=".prefs.BackupDatabaseService" /> | |
243 | + | |
242 | 244 | <!-- Provider : Notes & Templates Content Provider --> |
243 | 245 | <provider |
244 | 246 | android:name=".provider.NoteProvider" |
@@ -24,22 +24,13 @@ | ||
24 | 24 | package org.routine_work.notepad.prefs; |
25 | 25 | |
26 | 26 | import android.app.Activity; |
27 | -import android.media.MediaScannerConnection; | |
27 | +import android.content.Intent; | |
28 | 28 | import android.os.Bundle; |
29 | -import android.os.Environment; | |
30 | -import android.text.format.DateFormat; | |
31 | 29 | import android.util.Log; |
32 | 30 | import android.view.View; |
33 | 31 | import android.view.View.OnClickListener; |
34 | 32 | import android.widget.Button; |
35 | -import android.widget.Toast; | |
36 | -import java.io.File; | |
37 | -import java.io.FileInputStream; | |
38 | -import java.io.FileOutputStream; | |
39 | -import java.io.IOException; | |
40 | -import java.nio.channels.FileChannel; | |
41 | 33 | import org.routine_work.notepad.R; |
42 | -import org.routine_work.notepad.provider.NoteStore; | |
43 | 34 | |
44 | 35 | public class BackupDatabaseActivity extends Activity |
45 | 36 | implements OnClickListener, BackupConstants |
@@ -50,6 +41,7 @@ public class BackupDatabaseActivity extends Activity | ||
50 | 41 | @Override |
51 | 42 | protected void onCreate(Bundle savedInstanceState) |
52 | 43 | { |
44 | + Log.v(LOG_TAG, "Hello"); | |
53 | 45 | setTheme(NotepadPreferenceUtils.getTheme(this)); |
54 | 46 | super.onCreate(savedInstanceState); |
55 | 47 | setContentView(R.layout.backup_database_activity); |
@@ -58,6 +50,7 @@ public class BackupDatabaseActivity extends Activity | ||
58 | 50 | okButton.setOnClickListener(this); |
59 | 51 | Button cancelButton = (Button) findViewById(R.id.cancel_button); |
60 | 52 | cancelButton.setOnClickListener(this); |
53 | + Log.v(LOG_TAG, "Bye"); | |
61 | 54 | } |
62 | 55 | |
63 | 56 | @Override |
@@ -67,7 +60,7 @@ public class BackupDatabaseActivity extends Activity | ||
67 | 60 | switch (id) |
68 | 61 | { |
69 | 62 | case R.id.ok_button: |
70 | - backupDatabaseFile(); | |
63 | + startBackupDatabaseService(); | |
71 | 64 | setResult(RESULT_OK); |
72 | 65 | finish(); |
73 | 66 | break; |
@@ -78,52 +71,13 @@ public class BackupDatabaseActivity extends Activity | ||
78 | 71 | } |
79 | 72 | } |
80 | 73 | |
81 | - private void backupDatabaseFile() | |
74 | + private void startBackupDatabaseService() | |
82 | 75 | { |
83 | - File databaseFilePath = NoteStore.getNoteDatabasePath(this); | |
84 | - Log.d(LOG_TAG, "databaseFilePath => " + databaseFilePath); | |
76 | + Log.v(LOG_TAG, "Hello"); | |
85 | 77 | |
86 | - String backupFileName = DateFormat.format(BACKUP_FILE_DATE_FORMAT, System.currentTimeMillis()) + BACKUP_FILE_SUFFIX; | |
87 | - File backupDirPath = Environment.getExternalStorageDirectory(); | |
88 | - backupDirPath = new File(backupDirPath, BACKUP_DIR_NAME); | |
89 | - Log.d(LOG_TAG, "backupDirPath => " + backupDirPath); | |
90 | - File backupFilePath = new File(backupDirPath, backupFileName); | |
91 | - Log.d(LOG_TAG, "backupFilePath => " + backupFilePath); | |
78 | + Intent backDatabaseIntent = new Intent(this, BackupDatabaseService.class); | |
79 | + startService(backDatabaseIntent); | |
92 | 80 | |
93 | - String externalStorageState = Environment.getExternalStorageState(); | |
94 | - if (Environment.MEDIA_MOUNTED.equals(externalStorageState)) | |
95 | - { | |
96 | - try | |
97 | - { | |
98 | - if (backupDirPath.exists() == false) | |
99 | - { | |
100 | - Log.i(LOG_TAG, "Create backup directory. backupDirPath => " + backupDirPath); | |
101 | - boolean mkdirs = backupDirPath.mkdirs(); | |
102 | - Log.d(LOG_TAG, "mkdirs => " + mkdirs); | |
103 | - } | |
104 | - | |
105 | - Log.i(LOG_TAG, "Backup database " + databaseFilePath + " to " + backupFilePath); | |
106 | - FileChannel inputChannel = new FileInputStream(databaseFilePath).getChannel(); | |
107 | - FileChannel outputChannel = new FileOutputStream(backupFilePath).getChannel(); | |
108 | - inputChannel.transferTo(0, inputChannel.size(), outputChannel); | |
109 | - inputChannel.close(); | |
110 | - outputChannel.close(); | |
111 | - | |
112 | - MediaScannerConnection.scanFile(this, new String[] | |
113 | - { | |
114 | - backupFilePath.getAbsolutePath() | |
115 | - }, null, null); | |
116 | - } | |
117 | - catch (IOException ex) | |
118 | - { | |
119 | - Log.e(LOG_TAG, "The database file copying is failed.", ex); | |
120 | - } | |
121 | - } | |
122 | - else | |
123 | - { | |
124 | - String message = "The external storage is not mounted."; | |
125 | - Log.e(LOG_TAG, message + "externalStorageState => " + externalStorageState); | |
126 | - Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); | |
127 | - } | |
81 | + Log.v(LOG_TAG, "Bye"); | |
128 | 82 | } |
129 | 83 | } |
@@ -0,0 +1,100 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package org.routine_work.notepad.prefs; | |
25 | + | |
26 | +import android.app.IntentService; | |
27 | +import android.content.Intent; | |
28 | +import android.os.Environment; | |
29 | +import android.text.format.DateFormat; | |
30 | +import android.widget.Toast; | |
31 | +import java.io.File; | |
32 | +import java.io.FileInputStream; | |
33 | +import java.io.FileOutputStream; | |
34 | +import java.io.IOException; | |
35 | +import java.nio.channels.FileChannel; | |
36 | +import org.routine_work.notepad.provider.NoteStore; | |
37 | +import org.routine_work.utils.Log; | |
38 | + | |
39 | +public class BackupDatabaseService extends IntentService | |
40 | + implements BackupConstants | |
41 | +{ | |
42 | + | |
43 | + private static final String LOG_TAG = "simple-notepad"; | |
44 | + | |
45 | + public BackupDatabaseService(String name) | |
46 | + { | |
47 | + super(name); | |
48 | + } | |
49 | + | |
50 | + public BackupDatabaseService() | |
51 | + { | |
52 | + this("BackupDatabaseService"); | |
53 | + } | |
54 | + | |
55 | + @Override | |
56 | + protected void onHandleIntent(Intent intent) | |
57 | + { | |
58 | + String externalStorageState = Environment.getExternalStorageState(); | |
59 | + if (Environment.MEDIA_MOUNTED.equals(externalStorageState)) | |
60 | + { | |
61 | + File databaseFilePath = NoteStore.getNoteDatabasePath(this); | |
62 | + Log.d(LOG_TAG, "databaseFilePath => " + databaseFilePath); | |
63 | + | |
64 | + String backupFileName = DateFormat.format(BACKUP_FILE_DATE_FORMAT, System.currentTimeMillis()) + BACKUP_FILE_SUFFIX; | |
65 | + File backupDirPath = Environment.getExternalStorageDirectory(); | |
66 | + backupDirPath = new File(backupDirPath, BACKUP_DIR_NAME); | |
67 | + Log.d(LOG_TAG, "backupDirPath => " + backupDirPath); | |
68 | + File backupFilePath = new File(backupDirPath, backupFileName); | |
69 | + Log.d(LOG_TAG, "backupFilePath => " + backupFilePath); | |
70 | + | |
71 | + try | |
72 | + { | |
73 | + if (backupDirPath.exists() == false) | |
74 | + { | |
75 | + Log.i(LOG_TAG, "Create backup directory. backupDirPath => " + backupDirPath); | |
76 | + boolean mkdirs = backupDirPath.mkdirs(); | |
77 | + Log.d(LOG_TAG, "mkdirs => " + mkdirs); | |
78 | + } | |
79 | + | |
80 | + Log.i(LOG_TAG, "Backup database " + databaseFilePath + " to " + backupFilePath); | |
81 | + FileChannel inputChannel = new FileInputStream(databaseFilePath).getChannel(); | |
82 | + FileChannel outputChannel = new FileOutputStream(backupFilePath).getChannel(); | |
83 | + inputChannel.transferTo(0, inputChannel.size(), outputChannel); | |
84 | + inputChannel.close(); | |
85 | + outputChannel.close(); | |
86 | + } | |
87 | + catch (IOException ex) | |
88 | + { | |
89 | + Log.e(LOG_TAG, "The database file copying is failed.", ex); | |
90 | + } | |
91 | + } | |
92 | + else | |
93 | + { | |
94 | + String message = "The external storage is not mounted."; | |
95 | + Log.e(LOG_TAG, message + " externalStorageState => " + externalStorageState); | |
96 | + Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); | |
97 | + } | |
98 | + } | |
99 | + | |
100 | +} |
@@ -237,6 +237,8 @@ | ||
237 | 237 | |
238 | 238 | <service android:name=".provider.NoteDBOptimizer" /> |
239 | 239 | |
240 | + <service android:name=".prefs.BackupDatabaseService" /> | |
241 | + | |
240 | 242 | <!-- Provider : Notes & Templates Content Provider --> |
241 | 243 | <provider |
242 | 244 | android:name=".provider.NoteProvider" |
@@ -24,24 +24,16 @@ | ||
24 | 24 | package org.routine_work.notepad.prefs; |
25 | 25 | |
26 | 26 | import android.app.Activity; |
27 | +import android.content.Intent; | |
27 | 28 | import android.os.Bundle; |
28 | -import android.os.Environment; | |
29 | -import android.text.format.DateFormat; | |
30 | 29 | import android.util.Log; |
31 | 30 | import android.view.View; |
32 | 31 | import android.view.View.OnClickListener; |
33 | 32 | import android.widget.Button; |
34 | -import android.widget.Toast; | |
35 | -import java.io.File; | |
36 | -import java.io.FileInputStream; | |
37 | -import java.io.FileOutputStream; | |
38 | -import java.io.IOException; | |
39 | -import java.nio.channels.FileChannel; | |
40 | 33 | import org.routine_work.notepad.R; |
41 | -import org.routine_work.notepad.provider.NoteStore; | |
42 | 34 | |
43 | 35 | public class BackupDatabaseActivity extends Activity |
44 | - implements OnClickListener, BackupConstants | |
36 | + implements OnClickListener | |
45 | 37 | { |
46 | 38 | |
47 | 39 | private static final String LOG_TAG = "simple-notepad"; |
@@ -49,6 +41,7 @@ public class BackupDatabaseActivity extends Activity | ||
49 | 41 | @Override |
50 | 42 | protected void onCreate(Bundle savedInstanceState) |
51 | 43 | { |
44 | + Log.v(LOG_TAG, "Hello"); | |
52 | 45 | // setTheme(NotepadPreferenceUtils.getTheme(this)); |
53 | 46 | super.onCreate(savedInstanceState); |
54 | 47 | setContentView(R.layout.backup_database_activity); |
@@ -57,6 +50,7 @@ public class BackupDatabaseActivity extends Activity | ||
57 | 50 | okButton.setOnClickListener(this); |
58 | 51 | Button cancelButton = (Button) findViewById(R.id.cancel_button); |
59 | 52 | cancelButton.setOnClickListener(this); |
53 | + Log.v(LOG_TAG, "Bye"); | |
60 | 54 | } |
61 | 55 | |
62 | 56 | @Override |
@@ -66,7 +60,7 @@ public class BackupDatabaseActivity extends Activity | ||
66 | 60 | switch (id) |
67 | 61 | { |
68 | 62 | case R.id.ok_button: |
69 | - backupDatabaseFile(); | |
63 | + startBackupDatabaseService(); | |
70 | 64 | setResult(RESULT_OK); |
71 | 65 | finish(); |
72 | 66 | break; |
@@ -77,47 +71,13 @@ public class BackupDatabaseActivity extends Activity | ||
77 | 71 | } |
78 | 72 | } |
79 | 73 | |
80 | - private void backupDatabaseFile() | |
74 | + private void startBackupDatabaseService() | |
81 | 75 | { |
82 | - File databaseFilePath = NoteStore.getNoteDatabasePath(this); | |
83 | - Log.d(LOG_TAG, "databaseFilePath => " + databaseFilePath); | |
76 | + Log.v(LOG_TAG, "Hello"); | |
84 | 77 | |
85 | - String backupFileName = DateFormat.format(BACKUP_FILE_DATE_FORMAT, System.currentTimeMillis()) + BACKUP_FILE_SUFFIX; | |
86 | - File backupDirPath = Environment.getExternalStorageDirectory(); | |
87 | - backupDirPath = new File(backupDirPath, BACKUP_DIR_NAME); | |
88 | - Log.d(LOG_TAG, "backupDirPath => " + backupDirPath); | |
89 | - File backupFilePath = new File(backupDirPath, backupFileName); | |
90 | - Log.d(LOG_TAG, "backupFilePath => " + backupFilePath); | |
78 | + Intent backDatabaseIntent = new Intent(this, BackupDatabaseService.class); | |
79 | + startService(backDatabaseIntent); | |
91 | 80 | |
92 | - String externalStorageState = Environment.getExternalStorageState(); | |
93 | - if (Environment.MEDIA_MOUNTED.equals(externalStorageState)) | |
94 | - { | |
95 | - try | |
96 | - { | |
97 | - if (backupDirPath.exists() == false) | |
98 | - { | |
99 | - Log.i(LOG_TAG, "Create backup directory. backupDirPath => " + backupDirPath); | |
100 | - boolean mkdirs = backupDirPath.mkdirs(); | |
101 | - Log.d(LOG_TAG, "mkdirs => " + mkdirs); | |
102 | - } | |
103 | - | |
104 | - Log.i(LOG_TAG, "Backup database " + databaseFilePath + " to " + backupFilePath); | |
105 | - FileChannel inputChannel = new FileInputStream(databaseFilePath).getChannel(); | |
106 | - FileChannel outputChannel = new FileOutputStream(backupFilePath).getChannel(); | |
107 | - inputChannel.transferTo(0, inputChannel.size(), outputChannel); | |
108 | - inputChannel.close(); | |
109 | - outputChannel.close(); | |
110 | - } | |
111 | - catch (IOException ex) | |
112 | - { | |
113 | - Log.e(LOG_TAG, "The database file copying is failed.", ex); | |
114 | - } | |
115 | - } | |
116 | - else | |
117 | - { | |
118 | - String message = "The external storage is not mounted."; | |
119 | - Log.e(LOG_TAG, message + "externalStorageState => " + externalStorageState); | |
120 | - Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); | |
121 | - } | |
81 | + Log.v(LOG_TAG, "Bye"); | |
122 | 82 | } |
123 | 83 | } |
@@ -0,0 +1,100 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package org.routine_work.notepad.prefs; | |
25 | + | |
26 | +import android.app.IntentService; | |
27 | +import android.content.Intent; | |
28 | +import android.os.Environment; | |
29 | +import android.text.format.DateFormat; | |
30 | +import android.widget.Toast; | |
31 | +import java.io.File; | |
32 | +import java.io.FileInputStream; | |
33 | +import java.io.FileOutputStream; | |
34 | +import java.io.IOException; | |
35 | +import java.nio.channels.FileChannel; | |
36 | +import org.routine_work.notepad.provider.NoteStore; | |
37 | +import org.routine_work.utils.Log; | |
38 | + | |
39 | +public class BackupDatabaseService extends IntentService | |
40 | + implements BackupConstants | |
41 | +{ | |
42 | + | |
43 | + private static final String LOG_TAG = "simple-notepad"; | |
44 | + | |
45 | + public BackupDatabaseService(String name) | |
46 | + { | |
47 | + super(name); | |
48 | + } | |
49 | + | |
50 | + public BackupDatabaseService() | |
51 | + { | |
52 | + this("BackupDatabaseService"); | |
53 | + } | |
54 | + | |
55 | + @Override | |
56 | + protected void onHandleIntent(Intent intent) | |
57 | + { | |
58 | + String externalStorageState = Environment.getExternalStorageState(); | |
59 | + if (Environment.MEDIA_MOUNTED.equals(externalStorageState)) | |
60 | + { | |
61 | + File databaseFilePath = NoteStore.getNoteDatabasePath(this); | |
62 | + Log.d(LOG_TAG, "databaseFilePath => " + databaseFilePath); | |
63 | + | |
64 | + String backupFileName = DateFormat.format(BACKUP_FILE_DATE_FORMAT, System.currentTimeMillis()) + BACKUP_FILE_SUFFIX; | |
65 | + File backupDirPath = Environment.getExternalStorageDirectory(); | |
66 | + backupDirPath = new File(backupDirPath, BACKUP_DIR_NAME); | |
67 | + Log.d(LOG_TAG, "backupDirPath => " + backupDirPath); | |
68 | + File backupFilePath = new File(backupDirPath, backupFileName); | |
69 | + Log.d(LOG_TAG, "backupFilePath => " + backupFilePath); | |
70 | + | |
71 | + try | |
72 | + { | |
73 | + if (backupDirPath.exists() == false) | |
74 | + { | |
75 | + Log.i(LOG_TAG, "Create backup directory. backupDirPath => " + backupDirPath); | |
76 | + boolean mkdirs = backupDirPath.mkdirs(); | |
77 | + Log.d(LOG_TAG, "mkdirs => " + mkdirs); | |
78 | + } | |
79 | + | |
80 | + Log.i(LOG_TAG, "Backup database " + databaseFilePath + " to " + backupFilePath); | |
81 | + FileChannel inputChannel = new FileInputStream(databaseFilePath).getChannel(); | |
82 | + FileChannel outputChannel = new FileOutputStream(backupFilePath).getChannel(); | |
83 | + inputChannel.transferTo(0, inputChannel.size(), outputChannel); | |
84 | + inputChannel.close(); | |
85 | + outputChannel.close(); | |
86 | + } | |
87 | + catch (IOException ex) | |
88 | + { | |
89 | + Log.e(LOG_TAG, "The database file copying is failed.", ex); | |
90 | + } | |
91 | + } | |
92 | + else | |
93 | + { | |
94 | + String message = "The external storage is not mounted."; | |
95 | + Log.e(LOG_TAG, message + " externalStorageState => " + externalStorageState); | |
96 | + Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); | |
97 | + } | |
98 | + } | |
99 | + | |
100 | +} |