1 /* 2 * Copyright (C) 2017 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 androidx.contentpager.content; 18 19 import static androidx.core.util.Preconditions.checkArgument; 20 21 import android.app.LoaderManager; 22 import android.app.LoaderManager.LoaderCallbacks; 23 import android.content.Context; 24 import android.content.Loader; 25 import android.database.Cursor; 26 import android.os.Bundle; 27 import android.util.Log; 28 29 import androidx.annotation.NonNull; 30 31 /** 32 * A {@link ContentPager.QueryRunner} that executes queries using a {@link LoaderManager}. 33 * Use this when preparing {@link ContentPager} to run in an Activity or Fragment scope. 34 */ 35 public final class LoaderQueryRunner implements ContentPager.QueryRunner { 36 37 private static final boolean DEBUG = false; 38 private static final String TAG = "LoaderQueryRunner"; 39 private static final String CONTENT_URI_KEY = "contentUri"; 40 41 private final Context mContext; 42 private final LoaderManager mLoaderMgr; 43 44 public LoaderQueryRunner(@NonNull Context context, @NonNull LoaderManager loaderMgr) { 45 mContext = context; 46 mLoaderMgr = loaderMgr; 47 } 48 49 @Override 50 @SuppressWarnings("unchecked") // feels spurious. But can't commit line :80 w/o this. 51 public void query(final @NonNull Query query, @NonNull final Callback callback) { 52 if (DEBUG) Log.d(TAG, "Handling query: " + query); 53 54 LoaderCallbacks callbacks = new LoaderCallbacks<Cursor>() { 55 @Override 56 public Loader<Cursor> onCreateLoader(final int id, final Bundle args) { 57 if (DEBUG) Log.i(TAG, "Loading results for query: " + query); 58 checkArgument(id == query.getId(), "Id doesn't match query id."); 59 60 return new android.content.CursorLoader(mContext) { 61 @Override 62 public Cursor loadInBackground() { 63 return callback.runQueryInBackground(query); 64 } 65 }; 66 } 67 68 @Override 69 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { 70 if (DEBUG) Log.i(TAG, "Finished loading: " + query); 71 mLoaderMgr.destroyLoader(query.getId()); 72 callback.onQueryFinished(query, cursor); 73 } 74 75 @Override 76 public void onLoaderReset(Loader<Cursor> loader) { 77 if (DEBUG) Log.w(TAG, "Ignoring loader reset for query: " + query); 78 } 79 }; 80 81 mLoaderMgr.restartLoader(query.getId(), null, callbacks); 82 } 83 84 @Override 85 public boolean isRunning(@NonNull Query query) { 86 Loader<Cursor> loader = mLoaderMgr.getLoader(query.getId()); 87 return loader != null && loader.isStarted(); 88 // Hmm, when exactly would the loader not be started? Does it imply that it will 89 // be starting at some point? 90 } 91 92 @Override 93 public void cancel(@NonNull Query query) { 94 mLoaderMgr.destroyLoader(query.getId()); 95 } 96 } 97