diff --git a/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java b/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java index c28806b42cd7d..e15e6e03abc79 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java +++ b/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java @@ -61,6 +61,7 @@ import android.os.Parcelable; import android.os.SystemProperties; import android.provider.DocumentsContract; import android.provider.DocumentsContract.Document; +import android.support.annotation.Nullable; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; @@ -96,7 +97,6 @@ import com.android.documentsui.model.DocumentInfo; import com.android.documentsui.model.DocumentStack; import com.android.documentsui.model.RootInfo; import com.android.internal.util.Preconditions; - import com.google.common.collect.Lists; import java.util.ArrayList; @@ -129,6 +129,8 @@ public class DirectoryFragment extends Fragment { private static final String EXTRA_QUERY = "query"; private static final String EXTRA_IGNORE_STATE = "ignoreState"; + private final Model mModel = new Model(); + private final Handler mHandler = new Handler(Looper.getMainLooper()); private View mEmptyView; @@ -147,7 +149,6 @@ public class DirectoryFragment extends Fragment { private LoaderCallbacks mCallbacks; private FragmentTuner mFragmentTuner; private DocumentClipper mClipper; - private MultiSelectManager mSelectionManager; // These are lazily initialized. private LinearLayoutManager mListLayout; private GridLayoutManager mGridLayout; @@ -261,7 +262,7 @@ public class DirectoryFragment extends Fragment { } // Clear any outstanding selection - mSelectionManager.clearSelection(); + mModel.clearSelection(); } @Override @@ -290,14 +291,17 @@ public class DirectoryFragment extends Fragment { } }; - mSelectionManager = new MultiSelectManager( + // TODO: instead of inserting the view into the constructor, extract listener-creation code + // and set the listener on the view after the fact. Then the view doesn't need to be passed + // into the selection manager which is passed into the model. + MultiSelectManager selMgr= new MultiSelectManager( mRecView, listener, state.allowMultiple ? MultiSelectManager.MODE_MULTIPLE : MultiSelectManager.MODE_SINGLE); - - mSelectionManager.addCallback(new SelectionModeListener()); + selMgr.addCallback(new SelectionModeListener()); + mModel.setSelectionManager(selMgr); mType = getArguments().getInt(EXTRA_TYPE); mStateKey = buildStateKey(root, doc); @@ -367,7 +371,9 @@ public class DirectoryFragment extends Fragment { if (!isAdded()) return; - mAdapter.replaceResult(result); + // TODO: make the adapter listen to the model + mModel.update(result); + mAdapter.update(); // Push latest state up to UI // TODO: if mode change was racing with us, don't overwrite it @@ -380,7 +386,7 @@ public class DirectoryFragment extends Fragment { updateDisplayState(); // When launched into empty recents, show drawer - if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched && + if (mType == TYPE_RECENT_OPEN && mModel.isEmpty() && !state.stackTouched && context instanceof DocumentsActivity) { ((DocumentsActivity) context).setRootsDrawerOpen(true); } @@ -398,7 +404,9 @@ public class DirectoryFragment extends Fragment { @Override public void onLoaderReset(Loader loader) { - mAdapter.replaceResult(null); + // TODO: make the adapter listen to the model. + mModel.update(null); + mAdapter.update(); } }; @@ -433,7 +441,7 @@ public class DirectoryFragment extends Fragment { } private boolean onSingleTapUp(MotionEvent e) { - if (Events.isTouchEvent(e) && mSelectionManager.getSelection().isEmpty()) { + if (Events.isTouchEvent(e) && mModel.getSelection().isEmpty()) { int position = getEventAdapterPosition(e); if (position != RecyclerView.NO_POSITION) { return handleViewItem(position); @@ -454,14 +462,14 @@ public class DirectoryFragment extends Fragment { } private boolean handleViewItem(int position) { - final Cursor cursor = mAdapter.getItem(position); + final Cursor cursor = mModel.getItem(position); checkNotNull(cursor, "Cursor cannot be null."); final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); if (isDocumentEnabled(docMimeType, docFlags)) { final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); - ((BaseActivity) getActivity()).onDocumentPicked(doc, mAdapter); - mSelectionManager.clearSelection(); + ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel); + mModel.clearSelection(); return true; } return false; @@ -598,7 +606,7 @@ public class DirectoryFragment extends Fragment { public boolean onBeforeItemStateChange(int position, boolean selected) { // Directories and footer items cannot be checked if (selected) { - final Cursor cursor = mAdapter.getItem(position); + final Cursor cursor = mModel.getItem(position); checkNotNull(cursor, "Cursor cannot be null."); final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); @@ -610,7 +618,7 @@ public class DirectoryFragment extends Fragment { @Override public void onItemStateChanged(int position, boolean selected) { - final Cursor cursor = mAdapter.getItem(position); + final Cursor cursor = mModel.getItem(position); checkNotNull(cursor, "Cursor cannot be null."); final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); @@ -621,7 +629,7 @@ public class DirectoryFragment extends Fragment { @Override public void onSelectionChanged() { - mSelectionManager.getSelection(mSelected); + mModel.getSelection(mSelected); if (mSelected.size() > 0) { if (DEBUG) Log.d(TAG, "Maybe starting action mode."); if (mActionMode == null) { @@ -651,7 +659,7 @@ public class DirectoryFragment extends Fragment { if (DEBUG) Log.d(TAG, "Handling action mode destroyed."); mActionMode = null; // clear selection - mSelectionManager.clearSelection(); + mModel.clearSelection(); mSelected.clear(); mNoDeleteCount = 0; } @@ -659,8 +667,8 @@ public class DirectoryFragment extends Fragment { @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { mode.getMenuInflater().inflate(R.menu.mode_directory, menu); - mode.setTitle(TextUtils.formatSelectedCount(mSelectionManager.getSelection().size())); - return mSelectionManager.getSelection().size() > 0; + mode.setTitle(TextUtils.formatSelectedCount(mModel.getSelection().size())); + return mModel.getSelection().size() > 0; } @Override @@ -679,8 +687,7 @@ public class DirectoryFragment extends Fragment { @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { - Selection selection = new Selection(); - mSelectionManager.getSelection(selection); + Selection selection = mModel.getSelection(new Selection()); final int id = item.getItemId(); if (id == R.id.menu_open) { @@ -948,50 +955,31 @@ public class DirectoryFragment extends Fragment { } } - private final class DocumentsAdapter extends RecyclerView.Adapter - implements DocumentContext { + private final class DocumentsAdapter extends RecyclerView.Adapter { private final Context mContext; private final LayoutInflater mInflater; // TODO: Bring back support for footers. private final List