mobile/android/base/home/SimpleCursorLoader.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/home/SimpleCursorLoader.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,126 @@
     1.4 +/*
     1.5 + * This is an adapted version of Android's original CursorLoader
     1.6 + * without all the ContentProvider-specific bits.
     1.7 + *
     1.8 + * Copyright (C) 2011 The Android Open Source Project
     1.9 + *
    1.10 + * Licensed under the Apache License, Version 2.0 (the "License");
    1.11 + * you may not use this file except in compliance with the License.
    1.12 + * You may obtain a copy of the License at
    1.13 + *
    1.14 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing, software
    1.17 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + * See the License for the specific language governing permissions and
    1.20 + * limitations under the License.
    1.21 + */
    1.22 +
    1.23 +package org.mozilla.gecko.home;
    1.24 +
    1.25 +import android.content.Context;
    1.26 +import android.database.Cursor;
    1.27 +import android.support.v4.content.AsyncTaskLoader;
    1.28 +
    1.29 +abstract class SimpleCursorLoader extends AsyncTaskLoader<Cursor> {
    1.30 +    final ForceLoadContentObserver mObserver;
    1.31 +    Cursor mCursor;
    1.32 +
    1.33 +    public SimpleCursorLoader(Context context) {
    1.34 +        super(context);
    1.35 +        mObserver = new ForceLoadContentObserver();
    1.36 +    }
    1.37 +
    1.38 +    /**
    1.39 +     * Loads the target cursor for this loader. This method is called
    1.40 +     * on a worker thread.
    1.41 +     */
    1.42 +    protected abstract Cursor loadCursor();
    1.43 +
    1.44 +    /* Runs on a worker thread */
    1.45 +    @Override
    1.46 +    public Cursor loadInBackground() {
    1.47 +        Cursor cursor = loadCursor();
    1.48 +
    1.49 +        if (cursor != null) {
    1.50 +            // Ensure the cursor window is filled
    1.51 +            cursor.getCount();
    1.52 +            cursor.registerContentObserver(mObserver);
    1.53 +        }
    1.54 +
    1.55 +        return cursor;
    1.56 +    }
    1.57 +
    1.58 +    /* Runs on the UI thread */
    1.59 +    @Override
    1.60 +    public void deliverResult(Cursor cursor) {
    1.61 +        if (isReset()) {
    1.62 +            // An async query came in while the loader is stopped
    1.63 +            if (cursor != null) {
    1.64 +                cursor.close();
    1.65 +            }
    1.66 +
    1.67 +            return;
    1.68 +        }
    1.69 +
    1.70 +        Cursor oldCursor = mCursor;
    1.71 +        mCursor = cursor;
    1.72 +
    1.73 +        if (isStarted()) {
    1.74 +            super.deliverResult(cursor);
    1.75 +        }
    1.76 +
    1.77 +        if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
    1.78 +            oldCursor.close();
    1.79 +        }
    1.80 +    }
    1.81 +
    1.82 +    /**
    1.83 +     * Starts an asynchronous load of the list data. When the result is ready the callbacks
    1.84 +     * will be called on the UI thread. If a previous load has been completed and is still valid
    1.85 +     * the result may be passed to the callbacks immediately.
    1.86 +     *
    1.87 +     * Must be called from the UI thread
    1.88 +     */
    1.89 +    @Override
    1.90 +    protected void onStartLoading() {
    1.91 +        if (mCursor != null) {
    1.92 +            deliverResult(mCursor);
    1.93 +        }
    1.94 +
    1.95 +        if (takeContentChanged() || mCursor == null) {
    1.96 +            forceLoad();
    1.97 +        }
    1.98 +    }
    1.99 +
   1.100 +    /**
   1.101 +     * Must be called from the UI thread
   1.102 +     */
   1.103 +    @Override
   1.104 +    protected void onStopLoading() {
   1.105 +        // Attempt to cancel the current load task if possible.
   1.106 +        cancelLoad();
   1.107 +    }
   1.108 +
   1.109 +    @Override
   1.110 +    public void onCanceled(Cursor cursor) {
   1.111 +        if (cursor != null && !cursor.isClosed()) {
   1.112 +            cursor.close();
   1.113 +        }
   1.114 +    }
   1.115 +
   1.116 +    @Override
   1.117 +    protected void onReset() {
   1.118 +        super.onReset();
   1.119 +
   1.120 +        // Ensure the loader is stopped
   1.121 +        onStopLoading();
   1.122 +
   1.123 +        if (mCursor != null && !mCursor.isClosed()) {
   1.124 +            mCursor.close();
   1.125 +        }
   1.126 +
   1.127 +        mCursor = null;
   1.128 +    }
   1.129 +}
   1.130 \ No newline at end of file

mercurial