Android Open Source - Android_Sunshine_Udacity Sunshine Sync Adapter






From Project

Back to project page Android_Sunshine_Udacity.

License

The source code is released under:

MIT License

If you think the Android project Android_Sunshine_Udacity 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 com.example.talonso.sunshine.sync;
/*ww  w . j  a va  2  s .c om*/
        import android.accounts.Account;
        import android.accounts.AccountManager;
        import android.content.AbstractThreadedSyncAdapter;
        import android.content.ContentProviderClient;
        import android.content.ContentResolver;
        import android.content.Context;
        import android.content.SyncResult;
        import android.os.Bundle;
        import android.util.Log;

        import com.example.talonso.sunshine.R;

public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter {
    public final String LOG_TAG = SunshineSyncAdapter.class.getSimpleName();

    public SunshineSyncAdapter(Context context, boolean autoInitialize) {
        super(context, autoInitialize);
    }

    @Override
    public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
        Log.d(LOG_TAG, "onPerformSync Called.");

    }

    /**
     * Helper method to have the sync adapter sync immediately
     * @param context The context used to access the account service
     */
    public static void syncImmediately(Context context) {
        Bundle bundle = new Bundle();
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        ContentResolver.requestSync(getSyncAccount(context),
                context.getString(R.string.content_authority), bundle);
    }

    /**
     * Helper method to get the fake account to be used with SyncAdapter, or make a new one
     * if the fake account doesn't exist yet.  If we make a new account, we call the
     * onAccountCreated method so we can initialize things.
     *
     * @param context The context used to access the account service
     * @return a fake account.
     */
    public static Account getSyncAccount(Context context) {
        // Get an instance of the Android account manager
        AccountManager accountManager =
                (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

        // Create the account type and default account
        Account newAccount = new Account(
                context.getString(R.string.app_name), context.getString(R.string.sync_account_type));

        // If the password doesn't exist, the account doesn't exist
        if ( null == accountManager.getPassword(newAccount) ) {

        /*
         * Add the account and account type, no password or user data
         * If successful, return the Account object, otherwise report an error.
         */
            if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
                return null;
            }
            /*
             * If you don't set android:syncable="true" in
             * in your <provider> element in the manifest,
             * then call ContentResolver.setIsSyncable(account, AUTHORITY, 1)
             * here.
             */

        }
        return newAccount;
    }
}




Java Source Code List

com.example.talonso.sunshine.DetailActivity.java
com.example.talonso.sunshine.DetailFragment.java
com.example.talonso.sunshine.ForecastAdapter.java
com.example.talonso.sunshine.ForecastFragment.java
com.example.talonso.sunshine.FullTestSuite.java
com.example.talonso.sunshine.MainActivity.java
com.example.talonso.sunshine.SettingsActivity.java
com.example.talonso.sunshine.Utility.java
com.example.talonso.sunshine.data.TestDb.java
com.example.talonso.sunshine.data.TestProvider.java
com.example.talonso.sunshine.data.TestUriMatcher.java
com.example.talonso.sunshine.data.TestUtilities.java
com.example.talonso.sunshine.data.TestWeatherContract.java
com.example.talonso.sunshine.data.WeatherContract.java
com.example.talonso.sunshine.data.WeatherDbHelper.java
com.example.talonso.sunshine.data.WeatherProvider.java
com.example.talonso.sunshine.sync.SunshineAuthenticatorService.java
com.example.talonso.sunshine.sync.SunshineAuthenticator.java
com.example.talonso.sunshine.sync.SunshineSyncAdapter.java
com.example.talonso.sunshine.utils.PollingCheck.java