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