• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 import com.android.tv.dvr.ScheduledRecording;
25 import com.android.tv.dvr.provider.DvrContract.Recordings;
26 import com.android.tv.util.NamedThreadFactory;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 
33 /**
34  * {@link AsyncTask} that defaults to executing on its own single threaded Executor Service.
35  */
36 public abstract class AsyncDvrDbTask<Params, Progress, Result>
37         extends AsyncTask<Params, Progress, Result> {
38     private static final NamedThreadFactory THREAD_FACTORY = new NamedThreadFactory(
39             AsyncDvrDbTask.class.getSimpleName());
40     private static final ExecutorService DB_EXECUTOR = Executors
41             .newSingleThreadExecutor(THREAD_FACTORY);
42 
43     private static DvrDatabaseHelper sDbHelper;
44 
initializeDbHelper(Context context)45     private static synchronized DvrDatabaseHelper initializeDbHelper(Context context) {
46         if (sDbHelper == null) {
47             sDbHelper = new DvrDatabaseHelper(context.getApplicationContext());
48         }
49         return sDbHelper;
50     }
51 
52     final Context mContext;
53 
AsyncDvrDbTask(Context context)54     private AsyncDvrDbTask(Context context) {
55         mContext = context;
56     }
57 
58     /**
59      * Execute the task on the {@link #DB_EXECUTOR} thread.
60      */
61     @SafeVarargs
executeOnDbThread(Params... params)62     public final void executeOnDbThread(Params... params) {
63         executeOnExecutor(DB_EXECUTOR, params);
64     }
65 
66     @Override
doInBackground(Params... params)67     protected final Result doInBackground(Params... params) {
68         initializeDbHelper(mContext);
69         return doInDvrBackground(params);
70     }
71 
72     /**
73      * Executes in the background after {@link #initializeDbHelper(Context)}
74      */
75     @Nullable
doInDvrBackground(Params... params)76     protected abstract Result doInDvrBackground(Params... params);
77 
78      /**
79      * Inserts recordings returning the list of recordings with id set.
80      * The id will be -1 if there was an error.
81      */
82     public abstract static class AsyncAddRecordingTask
83             extends AsyncDvrDbTask<ScheduledRecording, Void, List<ScheduledRecording>> {
84 
AsyncAddRecordingTask(Context context)85         public AsyncAddRecordingTask(Context context) {
86             super(context);
87         }
88 
89         @Override
doInDvrBackground(ScheduledRecording... params)90         protected final List<ScheduledRecording> doInDvrBackground(ScheduledRecording... params) {
91             return sDbHelper.insertRecordings(params);
92         }
93     }
94 
95     /**
96      * Update recordings.
97      *
98      * @return list of row update counts.  The count will be -1 if there was an error or 0
99      * if no match was found. The count is expected to be exactly 1 for each recording.
100      */
101     public abstract static class AsyncUpdateRecordingTask
102             extends AsyncDvrDbTask<ScheduledRecording, Void, List<Integer>> {
AsyncUpdateRecordingTask(Context context)103         public AsyncUpdateRecordingTask(Context context) {
104             super(context);
105         }
106 
107         @Override
doInDvrBackground(ScheduledRecording... params)108         protected final List<Integer> doInDvrBackground(ScheduledRecording... params) {
109             return sDbHelper.updateRecordings(params);
110         }
111     }
112 
113     /**
114      * Delete recordings.
115      *
116      * @return list of row delete counts.  The count will be -1 if there was an error or 0
117      * if no match was found. The count is expected to be exactly 1 for each recording.
118      */
119     public abstract static class AsyncDeleteRecordingTask
120             extends AsyncDvrDbTask<ScheduledRecording, Void, List<Integer>> {
AsyncDeleteRecordingTask(Context context)121         public AsyncDeleteRecordingTask(Context context) {
122             super(context);
123         }
124 
125         @Override
doInDvrBackground(ScheduledRecording... params)126         protected final List<Integer> doInDvrBackground(ScheduledRecording... params) {
127             return sDbHelper.deleteRecordings(params);
128         }
129     }
130 
131     public abstract static class AsyncDvrQueryTask
132             extends AsyncDvrDbTask<Void, Void, List<ScheduledRecording>> {
AsyncDvrQueryTask(Context context)133         public AsyncDvrQueryTask(Context context) {
134             super(context);
135         }
136 
137         @Override
138         @Nullable
doInDvrBackground(Void... params)139         protected final List<ScheduledRecording> doInDvrBackground(Void... params) {
140             if (isCancelled()) {
141                 return null;
142             }
143 
144             if (isCancelled()) {
145                 return null;
146             }
147             if (isCancelled()) {
148                 return null;
149             }
150             List<ScheduledRecording> scheduledRecordings = new ArrayList<>();
151             try (Cursor c = sDbHelper.query(Recordings.TABLE_NAME, ScheduledRecording.PROJECTION)) {
152                 while (c.moveToNext() && !isCancelled()) {
153                     scheduledRecordings.add(ScheduledRecording.fromCursor(c));
154                 }
155             }
156             return scheduledRecordings;
157         }
158     }
159 }
160