|
| 1 | +/** |
| 2 | + * Copyright (C) 2010-2015 eBusiness Information, Excilys Group |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed To in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package org.androidannotations.api; |
| 17 | + |
| 18 | +import java.util.Iterator; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Map.Entry; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.WeakHashMap; |
| 23 | + |
| 24 | +import android.os.Handler; |
| 25 | + |
| 26 | +/** |
| 27 | + * This class provide operations for |
| 28 | + * {@link org.androidannotations.annotations.UiThread UiThread} tasks. |
| 29 | + */ |
| 30 | +public class UiThreadExecutor { |
| 31 | + |
| 32 | + private static final Map<Runnable, TaskInfo> TASKS = new WeakHashMap<Runnable, TaskInfo>(); |
| 33 | + |
| 34 | + private UiThreadExecutor() { |
| 35 | + // should not be instantiated |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Store a new task in the map for providing cancellation. This method is |
| 40 | + * used by AndroidAnnotations and not intended to be called by clients. |
| 41 | + * |
| 42 | + * @param id |
| 43 | + * the identifier of the task |
| 44 | + * @param task |
| 45 | + * the task itself |
| 46 | + * @param handler |
| 47 | + * the {@link Handler} which runs the task |
| 48 | + */ |
| 49 | + public static synchronized void addTask(String id, Runnable task, Handler handler) { |
| 50 | + TASKS.put(task, new TaskInfo(id, handler)); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Cancel all tasks having the specified <code>id</code>. |
| 55 | + * |
| 56 | + * @param id |
| 57 | + * the cancellation identifier |
| 58 | + */ |
| 59 | + public static synchronized void cancelAll(String id) { |
| 60 | + Set<Entry<Runnable, TaskInfo>> entrySet = TASKS.entrySet(); |
| 61 | + Iterator<Entry<Runnable, TaskInfo>> iterator = entrySet.iterator(); |
| 62 | + |
| 63 | + while (iterator.hasNext()) { |
| 64 | + Entry<Runnable, TaskInfo> next = iterator.next(); |
| 65 | + Runnable task = next.getKey(); |
| 66 | + TaskInfo info = next.getValue(); |
| 67 | + |
| 68 | + if (info.id.equals(id)) { |
| 69 | + info.handler.removeCallbacks(task); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static class TaskInfo { |
| 75 | + String id; |
| 76 | + |
| 77 | + Handler handler; |
| 78 | + |
| 79 | + TaskInfo(String id, Handler handler) { |
| 80 | + this.id = id; |
| 81 | + this.handler = handler; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments