From 41ba899cb71c83955bcaa9fc25f70d2b1fe01ed5 Mon Sep 17 00:00:00 2001 From: Kenny Root Date: Sun, 23 May 2010 05:38:57 +0000 Subject: Add backup feature for API 8 git-svn-id: https://connectbot.googlecode.com/svn/trunk/connectbot@502 df292f66-193f-0410-a5fc-6d59da041ff2 --- src/org/connectbot/service/BackupAgent.java | 73 ++++++++++++++++++++++++ src/org/connectbot/service/BackupWrapper.java | 71 +++++++++++++++++++++++ src/org/connectbot/util/HostDatabase.java | 4 +- src/org/connectbot/util/OnDbWrittenListener.java | 26 +++++++++ src/org/connectbot/util/PreferenceConstants.java | 5 ++ 5 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 src/org/connectbot/service/BackupAgent.java create mode 100644 src/org/connectbot/service/BackupWrapper.java create mode 100644 src/org/connectbot/util/OnDbWrittenListener.java (limited to 'src/org') diff --git a/src/org/connectbot/service/BackupAgent.java b/src/org/connectbot/service/BackupAgent.java new file mode 100644 index 0000000..1e3bd81 --- /dev/null +++ b/src/org/connectbot/service/BackupAgent.java @@ -0,0 +1,73 @@ +/* + * ConnectBot: simple, powerful, open-source SSH client for Android + * Copyright 2010 Kenny Root, Jeffrey Sharkey + * + * 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 org.connectbot.service; + +import java.io.IOException; + +import org.connectbot.util.HostDatabase; +import org.connectbot.util.PreferenceConstants; +import org.connectbot.util.PubkeyDatabase; + +import android.app.backup.BackupAgentHelper; +import android.app.backup.BackupDataInput; +import android.app.backup.BackupDataOutput; +import android.app.backup.FileBackupHelper; +import android.app.backup.SharedPreferencesBackupHelper; +import android.os.ParcelFileDescriptor; +import android.util.Log; + +/** + * @author kroot + * + */ +public class BackupAgent extends BackupAgentHelper { + @Override + public void onCreate() { + Log.d("ConnectBot.BackupAgent", "onCreate called"); + + SharedPreferencesBackupHelper prefs = new SharedPreferencesBackupHelper(this, getPackageName() + "_preferences"); + addHelper(PreferenceConstants.BACKUP_PREF_KEY, prefs); + + FileBackupHelper hosts = new FileBackupHelper(this, "../databases/" + HostDatabase.DB_NAME); + addHelper(HostDatabase.DB_NAME, hosts); + + FileBackupHelper pubkeys = new FileBackupHelper(this, "../databases/" + PubkeyDatabase.DB_NAME); + addHelper(PubkeyDatabase.DB_NAME, pubkeys); + + } + + @Override + public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, + ParcelFileDescriptor newState) throws IOException { + synchronized (HostDatabase.dbLock) { + super.onBackup(oldState, data, newState); + } + } + + @Override + public void onRestore(BackupDataInput data, int appVersionCode, + ParcelFileDescriptor newState) throws IOException { + Log.d("ConnectBot.BackupAgent", "onRestore called"); + + synchronized (HostDatabase.dbLock) { + Log.d("ConnectBot.BackupAgent", "onRestore in-lock"); + + super.onRestore(data, appVersionCode, newState); + } + } +} diff --git a/src/org/connectbot/service/BackupWrapper.java b/src/org/connectbot/service/BackupWrapper.java new file mode 100644 index 0000000..bfc7535 --- /dev/null +++ b/src/org/connectbot/service/BackupWrapper.java @@ -0,0 +1,71 @@ +/* + * ConnectBot: simple, powerful, open-source SSH client for Android + * Copyright 2010 Kenny Root, Jeffrey Sharkey + * + * 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 org.connectbot.service; + +import org.connectbot.util.PreferenceConstants; + +import android.app.backup.BackupManager; +import android.content.Context; + +/** + * @author kroot + * + */ +public abstract class BackupWrapper { + public static BackupWrapper getInstance() { + if (PreferenceConstants.PRE_FROYO) + return PreFroyo.Holder.sInstance; + else + return FroyoAndBeyond.Holder.sInstance; + } + + public abstract void onDataChanged(Context context); + + private static class PreFroyo extends BackupWrapper { + private static class Holder { + private static final PreFroyo sInstance = new PreFroyo(); + } + + @Override + public void onDataChanged(Context context) { + // do nothing for now + } + } + + private static class FroyoAndBeyond extends BackupWrapper { + private static class Holder { + private static final FroyoAndBeyond sInstance = new FroyoAndBeyond(); + } + + private static BackupManager mBackupManager; + + @Override + public void onDataChanged(Context context) { + checkBackupManager(context); + if (mBackupManager != null) { + mBackupManager.dataChanged(); + } + } + + private void checkBackupManager(Context context) { + if (mBackupManager == null) { + mBackupManager = new BackupManager(context); + } + } + } +} diff --git a/src/org/connectbot/util/HostDatabase.java b/src/org/connectbot/util/HostDatabase.java index bd567ce..b0dce6d 100644 --- a/src/org/connectbot/util/HostDatabase.java +++ b/src/org/connectbot/util/HostDatabase.java @@ -134,14 +134,12 @@ public class HostDatabase extends RobustSQLiteOpenHelper { addIndexName(TABLE_COLOR_DEFAULTS + FIELD_COLOR_SCHEME + "index"); } - private Object dbLock; + public static final Object[] dbLock = new Object[0]; public HostDatabase(Context context) { super(context, DB_NAME, null, DB_VERSION); getWritableDatabase().close(); - - dbLock = new Object(); } @Override diff --git a/src/org/connectbot/util/OnDbWrittenListener.java b/src/org/connectbot/util/OnDbWrittenListener.java new file mode 100644 index 0000000..ef33797 --- /dev/null +++ b/src/org/connectbot/util/OnDbWrittenListener.java @@ -0,0 +1,26 @@ +/* + * ConnectBot: simple, powerful, open-source SSH client for Android + * Copyright 2010 Kenny Root, Jeffrey Sharkey + * + * 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 org.connectbot.util; + +/** + * @author kroot + * + */ +public interface OnDbWrittenListener { + public void onDbWritten(); +} diff --git a/src/org/connectbot/util/PreferenceConstants.java b/src/org/connectbot/util/PreferenceConstants.java index ee1b95a..37e1871 100644 --- a/src/org/connectbot/util/PreferenceConstants.java +++ b/src/org/connectbot/util/PreferenceConstants.java @@ -25,6 +25,8 @@ import android.os.Build; */ public class PreferenceConstants { public static final boolean PRE_ECLAIR = (Integer.parseInt(Build.VERSION.SDK) <= 4); + public static final boolean PRE_FROYO = PRE_ECLAIR ? true : + (Build.VERSION.SDK_INT <= 7); public static final String MEMKEYS = "memkeys"; public static final String UPDATE = "update"; @@ -77,4 +79,7 @@ public class PreferenceConstants { public static final float DEFAULT_BELL_VOLUME = 0.25f; public static final String CONNECTION_PERSIST = "connPersist"; + + /* Backup identifiers */ + public static final String BACKUP_PREF_KEY = "prefs"; } -- cgit v1.2.3