aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2014-07-29 18:57:59 -0700
committerScott Jackson <daneren2005@gmail.com>2014-07-29 18:57:59 -0700
commit7e88ccbb4ee8c500efa81b2cabe6c0c83a557806 (patch)
treea0a992bc831b700484677b39a433ee56a8339ac4
parent742acf47434197d430cc24bc1f10fb032ee5939b (diff)
downloaddsub-7e88ccbb4ee8c500efa81b2cabe6c0c83a557806.tar.gz
dsub-7e88ccbb4ee8c500efa81b2cabe6c0c83a557806.tar.bz2
dsub-7e88ccbb4ee8c500efa81b2cabe6c0c83a557806.zip
#384 Use SyncRequest to not wake system up when no wifi for 4.4+
-rw-r--r--src/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java38
-rw-r--r--src/github/daneren2005/dsub/util/SyncUtil.java88
2 files changed, 90 insertions, 36 deletions
diff --git a/src/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java b/src/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java
index feec5149..5dba07f1 100644
--- a/src/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java
+++ b/src/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java
@@ -63,6 +63,7 @@ import github.daneren2005.dsub.util.BackgroundTask;
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.FileUtil;
import github.daneren2005.dsub.util.SilentBackgroundTask;
+import github.daneren2005.dsub.util.SyncUtil;
import github.daneren2005.dsub.util.UserUtil;
import github.daneren2005.dsub.util.Util;
import github.daneren2005.dsub.view.ChangeLog;
@@ -300,7 +301,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity {
}
}
- createAccount();
+ SyncUtil.createAccounts(this);
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleWithFixedDelay(runnable, 0L, 1000L, TimeUnit.MILLISECONDS);
@@ -489,41 +490,6 @@ public class SubsonicFragmentActivity extends SubsonicActivity {
editor.commit();
}
- private void createAccount() {
- final Context context = this;
-
- new SilentBackgroundTask<Void>(this) {
- @Override
- protected Void doInBackground() throws Throwable {
- AccountManager accountManager = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);
- Account account = new Account(Constants.SYNC_ACCOUNT_NAME, Constants.SYNC_ACCOUNT_TYPE);
- accountManager.addAccountExplicitly(account, null, null);
-
- SharedPreferences prefs = Util.getPreferences(context);
- boolean syncEnabled = prefs.getBoolean(Constants.PREFERENCES_KEY_SYNC_ENABLED, true);
- int syncInterval = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_SYNC_INTERVAL, "60"));
-
- // Add enabled/frequency to playlist/podcasts syncing
- ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, syncEnabled);
- ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, new Bundle(), 60L * syncInterval);
- ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_PODCAST_AUTHORITY, syncEnabled);
- ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_PODCAST_AUTHORITY, new Bundle(), 60L * syncInterval);
-
- // Add for starred/recently added
- ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_STARRED_AUTHORITY, (syncEnabled && prefs.getBoolean(Constants.PREFERENCES_KEY_SYNC_STARRED, false)));
- ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_STARRED_AUTHORITY, new Bundle(), 60L * syncInterval);
- ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_MOST_RECENT_AUTHORITY, (syncEnabled && prefs.getBoolean(Constants.PREFERENCES_KEY_SYNC_MOST_RECENT, false)));
- ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_MOST_RECENT_AUTHORITY, new Bundle(), 60L * syncInterval);
- return null;
- }
-
- @Override
- protected void done(Void result) {
-
- }
- }.execute();
- }
-
private void showInfoDialog() {
if (!infoDialogDisplayed) {
infoDialogDisplayed = true;
diff --git a/src/github/daneren2005/dsub/util/SyncUtil.java b/src/github/daneren2005/dsub/util/SyncUtil.java
index 15fa2d47..83d8554a 100644
--- a/src/github/daneren2005/dsub/util/SyncUtil.java
+++ b/src/github/daneren2005/dsub/util/SyncUtil.java
@@ -1,9 +1,16 @@
package github.daneren2005.dsub.util;
+import android.accounts.Account;
+import android.accounts.AccountManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
+import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
+import android.content.SharedPreferences;
+import android.content.SyncRequest;
+import android.os.Build;
+import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import java.io.File;
@@ -23,6 +30,87 @@ public final class SyncUtil {
private static ArrayList<SyncSet> syncedPodcasts;
private static String url;
+ public static void createAccounts(final Context context) {
+ new SilentBackgroundTask<Void>(context) {
+ @Override
+ protected Void doInBackground() throws Throwable {
+ SharedPreferences prefs = Util.getPreferences(context);
+ boolean syncEnabled = prefs.getBoolean(Constants.PREFERENCES_KEY_SYNC_ENABLED, true);
+ long syncInterval = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_SYNC_INTERVAL, "60")) * 60L;
+ boolean starred = syncEnabled && prefs.getBoolean(Constants.PREFERENCES_KEY_SYNC_STARRED, false);
+ boolean recent = syncEnabled && prefs.getBoolean(Constants.PREFERENCES_KEY_SYNC_MOST_RECENT, false);
+
+ AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
+ Account account = new Account(Constants.SYNC_ACCOUNT_NAME, Constants.SYNC_ACCOUNT_TYPE);
+ accountManager.addAccountExplicitly(account, null, null);
+
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+ createAccountsImpl(context, account, syncEnabled, syncInterval, starred, recent);
+ } else {
+ createAccountsLegacy(context, account, syncEnabled, syncInterval, starred, recent);
+ }
+ return null;
+ }
+
+ @Override
+ protected void done(Void result) {
+
+ }
+ }.execute();
+ }
+
+ private static void createAccountsImpl(Context context, Account account, boolean syncEnabled, long syncInterval, boolean starred, boolean recent) {
+ // Set wiggle room at 10% instead of default 4%, doesn't really matter when it executes
+ long wiggleRoom = syncInterval / 10;
+ boolean wifiRequired = Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_SYNC_WIFI, true);
+
+ ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, syncEnabled);
+ SyncRequest playlistRequest = new SyncRequest.Builder()
+ .setSyncAdapter(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY)
+ .syncPeriodic(syncInterval, wiggleRoom)
+ .setDisallowMetered(wifiRequired)
+ .build();
+ ContentResolver.requestSync(playlistRequest);
+
+ ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_PODCAST_AUTHORITY, syncEnabled);
+ SyncRequest podcastRequest = new SyncRequest.Builder()
+ .setSyncAdapter(account, Constants.SYNC_ACCOUNT_PODCAST_AUTHORITY)
+ .syncPeriodic(syncInterval, wiggleRoom)
+ .setDisallowMetered(wifiRequired)
+ .build();
+ ContentResolver.requestSync(podcastRequest);
+
+ // Add for starred/recently added
+ ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_STARRED_AUTHORITY, starred);
+ SyncRequest starredRequest = new SyncRequest.Builder()
+ .setSyncAdapter(account, Constants.SYNC_ACCOUNT_STARRED_AUTHORITY)
+ .syncPeriodic(syncInterval, wiggleRoom)
+ .setDisallowMetered(wifiRequired)
+ .build();
+ ContentResolver.requestSync(starredRequest);
+
+ ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_MOST_RECENT_AUTHORITY, recent);
+ SyncRequest recentRequest = new SyncRequest.Builder()
+ .setSyncAdapter(account, Constants.SYNC_ACCOUNT_MOST_RECENT_AUTHORITY)
+ .syncPeriodic(syncInterval, wiggleRoom)
+ .setDisallowMetered(wifiRequired)
+ .build();
+ ContentResolver.requestSync(recentRequest);
+ }
+ private static void createAccountsLegacy(Context context, Account account, boolean syncEnabled, long syncInterval, boolean starred, boolean recent) {
+ // Add enabled/frequency to playlist/podcasts syncing
+ ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, syncEnabled);
+ ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, new Bundle(), syncInterval);
+ ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_PODCAST_AUTHORITY, syncEnabled);
+ ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_PODCAST_AUTHORITY, new Bundle(), syncInterval);
+
+ // Add for starred/recently added
+ ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_STARRED_AUTHORITY, starred);
+ ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_STARRED_AUTHORITY, new Bundle(), syncInterval);
+ ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_MOST_RECENT_AUTHORITY, recent);
+ ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_MOST_RECENT_AUTHORITY, new Bundle(), syncInterval);
+ }
+
private static void checkRestURL(Context context) {
int instance = Util.getActiveServer(context);
String newURL = Util.getRestUrl(context, null, instance, false);