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