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