188d80f4471c900628e2cb6eef23029b99af48e09Chris Wren/* 288d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * Copyright (C) 2013 The Android Open Source Project 388d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * 488d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * Licensed under the Apache License, Version 2.0 (the "License"); 588d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * you may not use this file except in compliance with the License. 688d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * You may obtain a copy of the License at 788d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * 888d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * http://www.apache.org/licenses/LICENSE-2.0 988d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * 1088d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * Unless required by applicable law or agreed to in writing, software 1188d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * distributed under the License is distributed on an "AS IS" BASIS, 1288d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1388d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * See the License for the specific language governing permissions and 1488d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * limitations under the License. 1588d80f4471c900628e2cb6eef23029b99af48e09Chris Wren */ 1688d80f4471c900628e2cb6eef23029b99af48e09Chris Wrenpackage com.android.dreams.phototable; 1788d80f4471c900628e2cb6eef23029b99af48e09Chris Wren 1888d80f4471c900628e2cb6eef23029b99af48e09Chris Wrenimport android.content.Context; 1988d80f4471c900628e2cb6eef23029b99af48e09Chris Wrenimport android.content.SharedPreferences; 2088d80f4471c900628e2cb6eef23029b99af48e09Chris Wrenimport android.database.Cursor; 2188d80f4471c900628e2cb6eef23029b99af48e09Chris Wren 2288d80f4471c900628e2cb6eef23029b99af48e09Chris Wren/** 2388d80f4471c900628e2cb6eef23029b99af48e09Chris Wren * Common implementation for sources that load images from a cursor. 2488d80f4471c900628e2cb6eef23029b99af48e09Chris Wren */ 2588d80f4471c900628e2cb6eef23029b99af48e09Chris Wrenpublic abstract class CursorPhotoSource extends PhotoSource { 2688d80f4471c900628e2cb6eef23029b99af48e09Chris Wren 2788d80f4471c900628e2cb6eef23029b99af48e09Chris Wren // An invalid cursor position to represent the uninitialized state. 2888d80f4471c900628e2cb6eef23029b99af48e09Chris Wren protected static final int UNINITIALIZED = -1; 2988d80f4471c900628e2cb6eef23029b99af48e09Chris Wren // An invalid cursor position to represent the error state. 3088d80f4471c900628e2cb6eef23029b99af48e09Chris Wren protected static final int INVALID = -2; 3188d80f4471c900628e2cb6eef23029b99af48e09Chris Wren 3288d80f4471c900628e2cb6eef23029b99af48e09Chris Wren public CursorPhotoSource(Context context, SharedPreferences settings) { 3388d80f4471c900628e2cb6eef23029b99af48e09Chris Wren super(context, settings); 3488d80f4471c900628e2cb6eef23029b99af48e09Chris Wren } 3588d80f4471c900628e2cb6eef23029b99af48e09Chris Wren 3688d80f4471c900628e2cb6eef23029b99af48e09Chris Wren public CursorPhotoSource(Context context, SharedPreferences settings, PhotoSource fallback) { 3788d80f4471c900628e2cb6eef23029b99af48e09Chris Wren super(context, settings, fallback); 3888d80f4471c900628e2cb6eef23029b99af48e09Chris Wren } 3988d80f4471c900628e2cb6eef23029b99af48e09Chris Wren 4088d80f4471c900628e2cb6eef23029b99af48e09Chris Wren @Override 4188d80f4471c900628e2cb6eef23029b99af48e09Chris Wren protected ImageData naturalNext(ImageData current) { 42bcfd4439d730a4d783a02596c8ab444796323aadChris Wren if (current.cursor == null || current.cursor.isClosed()) { 4388d80f4471c900628e2cb6eef23029b99af48e09Chris Wren openCursor(current); 4488d80f4471c900628e2cb6eef23029b99af48e09Chris Wren } 45bcfd4439d730a4d783a02596c8ab444796323aadChris Wren findPosition(current); 4688d80f4471c900628e2cb6eef23029b99af48e09Chris Wren current.cursor.moveToPosition(current.position); 4788d80f4471c900628e2cb6eef23029b99af48e09Chris Wren current.cursor.moveToNext(); 4888d80f4471c900628e2cb6eef23029b99af48e09Chris Wren ImageData data = null; 4988d80f4471c900628e2cb6eef23029b99af48e09Chris Wren if (!current.cursor.isAfterLast()) { 5088d80f4471c900628e2cb6eef23029b99af48e09Chris Wren data = unpackImageData(current.cursor, null); 5188d80f4471c900628e2cb6eef23029b99af48e09Chris Wren data.cursor = current.cursor; 52523231c2f20cc14305bcf5c63feb76cd9f2a6b46Viktor Yakovel data.uri = current.uri; 5388d80f4471c900628e2cb6eef23029b99af48e09Chris Wren data.position = current.cursor.getPosition(); 5488d80f4471c900628e2cb6eef23029b99af48e09Chris Wren } 5588d80f4471c900628e2cb6eef23029b99af48e09Chris Wren return data; 5688d80f4471c900628e2cb6eef23029b99af48e09Chris Wren } 5788d80f4471c900628e2cb6eef23029b99af48e09Chris Wren 5888d80f4471c900628e2cb6eef23029b99af48e09Chris Wren @Override 5988d80f4471c900628e2cb6eef23029b99af48e09Chris Wren protected ImageData naturalPrevious(ImageData current) { 60bcfd4439d730a4d783a02596c8ab444796323aadChris Wren if (current.cursor == null || current.cursor.isClosed()) { 6188d80f4471c900628e2cb6eef23029b99af48e09Chris Wren openCursor(current); 6288d80f4471c900628e2cb6eef23029b99af48e09Chris Wren } 63bcfd4439d730a4d783a02596c8ab444796323aadChris Wren findPosition(current); 6488d80f4471c900628e2cb6eef23029b99af48e09Chris Wren current.cursor.moveToPosition(current.position); 6588d80f4471c900628e2cb6eef23029b99af48e09Chris Wren current.cursor.moveToPrevious(); 6688d80f4471c900628e2cb6eef23029b99af48e09Chris Wren ImageData data = null; 6788d80f4471c900628e2cb6eef23029b99af48e09Chris Wren if (!current.cursor.isBeforeFirst()) { 6888d80f4471c900628e2cb6eef23029b99af48e09Chris Wren data = unpackImageData(current.cursor, null); 6988d80f4471c900628e2cb6eef23029b99af48e09Chris Wren data.cursor = current.cursor; 70523231c2f20cc14305bcf5c63feb76cd9f2a6b46Viktor Yakovel data.uri = current.uri; 7188d80f4471c900628e2cb6eef23029b99af48e09Chris Wren data.position = current.cursor.getPosition(); 7288d80f4471c900628e2cb6eef23029b99af48e09Chris Wren } 7388d80f4471c900628e2cb6eef23029b99af48e09Chris Wren return data; 7488d80f4471c900628e2cb6eef23029b99af48e09Chris Wren } 7588d80f4471c900628e2cb6eef23029b99af48e09Chris Wren 76bcfd4439d730a4d783a02596c8ab444796323aadChris Wren @Override 77bcfd4439d730a4d783a02596c8ab444796323aadChris Wren protected void donePaging(ImageData current) { 78bcfd4439d730a4d783a02596c8ab444796323aadChris Wren if (current.cursor != null && !current.cursor.isClosed()) { 79bcfd4439d730a4d783a02596c8ab444796323aadChris Wren current.cursor.close(); 80bcfd4439d730a4d783a02596c8ab444796323aadChris Wren } 81bcfd4439d730a4d783a02596c8ab444796323aadChris Wren } 82bcfd4439d730a4d783a02596c8ab444796323aadChris Wren 8388d80f4471c900628e2cb6eef23029b99af48e09Chris Wren protected abstract void openCursor(ImageData data); 8488d80f4471c900628e2cb6eef23029b99af48e09Chris Wren protected abstract void findPosition(ImageData data); 8588d80f4471c900628e2cb6eef23029b99af48e09Chris Wren protected abstract ImageData unpackImageData(Cursor cursor, ImageData data); 8688d80f4471c900628e2cb6eef23029b99af48e09Chris Wren} 8788d80f4471c900628e2cb6eef23029b99af48e09Chris Wren 88