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 17 package com.android.documentsui; 18 19 import static com.android.documentsui.DocumentsActivity.TAG; 20 21 import android.database.AbstractCursor; 22 import android.database.Cursor; 23 import android.os.Bundle; 24 import android.provider.DocumentsContract.Document; 25 import android.util.Log; 26 27 /** 28 * Cursor wrapper that filters MIME types not matching given list. 29 */ 30 public class FilteringCursorWrapper extends AbstractCursor { 31 private final Cursor mCursor; 32 33 private final int[] mPosition; 34 private int mCount; 35 36 public FilteringCursorWrapper(Cursor cursor, String[] acceptMimes) { 37 this(cursor, acceptMimes, null, Long.MIN_VALUE); 38 } 39 40 public FilteringCursorWrapper(Cursor cursor, String[] acceptMimes, String[] rejectMimes) { 41 this(cursor, acceptMimes, rejectMimes, Long.MIN_VALUE); 42 } 43 44 public FilteringCursorWrapper( 45 Cursor cursor, String[] acceptMimes, String[] rejectMimes, long rejectBefore) { 46 mCursor = cursor; 47 48 final int count = cursor.getCount(); 49 mPosition = new int[count]; 50 51 cursor.moveToPosition(-1); 52 while (cursor.moveToNext()) { 53 final String mimeType = cursor.getString( 54 cursor.getColumnIndex(Document.COLUMN_MIME_TYPE)); 55 final long lastModified = cursor.getLong( 56 cursor.getColumnIndex(Document.COLUMN_LAST_MODIFIED)); 57 if (rejectMimes != null && MimePredicate.mimeMatches(rejectMimes, mimeType)) { 58 continue; 59 } 60 if (lastModified < rejectBefore) { 61 continue; 62 } 63 if (MimePredicate.mimeMatches(acceptMimes, mimeType)) { 64 mPosition[mCount++] = cursor.getPosition(); 65 } 66 } 67 68 Log.d(TAG, "Before filtering " + cursor.getCount() + ", after " + mCount); 69 } 70 71 @Override 72 public Bundle getExtras() { 73 return mCursor.getExtras(); 74 } 75 76 @Override 77 public void close() { 78 super.close(); 79 mCursor.close(); 80 } 81 82 @Override 83 public boolean onMove(int oldPosition, int newPosition) { 84 return mCursor.moveToPosition(mPosition[newPosition]); 85 } 86 87 @Override 88 public String[] getColumnNames() { 89 return mCursor.getColumnNames(); 90 } 91 92 @Override 93 public int getCount() { 94 return mCount; 95 } 96 97 @Override 98 public double getDouble(int column) { 99 return mCursor.getDouble(column); 100 } 101 102 @Override 103 public float getFloat(int column) { 104 return mCursor.getFloat(column); 105 } 106 107 @Override 108 public int getInt(int column) { 109 return mCursor.getInt(column); 110 } 111 112 @Override 113 public long getLong(int column) { 114 return mCursor.getLong(column); 115 } 116 117 @Override 118 public short getShort(int column) { 119 return mCursor.getShort(column); 120 } 121 122 @Override 123 public String getString(int column) { 124 return mCursor.getString(column); 125 } 126 127 @Override 128 public int getType(int column) { 129 return mCursor.getType(column); 130 } 131 132 @Override 133 public boolean isNull(int column) { 134 return mCursor.isNull(column); 135 } 136 } 137