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; |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 27 | import static com.android.documentsui.Shared.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; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 31 | import static com.android.internal.util.Preconditions.checkNotNull; |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 32 | import static com.android.internal.util.Preconditions.checkState; |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 33 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 34 | import android.app.Activity; |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 35 | import android.app.ActivityManager; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 36 | import android.app.Fragment; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 37 | import android.app.FragmentManager; |
| 38 | import android.app.FragmentTransaction; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 39 | import android.app.LoaderManager.LoaderCallbacks; |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 40 | import android.content.ClipData; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 41 | import android.content.ContentProviderClient; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 42 | import android.content.ContentResolver; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 43 | import android.content.ContentValues; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 44 | import android.content.Context; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 45 | import android.content.Intent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 46 | import android.content.Loader; |
Jeff Sharkey | 083d7e1 | 2014-07-27 21:01:45 -0700 | [diff] [blame] | 47 | import android.content.res.Resources; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 48 | import android.database.Cursor; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 49 | import android.graphics.Bitmap; |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 50 | import android.graphics.Canvas; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 51 | import android.graphics.Point; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 52 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 53 | import android.net.Uri; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 54 | import android.os.AsyncTask; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 55 | import android.os.Bundle; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 56 | import android.os.CancellationSignal; |
Makoto Onuki | 7777875 | 2015-07-01 14:55:14 -0700 | [diff] [blame] | 57 | import android.os.Handler; |
| 58 | import android.os.Looper; |
Jeff Sharkey | e39a89b | 2013-10-29 11:56:37 -0700 | [diff] [blame] | 59 | import android.os.OperationCanceledException; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 60 | import android.os.Parcelable; |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 61 | import android.os.SystemProperties; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 62 | import android.provider.DocumentsContract; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 63 | import android.provider.DocumentsContract.Document; |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 64 | import android.support.annotation.Nullable; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 65 | import android.support.annotation.VisibleForTesting; |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 66 | import android.support.design.widget.Snackbar; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 67 | import android.support.v7.widget.GridLayoutManager; |
| 68 | import android.support.v7.widget.LinearLayoutManager; |
| 69 | import android.support.v7.widget.RecyclerView; |
| 70 | import android.support.v7.widget.RecyclerView.LayoutManager; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 71 | import android.support.v7.widget.RecyclerView.RecyclerListener; |
| 72 | import android.support.v7.widget.RecyclerView.ViewHolder; |
Jeff Sharkey | 6d57927 | 2015-06-11 09:16:19 -0700 | [diff] [blame] | 73 | import android.text.TextUtils; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 74 | import android.text.format.DateUtils; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 75 | import android.text.format.Formatter; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 76 | import android.text.format.Time; |
| 77 | import android.util.Log; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 78 | import android.util.SparseArray; |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 79 | import android.util.SparseBooleanArray; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 80 | import android.view.ActionMode; |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 81 | import android.view.DragEvent; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 82 | import android.view.GestureDetector; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 83 | import android.view.LayoutInflater; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 84 | import android.view.Menu; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 85 | import android.view.MenuItem; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 86 | import android.view.MotionEvent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 87 | import android.view.View; |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 88 | import android.view.View.OnLayoutChangeListener; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 89 | import android.view.ViewGroup; |
Vladislav Kaznacheev | 9400b89 | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 90 | import android.view.ViewParent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 91 | import android.widget.ImageView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 92 | import android.widget.TextView; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 93 | import android.widget.Toast; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 94 | |
Steve McKay | 351a749 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 95 | import com.android.documentsui.BaseActivity.DocumentContext; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 96 | import com.android.documentsui.BaseActivity.State; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 97 | import com.android.documentsui.MultiSelectManager.Selection; |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 98 | import com.android.documentsui.ProviderExecutor.Preemptable; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 99 | import com.android.documentsui.RecentsProvider.StateColumns; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 100 | import com.android.documentsui.model.DocumentInfo; |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 101 | import com.android.documentsui.model.DocumentStack; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 102 | import com.android.documentsui.model.RootInfo; |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 103 | import com.android.internal.util.Preconditions; |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 104 | import com.google.common.collect.Lists; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 105 | |
| 106 | import java.util.ArrayList; |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 107 | import java.util.Collections; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 108 | import java.util.List; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Display the documents inside a single directory. |
| 112 | */ |
| 113 | public class DirectoryFragment extends Fragment { |
| 114 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 115 | public static final int TYPE_NORMAL = 1; |
| 116 | public static final int TYPE_SEARCH = 2; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 117 | public static final int TYPE_RECENT_OPEN = 3; |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 118 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 119 | public static final int ANIM_NONE = 1; |
| 120 | public static final int ANIM_SIDE = 2; |
| 121 | public static final int ANIM_DOWN = 3; |
| 122 | public static final int ANIM_UP = 4; |
| 123 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 124 | public static final int REQUEST_COPY_DESTINATION = 1; |
| 125 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 126 | private static final int LOADER_ID = 42; |
| 127 | private static final boolean DEBUG = false; |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 128 | private static final boolean DEBUG_ENABLE_DND = false; |
| 129 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 130 | private static final String EXTRA_TYPE = "type"; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 131 | private static final String EXTRA_ROOT = "root"; |
| 132 | private static final String EXTRA_DOC = "doc"; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 133 | private static final String EXTRA_QUERY = "query"; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 134 | private static final String EXTRA_IGNORE_STATE = "ignoreState"; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 135 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 136 | private Model mModel; |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 137 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 138 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 139 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 140 | private View mEmptyView; |
| 141 | private RecyclerView mRecView; |
| 142 | |
| 143 | private int mType = TYPE_NORMAL; |
| 144 | private String mStateKey; |
| 145 | |
| 146 | private int mLastMode = MODE_UNKNOWN; |
| 147 | private int mLastSortOrder = SORT_ORDER_UNKNOWN; |
| 148 | private boolean mLastShowSize; |
| 149 | private boolean mHideGridTitles; |
| 150 | private boolean mSvelteRecents; |
| 151 | private Point mThumbSize; |
| 152 | private DocumentsAdapter mAdapter; |
| 153 | private LoaderCallbacks<DirectoryResult> mCallbacks; |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 154 | private FragmentTuner mFragmentTuner; |
| 155 | private DocumentClipper mClipper; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 156 | // These are lazily initialized. |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 157 | private LinearLayoutManager mListLayout; |
| 158 | private GridLayoutManager mGridLayout; |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 159 | private int mColumnCount = 1; // This will get updated when layout changes. |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 160 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 161 | public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) { |
| 162 | show(fm, TYPE_NORMAL, root, doc, null, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 165 | public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) { |
| 166 | show(fm, TYPE_SEARCH, root, null, query, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 169 | public static void showRecentsOpen(FragmentManager fm, int anim) { |
| 170 | show(fm, TYPE_RECENT_OPEN, null, null, null, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 173 | private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc, |
| 174 | String query, int anim) { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 175 | final Bundle args = new Bundle(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 176 | args.putInt(EXTRA_TYPE, type); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 177 | args.putParcelable(EXTRA_ROOT, root); |
| 178 | args.putParcelable(EXTRA_DOC, doc); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 179 | args.putString(EXTRA_QUERY, query); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 180 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 181 | final FragmentTransaction ft = fm.beginTransaction(); |
| 182 | switch (anim) { |
| 183 | case ANIM_SIDE: |
| 184 | args.putBoolean(EXTRA_IGNORE_STATE, true); |
| 185 | break; |
| 186 | case ANIM_DOWN: |
| 187 | args.putBoolean(EXTRA_IGNORE_STATE, true); |
| 188 | ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen); |
| 189 | break; |
| 190 | case ANIM_UP: |
| 191 | ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up); |
| 192 | break; |
| 193 | } |
| 194 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 195 | final DirectoryFragment fragment = new DirectoryFragment(); |
| 196 | fragment.setArguments(args); |
| 197 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 198 | ft.replace(R.id.container_directory, fragment); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 199 | ft.commitAllowingStateLoss(); |
| 200 | } |
| 201 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 202 | private static String buildStateKey(RootInfo root, DocumentInfo doc) { |
| 203 | final StringBuilder builder = new StringBuilder(); |
| 204 | builder.append(root != null ? root.authority : "null").append(';'); |
| 205 | builder.append(root != null ? root.rootId : "null").append(';'); |
| 206 | builder.append(doc != null ? doc.documentId : "null"); |
| 207 | return builder.toString(); |
| 208 | } |
| 209 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 210 | public static DirectoryFragment get(FragmentManager fm) { |
| 211 | // TODO: deal with multiple directories shown at once |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 212 | return (DirectoryFragment) fm.findFragmentById(R.id.container_directory); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 215 | @Override |
| 216 | public View onCreateView( |
| 217 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 218 | final Context context = inflater.getContext(); |
Jeff Sharkey | 083d7e1 | 2014-07-27 21:01:45 -0700 | [diff] [blame] | 219 | final Resources res = context.getResources(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 220 | final View view = inflater.inflate(R.layout.fragment_directory, container, false); |
| 221 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 222 | mEmptyView = view.findViewById(android.R.id.empty); |
| 223 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 224 | mRecView = (RecyclerView) view.findViewById(R.id.recyclerView); |
| 225 | mRecView.setRecyclerListener( |
| 226 | new RecyclerListener() { |
| 227 | @Override |
| 228 | public void onViewRecycled(ViewHolder holder) { |
| 229 | cancelThumbnailTask(holder.itemView); |
| 230 | } |
| 231 | }); |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 232 | |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 233 | // TODO: Rather than update columns on layout changes, push this |
| 234 | // code (or something like it) into GridLayoutManager. |
| 235 | mRecView.addOnLayoutChangeListener( |
| 236 | new OnLayoutChangeListener() { |
| 237 | |
| 238 | @Override |
| 239 | public void onLayoutChange( |
| 240 | View v, int left, int top, int right, int bottom, int oldLeft, |
| 241 | int oldTop, int oldRight, int oldBottom) { |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 242 | mColumnCount = calculateColumnCount(); |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 243 | if (mGridLayout != null) { |
| 244 | mGridLayout.setSpanCount(mColumnCount); |
| 245 | } |
| 246 | } |
| 247 | }); |
| 248 | |
| 249 | // TODO: Add a divider between views (which might use RecyclerView.ItemDecoration). |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 250 | if (DEBUG_ENABLE_DND) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 251 | setupDragAndDropOnDirectoryView(mRecView); |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 252 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 253 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 254 | return view; |
| 255 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 256 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 257 | @Override |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 258 | public void onDestroyView() { |
| 259 | super.onDestroyView(); |
| 260 | |
| 261 | // Cancel any outstanding thumbnail requests |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 262 | final int count = mRecView.getChildCount(); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 263 | for (int i = 0; i < count; i++) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 264 | final View view = mRecView.getChildAt(i); |
| 265 | cancelThumbnailTask(view); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 266 | } |
Jeff Sharkey | faaeb39 | 2013-10-04 14:44:56 -0700 | [diff] [blame] | 267 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 268 | // Clear any outstanding selection |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 269 | mModel.clearSelection(); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 273 | public void onActivityCreated(Bundle savedInstanceState) { |
| 274 | super.onActivityCreated(savedInstanceState); |
| 275 | |
| 276 | final Context context = getActivity(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 277 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 278 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 279 | final RootInfo root = getArguments().getParcelable(EXTRA_ROOT); |
| 280 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 281 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 282 | mAdapter = new DocumentsAdapter(context); |
| 283 | mRecView.setAdapter(mAdapter); |
| 284 | |
| 285 | GestureDetector.SimpleOnGestureListener listener = |
| 286 | new GestureDetector.SimpleOnGestureListener() { |
| 287 | @Override |
| 288 | public boolean onSingleTapUp(MotionEvent e) { |
| 289 | return DirectoryFragment.this.onSingleTapUp(e); |
| 290 | } |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 291 | @Override |
| 292 | public boolean onDoubleTap(MotionEvent e) { |
| 293 | Log.d(TAG, "Handling double tap."); |
| 294 | return DirectoryFragment.this.onDoubleTap(e); |
| 295 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 296 | }; |
| 297 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 298 | // TODO: instead of inserting the view into the constructor, extract listener-creation code |
| 299 | // and set the listener on the view after the fact. Then the view doesn't need to be passed |
| 300 | // into the selection manager which is passed into the model. |
| 301 | MultiSelectManager selMgr= new MultiSelectManager( |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 302 | mRecView, |
| 303 | listener, |
| 304 | state.allowMultiple |
| 305 | ? MultiSelectManager.MODE_MULTIPLE |
| 306 | : MultiSelectManager.MODE_SINGLE); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 307 | selMgr.addCallback(new SelectionModeListener()); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 308 | |
| 309 | mModel = new Model(context, selMgr); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 310 | mModel.setSelectionManager(selMgr); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 311 | mModel.addUpdateListener(mAdapter); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 312 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 313 | mType = getArguments().getInt(EXTRA_TYPE); |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 314 | mStateKey = buildStateKey(root, doc); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 315 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 316 | mFragmentTuner = pickFragmentTuner(state); |
| 317 | mClipper = new DocumentClipper(context); |
| 318 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 319 | if (mType == TYPE_RECENT_OPEN) { |
| 320 | // Hide titles when showing recents for picking images/videos |
| 321 | mHideGridTitles = MimePredicate.mimeMatches( |
| 322 | MimePredicate.VISUAL_MIMES, state.acceptMimes); |
| 323 | } else { |
| 324 | mHideGridTitles = (doc != null) && doc.isGridTitlesHidden(); |
| 325 | } |
| 326 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 327 | final ActivityManager am = (ActivityManager) context.getSystemService( |
| 328 | Context.ACTIVITY_SERVICE); |
| 329 | mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN); |
| 330 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 331 | mCallbacks = new LoaderCallbacks<DirectoryResult>() { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 332 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 333 | public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 334 | final String query = getArguments().getString(EXTRA_QUERY); |
Jeff Sharkey | 46165b5 | 2013-07-31 20:53:22 -0700 | [diff] [blame] | 335 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 336 | Uri contentsUri; |
| 337 | switch (mType) { |
| 338 | case TYPE_NORMAL: |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 339 | contentsUri = DocumentsContract.buildChildDocumentsUri( |
| 340 | doc.authority, doc.documentId); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 341 | if (state.action == ACTION_MANAGE) { |
| 342 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 343 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 344 | return new DirectoryLoader( |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 345 | context, mType, root, doc, contentsUri, state.userSortOrder); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 346 | case TYPE_SEARCH: |
| 347 | contentsUri = DocumentsContract.buildSearchDocumentsUri( |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 348 | root.authority, root.rootId, query); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 349 | if (state.action == ACTION_MANAGE) { |
| 350 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 351 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 352 | return new DirectoryLoader( |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 353 | context, mType, root, doc, contentsUri, state.userSortOrder); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 354 | case TYPE_RECENT_OPEN: |
Jeff Sharkey | 1c903cc | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 355 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 356 | return new RecentLoader(context, roots, state); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 357 | default: |
| 358 | throw new IllegalStateException("Unknown type " + mType); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 359 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 363 | public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) { |
Makoto Onuki | 7777875 | 2015-07-01 14:55:14 -0700 | [diff] [blame] | 364 | if (result == null || result.exception != null) { |
| 365 | // onBackPressed does a fragment transaction, which can't be done inside |
| 366 | // onLoadFinished |
| 367 | mHandler.post(new Runnable() { |
| 368 | @Override |
| 369 | public void run() { |
| 370 | final Activity activity = getActivity(); |
| 371 | if (activity != null) { |
| 372 | activity.onBackPressed(); |
| 373 | } |
| 374 | } |
| 375 | }); |
| 376 | return; |
| 377 | } |
| 378 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 379 | if (!isAdded()) return; |
| 380 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 381 | mModel.update(result); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 382 | |
| 383 | // Push latest state up to UI |
| 384 | // TODO: if mode change was racing with us, don't overwrite it |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 385 | if (result.mode != MODE_UNKNOWN) { |
| 386 | state.derivedMode = result.mode; |
| 387 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 388 | state.derivedSortOrder = result.sortOrder; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 389 | ((BaseActivity) context).onStateChanged(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 390 | |
| 391 | updateDisplayState(); |
| 392 | |
Jeff Sharkey | 25f10b3 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 393 | // When launched into empty recents, show drawer |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 394 | if (mType == TYPE_RECENT_OPEN && mModel.isEmpty() && !state.stackTouched && |
Steve McKay | b68dd22 | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 395 | context instanceof DocumentsActivity) { |
| 396 | ((DocumentsActivity) context).setRootsDrawerOpen(true); |
Jeff Sharkey | 25f10b3 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 399 | // Restore any previous instance state |
| 400 | final SparseArray<Parcelable> container = state.dirState.remove(mStateKey); |
| 401 | if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) { |
| 402 | getView().restoreHierarchyState(container); |
| 403 | } else if (mLastSortOrder != state.derivedSortOrder) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 404 | mRecView.smoothScrollToPosition(0); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 405 | } |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 406 | |
| 407 | mLastSortOrder = state.derivedSortOrder; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 411 | public void onLoaderReset(Loader<DirectoryResult> loader) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 412 | mModel.update(null); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 413 | } |
| 414 | }; |
| 415 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 416 | // Kick off loader at least once |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 417 | getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 418 | |
Kyle Horimoto | 426bd0d | 2015-07-29 15:33:49 -0700 | [diff] [blame] | 419 | mFragmentTuner.afterActivityCreated(this); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 420 | updateDisplayState(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 423 | @Override |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 424 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 425 | // There's only one request code right now. Replace this with a switch statement or |
| 426 | // something more scalable when more codes are added. |
| 427 | if (requestCode != REQUEST_COPY_DESTINATION) { |
| 428 | return; |
| 429 | } |
| 430 | if (resultCode == Activity.RESULT_CANCELED || data == null) { |
| 431 | // User pressed the back button or otherwise cancelled the destination pick. Don't |
| 432 | // proceed with the copy. |
| 433 | return; |
| 434 | } |
| 435 | |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 436 | CopyService.start(getActivity(), getDisplayState(this).selectedDocumentsForCopy, |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 437 | (DocumentStack) data.getParcelableExtra(CopyService.EXTRA_STACK), |
| 438 | data.getIntExtra(CopyService.EXTRA_TRANSFER_MODE, CopyService.TRANSFER_MODE_NONE)); |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 441 | private int getEventAdapterPosition(MotionEvent e) { |
| 442 | View view = mRecView.findChildViewUnder(e.getX(), e.getY()); |
| 443 | return view != null ? mRecView.getChildAdapterPosition(view) : RecyclerView.NO_POSITION; |
| 444 | } |
| 445 | |
| 446 | private boolean onSingleTapUp(MotionEvent e) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 447 | if (Events.isTouchEvent(e) && mModel.getSelection().isEmpty()) { |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 448 | int position = getEventAdapterPosition(e); |
| 449 | if (position != RecyclerView.NO_POSITION) { |
| 450 | return handleViewItem(position); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 453 | return false; |
| 454 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 455 | |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 456 | protected boolean onDoubleTap(MotionEvent e) { |
| 457 | if (Events.isMouseEvent(e)) { |
| 458 | Log.d(TAG, "Handling double tap from mouse."); |
| 459 | int position = getEventAdapterPosition(e); |
| 460 | if (position != RecyclerView.NO_POSITION) { |
| 461 | return handleViewItem(position); |
| 462 | } |
| 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | private boolean handleViewItem(int position) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 468 | final Cursor cursor = mModel.getItem(position); |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 469 | checkNotNull(cursor, "Cursor cannot be null."); |
| 470 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 471 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 472 | if (isDocumentEnabled(docMimeType, docFlags)) { |
| 473 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 474 | ((BaseActivity) getActivity()).onDocumentPicked(doc, mModel); |
| 475 | mModel.clearSelection(); |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 476 | return true; |
| 477 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 478 | return false; |
| 479 | } |
| 480 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 481 | @Override |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 482 | public void onStop() { |
| 483 | super.onStop(); |
| 484 | |
| 485 | // Remember last scroll location |
| 486 | final SparseArray<Parcelable> container = new SparseArray<Parcelable>(); |
| 487 | getView().saveHierarchyState(container); |
| 488 | final State state = getDisplayState(this); |
| 489 | state.dirState.put(mStateKey, container); |
| 490 | } |
| 491 | |
| 492 | @Override |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 493 | public void onResume() { |
| 494 | super.onResume(); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 495 | updateDisplayState(); |
| 496 | } |
| 497 | |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 498 | public void onDisplayStateChanged() { |
| 499 | updateDisplayState(); |
| 500 | } |
| 501 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 502 | public void onUserSortOrderChanged() { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 503 | // Sort order change always triggers reload; we'll trigger state change |
| 504 | // on the flip side. |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 505 | getLoaderManager().restartLoader(LOADER_ID, null, mCallbacks); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | public void onUserModeChanged() { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 509 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 510 | final State state = getDisplayState(this); |
| 511 | |
| 512 | final RootInfo root = getArguments().getParcelable(EXTRA_ROOT); |
| 513 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 514 | |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 515 | if (root != null && doc != null) { |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 516 | final Uri stateUri = RecentsProvider.buildState( |
| 517 | root.authority, root.rootId, doc.documentId); |
| 518 | final ContentValues values = new ContentValues(); |
| 519 | values.put(StateColumns.MODE, state.userMode); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 520 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 521 | new AsyncTask<Void, Void, Void>() { |
| 522 | @Override |
| 523 | protected Void doInBackground(Void... params) { |
| 524 | resolver.insert(stateUri, values); |
| 525 | return null; |
| 526 | } |
| 527 | }.execute(); |
| 528 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 529 | |
| 530 | // Mode change is just visual change; no need to kick loader, and |
| 531 | // deliver change event immediately. |
| 532 | state.derivedMode = state.userMode; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 533 | ((BaseActivity) getActivity()).onStateChanged(); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 534 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 535 | updateDisplayState(); |
| 536 | } |
| 537 | |
| 538 | private void updateDisplayState() { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 539 | final State state = getDisplayState(this); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 540 | |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 541 | if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 542 | mLastMode = state.derivedMode; |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 543 | mLastShowSize = state.showSize; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 544 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 545 | updateLayout(state.derivedMode); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 546 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 547 | mRecView.setAdapter(mAdapter); |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Returns a {@code LayoutManager} for {@code mode}, lazily initializing |
| 552 | * classes as needed. |
| 553 | */ |
| 554 | private void updateLayout(int mode) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 555 | final int thumbSize; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 556 | |
| 557 | final LayoutManager layout; |
| 558 | switch (mode) { |
| 559 | case MODE_GRID: |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 560 | thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width); |
Steve McKay | a9be718 | 2015-07-22 16:03:35 -0700 | [diff] [blame] | 561 | if (mGridLayout == null) { |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 562 | mGridLayout = new GridLayoutManager(getContext(), mColumnCount ); |
Steve McKay | a9be718 | 2015-07-22 16:03:35 -0700 | [diff] [blame] | 563 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 564 | layout = mGridLayout; |
| 565 | break; |
| 566 | case MODE_LIST: |
Steve McKay | a9be718 | 2015-07-22 16:03:35 -0700 | [diff] [blame] | 567 | thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 568 | if (mListLayout == null) { |
| 569 | mListLayout = new LinearLayoutManager(getContext()); |
| 570 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 571 | layout = mListLayout; |
| 572 | break; |
| 573 | case MODE_UNKNOWN: |
| 574 | default: |
| 575 | throw new IllegalArgumentException("Unsupported layout mode: " + mode); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 576 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 577 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 578 | mRecView.setLayoutManager(layout); |
Kyle Horimoto | 2da6e4a | 2015-08-27 16:44:00 -0700 | [diff] [blame] | 579 | // TODO: Once b/23691541 is resolved, use a listener within MultiSelectManager instead of |
| 580 | // imperatively calling this function. |
Steve McKay | 9058e04 | 2015-09-01 12:31:24 -0700 | [diff] [blame] | 581 | mModel.mSelectionManager.handleLayoutChanged(); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 582 | // setting layout manager automatically invalidates existing ViewHolders. |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 583 | mThumbSize = new Point(thumbSize, thumbSize); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 586 | private int calculateColumnCount() { |
| 587 | int cellWidth = getResources().getDimensionPixelSize(R.dimen.grid_width); |
| 588 | int cellMargin = 2 * getResources().getDimensionPixelSize(R.dimen.grid_item_margin); |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 589 | int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight(); |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 590 | |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 591 | checkState(mRecView.getWidth() > 0); |
| 592 | int columnCount = Math.max(1, |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 593 | (mRecView.getWidth() - viewPadding) / (cellWidth + cellMargin)); |
| 594 | |
Steve McKay | d57f5fa | 2015-07-23 16:33:41 -0700 | [diff] [blame] | 595 | return columnCount; |
| 596 | } |
| 597 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 598 | /** |
| 599 | * Manages the integration between our ActionMode and MultiSelectManager, initiating |
| 600 | * ActionMode when there is a selection, canceling it when there is no selection, |
| 601 | * and clearing selection when action mode is explicitly exited by the user. |
| 602 | */ |
| 603 | private final class SelectionModeListener |
| 604 | implements MultiSelectManager.Callback, ActionMode.Callback { |
| 605 | |
| 606 | private Selection mSelected = new Selection(); |
| 607 | private ActionMode mActionMode; |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 608 | private int mNoDeleteCount = 0; |
| 609 | private Menu mMenu; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 610 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 611 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 612 | public boolean onBeforeItemStateChange(int position, boolean selected) { |
| 613 | // Directories and footer items cannot be checked |
| 614 | if (selected) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 615 | final Cursor cursor = mModel.getItem(position); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 616 | checkNotNull(cursor, "Cursor cannot be null."); |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 617 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 618 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 619 | return isDocumentEnabled(docMimeType, docFlags); |
| 620 | } |
| 621 | return true; |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | public void onItemStateChanged(int position, boolean selected) { |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 626 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 627 | final Cursor cursor = mModel.getItem(position); |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 628 | checkNotNull(cursor, "Cursor cannot be null."); |
| 629 | |
| 630 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 631 | if ((docFlags & Document.FLAG_SUPPORTS_DELETE) == 0) { |
| 632 | mNoDeleteCount += selected ? 1 : -1; |
| 633 | } |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 634 | } |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 635 | |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 636 | @Override |
| 637 | public void onSelectionChanged() { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 638 | mModel.getSelection(mSelected); |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 639 | if (mSelected.size() > 0) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 640 | if (DEBUG) Log.d(TAG, "Maybe starting action mode."); |
| 641 | if (mActionMode == null) { |
| 642 | if (DEBUG) Log.d(TAG, "Yeah. Starting action mode."); |
| 643 | mActionMode = getActivity().startActionMode(this); |
Tomasz Mikolajewski | 2b6b066 | 2015-07-28 14:59:00 +0900 | [diff] [blame] | 644 | getActivity().getWindow().setStatusBarColor( |
| 645 | getResources().getColor(R.color.action_mode_status_bar_background)); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 646 | } |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 647 | updateActionMenu(); |
| 648 | } else { |
| 649 | if (DEBUG) Log.d(TAG, "Finishing action mode."); |
| 650 | if (mActionMode != null) { |
| 651 | mActionMode.finish(); |
| 652 | } |
Tomasz Mikolajewski | 2b6b066 | 2015-07-28 14:59:00 +0900 | [diff] [blame] | 653 | getActivity().getWindow().setStatusBarColor( |
| 654 | getResources().getColor(R.color.status_bar_background)); |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 655 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 656 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 657 | if (mActionMode != null) { |
| 658 | mActionMode.setTitle(TextUtils.formatSelectedCount(mSelected.size())); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | // Called when the user exits the action mode |
| 663 | @Override |
| 664 | public void onDestroyActionMode(ActionMode mode) { |
| 665 | if (DEBUG) Log.d(TAG, "Handling action mode destroyed."); |
| 666 | mActionMode = null; |
| 667 | // clear selection |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 668 | mModel.clearSelection(); |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 669 | mSelected.clear(); |
| 670 | mNoDeleteCount = 0; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 673 | @Override |
| 674 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 675 | mode.getMenuInflater().inflate(R.menu.mode_directory, menu); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 676 | mode.setTitle(TextUtils.formatSelectedCount(mModel.getSelection().size())); |
| 677 | return mModel.getSelection().size() > 0; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | @Override |
| 681 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 682 | mMenu = menu; |
| 683 | updateActionMenu(); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 684 | return true; |
| 685 | } |
| 686 | |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 687 | private void updateActionMenu() { |
| 688 | checkNotNull(mMenu); |
| 689 | // Delegate update logic to our owning action, since specialized logic is desired. |
| 690 | mFragmentTuner.updateActionMenu(mMenu, mType, mNoDeleteCount == 0); |
| 691 | } |
| 692 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 693 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 694 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 695 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 696 | Selection selection = mModel.getSelection(new Selection()); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 697 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 698 | final int id = item.getItemId(); |
| 699 | if (id == R.id.menu_open) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 700 | openDocuments(selection); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 701 | mode.finish(); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 702 | return true; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 703 | |
| 704 | } else if (id == R.id.menu_share) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 705 | shareDocuments(selection); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 706 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 707 | return true; |
| 708 | |
| 709 | } else if (id == R.id.menu_delete) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 710 | deleteDocuments(selection); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 711 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 712 | return true; |
| 713 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 714 | } else if (id == R.id.menu_copy_to) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 715 | transferDocuments(selection, CopyService.TRANSFER_MODE_COPY); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 716 | mode.finish(); |
| 717 | return true; |
| 718 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 719 | } else if (id == R.id.menu_move_to) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 720 | transferDocuments(selection, CopyService.TRANSFER_MODE_MOVE); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 721 | mode.finish(); |
| 722 | return true; |
| 723 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 724 | } else if (id == R.id.menu_copy_to_clipboard) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 725 | copySelectionToClipboard(selection); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 726 | mode.finish(); |
| 727 | return true; |
| 728 | |
Ben Kwa | 512a6ba | 2015-03-31 08:15:21 -0700 | [diff] [blame] | 729 | } else if (id == R.id.menu_select_all) { |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 730 | selectAllFiles(); |
Ben Kwa | 512a6ba | 2015-03-31 08:15:21 -0700 | [diff] [blame] | 731 | return true; |
| 732 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 733 | } else { |
| 734 | return false; |
| 735 | } |
| 736 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 737 | } |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 738 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 739 | private static void cancelThumbnailTask(View view) { |
| 740 | final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb); |
| 741 | if (iconThumb != null) { |
| 742 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag(); |
| 743 | if (oldTask != null) { |
| 744 | oldTask.preempt(); |
| 745 | iconThumb.setTag(null); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 748 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 749 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 750 | private void openDocuments(final Selection selected) { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 751 | new GetDocumentsTask() { |
| 752 | @Override |
| 753 | void onDocumentsReady(List<DocumentInfo> docs) { |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 754 | // TODO: Implement support in Files activity for opening multiple docs. |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 755 | BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 756 | } |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 757 | }.execute(selected); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 760 | private void shareDocuments(final Selection selected) { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 761 | new GetDocumentsTask() { |
| 762 | @Override |
| 763 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 764 | Intent intent; |
| 765 | |
| 766 | // Filter out directories - those can't be shared. |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 767 | List<DocumentInfo> docsForSend = new ArrayList<>(); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 768 | for (DocumentInfo doc: docs) { |
| 769 | if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) { |
| 770 | docsForSend.add(doc); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | if (docsForSend.size() == 1) { |
| 775 | final DocumentInfo doc = docsForSend.get(0); |
| 776 | |
| 777 | intent = new Intent(Intent.ACTION_SEND); |
| 778 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 779 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 780 | intent.setType(doc.mimeType); |
| 781 | intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri); |
| 782 | |
| 783 | } else if (docsForSend.size() > 1) { |
| 784 | intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 785 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 786 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 787 | |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 788 | final ArrayList<String> mimeTypes = new ArrayList<>(); |
| 789 | final ArrayList<Uri> uris = new ArrayList<>(); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 790 | for (DocumentInfo doc : docsForSend) { |
| 791 | mimeTypes.add(doc.mimeType); |
| 792 | uris.add(doc.derivedUri); |
| 793 | } |
| 794 | |
| 795 | intent.setType(findCommonMimeType(mimeTypes)); |
| 796 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 797 | |
| 798 | } else { |
| 799 | return; |
| 800 | } |
| 801 | |
| 802 | intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via)); |
| 803 | startActivity(intent); |
| 804 | } |
| 805 | }.execute(selected); |
| 806 | } |
| 807 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 808 | private void deleteDocuments(final Selection selected) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 809 | Context context = getActivity(); |
| 810 | ContentResolver resolver = context.getContentResolver(); |
| 811 | String message = Shared.getQuantityString(context, R.plurals.deleting, selected.size()); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 812 | |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 813 | mModel.markForDeletion(selected); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 814 | |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 815 | Activity activity = getActivity(); |
| 816 | Snackbar.make(this.getView(), message, Snackbar.LENGTH_LONG) |
| 817 | .setAction( |
| 818 | R.string.undo, |
| 819 | new android.view.View.OnClickListener() { |
| 820 | @Override |
| 821 | public void onClick(View view) {} |
| 822 | }) |
| 823 | .setCallback( |
| 824 | new Snackbar.Callback() { |
| 825 | @Override |
| 826 | public void onDismissed(Snackbar snackbar, int event) { |
| 827 | if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) { |
| 828 | mModel.undoDeletion(); |
| 829 | } else { |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 830 | // TODO: Use a listener rather than pushing the view. |
| 831 | mModel.finalizeDeletion(DirectoryFragment.this.getView()); |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 832 | } |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 833 | } |
| 834 | }) |
| 835 | .show(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 838 | private void transferDocuments(final Selection selected, final int mode) { |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 839 | // Pop up a dialog to pick a destination. This is inadequate but works for now. |
| 840 | // TODO: Implement a picker that is to spec. |
Daichi Hirono | caadd41 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 841 | final Intent intent = new Intent( |
Daichi Hirono | 22574ed | 2015-04-15 13:41:18 +0900 | [diff] [blame] | 842 | BaseActivity.DocumentsIntent.ACTION_OPEN_COPY_DESTINATION, |
Daichi Hirono | caadd41 | 2015-04-10 15:50:38 +0900 | [diff] [blame] | 843 | Uri.EMPTY, |
| 844 | getActivity(), |
| 845 | DocumentsActivity.class); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 846 | |
| 847 | new GetDocumentsTask() { |
| 848 | @Override |
| 849 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 850 | getDisplayState(DirectoryFragment.this).selectedDocumentsForCopy = docs; |
| 851 | |
| 852 | boolean directoryCopy = false; |
| 853 | for (DocumentInfo info : docs) { |
| 854 | if (Document.MIME_TYPE_DIR.equals(info.mimeType)) { |
| 855 | directoryCopy = true; |
| 856 | break; |
| 857 | } |
| 858 | } |
| 859 | intent.putExtra(BaseActivity.DocumentsIntent.EXTRA_DIRECTORY_COPY, directoryCopy); |
| 860 | intent.putExtra(CopyService.EXTRA_TRANSFER_MODE, mode); |
| 861 | startActivityForResult(intent, REQUEST_COPY_DESTINATION); |
Daichi Hirono | f2a822d | 2015-04-14 17:12:54 +0900 | [diff] [blame] | 862 | } |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 863 | }.execute(selected); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 866 | private static State getDisplayState(Fragment fragment) { |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 867 | return ((BaseActivity) fragment.getActivity()).getDisplayState(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 870 | private static abstract class Footer { |
| 871 | private final int mItemViewType; |
| 872 | |
| 873 | public Footer(int itemViewType) { |
| 874 | mItemViewType = itemViewType; |
| 875 | } |
| 876 | |
| 877 | public abstract View getView(View convertView, ViewGroup parent); |
| 878 | |
| 879 | public int getItemViewType() { |
| 880 | return mItemViewType; |
| 881 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 884 | private class LoadingFooter extends Footer { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 885 | public LoadingFooter() { |
| 886 | super(1); |
| 887 | } |
| 888 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 889 | @Override |
| 890 | public View getView(View convertView, ViewGroup parent) { |
| 891 | final Context context = parent.getContext(); |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 892 | final State state = getDisplayState(DirectoryFragment.this); |
| 893 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 894 | if (convertView == null) { |
| 895 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 896 | if (state.derivedMode == MODE_LIST) { |
| 897 | convertView = inflater.inflate(R.layout.item_loading_list, parent, false); |
| 898 | } else if (state.derivedMode == MODE_GRID) { |
| 899 | convertView = inflater.inflate(R.layout.item_loading_grid, parent, false); |
| 900 | } else { |
| 901 | throw new IllegalStateException(); |
| 902 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 903 | } |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 904 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 905 | return convertView; |
| 906 | } |
| 907 | } |
| 908 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 909 | private class MessageFooter extends Footer { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 910 | private final int mIcon; |
| 911 | private final String mMessage; |
| 912 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 913 | public MessageFooter(int itemViewType, int icon, String message) { |
| 914 | super(itemViewType); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 915 | mIcon = icon; |
| 916 | mMessage = message; |
| 917 | } |
| 918 | |
| 919 | @Override |
| 920 | public View getView(View convertView, ViewGroup parent) { |
| 921 | final Context context = parent.getContext(); |
| 922 | final State state = getDisplayState(DirectoryFragment.this); |
| 923 | |
| 924 | if (convertView == null) { |
| 925 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 926 | if (state.derivedMode == MODE_LIST) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 927 | convertView = inflater.inflate(R.layout.item_message_list, parent, false); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 928 | } else if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 929 | convertView = inflater.inflate(R.layout.item_message_grid, parent, false); |
| 930 | } else { |
| 931 | throw new IllegalStateException(); |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
| 936 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 937 | icon.setImageResource(mIcon); |
| 938 | title.setText(mMessage); |
| 939 | return convertView; |
| 940 | } |
| 941 | } |
| 942 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 943 | // Provide a reference to the views for each data item |
| 944 | // Complex data items may need more than one view per item, and |
| 945 | // you provide access to all the views for a data item in a view holder |
| 946 | private static final class DocumentHolder extends RecyclerView.ViewHolder { |
| 947 | // each data item is just a string in this case |
| 948 | public View view; |
| 949 | public String docId; // The stable document id. |
| 950 | public DocumentHolder(View view) { |
| 951 | super(view); |
| 952 | this.view = view; |
| 953 | } |
| 954 | } |
| 955 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 956 | private final class DocumentsAdapter extends RecyclerView.Adapter<DocumentHolder> |
| 957 | implements Model.UpdateListener { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 958 | |
| 959 | private final Context mContext; |
| 960 | private final LayoutInflater mInflater; |
| 961 | // TODO: Bring back support for footers. |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 962 | private final List<Footer> mFooters = new ArrayList<>(); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 963 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 964 | public DocumentsAdapter(Context context) { |
| 965 | mContext = context; |
| 966 | mInflater = LayoutInflater.from(context); |
| 967 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 968 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 969 | public void onModelUpdate(Model model) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 970 | mFooters.clear(); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 971 | if (model.info != null) { |
| 972 | mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, model.info)); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 973 | } |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 974 | if (model.error != null) { |
| 975 | mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, model.error)); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 976 | } |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 977 | if (model.isLoading()) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 978 | mFooters.add(new LoadingFooter()); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 979 | } |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 980 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 981 | if (model.isEmpty()) { |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 982 | mEmptyView.setVisibility(View.VISIBLE); |
| 983 | } else { |
| 984 | mEmptyView.setVisibility(View.GONE); |
| 985 | } |
| 986 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 987 | notifyDataSetChanged(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 990 | public void onModelUpdateFailed(Exception e) { |
| 991 | String error = getString(R.string.query_error); |
| 992 | mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error)); |
| 993 | notifyDataSetChanged(); |
| 994 | } |
| 995 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 996 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 997 | public DocumentHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 998 | final State state = getDisplayState(DirectoryFragment.this); |
| 999 | final LayoutInflater inflater = LayoutInflater.from(getContext()); |
| 1000 | switch (state.derivedMode) { |
| 1001 | case MODE_GRID: |
| 1002 | return new DocumentHolder(inflater.inflate(R.layout.item_doc_grid, parent, false)); |
| 1003 | case MODE_LIST: |
| 1004 | return new DocumentHolder(inflater.inflate(R.layout.item_doc_list, parent, false)); |
| 1005 | case MODE_UNKNOWN: |
| 1006 | default: |
| 1007 | throw new IllegalStateException("Unsupported layout mode."); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1011 | @Override |
| 1012 | public void onBindViewHolder(DocumentHolder holder, int position) { |
| 1013 | |
| 1014 | final Context context = getContext(); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 1015 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1016 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 1017 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
| 1018 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 1019 | context, mThumbSize); |
| 1020 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1021 | final Cursor cursor = mModel.getItem(position); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1022 | checkNotNull(cursor, "Cursor cannot be null."); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1023 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 1024 | final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY); |
| 1025 | final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1026 | final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID); |
| 1027 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 1028 | final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME); |
| 1029 | final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED); |
| 1030 | final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON); |
| 1031 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 1032 | final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY); |
| 1033 | final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1034 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1035 | holder.docId = docId; |
| 1036 | final View itemView = holder.view; |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1037 | itemView.setActivated(mModel.isSelected(position)); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1038 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1039 | final View line1 = itemView.findViewById(R.id.line1); |
| 1040 | final View line2 = itemView.findViewById(R.id.line2); |
| 1041 | |
| 1042 | final ImageView iconMime = (ImageView) itemView.findViewById(R.id.icon_mime); |
| 1043 | final ImageView iconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb); |
| 1044 | final TextView title = (TextView) itemView.findViewById(android.R.id.title); |
| 1045 | final ImageView icon1 = (ImageView) itemView.findViewById(android.R.id.icon1); |
| 1046 | final ImageView icon2 = (ImageView) itemView.findViewById(android.R.id.icon2); |
| 1047 | final TextView summary = (TextView) itemView.findViewById(android.R.id.summary); |
| 1048 | final TextView date = (TextView) itemView.findViewById(R.id.date); |
| 1049 | final TextView size = (TextView) itemView.findViewById(R.id.size); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1050 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1051 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1052 | if (oldTask != null) { |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 1053 | oldTask.preempt(); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1054 | iconThumb.setTag(null); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1057 | iconMime.animate().cancel(); |
| 1058 | iconThumb.animate().cancel(); |
| 1059 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1060 | final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 1061 | final boolean allowThumbnail = (state.derivedMode == MODE_GRID) |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1062 | || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 1063 | final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1064 | |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1065 | final boolean enabled = isDocumentEnabled(docMimeType, docFlags); |
| 1066 | final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f; |
| 1067 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1068 | boolean cacheHit = false; |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1069 | if (showThumbnail) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 1070 | final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1071 | final Bitmap cachedResult = thumbs.get(uri); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1072 | if (cachedResult != null) { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1073 | iconThumb.setImageBitmap(cachedResult); |
| 1074 | cacheHit = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1075 | } else { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1076 | iconThumb.setImageDrawable(null); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1077 | // TODO: Hang this off DocumentHolder? |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1078 | final ThumbnailAsyncTask task = new ThumbnailAsyncTask( |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1079 | uri, iconMime, iconThumb, mThumbSize, iconAlpha); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1080 | iconThumb.setTag(task); |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 1081 | ProviderExecutor.forAuthority(docAuthority).execute(task); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1082 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | // Always throw MIME icon into place, even when a thumbnail is being |
| 1086 | // loaded in background. |
| 1087 | if (cacheHit) { |
| 1088 | iconMime.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 1089 | iconMime.setImageDrawable(null); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1090 | iconThumb.setAlpha(1f); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1091 | } else { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1092 | iconMime.setAlpha(1f); |
| 1093 | iconThumb.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 1094 | iconThumb.setImageDrawable(null); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1095 | iconMime.setImageDrawable( |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1096 | getDocumentIcon(mContext, docAuthority, docId, docMimeType, docIcon, state)); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1099 | boolean hasLine1 = false; |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1100 | boolean hasLine2 = false; |
| 1101 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1102 | final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles; |
| 1103 | if (!hideTitle) { |
| 1104 | title.setText(docDisplayName); |
| 1105 | hasLine1 = true; |
| 1106 | } |
| 1107 | |
| 1108 | Drawable iconDrawable = null; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 1109 | if (mType == TYPE_RECENT_OPEN) { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 1110 | // We've already had to enumerate roots before any results can |
| 1111 | // be shown, so this will never block. |
| 1112 | final RootInfo root = roots.getRootBlocking(docAuthority, docRootId); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 1113 | if (state.derivedMode == MODE_GRID) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1114 | iconDrawable = root.loadGridIcon(mContext); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 1115 | } else { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1116 | iconDrawable = root.loadIcon(mContext); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 1117 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1118 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1119 | if (summary != null) { |
| 1120 | final boolean alwaysShowSummary = getResources() |
| 1121 | .getBoolean(R.bool.always_show_summary); |
| 1122 | if (alwaysShowSummary) { |
| 1123 | summary.setText(root.getDirectoryString()); |
| 1124 | summary.setVisibility(View.VISIBLE); |
| 1125 | hasLine2 = true; |
| 1126 | } else { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 1127 | if (iconDrawable != null && roots.isIconUniqueBlocking(root)) { |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1128 | // No summary needed if icon speaks for itself |
| 1129 | summary.setVisibility(View.INVISIBLE); |
| 1130 | } else { |
| 1131 | summary.setText(root.getDirectoryString()); |
| 1132 | summary.setVisibility(View.VISIBLE); |
| 1133 | summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END); |
| 1134 | hasLine2 = true; |
| 1135 | } |
| 1136 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1137 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1138 | } else { |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1139 | // Directories showing thumbnails in grid mode get a little icon |
| 1140 | // hint to remind user they're a directory. |
| 1141 | if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID |
| 1142 | && showThumbnail) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1143 | iconDrawable = IconUtils.applyTintAttr(mContext, R.drawable.ic_doc_folder, |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 1144 | android.R.attr.textColorPrimaryInverse); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1147 | if (summary != null) { |
| 1148 | if (docSummary != null) { |
| 1149 | summary.setText(docSummary); |
| 1150 | summary.setVisibility(View.VISIBLE); |
| 1151 | hasLine2 = true; |
| 1152 | } else { |
| 1153 | summary.setVisibility(View.INVISIBLE); |
| 1154 | } |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 1155 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1158 | if (icon1 != null) icon1.setVisibility(View.GONE); |
| 1159 | if (icon2 != null) icon2.setVisibility(View.GONE); |
| 1160 | |
| 1161 | if (iconDrawable != null) { |
| 1162 | if (hasLine1) { |
| 1163 | icon1.setVisibility(View.VISIBLE); |
| 1164 | icon1.setImageDrawable(iconDrawable); |
| 1165 | } else { |
| 1166 | icon2.setVisibility(View.VISIBLE); |
| 1167 | icon2.setImageDrawable(iconDrawable); |
| 1168 | } |
| 1169 | } |
| 1170 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1171 | if (docLastModified == -1) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1172 | date.setText(null); |
| 1173 | } else { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1174 | date.setText(formatTime(mContext, docLastModified)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1175 | hasLine2 = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1176 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1177 | |
| 1178 | if (state.showSize) { |
| 1179 | size.setVisibility(View.VISIBLE); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1180 | if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) { |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1181 | size.setText(null); |
| 1182 | } else { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1183 | size.setText(Formatter.formatFileSize(mContext, docSize)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1184 | hasLine2 = true; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1185 | } |
| 1186 | } else { |
| 1187 | size.setVisibility(View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 1188 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1189 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1190 | if (line1 != null) { |
| 1191 | line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE); |
| 1192 | } |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1193 | if (line2 != null) { |
| 1194 | line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE); |
| 1195 | } |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1196 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1197 | setEnabledRecursive(itemView, enabled); |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1198 | |
| 1199 | iconMime.setAlpha(iconAlpha); |
| 1200 | iconThumb.setAlpha(iconAlpha); |
| 1201 | if (icon1 != null) icon1.setAlpha(iconAlpha); |
| 1202 | if (icon2 != null) icon2.setAlpha(iconAlpha); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1203 | |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 1204 | if (DEBUG_ENABLE_DND) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1205 | setupDragAndDropOnDocumentView(itemView, cursor); |
Steve McKay | 8e258c6 | 2015-05-06 14:27:57 -0700 | [diff] [blame] | 1206 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1207 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 1208 | |
Steve McKay | 351a749 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 1209 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1210 | public int getItemCount() { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1211 | return mModel.getItemCount(); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1212 | // return mCursorCount + mFooters.size(); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | @Override |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1216 | public int getItemViewType(int position) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1217 | final int itemCount = mModel.getItemCount(); |
| 1218 | if (position < itemCount) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1219 | return 0; |
| 1220 | } else { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1221 | position -= itemCount; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1222 | return mFooters.get(position).getItemViewType(); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1223 | } |
| 1224 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | private static String formatTime(Context context, long when) { |
| 1228 | // TODO: DateUtils should make this easier |
| 1229 | Time then = new Time(); |
| 1230 | then.set(when); |
| 1231 | Time now = new Time(); |
| 1232 | now.setToNow(); |
| 1233 | |
| 1234 | int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT |
| 1235 | | DateUtils.FORMAT_ABBREV_ALL; |
| 1236 | |
| 1237 | if (then.year != now.year) { |
| 1238 | flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE; |
| 1239 | } else if (then.yearDay != now.yearDay) { |
| 1240 | flags |= DateUtils.FORMAT_SHOW_DATE; |
| 1241 | } else { |
| 1242 | flags |= DateUtils.FORMAT_SHOW_TIME; |
| 1243 | } |
| 1244 | |
| 1245 | return DateUtils.formatDateTime(context, when, flags); |
| 1246 | } |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 1247 | |
| 1248 | private String findCommonMimeType(List<String> mimeTypes) { |
| 1249 | String[] commonType = mimeTypes.get(0).split("/"); |
| 1250 | if (commonType.length != 2) { |
| 1251 | return "*/*"; |
| 1252 | } |
| 1253 | |
| 1254 | for (int i = 1; i < mimeTypes.size(); i++) { |
| 1255 | String[] type = mimeTypes.get(i).split("/"); |
| 1256 | if (type.length != 2) continue; |
| 1257 | |
| 1258 | if (!commonType[1].equals(type[1])) { |
| 1259 | commonType[1] = "*"; |
| 1260 | } |
| 1261 | |
| 1262 | if (!commonType[0].equals(type[0])) { |
| 1263 | commonType[0] = "*"; |
| 1264 | commonType[1] = "*"; |
| 1265 | break; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | return commonType[0] + "/" + commonType[1]; |
| 1270 | } |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1271 | |
| 1272 | private void setEnabledRecursive(View v, boolean enabled) { |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1273 | if (v == null) return; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1274 | if (v.isEnabled() == enabled) return; |
| 1275 | v.setEnabled(enabled); |
| 1276 | |
| 1277 | if (v instanceof ViewGroup) { |
| 1278 | final ViewGroup vg = (ViewGroup) v; |
| 1279 | for (int i = vg.getChildCount() - 1; i >= 0; i--) { |
| 1280 | setEnabledRecursive(vg.getChildAt(i), enabled); |
| 1281 | } |
| 1282 | } |
| 1283 | } |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1284 | |
| 1285 | private boolean isDocumentEnabled(String docMimeType, int docFlags) { |
| 1286 | final State state = getDisplayState(DirectoryFragment.this); |
| 1287 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1288 | // Directories are always enabled |
| 1289 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 1290 | return true; |
| 1291 | } |
| 1292 | |
Jeff Sharkey | 783ebc2 | 2013-09-26 19:42:52 -0700 | [diff] [blame] | 1293 | // Read-only files are disabled when creating |
| 1294 | if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) { |
| 1295 | return false; |
| 1296 | } |
| 1297 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1298 | return MimePredicate.mimeMatches(state.acceptMimes, docMimeType); |
| 1299 | } |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1300 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1301 | private void copyFromClipboard() { |
| 1302 | new AsyncTask<Void, Void, List<DocumentInfo>>() { |
| 1303 | |
| 1304 | @Override |
| 1305 | protected List<DocumentInfo> doInBackground(Void... params) { |
| 1306 | return mClipper.getClippedDocuments(); |
| 1307 | } |
| 1308 | |
| 1309 | @Override |
| 1310 | protected void onPostExecute(List<DocumentInfo> docs) { |
| 1311 | DocumentInfo destination = |
| 1312 | ((BaseActivity) getActivity()).getCurrentDirectory(); |
| 1313 | copyDocuments(docs, destination); |
| 1314 | } |
| 1315 | }.execute(); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1318 | private void copyFromClipData(final ClipData clipData, final DocumentInfo destination) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1319 | checkNotNull(clipData); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1320 | new AsyncTask<Void, Void, List<DocumentInfo>>() { |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1321 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1322 | @Override |
| 1323 | protected List<DocumentInfo> doInBackground(Void... params) { |
| 1324 | return mClipper.getDocumentsFromClipData(clipData); |
| 1325 | } |
| 1326 | |
| 1327 | @Override |
| 1328 | protected void onPostExecute(List<DocumentInfo> docs) { |
| 1329 | copyDocuments(docs, destination); |
| 1330 | } |
| 1331 | }.execute(); |
| 1332 | } |
| 1333 | |
| 1334 | private void copyDocuments(final List<DocumentInfo> docs, final DocumentInfo destination) { |
| 1335 | if (!canCopy(docs, destination)) { |
| 1336 | Toast.makeText( |
| 1337 | getActivity(), |
| 1338 | R.string.clipboard_files_cannot_paste, Toast.LENGTH_SHORT).show(); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1339 | return; |
| 1340 | } |
| 1341 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1342 | if (docs.isEmpty()) { |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1343 | return; |
Vladislav Kaznacheev | e16887a | 2015-05-05 12:09:47 -0700 | [diff] [blame] | 1344 | } |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1345 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1346 | final DocumentStack curStack = getDisplayState(DirectoryFragment.this).stack; |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1347 | DocumentStack tmpStack = new DocumentStack(); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1348 | if (destination != null) { |
| 1349 | tmpStack.push(destination); |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1350 | tmpStack.addAll(curStack); |
| 1351 | } else { |
| 1352 | tmpStack = curStack; |
| 1353 | } |
| 1354 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1355 | CopyService.start(getActivity(), docs, tmpStack, CopyService.TRANSFER_MODE_COPY); |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | private ClipData getClipDataFromDocuments(List<DocumentInfo> docs) { |
| 1359 | Context context = getActivity(); |
| 1360 | final ContentResolver resolver = context.getContentResolver(); |
| 1361 | ClipData clipData = null; |
| 1362 | for (DocumentInfo doc : docs) { |
| 1363 | final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId); |
| 1364 | if (clipData == null) { |
Vladislav Kaznacheev | e16887a | 2015-05-05 12:09:47 -0700 | [diff] [blame] | 1365 | // TODO: figure out what this string should be. |
| 1366 | // Currently it is not displayed anywhere in the UI, but this might change. |
| 1367 | final String label = ""; |
| 1368 | clipData = ClipData.newUri(resolver, label, uri); |
Vladislav Kaznacheev | 89b9033 | 2015-05-01 13:46:57 -0700 | [diff] [blame] | 1369 | } else { |
| 1370 | // TODO: update list of mime types in ClipData. |
| 1371 | clipData.addItem(new ClipData.Item(uri)); |
| 1372 | } |
| 1373 | } |
| 1374 | return clipData; |
| 1375 | } |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1376 | |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1377 | void copySelectedToClipboard() { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1378 | Selection sel = mModel.getSelection(new Selection()); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1379 | copySelectionToClipboard(sel); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1380 | } |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1381 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1382 | void copySelectionToClipboard(Selection items) { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1383 | new GetDocumentsTask() { |
| 1384 | @Override |
| 1385 | void onDocumentsReady(List<DocumentInfo> docs) { |
| 1386 | mClipper.clipDocuments(docs); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1387 | Activity activity = getActivity(); |
| 1388 | Toast.makeText(activity, |
| 1389 | activity.getResources().getQuantityString( |
| 1390 | R.plurals.clipboard_files_clipped, docs.size(), docs.size()), |
| 1391 | Toast.LENGTH_SHORT).show(); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1392 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1393 | }.execute(items); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | void pasteFromClipboard() { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1397 | copyFromClipboard(); |
| 1398 | getActivity().invalidateOptionsMenu(); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1401 | /** |
| 1402 | * Returns true if the list of files can be copied to destination. Note that this |
| 1403 | * is a policy check only. Currently the method does not attempt to verify |
| 1404 | * available space or any other environmental aspects possibly resulting in |
| 1405 | * failure to copy. |
| 1406 | * |
| 1407 | * @return true if the list of files can be copied to destination. |
| 1408 | */ |
| 1409 | boolean canCopy(List<DocumentInfo> files, DocumentInfo dest) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1410 | BaseActivity activity = (BaseActivity) getActivity(); |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1411 | |
| 1412 | final RootInfo root = activity.getCurrentRoot(); |
| 1413 | |
| 1414 | // Can't copy folders to Downloads. |
| 1415 | if (root.isDownloads()) { |
| 1416 | for (DocumentInfo docs : files) { |
| 1417 | if (docs.isDirectory()) { |
| 1418 | return false; |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | return dest != null && dest.isDirectory() && dest.isCreateSupported(); |
| 1424 | } |
| 1425 | |
| 1426 | void selectAllFiles() { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1427 | boolean changed = mModel.selectAll(); |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 1428 | if (changed) { |
| 1429 | updateDisplayState(); |
| 1430 | } |
Steve McKay | 0599a44 | 2015-05-05 14:50:00 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1433 | private void setupDragAndDropOnDirectoryView(View view) { |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1434 | // Listen for drops on non-directory items and empty space. |
| 1435 | view.setOnDragListener(mOnDragListener); |
| 1436 | } |
| 1437 | |
| 1438 | private void setupDragAndDropOnDocumentView(View view, Cursor cursor) { |
| 1439 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 1440 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 1441 | // Make a directory item a drop target. Drop on non-directories and empty space |
| 1442 | // is handled at the list/grid view level. |
| 1443 | view.setOnDragListener(mOnDragListener); |
| 1444 | } |
| 1445 | |
| 1446 | // Temporary: attaching the listener to the title only. |
| 1447 | // Attaching to the entire item conflicts with the item long click handler responsible |
| 1448 | // for item selection. |
| 1449 | final View title = view.findViewById(android.R.id.title); |
| 1450 | title.setOnLongClickListener(mLongClickListener); |
| 1451 | } |
| 1452 | |
| 1453 | private View.OnDragListener mOnDragListener = new View.OnDragListener() { |
| 1454 | @Override |
| 1455 | public boolean onDrag(View v, DragEvent event) { |
| 1456 | switch (event.getAction()) { |
| 1457 | case DragEvent.ACTION_DRAG_STARTED: |
| 1458 | // TODO: Check if the event contains droppable data. |
| 1459 | return true; |
| 1460 | |
| 1461 | // TODO: Highlight potential drop target directory? |
| 1462 | // TODO: Expand drop target directory on hover? |
| 1463 | case DragEvent.ACTION_DRAG_ENTERED: |
| 1464 | case DragEvent.ACTION_DRAG_LOCATION: |
| 1465 | case DragEvent.ACTION_DRAG_EXITED: |
| 1466 | case DragEvent.ACTION_DRAG_ENDED: |
| 1467 | return true; |
| 1468 | |
| 1469 | case DragEvent.ACTION_DROP: |
Vladislav Kaznacheev | 9400b89 | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 1470 | int dstPosition = mRecView.getChildAdapterPosition(getContainingItemView(v)); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1471 | DocumentInfo dstDir = null; |
| 1472 | if (dstPosition != android.widget.AdapterView.INVALID_POSITION) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1473 | Cursor dstCursor = mModel.getItem(dstPosition); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1474 | checkNotNull(dstCursor, "Cursor cannot be null."); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1475 | dstDir = DocumentInfo.fromDirectoryCursor(dstCursor); |
| 1476 | // TODO: Do not drop into the directory where the documents came from. |
| 1477 | } |
| 1478 | copyFromClipData(event.getClipData(), dstDir); |
| 1479 | return true; |
| 1480 | } |
| 1481 | return false; |
| 1482 | } |
| 1483 | }; |
| 1484 | |
Vladislav Kaznacheev | 9400b89 | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 1485 | private View getContainingItemView(View view) { |
| 1486 | while (true) { |
| 1487 | if (view.getLayoutParams() instanceof RecyclerView.LayoutParams) { |
| 1488 | return view; |
| 1489 | } |
| 1490 | ViewParent parent = view.getParent(); |
| 1491 | if (parent == null || !(parent instanceof View)) { |
| 1492 | return null; |
| 1493 | } |
| 1494 | view = (View) parent; |
| 1495 | } |
| 1496 | } |
| 1497 | |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1498 | private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() { |
| 1499 | @Override |
| 1500 | public boolean onLongClick(View v) { |
| 1501 | final List<DocumentInfo> docs = getDraggableDocuments(v); |
| 1502 | if (docs.isEmpty()) { |
| 1503 | return false; |
| 1504 | } |
| 1505 | v.startDrag( |
| 1506 | getClipDataFromDocuments(docs), |
| 1507 | new DrawableShadowBuilder(getDragShadowIcon(docs)), |
| 1508 | null, |
| 1509 | View.DRAG_FLAG_GLOBAL |
| 1510 | ); |
| 1511 | return true; |
| 1512 | } |
| 1513 | }; |
| 1514 | |
| 1515 | private List<DocumentInfo> getDraggableDocuments(View currentItemView) { |
Vladislav Kaznacheev | 9400b89 | 2015-09-04 09:17:37 -0700 | [diff] [blame] | 1516 | int position = mRecView.getChildAdapterPosition(getContainingItemView(currentItemView)); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1517 | if (position == android.widget.AdapterView.INVALID_POSITION) { |
| 1518 | return Collections.EMPTY_LIST; |
| 1519 | } |
| 1520 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1521 | final List<DocumentInfo> selectedDocs = mModel.getSelectedDocuments(); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1522 | if (!selectedDocs.isEmpty()) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1523 | if (!mModel.isSelected(position)) { |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1524 | // There is a selection that does not include the current item, drag nothing. |
| 1525 | return Collections.EMPTY_LIST; |
| 1526 | } |
| 1527 | return selectedDocs; |
| 1528 | } |
| 1529 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1530 | final Cursor cursor = mModel.getItem(position); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1531 | checkNotNull(cursor, "Cursor cannot be null."); |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1532 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 1533 | |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1534 | return Lists.newArrayList(doc); |
| 1535 | } |
| 1536 | |
| 1537 | private Drawable getDragShadowIcon(List<DocumentInfo> docs) { |
| 1538 | if (docs.size() == 1) { |
| 1539 | final DocumentInfo doc = docs.get(0); |
| 1540 | return getDocumentIcon(getActivity(), doc.authority, doc.documentId, |
| 1541 | doc.mimeType, doc.icon, getDisplayState(this)); |
| 1542 | } |
| 1543 | return getActivity().getDrawable(R.drawable.ic_doc_generic); |
| 1544 | } |
| 1545 | |
| 1546 | public static Drawable getDocumentIcon(Context context, String docAuthority, String docId, |
| 1547 | String docMimeType, int docIcon, State state) { |
| 1548 | if (docIcon != 0) { |
| 1549 | return IconUtils.loadPackageIcon(context, docAuthority, docIcon); |
| 1550 | } else { |
| 1551 | return IconUtils.loadMimeIcon(context, docMimeType, docAuthority, docId, |
| 1552 | state.derivedMode); |
| 1553 | } |
| 1554 | } |
| 1555 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1556 | private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> |
| 1557 | implements Preemptable { |
| 1558 | private final Uri mUri; |
| 1559 | private final ImageView mIconMime; |
| 1560 | private final ImageView mIconThumb; |
| 1561 | private final Point mThumbSize; |
| 1562 | private final float mTargetAlpha; |
| 1563 | private final CancellationSignal mSignal; |
| 1564 | |
| 1565 | public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize, |
| 1566 | float targetAlpha) { |
| 1567 | mUri = uri; |
| 1568 | mIconMime = iconMime; |
| 1569 | mIconThumb = iconThumb; |
| 1570 | mThumbSize = thumbSize; |
| 1571 | mTargetAlpha = targetAlpha; |
| 1572 | mSignal = new CancellationSignal(); |
| 1573 | } |
| 1574 | |
| 1575 | @Override |
| 1576 | public void preempt() { |
| 1577 | cancel(false); |
| 1578 | mSignal.cancel(); |
| 1579 | } |
| 1580 | |
| 1581 | @Override |
| 1582 | protected Bitmap doInBackground(Uri... params) { |
| 1583 | if (isCancelled()) return null; |
| 1584 | |
| 1585 | final Context context = mIconThumb.getContext(); |
| 1586 | final ContentResolver resolver = context.getContentResolver(); |
| 1587 | |
| 1588 | ContentProviderClient client = null; |
| 1589 | Bitmap result = null; |
| 1590 | try { |
| 1591 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 1592 | resolver, mUri.getAuthority()); |
| 1593 | result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal); |
| 1594 | if (result != null) { |
| 1595 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 1596 | context, mThumbSize); |
| 1597 | thumbs.put(mUri, result); |
| 1598 | } |
| 1599 | } catch (Exception e) { |
| 1600 | if (!(e instanceof OperationCanceledException)) { |
| 1601 | Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e); |
| 1602 | } |
| 1603 | } finally { |
| 1604 | ContentProviderClient.releaseQuietly(client); |
| 1605 | } |
| 1606 | return result; |
| 1607 | } |
| 1608 | |
| 1609 | @Override |
| 1610 | protected void onPostExecute(Bitmap result) { |
| 1611 | if (mIconThumb.getTag() == this && result != null) { |
| 1612 | mIconThumb.setTag(null); |
| 1613 | mIconThumb.setImageBitmap(result); |
| 1614 | |
| 1615 | mIconMime.setAlpha(mTargetAlpha); |
| 1616 | mIconMime.animate().alpha(0f).start(); |
| 1617 | mIconThumb.setAlpha(0f); |
| 1618 | mIconThumb.animate().alpha(mTargetAlpha).start(); |
| 1619 | } |
| 1620 | } |
| 1621 | } |
| 1622 | |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1623 | private class DrawableShadowBuilder extends View.DragShadowBuilder { |
| 1624 | |
| 1625 | private final Drawable mShadow; |
| 1626 | |
| 1627 | private final int mShadowDimension; |
| 1628 | |
| 1629 | public DrawableShadowBuilder(Drawable shadow) { |
| 1630 | mShadow = shadow; |
| 1631 | mShadowDimension = getResources().getDimensionPixelSize( |
| 1632 | R.dimen.drag_shadow_size); |
| 1633 | mShadow.setBounds(0, 0, mShadowDimension, mShadowDimension); |
| 1634 | } |
| 1635 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1636 | @Override |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1637 | public void onProvideShadowMetrics( |
| 1638 | Point shadowSize, Point shadowTouchPoint) { |
| 1639 | shadowSize.set(mShadowDimension, mShadowDimension); |
| 1640 | shadowTouchPoint.set(mShadowDimension / 2, mShadowDimension / 2); |
| 1641 | } |
| 1642 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1643 | @Override |
Vladislav Kaznacheev | b6da722 | 2015-05-01 14:18:57 -0700 | [diff] [blame] | 1644 | public void onDrawShadow(Canvas canvas) { |
| 1645 | mShadow.draw(canvas); |
| 1646 | } |
| 1647 | } |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1648 | |
| 1649 | private FragmentTuner pickFragmentTuner(final State state) { |
| 1650 | return state.action == ACTION_BROWSE_ALL |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 1651 | ? new FilesTuner() |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1652 | : new DefaultTuner(state); |
| 1653 | } |
| 1654 | |
| 1655 | /** |
| 1656 | * Interface for specializing the Fragment for the "host" Activity. |
| 1657 | * Feel free to expand the role of this class to handle other specializations. |
| 1658 | */ |
| 1659 | private interface FragmentTuner { |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 1660 | void updateActionMenu(Menu menu, int dirType, boolean canDelete); |
Kyle Horimoto | 426bd0d | 2015-07-29 15:33:49 -0700 | [diff] [blame] | 1661 | void afterActivityCreated(DirectoryFragment fragment); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /** |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1665 | * Abstract task providing support for loading documents *off* |
| 1666 | * the main thread. And if it isn't obvious, creating a list |
| 1667 | * of documents (especially large lists) can be pretty expensive. |
| 1668 | */ |
| 1669 | private abstract class GetDocumentsTask |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1670 | extends AsyncTask<Selection, Void, List<DocumentInfo>> { |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1671 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1672 | protected final List<DocumentInfo> doInBackground(Selection... selected) { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1673 | return mModel.getDocuments(selected[0]); |
Steve McKay | 9276f3b | 2015-05-27 16:11:42 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | @Override |
| 1677 | protected final void onPostExecute(List<DocumentInfo> docs) { |
| 1678 | onDocumentsReady(docs); |
| 1679 | } |
| 1680 | |
| 1681 | abstract void onDocumentsReady(List<DocumentInfo> docs); |
| 1682 | } |
| 1683 | |
| 1684 | /** |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1685 | * Provides support for Platform specific specializations of DirectoryFragment. |
| 1686 | */ |
| 1687 | private static final class DefaultTuner implements FragmentTuner { |
| 1688 | |
| 1689 | private final State mState; |
| 1690 | |
| 1691 | public DefaultTuner(State state) { |
| 1692 | mState = state; |
| 1693 | } |
| 1694 | |
| 1695 | @Override |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 1696 | public void updateActionMenu(Menu menu, int dirType, boolean canDelete) { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1697 | Preconditions.checkState(mState.action != ACTION_BROWSE_ALL); |
| 1698 | |
| 1699 | final MenuItem open = menu.findItem(R.id.menu_open); |
| 1700 | final MenuItem share = menu.findItem(R.id.menu_share); |
| 1701 | final MenuItem delete = menu.findItem(R.id.menu_delete); |
| 1702 | final MenuItem copyTo = menu.findItem(R.id.menu_copy_to); |
| 1703 | final MenuItem moveTo = menu.findItem(R.id.menu_move_to); |
| 1704 | final MenuItem copyToClipboard = menu.findItem(R.id.menu_copy_to_clipboard); |
| 1705 | |
| 1706 | final boolean manageOrBrowse = (mState.action == ACTION_MANAGE |
| 1707 | || mState.action == ACTION_BROWSE); |
| 1708 | |
| 1709 | open.setVisible(!manageOrBrowse); |
| 1710 | share.setVisible(manageOrBrowse); |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 1711 | delete.setVisible(manageOrBrowse && canDelete); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1712 | // Disable copying from the Recents view. |
| 1713 | copyTo.setVisible(manageOrBrowse && dirType != TYPE_RECENT_OPEN); |
| 1714 | moveTo.setVisible(SystemProperties.getBoolean("debug.documentsui.enable_move", false)); |
| 1715 | |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 1716 | // Only shown in files mode. |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1717 | copyToClipboard.setVisible(false); |
| 1718 | } |
Kyle Horimoto | 426bd0d | 2015-07-29 15:33:49 -0700 | [diff] [blame] | 1719 | |
| 1720 | @Override |
| 1721 | public void afterActivityCreated(DirectoryFragment fragment) {} |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | /** |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 1725 | * Provides support for Files activity specific specializations of DirectoryFragment. |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1726 | */ |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 1727 | private static final class FilesTuner implements FragmentTuner { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1728 | @Override |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 1729 | public void updateActionMenu(Menu menu, int dirType, boolean canDelete) { |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1730 | menu.findItem(R.id.menu_share).setVisible(true); |
Steve McKay | 4f4232d | 2015-07-22 12:13:46 -0700 | [diff] [blame] | 1731 | menu.findItem(R.id.menu_delete).setVisible(canDelete); |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1732 | menu.findItem(R.id.menu_copy_to_clipboard).setVisible(true); |
| 1733 | |
| 1734 | menu.findItem(R.id.menu_open).setVisible(false); |
| 1735 | menu.findItem(R.id.menu_copy_to).setVisible(false); |
| 1736 | menu.findItem(R.id.menu_move_to).setVisible(false); |
| 1737 | } |
Kyle Horimoto | 426bd0d | 2015-07-29 15:33:49 -0700 | [diff] [blame] | 1738 | |
| 1739 | @Override |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1740 | public void afterActivityCreated(DirectoryFragment fragment) {} |
Steve McKay | 1f19948 | 2015-05-20 15:58:42 -0700 | [diff] [blame] | 1741 | } |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1742 | |
| 1743 | /** |
| 1744 | * The data model for the current loaded directory. |
| 1745 | */ |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1746 | @VisibleForTesting |
| 1747 | public static final class Model implements DocumentContext { |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1748 | private MultiSelectManager mSelectionManager; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1749 | private Context mContext; |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1750 | private int mCursorCount; |
| 1751 | private boolean mIsLoading; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1752 | private SparseBooleanArray mMarkedForDeletion = new SparseBooleanArray(); |
| 1753 | private UpdateListener mUpdateListener; |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1754 | @Nullable private Cursor mCursor; |
| 1755 | @Nullable private String info; |
| 1756 | @Nullable private String error; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1757 | |
| 1758 | Model(Context context, MultiSelectManager selectionManager) { |
| 1759 | mContext = context; |
| 1760 | mSelectionManager = selectionManager; |
| 1761 | } |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1762 | |
| 1763 | /** |
| 1764 | * Sets the selection manager used by the model. |
| 1765 | * TODO: the model should instantiate the selection manager. See onActivityCreated. |
| 1766 | */ |
| 1767 | void setSelectionManager(MultiSelectManager mgr) { |
| 1768 | mSelectionManager = mgr; |
| 1769 | } |
| 1770 | |
| 1771 | /** |
| 1772 | * Selects all files in the current directory. |
| 1773 | * @return true if the selection state changed for any files. |
| 1774 | */ |
| 1775 | boolean selectAll() { |
| 1776 | return mSelectionManager.setItemsSelected(0, mCursorCount, true); |
| 1777 | } |
| 1778 | |
| 1779 | /** |
| 1780 | * Clones the current selection into the given Selection object. |
| 1781 | * @param selection |
| 1782 | * @return The selection that was passed in, for convenience. |
| 1783 | */ |
| 1784 | Selection getSelection(Selection selection) { |
| 1785 | return mSelectionManager.getSelection(selection); |
| 1786 | } |
| 1787 | |
| 1788 | /** |
| 1789 | * @return The current selection (the live instance, not a copy). |
| 1790 | */ |
| 1791 | Selection getSelection() { |
| 1792 | return mSelectionManager.getSelection(); |
| 1793 | } |
| 1794 | |
| 1795 | boolean isSelected(int position) { |
| 1796 | return mSelectionManager.getSelection().contains(position); |
| 1797 | } |
| 1798 | |
| 1799 | void clearSelection() { |
| 1800 | mSelectionManager.clearSelection(); |
| 1801 | } |
| 1802 | |
| 1803 | void update(DirectoryResult result) { |
| 1804 | if (DEBUG) Log.i(TAG, "Updating model with new result set."); |
| 1805 | |
| 1806 | if (result == null) { |
| 1807 | mCursor = null; |
| 1808 | mCursorCount = 0; |
| 1809 | info = null; |
| 1810 | error = null; |
| 1811 | mIsLoading = false; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1812 | if (mUpdateListener != null) mUpdateListener.onModelUpdate(this); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1813 | return; |
| 1814 | } |
| 1815 | |
| 1816 | if (result.exception != null) { |
| 1817 | Log.e(TAG, "Error while loading directory contents", result.exception); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1818 | if (mUpdateListener != null) mUpdateListener.onModelUpdateFailed(result.exception); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1819 | return; |
| 1820 | } |
| 1821 | |
| 1822 | mCursor = result.cursor; |
| 1823 | mCursorCount = mCursor.getCount(); |
| 1824 | |
| 1825 | final Bundle extras = mCursor.getExtras(); |
| 1826 | if (extras != null) { |
| 1827 | info = extras.getString(DocumentsContract.EXTRA_INFO); |
| 1828 | error = extras.getString(DocumentsContract.EXTRA_ERROR); |
| 1829 | mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false); |
| 1830 | } |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1831 | |
| 1832 | if (mUpdateListener != null) mUpdateListener.onModelUpdate(this); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1835 | int getItemCount() { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1836 | return mCursorCount - mMarkedForDeletion.size(); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1839 | Cursor getItem(int position) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1840 | // Items marked for deletion are masked out of the UI. To do this, for every marked |
| 1841 | // item whose position is less than the requested item position, advance the requested |
| 1842 | // position by 1. |
| 1843 | final int originalPos = position; |
| 1844 | final int size = mMarkedForDeletion.size(); |
| 1845 | for (int i = 0; i <= size; ++i) { |
| 1846 | // It'd be more concise, but less efficient, to iterate over positions while calling |
| 1847 | // mMarkedForDeletion.get. Instead, iterate over deleted entries. |
| 1848 | if (mMarkedForDeletion.keyAt(i) <= position && mMarkedForDeletion.valueAt(i)) { |
| 1849 | ++position; |
| 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | if (DEBUG) { |
| 1854 | Log.d(TAG, "Item position adjusted for deletion. Original: " + originalPos |
| 1855 | + " Adjusted: " + position); |
| 1856 | } |
| 1857 | |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1858 | if (position >= mCursorCount) { |
| 1859 | throw new IndexOutOfBoundsException("Attempt to retrieve " + position + " of " + |
| 1860 | mCursorCount + " items"); |
| 1861 | } |
| 1862 | |
| 1863 | mCursor.moveToPosition(position); |
| 1864 | return mCursor; |
| 1865 | } |
| 1866 | |
| 1867 | private boolean isEmpty() { |
| 1868 | return mCursorCount == 0; |
| 1869 | } |
| 1870 | |
| 1871 | private boolean isLoading() { |
| 1872 | return mIsLoading; |
| 1873 | } |
| 1874 | |
| 1875 | private List<DocumentInfo> getSelectedDocuments() { |
| 1876 | Selection sel = getSelection(new Selection()); |
| 1877 | return getDocuments(sel); |
| 1878 | } |
| 1879 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1880 | List<DocumentInfo> getDocuments(Selection items) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1881 | final int size = (items != null) ? items.size() : 0; |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1882 | |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1883 | final List<DocumentInfo> docs = new ArrayList<>(size); |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 1884 | for (int i = 0; i < size; i++) { |
| 1885 | final Cursor cursor = getItem(items.get(i)); |
| 1886 | checkNotNull(cursor, "Cursor cannot be null."); |
| 1887 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
| 1888 | docs.add(doc); |
| 1889 | } |
| 1890 | return docs; |
| 1891 | } |
| 1892 | |
| 1893 | @Override |
| 1894 | public Cursor getCursor() { |
| 1895 | if (Looper.myLooper() != Looper.getMainLooper()) { |
| 1896 | throw new IllegalStateException("Can't call getCursor from non-main thread."); |
| 1897 | } |
| 1898 | return mCursor; |
| 1899 | } |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1900 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1901 | List<DocumentInfo> getDocumentsMarkedForDeletion() { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1902 | final int size = mMarkedForDeletion.size(); |
| 1903 | List<DocumentInfo> docs = new ArrayList<>(size); |
| 1904 | |
| 1905 | for (int i = 0; i < size; ++i) { |
| 1906 | final int position = mMarkedForDeletion.keyAt(i); |
| 1907 | checkState(position < mCursorCount); |
| 1908 | mCursor.moveToPosition(position); |
| 1909 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(mCursor); |
| 1910 | docs.add(doc); |
| 1911 | } |
| 1912 | return docs; |
| 1913 | } |
| 1914 | |
| 1915 | /** |
| 1916 | * Marks the given files for deletion. This will remove them from the UI. Clients must then |
| 1917 | * call either {@link #undoDeletion()} or {@link #finalizeDeletion()} to cancel or confirm |
| 1918 | * the deletion, respectively. Only one deletion operation is allowed at a time. |
| 1919 | * |
| 1920 | * @param selected A selection representing the files to delete. |
| 1921 | */ |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1922 | void markForDeletion(Selection selected) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1923 | // Only one deletion operation at a time. |
| 1924 | checkState(mMarkedForDeletion.size() == 0); |
| 1925 | // There should never be more to delete than what exists. |
| 1926 | checkState(mCursorCount >= selected.size()); |
| 1927 | |
| 1928 | final int size = selected.size(); |
| 1929 | for (int i = 0; i < size; ++i) { |
| 1930 | int position = selected.get(i); |
| 1931 | if (DEBUG) Log.d(TAG, "Marked position " + position + " for deletion"); |
| 1932 | mMarkedForDeletion.append(position, true); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1933 | if (mUpdateListener != null) mUpdateListener.notifyItemRemoved(position); |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | /** |
| 1938 | * Cancels an ongoing deletion operation. All files currently marked for deletion will be |
| 1939 | * unmarked, and restored in the UI. See {@link #markForDeletion(Selection)}. |
| 1940 | */ |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1941 | void undoDeletion() { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1942 | // Iterate over deleted items, temporarily marking them false in the deletion list, and |
| 1943 | // re-adding them to the UI. |
| 1944 | final int size = mMarkedForDeletion.size(); |
| 1945 | for (int i = 0; i < size; ++i) { |
| 1946 | final int position = mMarkedForDeletion.keyAt(i); |
| 1947 | mMarkedForDeletion.put(position, false); |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1948 | if (mUpdateListener != null) mUpdateListener.notifyItemInserted(position); |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | // Then, clear the deletion list. |
| 1952 | mMarkedForDeletion.clear(); |
| 1953 | } |
| 1954 | |
| 1955 | /** |
| 1956 | * Finalizes an ongoing deletion operation. All files currently marked for deletion will be |
| 1957 | * deleted. See {@link #markForDeletion(Selection)}. |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1958 | * |
| 1959 | * @param view The view which will be used to interact with the user (e.g. surfacing |
| 1960 | * snackbars) for errors, info, etc. |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1961 | */ |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1962 | void finalizeDeletion(final View view) { |
| 1963 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1964 | DeleteFilesTask task = new DeleteFilesTask( |
| 1965 | resolver, |
| 1966 | new Runnable() { |
| 1967 | @Override |
| 1968 | public void run() { |
| 1969 | Snackbar.make( |
| 1970 | view, |
| 1971 | R.string.toast_failed_delete, |
| 1972 | Snackbar.LENGTH_LONG) |
| 1973 | .show(); |
| 1974 | |
| 1975 | } |
| 1976 | }); |
| 1977 | task.execute(); |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | /** |
| 1981 | * A Task which collects the DocumentInfo for documents that have been marked for deletion, |
| 1982 | * and actually deletes them. |
| 1983 | */ |
| 1984 | private class DeleteFilesTask extends AsyncTask<Void, Void, List<DocumentInfo>> { |
| 1985 | private ContentResolver mResolver; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1986 | private Runnable mErrorCallback; |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1987 | |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1988 | /** |
| 1989 | * @param resolver A ContentResolver for performing the actual file deletions. |
| 1990 | * @param errorCallback A Runnable that is executed in the event that one or more errors |
| 1991 | * occured while copying files. Execution will occur on the UI thread. |
| 1992 | */ |
| 1993 | public DeleteFilesTask(ContentResolver resolver, Runnable errorCallback) { |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1994 | mResolver = resolver; |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 1995 | mErrorCallback = errorCallback; |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | @Override |
| 1999 | protected List<DocumentInfo> doInBackground(Void... params) { |
| 2000 | return getDocumentsMarkedForDeletion(); |
| 2001 | } |
| 2002 | |
| 2003 | @Override |
| 2004 | protected void onPostExecute(List<DocumentInfo> docs) { |
| 2005 | boolean hadTrouble = false; |
| 2006 | for (DocumentInfo doc : docs) { |
| 2007 | if (!doc.isDeleteSupported()) { |
| 2008 | Log.w(TAG, doc + " could not be deleted. Skipping..."); |
| 2009 | hadTrouble = true; |
| 2010 | continue; |
| 2011 | } |
| 2012 | |
| 2013 | ContentProviderClient client = null; |
| 2014 | try { |
| 2015 | if (DEBUG) Log.d(TAG, "Deleting: " + doc.displayName); |
| 2016 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 2017 | mResolver, doc.derivedUri.getAuthority()); |
| 2018 | DocumentsContract.deleteDocument(client, doc.derivedUri); |
| 2019 | } catch (Exception e) { |
| 2020 | Log.w(TAG, "Failed to delete " + doc); |
| 2021 | hadTrouble = true; |
| 2022 | } finally { |
| 2023 | ContentProviderClient.releaseQuietly(client); |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | if (hadTrouble) { |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 2028 | // TODO show which files failed? b/23720103 |
| 2029 | mErrorCallback.run(); |
Ben Kwa | 9192318 | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 2030 | if (DEBUG) Log.d(TAG, "Deletion task completed. Some deletions failed."); |
| 2031 | } else { |
| 2032 | if (DEBUG) Log.d(TAG, "Deletion task completed successfully."); |
| 2033 | } |
| 2034 | mMarkedForDeletion.clear(); |
| 2035 | } |
| 2036 | } |
Ben Kwa | 7461a95 | 2015-09-01 11:03:01 -0700 | [diff] [blame^] | 2037 | |
| 2038 | void addUpdateListener(UpdateListener listener) { |
| 2039 | checkState(mUpdateListener == null); |
| 2040 | mUpdateListener = listener; |
| 2041 | } |
| 2042 | |
| 2043 | interface UpdateListener { |
| 2044 | /** |
| 2045 | * Called when a successful update has occurred. |
| 2046 | */ |
| 2047 | void onModelUpdate(Model model); |
| 2048 | |
| 2049 | /** |
| 2050 | * Called when an update has been attempted but failed. |
| 2051 | */ |
| 2052 | void onModelUpdateFailed(Exception e); |
| 2053 | |
| 2054 | /** |
| 2055 | * Called when an item has been removed from the model. |
| 2056 | */ |
| 2057 | void notifyItemRemoved(int position); |
| 2058 | |
| 2059 | /** |
| 2060 | * Called when an item has been added to the model. |
| 2061 | */ |
| 2062 | void notifyItemInserted(int position); |
| 2063 | } |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 2064 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 2065 | } |