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