• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Simple Notepad Application for Android OS


Commit MetaInfo

Revisionc103a2a1b60ab43e839932ee9c9999c4bf3ebcee (tree)
Time2014-07-12 16:15:55
AuthorMasahiko, SAWAI <say@user...>
CommiterMasahiko, SAWAI

Log Message

Database backup process is now as a service

Change Summary

Incremental Difference

--- a/notepad-app-level-11/AndroidManifest.xml
+++ b/notepad-app-level-11/AndroidManifest.xml
@@ -239,6 +239,8 @@
239239
240240 <service android:name=".provider.NoteDBOptimizer" />
241241
242+ <service android:name=".prefs.BackupDatabaseService" />
243+
242244 <!-- Provider : Notes & Templates Content Provider -->
243245 <provider
244246 android:name=".provider.NoteProvider"
--- a/notepad-app-level-11/src/org/routine_work/notepad/prefs/BackupDatabaseActivity.java
+++ b/notepad-app-level-11/src/org/routine_work/notepad/prefs/BackupDatabaseActivity.java
@@ -24,22 +24,13 @@
2424 package org.routine_work.notepad.prefs;
2525
2626 import android.app.Activity;
27-import android.media.MediaScannerConnection;
27+import android.content.Intent;
2828 import android.os.Bundle;
29-import android.os.Environment;
30-import android.text.format.DateFormat;
3129 import android.util.Log;
3230 import android.view.View;
3331 import android.view.View.OnClickListener;
3432 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;
4133 import org.routine_work.notepad.R;
42-import org.routine_work.notepad.provider.NoteStore;
4334
4435 public class BackupDatabaseActivity extends Activity
4536 implements OnClickListener, BackupConstants
@@ -50,6 +41,7 @@ public class BackupDatabaseActivity extends Activity
5041 @Override
5142 protected void onCreate(Bundle savedInstanceState)
5243 {
44+ Log.v(LOG_TAG, "Hello");
5345 setTheme(NotepadPreferenceUtils.getTheme(this));
5446 super.onCreate(savedInstanceState);
5547 setContentView(R.layout.backup_database_activity);
@@ -58,6 +50,7 @@ public class BackupDatabaseActivity extends Activity
5850 okButton.setOnClickListener(this);
5951 Button cancelButton = (Button) findViewById(R.id.cancel_button);
6052 cancelButton.setOnClickListener(this);
53+ Log.v(LOG_TAG, "Bye");
6154 }
6255
6356 @Override
@@ -67,7 +60,7 @@ public class BackupDatabaseActivity extends Activity
6760 switch (id)
6861 {
6962 case R.id.ok_button:
70- backupDatabaseFile();
63+ startBackupDatabaseService();
7164 setResult(RESULT_OK);
7265 finish();
7366 break;
@@ -78,52 +71,13 @@ public class BackupDatabaseActivity extends Activity
7871 }
7972 }
8073
81- private void backupDatabaseFile()
74+ private void startBackupDatabaseService()
8275 {
83- File databaseFilePath = NoteStore.getNoteDatabasePath(this);
84- Log.d(LOG_TAG, "databaseFilePath => " + databaseFilePath);
76+ Log.v(LOG_TAG, "Hello");
8577
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);
9280
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");
12882 }
12983 }
--- /dev/null
+++ b/notepad-app-level-11/src/org/routine_work/notepad/prefs/BackupDatabaseService.java
@@ -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+}
--- a/notepad-app/AndroidManifest.xml
+++ b/notepad-app/AndroidManifest.xml
@@ -237,6 +237,8 @@
237237
238238 <service android:name=".provider.NoteDBOptimizer" />
239239
240+ <service android:name=".prefs.BackupDatabaseService" />
241+
240242 <!-- Provider : Notes & Templates Content Provider -->
241243 <provider
242244 android:name=".provider.NoteProvider"
--- a/notepad-app/src/org/routine_work/notepad/prefs/BackupDatabaseActivity.java
+++ b/notepad-app/src/org/routine_work/notepad/prefs/BackupDatabaseActivity.java
@@ -24,24 +24,16 @@
2424 package org.routine_work.notepad.prefs;
2525
2626 import android.app.Activity;
27+import android.content.Intent;
2728 import android.os.Bundle;
28-import android.os.Environment;
29-import android.text.format.DateFormat;
3029 import android.util.Log;
3130 import android.view.View;
3231 import android.view.View.OnClickListener;
3332 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;
4033 import org.routine_work.notepad.R;
41-import org.routine_work.notepad.provider.NoteStore;
4234
4335 public class BackupDatabaseActivity extends Activity
44- implements OnClickListener, BackupConstants
36+ implements OnClickListener
4537 {
4638
4739 private static final String LOG_TAG = "simple-notepad";
@@ -49,6 +41,7 @@ public class BackupDatabaseActivity extends Activity
4941 @Override
5042 protected void onCreate(Bundle savedInstanceState)
5143 {
44+ Log.v(LOG_TAG, "Hello");
5245 // setTheme(NotepadPreferenceUtils.getTheme(this));
5346 super.onCreate(savedInstanceState);
5447 setContentView(R.layout.backup_database_activity);
@@ -57,6 +50,7 @@ public class BackupDatabaseActivity extends Activity
5750 okButton.setOnClickListener(this);
5851 Button cancelButton = (Button) findViewById(R.id.cancel_button);
5952 cancelButton.setOnClickListener(this);
53+ Log.v(LOG_TAG, "Bye");
6054 }
6155
6256 @Override
@@ -66,7 +60,7 @@ public class BackupDatabaseActivity extends Activity
6660 switch (id)
6761 {
6862 case R.id.ok_button:
69- backupDatabaseFile();
63+ startBackupDatabaseService();
7064 setResult(RESULT_OK);
7165 finish();
7266 break;
@@ -77,47 +71,13 @@ public class BackupDatabaseActivity extends Activity
7771 }
7872 }
7973
80- private void backupDatabaseFile()
74+ private void startBackupDatabaseService()
8175 {
82- File databaseFilePath = NoteStore.getNoteDatabasePath(this);
83- Log.d(LOG_TAG, "databaseFilePath => " + databaseFilePath);
76+ Log.v(LOG_TAG, "Hello");
8477
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);
9180
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");
12282 }
12383 }
--- /dev/null
+++ b/notepad-app/src/org/routine_work/notepad/prefs/BackupDatabaseService.java
@@ -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+}