DirectoryFragment.java revision 33819318f61224cdda45e5e9a70dc9cdb9f33911
19e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey/*
29e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey * Copyright (C) 2013 The Android Open Source Project
39e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey *
49e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
59e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey * you may not use this file except in compliance with the License.
69e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey * You may obtain a copy of the License at
79e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey *
89e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
99e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey *
109e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
119e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
129e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey * See the License for the specific language governing permissions and
149e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey * limitations under the License.
159e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey */
169e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
179e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeypackage com.android.documentsui;
189e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
199d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkeyimport static com.android.documentsui.DocumentsActivity.TAG;
20b180a65d41adc731cbff1536c7ede15174bc08e1Jeff Sharkeyimport static com.android.documentsui.DocumentsActivity.State.ACTION_CREATE;
21b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkeyimport static com.android.documentsui.DocumentsActivity.State.ACTION_MANAGE;
22b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkeyimport static com.android.documentsui.DocumentsActivity.State.MODE_GRID;
23b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkeyimport static com.android.documentsui.DocumentsActivity.State.MODE_LIST;
24d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkeyimport static com.android.documentsui.DocumentsActivity.State.MODE_UNKNOWN;
25d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkeyimport static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_UNKNOWN;
265dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkeyimport static com.android.documentsui.model.DocumentInfo.getCursorInt;
275dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkeyimport static com.android.documentsui.model.DocumentInfo.getCursorLong;
285dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkeyimport static com.android.documentsui.model.DocumentInfo.getCursorString;
299d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
30d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkeyimport android.app.ActivityManager;
3154e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.app.Fragment;
329e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.app.FragmentManager;
339e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.app.FragmentTransaction;
349e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.app.LoaderManager.LoaderCallbacks;
357aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkeyimport android.content.ContentProviderClient;
364eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkeyimport android.content.ContentResolver;
37fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkeyimport android.content.ContentValues;
389e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.content.Context;
394eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkeyimport android.content.Intent;
409e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.content.Loader;
415dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkeyimport android.database.Cursor;
429d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkeyimport android.graphics.Bitmap;
439d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkeyimport android.graphics.Point;
444ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkeyimport android.graphics.drawable.Drawable;
451feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkeyimport android.graphics.drawable.InsetDrawable;
469e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.net.Uri;
479d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkeyimport android.os.AsyncTask;
489e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.os.Bundle;
494ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkeyimport android.os.CancellationSignal;
5033819318f61224cdda45e5e9a70dc9cdb9f33911Jeff Sharkeyimport android.os.OperationCanceledException;
51e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkeyimport android.os.Parcelable;
529e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.provider.DocumentsContract;
535dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkeyimport android.provider.DocumentsContract.Document;
5454e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.text.format.DateUtils;
55b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkeyimport android.text.format.Formatter;
569d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkeyimport android.text.format.Time;
579d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkeyimport android.util.Log;
58e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkeyimport android.util.SparseArray;
59be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkeyimport android.util.SparseBooleanArray;
60be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkeyimport android.view.ActionMode;
619e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.view.LayoutInflater;
6254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.view.Menu;
6354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.view.MenuItem;
649e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.view.View;
659e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.view.ViewGroup;
66be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkeyimport android.widget.AbsListView;
67be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkeyimport android.widget.AbsListView.MultiChoiceModeListener;
684ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkeyimport android.widget.AbsListView.RecyclerListener;
6954e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.widget.AdapterView;
7054e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.widget.AdapterView.OnItemClickListener;
71ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkeyimport android.widget.BaseAdapter;
721feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkeyimport android.widget.FrameLayout;
7354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.widget.GridView;
749e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.widget.ImageView;
759e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.widget.ListView;
769e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.widget.TextView;
774eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkeyimport android.widget.Toast;
789e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
79b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkeyimport com.android.documentsui.DocumentsActivity.State;
80b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkeyimport com.android.documentsui.ProviderExecutor.Preemptable;
81fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkeyimport com.android.documentsui.RecentsProvider.StateColumns;
82ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkeyimport com.android.documentsui.model.DocumentInfo;
83251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkeyimport com.android.documentsui.model.RootInfo;
84be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkeyimport com.google.android.collect.Lists;
85be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
86be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkeyimport java.util.ArrayList;
87ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkeyimport java.util.List;
8854e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
8954e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey/**
9054e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey * Display the documents inside a single directory.
9154e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey */
9254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeypublic class DirectoryFragment extends Fragment {
9354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
949fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey    private View mEmptyView;
9554e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    private ListView mListView;
9654e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    private GridView mGridView;
9754e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
98be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey    private AbsListView mCurrentView;
99be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
100ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey    public static final int TYPE_NORMAL = 1;
101ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey    public static final int TYPE_SEARCH = 2;
102251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey    public static final int TYPE_RECENT_OPEN = 3;
103dc2963aecaf38bf53d6de82957412a486049c207Jeff Sharkey
104e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static final int ANIM_NONE = 1;
105e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static final int ANIM_SIDE = 2;
106e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static final int ANIM_DOWN = 3;
107e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static final int ANIM_UP = 4;
108e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey
109dc2963aecaf38bf53d6de82957412a486049c207Jeff Sharkey    private int mType = TYPE_NORMAL;
110e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    private String mStateKey;
111dc2963aecaf38bf53d6de82957412a486049c207Jeff Sharkey
112d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    private int mLastMode = MODE_UNKNOWN;
113d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    private int mLastSortOrder = SORT_ORDER_UNKNOWN;
114ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey    private boolean mLastShowSize = false;
115d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
116f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey    private boolean mHideGridTitles = false;
117f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
118d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey    private boolean mSvelteRecents;
1199d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey    private Point mThumbSize;
1209d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
1219e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey    private DocumentsAdapter mAdapter;
122b448660a227f93d7bd5c5d019a21745e67c2f54dJeff Sharkey    private LoaderCallbacks<DirectoryResult> mCallbacks;
1239e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
124b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey    private static final String EXTRA_TYPE = "type";
125d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    private static final String EXTRA_ROOT = "root";
126d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    private static final String EXTRA_DOC = "doc";
127251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey    private static final String EXTRA_QUERY = "query";
128e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    private static final String EXTRA_IGNORE_STATE = "ignoreState";
1299e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
130db5ef125007644daa94aeaf1bd8637f4e0095e94Jeff Sharkey    private final int mLoaderId = 42;
131b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey
132e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
133e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        show(fm, TYPE_NORMAL, root, doc, null, anim);
134b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey    }
135b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey
136e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
137e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        show(fm, TYPE_SEARCH, root, null, query, anim);
138b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey    }
139b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey
140e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static void showRecentsOpen(FragmentManager fm, int anim) {
141e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
142b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey    }
1439e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
144e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
145e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey            String query, int anim) {
1469e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        final Bundle args = new Bundle();
147b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey        args.putInt(EXTRA_TYPE, type);
148d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        args.putParcelable(EXTRA_ROOT, root);
149d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        args.putParcelable(EXTRA_DOC, doc);
150251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey        args.putString(EXTRA_QUERY, query);
1519e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
152e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        final FragmentTransaction ft = fm.beginTransaction();
153e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        switch (anim) {
154e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey            case ANIM_SIDE:
155e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                args.putBoolean(EXTRA_IGNORE_STATE, true);
156e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                break;
157e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey            case ANIM_DOWN:
158e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                args.putBoolean(EXTRA_IGNORE_STATE, true);
159e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
160e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                break;
161e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey            case ANIM_UP:
162e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
163e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                break;
164e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        }
165e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey
1669e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        final DirectoryFragment fragment = new DirectoryFragment();
1679e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        fragment.setArguments(args);
1689e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
16966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        ft.replace(R.id.container_directory, fragment);
1709e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        ft.commitAllowingStateLoss();
1719e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey    }
1729e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
173e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    private static String buildStateKey(RootInfo root, DocumentInfo doc) {
174e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        final StringBuilder builder = new StringBuilder();
175e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        builder.append(root != null ? root.authority : "null").append(';');
176e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        builder.append(root != null ? root.rootId : "null").append(';');
177e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        builder.append(doc != null ? doc.documentId : "null");
178e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        return builder.toString();
179e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    }
180e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey
181ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey    public static DirectoryFragment get(FragmentManager fm) {
182ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        // TODO: deal with multiple directories shown at once
18366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
184ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey    }
185ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
1869e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey    @Override
1879e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey    public View onCreateView(
1889e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
1899e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        final Context context = inflater.getContext();
19054e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        final View view = inflater.inflate(R.layout.fragment_directory, container, false);
19154e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
1929fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey        mEmptyView = view.findViewById(android.R.id.empty);
1939fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
19454e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        mListView = (ListView) view.findViewById(R.id.list);
19554e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        mListView.setOnItemClickListener(mItemListener);
196be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        mListView.setMultiChoiceModeListener(mMultiListener);
1974ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        mListView.setRecyclerListener(mRecycleListener);
19854e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
19954e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        mGridView = (GridView) view.findViewById(R.id.grid);
20054e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        mGridView.setOnItemClickListener(mItemListener);
201be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        mGridView.setMultiChoiceModeListener(mMultiListener);
2024ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        mGridView.setRecyclerListener(mRecycleListener);
20354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
2045dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey        return view;
2055dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey    }
20654e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
2075dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey    @Override
208d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey    public void onDestroyView() {
209d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        super.onDestroyView();
210d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey
211d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        // Cancel any outstanding thumbnail requests
212d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
213d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        final int count = target.getChildCount();
214d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        for (int i = 0; i < count; i++) {
215d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            final View view = target.getChildAt(i);
216d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            mRecycleListener.onMovedToScrapHeap(view);
217d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        }
218ca25db0cb3d6b9686a42c9cb99d90dc095f20cd8Jeff Sharkey
219ca25db0cb3d6b9686a42c9cb99d90dc095f20cd8Jeff Sharkey        // Tear down any selection in progress
220ca25db0cb3d6b9686a42c9cb99d90dc095f20cd8Jeff Sharkey        mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
221ca25db0cb3d6b9686a42c9cb99d90dc095f20cd8Jeff Sharkey        mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
222d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey    }
223d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey
224d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey    @Override
2255dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey    public void onActivityCreated(Bundle savedInstanceState) {
2265dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey        super.onActivityCreated(savedInstanceState);
2275dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey
2285dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey        final Context context = getActivity();
229d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        final State state = getDisplayState(DirectoryFragment.this);
2305dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey
231f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey        final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
232f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey        final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
233f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
234251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey        mAdapter = new DocumentsAdapter();
235b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey        mType = getArguments().getInt(EXTRA_TYPE);
236e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        mStateKey = buildStateKey(root, doc);
2379e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
238f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey        if (mType == TYPE_RECENT_OPEN) {
239f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            // Hide titles when showing recents for picking images/videos
240f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            mHideGridTitles = MimePredicate.mimeMatches(
241f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    MimePredicate.VISUAL_MIMES, state.acceptMimes);
242f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey        } else {
243f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
244f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey        }
245f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
246d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        final ActivityManager am = (ActivityManager) context.getSystemService(
247d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey                Context.ACTIVITY_SERVICE);
248d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
249d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey
250b448660a227f93d7bd5c5d019a21745e67c2f54dJeff Sharkey        mCallbacks = new LoaderCallbacks<DirectoryResult>() {
2519e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            @Override
252b448660a227f93d7bd5c5d019a21745e67c2f54dJeff Sharkey            public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
253251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                final String query = getArguments().getString(EXTRA_QUERY);
254251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey
25554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                Uri contentsUri;
256251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                switch (mType) {
257251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                    case TYPE_NORMAL:
258d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                        contentsUri = DocumentsContract.buildChildDocumentsUri(
259d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                                doc.authority, doc.documentId);
2604ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                        if (state.action == ACTION_MANAGE) {
2614ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                            contentsUri = DocumentsContract.setManageMode(contentsUri);
2624ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                        }
263fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                        return new DirectoryLoader(
2644ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                                context, mType, root, doc, contentsUri, state.userSortOrder);
265251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                    case TYPE_SEARCH:
266251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                        contentsUri = DocumentsContract.buildSearchDocumentsUri(
2673e1189b3590aefb65a2af720ae2ba959bbd4188dJeff Sharkey                                root.authority, root.rootId, query);
2684ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                        if (state.action == ACTION_MANAGE) {
2694ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                            contentsUri = DocumentsContract.setManageMode(contentsUri);
2704ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                        }
271fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                        return new DirectoryLoader(
2724ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                                context, mType, root, doc, contentsUri, state.userSortOrder);
273251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                    case TYPE_RECENT_OPEN:
274348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey                        final RootsCache roots = DocumentsApplication.getRootsCache(context);
275a9ce049db87259e302e2368d2a4a1c11a94fd831Jeff Sharkey                        return new RecentLoader(context, roots, state);
276251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                    default:
277251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                        throw new IllegalStateException("Unknown type " + mType);
278251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                }
2799e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            }
2809e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
2819e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            @Override
282b448660a227f93d7bd5c5d019a21745e67c2f54dJeff Sharkey            public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
283d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                if (!isAdded()) return;
284d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
285d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey                mAdapter.swapResult(result);
286d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
287d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                // Push latest state up to UI
288d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                // TODO: if mode change was racing with us, don't overwrite it
28988f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                if (result.mode != MODE_UNKNOWN) {
29088f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    state.derivedMode = result.mode;
29188f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                }
292fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                state.derivedSortOrder = result.sortOrder;
293d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                ((DocumentsActivity) context).onStateChanged();
294d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
295d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                updateDisplayState();
296d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
297a82c2e24bc226c123075331cdaafb171b3b07b85Jeff Sharkey                // When launched into empty recents, show drawer
298a82c2e24bc226c123075331cdaafb171b3b07b85Jeff Sharkey                if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched) {
299a82c2e24bc226c123075331cdaafb171b3b07b85Jeff Sharkey                    ((DocumentsActivity) context).setRootsDrawerOpen(true);
300a82c2e24bc226c123075331cdaafb171b3b07b85Jeff Sharkey                }
301a82c2e24bc226c123075331cdaafb171b3b07b85Jeff Sharkey
302e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                // Restore any previous instance state
303e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
304e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
305e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                    getView().restoreHierarchyState(container);
306e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                } else if (mLastSortOrder != state.derivedSortOrder) {
307d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                    mListView.smoothScrollToPosition(0);
308d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                    mGridView.smoothScrollToPosition(0);
309d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                }
310e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey
311e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                mLastSortOrder = state.derivedSortOrder;
3129e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            }
3139e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
3149e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            @Override
315b448660a227f93d7bd5c5d019a21745e67c2f54dJeff Sharkey            public void onLoaderReset(Loader<DirectoryResult> loader) {
316d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey                mAdapter.swapResult(null);
3179e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            }
3189e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        };
3199e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
320d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        // Kick off loader at least once
321d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
322d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
323b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey        updateDisplayState();
32454e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    }
3259e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
32628c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey    @Override
327e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public void onStop() {
328e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        super.onStop();
329e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey
330e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        // Remember last scroll location
331e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
332e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        getView().saveHierarchyState(container);
333e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        final State state = getDisplayState(this);
334e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        state.dirState.put(mStateKey, container);
335e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    }
336e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey
337e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    @Override
33888f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey    public void onResume() {
33988f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey        super.onResume();
34028c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey        updateDisplayState();
34128c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey    }
34228c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey
343d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    public void onUserSortOrderChanged() {
344fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        // Sort order change always triggers reload; we'll trigger state change
345fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        // on the flip side.
346d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
347d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    }
348d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
349d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    public void onUserModeChanged() {
350fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        final ContentResolver resolver = getActivity().getContentResolver();
351fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        final State state = getDisplayState(this);
352fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey
353fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
354fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
355fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey
3563e1189b3590aefb65a2af720ae2ba959bbd4188dJeff Sharkey        if (root != null && doc != null) {
35788f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            final Uri stateUri = RecentsProvider.buildState(
35888f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    root.authority, root.rootId, doc.documentId);
35988f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            final ContentValues values = new ContentValues();
36088f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            values.put(StateColumns.MODE, state.userMode);
36188f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey
36288f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            new AsyncTask<Void, Void, Void>() {
36388f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                @Override
36488f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                protected Void doInBackground(Void... params) {
36588f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    resolver.insert(stateUri, values);
36688f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    return null;
36788f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                }
36888f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            }.execute();
36988f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey        }
370fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey
371fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        // Mode change is just visual change; no need to kick loader, and
372fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        // deliver change event immediately.
373fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        state.derivedMode = state.userMode;
374fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        ((DocumentsActivity) getActivity()).onStateChanged();
375fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey
376d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        updateDisplayState();
377d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    }
378d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
379d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    private void updateDisplayState() {
380b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        final State state = getDisplayState(this);
38154e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
382ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey        if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
383fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        mLastMode = state.derivedMode;
384ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey        mLastShowSize = state.showSize;
38554e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
386fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
387fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
38854e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
389be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        final int choiceMode;
3901482789374fb8da3abea8f6f4f272a67205b95dbJeff Sharkey        if (state.allowMultiple) {
391be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
392be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        } else {
393be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            choiceMode = ListView.CHOICE_MODE_NONE;
394be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        }
395be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
3969d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        final int thumbSize;
397fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        if (state.derivedMode == MODE_GRID) {
3989d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
39954e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey            mListView.setAdapter(null);
400be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
40154e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey            mGridView.setAdapter(mAdapter);
402be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
40354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey            mGridView.setNumColumns(GridView.AUTO_FIT);
404be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mGridView.setChoiceMode(choiceMode);
405be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mCurrentView = mGridView;
406fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        } else if (state.derivedMode == MODE_LIST) {
407d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey            thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
40854e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey            mGridView.setAdapter(null);
409be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
41054e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey            mListView.setAdapter(mAdapter);
411be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mListView.setChoiceMode(choiceMode);
412be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mCurrentView = mListView;
4131482789374fb8da3abea8f6f4f272a67205b95dbJeff Sharkey        } else {
414fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey            throw new IllegalStateException("Unknown state " + state.derivedMode);
41554e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        }
4169d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
4179d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        mThumbSize = new Point(thumbSize, thumbSize);
41854e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    }
41954e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
42054e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    private OnItemClickListener mItemListener = new OnItemClickListener() {
42154e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        @Override
42254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
4235dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final Cursor cursor = mAdapter.getItem(position);
4243f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            if (cursor != null) {
4252a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
4262a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
4272a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                if (isDocumentEnabled(docMimeType, docFlags)) {
4282a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                    final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
4293f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                    ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
4303f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                }
431a5599ef636e37cb0b6474349936999be1afe6987Jeff Sharkey            }
43254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        }
43354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    };
43454e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
435be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey    private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
436be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        @Override
437be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
438be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
439be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            return true;
440be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        }
441be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
442be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        @Override
443be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
444b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            final State state = getDisplayState(DirectoryFragment.this);
4454eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
4464eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final MenuItem open = menu.findItem(R.id.menu_open);
4474eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final MenuItem share = menu.findItem(R.id.menu_share);
4484eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final MenuItem delete = menu.findItem(R.id.menu_delete);
4494eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
4504eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final boolean manageMode = state.action == ACTION_MANAGE;
4514eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            open.setVisible(!manageMode);
4524eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            share.setVisible(manageMode);
4534eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            delete.setVisible(manageMode);
4544eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
455be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            return true;
456be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        }
457be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
458be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        @Override
459be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
4604eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
461ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey            final ArrayList<DocumentInfo> docs = Lists.newArrayList();
4624eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final int size = checked.size();
4634eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            for (int i = 0; i < size; i++) {
4644eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                if (checked.valueAt(i)) {
4655dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                    final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
466251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                    final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
4674eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                    docs.add(doc);
468be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                }
4694eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            }
4704eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
4714eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final int id = item.getItemId();
4724eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            if (id == R.id.menu_open) {
4734eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
474b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                mode.finish();
4754eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                return true;
476be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
4774eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            } else if (id == R.id.menu_share) {
4784eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                onShareDocuments(docs);
479b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                mode.finish();
480be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                return true;
4814eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
4824eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            } else if (id == R.id.menu_delete) {
4834eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                onDeleteDocuments(docs);
484b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                mode.finish();
4854eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                return true;
4864eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
487be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            } else {
488be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                return false;
489be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            }
490be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        }
491be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
492be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        @Override
493be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        public void onDestroyActionMode(ActionMode mode) {
494be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            // ignored
495be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        }
496be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
497be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        @Override
498be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        public void onItemCheckedStateChanged(
499be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                ActionMode mode, int position, long id, boolean checked) {
500be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            if (checked) {
5013f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                // Directories and footer items cannot be checked
5023f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                boolean valid = false;
5033f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
5045dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                final Cursor cursor = mAdapter.getItem(position);
5053f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                if (cursor != null) {
5063f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                    final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
5072a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                    final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
5082a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                    if (!Document.MIME_TYPE_DIR.equals(docMimeType)) {
5092a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                        valid = isDocumentEnabled(docMimeType, docFlags);
5102a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                    }
5113f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                }
5123f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
5133f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                if (!valid) {
514be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                    mCurrentView.setItemChecked(position, false);
515be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                }
516be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            }
517be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
518be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mode.setTitle(getResources()
519be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                    .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
520be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        }
521be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey    };
522be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
5234ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey    private RecyclerListener mRecycleListener = new RecyclerListener() {
5244ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        @Override
5254ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        public void onMovedToScrapHeap(View view) {
5264ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
5274ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            if (iconThumb != null) {
5284ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
5294ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                if (oldTask != null) {
530b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey                    oldTask.preempt();
5314ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    iconThumb.setTag(null);
5324ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                }
5334ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            }
5344ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        }
5354ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey    };
5364ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey
537ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey    private void onShareDocuments(List<DocumentInfo> docs) {
538b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        Intent intent;
539b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        if (docs.size() == 1) {
540b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            final DocumentInfo doc = docs.get(0);
541b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
542b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            intent = new Intent(Intent.ACTION_SEND);
543b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
544b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            intent.addCategory(Intent.CATEGORY_DEFAULT);
545b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            intent.setType(doc.mimeType);
546d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey            intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
5474eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
548b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        } else if (docs.size() > 1) {
5494eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
5504eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
5514eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            intent.addCategory(Intent.CATEGORY_DEFAULT);
552b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
553b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            final ArrayList<String> mimeTypes = Lists.newArrayList();
554b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            final ArrayList<Uri> uris = Lists.newArrayList();
555b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            for (DocumentInfo doc : docs) {
556b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                mimeTypes.add(doc.mimeType);
557d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                uris.add(doc.derivedUri);
558b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            }
559b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
560b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            intent.setType(findCommonMimeType(mimeTypes));
5614eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
562b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
5634eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        } else {
564b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            return;
5654eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        }
5664eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
567b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
5684eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        startActivity(intent);
5694eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey    }
5704eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
571ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey    private void onDeleteDocuments(List<DocumentInfo> docs) {
5724eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        final Context context = getActivity();
5734eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        final ContentResolver resolver = context.getContentResolver();
5744eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
5754eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        boolean hadTrouble = false;
576ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey        for (DocumentInfo doc : docs) {
5774eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            if (!doc.isDeleteSupported()) {
5784eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                Log.w(TAG, "Skipping " + doc);
5794eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                hadTrouble = true;
5804eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                continue;
5814eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            }
5824eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
5837aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            ContentProviderClient client = null;
5847aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            try {
5857aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                client = DocumentsApplication.acquireUnstableProviderOrThrow(
5867aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                        resolver, doc.derivedUri.getAuthority());
5877aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                DocumentsContract.deleteDocument(client, doc.derivedUri);
5887aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            } catch (Exception e) {
589ded77187ef53341765fcab8e29cda94810fc2ca5Jeff Sharkey                Log.w(TAG, "Failed to delete " + doc);
5904eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                hadTrouble = true;
5917aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            } finally {
5927aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                ContentProviderClient.releaseQuietly(client);
5934eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            }
5944eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        }
5954eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
5964eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        if (hadTrouble) {
5974eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
5984eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        }
5994eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey    }
6004eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
601b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey    private static State getDisplayState(Fragment fragment) {
6021482789374fb8da3abea8f6f4f272a67205b95dbJeff Sharkey        return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
60354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    }
60454e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
6053f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey    private static abstract class Footer {
6063f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        private final int mItemViewType;
6073f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
6083f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        public Footer(int itemViewType) {
6093f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            mItemViewType = itemViewType;
6103f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        }
6113f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
6123f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        public abstract View getView(View convertView, ViewGroup parent);
6133f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
6143f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        public int getItemViewType() {
6153f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            return mItemViewType;
6163f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        }
617954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey    }
618954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
619ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey    private class LoadingFooter extends Footer {
6203f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        public LoadingFooter() {
6213f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            super(1);
6223f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        }
6233f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
624954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        @Override
625954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        public View getView(View convertView, ViewGroup parent) {
626954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            final Context context = parent.getContext();
627ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey            final State state = getDisplayState(DirectoryFragment.this);
628ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey
629954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            if (convertView == null) {
630954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                final LayoutInflater inflater = LayoutInflater.from(context);
631ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                if (state.derivedMode == MODE_LIST) {
632ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                    convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
633ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                } else if (state.derivedMode == MODE_GRID) {
634ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                    convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
635ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                } else {
636ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                    throw new IllegalStateException();
637ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                }
638954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            }
639ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey
640954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            return convertView;
641954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        }
642954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey    }
643954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
6443f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey    private class MessageFooter extends Footer {
645954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        private final int mIcon;
646954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        private final String mMessage;
647954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
6483f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        public MessageFooter(int itemViewType, int icon, String message) {
6493f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            super(itemViewType);
650954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            mIcon = icon;
651954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            mMessage = message;
652954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        }
653954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
654954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        @Override
655954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        public View getView(View convertView, ViewGroup parent) {
656954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            final Context context = parent.getContext();
657954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            final State state = getDisplayState(DirectoryFragment.this);
658954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
659954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            if (convertView == null) {
660954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                final LayoutInflater inflater = LayoutInflater.from(context);
661fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                if (state.derivedMode == MODE_LIST) {
662954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                    convertView = inflater.inflate(R.layout.item_message_list, parent, false);
663fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                } else if (state.derivedMode == MODE_GRID) {
664954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                    convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
665954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                } else {
666954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                    throw new IllegalStateException();
667954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                }
668954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            }
669954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
670954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
671954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
672954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            icon.setImageResource(mIcon);
673954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            title.setText(mMessage);
674954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            return convertView;
675954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        }
676954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey    }
677954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
678ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey    private class DocumentsAdapter extends BaseAdapter {
6795dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey        private Cursor mCursor;
680954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        private int mCursorCount;
681954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
682954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        private List<Footer> mFooters = Lists.newArrayList();
683ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
684d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        public void swapResult(DirectoryResult result) {
685d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            mCursor = result != null ? result.cursor : null;
686d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            mCursorCount = mCursor != null ? mCursor.getCount() : 0;
687954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
688954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            mFooters.clear();
689954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
690d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
691954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            if (extras != null) {
692954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                final String info = extras.getString(DocumentsContract.EXTRA_INFO);
693954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                if (info != null) {
6941feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkey                    mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
695954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                }
696954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
697954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                if (error != null) {
6983f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                    mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
699954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                }
700954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
701954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                    mFooters.add(new LoadingFooter());
702954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                }
703954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            }
7049fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
705d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            if (result != null && result.exception != null) {
7067aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                mFooters.add(new MessageFooter(
7077aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                        3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
7087aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            }
7097aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey
7105dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            if (isEmpty()) {
7119fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                mEmptyView.setVisibility(View.VISIBLE);
7129fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            } else {
7139fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                mEmptyView.setVisibility(View.GONE);
7149fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            }
7159fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
716ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            notifyDataSetChanged();
7179e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        }
7189e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
7199e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        @Override
720ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        public View getView(int position, View convertView, ViewGroup parent) {
721954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            if (position < mCursorCount) {
722954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                return getDocumentView(position, convertView, parent);
723954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            } else {
724954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                position -= mCursorCount;
7253f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                convertView = mFooters.get(position).getView(convertView, parent);
7263f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                // Only the view itself is disabled; contents inside shouldn't
7273f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                // be dimmed.
7283f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                convertView.setEnabled(false);
7293f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                return convertView;
730954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            }
731954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        }
732954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
733954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        private View getDocumentView(int position, View convertView, ViewGroup parent) {
734ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            final Context context = parent.getContext();
735b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            final State state = getDisplayState(DirectoryFragment.this);
736ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
737f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
738f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
7394eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final RootsCache roots = DocumentsApplication.getRootsCache(context);
7404eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
7414eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                    context, mThumbSize);
7424eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
743ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            if (convertView == null) {
744ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey                final LayoutInflater inflater = LayoutInflater.from(context);
745fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                if (state.derivedMode == MODE_LIST) {
746ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey                    convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
747fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                } else if (state.derivedMode == MODE_GRID) {
748ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey                    convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
7491feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkey
7501feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkey                    // Apply padding to grid items
7511feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkey                    final FrameLayout grid = (FrameLayout) convertView;
7521feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkey                    final int gridPadding = getResources()
7531feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkey                            .getDimensionPixelSize(R.dimen.grid_padding);
754deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey
755deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    // Tricksy hobbitses! We need to fully clear the drawable so
756deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    // the view doesn't clobber the new InsetDrawable callback
757deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    // when setting back later.
758deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    final Drawable fg = grid.getForeground();
759deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    final Drawable bg = grid.getBackground();
760deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    grid.setForeground(null);
761deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    grid.setBackground(null);
762deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    grid.setForeground(new InsetDrawable(fg, gridPadding));
763deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    grid.setBackground(new InsetDrawable(bg, gridPadding));
764ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey                } else {
765ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey                    throw new IllegalStateException();
766ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey                }
767ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            }
768ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
7695dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final Cursor cursor = getItem(position);
7705dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey
771251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey            final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
772251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey            final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
7735dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
7745dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
7755dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
7765dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
7775dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
7785dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
7795dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
7805dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
781ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
782f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            final View line1 = convertView.findViewById(R.id.line1);
783f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            final View line2 = convertView.findViewById(R.id.line2);
784f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
7854ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
7864ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
787ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
788b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey            final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
789f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
790ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
791b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey            final TextView date = (TextView) convertView.findViewById(R.id.date);
792b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey            final TextView size = (TextView) convertView.findViewById(R.id.size);
793ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
7944ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
7959d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            if (oldTask != null) {
796b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey                oldTask.preempt();
7974ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                iconThumb.setTag(null);
7989d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            }
7999d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
8004ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            iconMime.animate().cancel();
8014ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            iconThumb.animate().cancel();
8024ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey
8033f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
804fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey            final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
805f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
806d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
8073f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
8084ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            boolean cacheHit = false;
809f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            if (showThumbnail) {
810251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
8115dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                final Bitmap cachedResult = thumbs.get(uri);
8129d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                if (cachedResult != null) {
8134ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    iconThumb.setImageBitmap(cachedResult);
8144ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    cacheHit = true;
8159d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                } else {
8164ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    iconThumb.setImageDrawable(null);
8174ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
8184ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                            uri, iconMime, iconThumb, mThumbSize);
8194ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    iconThumb.setTag(task);
820b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey                    ProviderExecutor.forAuthority(docAuthority).execute(task);
8219d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                }
8224ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            }
8234ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey
8244ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            // Always throw MIME icon into place, even when a thumbnail is being
8254ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            // loaded in background.
8264ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            if (cacheHit) {
8274ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                iconMime.setAlpha(0f);
8286efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                iconMime.setImageDrawable(null);
8294ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                iconThumb.setAlpha(1f);
8309e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            } else {
8314ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                iconMime.setAlpha(1f);
8324ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                iconThumb.setAlpha(0f);
8336efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                iconThumb.setImageDrawable(null);
8344ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                if (docIcon != 0) {
8354ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    iconMime.setImageDrawable(
8364ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                            IconUtils.loadPackageIcon(context, docAuthority, docIcon));
8374ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                } else {
838563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey                    iconMime.setImageDrawable(IconUtils.loadMimeIcon(
839563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey                            context, docMimeType, docAuthority, docId, state.derivedMode));
8404ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                }
8419e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            }
8429e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
843f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            boolean hasLine1 = false;
84428c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey            boolean hasLine2 = false;
84528c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey
846f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
847f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            if (!hideTitle) {
848f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                title.setText(docDisplayName);
849f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                hasLine1 = true;
850f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            }
851f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
852f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            Drawable iconDrawable = null;
853251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey            if (mType == TYPE_RECENT_OPEN) {
854a9ce049db87259e302e2368d2a4a1c11a94fd831Jeff Sharkey                // We've already had to enumerate roots before any results can
855a9ce049db87259e302e2368d2a4a1c11a94fd831Jeff Sharkey                // be shown, so this will never block.
856a9ce049db87259e302e2368d2a4a1c11a94fd831Jeff Sharkey                final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
857f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                iconDrawable = root.loadIcon(context);
8584ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey
85988f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                if (summary != null) {
86088f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    final boolean alwaysShowSummary = getResources()
86188f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            .getBoolean(R.bool.always_show_summary);
86288f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    if (alwaysShowSummary) {
86388f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        summary.setText(root.getDirectoryString());
86488f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        summary.setVisibility(View.VISIBLE);
86588f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        hasLine2 = true;
86688f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    } else {
867a9ce049db87259e302e2368d2a4a1c11a94fd831Jeff Sharkey                        if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
86888f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            // No summary needed if icon speaks for itself
86988f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            summary.setVisibility(View.INVISIBLE);
87088f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        } else {
87188f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            summary.setText(root.getDirectoryString());
87288f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            summary.setVisibility(View.VISIBLE);
87388f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
87488f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            hasLine2 = true;
87588f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        }
87688f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    }
8774ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                }
8785dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            } else {
879f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                // Directories showing thumbnails in grid mode get a little icon
880f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                // hint to remind user they're a directory.
881f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
882f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                        && showThumbnail) {
883f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    iconDrawable = context.getResources().getDrawable(R.drawable.ic_root_folder);
884f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                }
885f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
88688f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                if (summary != null) {
88788f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    if (docSummary != null) {
88888f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        summary.setText(docSummary);
88988f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        summary.setVisibility(View.VISIBLE);
89088f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        hasLine2 = true;
89188f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    } else {
89288f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        summary.setVisibility(View.INVISIBLE);
89388f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    }
894251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                }
895b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey            }
896b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey
897f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            if (icon1 != null) icon1.setVisibility(View.GONE);
898f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            if (icon2 != null) icon2.setVisibility(View.GONE);
899f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
900f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            if (iconDrawable != null) {
901f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                if (hasLine1) {
902f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    icon1.setVisibility(View.VISIBLE);
903f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    icon1.setImageDrawable(iconDrawable);
904f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                } else {
905f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    icon2.setVisibility(View.VISIBLE);
906f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    icon2.setImageDrawable(iconDrawable);
907f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                }
908f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            }
909f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
9105dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            if (docLastModified == -1) {
9119d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                date.setText(null);
9129d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            } else {
9135dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                date.setText(formatTime(context, docLastModified));
91428c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey                hasLine2 = true;
9159d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            }
916b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey
917b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey            if (state.showSize) {
918b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey                size.setVisibility(View.VISIBLE);
9195dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
920b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey                    size.setText(null);
921b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey                } else {
9225dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                    size.setText(Formatter.formatFileSize(context, docSize));
92328c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey                    hasLine2 = true;
924b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey                }
925b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey            } else {
926b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey                size.setVisibility(View.GONE);
92754e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey            }
928ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
929f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            if (line1 != null) {
930f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
931f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            }
93288f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            if (line2 != null) {
93388f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
93488f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            }
93528c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey
9362a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey            final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
9373f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            if (enabled) {
9383f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                setEnabledRecursive(convertView, true);
9396efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                iconMime.setAlpha(1f);
9406efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                iconThumb.setAlpha(1f);
941f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                if (icon1 != null) icon1.setAlpha(1f);
942f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                if (icon2 != null) icon2.setAlpha(1f);
9433f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            } else {
9443f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                setEnabledRecursive(convertView, false);
9456efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                iconMime.setAlpha(0.5f);
9466efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                iconThumb.setAlpha(0.5f);
947f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                if (icon1 != null) icon1.setAlpha(0.5f);
948f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                if (icon2 != null) icon2.setAlpha(0.5f);
9493f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            }
9503f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
951ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            return convertView;
95254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        }
95354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
954ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        @Override
955ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        public int getCount() {
956954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            return mCursorCount + mFooters.size();
957ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        }
9589e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
959ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        @Override
9605dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey        public Cursor getItem(int position) {
961954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            if (position < mCursorCount) {
9625dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                mCursor.moveToPosition(position);
963954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                return mCursor;
964954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            } else {
965954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                return null;
9665dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            }
967ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        }
96854e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
969ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        @Override
970ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        public long getItemId(int position) {
9715dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            return position;
972ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        }
973954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
974954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        @Override
9753f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        public int getViewTypeCount() {
9763f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            return 4;
9773f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        }
9783f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
9793f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        @Override
980954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        public int getItemViewType(int position) {
981954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            if (position < mCursorCount) {
982954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                return 0;
983954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            } else {
9843f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                position -= mCursorCount;
9853f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                return mFooters.get(position).getItemViewType();
986954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            }
987954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        }
9889e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey    }
9899d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
990b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey    private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
991b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey            implements Preemptable {
9924ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        private final Uri mUri;
9934ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        private final ImageView mIconMime;
9944ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        private final ImageView mIconThumb;
9954eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        private final Point mThumbSize;
9964ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        private final CancellationSignal mSignal;
9979d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
9984ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        public ThumbnailAsyncTask(
9994ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
10004ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            mUri = uri;
10014ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            mIconMime = iconMime;
10024ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            mIconThumb = iconThumb;
10034eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            mThumbSize = thumbSize;
10044ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            mSignal = new CancellationSignal();
10059d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        }
10069d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
1007b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey        @Override
1008b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey        public void preempt() {
10094ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            cancel(false);
10104ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            mSignal.cancel();
10119d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        }
10129d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10139d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        @Override
10149d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        protected Bitmap doInBackground(Uri... params) {
1015d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            if (isCancelled()) return null;
1016d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey
10174ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            final Context context = mIconThumb.getContext();
10187aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            final ContentResolver resolver = context.getContentResolver();
10199d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10207aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            ContentProviderClient client = null;
10219d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            Bitmap result = null;
10229d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            try {
10237aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                client = DocumentsApplication.acquireUnstableProviderOrThrow(
10247aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                        resolver, mUri.getAuthority());
10257aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
10269d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                if (result != null) {
10274eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                    final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
10284eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                            context, mThumbSize);
10294ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    thumbs.put(mUri, result);
10309d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                }
10319d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            } catch (Exception e) {
103233819318f61224cdda45e5e9a70dc9cdb9f33911Jeff Sharkey                if (!(e instanceof OperationCanceledException)) {
103333819318f61224cdda45e5e9a70dc9cdb9f33911Jeff Sharkey                    Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
103433819318f61224cdda45e5e9a70dc9cdb9f33911Jeff Sharkey                }
10357aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            } finally {
10367aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                ContentProviderClient.releaseQuietly(client);
10379d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            }
10389d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            return result;
10399d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        }
10409d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10419d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        @Override
10429d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        protected void onPostExecute(Bitmap result) {
10434ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            if (mIconThumb.getTag() == this && result != null) {
10444ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                mIconThumb.setTag(null);
10454ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                mIconThumb.setImageBitmap(result);
10464ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey
10476efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                final float targetAlpha = mIconMime.isEnabled() ? 1f : 0.5f;
10486efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                mIconMime.setAlpha(targetAlpha);
10494ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                mIconMime.animate().alpha(0f).start();
10504ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                mIconThumb.setAlpha(0f);
10516efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                mIconThumb.animate().alpha(targetAlpha).start();
10529d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            }
10539d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        }
10549d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey    }
10559d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10569d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey    private static String formatTime(Context context, long when) {
10579d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        // TODO: DateUtils should make this easier
10589d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        Time then = new Time();
10599d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        then.set(when);
10609d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        Time now = new Time();
10619d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        now.setToNow();
10629d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10639d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
10649d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                | DateUtils.FORMAT_ABBREV_ALL;
10659d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10669d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        if (then.year != now.year) {
10679d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
10689d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        } else if (then.yearDay != now.yearDay) {
10699d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            flags |= DateUtils.FORMAT_SHOW_DATE;
10709d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        } else {
10719d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            flags |= DateUtils.FORMAT_SHOW_TIME;
10729d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        }
10739d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10749d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        return DateUtils.formatDateTime(context, when, flags);
10759d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey    }
1076b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
1077b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey    private String findCommonMimeType(List<String> mimeTypes) {
1078b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        String[] commonType = mimeTypes.get(0).split("/");
1079b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        if (commonType.length != 2) {
1080b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            return "*/*";
1081b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        }
1082b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
1083b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        for (int i = 1; i < mimeTypes.size(); i++) {
1084b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            String[] type = mimeTypes.get(i).split("/");
1085b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            if (type.length != 2) continue;
1086b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
1087b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            if (!commonType[1].equals(type[1])) {
1088b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                commonType[1] = "*";
1089b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            }
1090b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
1091b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            if (!commonType[0].equals(type[0])) {
1092b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                commonType[0] = "*";
1093b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                commonType[1] = "*";
1094b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                break;
1095b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            }
1096b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        }
1097b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
1098b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        return commonType[0] + "/" + commonType[1];
1099b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey    }
11003f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
11013f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey    private void setEnabledRecursive(View v, boolean enabled) {
1102f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey        if (v == null) return;
11033f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        if (v.isEnabled() == enabled) return;
11043f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        v.setEnabled(enabled);
11053f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
11063f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        if (v instanceof ViewGroup) {
11073f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            final ViewGroup vg = (ViewGroup) v;
11083f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            for (int i = vg.getChildCount() - 1; i >= 0; i--) {
11093f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                setEnabledRecursive(vg.getChildAt(i), enabled);
11103f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            }
11113f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        }
11123f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey    }
11132a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey
11142a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey    private boolean isDocumentEnabled(String docMimeType, int docFlags) {
11152a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey        final State state = getDisplayState(DirectoryFragment.this);
11162a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey
11172a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey        // Directories are always enabled
11182a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey        if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
11192a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey            return true;
11202a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey        }
11212a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey
11223d52dc9c3a2fd9997322ce5e28607b3f7e9bfcf7Jeff Sharkey        // Read-only files are disabled when creating
11233d52dc9c3a2fd9997322ce5e28607b3f7e9bfcf7Jeff Sharkey        if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
11243d52dc9c3a2fd9997322ce5e28607b3f7e9bfcf7Jeff Sharkey            return false;
11253d52dc9c3a2fd9997322ce5e28607b3f7e9bfcf7Jeff Sharkey        }
11263d52dc9c3a2fd9997322ce5e28607b3f7e9bfcf7Jeff Sharkey
11272a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey        return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
11282a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey    }
11299e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey}
1130