Back to project page Wardrobe_app.
The source code is released under:
Apache License
If you think the Android project Wardrobe_app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.android.busolo.apps.wardrobe.sync; /* w w w.j a va 2 s. c o m*/ import android.content.ContentProviderOperation; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.OperationApplicationException; import android.content.SharedPreferences; import android.content.SyncResult; import android.database.Cursor; import android.net.ConnectivityManager; import android.os.RemoteException; import android.preference.PreferenceManager; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import static com.android.busolo.apps.wardrobe.utils.LogUtils.makeLogTag; import static com.android.busolo.apps.wardrobe.utils.LogUtils.LOGI; import static com.android.busolo.apps.wardrobe.utils.LogUtils.LOGD; /** * Created by s-brian on 5/24/2014. */ public class SyncHelper {private static final String TAG = makeLogTag(SyncHelper.class); public static final int FLAG_SYNC_LOCAL = 0x1; public static final int FLAG_SYNC_REMOTE = 0x2; private static final int LOCAL_VERSION_CURRENT = 2013171; private Context mContext; public SyncHelper(Context context) { mContext = context; } /** * Start a sync manually using the supplied context. * * @param context The context to start the sync service under. */ public static void requestManualSync(Context context) { Context appContext = context.getApplicationContext(); Intent serviceIntent = new Intent(appContext, SyncService.class); serviceIntent.putExtra(ContentResolver.SYNC_EXTRAS_MANUAL, true); appContext.startService(serviceIntent); } /** * Loads conference information (sessions, rooms, tracks, speakers, etc.) * from a local static cache data and then syncs down data from the * Conference API. * * @param syncResult Optional {@link SyncResult} object to populate. * @throws java.io.IOException */ public void performSync(SyncResult syncResult, int flags) throws IOException { final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); final int localVersion = prefs.getInt("local_data_version", 0); // Bulk of sync work, performed by executing several fetches from // local and online sources. final ContentResolver resolver = mContext.getContentResolver(); ArrayList<ContentProviderOperation> batch = new ArrayList<ContentProviderOperation>(); LOGI(TAG, "Performing sync"); if ((flags & FLAG_SYNC_LOCAL) != 0) { final long startLocal = System.currentTimeMillis(); final boolean localParse = localVersion < LOCAL_VERSION_CURRENT; LOGD(TAG, "found localVersion=" + localVersion + " and LOCAL_VERSION_CURRENT=" + LOCAL_VERSION_CURRENT); // Only run local sync if there's a newer version of data available // than what was last locally-sync'd. if (localParse) { prefs.edit().putInt("local_data_version", LOCAL_VERSION_CURRENT).commit(); if (syncResult != null) { ++syncResult.stats.numUpdates; // TODO: better way of indicating progress? ++syncResult.stats.numEntries; } } LOGD(TAG, "Local sync took " + (System.currentTimeMillis() - startLocal) + "ms"); } } private boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService( Context.CONNECTIVITY_SERVICE); return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting(); } /** * Write the byte array directly to a file. * @throws IOException */ private void writeFile(byte[] data, File file) throws IOException { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, false)); bos.write(data); bos.close(); } }