1 /* 2 * Copyright (C) 2013 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 package com.android.dreams.phototable; 17 18 import android.content.Context; 19 import android.content.SharedPreferences; 20 import android.database.Cursor; 21 22 /** 23 * Common implementation for sources that load images from a cursor. 24 */ 25 public abstract class CursorPhotoSource extends PhotoSource { 26 27 // An invalid cursor position to represent the uninitialized state. 28 protected static final int UNINITIALIZED = -1; 29 // An invalid cursor position to represent the error state. 30 protected static final int INVALID = -2; 31 CursorPhotoSource(Context context, SharedPreferences settings)32 public CursorPhotoSource(Context context, SharedPreferences settings) { 33 super(context, settings); 34 } 35 CursorPhotoSource(Context context, SharedPreferences settings, PhotoSource fallback)36 public CursorPhotoSource(Context context, SharedPreferences settings, PhotoSource fallback) { 37 super(context, settings, fallback); 38 } 39 40 @Override naturalNext(ImageData current)41 protected ImageData naturalNext(ImageData current) { 42 if (current.cursor == null || current.cursor.isClosed()) { 43 openCursor(current); 44 } 45 findPosition(current); 46 current.cursor.moveToPosition(current.position); 47 current.cursor.moveToNext(); 48 ImageData data = null; 49 if (!current.cursor.isAfterLast()) { 50 data = unpackImageData(current.cursor, null); 51 data.cursor = current.cursor; 52 data.uri = current.uri; 53 data.position = current.cursor.getPosition(); 54 } 55 return data; 56 } 57 58 @Override naturalPrevious(ImageData current)59 protected ImageData naturalPrevious(ImageData current) { 60 if (current.cursor == null || current.cursor.isClosed()) { 61 openCursor(current); 62 } 63 findPosition(current); 64 current.cursor.moveToPosition(current.position); 65 current.cursor.moveToPrevious(); 66 ImageData data = null; 67 if (!current.cursor.isBeforeFirst()) { 68 data = unpackImageData(current.cursor, null); 69 data.cursor = current.cursor; 70 data.uri = current.uri; 71 data.position = current.cursor.getPosition(); 72 } 73 return data; 74 } 75 76 @Override donePaging(ImageData current)77 protected void donePaging(ImageData current) { 78 if (current.cursor != null && !current.cursor.isClosed()) { 79 current.cursor.close(); 80 } 81 } 82 openCursor(ImageData data)83 protected abstract void openCursor(ImageData data); findPosition(ImageData data)84 protected abstract void findPosition(ImageData data); unpackImageData(Cursor cursor, ImageData data)85 protected abstract ImageData unpackImageData(Cursor cursor, ImageData data); 86 } 87 88