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