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