summaryrefslogtreecommitdiffstats
path: root/src/org/codeaurora/gallery3d/ext/MovieListLoader.java
blob: 94e6afd86abb4ab53025c2cfab3717cd6d3c5a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package org.codeaurora.gallery3d.ext;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;

/**
 * Movie list loader class. It will load videos from MediaProvider database.
 * If MoviePlayer starting activity doesn't set any thing, default OrderBy will be used.
 * Default OrderBy: MediaStore.Video.Media.DATE_TAKEN + " DESC, " + MediaStore.Video.Media._ID + " DESC ";
 */
public class MovieListLoader implements IMovieListLoader {  
    private static final String TAG = "MovieListLoader";
    private static final boolean LOG = true;
    
    private MovieListFetcherTask mListTask;
    
    @Override
    public void fillVideoList(Activity activity, Intent intent, final LoaderListener l,
            IMovieItem currentMovieItem) {

        // determine if a video playlist has been passed in through the intent
        // if a playlist does exist, use that
        ArrayList<Uri> uris = intent.getParcelableArrayListExtra("EXTRA_FILE_LIST");
        if (uris != null) {
            final MovieList movieList = new MovieList();
            ContentResolver cr = activity.getContentResolver();

            for(Uri uri : uris) {
                // add currentMovieItem in its proper place in the video playlist
                // 'Next' and 'Previous' functionality in MovieListHooker is dependent on reference
                // matching currentMovieItem
                if (currentMovieItem.getOriginalUri().equals(uri)) {
                    movieList.add(currentMovieItem);
                    continue;
                }

                File videoFile = new File(uri.getPath());
                movieList.add(new MovieItem(uri, cr.getType(uri), videoFile.getName()));
            }

            // notify callback on main thread
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    l.onListLoaded(movieList);
                }
            });

            return;
        }

        // proceed with creating a playlist if one isn't found
        boolean fetechAll = false;
        if (intent.hasExtra(EXTRA_ALL_VIDEO_FOLDER)) {
            fetechAll = intent.getBooleanExtra(EXTRA_ALL_VIDEO_FOLDER, false);
        }
        //default order by
        String orderBy = MediaStore.Video.Media.DATE_TAKEN + " DESC, " + MediaStore.Video.Media._ID + " DESC ";
        if (intent.hasExtra(EXTRA_ORDERBY)) {
            orderBy = intent.getStringExtra(EXTRA_ORDERBY);
        }
        cancelList();
        mListTask = new MovieListFetcherTask(activity, fetechAll, l, orderBy);
        mListTask.execute(currentMovieItem);
        if (LOG) {
            Log.v(TAG, "fillVideoList() fetechAll=" + fetechAll + ", orderBy=" + orderBy);
        }
    }
    
    @Override
    public boolean isEnabledVideoList(Intent intent) {
        boolean enable = true;
        if (intent != null && intent.hasExtra(EXTRA_ENABLE_VIDEO_LIST)) {
            enable = intent.getBooleanExtra(EXTRA_ENABLE_VIDEO_LIST, true);
        }
        if (LOG) {
            Log.v(TAG, "isEnabledVideoList() return " + enable);
        }
        return enable;
    }
    
    @Override
    public void cancelList() {
        if (mListTask != null) {
            mListTask.cancel(true);
        }
    }

    private class MovieListFetcherTask extends AsyncTask<IMovieItem, Void, IMovieList> {
        private static final String TAG = "MovieListFetcherTask";
        private static final boolean LOG = true;
        
        // TODO comments by sunlei
//        public static final String COLUMN_STEREO_TYPE = MediaStore.Video.Media.STEREO_TYPE;
//        public static final String COLUMN_STEREO_TYPE = "STEREO_TYPE";
        
        private final ContentResolver mCr;
        private final LoaderListener mFetecherListener;
        private final boolean mFetechAll;
        private final String mOrderBy;
        
        public MovieListFetcherTask(Context context, boolean fetechAll, LoaderListener l, String orderBy) {
            mCr = context.getContentResolver();
            mFetecherListener = l;
            mFetechAll = fetechAll;
            mOrderBy = orderBy;
            if (LOG) {
                Log.v(TAG, "MovieListFetcherTask() fetechAll=" + fetechAll + ", orderBy=" + orderBy);
            }
        }
        
        @Override
        protected void onPostExecute(IMovieList params) {
            if (LOG) {
                Log.v(TAG, "onPostExecute() isCancelled()=" + isCancelled());
            }
            if (isCancelled()) {
                return;
            }
            if (mFetecherListener != null) {
                mFetecherListener.onListLoaded(params);
            }
        }
        
        @Override
        protected IMovieList doInBackground(IMovieItem... params) {
            if (LOG) {
                Log.v(TAG, "doInBackground() begin");
            }
            if (params[0] == null) {
                return null;
            }
            IMovieList movieList = null;
            Uri uri = params[0].getUri();
            String mime = params[0].getMimeType();
            if (mFetechAll) { //get all list
                if (MovieUtils.isLocalFile(uri, mime)) {
                    String uristr = String.valueOf(uri);
                    if (uristr.toLowerCase().startsWith("content://media")) {
                        //from gallery, gallery3D, videoplayer
                        long curId = Long.parseLong(uri.getPathSegments().get(3));
                        movieList = fillUriList(null, null, curId, params[0]);
                    } else if (uristr.toLowerCase().startsWith("file://")) {
                        long curId = getCursorId(uri);
                        movieList = fillUriList(null, null, curId, params[0]);
                    }
                }
            } else { //get current list
                if (MovieUtils.isLocalFile(uri, mime)) {
                    String uristr = String.valueOf(uri);
                    if (uristr.toLowerCase().startsWith("content://media")) {
                        Cursor cursor = mCr.query(uri,
                                new String[]{MediaStore.Video.Media.BUCKET_ID},
                                null, null, null);
                        long bucketId = -1;
                        if (cursor != null) {
                            if (cursor.moveToFirst()) {
                                bucketId = cursor.getLong(0);
                            }
                            cursor.close();
                        }
                        long curId = Long.parseLong(uri.getPathSegments().get(3));
                        movieList = fillUriList(MediaStore.Video.Media.BUCKET_ID + "=? ",
                                new String[]{String.valueOf(bucketId)}, curId, params[0]);
                    } else if (uristr.toLowerCase().startsWith("file://")) {
                        String data = Uri.decode(uri.toString());
                        data = data.replaceAll("'", "''");
                        String where = "_data LIKE '%" + data.replaceFirst("file:///", "") + "'";
                        Cursor cursor = mCr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                                new String[]{"_id", MediaStore.Video.Media.BUCKET_ID},
                                where, null, null);
                        long bucketId = -1;
                        long curId = -1;
                        if (cursor != null) {
                            if (cursor.moveToFirst()) {
                                curId = cursor.getLong(0);
                                bucketId = cursor.getLong(1);
                            }
                            cursor.close();
                        }
                        movieList = fillUriList(MediaStore.Video.Media.BUCKET_ID + "=? ",
                                new String[]{String.valueOf(bucketId)}, curId, params[0]);
                    }
                }
            }
            if (LOG) {
                Log.v(TAG, "doInBackground() done return " + movieList);
            }
            return movieList;
        }
        
        private IMovieList fillUriList(String where, String[] whereArgs, long curId, IMovieItem current) {
            IMovieList movieList = null;
            Cursor cursor = null;
            try {
                cursor = mCr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        new String[]{"_id", "mime_type", OpenableColumns.DISPLAY_NAME},
                        where,
                        whereArgs,
                        mOrderBy);
                boolean find = false;
                if (cursor != null && cursor.getCount() > 0) {
                    movieList = new MovieList();
                    while (cursor.moveToNext()) {
                        long id = cursor.getLong(0);
                        if (!find && id == curId) {
                            find = true;
                            movieList.add(current);
                            continue;
                        }
                        Uri uri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id);
                        String mimeType = cursor.getString(1);
                        String title = cursor.getString(2);

                        movieList.add(new MovieItem(uri, mimeType, title));
                    }
                }
            } catch (final SQLiteException e) {
                e.printStackTrace();
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
            if (LOG) {
                Log.v(TAG, "fillUriList() cursor=" + cursor + ", return " + movieList);
            }
            return movieList;
        }

        private long getCursorId(Uri uri) {
            long curId = -1;
            Cursor cursor = null;
            String data = Uri.decode(uri.toString());
            data = data.replaceAll("'", "''");
            String where = "_data LIKE '%" + data.replaceFirst("file:///", "") + "'";
            try {
                cursor = mCr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        new String[] {
                            "_id"
                        }, where, null, null);

                if (cursor != null && cursor.moveToFirst()) {
                    curId = cursor.getLong(0);
                }
            } catch (final SQLiteException e) {
                e.printStackTrace();
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
            return curId;
        }
    }
}