diff options
Diffstat (limited to 'src/com/android/tv/util/AsyncDbTask.java')
-rw-r--r-- | src/com/android/tv/util/AsyncDbTask.java | 240 |
1 files changed, 142 insertions, 98 deletions
diff --git a/src/com/android/tv/util/AsyncDbTask.java b/src/com/android/tv/util/AsyncDbTask.java index 477412e4..60fa3018 100644 --- a/src/com/android/tv/util/AsyncDbTask.java +++ b/src/com/android/tv/util/AsyncDbTask.java @@ -27,24 +27,20 @@ import android.support.annotation.Nullable; import android.support.annotation.WorkerThread; import android.util.Log; import android.util.Range; - +import com.android.tv.TvSingletons; +import com.android.tv.common.BuildConfig; import com.android.tv.common.SoftPreconditions; -import com.android.tv.data.Channel; +import com.android.tv.data.ChannelImpl; import com.android.tv.data.Program; +import com.android.tv.data.api.Channel; import com.android.tv.dvr.data.RecordedProgram; - import java.util.ArrayList; import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.Executor; /** * {@link AsyncTask} that defaults to executing on its own single threaded Executor Service. * - * <p>Instances of this class should only be executed this using {@link - * #executeOnDbThread(Object[])}. - * * @param <Params> the type of the parameters sent to the task upon execution. * @param <Progress> the type of the progress units published during the background computation. * @param <Result> the type of the result of the background computation. @@ -54,38 +50,19 @@ public abstract class AsyncDbTask<Params, Progress, Result> private static final String TAG = "AsyncDbTask"; private static final boolean DEBUG = false; - private static final NamedThreadFactory THREAD_FACTORY = new NamedThreadFactory( - AsyncDbTask.class.getSimpleName()); - private static final ExecutorService DB_EXECUTOR = Executors - .newSingleThreadExecutor(THREAD_FACTORY); + private final Executor mExecutor; + boolean mCalledExecuteOnDbThread; - /** - * Returns the single tread executor used for DbTasks. - */ - public static ExecutorService getExecutor() { - return DB_EXECUTOR; - } - - /** - * Executes the given command at some time in the future. - * - * <p>The command will be executed by {@link #getExecutor()}. - * - * @param command the runnable task - * @throws RejectedExecutionException if this task cannot be - * accepted for execution - * @throws NullPointerException if command is null - */ - public static void executeOnDbThread(Runnable command) { - DB_EXECUTOR.execute(command); + protected AsyncDbTask(Executor mExecutor) { + this.mExecutor = mExecutor; } /** * Returns the result of a {@link ContentResolver#query(Uri, String[], String, String[], * String)}. * - * <p> {@link #doInBackground(Void...)} executes the query on call {@link #onQuery(Cursor)} - * which is implemented by subclasses. + * <p>{@link #doInBackground(Void...)} executes the query on call {@link #onQuery(Cursor)} which + * is implemented by subclasses. * * @param <Result> the type of result returned by {@link #onQuery(Cursor)} */ @@ -97,9 +74,15 @@ public abstract class AsyncDbTask<Params, Progress, Result> private final String[] mSelectionArgs; private final String mOrderBy; - - public AsyncQueryTask(ContentResolver contentResolver, Uri uri, String[] projection, - String selection, String[] selectionArgs, String orderBy) { + public AsyncQueryTask( + Executor executor, + ContentResolver contentResolver, + Uri uri, + String[] projection, + String selection, + String[] selectionArgs, + String orderBy) { + super(executor); mContentResolver = contentResolver; mUri = uri; mProjection = projection; @@ -110,13 +93,15 @@ public abstract class AsyncDbTask<Params, Progress, Result> @Override protected final Result doInBackground(Void... params) { - if (!THREAD_FACTORY.namedWithPrefix(Thread.currentThread())) { - IllegalStateException e = new IllegalStateException(this - + " should only be executed using executeOnDbThread, " - + "but it was called on thread " - + Thread.currentThread()); + if (!mCalledExecuteOnDbThread) { + IllegalStateException e = + new IllegalStateException( + this + + " should only be executed using executeOnDbThread, " + + "but it was called on thread " + + Thread.currentThread()); Log.w(TAG, e); - if (DEBUG) { + if (BuildConfig.ENG) { throw e; } } @@ -128,8 +113,9 @@ public abstract class AsyncDbTask<Params, Progress, Result> if (DEBUG) { Log.v(TAG, "Starting query for " + this); } - try (Cursor c = mContentResolver - .query(mUri, mProjection, mSelection, mSelectionArgs, mOrderBy)) { + try (Cursor c = + mContentResolver.query( + mUri, mProjection, mSelection, mSelectionArgs, mOrderBy)) { if (c != null && !isCancelled()) { Result result = onQuery(c); if (DEBUG) { @@ -147,7 +133,7 @@ public abstract class AsyncDbTask<Params, Progress, Result> return null; } } catch (Exception e) { - SoftPreconditions.warn(TAG, null, "Error querying " + this, e); + SoftPreconditions.warn(TAG, null, e, "Error querying " + this); return null; } } @@ -176,14 +162,35 @@ public abstract class AsyncDbTask<Params, Progress, Result> public abstract static class AsyncQueryListTask<T> extends AsyncQueryTask<List<T>> { private final CursorFilter mFilter; - public AsyncQueryListTask(ContentResolver contentResolver, Uri uri, String[] projection, - String selection, String[] selectionArgs, String orderBy) { - this(contentResolver, uri, projection, selection, selectionArgs, orderBy, null); + public AsyncQueryListTask( + Executor executor, + ContentResolver contentResolver, + Uri uri, + String[] projection, + String selection, + String[] selectionArgs, + String orderBy) { + this( + executor, + contentResolver, + uri, + projection, + selection, + selectionArgs, + orderBy, + null); } - public AsyncQueryListTask(ContentResolver contentResolver, Uri uri, String[] projection, - String selection, String[] selectionArgs, String orderBy, CursorFilter filter) { - super(contentResolver, uri, projection, selection, selectionArgs, orderBy); + public AsyncQueryListTask( + Executor executor, + ContentResolver contentResolver, + Uri uri, + String[] projection, + String selection, + String[] selectionArgs, + String orderBy, + CursorFilter filter) { + super(executor, contentResolver, uri, projection, selection, selectionArgs, orderBy); mFilter = filter; } @@ -228,9 +235,15 @@ public abstract class AsyncDbTask<Params, Progress, Result> */ public abstract static class AsyncQueryItemTask<T> extends AsyncQueryTask<T> { - public AsyncQueryItemTask(ContentResolver contentResolver, Uri uri, String[] projection, - String selection, String[] selectionArgs, String orderBy) { - super(contentResolver, uri, projection, selection, selectionArgs, orderBy); + public AsyncQueryItemTask( + Executor executor, + ContentResolver contentResolver, + Uri uri, + String[] projection, + String selection, + String[] selectionArgs, + String orderBy) { + super(executor, contentResolver, uri, projection, selection, selectionArgs, orderBy); } @Override @@ -251,7 +264,6 @@ public abstract class AsyncDbTask<Params, Progress, Result> } return null; } - } /** @@ -268,33 +280,55 @@ public abstract class AsyncDbTask<Params, Progress, Result> protected abstract T fromCursor(Cursor c); } - /** - * Gets an {@link List} of {@link Channel}s from {@link TvContract.Channels#CONTENT_URI}. - */ + /** Gets an {@link List} of {@link Channel}s from {@link TvContract.Channels#CONTENT_URI}. */ public abstract static class AsyncChannelQueryTask extends AsyncQueryListTask<Channel> { - public AsyncChannelQueryTask(ContentResolver contentResolver) { - super(contentResolver, TvContract.Channels.CONTENT_URI, Channel.PROJECTION, - null, null, null); + public AsyncChannelQueryTask(Executor executor, ContentResolver contentResolver) { + super( + executor, + contentResolver, + TvContract.Channels.CONTENT_URI, + ChannelImpl.PROJECTION, + null, + null, + null); } @Override protected final Channel fromCursor(Cursor c) { - return Channel.fromCursor(c); + return ChannelImpl.fromCursor(c); } } - /** - * Gets an {@link List} of {@link Program}s from {@link TvContract.Programs#CONTENT_URI}. - */ + /** Gets an {@link List} of {@link Program}s from {@link TvContract.Programs#CONTENT_URI}. */ public abstract static class AsyncProgramQueryTask extends AsyncQueryListTask<Program> { - public AsyncProgramQueryTask(ContentResolver contentResolver) { - super(contentResolver, Programs.CONTENT_URI, Program.PROJECTION, null, null, null); + public AsyncProgramQueryTask(Executor executor, ContentResolver contentResolver) { + super( + executor, + contentResolver, + Programs.CONTENT_URI, + Program.PROJECTION, + null, + null, + null); } - public AsyncProgramQueryTask(ContentResolver contentResolver, Uri uri, String selection, - String[] selectionArgs, String sortOrder, CursorFilter filter) { - super(contentResolver, uri, Program.PROJECTION, selection, selectionArgs, sortOrder, + public AsyncProgramQueryTask( + Executor executor, + ContentResolver contentResolver, + Uri uri, + String selection, + String[] selectionArgs, + String sortOrder, + CursorFilter filter) { + super( + executor, + contentResolver, + uri, + Program.PROJECTION, + selection, + selectionArgs, + sortOrder, filter); } @@ -304,13 +338,12 @@ public abstract class AsyncDbTask<Params, Progress, Result> } } - /** - * Gets an {@link List} of {@link TvContract.RecordedPrograms}s. - */ + /** Gets an {@link List} of {@link TvContract.RecordedPrograms}s. */ public abstract static class AsyncRecordedProgramQueryTask extends AsyncQueryListTask<RecordedProgram> { - public AsyncRecordedProgramQueryTask(ContentResolver contentResolver, Uri uri) { - super(contentResolver, uri, RecordedProgram.PROJECTION, null, null, null); + public AsyncRecordedProgramQueryTask( + Executor executor, ContentResolver contentResolver, Uri uri) { + super(executor, contentResolver, uri, RecordedProgram.PROJECTION, null, null, null); } @Override @@ -319,31 +352,39 @@ public abstract class AsyncDbTask<Params, Progress, Result> } } - /** - * Execute the task on the {@link #DB_EXECUTOR} thread. - */ + /** Execute the task on {@link TvSingletons#getDbExecutor()}. */ @SafeVarargs @MainThread public final void executeOnDbThread(Params... params) { - executeOnExecutor(DB_EXECUTOR, params); + mCalledExecuteOnDbThread = true; + executeOnExecutor(mExecutor, params); } /** * Gets an {@link List} of {@link Program}s for a given channel and period {@link - * TvContract#buildProgramsUriForChannel(long, long, long)}. If the {@code period} is - * {@code null}, then all the programs is queried. + * TvContract#buildProgramsUriForChannel(long, long, long)}. If the {@code period} is {@code + * null}, then all the programs is queried. */ public static class LoadProgramsForChannelTask extends AsyncProgramQueryTask { protected final Range<Long> mPeriod; protected final long mChannelId; - public LoadProgramsForChannelTask(ContentResolver contentResolver, long channelId, + public LoadProgramsForChannelTask( + Executor executor, + ContentResolver contentResolver, + long channelId, @Nullable Range<Long> period) { - super(contentResolver, period == null - ? TvContract.buildProgramsUriForChannel(channelId) - : TvContract.buildProgramsUriForChannel(channelId, period.getLower(), - period.getUpper()), - null, null, null, null); + super( + executor, + contentResolver, + period == null + ? TvContract.buildProgramsUriForChannel(channelId) + : TvContract.buildProgramsUriForChannel( + channelId, period.getLower(), period.getUpper()), + null, + null, + null, + null); mPeriod = period; mChannelId = channelId; } @@ -357,14 +398,19 @@ public abstract class AsyncDbTask<Params, Progress, Result> } } - /** - * Gets a single {@link Program} from {@link TvContract.Programs#CONTENT_URI}. - */ + /** Gets a single {@link Program} from {@link TvContract.Programs#CONTENT_URI}. */ public static class AsyncQueryProgramTask extends AsyncQueryItemTask<Program> { - public AsyncQueryProgramTask(ContentResolver contentResolver, long programId) { - super(contentResolver, TvContract.buildProgramUri(programId), Program.PROJECTION, null, - null, null); + public AsyncQueryProgramTask( + Executor executor, ContentResolver contentResolver, long programId) { + super( + executor, + contentResolver, + TvContract.buildProgramUri(programId), + Program.PROJECTION, + null, + null, + null); } @Override @@ -373,8 +419,6 @@ public abstract class AsyncDbTask<Params, Progress, Result> } } - /** - * An interface which filters the row. - */ - public interface CursorFilter extends Filter<Cursor> { } + /** An interface which filters the row. */ + public interface CursorFilter extends Filter<Cursor> {} } |