Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * This is an adapted version of Android's original CursorLoader |
michael@0 | 3 | * without all the ContentProvider-specific bits. |
michael@0 | 4 | * |
michael@0 | 5 | * Copyright (C) 2011 The Android Open Source Project |
michael@0 | 6 | * |
michael@0 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 8 | * you may not use this file except in compliance with the License. |
michael@0 | 9 | * You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 16 | * See the License for the specific language governing permissions and |
michael@0 | 17 | * limitations under the License. |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | package org.mozilla.gecko.home; |
michael@0 | 21 | |
michael@0 | 22 | import android.content.Context; |
michael@0 | 23 | import android.database.Cursor; |
michael@0 | 24 | import android.support.v4.content.AsyncTaskLoader; |
michael@0 | 25 | |
michael@0 | 26 | abstract class SimpleCursorLoader extends AsyncTaskLoader<Cursor> { |
michael@0 | 27 | final ForceLoadContentObserver mObserver; |
michael@0 | 28 | Cursor mCursor; |
michael@0 | 29 | |
michael@0 | 30 | public SimpleCursorLoader(Context context) { |
michael@0 | 31 | super(context); |
michael@0 | 32 | mObserver = new ForceLoadContentObserver(); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | /** |
michael@0 | 36 | * Loads the target cursor for this loader. This method is called |
michael@0 | 37 | * on a worker thread. |
michael@0 | 38 | */ |
michael@0 | 39 | protected abstract Cursor loadCursor(); |
michael@0 | 40 | |
michael@0 | 41 | /* Runs on a worker thread */ |
michael@0 | 42 | @Override |
michael@0 | 43 | public Cursor loadInBackground() { |
michael@0 | 44 | Cursor cursor = loadCursor(); |
michael@0 | 45 | |
michael@0 | 46 | if (cursor != null) { |
michael@0 | 47 | // Ensure the cursor window is filled |
michael@0 | 48 | cursor.getCount(); |
michael@0 | 49 | cursor.registerContentObserver(mObserver); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | return cursor; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | /* Runs on the UI thread */ |
michael@0 | 56 | @Override |
michael@0 | 57 | public void deliverResult(Cursor cursor) { |
michael@0 | 58 | if (isReset()) { |
michael@0 | 59 | // An async query came in while the loader is stopped |
michael@0 | 60 | if (cursor != null) { |
michael@0 | 61 | cursor.close(); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | return; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | Cursor oldCursor = mCursor; |
michael@0 | 68 | mCursor = cursor; |
michael@0 | 69 | |
michael@0 | 70 | if (isStarted()) { |
michael@0 | 71 | super.deliverResult(cursor); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) { |
michael@0 | 75 | oldCursor.close(); |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | /** |
michael@0 | 80 | * Starts an asynchronous load of the list data. When the result is ready the callbacks |
michael@0 | 81 | * will be called on the UI thread. If a previous load has been completed and is still valid |
michael@0 | 82 | * the result may be passed to the callbacks immediately. |
michael@0 | 83 | * |
michael@0 | 84 | * Must be called from the UI thread |
michael@0 | 85 | */ |
michael@0 | 86 | @Override |
michael@0 | 87 | protected void onStartLoading() { |
michael@0 | 88 | if (mCursor != null) { |
michael@0 | 89 | deliverResult(mCursor); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | if (takeContentChanged() || mCursor == null) { |
michael@0 | 93 | forceLoad(); |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | /** |
michael@0 | 98 | * Must be called from the UI thread |
michael@0 | 99 | */ |
michael@0 | 100 | @Override |
michael@0 | 101 | protected void onStopLoading() { |
michael@0 | 102 | // Attempt to cancel the current load task if possible. |
michael@0 | 103 | cancelLoad(); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | @Override |
michael@0 | 107 | public void onCanceled(Cursor cursor) { |
michael@0 | 108 | if (cursor != null && !cursor.isClosed()) { |
michael@0 | 109 | cursor.close(); |
michael@0 | 110 | } |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | @Override |
michael@0 | 114 | protected void onReset() { |
michael@0 | 115 | super.onReset(); |
michael@0 | 116 | |
michael@0 | 117 | // Ensure the loader is stopped |
michael@0 | 118 | onStopLoading(); |
michael@0 | 119 | |
michael@0 | 120 | if (mCursor != null && !mCursor.isClosed()) { |
michael@0 | 121 | mCursor.close(); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | mCursor = null; |
michael@0 | 125 | } |
michael@0 | 126 | } |