rpicms-android

Android app for the RPICMS.
git clone git://archive.git.mtrnord.blog/RpicmsTeam/rpicms-android.git
Log | Files | Refs

SyncUtils.java (4404B)


      1 package de.nordgedanken.rpicms.basicsyncadapter;
      2 
      3 /**
      4  * Created by frank on 13.10.14.
      5  */
      6 import android.accounts.Account;
      7 import android.accounts.AccountManager;
      8 import android.annotation.TargetApi;
      9 import android.content.ContentResolver;
     10 import android.content.Context;
     11 import android.os.Build;
     12 import android.os.Bundle;
     13 import android.preference.PreferenceManager;
     14 
     15 import de.nordgedanken.rpicms.common.accounts.GenericAccountService;
     16 import de.nordgedanken.rpicms.basicsyncadapter.provider.FeedContract;
     17 public class SyncUtils {
     18 
     19 
     20     /**
     21      * Static helper methods for working with the sync framework.
     22      */
     23         private static final long SYNC_FREQUENCY = 60 * 60;  // 1 hour (in seconds)
     24         private static final String CONTENT_AUTHORITY = FeedContract.CONTENT_AUTHORITY;
     25         private static final String PREF_SETUP_COMPLETE = "setup_complete";
     26         // Value below must match the account type specified in res/xml/syncadapter.xml
     27         public static final String ACCOUNT_TYPE = "de.nordgedanken.rpicms.basicsyncadapter.account";
     28 
     29         /**
     30          * Create an entry for this application in the system account list, if it isn't already there.
     31          *
     32          * @param context Context
     33          */
     34         @TargetApi(Build.VERSION_CODES.FROYO)
     35         public static void CreateSyncAccount(Context context) {
     36             boolean newAccount = false;
     37             boolean setupComplete = PreferenceManager
     38                     .getDefaultSharedPreferences(context).getBoolean(PREF_SETUP_COMPLETE, false);
     39 
     40             // Create account, if it's missing. (Either first run, or user has deleted account.)
     41             Account account = GenericAccountService.GetAccount(ACCOUNT_TYPE);
     42             AccountManager accountManager =
     43                     (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
     44             if (accountManager.addAccountExplicitly(account, null, null)) {
     45                 // Inform the system that this account supports sync
     46                 ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);
     47                 // Inform the system that this account is eligible for auto sync when the network is up
     48                 ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);
     49                 // Recommend a schedule for automatic synchronization. The system may modify this based
     50                 // on other scheduled syncs and network utilization.
     51                 ContentResolver.addPeriodicSync(
     52                         account, CONTENT_AUTHORITY, new Bundle(),SYNC_FREQUENCY);
     53                 newAccount = true;
     54             }
     55 
     56             // Schedule an initial sync if we detect problems with either our account or our local
     57             // data has been deleted. (Note that it's possible to clear app data WITHOUT affecting
     58             // the account list, so wee need to check both.)
     59             if (newAccount || !setupComplete) {
     60                 TriggerRefresh();
     61                 PreferenceManager.getDefaultSharedPreferences(context).edit()
     62                         .putBoolean(PREF_SETUP_COMPLETE, true).commit();
     63             }
     64         }
     65 
     66         /**
     67          * Helper method to trigger an immediate sync ("refresh").
     68          *
     69          * <p>This should only be used when we need to preempt the normal sync schedule. Typically, this
     70          * means the user has pressed the "refresh" button.
     71          *
     72          * Note that SYNC_EXTRAS_MANUAL will cause an immediate sync, without any optimization to
     73          * preserve battery life. If you know new data is available (perhaps via a GCM notification),
     74          * but the user is not actively waiting for that data, you should omit this flag; this will give
     75          * the OS additional freedom in scheduling your sync request.
     76          */
     77         public static void TriggerRefresh() {
     78             Bundle b = new Bundle();
     79             // Disable sync backoff and ignore sync preferences. In other words...perform sync NOW!
     80             b.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
     81             b.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
     82             ContentResolver.requestSync(
     83                     GenericAccountService.GetAccount(ACCOUNT_TYPE), // Sync account
     84                     FeedContract.CONTENT_AUTHORITY,                 // Content authority
     85                     b);                                             // Extras
     86         }
     87 
     88 }