Home | History | Annotate | Download | only in documentsui
      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 android.database.AbstractCursor;
     20 import android.database.Cursor;
     21 import android.os.Bundle;
     22 
     23 /**
     24  * Cursor wrapper that adds columns to identify which root a document came from.
     25  */
     26 public class RootCursorWrapper extends AbstractCursor {
     27     private final String mAuthority;
     28     private final String mRootId;
     29 
     30     private final Cursor mCursor;
     31     private final int mCount;
     32 
     33     private final String[] mColumnNames;
     34 
     35     private final int mAuthorityIndex;
     36     private final int mRootIdIndex;
     37 
     38     public static final String COLUMN_AUTHORITY = "android:authority";
     39     public static final String COLUMN_ROOT_ID = "android:rootId";
     40 
     41     public RootCursorWrapper(String authority, String rootId, Cursor cursor, int maxCount) {
     42         mAuthority = authority;
     43         mRootId = rootId;
     44         mCursor = cursor;
     45 
     46         final int count = cursor.getCount();
     47         if (maxCount > 0 && count > maxCount) {
     48             mCount = maxCount;
     49         } else {
     50             mCount = count;
     51         }
     52 
     53         if (cursor.getColumnIndex(COLUMN_AUTHORITY) != -1
     54                 || cursor.getColumnIndex(COLUMN_ROOT_ID) != -1) {
     55             throw new IllegalArgumentException("Cursor contains internal columns!");
     56         }
     57         final String[] before = cursor.getColumnNames();
     58         mColumnNames = new String[before.length + 2];
     59         System.arraycopy(before, 0, mColumnNames, 0, before.length);
     60         mAuthorityIndex = before.length;
     61         mRootIdIndex = before.length + 1;
     62         mColumnNames[mAuthorityIndex] = COLUMN_AUTHORITY;
     63         mColumnNames[mRootIdIndex] = COLUMN_ROOT_ID;
     64     }
     65 
     66     @Override
     67     public Bundle getExtras() {
     68         return mCursor.getExtras();
     69     }
     70 
     71     @Override
     72     public void close() {
     73         super.close();
     74         mCursor.close();
     75     }
     76 
     77     @Override
     78     public boolean onMove(int oldPosition, int newPosition) {
     79         return mCursor.moveToPosition(newPosition);
     80     }
     81 
     82     @Override
     83     public String[] getColumnNames() {
     84         return mColumnNames;
     85     }
     86 
     87     @Override
     88     public int getCount() {
     89         return mCount;
     90     }
     91 
     92     @Override
     93     public double getDouble(int column) {
     94         return mCursor.getDouble(column);
     95     }
     96 
     97     @Override
     98     public float getFloat(int column) {
     99         return mCursor.getFloat(column);
    100     }
    101 
    102     @Override
    103     public int getInt(int column) {
    104         return mCursor.getInt(column);
    105     }
    106 
    107     @Override
    108     public long getLong(int column) {
    109         return mCursor.getLong(column);
    110     }
    111 
    112     @Override
    113     public short getShort(int column) {
    114         return mCursor.getShort(column);
    115     }
    116 
    117     @Override
    118     public String getString(int column) {
    119         if (column == mAuthorityIndex) {
    120             return mAuthority;
    121         } else if (column == mRootIdIndex) {
    122             return mRootId;
    123         } else {
    124             return mCursor.getString(column);
    125         }
    126     }
    127 
    128     @Override
    129     public int getType(int column) {
    130         return mCursor.getType(column);
    131     }
    132 
    133     @Override
    134     public boolean isNull(int column) {
    135         return mCursor.isNull(column);
    136     }
    137 }
    138