| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 package org.appspot.apprtc.util; | 11 package org.appspot.apprtc.util; |
| 12 | 12 |
| 13 import android.os.Handler; | 13 import android.os.Handler; |
| 14 import android.os.Looper; | 14 import android.os.Looper; |
| 15 import android.util.Log; | 15 import android.util.Log; |
| 16 | 16 |
| 17 import java.util.LinkedList; |
| 18 import java.util.List; |
| 17 import java.util.concurrent.Executor; | 19 import java.util.concurrent.Executor; |
| 18 | 20 |
| 19 /** | 21 /** |
| 20 * Looper based executor class. | 22 * Looper based executor class. |
| 21 */ | 23 */ |
| 22 public class LooperExecutor extends Thread implements Executor { | 24 public class LooperExecutor extends Thread implements Executor { |
| 23 private static final String TAG = "LooperExecutor"; | 25 private static final String TAG = "LooperExecutor"; |
| 24 // Object used to signal that looper thread has started and Handler instance | 26 // Object used to signal that looper thread has started and Handler instance |
| 25 // associated with looper thread has been allocated. | 27 // associated with looper thread has been allocated. |
| 26 private final Object looperStartedEvent = new Object(); | 28 private final Object looperStartedEvent = new Object(); |
| 29 private final List<Runnable> scheduledPeriodicRunnables = new LinkedList<Runna
ble>(); |
| 27 private Handler handler = null; | 30 private Handler handler = null; |
| 28 private boolean running = false; | 31 private boolean running = false; |
| 29 private long threadId; | 32 private long threadId; |
| 30 | 33 |
| 31 @Override | 34 @Override |
| 32 public void run() { | 35 public void run() { |
| 33 Looper.prepare(); | 36 Looper.prepare(); |
| 34 synchronized (looperStartedEvent) { | 37 synchronized (looperStartedEvent) { |
| 35 Log.d(TAG, "Looper thread started."); | 38 Log.d(TAG, "Looper thread started."); |
| 36 handler = new Handler(); | 39 handler = new Handler(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 Log.d(TAG, "Looper thread finished."); | 75 Log.d(TAG, "Looper thread finished."); |
| 73 } | 76 } |
| 74 }); | 77 }); |
| 75 } | 78 } |
| 76 | 79 |
| 77 // Checks if current thread is a looper thread. | 80 // Checks if current thread is a looper thread. |
| 78 public boolean checkOnLooperThread() { | 81 public boolean checkOnLooperThread() { |
| 79 return (Thread.currentThread().getId() == threadId); | 82 return (Thread.currentThread().getId() == threadId); |
| 80 } | 83 } |
| 81 | 84 |
| 85 public synchronized void scheduleAtFixedRate(final Runnable command, final lon
g periodMillis) { |
| 86 if (!running) { |
| 87 Log.w(TAG, "Trying to schedule task for non running executor"); |
| 88 return; |
| 89 } |
| 90 Runnable runnable = new Runnable() { |
| 91 @Override |
| 92 public void run() { |
| 93 if (running) { |
| 94 command.run(); |
| 95 if (!handler.postDelayed(this, periodMillis)) { |
| 96 Log.e(TAG, "Failed to post a delayed runnable in the chain."); |
| 97 } |
| 98 } |
| 99 } |
| 100 }; |
| 101 scheduledPeriodicRunnables.add(runnable); |
| 102 if (!handler.postDelayed(runnable, periodMillis)) { |
| 103 Log.e(TAG, "Failed to post a delayed runnable."); |
| 104 } |
| 105 } |
| 106 |
| 107 public synchronized void cancelScheduledTasks() { |
| 108 if (!running) { |
| 109 Log.w(TAG, "Trying to cancel schedule tasks for non running executor"); |
| 110 return; |
| 111 } |
| 112 |
| 113 // Stop scheduled periodic tasks. |
| 114 for (Runnable r : scheduledPeriodicRunnables) { |
| 115 handler.removeCallbacks(r); |
| 116 } |
| 117 scheduledPeriodicRunnables.clear(); |
| 118 } |
| 119 |
| 82 @Override | 120 @Override |
| 83 public synchronized void execute(final Runnable runnable) { | 121 public synchronized void execute(final Runnable runnable) { |
| 84 if (!running) { | 122 if (!running) { |
| 85 Log.w(TAG, "Running looper executor without calling requestStart()"); | 123 Log.w(TAG, "Running looper executor without calling requestStart()"); |
| 86 return; | 124 return; |
| 87 } | 125 } |
| 88 if (Thread.currentThread().getId() == threadId) { | 126 if (Thread.currentThread().getId() == threadId) { |
| 89 runnable.run(); | 127 runnable.run(); |
| 90 } else { | 128 } else { |
| 91 handler.post(runnable); | 129 handler.post(runnable); |
| 92 } | 130 } |
| 93 } | 131 } |
| 94 | 132 |
| 95 } | 133 } |
| OLD | NEW |