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