Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.documentsui; |
| 18 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.DocumentsActivity.TAG; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame^] | 20 | import static com.android.documentsui.BaseActivity.State.ACTION_CREATE; |
| 21 | import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE; |
| 22 | import static com.android.documentsui.BaseActivity.State.MODE_GRID; |
| 23 | import static com.android.documentsui.BaseActivity.State.MODE_LIST; |
| 24 | import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN; |
| 25 | import static com.android.documentsui.BaseActivity.State.SORT_ORDER_UNKNOWN; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 26 | import static com.android.documentsui.model.DocumentInfo.getCursorInt; |
| 27 | import static com.android.documentsui.model.DocumentInfo.getCursorLong; |
| 28 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 29 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 30 | import android.app.ActivityManager; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 31 | import android.app.Fragment; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 32 | import android.app.FragmentManager; |
| 33 | import android.app.FragmentTransaction; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 34 | import android.app.LoaderManager.LoaderCallbacks; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 35 | import android.content.ContentProviderClient; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 36 | import android.content.ContentResolver; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 37 | import android.content.ContentValues; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 38 | import android.content.Context; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 39 | import android.content.Intent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 40 | import android.content.Loader; |
Jeff Sharkey | 083d7e1 | 2014-07-27 21:01:45 -0700 | [diff] [blame] | 41 | import android.content.res.Resources; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 42 | import android.database.Cursor; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 43 | import android.graphics.Bitmap; |
| 44 | import android.graphics.Point; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 45 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | db884f5 | 2013-09-21 15:11:19 -0700 | [diff] [blame] | 46 | import android.graphics.drawable.InsetDrawable; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 47 | import android.net.Uri; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 48 | import android.os.AsyncTask; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 49 | import android.os.Bundle; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 50 | import android.os.CancellationSignal; |
Jeff Sharkey | e39a89b | 2013-10-29 11:56:37 -0700 | [diff] [blame] | 51 | import android.os.OperationCanceledException; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 52 | import android.os.Parcelable; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 53 | import android.provider.DocumentsContract; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 54 | import android.provider.DocumentsContract.Document; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 55 | import android.text.format.DateUtils; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 56 | import android.text.format.Formatter; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 57 | import android.text.format.Time; |
| 58 | import android.util.Log; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 59 | import android.util.SparseArray; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 60 | import android.util.SparseBooleanArray; |
| 61 | import android.view.ActionMode; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 62 | import android.view.LayoutInflater; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 63 | import android.view.Menu; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 64 | import android.view.MenuItem; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 65 | import android.view.View; |
| 66 | import android.view.ViewGroup; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 67 | import android.widget.AbsListView; |
| 68 | import android.widget.AbsListView.MultiChoiceModeListener; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 69 | import android.widget.AbsListView.RecyclerListener; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 70 | import android.widget.AdapterView; |
| 71 | import android.widget.AdapterView.OnItemClickListener; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 72 | import android.widget.BaseAdapter; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 73 | import android.widget.GridView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 74 | import android.widget.ImageView; |
| 75 | import android.widget.ListView; |
| 76 | import android.widget.TextView; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 77 | import android.widget.Toast; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 78 | |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame^] | 79 | import com.android.documentsui.BaseActivity.State; |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 80 | import com.android.documentsui.ProviderExecutor.Preemptable; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 81 | import com.android.documentsui.RecentsProvider.StateColumns; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 82 | import com.android.documentsui.model.DocumentInfo; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 83 | import com.android.documentsui.model.RootInfo; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 84 | import com.google.android.collect.Lists; |
| 85 | |
| 86 | import java.util.ArrayList; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 87 | import java.util.List; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Display the documents inside a single directory. |
| 91 | */ |
| 92 | public class DirectoryFragment extends Fragment { |
| 93 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 94 | private View mEmptyView; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 95 | private ListView mListView; |
| 96 | private GridView mGridView; |
| 97 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 98 | private AbsListView mCurrentView; |
| 99 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 100 | public static final int TYPE_NORMAL = 1; |
| 101 | public static final int TYPE_SEARCH = 2; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 102 | public static final int TYPE_RECENT_OPEN = 3; |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 103 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 104 | public static final int ANIM_NONE = 1; |
| 105 | public static final int ANIM_SIDE = 2; |
| 106 | public static final int ANIM_DOWN = 3; |
| 107 | public static final int ANIM_UP = 4; |
| 108 | |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 109 | private int mType = TYPE_NORMAL; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 110 | private String mStateKey; |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 111 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 112 | private int mLastMode = MODE_UNKNOWN; |
| 113 | private int mLastSortOrder = SORT_ORDER_UNKNOWN; |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 114 | private boolean mLastShowSize = false; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 115 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 116 | private boolean mHideGridTitles = false; |
| 117 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 118 | private boolean mSvelteRecents; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 119 | private Point mThumbSize; |
| 120 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 121 | private DocumentsAdapter mAdapter; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 122 | private LoaderCallbacks<DirectoryResult> mCallbacks; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 123 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 124 | private static final String EXTRA_TYPE = "type"; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 125 | private static final String EXTRA_ROOT = "root"; |
| 126 | private static final String EXTRA_DOC = "doc"; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 127 | private static final String EXTRA_QUERY = "query"; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 128 | private static final String EXTRA_IGNORE_STATE = "ignoreState"; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 129 | |
Jeff Sharkey | c0075dc | 2013-10-25 17:12:49 -0700 | [diff] [blame] | 130 | private final int mLoaderId = 42; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 131 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 132 | public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) { |
| 133 | show(fm, TYPE_NORMAL, root, doc, null, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 136 | public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) { |
| 137 | show(fm, TYPE_SEARCH, root, null, query, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 140 | public static void showRecentsOpen(FragmentManager fm, int anim) { |
| 141 | show(fm, TYPE_RECENT_OPEN, null, null, null, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 144 | private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc, |
| 145 | String query, int anim) { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 146 | final Bundle args = new Bundle(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 147 | args.putInt(EXTRA_TYPE, type); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 148 | args.putParcelable(EXTRA_ROOT, root); |
| 149 | args.putParcelable(EXTRA_DOC, doc); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 150 | args.putString(EXTRA_QUERY, query); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 151 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 152 | final FragmentTransaction ft = fm.beginTransaction(); |
| 153 | switch (anim) { |
| 154 | case ANIM_SIDE: |
| 155 | args.putBoolean(EXTRA_IGNORE_STATE, true); |
| 156 | break; |
| 157 | case ANIM_DOWN: |
| 158 | args.putBoolean(EXTRA_IGNORE_STATE, true); |
| 159 | ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen); |
| 160 | break; |
| 161 | case ANIM_UP: |
| 162 | ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up); |
| 163 | break; |
| 164 | } |
| 165 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 166 | final DirectoryFragment fragment = new DirectoryFragment(); |
| 167 | fragment.setArguments(args); |
| 168 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 169 | ft.replace(R.id.container_directory, fragment); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 170 | ft.commitAllowingStateLoss(); |
| 171 | } |
| 172 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 173 | private static String buildStateKey(RootInfo root, DocumentInfo doc) { |
| 174 | final StringBuilder builder = new StringBuilder(); |
| 175 | builder.append(root != null ? root.authority : "null").append(';'); |
| 176 | builder.append(root != null ? root.rootId : "null").append(';'); |
| 177 | builder.append(doc != null ? doc.documentId : "null"); |
| 178 | return builder.toString(); |
| 179 | } |
| 180 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 181 | public static DirectoryFragment get(FragmentManager fm) { |
| 182 | // TODO: deal with multiple directories shown at once |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 183 | return (DirectoryFragment) fm.findFragmentById(R.id.container_directory); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 186 | @Override |
| 187 | public View onCreateView( |
| 188 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 189 | final Context context = inflater.getContext(); |
Jeff Sharkey | 083d7e1 | 2014-07-27 21:01:45 -0700 | [diff] [blame] | 190 | final Resources res = context.getResources(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 191 | final View view = inflater.inflate(R.layout.fragment_directory, container, false); |
| 192 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 193 | mEmptyView = view.findViewById(android.R.id.empty); |
| 194 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 195 | mListView = (ListView) view.findViewById(R.id.list); |
| 196 | mListView.setOnItemClickListener(mItemListener); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 197 | mListView.setMultiChoiceModeListener(mMultiListener); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 198 | mListView.setRecyclerListener(mRecycleListener); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 199 | |
Jeff Sharkey | 083d7e1 | 2014-07-27 21:01:45 -0700 | [diff] [blame] | 200 | // Indent our list divider to align with text |
| 201 | final Drawable divider = mListView.getDivider(); |
| 202 | final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left); |
| 203 | final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset); |
| 204 | if (insetLeft) { |
| 205 | mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0)); |
| 206 | } else { |
| 207 | mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0)); |
| 208 | } |
| 209 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 210 | mGridView = (GridView) view.findViewById(R.id.grid); |
| 211 | mGridView.setOnItemClickListener(mItemListener); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 212 | mGridView.setMultiChoiceModeListener(mMultiListener); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 213 | mGridView.setRecyclerListener(mRecycleListener); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 214 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 215 | return view; |
| 216 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 217 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 218 | @Override |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 219 | public void onDestroyView() { |
| 220 | super.onDestroyView(); |
| 221 | |
| 222 | // Cancel any outstanding thumbnail requests |
| 223 | final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView; |
| 224 | final int count = target.getChildCount(); |
| 225 | for (int i = 0; i < count; i++) { |
| 226 | final View view = target.getChildAt(i); |
| 227 | mRecycleListener.onMovedToScrapHeap(view); |
| 228 | } |
Jeff Sharkey | faaeb39 | 2013-10-04 14:44:56 -0700 | [diff] [blame] | 229 | |
| 230 | // Tear down any selection in progress |
| 231 | mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE); |
| 232 | mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 236 | public void onActivityCreated(Bundle savedInstanceState) { |
| 237 | super.onActivityCreated(savedInstanceState); |
| 238 | |
| 239 | final Context context = getActivity(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 240 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 241 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 242 | final RootInfo root = getArguments().getParcelable(EXTRA_ROOT); |
| 243 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 244 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 245 | mAdapter = new DocumentsAdapter(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 246 | mType = getArguments().getInt(EXTRA_TYPE); |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 247 | mStateKey = buildStateKey(root, doc); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 248 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 249 | if (mType == TYPE_RECENT_OPEN) { |
| 250 | // Hide titles when showing recents for picking images/videos |
| 251 | mHideGridTitles = MimePredicate.mimeMatches( |
| 252 | MimePredicate.VISUAL_MIMES, state.acceptMimes); |
| 253 | } else { |
| 254 | mHideGridTitles = (doc != null) && doc.isGridTitlesHidden(); |
| 255 | } |
| 256 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 257 | final ActivityManager am = (ActivityManager) context.getSystemService( |
| 258 | Context.ACTIVITY_SERVICE); |
| 259 | mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN); |
| 260 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 261 | mCallbacks = new LoaderCallbacks<DirectoryResult>() { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 262 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 263 | public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 264 | final String query = getArguments().getString(EXTRA_QUERY); |
Jeff Sharkey | 46165b5 | 2013-07-31 20:53:22 -0700 | [diff] [blame] | 265 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 266 | Uri contentsUri; |
| 267 | switch (mType) { |
| 268 | case TYPE_NORMAL: |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 269 | contentsUri = DocumentsContract.buildChildDocumentsUri( |
| 270 | doc.authority, doc.documentId); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 271 | if (state.action == ACTION_MANAGE) { |
| 272 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 273 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 274 | return new DirectoryLoader( |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 275 | context, mType, root, doc, contentsUri, state.userSortOrder); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 276 | case TYPE_SEARCH: |
| 277 | contentsUri = DocumentsContract.buildSearchDocumentsUri( |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 278 | root.authority, root.rootId, query); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 279 | if (state.action == ACTION_MANAGE) { |
| 280 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 281 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 282 | return new DirectoryLoader( |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 283 | context, mType, root, doc, contentsUri, state.userSortOrder); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 284 | case TYPE_RECENT_OPEN: |
Jeff Sharkey | 1c903cc | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 285 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 286 | return new RecentLoader(context, roots, state); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 287 | default: |
| 288 | throw new IllegalStateException("Unknown type " + mType); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 289 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 293 | public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 294 | if (!isAdded()) return; |
| 295 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 296 | mAdapter.swapResult(result); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 297 | |
| 298 | // Push latest state up to UI |
| 299 | // TODO: if mode change was racing with us, don't overwrite it |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 300 | if (result.mode != MODE_UNKNOWN) { |
| 301 | state.derivedMode = result.mode; |
| 302 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 303 | state.derivedSortOrder = result.sortOrder; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame^] | 304 | ((BaseActivity) context).onStateChanged(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 305 | |
| 306 | updateDisplayState(); |
| 307 | |
Jeff Sharkey | 25f10b3 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 308 | // When launched into empty recents, show drawer |
| 309 | if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched) { |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame^] | 310 | ((BaseActivity) context).setRootsDrawerOpen(true); |
Jeff Sharkey | 25f10b3 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 313 | // Restore any previous instance state |
| 314 | final SparseArray<Parcelable> container = state.dirState.remove(mStateKey); |
| 315 | if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) { |
| 316 | getView().restoreHierarchyState(container); |
| 317 | } else if (mLastSortOrder != state.derivedSortOrder) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 318 | mListView.smoothScrollToPosition(0); |
| 319 | mGridView.smoothScrollToPosition(0); |
| 320 | } |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 321 | |
| 322 | mLastSortOrder = state.derivedSortOrder; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 326 | public void onLoaderReset(Loader<DirectoryResult> loader) { |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 327 | mAdapter.swapResult(null); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 328 | } |
| 329 | }; |
| 330 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 331 | // Kick off loader at least once |
| 332 | getLoaderManager().restartLoader(mLoaderId, null, mCallbacks); |
| 333 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 334 | updateDisplayState(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 337 | @Override |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 338 | public void onStop() { |
| 339 | super.onStop(); |
| 340 | |
| 341 | // Remember last scroll location |
| 342 | final SparseArray<Parcelable> container = new SparseArray<Parcelable>(); |
| 343 | getView().saveHierarchyState(container); |
| 344 | final State state = getDisplayState(this); |
| 345 | state.dirState.put(mStateKey, container); |
| 346 | } |
| 347 | |
| 348 | @Override |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 349 | public void onResume() { |
| 350 | super.onResume(); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 351 | updateDisplayState(); |
| 352 | } |
| 353 | |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 354 | public void onDisplayStateChanged() { |
| 355 | updateDisplayState(); |
| 356 | } |
| 357 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 358 | public void onUserSortOrderChanged() { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 359 | // Sort order change always triggers reload; we'll trigger state change |
| 360 | // on the flip side. |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 361 | getLoaderManager().restartLoader(mLoaderId, null, mCallbacks); |
| 362 | } |
| 363 | |
| 364 | public void onUserModeChanged() { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 365 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 366 | final State state = getDisplayState(this); |
| 367 | |
| 368 | final RootInfo root = getArguments().getParcelable(EXTRA_ROOT); |
| 369 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 370 | |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 371 | if (root != null && doc != null) { |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 372 | final Uri stateUri = RecentsProvider.buildState( |
| 373 | root.authority, root.rootId, doc.documentId); |
| 374 | final ContentValues values = new ContentValues(); |
| 375 | values.put(StateColumns.MODE, state.userMode); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 376 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 377 | new AsyncTask<Void, Void, Void>() { |
| 378 | @Override |
| 379 | protected Void doInBackground(Void... params) { |
| 380 | resolver.insert(stateUri, values); |
| 381 | return null; |
| 382 | } |
| 383 | }.execute(); |
| 384 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 385 | |
| 386 | // Mode change is just visual change; no need to kick loader, and |
| 387 | // deliver change event immediately. |
| 388 | state.derivedMode = state.userMode; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame^] | 389 | ((BaseActivity) getActivity()).onStateChanged(); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 390 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 391 | updateDisplayState(); |
| 392 | } |
| 393 | |
| 394 | private void updateDisplayState() { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 395 | final State state = getDisplayState(this); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 396 | |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 397 | if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 398 | mLastMode = state.derivedMode; |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 399 | mLastShowSize = state.showSize; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 400 | |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 401 | mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE); |
| 402 | mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 403 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 404 | final int choiceMode; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 405 | if (state.allowMultiple) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 406 | choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL; |
| 407 | } else { |
| 408 | choiceMode = ListView.CHOICE_MODE_NONE; |
| 409 | } |
| 410 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 411 | final int thumbSize; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 412 | if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 413 | thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 414 | mListView.setAdapter(null); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 415 | mListView.setChoiceMode(ListView.CHOICE_MODE_NONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 416 | mGridView.setAdapter(mAdapter); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 417 | mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width)); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 418 | mGridView.setNumColumns(GridView.AUTO_FIT); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 419 | mGridView.setChoiceMode(choiceMode); |
| 420 | mCurrentView = mGridView; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 421 | } else if (state.derivedMode == MODE_LIST) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 422 | thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 423 | mGridView.setAdapter(null); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 424 | mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 425 | mListView.setAdapter(mAdapter); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 426 | mListView.setChoiceMode(choiceMode); |
| 427 | mCurrentView = mListView; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 428 | } else { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 429 | throw new IllegalStateException("Unknown state " + state.derivedMode); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 430 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 431 | |
| 432 | mThumbSize = new Point(thumbSize, thumbSize); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | private OnItemClickListener mItemListener = new OnItemClickListener() { |
| 436 | @Override |
| 437 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 438 | final Cursor cursor = mAdapter.getItem(position); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 439 | if (cursor != null) { |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 440 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 441 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 442 | if (isDocumentEnabled(docMimeType, docFlags)) { |
| 443 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame^] | 444 | ((BaseActivity) getActivity()).onDocumentPicked(doc); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 445 | } |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 446 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 447 | } |
| 448 | }; |
| 449 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 450 | private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() { |
| 451 | @Override |
| 452 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 453 | mode.getMenuInflater().inflate(R.menu.mode_directory, menu); |
Stefan Wysocki | 43c97ea | 2014-03-04 11:27:55 +0100 | [diff] [blame] | 454 | mode.setTitle(getResources() |
| 455 | .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount())); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 456 | return true; |
| 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 461 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 462 | |
| 463 | final MenuItem open = menu.findItem(R.id.menu_open); |
| 464 | final MenuItem share = menu.findItem(R.id.menu_share); |
| 465 | final MenuItem delete = menu.findItem(R.id.menu_delete); |
| 466 | |
| 467 | final boolean manageMode = state.action == ACTION_MANAGE; |
| 468 | open.setVisible(!manageMode); |
| 469 | share.setVisible(manageMode); |
| 470 | delete.setVisible(manageMode); |
| 471 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 472 | return true; |
| 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 477 | final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions(); |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 478 | final ArrayList<DocumentInfo> docs = Lists.newArrayList(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 479 | final int size = checked.size(); |
| 480 | for (int i = 0; i < size; i++) { |
| 481 | if (checked.valueAt(i)) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 482 | final Cursor cursor = mAdapter.getItem(checked.keyAt(i)); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 483 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 484 | docs.add(doc); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 485 | } |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 486 | } |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 487 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 488 | final int id = item.getItemId(); |
| 489 | if (id == R.id.menu_open) { |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame^] | 490 | BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 491 | mode.finish(); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 492 | return true; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 493 | |
| 494 | } else if (id == R.id.menu_share) { |
| 495 | onShareDocuments(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 496 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 497 | return true; |
| 498 | |
| 499 | } else if (id == R.id.menu_delete) { |
| 500 | onDeleteDocuments(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 501 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 502 | return true; |
| 503 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 504 | } else { |
| 505 | return false; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | @Override |
| 510 | public void onDestroyActionMode(ActionMode mode) { |
| 511 | // ignored |
| 512 | } |
| 513 | |
| 514 | @Override |
| 515 | public void onItemCheckedStateChanged( |
| 516 | ActionMode mode, int position, long id, boolean checked) { |
| 517 | if (checked) { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 518 | // Directories and footer items cannot be checked |
| 519 | boolean valid = false; |
| 520 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 521 | final Cursor cursor = mAdapter.getItem(position); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 522 | if (cursor != null) { |
| 523 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 524 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 525 | if (!Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 526 | valid = isDocumentEnabled(docMimeType, docFlags); |
| 527 | } |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | if (!valid) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 531 | mCurrentView.setItemChecked(position, false); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | mode.setTitle(getResources() |
| 536 | .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount())); |
| 537 | } |
| 538 | }; |
| 539 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 540 | private RecyclerListener mRecycleListener = new RecyclerListener() { |
| 541 | @Override |
| 542 | public void onMovedToScrapHeap(View view) { |
| 543 | final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb); |
| 544 | if (iconThumb != null) { |
| 545 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag(); |
| 546 | if (oldTask != null) { |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 547 | oldTask.preempt(); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 548 | iconThumb.setTag(null); |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | }; |
| 553 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 554 | private void onShareDocuments(List<DocumentInfo> docs) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 555 | Intent intent; |
| 556 | if (docs.size() == 1) { |
| 557 | final DocumentInfo doc = docs.get(0); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 558 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 559 | intent = new Intent(Intent.ACTION_SEND); |
| 560 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 561 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 562 | intent.setType(doc.mimeType); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 563 | intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 564 | |
| 565 | } else if (docs.size() > 1) { |
| 566 | intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 567 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 568 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 569 | |
| 570 | final ArrayList<String> mimeTypes = Lists.newArrayList(); |
| 571 | final ArrayList<Uri> uris = Lists.newArrayList(); |
| 572 | for (DocumentInfo doc : docs) { |
| 573 | mimeTypes.add(doc.mimeType); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 574 | uris.add(doc.derivedUri); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | intent.setType(findCommonMimeType(mimeTypes)); |
| 578 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 579 | |
| 580 | } else { |
| 581 | return; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 584 | intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via)); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 585 | startActivity(intent); |
| 586 | } |
| 587 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 588 | private void onDeleteDocuments(List<DocumentInfo> docs) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 589 | final Context context = getActivity(); |
| 590 | final ContentResolver resolver = context.getContentResolver(); |
| 591 | |
| 592 | boolean hadTrouble = false; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 593 | for (DocumentInfo doc : docs) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 594 | if (!doc.isDeleteSupported()) { |
| 595 | Log.w(TAG, "Skipping " + doc); |
| 596 | hadTrouble = true; |
| 597 | continue; |
| 598 | } |
| 599 | |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 600 | ContentProviderClient client = null; |
| 601 | try { |
| 602 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 603 | resolver, doc.derivedUri.getAuthority()); |
| 604 | DocumentsContract.deleteDocument(client, doc.derivedUri); |
| 605 | } catch (Exception e) { |
Jeff Sharkey | 4c5fe52 | 2013-09-03 14:17:06 -0700 | [diff] [blame] | 606 | Log.w(TAG, "Failed to delete " + doc); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 607 | hadTrouble = true; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 608 | } finally { |
| 609 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
| 613 | if (hadTrouble) { |
| 614 | Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show(); |
| 615 | } |
| 616 | } |
| 617 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 618 | private static State getDisplayState(Fragment fragment) { |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame^] | 619 | return ((BaseActivity) fragment.getActivity()).getDisplayState(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 622 | private static abstract class Footer { |
| 623 | private final int mItemViewType; |
| 624 | |
| 625 | public Footer(int itemViewType) { |
| 626 | mItemViewType = itemViewType; |
| 627 | } |
| 628 | |
| 629 | public abstract View getView(View convertView, ViewGroup parent); |
| 630 | |
| 631 | public int getItemViewType() { |
| 632 | return mItemViewType; |
| 633 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 636 | private class LoadingFooter extends Footer { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 637 | public LoadingFooter() { |
| 638 | super(1); |
| 639 | } |
| 640 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 641 | @Override |
| 642 | public View getView(View convertView, ViewGroup parent) { |
| 643 | final Context context = parent.getContext(); |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 644 | final State state = getDisplayState(DirectoryFragment.this); |
| 645 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 646 | if (convertView == null) { |
| 647 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 648 | if (state.derivedMode == MODE_LIST) { |
| 649 | convertView = inflater.inflate(R.layout.item_loading_list, parent, false); |
| 650 | } else if (state.derivedMode == MODE_GRID) { |
| 651 | convertView = inflater.inflate(R.layout.item_loading_grid, parent, false); |
| 652 | } else { |
| 653 | throw new IllegalStateException(); |
| 654 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 655 | } |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 656 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 657 | return convertView; |
| 658 | } |
| 659 | } |
| 660 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 661 | private class MessageFooter extends Footer { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 662 | private final int mIcon; |
| 663 | private final String mMessage; |
| 664 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 665 | public MessageFooter(int itemViewType, int icon, String message) { |
| 666 | super(itemViewType); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 667 | mIcon = icon; |
| 668 | mMessage = message; |
| 669 | } |
| 670 | |
| 671 | @Override |
| 672 | public View getView(View convertView, ViewGroup parent) { |
| 673 | final Context context = parent.getContext(); |
| 674 | final State state = getDisplayState(DirectoryFragment.this); |
| 675 | |
| 676 | if (convertView == null) { |
| 677 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 678 | if (state.derivedMode == MODE_LIST) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 679 | convertView = inflater.inflate(R.layout.item_message_list, parent, false); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 680 | } else if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 681 | convertView = inflater.inflate(R.layout.item_message_grid, parent, false); |
| 682 | } else { |
| 683 | throw new IllegalStateException(); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
| 688 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 689 | icon.setImageResource(mIcon); |
| 690 | title.setText(mMessage); |
| 691 | return convertView; |
| 692 | } |
| 693 | } |
| 694 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 695 | private class DocumentsAdapter extends BaseAdapter { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 696 | private Cursor mCursor; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 697 | private int mCursorCount; |
| 698 | |
| 699 | private List<Footer> mFooters = Lists.newArrayList(); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 700 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 701 | public void swapResult(DirectoryResult result) { |
| 702 | mCursor = result != null ? result.cursor : null; |
| 703 | mCursorCount = mCursor != null ? mCursor.getCount() : 0; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 704 | |
| 705 | mFooters.clear(); |
| 706 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 707 | final Bundle extras = mCursor != null ? mCursor.getExtras() : null; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 708 | if (extras != null) { |
| 709 | final String info = extras.getString(DocumentsContract.EXTRA_INFO); |
| 710 | if (info != null) { |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 711 | mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info)); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 712 | } |
| 713 | final String error = extras.getString(DocumentsContract.EXTRA_ERROR); |
| 714 | if (error != null) { |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 715 | mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error)); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 716 | } |
| 717 | if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) { |
| 718 | mFooters.add(new LoadingFooter()); |
| 719 | } |
| 720 | } |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 721 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 722 | if (result != null && result.exception != null) { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 723 | mFooters.add(new MessageFooter( |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 724 | 3, R.drawable.ic_dialog_alert, getString(R.string.query_error))); |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 727 | if (isEmpty()) { |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 728 | mEmptyView.setVisibility(View.VISIBLE); |
| 729 | } else { |
| 730 | mEmptyView.setVisibility(View.GONE); |
| 731 | } |
| 732 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 733 | notifyDataSetChanged(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | @Override |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 737 | public View getView(int position, View convertView, ViewGroup parent) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 738 | if (position < mCursorCount) { |
| 739 | return getDocumentView(position, convertView, parent); |
| 740 | } else { |
| 741 | position -= mCursorCount; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 742 | convertView = mFooters.get(position).getView(convertView, parent); |
| 743 | // Only the view itself is disabled; contents inside shouldn't |
| 744 | // be dimmed. |
| 745 | convertView.setEnabled(false); |
| 746 | return convertView; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
| 750 | private View getDocumentView(int position, View convertView, ViewGroup parent) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 751 | final Context context = parent.getContext(); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 752 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 753 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 754 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 755 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 756 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
| 757 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 758 | context, mThumbSize); |
| 759 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 760 | if (convertView == null) { |
| 761 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 762 | if (state.derivedMode == MODE_LIST) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 763 | convertView = inflater.inflate(R.layout.item_doc_list, parent, false); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 764 | } else if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 765 | convertView = inflater.inflate(R.layout.item_doc_grid, parent, false); |
| 766 | } else { |
| 767 | throw new IllegalStateException(); |
| 768 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 769 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 770 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 771 | final Cursor cursor = getItem(position); |
| 772 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 773 | final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY); |
| 774 | final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 775 | final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID); |
| 776 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 777 | final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME); |
| 778 | final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED); |
| 779 | final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON); |
| 780 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 781 | final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY); |
| 782 | final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 783 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 784 | final View line1 = convertView.findViewById(R.id.line1); |
| 785 | final View line2 = convertView.findViewById(R.id.line2); |
| 786 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 787 | final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime); |
| 788 | final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 789 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 790 | final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 791 | final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 792 | final TextView summary = (TextView) convertView.findViewById(android.R.id.summary); |
| 793 | final TextView date = (TextView) convertView.findViewById(R.id.date); |
| 794 | final TextView size = (TextView) convertView.findViewById(R.id.size); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 795 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 796 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 797 | if (oldTask != null) { |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 798 | oldTask.preempt(); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 799 | iconThumb.setTag(null); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 802 | iconMime.animate().cancel(); |
| 803 | iconThumb.animate().cancel(); |
| 804 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 805 | final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 806 | final boolean allowThumbnail = (state.derivedMode == MODE_GRID) |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 807 | || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 808 | final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 809 | |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 810 | final boolean enabled = isDocumentEnabled(docMimeType, docFlags); |
| 811 | final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f; |
| 812 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 813 | boolean cacheHit = false; |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 814 | if (showThumbnail) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 815 | final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 816 | final Bitmap cachedResult = thumbs.get(uri); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 817 | if (cachedResult != null) { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 818 | iconThumb.setImageBitmap(cachedResult); |
| 819 | cacheHit = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 820 | } else { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 821 | iconThumb.setImageDrawable(null); |
| 822 | final ThumbnailAsyncTask task = new ThumbnailAsyncTask( |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 823 | uri, iconMime, iconThumb, mThumbSize, iconAlpha); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 824 | iconThumb.setTag(task); |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 825 | ProviderExecutor.forAuthority(docAuthority).execute(task); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 826 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | // Always throw MIME icon into place, even when a thumbnail is being |
| 830 | // loaded in background. |
| 831 | if (cacheHit) { |
| 832 | iconMime.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 833 | iconMime.setImageDrawable(null); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 834 | iconThumb.setAlpha(1f); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 835 | } else { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 836 | iconMime.setAlpha(1f); |
| 837 | iconThumb.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 838 | iconThumb.setImageDrawable(null); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 839 | if (docIcon != 0) { |
| 840 | iconMime.setImageDrawable( |
| 841 | IconUtils.loadPackageIcon(context, docAuthority, docIcon)); |
| 842 | } else { |
Jeff Sharkey | 2ceff51 | 2013-09-18 18:03:49 -0700 | [diff] [blame] | 843 | iconMime.setImageDrawable(IconUtils.loadMimeIcon( |
| 844 | context, docMimeType, docAuthority, docId, state.derivedMode)); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 845 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 848 | boolean hasLine1 = false; |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 849 | boolean hasLine2 = false; |
| 850 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 851 | final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles; |
| 852 | if (!hideTitle) { |
| 853 | title.setText(docDisplayName); |
| 854 | hasLine1 = true; |
| 855 | } |
| 856 | |
| 857 | Drawable iconDrawable = null; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 858 | if (mType == TYPE_RECENT_OPEN) { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 859 | // We've already had to enumerate roots before any results can |
| 860 | // be shown, so this will never block. |
| 861 | final RootInfo root = roots.getRootBlocking(docAuthority, docRootId); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 862 | if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 863 | iconDrawable = root.loadGridIcon(context); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 864 | } else { |
| 865 | iconDrawable = root.loadIcon(context); |
| 866 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 867 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 868 | if (summary != null) { |
| 869 | final boolean alwaysShowSummary = getResources() |
| 870 | .getBoolean(R.bool.always_show_summary); |
| 871 | if (alwaysShowSummary) { |
| 872 | summary.setText(root.getDirectoryString()); |
| 873 | summary.setVisibility(View.VISIBLE); |
| 874 | hasLine2 = true; |
| 875 | } else { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 876 | if (iconDrawable != null && roots.isIconUniqueBlocking(root)) { |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 877 | // No summary needed if icon speaks for itself |
| 878 | summary.setVisibility(View.INVISIBLE); |
| 879 | } else { |
| 880 | summary.setText(root.getDirectoryString()); |
| 881 | summary.setVisibility(View.VISIBLE); |
| 882 | summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END); |
| 883 | hasLine2 = true; |
| 884 | } |
| 885 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 886 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 887 | } else { |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 888 | // Directories showing thumbnails in grid mode get a little icon |
| 889 | // hint to remind user they're a directory. |
| 890 | if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID |
| 891 | && showThumbnail) { |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 892 | iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder, |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 893 | android.R.attr.textColorPrimaryInverse); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 896 | if (summary != null) { |
| 897 | if (docSummary != null) { |
| 898 | summary.setText(docSummary); |
| 899 | summary.setVisibility(View.VISIBLE); |
| 900 | hasLine2 = true; |
| 901 | } else { |
| 902 | summary.setVisibility(View.INVISIBLE); |
| 903 | } |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 904 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 907 | if (icon1 != null) icon1.setVisibility(View.GONE); |
| 908 | if (icon2 != null) icon2.setVisibility(View.GONE); |
| 909 | |
| 910 | if (iconDrawable != null) { |
| 911 | if (hasLine1) { |
| 912 | icon1.setVisibility(View.VISIBLE); |
| 913 | icon1.setImageDrawable(iconDrawable); |
| 914 | } else { |
| 915 | icon2.setVisibility(View.VISIBLE); |
| 916 | icon2.setImageDrawable(iconDrawable); |
| 917 | } |
| 918 | } |
| 919 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 920 | if (docLastModified == -1) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 921 | date.setText(null); |
| 922 | } else { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 923 | date.setText(formatTime(context, docLastModified)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 924 | hasLine2 = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 925 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 926 | |
| 927 | if (state.showSize) { |
| 928 | size.setVisibility(View.VISIBLE); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 929 | if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) { |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 930 | size.setText(null); |
| 931 | } else { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 932 | size.setText(Formatter.formatFileSize(context, docSize)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 933 | hasLine2 = true; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 934 | } |
| 935 | } else { |
| 936 | size.setVisibility(View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 937 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 938 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 939 | if (line1 != null) { |
| 940 | line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE); |
| 941 | } |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 942 | if (line2 != null) { |
| 943 | line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE); |
| 944 | } |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 945 | |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 946 | setEnabledRecursive(convertView, enabled); |
| 947 | |
| 948 | iconMime.setAlpha(iconAlpha); |
| 949 | iconThumb.setAlpha(iconAlpha); |
| 950 | if (icon1 != null) icon1.setAlpha(iconAlpha); |
| 951 | if (icon2 != null) icon2.setAlpha(iconAlpha); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 952 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 953 | return convertView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 954 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 955 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 956 | @Override |
| 957 | public int getCount() { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 958 | return mCursorCount + mFooters.size(); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 959 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 960 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 961 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 962 | public Cursor getItem(int position) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 963 | if (position < mCursorCount) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 964 | mCursor.moveToPosition(position); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 965 | return mCursor; |
| 966 | } else { |
| 967 | return null; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 968 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 969 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 970 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 971 | @Override |
| 972 | public long getItemId(int position) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 973 | return position; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 974 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 975 | |
| 976 | @Override |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 977 | public int getViewTypeCount() { |
| 978 | return 4; |
| 979 | } |
| 980 | |
| 981 | @Override |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 982 | public int getItemViewType(int position) { |
| 983 | if (position < mCursorCount) { |
| 984 | return 0; |
| 985 | } else { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 986 | position -= mCursorCount; |
| 987 | return mFooters.get(position).getItemViewType(); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 988 | } |
| 989 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 990 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 991 | |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 992 | private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> |
| 993 | implements Preemptable { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 994 | private final Uri mUri; |
| 995 | private final ImageView mIconMime; |
| 996 | private final ImageView mIconThumb; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 997 | private final Point mThumbSize; |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 998 | private final float mTargetAlpha; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 999 | private final CancellationSignal mSignal; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1000 | |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1001 | public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize, |
| 1002 | float targetAlpha) { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1003 | mUri = uri; |
| 1004 | mIconMime = iconMime; |
| 1005 | mIconThumb = iconThumb; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 1006 | mThumbSize = thumbSize; |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1007 | mTargetAlpha = targetAlpha; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1008 | mSignal = new CancellationSignal(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 1011 | @Override |
| 1012 | public void preempt() { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1013 | cancel(false); |
| 1014 | mSignal.cancel(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | @Override |
| 1018 | protected Bitmap doInBackground(Uri... params) { |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 1019 | if (isCancelled()) return null; |
| 1020 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1021 | final Context context = mIconThumb.getContext(); |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1022 | final ContentResolver resolver = context.getContentResolver(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1023 | |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1024 | ContentProviderClient client = null; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1025 | Bitmap result = null; |
| 1026 | try { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1027 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 1028 | resolver, mUri.getAuthority()); |
| 1029 | result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1030 | if (result != null) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 1031 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 1032 | context, mThumbSize); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1033 | thumbs.put(mUri, result); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1034 | } |
| 1035 | } catch (Exception e) { |
Jeff Sharkey | e39a89b | 2013-10-29 11:56:37 -0700 | [diff] [blame] | 1036 | if (!(e instanceof OperationCanceledException)) { |
| 1037 | Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e); |
| 1038 | } |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1039 | } finally { |
| 1040 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1041 | } |
| 1042 | return result; |
| 1043 | } |
| 1044 | |
| 1045 | @Override |
| 1046 | protected void onPostExecute(Bitmap result) { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1047 | if (mIconThumb.getTag() == this && result != null) { |
| 1048 | mIconThumb.setTag(null); |
| 1049 | mIconThumb.setImageBitmap(result); |
| 1050 | |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1051 | mIconMime.setAlpha(mTargetAlpha); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1052 | mIconMime.animate().alpha(0f).start(); |
| 1053 | mIconThumb.setAlpha(0f); |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1054 | mIconThumb.animate().alpha(mTargetAlpha).start(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | private static String formatTime(Context context, long when) { |
| 1060 | // TODO: DateUtils should make this easier |
| 1061 | Time then = new Time(); |
| 1062 | then.set(when); |
| 1063 | Time now = new Time(); |
| 1064 | now.setToNow(); |
| 1065 | |
| 1066 | int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT |
| 1067 | | DateUtils.FORMAT_ABBREV_ALL; |
| 1068 | |
| 1069 | if (then.year != now.year) { |
| 1070 | flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE; |
| 1071 | } else if (then.yearDay != now.yearDay) { |
| 1072 | flags |= DateUtils.FORMAT_SHOW_DATE; |
| 1073 | } else { |
| 1074 | flags |= DateUtils.FORMAT_SHOW_TIME; |
| 1075 | } |
| 1076 | |
| 1077 | return DateUtils.formatDateTime(context, when, flags); |
| 1078 | } |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 1079 | |
| 1080 | private String findCommonMimeType(List<String> mimeTypes) { |
| 1081 | String[] commonType = mimeTypes.get(0).split("/"); |
| 1082 | if (commonType.length != 2) { |
| 1083 | return "*/*"; |
| 1084 | } |
| 1085 | |
| 1086 | for (int i = 1; i < mimeTypes.size(); i++) { |
| 1087 | String[] type = mimeTypes.get(i).split("/"); |
| 1088 | if (type.length != 2) continue; |
| 1089 | |
| 1090 | if (!commonType[1].equals(type[1])) { |
| 1091 | commonType[1] = "*"; |
| 1092 | } |
| 1093 | |
| 1094 | if (!commonType[0].equals(type[0])) { |
| 1095 | commonType[0] = "*"; |
| 1096 | commonType[1] = "*"; |
| 1097 | break; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | return commonType[0] + "/" + commonType[1]; |
| 1102 | } |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1103 | |
| 1104 | private void setEnabledRecursive(View v, boolean enabled) { |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1105 | if (v == null) return; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1106 | if (v.isEnabled() == enabled) return; |
| 1107 | v.setEnabled(enabled); |
| 1108 | |
| 1109 | if (v instanceof ViewGroup) { |
| 1110 | final ViewGroup vg = (ViewGroup) v; |
| 1111 | for (int i = vg.getChildCount() - 1; i >= 0; i--) { |
| 1112 | setEnabledRecursive(vg.getChildAt(i), enabled); |
| 1113 | } |
| 1114 | } |
| 1115 | } |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1116 | |
| 1117 | private boolean isDocumentEnabled(String docMimeType, int docFlags) { |
| 1118 | final State state = getDisplayState(DirectoryFragment.this); |
| 1119 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1120 | // Directories are always enabled |
| 1121 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 1122 | return true; |
| 1123 | } |
| 1124 | |
Jeff Sharkey | 783ebc2 | 2013-09-26 19:42:52 -0700 | [diff] [blame] | 1125 | // Read-only files are disabled when creating |
| 1126 | if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) { |
| 1127 | return false; |
| 1128 | } |
| 1129 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1130 | return MimePredicate.mimeMatches(state.acceptMimes, docMimeType); |
| 1131 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1132 | } |