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