DirectoryFragment.java revision db5ef125007644daa94aeaf1bd8637f4e0095e94
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;
50e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkeyimport android.os.Parcelable;
519e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.provider.DocumentsContract;
525dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkeyimport android.provider.DocumentsContract.Document;
5354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.text.format.DateUtils;
54b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkeyimport android.text.format.Formatter;
559d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkeyimport android.text.format.Time;
569d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkeyimport android.util.Log;
57e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkeyimport android.util.SparseArray;
58be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkeyimport android.util.SparseBooleanArray;
59be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkeyimport android.view.ActionMode;
609e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.view.LayoutInflater;
6154e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.view.Menu;
6254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.view.MenuItem;
639e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.view.View;
649e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.view.ViewGroup;
65be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkeyimport android.widget.AbsListView;
66be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkeyimport android.widget.AbsListView.MultiChoiceModeListener;
674ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkeyimport android.widget.AbsListView.RecyclerListener;
6854e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.widget.AdapterView;
6954e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.widget.AdapterView.OnItemClickListener;
70ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkeyimport android.widget.BaseAdapter;
711feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkeyimport android.widget.FrameLayout;
7254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeyimport android.widget.GridView;
739e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.widget.ImageView;
749e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.widget.ListView;
759e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkeyimport android.widget.TextView;
764eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkeyimport android.widget.Toast;
779e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
78b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkeyimport com.android.documentsui.DocumentsActivity.State;
79b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkeyimport com.android.documentsui.ProviderExecutor.Preemptable;
80fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkeyimport com.android.documentsui.RecentsProvider.StateColumns;
81ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkeyimport com.android.documentsui.model.DocumentInfo;
82251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkeyimport com.android.documentsui.model.RootInfo;
83be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkeyimport com.google.android.collect.Lists;
84be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
85be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkeyimport java.util.ArrayList;
86ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkeyimport java.util.List;
8754e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
8854e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey/**
8954e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey * Display the documents inside a single directory.
9054e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey */
9154e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkeypublic class DirectoryFragment extends Fragment {
9254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
939fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey    private View mEmptyView;
9454e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    private ListView mListView;
9554e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    private GridView mGridView;
9654e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
97be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey    private AbsListView mCurrentView;
98be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
99ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey    public static final int TYPE_NORMAL = 1;
100ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey    public static final int TYPE_SEARCH = 2;
101251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey    public static final int TYPE_RECENT_OPEN = 3;
102dc2963aecaf38bf53d6de82957412a486049c207Jeff Sharkey
103e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static final int ANIM_NONE = 1;
104e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static final int ANIM_SIDE = 2;
105e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static final int ANIM_DOWN = 3;
106e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static final int ANIM_UP = 4;
107e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey
108dc2963aecaf38bf53d6de82957412a486049c207Jeff Sharkey    private int mType = TYPE_NORMAL;
109e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    private String mStateKey;
110dc2963aecaf38bf53d6de82957412a486049c207Jeff Sharkey
111d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    private int mLastMode = MODE_UNKNOWN;
112d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    private int mLastSortOrder = SORT_ORDER_UNKNOWN;
113ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey    private boolean mLastShowSize = false;
114d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
115f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey    private boolean mHideGridTitles = false;
116f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
117d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey    private boolean mSvelteRecents;
1189d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey    private Point mThumbSize;
1199d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
1209e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey    private DocumentsAdapter mAdapter;
121b448660a227f93d7bd5c5d019a21745e67c2f54dJeff Sharkey    private LoaderCallbacks<DirectoryResult> mCallbacks;
1229e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
123b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey    private static final String EXTRA_TYPE = "type";
124d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    private static final String EXTRA_ROOT = "root";
125d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    private static final String EXTRA_DOC = "doc";
126251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey    private static final String EXTRA_QUERY = "query";
127e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    private static final String EXTRA_IGNORE_STATE = "ignoreState";
1289e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
129db5ef125007644daa94aeaf1bd8637f4e0095e94Jeff Sharkey    private final int mLoaderId = 42;
130b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey
131e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) {
132e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        show(fm, TYPE_NORMAL, root, doc, null, anim);
133b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey    }
134b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey
135e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) {
136e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        show(fm, TYPE_SEARCH, root, null, query, anim);
137b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey    }
138b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey
139e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public static void showRecentsOpen(FragmentManager fm, int anim) {
140e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        show(fm, TYPE_RECENT_OPEN, null, null, null, anim);
141b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey    }
1429e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
143e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc,
144e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey            String query, int anim) {
1459e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        final Bundle args = new Bundle();
146b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey        args.putInt(EXTRA_TYPE, type);
147d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        args.putParcelable(EXTRA_ROOT, root);
148d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        args.putParcelable(EXTRA_DOC, doc);
149251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey        args.putString(EXTRA_QUERY, query);
1509e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
151e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        final FragmentTransaction ft = fm.beginTransaction();
152e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        switch (anim) {
153e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey            case ANIM_SIDE:
154e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                args.putBoolean(EXTRA_IGNORE_STATE, true);
155e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                break;
156e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey            case ANIM_DOWN:
157e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                args.putBoolean(EXTRA_IGNORE_STATE, true);
158e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen);
159e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                break;
160e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey            case ANIM_UP:
161e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up);
162e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                break;
163e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        }
164e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey
1659e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        final DirectoryFragment fragment = new DirectoryFragment();
1669e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        fragment.setArguments(args);
1679e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
16866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        ft.replace(R.id.container_directory, fragment);
1699e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        ft.commitAllowingStateLoss();
1709e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey    }
1719e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
172e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    private static String buildStateKey(RootInfo root, DocumentInfo doc) {
173e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        final StringBuilder builder = new StringBuilder();
174e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        builder.append(root != null ? root.authority : "null").append(';');
175e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        builder.append(root != null ? root.rootId : "null").append(';');
176e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        builder.append(doc != null ? doc.documentId : "null");
177e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        return builder.toString();
178e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    }
179e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey
180ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey    public static DirectoryFragment get(FragmentManager fm) {
181ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        // TODO: deal with multiple directories shown at once
18266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        return (DirectoryFragment) fm.findFragmentById(R.id.container_directory);
183ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey    }
184ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
1859e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey    @Override
1869e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey    public View onCreateView(
1879e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
1889e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        final Context context = inflater.getContext();
18954e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        final View view = inflater.inflate(R.layout.fragment_directory, container, false);
19054e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
1919fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey        mEmptyView = view.findViewById(android.R.id.empty);
1929fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
19354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        mListView = (ListView) view.findViewById(R.id.list);
19454e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        mListView.setOnItemClickListener(mItemListener);
195be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        mListView.setMultiChoiceModeListener(mMultiListener);
1964ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        mListView.setRecyclerListener(mRecycleListener);
19754e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
19854e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        mGridView = (GridView) view.findViewById(R.id.grid);
19954e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        mGridView.setOnItemClickListener(mItemListener);
200be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        mGridView.setMultiChoiceModeListener(mMultiListener);
2014ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        mGridView.setRecyclerListener(mRecycleListener);
20254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
2035dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey        return view;
2045dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey    }
20554e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
2065dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey    @Override
207d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey    public void onDestroyView() {
208d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        super.onDestroyView();
209d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey
210d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        // Cancel any outstanding thumbnail requests
211d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView;
212d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        final int count = target.getChildCount();
213d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        for (int i = 0; i < count; i++) {
214d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            final View view = target.getChildAt(i);
215d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            mRecycleListener.onMovedToScrapHeap(view);
216d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        }
217ca25db0cb3d6b9686a42c9cb99d90dc095f20cd8Jeff Sharkey
218ca25db0cb3d6b9686a42c9cb99d90dc095f20cd8Jeff Sharkey        // Tear down any selection in progress
219ca25db0cb3d6b9686a42c9cb99d90dc095f20cd8Jeff Sharkey        mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
220ca25db0cb3d6b9686a42c9cb99d90dc095f20cd8Jeff Sharkey        mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
221d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey    }
222d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey
223d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey    @Override
2245dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey    public void onActivityCreated(Bundle savedInstanceState) {
2255dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey        super.onActivityCreated(savedInstanceState);
2265dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey
2275dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey        final Context context = getActivity();
228d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        final State state = getDisplayState(DirectoryFragment.this);
2295dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey
230f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey        final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
231f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey        final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
232f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
233251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey        mAdapter = new DocumentsAdapter();
234b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey        mType = getArguments().getInt(EXTRA_TYPE);
235e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        mStateKey = buildStateKey(root, doc);
2369e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
237f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey        if (mType == TYPE_RECENT_OPEN) {
238f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            // Hide titles when showing recents for picking images/videos
239f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            mHideGridTitles = MimePredicate.mimeMatches(
240f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    MimePredicate.VISUAL_MIMES, state.acceptMimes);
241f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey        } else {
242f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
243f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey        }
244f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
245d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        final ActivityManager am = (ActivityManager) context.getSystemService(
246d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey                Context.ACTIVITY_SERVICE);
247d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN);
248d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey
249b448660a227f93d7bd5c5d019a21745e67c2f54dJeff Sharkey        mCallbacks = new LoaderCallbacks<DirectoryResult>() {
2509e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            @Override
251b448660a227f93d7bd5c5d019a21745e67c2f54dJeff Sharkey            public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
252251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                final String query = getArguments().getString(EXTRA_QUERY);
253251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey
25454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                Uri contentsUri;
255251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                switch (mType) {
256251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                    case TYPE_NORMAL:
257d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                        contentsUri = DocumentsContract.buildChildDocumentsUri(
258d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                                doc.authority, doc.documentId);
2594ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                        if (state.action == ACTION_MANAGE) {
2604ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                            contentsUri = DocumentsContract.setManageMode(contentsUri);
2614ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                        }
262fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                        return new DirectoryLoader(
2634ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                                context, mType, root, doc, contentsUri, state.userSortOrder);
264251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                    case TYPE_SEARCH:
265251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                        contentsUri = DocumentsContract.buildSearchDocumentsUri(
2663e1189b3590aefb65a2af720ae2ba959bbd4188dJeff Sharkey                                root.authority, root.rootId, query);
2674ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                        if (state.action == ACTION_MANAGE) {
2684ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                            contentsUri = DocumentsContract.setManageMode(contentsUri);
2694ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                        }
270fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                        return new DirectoryLoader(
2714ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                                context, mType, root, doc, contentsUri, state.userSortOrder);
272251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                    case TYPE_RECENT_OPEN:
273348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey                        final RootsCache roots = DocumentsApplication.getRootsCache(context);
274a9ce049db87259e302e2368d2a4a1c11a94fd831Jeff Sharkey                        return new RecentLoader(context, roots, state);
275251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                    default:
276251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                        throw new IllegalStateException("Unknown type " + mType);
277251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                }
2789e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            }
2799e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
2809e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            @Override
281b448660a227f93d7bd5c5d019a21745e67c2f54dJeff Sharkey            public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
282d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                if (!isAdded()) return;
283d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
284d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey                mAdapter.swapResult(result);
285d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
286d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                // Push latest state up to UI
287d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                // TODO: if mode change was racing with us, don't overwrite it
28888f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                if (result.mode != MODE_UNKNOWN) {
28988f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    state.derivedMode = result.mode;
29088f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                }
291fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                state.derivedSortOrder = result.sortOrder;
292d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                ((DocumentsActivity) context).onStateChanged();
293d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
294d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                updateDisplayState();
295d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
296a82c2e24bc226c123075331cdaafb171b3b07b85Jeff Sharkey                // When launched into empty recents, show drawer
297a82c2e24bc226c123075331cdaafb171b3b07b85Jeff Sharkey                if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched) {
298a82c2e24bc226c123075331cdaafb171b3b07b85Jeff Sharkey                    ((DocumentsActivity) context).setRootsDrawerOpen(true);
299a82c2e24bc226c123075331cdaafb171b3b07b85Jeff Sharkey                }
300a82c2e24bc226c123075331cdaafb171b3b07b85Jeff Sharkey
301e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                // Restore any previous instance state
302e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
303e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
304e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                    getView().restoreHierarchyState(container);
305e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                } else if (mLastSortOrder != state.derivedSortOrder) {
306d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                    mListView.smoothScrollToPosition(0);
307d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                    mGridView.smoothScrollToPosition(0);
308d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                }
309e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey
310e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey                mLastSortOrder = state.derivedSortOrder;
3119e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            }
3129e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
3139e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            @Override
314b448660a227f93d7bd5c5d019a21745e67c2f54dJeff Sharkey            public void onLoaderReset(Loader<DirectoryResult> loader) {
315d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey                mAdapter.swapResult(null);
3169e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            }
3179e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        };
3189e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
319d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        // Kick off loader at least once
320d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
321d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
322b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey        updateDisplayState();
32354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    }
3249e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
32528c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey    @Override
326e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    public void onStop() {
327e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        super.onStop();
328e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey
329e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        // Remember last scroll location
330e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        final SparseArray<Parcelable> container = new SparseArray<Parcelable>();
331e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        getView().saveHierarchyState(container);
332e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        final State state = getDisplayState(this);
333e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey        state.dirState.put(mStateKey, container);
334e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    }
335e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey
336e20a3acdc2d52c7eeb76940206145b3c419394a6Jeff Sharkey    @Override
33788f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey    public void onResume() {
33888f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey        super.onResume();
33928c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey        updateDisplayState();
34028c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey    }
34128c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey
342d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    public void onUserSortOrderChanged() {
343fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        // Sort order change always triggers reload; we'll trigger state change
344fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        // on the flip side.
345d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
346d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    }
347d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
348d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    public void onUserModeChanged() {
349fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        final ContentResolver resolver = getActivity().getContentResolver();
350fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        final State state = getDisplayState(this);
351fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey
352fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
353fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
354fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey
3553e1189b3590aefb65a2af720ae2ba959bbd4188dJeff Sharkey        if (root != null && doc != null) {
35688f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            final Uri stateUri = RecentsProvider.buildState(
35788f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    root.authority, root.rootId, doc.documentId);
35888f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            final ContentValues values = new ContentValues();
35988f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            values.put(StateColumns.MODE, state.userMode);
36088f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey
36188f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            new AsyncTask<Void, Void, Void>() {
36288f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                @Override
36388f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                protected Void doInBackground(Void... params) {
36488f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    resolver.insert(stateUri, values);
36588f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    return null;
36688f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                }
36788f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            }.execute();
36888f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey        }
369fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey
370fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        // Mode change is just visual change; no need to kick loader, and
371fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        // deliver change event immediately.
372fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        state.derivedMode = state.userMode;
373fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        ((DocumentsActivity) getActivity()).onStateChanged();
374fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey
375d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey        updateDisplayState();
376d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    }
377d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey
378d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey    private void updateDisplayState() {
379b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        final State state = getDisplayState(this);
38054e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
381ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey        if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return;
382fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        mLastMode = state.derivedMode;
383ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey        mLastShowSize = state.showSize;
38454e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
385fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
386fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
38754e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
388be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        final int choiceMode;
3891482789374fb8da3abea8f6f4f272a67205b95dbJeff Sharkey        if (state.allowMultiple) {
390be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
391be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        } else {
392be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            choiceMode = ListView.CHOICE_MODE_NONE;
393be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        }
394be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
3959d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        final int thumbSize;
396fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        if (state.derivedMode == MODE_GRID) {
3979d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
39854e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey            mListView.setAdapter(null);
399be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
40054e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey            mGridView.setAdapter(mAdapter);
401be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width));
40254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey            mGridView.setNumColumns(GridView.AUTO_FIT);
403be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mGridView.setChoiceMode(choiceMode);
404be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mCurrentView = mGridView;
405fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey        } else if (state.derivedMode == MODE_LIST) {
406d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey            thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
40754e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey            mGridView.setAdapter(null);
408be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
40954e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey            mListView.setAdapter(mAdapter);
410be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mListView.setChoiceMode(choiceMode);
411be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mCurrentView = mListView;
4121482789374fb8da3abea8f6f4f272a67205b95dbJeff Sharkey        } else {
413fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey            throw new IllegalStateException("Unknown state " + state.derivedMode);
41454e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        }
4159d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
4169d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        mThumbSize = new Point(thumbSize, thumbSize);
41754e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    }
41854e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
41954e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    private OnItemClickListener mItemListener = new OnItemClickListener() {
42054e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        @Override
42154e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
4225dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final Cursor cursor = mAdapter.getItem(position);
4233f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            if (cursor != null) {
4242a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
4252a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
4262a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                if (isDocumentEnabled(docMimeType, docFlags)) {
4272a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                    final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
4283f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                    ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
4293f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                }
430a5599ef636e37cb0b6474349936999be1afe6987Jeff Sharkey            }
43154e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        }
43254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    };
43354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
434be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey    private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() {
435be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        @Override
436be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
437be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mode.getMenuInflater().inflate(R.menu.mode_directory, menu);
438be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            return true;
439be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        }
440be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
441be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        @Override
442be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
443b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            final State state = getDisplayState(DirectoryFragment.this);
4444eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
4454eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final MenuItem open = menu.findItem(R.id.menu_open);
4464eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final MenuItem share = menu.findItem(R.id.menu_share);
4474eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final MenuItem delete = menu.findItem(R.id.menu_delete);
4484eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
4494eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final boolean manageMode = state.action == ACTION_MANAGE;
4504eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            open.setVisible(!manageMode);
4514eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            share.setVisible(manageMode);
4524eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            delete.setVisible(manageMode);
4534eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
454be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            return true;
455be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        }
456be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
457be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        @Override
458be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
4594eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
460ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey            final ArrayList<DocumentInfo> docs = Lists.newArrayList();
4614eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final int size = checked.size();
4624eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            for (int i = 0; i < size; i++) {
4634eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                if (checked.valueAt(i)) {
4645dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                    final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
465251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                    final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
4664eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                    docs.add(doc);
467be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                }
4684eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            }
4694eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
4704eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final int id = item.getItemId();
4714eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            if (id == R.id.menu_open) {
4724eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
473b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                mode.finish();
4744eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                return true;
475be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
4764eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            } else if (id == R.id.menu_share) {
4774eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                onShareDocuments(docs);
478b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                mode.finish();
479be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                return true;
4804eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
4814eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            } else if (id == R.id.menu_delete) {
4824eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                onDeleteDocuments(docs);
483b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                mode.finish();
4844eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                return true;
4854eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
486be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            } else {
487be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                return false;
488be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            }
489be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        }
490be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
491be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        @Override
492be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        public void onDestroyActionMode(ActionMode mode) {
493be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            // ignored
494be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        }
495be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
496be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        @Override
497be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        public void onItemCheckedStateChanged(
498be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                ActionMode mode, int position, long id, boolean checked) {
499be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            if (checked) {
5003f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                // Directories and footer items cannot be checked
5013f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                boolean valid = false;
5023f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
5035dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                final Cursor cursor = mAdapter.getItem(position);
5043f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                if (cursor != null) {
5053f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                    final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
5062a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                    final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
5072a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                    if (!Document.MIME_TYPE_DIR.equals(docMimeType)) {
5082a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                        valid = isDocumentEnabled(docMimeType, docFlags);
5092a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey                    }
5103f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                }
5113f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
5123f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                if (!valid) {
513be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                    mCurrentView.setItemChecked(position, false);
514be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                }
515be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            }
516be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
517be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey            mode.setTitle(getResources()
518be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey                    .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount()));
519be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey        }
520be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey    };
521be8b12e687bd10a526b1f54c2d8a52abdad15d85Jeff Sharkey
5224ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey    private RecyclerListener mRecycleListener = new RecyclerListener() {
5234ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        @Override
5244ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        public void onMovedToScrapHeap(View view) {
5254ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb);
5264ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            if (iconThumb != null) {
5274ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
5284ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                if (oldTask != null) {
529b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey                    oldTask.preempt();
5304ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    iconThumb.setTag(null);
5314ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                }
5324ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            }
5334ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        }
5344ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey    };
5354ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey
536ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey    private void onShareDocuments(List<DocumentInfo> docs) {
537b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        Intent intent;
538b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        if (docs.size() == 1) {
539b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            final DocumentInfo doc = docs.get(0);
540b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
541b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            intent = new Intent(Intent.ACTION_SEND);
542b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
543b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            intent.addCategory(Intent.CATEGORY_DEFAULT);
544b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            intent.setType(doc.mimeType);
545d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey            intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
5464eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
547b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        } else if (docs.size() > 1) {
5484eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
5494eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
5504eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            intent.addCategory(Intent.CATEGORY_DEFAULT);
551b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
552b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            final ArrayList<String> mimeTypes = Lists.newArrayList();
553b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            final ArrayList<Uri> uris = Lists.newArrayList();
554b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            for (DocumentInfo doc : docs) {
555b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                mimeTypes.add(doc.mimeType);
556d182bb641f228b2d28527a6aa86075f6358ab838Jeff Sharkey                uris.add(doc.derivedUri);
557b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            }
558b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
559b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            intent.setType(findCommonMimeType(mimeTypes));
5604eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
561b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
5624eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        } else {
563b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            return;
5644eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        }
5654eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
566b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
5674eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        startActivity(intent);
5684eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey    }
5694eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
570ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey    private void onDeleteDocuments(List<DocumentInfo> docs) {
5714eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        final Context context = getActivity();
5724eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        final ContentResolver resolver = context.getContentResolver();
5734eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
5744eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        boolean hadTrouble = false;
575ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey        for (DocumentInfo doc : docs) {
5764eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            if (!doc.isDeleteSupported()) {
5774eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                Log.w(TAG, "Skipping " + doc);
5784eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                hadTrouble = true;
5794eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                continue;
5804eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            }
5814eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
5827aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            ContentProviderClient client = null;
5837aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            try {
5847aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                client = DocumentsApplication.acquireUnstableProviderOrThrow(
5857aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                        resolver, doc.derivedUri.getAuthority());
5867aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                DocumentsContract.deleteDocument(client, doc.derivedUri);
5877aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            } catch (Exception e) {
588ded77187ef53341765fcab8e29cda94810fc2ca5Jeff Sharkey                Log.w(TAG, "Failed to delete " + doc);
5894eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                hadTrouble = true;
5907aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            } finally {
5917aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                ContentProviderClient.releaseQuietly(client);
5924eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            }
5934eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        }
5944eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
5954eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        if (hadTrouble) {
5964eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show();
5974eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        }
5984eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey    }
5994eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
600b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey    private static State getDisplayState(Fragment fragment) {
6011482789374fb8da3abea8f6f4f272a67205b95dbJeff Sharkey        return ((DocumentsActivity) fragment.getActivity()).getDisplayState();
60254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey    }
60354e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
6043f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey    private static abstract class Footer {
6053f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        private final int mItemViewType;
6063f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
6073f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        public Footer(int itemViewType) {
6083f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            mItemViewType = itemViewType;
6093f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        }
6103f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
6113f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        public abstract View getView(View convertView, ViewGroup parent);
6123f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
6133f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        public int getItemViewType() {
6143f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            return mItemViewType;
6153f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        }
616954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey    }
617954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
618ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey    private class LoadingFooter extends Footer {
6193f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        public LoadingFooter() {
6203f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            super(1);
6213f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        }
6223f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
623954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        @Override
624954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        public View getView(View convertView, ViewGroup parent) {
625954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            final Context context = parent.getContext();
626ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey            final State state = getDisplayState(DirectoryFragment.this);
627ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey
628954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            if (convertView == null) {
629954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                final LayoutInflater inflater = LayoutInflater.from(context);
630ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                if (state.derivedMode == MODE_LIST) {
631ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                    convertView = inflater.inflate(R.layout.item_loading_list, parent, false);
632ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                } else if (state.derivedMode == MODE_GRID) {
633ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                    convertView = inflater.inflate(R.layout.item_loading_grid, parent, false);
634ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                } else {
635ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                    throw new IllegalStateException();
636ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey                }
637954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            }
638ed903213e6d3d75d497498c9cf95aa8e74277a9eJeff Sharkey
639954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            return convertView;
640954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        }
641954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey    }
642954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
6433f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey    private class MessageFooter extends Footer {
644954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        private final int mIcon;
645954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        private final String mMessage;
646954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
6473f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        public MessageFooter(int itemViewType, int icon, String message) {
6483f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            super(itemViewType);
649954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            mIcon = icon;
650954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            mMessage = message;
651954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        }
652954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
653954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        @Override
654954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        public View getView(View convertView, ViewGroup parent) {
655954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            final Context context = parent.getContext();
656954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            final State state = getDisplayState(DirectoryFragment.this);
657954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
658954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            if (convertView == null) {
659954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                final LayoutInflater inflater = LayoutInflater.from(context);
660fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                if (state.derivedMode == MODE_LIST) {
661954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                    convertView = inflater.inflate(R.layout.item_message_list, parent, false);
662fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                } else if (state.derivedMode == MODE_GRID) {
663954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                    convertView = inflater.inflate(R.layout.item_message_grid, parent, false);
664954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                } else {
665954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                    throw new IllegalStateException();
666954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                }
667954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            }
668954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
669954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
670954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
671954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            icon.setImageResource(mIcon);
672954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            title.setText(mMessage);
673954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            return convertView;
674954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        }
675954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey    }
676954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
677ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey    private class DocumentsAdapter extends BaseAdapter {
6785dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey        private Cursor mCursor;
679954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        private int mCursorCount;
680954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
681954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        private List<Footer> mFooters = Lists.newArrayList();
682ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
683d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey        public void swapResult(DirectoryResult result) {
684d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            mCursor = result != null ? result.cursor : null;
685d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            mCursorCount = mCursor != null ? mCursor.getCount() : 0;
686954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
687954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            mFooters.clear();
688954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
689d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            final Bundle extras = mCursor != null ? mCursor.getExtras() : null;
690954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            if (extras != null) {
691954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                final String info = extras.getString(DocumentsContract.EXTRA_INFO);
692954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                if (info != null) {
6931feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkey                    mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info));
694954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                }
695954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                final String error = extras.getString(DocumentsContract.EXTRA_ERROR);
696954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                if (error != null) {
6973f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                    mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error));
698954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                }
699954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) {
700954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                    mFooters.add(new LoadingFooter());
701954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                }
702954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            }
7039fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
704d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            if (result != null && result.exception != null) {
7057aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                mFooters.add(new MessageFooter(
7067aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                        3, R.drawable.ic_dialog_alert, getString(R.string.query_error)));
7077aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            }
7087aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey
7095dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            if (isEmpty()) {
7109fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                mEmptyView.setVisibility(View.VISIBLE);
7119fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            } else {
7129fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                mEmptyView.setVisibility(View.GONE);
7139fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            }
7149fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
715ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            notifyDataSetChanged();
7169e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        }
7179e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
7189e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey        @Override
719ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        public View getView(int position, View convertView, ViewGroup parent) {
720954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            if (position < mCursorCount) {
721954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                return getDocumentView(position, convertView, parent);
722954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            } else {
723954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                position -= mCursorCount;
7243f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                convertView = mFooters.get(position).getView(convertView, parent);
7253f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                // Only the view itself is disabled; contents inside shouldn't
7263f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                // be dimmed.
7273f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                convertView.setEnabled(false);
7283f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                return convertView;
729954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            }
730954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        }
731954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
732954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        private View getDocumentView(int position, View convertView, ViewGroup parent) {
733ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            final Context context = parent.getContext();
734b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            final State state = getDisplayState(DirectoryFragment.this);
735ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
736f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
737f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
7384eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final RootsCache roots = DocumentsApplication.getRootsCache(context);
7394eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
7404eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                    context, mThumbSize);
7414eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey
742ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            if (convertView == null) {
743ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey                final LayoutInflater inflater = LayoutInflater.from(context);
744fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                if (state.derivedMode == MODE_LIST) {
745ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey                    convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
746fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey                } else if (state.derivedMode == MODE_GRID) {
747ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey                    convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);
7481feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkey
7491feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkey                    // Apply padding to grid items
7501feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkey                    final FrameLayout grid = (FrameLayout) convertView;
7511feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkey                    final int gridPadding = getResources()
7521feeddf518d31a7ff5d28000a631857442cd7161Jeff Sharkey                            .getDimensionPixelSize(R.dimen.grid_padding);
753deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey
754deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    // Tricksy hobbitses! We need to fully clear the drawable so
755deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    // the view doesn't clobber the new InsetDrawable callback
756deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    // when setting back later.
757deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    final Drawable fg = grid.getForeground();
758deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    final Drawable bg = grid.getBackground();
759deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    grid.setForeground(null);
760deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    grid.setBackground(null);
761deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    grid.setForeground(new InsetDrawable(fg, gridPadding));
762deffadeb7485e8660ecce12822e259d96fa06dceJeff Sharkey                    grid.setBackground(new InsetDrawable(bg, gridPadding));
763ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey                } else {
764ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey                    throw new IllegalStateException();
765ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey                }
766ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            }
767ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
7685dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final Cursor cursor = getItem(position);
7695dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey
770251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey            final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
771251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey            final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID);
7725dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
7735dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
7745dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
7755dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
7765dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
7775dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
7785dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
7795dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
780ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
781f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            final View line1 = convertView.findViewById(R.id.line1);
782f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            final View line2 = convertView.findViewById(R.id.line2);
783f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
7844ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
7854ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb);
786ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
787b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey            final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1);
788f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2);
789ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
790b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey            final TextView date = (TextView) convertView.findViewById(R.id.date);
791b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey            final TextView size = (TextView) convertView.findViewById(R.id.size);
792ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
7934ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
7949d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            if (oldTask != null) {
795b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey                oldTask.preempt();
7964ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                iconThumb.setTag(null);
7979d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            }
7989d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
7994ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            iconMime.animate().cancel();
8004ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            iconThumb.animate().cancel();
8014ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey
8023f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
803fb3445c9b31c7f8401d6eec0606dabee366c8aadJeff Sharkey            final boolean allowThumbnail = (state.derivedMode == MODE_GRID)
804f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType);
805d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents;
8063f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
8074ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            boolean cacheHit = false;
808f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            if (showThumbnail) {
809251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
8105dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                final Bitmap cachedResult = thumbs.get(uri);
8119d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                if (cachedResult != null) {
8124ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    iconThumb.setImageBitmap(cachedResult);
8134ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    cacheHit = true;
8149d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                } else {
8154ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    iconThumb.setImageDrawable(null);
8164ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
8174ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                            uri, iconMime, iconThumb, mThumbSize);
8184ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    iconThumb.setTag(task);
819b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey                    ProviderExecutor.forAuthority(docAuthority).execute(task);
8209d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                }
8214ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            }
8224ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey
8234ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            // Always throw MIME icon into place, even when a thumbnail is being
8244ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            // loaded in background.
8254ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            if (cacheHit) {
8264ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                iconMime.setAlpha(0f);
8276efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                iconMime.setImageDrawable(null);
8284ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                iconThumb.setAlpha(1f);
8299e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            } else {
8304ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                iconMime.setAlpha(1f);
8314ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                iconThumb.setAlpha(0f);
8326efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                iconThumb.setImageDrawable(null);
8334ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                if (docIcon != 0) {
8344ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    iconMime.setImageDrawable(
8354ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                            IconUtils.loadPackageIcon(context, docAuthority, docIcon));
8364ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                } else {
837563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey                    iconMime.setImageDrawable(IconUtils.loadMimeIcon(
838563ee0fbe99c234f3364044cb762a75abbbaa797Jeff Sharkey                            context, docMimeType, docAuthority, docId, state.derivedMode));
8394ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                }
8409e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey            }
8419e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
842f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            boolean hasLine1 = false;
84328c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey            boolean hasLine2 = false;
84428c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey
845f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles;
846f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            if (!hideTitle) {
847f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                title.setText(docDisplayName);
848f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                hasLine1 = true;
849f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            }
850f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
851f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            Drawable iconDrawable = null;
852251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey            if (mType == TYPE_RECENT_OPEN) {
853a9ce049db87259e302e2368d2a4a1c11a94fd831Jeff Sharkey                // We've already had to enumerate roots before any results can
854a9ce049db87259e302e2368d2a4a1c11a94fd831Jeff Sharkey                // be shown, so this will never block.
855a9ce049db87259e302e2368d2a4a1c11a94fd831Jeff Sharkey                final RootInfo root = roots.getRootBlocking(docAuthority, docRootId);
856f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                iconDrawable = root.loadIcon(context);
8574ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey
85888f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                if (summary != null) {
85988f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    final boolean alwaysShowSummary = getResources()
86088f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            .getBoolean(R.bool.always_show_summary);
86188f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    if (alwaysShowSummary) {
86288f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        summary.setText(root.getDirectoryString());
86388f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        summary.setVisibility(View.VISIBLE);
86488f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        hasLine2 = true;
86588f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    } else {
866a9ce049db87259e302e2368d2a4a1c11a94fd831Jeff Sharkey                        if (iconDrawable != null && roots.isIconUniqueBlocking(root)) {
86788f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            // No summary needed if icon speaks for itself
86888f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            summary.setVisibility(View.INVISIBLE);
86988f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        } else {
87088f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            summary.setText(root.getDirectoryString());
87188f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            summary.setVisibility(View.VISIBLE);
87288f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
87388f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                            hasLine2 = true;
87488f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        }
87588f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    }
8764ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                }
8775dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            } else {
878f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                // Directories showing thumbnails in grid mode get a little icon
879f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                // hint to remind user they're a directory.
880f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID
881f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                        && showThumbnail) {
882f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    iconDrawable = context.getResources().getDrawable(R.drawable.ic_root_folder);
883f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                }
884f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
88588f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                if (summary != null) {
88688f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    if (docSummary != null) {
88788f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        summary.setText(docSummary);
88888f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        summary.setVisibility(View.VISIBLE);
88988f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        hasLine2 = true;
89088f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    } else {
89188f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                        summary.setVisibility(View.INVISIBLE);
89288f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                    }
893251097b3789632000ccdaf7fb7d66a82ff37d882Jeff Sharkey                }
894b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey            }
895b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey
896f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            if (icon1 != null) icon1.setVisibility(View.GONE);
897f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            if (icon2 != null) icon2.setVisibility(View.GONE);
898f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
899f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            if (iconDrawable != null) {
900f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                if (hasLine1) {
901f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    icon1.setVisibility(View.VISIBLE);
902f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    icon1.setImageDrawable(iconDrawable);
903f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                } else {
904f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    icon2.setVisibility(View.VISIBLE);
905f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                    icon2.setImageDrawable(iconDrawable);
906f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                }
907f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            }
908f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey
9095dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            if (docLastModified == -1) {
9109d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                date.setText(null);
9119d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            } else {
9125dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                date.setText(formatTime(context, docLastModified));
91328c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey                hasLine2 = true;
9149d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            }
915b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey
916b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey            if (state.showSize) {
917b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey                size.setVisibility(View.VISIBLE);
9185dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
919b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey                    size.setText(null);
920b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey                } else {
9215dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                    size.setText(Formatter.formatFileSize(context, docSize));
92228c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey                    hasLine2 = true;
923b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey                }
924b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey            } else {
925b156f4bf8cdfe475a7116b627d84a281e1a281b7Jeff Sharkey                size.setVisibility(View.GONE);
92654e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey            }
927ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey
928f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            if (line1 != null) {
929f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE);
930f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey            }
93188f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            if (line2 != null) {
93288f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey                line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
93388f322c3115e85bb96fb46f9192b5daecd472c64Jeff Sharkey            }
93428c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey
9352a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey            final boolean enabled = isDocumentEnabled(docMimeType, docFlags);
9363f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            if (enabled) {
9373f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                setEnabledRecursive(convertView, true);
9386efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                iconMime.setAlpha(1f);
9396efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                iconThumb.setAlpha(1f);
940f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                if (icon1 != null) icon1.setAlpha(1f);
941f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                if (icon2 != null) icon2.setAlpha(1f);
9423f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            } else {
9433f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                setEnabledRecursive(convertView, false);
9446efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                iconMime.setAlpha(0.5f);
9456efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                iconThumb.setAlpha(0.5f);
946f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                if (icon1 != null) icon1.setAlpha(0.5f);
947f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey                if (icon2 != null) icon2.setAlpha(0.5f);
9483f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            }
9493f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
950ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey            return convertView;
95154e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey        }
95254e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
953ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        @Override
954ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        public int getCount() {
955954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            return mCursorCount + mFooters.size();
956ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        }
9579e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey
958ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        @Override
9595dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey        public Cursor getItem(int position) {
960954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            if (position < mCursorCount) {
9615dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey                mCursor.moveToPosition(position);
962954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                return mCursor;
963954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            } else {
964954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                return null;
9655dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            }
966ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        }
96754e55b740fef1be654c3959aee41ef5ddfa61293Jeff Sharkey
968ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        @Override
969ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        public long getItemId(int position) {
9705dfb345df7cb17b3a7e534a80a270b4afe7934daJeff Sharkey            return position;
971ef7184a1aa0be5d496a5cb495a0f9e11f342af44Jeff Sharkey        }
972954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey
973954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        @Override
9743f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        public int getViewTypeCount() {
9753f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            return 4;
9763f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        }
9773f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
9783f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        @Override
979954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        public int getItemViewType(int position) {
980954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            if (position < mCursorCount) {
981954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey                return 0;
982954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            } else {
9833f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                position -= mCursorCount;
9843f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                return mFooters.get(position).getItemViewType();
985954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey            }
986954be0232655d316bc5decbbd35579af902c75c2Jeff Sharkey        }
9879e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey    }
9889d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
989b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey    private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
990b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey            implements Preemptable {
9914ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        private final Uri mUri;
9924ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        private final ImageView mIconMime;
9934ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        private final ImageView mIconThumb;
9944eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        private final Point mThumbSize;
9954ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        private final CancellationSignal mSignal;
9969d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
9974ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey        public ThumbnailAsyncTask(
9984ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) {
9994ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            mUri = uri;
10004ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            mIconMime = iconMime;
10014ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            mIconThumb = iconThumb;
10024eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey            mThumbSize = thumbSize;
10034ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            mSignal = new CancellationSignal();
10049d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        }
10059d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
1006b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey        @Override
1007b3d42635aafacd80b1e1d257338ea6abb93d22c3Jeff Sharkey        public void preempt() {
10084ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            cancel(false);
10094ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            mSignal.cancel();
10109d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        }
10119d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10129d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        @Override
10139d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        protected Bitmap doInBackground(Uri... params) {
1014d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey            if (isCancelled()) return null;
1015d01571e6d4e1c403534e19142720530d324eac9bJeff Sharkey
10164ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            final Context context = mIconThumb.getContext();
10177aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            final ContentResolver resolver = context.getContentResolver();
10189d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10197aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            ContentProviderClient client = null;
10209d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            Bitmap result = null;
10219d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            try {
10227aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                client = DocumentsApplication.acquireUnstableProviderOrThrow(
10237aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                        resolver, mUri.getAuthority());
10247aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal);
10259d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                if (result != null) {
10264eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                    final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache(
10274eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey                            context, mThumbSize);
10284ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                    thumbs.put(mUri, result);
10299d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                }
10309d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            } catch (Exception e) {
10317aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e);
10327aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey            } finally {
10337aa7601c09ab5d87cc15a0ed9a8f511d494a4cbcJeff Sharkey                ContentProviderClient.releaseQuietly(client);
10349d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            }
10359d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            return result;
10369d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        }
10379d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10389d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        @Override
10399d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        protected void onPostExecute(Bitmap result) {
10404ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey            if (mIconThumb.getTag() == this && result != null) {
10414ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                mIconThumb.setTag(null);
10424ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                mIconThumb.setImageBitmap(result);
10434ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey
10446efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                final float targetAlpha = mIconMime.isEnabled() ? 1f : 0.5f;
10456efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                mIconMime.setAlpha(targetAlpha);
10464ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                mIconMime.animate().alpha(0f).start();
10474ec973925fc2cd18f9ec0d0ca5af588564fded27Jeff Sharkey                mIconThumb.setAlpha(0f);
10486efba22ce510352bb84910d6efc42fecafd31ed7Jeff Sharkey                mIconThumb.animate().alpha(targetAlpha).start();
10499d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            }
10509d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        }
10519d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey    }
10529d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10539d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey    private static String formatTime(Context context, long when) {
10549d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        // TODO: DateUtils should make this easier
10559d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        Time then = new Time();
10569d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        then.set(when);
10579d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        Time now = new Time();
10589d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        now.setToNow();
10599d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10609d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
10619d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey                | DateUtils.FORMAT_ABBREV_ALL;
10629d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10639d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        if (then.year != now.year) {
10649d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
10659d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        } else if (then.yearDay != now.yearDay) {
10669d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            flags |= DateUtils.FORMAT_SHOW_DATE;
10679d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        } else {
10689d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey            flags |= DateUtils.FORMAT_SHOW_TIME;
10699d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        }
10709d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey
10719d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey        return DateUtils.formatDateTime(context, when, flags);
10729d0843df7e3984293dc4ab6ee2f9502e898b63aaJeff Sharkey    }
1073b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
1074b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey    private String findCommonMimeType(List<String> mimeTypes) {
1075b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        String[] commonType = mimeTypes.get(0).split("/");
1076b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        if (commonType.length != 2) {
1077b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            return "*/*";
1078b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        }
1079b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
1080b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        for (int i = 1; i < mimeTypes.size(); i++) {
1081b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            String[] type = mimeTypes.get(i).split("/");
1082b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            if (type.length != 2) continue;
1083b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
1084b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            if (!commonType[1].equals(type[1])) {
1085b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                commonType[1] = "*";
1086b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            }
1087b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
1088b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            if (!commonType[0].equals(type[0])) {
1089b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                commonType[0] = "*";
1090b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                commonType[1] = "*";
1091b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey                break;
1092b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey            }
1093b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        }
1094b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey
1095b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey        return commonType[0] + "/" + commonType[1];
1096b51331116eb2ebbc41aaf69142916f9af6dffdd5Jeff Sharkey    }
10973f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
10983f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey    private void setEnabledRecursive(View v, boolean enabled) {
1099f6db154975ef575479ba4ab59d80bcf592288252Jeff Sharkey        if (v == null) return;
11003f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        if (v.isEnabled() == enabled) return;
11013f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        v.setEnabled(enabled);
11023f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey
11033f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        if (v instanceof ViewGroup) {
11043f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            final ViewGroup vg = (ViewGroup) v;
11053f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            for (int i = vg.getChildCount() - 1; i >= 0; i--) {
11063f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                setEnabledRecursive(vg.getChildAt(i), enabled);
11073f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            }
11083f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey        }
11093f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey    }
11102a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey
11112a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey    private boolean isDocumentEnabled(String docMimeType, int docFlags) {
11122a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey        final State state = getDisplayState(DirectoryFragment.this);
11132a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey
11142a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey        // Directories are always enabled
11152a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey        if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
11162a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey            return true;
11172a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey        }
11182a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey
11193d52dc9c3a2fd9997322ce5e28607b3f7e9bfcf7Jeff Sharkey        // Read-only files are disabled when creating
11203d52dc9c3a2fd9997322ce5e28607b3f7e9bfcf7Jeff Sharkey        if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) {
11213d52dc9c3a2fd9997322ce5e28607b3f7e9bfcf7Jeff Sharkey            return false;
11223d52dc9c3a2fd9997322ce5e28607b3f7e9bfcf7Jeff Sharkey        }
11233d52dc9c3a2fd9997322ce5e28607b3f7e9bfcf7Jeff Sharkey
11242a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey        return MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
11252a030b05a978281147df4d1cc4f12bc8d61c0729Jeff Sharkey    }
11269e0036ed7d3260d79cc5f9ffd8e3bbe760699924Jeff Sharkey}
1127