Back to project page AndroidShoppingList.
The source code is released under:
Apache License
If you think the Android project AndroidShoppingList listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * Copyright 2012 C. A. Fitzgerald//from w ww. ja v a 2 s. c om * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.riotopsys.shoppinglist.server.work; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.util.Log; import com.github.riotopsys.shoppinglist.AppKeys; import com.github.riotopsys.shoppinglist.model.Work; import com.github.riotopsys.shoppinglist.server.ServerInterfaceService; import com.github.riotopsys.shoppinglist.server.ServerTask; public class WorkQueue { private static class SingletonHolder { public static final WorkQueue INSTANCE = new WorkQueue(); } private static final long DELAY = 10000; private static final String TAG = null; public static WorkQueue getInstance() { return SingletonHolder.INSTANCE; } private LinkedList<Work> queue = new LinkedList<Work>(); public synchronized void offer(Context ctx, Work w) { w.vaildate(); queue.push(w); groomQueue(); updateTimer(ctx); } private void updateTimer(Context ctx) { Log.i(TAG, "kick"); AlarmManager mgr = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(ctx, ServerInterfaceService.class); i.putExtra(AppKeys.SERVER_TASK_KEY, ServerTask.PUSH_WORK); PendingIntent pi = PendingIntent.getService(ctx, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); mgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + DELAY, pi); } public synchronized List<Work> getAvailableWork() { List<Work> result = new LinkedList<Work>(queue); queue.clear(); return result; } public synchronized void reintegrateWork(List<Work> work) { LinkedList<Work> newWork = new LinkedList<Work>(queue); queue.clear(); queue.addAll(work); while (!newWork.isEmpty()) { queue.push(newWork.pollLast()); groomQueue(); } } private void groomQueue() { Work head = queue.get(0); boolean purgeHead = false; if (head.operation == Operations.CREATE_ITEM || head.operation == Operations.CREATE_LIST) { // noop conditions return; } Iterator<Work> i = queue.iterator(); i.next(); // skip first while (i.hasNext()) { Work work = i.next(); switch (head.operation) { case DELETE_ITEM: if (work.target == head.target) { if (work.operation == Operations.CREATE_ITEM) { // this item has only been created locally we can // clear the delete as well as it's not gone to the // server yet purgeHead = true; } // we've just deleted this item no need to do anything // to it i.remove(); } break; case UNSUBSCRIBE_LIST: // purge changes for this list and it's children if (work.target == head.target || (work.parent != null && work.parent == head.target)) { if (work.operation == Operations.CREATE_LIST) { // this item has only been created locally we can // clear the delete as well as it's not gone to the // server yet purgeHead = true; } // we've just deleted this item no need to do anything // to it i.remove(); } break; case FIELD_UPDATE: if (work.target == head.target && work.operation == Operations.FIELD_UPDATE && head.field.equals(work.field)) { // redundant changes to this field are removed i.remove(); } break; } } if (purgeHead) { queue.pop(); } } }