Android Open Source - Locast-Core-Android Locast Simple Sync Service






From Project

Back to project page Locast-Core-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project Locast-Core-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 edu.mit.mobile.android.locast.sync;
/*w  ww  . j a  v a 2 s.  c  o  m*/
/*
 * Copyright (C) 2011-2013  MIT Mobile Experience Lab
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import java.io.IOException;
import java.util.concurrent.PriorityBlockingQueue;

import org.json.JSONException;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.content.SyncResult;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import edu.mit.mobile.android.locast.Constants;
import edu.mit.mobile.android.locast.R;
import edu.mit.mobile.android.locast.data.NoPublicPath;
import edu.mit.mobile.android.locast.data.SyncException;
import edu.mit.mobile.android.locast.net.LocastApplicationCallbacks;
import edu.mit.mobile.android.locast.net.NetworkClient;
import edu.mit.mobile.android.locast.net.NetworkProtocolException;

/**
 * <p>
 * A sync manager for {@link SyncEngine} that uses a simplified interface to let activities sync
 * content. Requests are put in a {@link PriorityBlockingQueue} and are handled in reverse
 * chronological order (most recent requests are first). This will be used when
 * {@link AbsLocastAccountSyncService} and the accounts / sync framework isn't being used.
 * </p>
 *
 * <p>
 * Requests should be made using {@link LocastSyncService#startSync(Context, Uri, boolean, Bundle)}
 * and its various permutations.
 * </p>
 *
 */
public abstract class LocastSimpleSyncService extends LocastSyncService {
    private static final String TAG = LocastSimpleSyncService.class.getSimpleName();

    public static final boolean DEBUG = Constants.DEBUG;

    private SyncEngine mSyncEngine;

    private NetworkClient mNetworkClient;

    private SyncQueueProcessor mSyncProcessor;
    private Thread mSyncThread;

    private final String mAuthority;

    private SyncableProvider mProvider;

    private final PriorityBlockingQueue<SyncItem> mPriorityQueue = new PriorityBlockingQueue<LocastSimpleSyncService.SyncItem>();

    public ContentProviderClient mContentProviderClient;

    public LocastSimpleSyncService() {

        mAuthority = getAuthority();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mNetworkClient = ((LocastApplicationCallbacks) getApplication())
                .getNetworkClientForAccount(this, null);

        mContentProviderClient = getContentResolver().acquireContentProviderClient(mAuthority);

        mProvider = (SyncableProvider) mContentProviderClient.getLocalContentProvider();

        mSyncEngine = new SyncEngine(this, mNetworkClient, mProvider);
        mSyncProcessor = new SyncQueueProcessor();
        mSyncThread = new Thread(mSyncProcessor);
        mSyncThread.start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final Uri uri = intent.getData();

        final Bundle extras = intent.getExtras();

        enqueueItem(uri, extras);

        return START_REDELIVER_INTENT;
    }

    public void enqueueItem(Uri uri, Bundle extras) {

        final boolean expedited = extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
        final boolean manual = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);

        final SyncItem i = new SyncItem(uri, extras, (expedited ? 10 : 0) + (manual ? 5 : 0));

        if (!expedited && mPriorityQueue.contains(i)) {
            Log.d(TAG, "not adding " + i + " as it's already in the sync queue");
            return;
        }

        mPriorityQueue.add(i);
        Log.d(TAG, "enqueued " + i);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSyncProcessor.stop();
        mContentProviderClient.release();
        mProvider = null;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new LocalBinder();
    }

    public class LocalBinder extends Binder {
        public LocastSimpleSyncService getBinder() {
            return LocastSimpleSyncService.this;
        }
    }

    private void showNotification() {
        final Notification n = new NotificationCompat.Builder(this)
                .setContentTitle(getString(R.string.sync_notification))
                .setTicker(getString(R.string.sync_notification))
                .setSmallIcon(android.R.drawable.stat_notify_sync)
                .setOngoing(true)
                .setContentIntent(
                        PendingIntent.getActivity(this, 0,
                                getPackageManager().getLaunchIntentForPackage(getPackageName())
                                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0)).build();
        final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        nm.notify(R.id.locast_core__sync, n);

    }

    private void clearNotification() {
        final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(R.id.locast_core__sync);
    }

    private class SyncQueueProcessor implements Runnable {

        private boolean mKeepOn = true;

        public void stop() {
            mKeepOn = false;
            Thread.currentThread().interrupt();
        }

        @Override
        public void run() {
            while (mKeepOn) {

                try {
                    showNotification();
                    final SyncResult sr = new SyncResult();
                    final SyncItem item = mPriorityQueue.take();
                    Log.d(TAG, "took " + item + " from sync queue. Syncing...");
                    mSyncEngine.sync(item.uri, null, item.extras, mContentProviderClient, sr);
                    Log.d(TAG, "finished syncing " + item);
                    Log.d(TAG, mPriorityQueue.size() + " item(s) in queue");

                } catch (final RemoteException e) {
                    Log.e(TAG, "sync error", e);
                } catch (final SyncException e) {
                    Log.e(TAG, "sync error", e);
                } catch (final JSONException e) {
                    Log.e(TAG, "sync error", e);
                } catch (final IOException e) {
                    Log.e(TAG, "sync error", e);
                } catch (final NetworkProtocolException e) {
                    Log.e(TAG, "sync error", e);
                } catch (final NoPublicPath e) {
                    Log.e(TAG, "sync error", e);
                } catch (final OperationApplicationException e) {
                    Log.e(TAG, "sync error", e);
                } catch (final InterruptedException e) {
                    Log.w(TAG, "interrupted", e);
                } finally {
                    clearNotification();
                }
            }
        }
    }

    private static class SyncItem implements Comparable<SyncItem> {
        final Uri uri;
        final Bundle extras;
        final int priority; // higher priority is more likely to occur
        final long creationTime = System.currentTimeMillis();

        public SyncItem(Uri uri, Bundle extras, int priority) {
            this.uri = uri;
            this.extras = extras;
            this.priority = priority;
        }

        @Override
        public boolean equals(Object o) {
            if (o instanceof SyncItem) {
                final SyncItem osi = (SyncItem) o;
                return this.priority == osi.priority
                        && (uri != null ? uri.equals(osi.uri) : uri != osi.uri)
                        && (extras != null ? extras.equals(osi.extras) : extras != osi.extras);
            } else {
                return false;
            }
        }

        @Override
        public int compareTo(SyncItem another) {
            if (priority == another.priority) {
                return -/* inverted */Long.valueOf(creationTime).compareTo(another.creationTime);
            } else {
                return -/* inverted */Integer.valueOf(priority).compareTo(another.priority);
            }
        }

        @Override
        public String toString() {

            return "SyncItem: uri=" + uri + ", extras=" + extras + ", priority=" + priority
                    + ", creationTime=" + creationTime;
        }
    }
}




Java Source Code List

com.beoui.geocell.GeocellLogger.java
com.beoui.geocell.GeocellQueryEngine.java
com.beoui.geocell.GeocellUtils.java
com.beoui.geocell.annotations.Geocells.java
com.beoui.geocell.annotations.Latitude.java
com.beoui.geocell.annotations.Longitude.java
com.beoui.geocell.comparator.DoubleTupleComparator.java
com.beoui.geocell.comparator.LocationComparableTuple.java
com.beoui.geocell.model.BoundingBox.java
com.beoui.geocell.model.CostFunction.java
com.beoui.geocell.model.DefaultCostFunction.java
com.beoui.geocell.model.GeocellQuery.java
com.beoui.geocell.model.LocationCapable.java
com.beoui.geocell.model.Point.java
com.beoui.geocell.model.Tuple.java
com.stackoverflow.ArrayUtils.java
com.stackoverflow.CollectionUtils.java
com.stackoverflow.MediaUtils.java
com.stackoverflow.Predicate.java
edu.mit.mobile.android.MelAndroid.java
edu.mit.mobile.android.json.JSONArrayAdapter.java
edu.mit.mobile.android.locast.Constants.java
edu.mit.mobile.android.locast.accounts.AbsAccountChangeReceiver.java
edu.mit.mobile.android.locast.accounts.AbsLocastAuthenticationService.java
edu.mit.mobile.android.locast.accounts.AbsLocastAuthenticatorActivity.java
edu.mit.mobile.android.locast.accounts.AbsLocastAuthenticator.java
edu.mit.mobile.android.locast.accounts.AbsRegisterActivity.java
edu.mit.mobile.android.locast.accounts.LogoutFragment.java
edu.mit.mobile.android.locast.app.LocastApplication.java
edu.mit.mobile.android.locast.data.AbsComment.java
edu.mit.mobile.android.locast.data.AbsResourcesSync.java
edu.mit.mobile.android.locast.data.CastMedia.java
edu.mit.mobile.android.locast.data.ImageContent.java
edu.mit.mobile.android.locast.data.JSONSyncableIdenticalChildFinder.java
edu.mit.mobile.android.locast.data.JsonSyncableItem.java
edu.mit.mobile.android.locast.data.MediaProcessingException.java
edu.mit.mobile.android.locast.data.NoPublicPath.java
edu.mit.mobile.android.locast.data.OrderedList.java
edu.mit.mobile.android.locast.data.ResourcesSync.java
edu.mit.mobile.android.locast.data.SyncException.java
edu.mit.mobile.android.locast.data.SyncItemDeletedException.java
edu.mit.mobile.android.locast.data.SyncMapException.java
edu.mit.mobile.android.locast.data.SyncMap.java
edu.mit.mobile.android.locast.data.VideoContent.java
edu.mit.mobile.android.locast.data.interfaces.AuthorableUtils.java
edu.mit.mobile.android.locast.data.interfaces.Authorable.java
edu.mit.mobile.android.locast.data.interfaces.CommentableUtils.java
edu.mit.mobile.android.locast.data.interfaces.Commentable.java
edu.mit.mobile.android.locast.data.interfaces.FavoritableUtils.java
edu.mit.mobile.android.locast.data.interfaces.Favoritable.java
edu.mit.mobile.android.locast.data.interfaces.LocatableUtils.java
edu.mit.mobile.android.locast.data.interfaces.Locatable.java
edu.mit.mobile.android.locast.data.interfaces.PrivatelyAuthorableUtils.java
edu.mit.mobile.android.locast.data.interfaces.PrivatelyAuthorable.java
edu.mit.mobile.android.locast.data.interfaces.TitledUtils.java
edu.mit.mobile.android.locast.data.interfaces.Titled.java
edu.mit.mobile.android.locast.data.tags.IdenticalTagFinder.java
edu.mit.mobile.android.locast.data.tags.TagSyncField.java
edu.mit.mobile.android.locast.data.tags.Tag.java
edu.mit.mobile.android.locast.data.tags.TaggableUtils.java
edu.mit.mobile.android.locast.data.tags.TaggableWrapper.java
edu.mit.mobile.android.locast.data.tags.Taggable.java
edu.mit.mobile.android.locast.net.ClientResponseException.java
edu.mit.mobile.android.locast.net.LocastApplicationCallbacks.java
edu.mit.mobile.android.locast.net.NetworkClient.java
edu.mit.mobile.android.locast.net.NetworkProtocolException.java
edu.mit.mobile.android.locast.sync.AbsLocastAccountSyncService.java
edu.mit.mobile.android.locast.sync.AbsMediaSync.java
edu.mit.mobile.android.locast.sync.LocastSimpleSyncService.java
edu.mit.mobile.android.locast.sync.LocastSyncService.java
edu.mit.mobile.android.locast.sync.LocastSyncStatusObserver.java
edu.mit.mobile.android.locast.sync.NotificationProgressListener.java
edu.mit.mobile.android.locast.sync.SyncColumns.java
edu.mit.mobile.android.locast.sync.SyncEngine.java
edu.mit.mobile.android.locast.sync.SyncableProvider.java
edu.mit.mobile.android.locast.sync.SyncableSimpleContentProvider.java
edu.mit.mobile.android.locast.sync.Syncable.java
edu.mit.mobile.android.locast.widget.RemoteTagsAdapter.java
edu.mit.mobile.android.locast.widget.TagButton.java
edu.mit.mobile.android.locast.widget.TagListView.java
edu.mit.mobile.android.locast.widget.TagList.java
edu.mit.mobile.android.locast.widget.TagsLoaderCallbacks.java
edu.mit.mobile.android.location.IncrementalLocator.java
se.fnord.android.layout.PredicateLayout.java