1386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall/* 2386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * Copyright (C) 2015 The Android Open Source Project 3386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * 4386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * Licensed under the Apache License, Version 2.0 (the "License"); 5386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * you may not use this file except in compliance with the License. 6386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * You may obtain a copy of the License at 7386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * 8386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * http://www.apache.org/licenses/LICENSE-2.0 9386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * 10386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * Unless required by applicable law or agreed to in writing, software 11386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * distributed under the License is distributed on an "AS IS" BASIS, 12386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * See the License for the specific language governing permissions and 14386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * limitations under the License. 15386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall */ 16386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall 17386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lallpackage com.android.camera.async; 18386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall 19386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lallimport com.android.camera.util.Callback; 20386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall 21386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lallimport java.util.concurrent.Executor; 22386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall 23386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lallimport javax.annotation.ParametersAreNonnullByDefault; 24386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lallimport javax.annotation.concurrent.ThreadSafe; 25386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall 26386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall/** 27386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * Wraps a not-necessarily-thread-safe callback into a thread-safe callback 28386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * which runs it on an executor. 29386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall */ 30386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall@ThreadSafe 31386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall@ParametersAreNonnullByDefault 32386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lallpublic class ExecutorCallback<T> implements Updatable<T> { 33386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall private final Callback<T> mCallback; 34386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall private final Executor mExecutor; 35386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall 36386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall /** 37386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * @param callback The callback to wrap. 38386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall * @param executor The executor on which to invoke the callback. 39386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall */ 40386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall public ExecutorCallback(Callback<T> callback, Executor executor) { 41386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall mCallback = callback; 42386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall mExecutor = executor; 43386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall } 44386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall 45386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall @Override 46386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall public void update(final T t) { 47386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall mExecutor.execute(new Runnable() { 48386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall @Override 49386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall public void run() { 50386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall mCallback.onCallback(t); 51386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall } 52386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall }); 53386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall } 54386c5b885b99f67f9c0a7380f4be153f28333089Puneet Lall} 55