Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2ed26c4e authored by Jason Parks's avatar Jason Parks Committed by Android (Google) Code Review
Browse files

Merge "Change CursorTreeAdapter to close the cursors rather than deactivating...

Merge "Change CursorTreeAdapter to close the cursors rather than deactivating them. Fix SimpleCursorTreeAdapter to allow a null cursor as an argument."
parents adfb5990 4b08d3e4
Loading
Loading
Loading
Loading
+5 −3
Original line number Original line Diff line number Diff line
@@ -134,14 +134,16 @@ public abstract class CursorTreeAdapter extends BaseExpandableListAdapter implem
    /**
    /**
     * Sets the group Cursor.
     * Sets the group Cursor.
     * 
     * 
     * @param cursor The Cursor to set for the group.
     * @param cursor The Cursor to set for the group. If there is an existing cursor 
     * it will be closed.
     */
     */
    public void setGroupCursor(Cursor cursor) {
    public void setGroupCursor(Cursor cursor) {
        mGroupCursorHelper.changeCursor(cursor, false);
        mGroupCursorHelper.changeCursor(cursor, false);
    }
    }
    
    
    /**
    /**
     * Sets the children Cursor for a particular group.
     * Sets the children Cursor for a particular group. If there is an existing cursor
     * it will be closed.
     * <p>
     * <p>
     * This is useful when asynchronously querying to prevent blocking the UI.
     * This is useful when asynchronously querying to prevent blocking the UI.
     * 
     * 
@@ -476,7 +478,7 @@ public abstract class CursorTreeAdapter extends BaseExpandableListAdapter implem
            
            
            mCursor.unregisterContentObserver(mContentObserver);
            mCursor.unregisterContentObserver(mContentObserver);
            mCursor.unregisterDataSetObserver(mDataSetObserver);
            mCursor.unregisterDataSetObserver(mDataSetObserver);
            mCursor.deactivate();
            mCursor.close();
            mCursor = null;
            mCursor = null;
        }
        }
        
        
+26 −29
Original line number Original line Diff line number Diff line
@@ -38,6 +38,10 @@ import android.view.View;
 * binding can be found, an {@link IllegalStateException} is thrown.
 * binding can be found, an {@link IllegalStateException} is thrown.
 */
 */
public abstract class SimpleCursorTreeAdapter extends ResourceCursorTreeAdapter {
public abstract class SimpleCursorTreeAdapter extends ResourceCursorTreeAdapter {
    
    /** The name of the columns that contain the data to display for a group. */
    private String[] mGroupFromNames;
    
    /** The indices of columns that contain data to display for a group. */
    /** The indices of columns that contain data to display for a group. */
    private int[] mGroupFrom;
    private int[] mGroupFrom;
    /**
    /**
@@ -46,6 +50,9 @@ public abstract class SimpleCursorTreeAdapter extends ResourceCursorTreeAdapter
     */
     */
    private int[] mGroupTo;
    private int[] mGroupTo;


    /** The name of the columns that contain the data to display for a child. */
    private String[] mChildFromNames;
    
    /** The indices of columns that contain data to display for a child. */
    /** The indices of columns that contain data to display for a child. */
    private int[] mChildFrom;
    private int[] mChildFrom;
    /**
    /**
@@ -171,38 +178,12 @@ public abstract class SimpleCursorTreeAdapter extends ResourceCursorTreeAdapter


    private void init(String[] groupFromNames, int[] groupTo, String[] childFromNames,
    private void init(String[] groupFromNames, int[] groupTo, String[] childFromNames,
            int[] childTo) {
            int[] childTo) {
        
        mGroupFromNames = groupFromNames;
        mGroupTo = groupTo;
        mGroupTo = groupTo;
        
        
        mChildFromNames = childFromNames;
        mChildTo = childTo;
        mChildTo = childTo;
        
        // Get the group cursor column indices, the child cursor column indices will come
        // when needed
        initGroupFromColumns(groupFromNames);
        
        // Get a temporary child cursor to init the column indices
        if (getGroupCount() > 0) {
            MyCursorHelper tmpCursorHelper = getChildrenCursorHelper(0, true);
            if (tmpCursorHelper != null) {
                initChildrenFromColumns(childFromNames, tmpCursorHelper.getCursor());
                deactivateChildrenCursorHelper(0);
            }
        }
    }
    
    private void initFromColumns(Cursor cursor, String[] fromColumnNames, int[] fromColumns) {
        for (int i = fromColumnNames.length - 1; i >= 0; i--) {
            fromColumns[i] = cursor.getColumnIndexOrThrow(fromColumnNames[i]);
        }
    }
    
    private void initGroupFromColumns(String[] groupFromNames) {
        mGroupFrom = new int[groupFromNames.length];
        initFromColumns(mGroupCursorHelper.getCursor(), groupFromNames, mGroupFrom);
    }

    private void initChildrenFromColumns(String[] childFromNames, Cursor childCursor) {
        mChildFrom = new int[childFromNames.length];
        initFromColumns(childCursor, childFromNames, mChildFrom);
    }
    }
    
    
    /**
    /**
@@ -257,13 +238,29 @@ public abstract class SimpleCursorTreeAdapter extends ResourceCursorTreeAdapter
        }
        }
    }
    }
    
    
    private void initFromColumns(Cursor cursor, String[] fromColumnNames, int[] fromColumns) {
        for (int i = fromColumnNames.length - 1; i >= 0; i--) {
            fromColumns[i] = cursor.getColumnIndexOrThrow(fromColumnNames[i]);
        }
    }
    
    @Override
    @Override
    protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) {
    protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) {
        if (mChildFrom == null) {
            mChildFrom = new int[mChildFromNames.length];
            initFromColumns(cursor, mChildFromNames, mChildFrom);
        }
        
        bindView(view, context, cursor, mChildFrom, mChildTo);
        bindView(view, context, cursor, mChildFrom, mChildTo);
    }
    }


    @Override
    @Override
    protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {
    protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {
        if (mGroupFrom == null) {
            mGroupFrom = new int[mGroupFromNames.length];
            initFromColumns(cursor, mGroupFromNames, mGroupFrom);
        }
        
        bindView(view, context, cursor, mGroupFrom, mGroupTo);
        bindView(view, context, cursor, mGroupFrom, mGroupTo);
    }
    }