Android Open Source - cs50-final-project-android Sync Utils






From Project

Back to project page cs50-final-project-android.

License

The source code is released under:

MIT License

If you think the Android project cs50-final-project-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.cs50.recipes.util;
//from www  .j a va2s .c om
import net.cs50.recipes.accounts.AccountService;
import net.cs50.recipes.provider.RecipeContract;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

/**
 * Helper methods for working with the sync framework.
 */
public class SyncUtils {
    private static final String TAG = "SyncUtils";

    private static final long SYNC_FREQUENCY = 60 * 60; // 1 hour (in seconds)
    private static final String CONTENT_AUTHORITY = RecipeContract.CONTENT_AUTHORITY;

    private static AccountManager mAccountManager;

    private static String mAuthToken = null;

    // attach account manager for future use
    public static void attachAccountManager(AccountManager accountManager) {
        mAccountManager = accountManager;
    }

    // statically get the account manager, which is attached with above function
    public static AccountManager getAccountManager() {
        return mAccountManager;
    }

    // gets the first account with our app's ACCOUNT_TYPE
    public static Account getCurrentAccount() {
        Account[] accounts = mAccountManager.getAccountsByType(AccountService.ACCOUNT_TYPE);
        Log.i(TAG, "num accounts " + accounts.length);
        if (accounts.length > 0)
            return accounts[0];
        return null;
    }

    // make a sync account for the given application context
    public static void createSyncAccount(Context context) {

        AccountManager accountManager = (AccountManager) context
                .getSystemService(Context.ACCOUNT_SERVICE);

        // add new sync account asynchronously
        AccountManagerFuture<Bundle> future = accountManager.addAccount(
                AccountService.ACCOUNT_TYPE, AccountService.AUTH_TOKEN_TYPE, null, null,
                (Activity) context, new AccountManagerCallback<Bundle>() {
                    @Override
                    public void run(AccountManagerFuture<Bundle> future) {
                        try {
                            Bundle result = future.getResult();

                            String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME);
                            String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE);

                            Account account = new Account(accountName, accountType);

                            // this new account is syncable 
                            ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);
                            
                            // set up auto sync
                            ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);
                            ContentResolver.addPeriodicSync(account, CONTENT_AUTHORITY,
                                    new Bundle(), SYNC_FREQUENCY);

                            // first pull of data for this acount
                            triggerRefresh(account);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, null);
    }

    // run a sync now!
    public static void triggerRefresh(Account account) {
        Bundle b = new Bundle();
        b.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        b.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        ContentResolver.requestSync(account, 
                RecipeContract.CONTENT_AUTHORITY, 
                b); // extra parameters
    }

}




Java Source Code List

net.cs50.recipes.AboutFragment.java
net.cs50.recipes.BaseActivity.java
net.cs50.recipes.BaseDrawerActivity.java
net.cs50.recipes.CreateActivity.java
net.cs50.recipes.CreateDialog.java
net.cs50.recipes.MainActivity.java
net.cs50.recipes.RecipeListFragment.java
net.cs50.recipes.ViewRecipeActivity.java
net.cs50.recipes.accounts.AccountService.java
net.cs50.recipes.accounts.AuthenticatorActivity.java
net.cs50.recipes.models.Comment.java
net.cs50.recipes.models.Recipe.java
net.cs50.recipes.models.User.java
net.cs50.recipes.provider.RecipeContract.java
net.cs50.recipes.provider.RecipeProvider.java
net.cs50.recipes.sync.SyncAdapter.java
net.cs50.recipes.sync.SyncService.java
net.cs50.recipes.util.HttpHelper.java
net.cs50.recipes.util.ImageHelper.java
net.cs50.recipes.util.RecipeHelper.java
net.cs50.recipes.util.SelectionBuilder.java
net.cs50.recipes.util.SyncUtils.java