Home | History | Annotate | Download | only in provider
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.tv.dvr.provider;
     18 
     19 import android.content.Context;
     20 import android.database.Cursor;
     21 import android.os.AsyncTask;
     22 import android.support.annotation.Nullable;
     23 import com.android.tv.common.concurrent.NamedThreadFactory;
     24 import com.android.tv.dvr.data.ScheduledRecording;
     25 import com.android.tv.dvr.data.SeriesRecording;
     26 import com.android.tv.dvr.provider.DvrContract.Schedules;
     27 import com.android.tv.dvr.provider.DvrContract.SeriesRecordings;
     28 import java.util.ArrayList;
     29 import java.util.List;
     30 import java.util.concurrent.ExecutorService;
     31 import java.util.concurrent.Executors;
     32 
     33 /** {@link AsyncTask} that defaults to executing on its own single threaded Executor Service. */
     34 public abstract class AsyncDvrDbTask<Params, Progress, Result>
     35         extends AsyncTask<Params, Progress, Result> {
     36     private static final NamedThreadFactory THREAD_FACTORY =
     37             new NamedThreadFactory(AsyncDvrDbTask.class.getSimpleName());
     38     private static final ExecutorService DB_EXECUTOR =
     39             Executors.newSingleThreadExecutor(THREAD_FACTORY);
     40 
     41     private static DvrDatabaseHelper sDbHelper;
     42 
     43     private static synchronized DvrDatabaseHelper initializeDbHelper(Context context) {
     44         if (sDbHelper == null) {
     45             sDbHelper = new DvrDatabaseHelper(context.getApplicationContext());
     46         }
     47         return sDbHelper;
     48     }
     49 
     50     final Context mContext;
     51 
     52     private AsyncDvrDbTask(Context context) {
     53         mContext = context;
     54     }
     55 
     56     /** Execute the task on the {@link #DB_EXECUTOR} thread. */
     57     @SafeVarargs
     58     public final void executeOnDbThread(Params... params) {
     59         executeOnExecutor(DB_EXECUTOR, params);
     60     }
     61 
     62     @Override
     63     protected final Result doInBackground(Params... params) {
     64         initializeDbHelper(mContext);
     65         return doInDvrBackground(params);
     66     }
     67 
     68     /** Executes in the background after {@link #initializeDbHelper(Context)} */
     69     @Nullable
     70     protected abstract Result doInDvrBackground(Params... params);
     71 
     72     /** Inserts schedules. */
     73     public static class AsyncAddScheduleTask
     74             extends AsyncDvrDbTask<ScheduledRecording, Void, Void> {
     75         public AsyncAddScheduleTask(Context context) {
     76             super(context);
     77         }
     78 
     79         @Override
     80         protected final Void doInDvrBackground(ScheduledRecording... params) {
     81             sDbHelper.insertSchedules(params);
     82             return null;
     83         }
     84     }
     85 
     86     /** Update schedules. */
     87     public static class AsyncUpdateScheduleTask
     88             extends AsyncDvrDbTask<ScheduledRecording, Void, Void> {
     89         public AsyncUpdateScheduleTask(Context context) {
     90             super(context);
     91         }
     92 
     93         @Override
     94         protected final Void doInDvrBackground(ScheduledRecording... params) {
     95             sDbHelper.updateSchedules(params);
     96             return null;
     97         }
     98     }
     99 
    100     /** Delete schedules. */
    101     public static class AsyncDeleteScheduleTask
    102             extends AsyncDvrDbTask<ScheduledRecording, Void, Void> {
    103         public AsyncDeleteScheduleTask(Context context) {
    104             super(context);
    105         }
    106 
    107         @Override
    108         protected final Void doInDvrBackground(ScheduledRecording... params) {
    109             sDbHelper.deleteSchedules(params);
    110             return null;
    111         }
    112     }
    113 
    114     /** Returns all {@link ScheduledRecording}s. */
    115     public abstract static class AsyncDvrQueryScheduleTask
    116             extends AsyncDvrDbTask<Void, Void, List<ScheduledRecording>> {
    117         public AsyncDvrQueryScheduleTask(Context context) {
    118             super(context);
    119         }
    120 
    121         @Override
    122         @Nullable
    123         protected final List<ScheduledRecording> doInDvrBackground(Void... params) {
    124             if (isCancelled()) {
    125                 return null;
    126             }
    127             List<ScheduledRecording> scheduledRecordings = new ArrayList<>();
    128             try (Cursor c = sDbHelper.query(Schedules.TABLE_NAME, ScheduledRecording.PROJECTION)) {
    129                 while (c.moveToNext() && !isCancelled()) {
    130                     scheduledRecordings.add(ScheduledRecording.fromCursor(c));
    131                 }
    132             }
    133             return scheduledRecordings;
    134         }
    135     }
    136 
    137     /** Inserts series recordings. */
    138     public static class AsyncAddSeriesRecordingTask
    139             extends AsyncDvrDbTask<SeriesRecording, Void, Void> {
    140         public AsyncAddSeriesRecordingTask(Context context) {
    141             super(context);
    142         }
    143 
    144         @Override
    145         protected final Void doInDvrBackground(SeriesRecording... params) {
    146             sDbHelper.insertSeriesRecordings(params);
    147             return null;
    148         }
    149     }
    150 
    151     /** Update series recordings. */
    152     public static class AsyncUpdateSeriesRecordingTask
    153             extends AsyncDvrDbTask<SeriesRecording, Void, Void> {
    154         public AsyncUpdateSeriesRecordingTask(Context context) {
    155             super(context);
    156         }
    157 
    158         @Override
    159         protected final Void doInDvrBackground(SeriesRecording... params) {
    160             sDbHelper.updateSeriesRecordings(params);
    161             return null;
    162         }
    163     }
    164 
    165     /** Delete series recordings. */
    166     public static class AsyncDeleteSeriesRecordingTask
    167             extends AsyncDvrDbTask<SeriesRecording, Void, Void> {
    168         public AsyncDeleteSeriesRecordingTask(Context context) {
    169             super(context);
    170         }
    171 
    172         @Override
    173         protected final Void doInDvrBackground(SeriesRecording... params) {
    174             sDbHelper.deleteSeriesRecordings(params);
    175             return null;
    176         }
    177     }
    178 
    179     /** Returns all {@link SeriesRecording}s. */
    180     public abstract static class AsyncDvrQuerySeriesRecordingTask
    181             extends AsyncDvrDbTask<Void, Void, List<SeriesRecording>> {
    182         public AsyncDvrQuerySeriesRecordingTask(Context context) {
    183             super(context);
    184         }
    185 
    186         @Override
    187         @Nullable
    188         protected final List<SeriesRecording> doInDvrBackground(Void... params) {
    189             if (isCancelled()) {
    190                 return null;
    191             }
    192             List<SeriesRecording> scheduledRecordings = new ArrayList<>();
    193             try (Cursor c =
    194                     sDbHelper.query(SeriesRecordings.TABLE_NAME, SeriesRecording.PROJECTION)) {
    195                 while (c.moveToNext() && !isCancelled()) {
    196                     scheduledRecordings.add(SeriesRecording.fromCursor(c));
    197                 }
    198             }
    199             return scheduledRecordings;
    200         }
    201     }
    202 }
    203