From 9b5a1f5bf692a2c94f8e7baee7a56b50b104f61d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Blot=20=28=40U-Exp=29?= Date: Sun, 30 Nov 2014 19:59:05 +0100 Subject: [PATCH] Prepare a new sync adapter. This permit to create slow sync and fast sync queries. At this time it's same * Also added missing XML files --- AndroidManifest.xml | 24 ++++++- res/values-en/strings.xml | 25 ++++++- res/values/strings.xml | 26 ++++++- res/xml/global_preferences.xml | 16 +++++ res/xml/owncloud_account_authenticator.xml | 6 ++ res/xml/pref_data_sync.xml | 21 ++++++ res/xml/slow_sync_adapter.xml | 9 +++ res/xml/sync_adapter.xml | 9 +++ .../activities/GeneralSettingsActivity.java | 33 +++++++++ .../sync_adapters/SmsSlowSyncService.java | 68 +++++++++++++++++++ 10 files changed, 233 insertions(+), 4 deletions(-) create mode 100644 res/xml/global_preferences.xml create mode 100644 res/xml/owncloud_account_authenticator.xml create mode 100644 res/xml/pref_data_sync.xml create mode 100644 res/xml/slow_sync_adapter.xml create mode 100644 res/xml/sync_adapter.xml create mode 100644 src/fr/unix_experience/owncloud_sms/sync_adapters/SmsSlowSyncService.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index f2e74c7..2c84fc0 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -40,7 +40,7 @@ + android:process=":sync"> @@ -52,7 +52,27 @@ + android:label="@string/pref_title_slow_sync" + android:authorities="@string/account_authority"> + + + + + + + + + + + diff --git a/res/values-en/strings.xml b/res/values-en/strings.xml index 4dd6545..bb92197 100644 --- a/res/values-en/strings.xml +++ b/res/values-en/strings.xml @@ -26,7 +26,10 @@ */ --> - Sync frequency + Fast Sync + Fast Sync frequency + Secure Slow Sync + Secure Slow Sync frequency @@ -43,6 +46,7 @@ General Settings + 5 minutes 15 minutes 30 minutes 1 hour @@ -53,6 +57,7 @@ Never + 5 15 30 60 @@ -63,6 +68,24 @@ -1 + + 1 hour + 3 hours + 6 hours + 12 hours + 24 hours + Never + + + + 60 + 180 + 360 + 720 + 1440 + -1 + + Sign in diff --git a/res/values/strings.xml b/res/values/strings.xml index d9fa6c2..e9b55a0 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -31,13 +31,17 @@ ownCloud-SMS fr.unix_experience.owncloud_sms fr.unix_experience.owncloud_sms.datasync.provider + fr.unix_experience.owncloud_sms.datasync.slowsync_provider fr.unix_experience.owncloud_sms Login logo - Sync frequency + Fast Sync + Fast Sync frequency + Secure Slow Sync + Secure Slow Sync frequency Settings Synchronize now Synchronization @@ -51,6 +55,7 @@ General Settings + 5 minutes 15 minutes 30 minutes 1 hour @@ -61,6 +66,7 @@ Never + 5 15 30 60 @@ -71,6 +77,24 @@ -1 + + 1 hour + 3 hours + 6 hours + 12 hours + 24 hours + Never + + + + 60 + 180 + 360 + 720 + 1440 + -1 + + Sign in diff --git a/res/xml/global_preferences.xml b/res/xml/global_preferences.xml new file mode 100644 index 0000000..5d9862b --- /dev/null +++ b/res/xml/global_preferences.xml @@ -0,0 +1,16 @@ + + + + + + + \ No newline at end of file diff --git a/res/xml/owncloud_account_authenticator.xml b/res/xml/owncloud_account_authenticator.xml new file mode 100644 index 0000000..b793a6a --- /dev/null +++ b/res/xml/owncloud_account_authenticator.xml @@ -0,0 +1,6 @@ + \ No newline at end of file diff --git a/res/xml/pref_data_sync.xml b/res/xml/pref_data_sync.xml new file mode 100644 index 0000000..ab124c6 --- /dev/null +++ b/res/xml/pref_data_sync.xml @@ -0,0 +1,21 @@ + + + + + + diff --git a/res/xml/slow_sync_adapter.xml b/res/xml/slow_sync_adapter.xml new file mode 100644 index 0000000..08f6251 --- /dev/null +++ b/res/xml/slow_sync_adapter.xml @@ -0,0 +1,9 @@ + + \ No newline at end of file diff --git a/res/xml/sync_adapter.xml b/res/xml/sync_adapter.xml new file mode 100644 index 0000000..0f1f0f3 --- /dev/null +++ b/res/xml/sync_adapter.xml @@ -0,0 +1,9 @@ + + \ No newline at end of file diff --git a/src/fr/unix_experience/owncloud_sms/activities/GeneralSettingsActivity.java b/src/fr/unix_experience/owncloud_sms/activities/GeneralSettingsActivity.java index 3f62a6b..0c2fb4d 100644 --- a/src/fr/unix_experience/owncloud_sms/activities/GeneralSettingsActivity.java +++ b/src/fr/unix_experience/owncloud_sms/activities/GeneralSettingsActivity.java @@ -39,6 +39,7 @@ public class GeneralSettingsActivity extends PreferenceActivity { private static final boolean ALWAYS_SIMPLE_PREFS = false; static AccountManager mAccountMgr; static String mAccountAuthority; + static String mSlowSyncAccountAuthority; static String mAccountType; @Override @@ -47,6 +48,7 @@ public class GeneralSettingsActivity extends PreferenceActivity { mAccountMgr = AccountManager.get(getBaseContext()); mAccountAuthority = getString(R.string.account_authority); + mSlowSyncAccountAuthority = getString(R.string.slowsync_account_authority); mAccountType = getString(R.string.account_type); setupSimplePreferencesScreen(); } @@ -69,6 +71,7 @@ public class GeneralSettingsActivity extends PreferenceActivity { // their values. When their values change, their summaries are updated // to reflect the new value, per the Android Design guidelines. bindPreferenceSummaryToValue(findPreference("sync_frequency")); + bindPreferenceSummaryToValue(findPreference("slow_sync_frequency")); } /** {@inheritDoc} */ @@ -151,6 +154,35 @@ public class GeneralSettingsActivity extends PreferenceActivity { } } + } else if (prefKey.equals(new String("slow_sync_frequency"))) { + long syncFreq = Long.parseLong((String)value); + + // Get ownCloud SMS account list + Account[] myAccountList = mAccountMgr.getAccountsByType(mAccountType); + for (int i = 0; i < myAccountList.length; i++) { + // And get all authorities for this account + List syncList = ContentResolver.getPeriodicSyncs(myAccountList[i], mSlowSyncAccountAuthority); + + boolean foundSameSyncCycle = false; + for (int j = 0; j < syncList.size(); j++) { + PeriodicSync ps = syncList.get(i); + + if (ps.period == syncFreq && ps.extras.getInt("synctype") == 2) { + foundSameSyncCycle = true; + } + } + + if (foundSameSyncCycle == false) { + Bundle b = new Bundle(); + b.putInt("synctype", 2); + + ContentResolver.removePeriodicSync(myAccountList[i], + mSlowSyncAccountAuthority, b); + ContentResolver.addPeriodicSync(myAccountList[i], + mSlowSyncAccountAuthority, b, syncFreq * 60); + } + } + } } else { // For all other preferences, set the summary to the value's @@ -203,6 +235,7 @@ public class GeneralSettingsActivity extends PreferenceActivity { // updated to reflect the new value, per the Android Design // guidelines. bindPreferenceSummaryToValue(findPreference("sync_frequency")); + bindPreferenceSummaryToValue(findPreference("slow_sync_frequency")); } } } diff --git a/src/fr/unix_experience/owncloud_sms/sync_adapters/SmsSlowSyncService.java b/src/fr/unix_experience/owncloud_sms/sync_adapters/SmsSlowSyncService.java new file mode 100644 index 0000000..459b1a0 --- /dev/null +++ b/src/fr/unix_experience/owncloud_sms/sync_adapters/SmsSlowSyncService.java @@ -0,0 +1,68 @@ +package fr.unix_experience.owncloud_sms.sync_adapters; + +/* + * Copyright (c) 2014, Loic Blot + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; + +public class SmsSlowSyncService extends Service { + // Storage for an instance of the sync adapter + private static SmsSyncAdapter _adapter = null; + // Object to use as a thread-safe lock + private static final Object sSyncAdapterLock = new Object(); + /* + * Instantiate the sync adapter object. + */ + @Override + public void onCreate() { + /* + * Create the sync adapter as a singleton. + * Set the sync adapter as syncable + * Disallow parallel syncs + */ + synchronized (sSyncAdapterLock) { + if (_adapter == null) { + _adapter = new SmsSyncAdapter(getApplicationContext(), true); + } + } + } + /** + * Return an object that allows the system to invoke + * the sync adapter. + * + */ + @Override + public IBinder onBind(Intent intent) { + /* + * Get the object that allows external processes + * to call onPerformSync(). The object is created + * in the base class code when the SyncAdapter + * constructors call super() + */ + return _adapter.getSyncAdapterBinder(); + } +}