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 | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.DocumentsActivity.TAG; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 20 | import static com.android.documentsui.BaseActivity.State.ACTION_CREATE; |
| 21 | import static com.android.documentsui.BaseActivity.State.ACTION_MANAGE; |
| 22 | import static com.android.documentsui.BaseActivity.State.MODE_GRID; |
| 23 | import static com.android.documentsui.BaseActivity.State.MODE_LIST; |
| 24 | import static com.android.documentsui.BaseActivity.State.MODE_UNKNOWN; |
| 25 | import static com.android.documentsui.BaseActivity.State.SORT_ORDER_UNKNOWN; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 26 | import static com.android.documentsui.model.DocumentInfo.getCursorInt; |
| 27 | import static com.android.documentsui.model.DocumentInfo.getCursorLong; |
| 28 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 29 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 30 | import android.app.Activity; |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 31 | import android.app.ActivityManager; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 32 | import android.app.Fragment; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 33 | import android.app.FragmentManager; |
| 34 | import android.app.FragmentTransaction; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 35 | import android.app.LoaderManager.LoaderCallbacks; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 36 | import android.content.ContentProviderClient; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 37 | import android.content.ContentResolver; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 38 | import android.content.ContentValues; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 39 | import android.content.Context; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 40 | import android.content.Intent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 41 | import android.content.Loader; |
Jeff Sharkey | 083d7e1 | 2014-07-27 21:01:45 -0700 | [diff] [blame] | 42 | import android.content.res.Resources; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 43 | import android.database.Cursor; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 44 | import android.graphics.Bitmap; |
| 45 | import android.graphics.Point; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 46 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | db884f5 | 2013-09-21 15:11:19 -0700 | [diff] [blame] | 47 | import android.graphics.drawable.InsetDrawable; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 48 | import android.net.Uri; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 49 | import android.os.AsyncTask; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 50 | import android.os.Bundle; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 51 | import android.os.CancellationSignal; |
Jeff Sharkey | e39a89b | 2013-10-29 11:56:37 -0700 | [diff] [blame] | 52 | import android.os.OperationCanceledException; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 53 | import android.os.Parcelable; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 54 | import android.os.SystemProperties; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 55 | import android.provider.DocumentsContract; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 56 | import android.provider.DocumentsContract.Document; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 57 | import android.text.format.DateUtils; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 58 | import android.text.format.Formatter; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 59 | import android.text.format.Time; |
| 60 | import android.util.Log; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 61 | import android.util.SparseArray; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 62 | import android.util.SparseBooleanArray; |
| 63 | import android.view.ActionMode; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 64 | import android.view.LayoutInflater; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 65 | import android.view.Menu; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 66 | import android.view.MenuItem; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 67 | import android.view.View; |
| 68 | import android.view.ViewGroup; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 69 | import android.widget.AbsListView; |
| 70 | import android.widget.AbsListView.MultiChoiceModeListener; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 71 | import android.widget.AbsListView.RecyclerListener; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 72 | import android.widget.AdapterView; |
| 73 | import android.widget.AdapterView.OnItemClickListener; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 74 | import android.widget.BaseAdapter; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 75 | import android.widget.GridView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 76 | import android.widget.ImageView; |
| 77 | import android.widget.ListView; |
| 78 | import android.widget.TextView; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 79 | import android.widget.Toast; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 80 | |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 81 | import com.android.documentsui.BaseActivity.State; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 82 | import com.android.documentsui.CopyService; |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 83 | import com.android.documentsui.ProviderExecutor.Preemptable; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 84 | import com.android.documentsui.RecentsProvider.StateColumns; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 85 | import com.android.documentsui.model.DocumentInfo; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 86 | import com.android.documentsui.model.RootInfo; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 87 | import com.google.android.collect.Lists; |
| 88 | |
| 89 | import java.util.ArrayList; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 90 | import java.util.List; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Display the documents inside a single directory. |
| 94 | */ |
| 95 | public class DirectoryFragment extends Fragment { |
| 96 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 97 | private View mEmptyView; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 98 | private ListView mListView; |
| 99 | private GridView mGridView; |
| 100 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 101 | private AbsListView mCurrentView; |
| 102 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 103 | private List<DocumentInfo> mSelectedDocumentsForCopy; |
| 104 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 105 | public static final int TYPE_NORMAL = 1; |
| 106 | public static final int TYPE_SEARCH = 2; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 107 | public static final int TYPE_RECENT_OPEN = 3; |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 108 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 109 | public static final int ANIM_NONE = 1; |
| 110 | public static final int ANIM_SIDE = 2; |
| 111 | public static final int ANIM_DOWN = 3; |
| 112 | public static final int ANIM_UP = 4; |
| 113 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 114 | public static final int REQUEST_COPY_DESTINATION = 1; |
| 115 | |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 116 | private int mType = TYPE_NORMAL; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 117 | private String mStateKey; |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 118 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 119 | private int mLastMode = MODE_UNKNOWN; |
| 120 | private int mLastSortOrder = SORT_ORDER_UNKNOWN; |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 121 | private boolean mLastShowSize = false; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 122 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 123 | private boolean mHideGridTitles = false; |
| 124 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 125 | private boolean mSvelteRecents; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 126 | private Point mThumbSize; |
| 127 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 128 | private DocumentsAdapter mAdapter; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 129 | private LoaderCallbacks<DirectoryResult> mCallbacks; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 130 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 131 | private static final String EXTRA_TYPE = "type"; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 132 | private static final String EXTRA_ROOT = "root"; |
| 133 | private static final String EXTRA_DOC = "doc"; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 134 | private static final String EXTRA_QUERY = "query"; |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 135 | private static final String EXTRA_IGNORE_STATE = "ignoreState"; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 136 | |
Jeff Sharkey | c0075dc | 2013-10-25 17:12:49 -0700 | [diff] [blame] | 137 | private final int mLoaderId = 42; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 138 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 139 | public static void showNormal(FragmentManager fm, RootInfo root, DocumentInfo doc, int anim) { |
| 140 | show(fm, TYPE_NORMAL, root, doc, null, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 143 | public static void showSearch(FragmentManager fm, RootInfo root, String query, int anim) { |
| 144 | show(fm, TYPE_SEARCH, root, null, query, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 147 | public static void showRecentsOpen(FragmentManager fm, int anim) { |
| 148 | show(fm, TYPE_RECENT_OPEN, null, null, null, anim); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 151 | private static void show(FragmentManager fm, int type, RootInfo root, DocumentInfo doc, |
| 152 | String query, int anim) { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 153 | final Bundle args = new Bundle(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 154 | args.putInt(EXTRA_TYPE, type); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 155 | args.putParcelable(EXTRA_ROOT, root); |
| 156 | args.putParcelable(EXTRA_DOC, doc); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 157 | args.putString(EXTRA_QUERY, query); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 158 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 159 | final FragmentTransaction ft = fm.beginTransaction(); |
| 160 | switch (anim) { |
| 161 | case ANIM_SIDE: |
| 162 | args.putBoolean(EXTRA_IGNORE_STATE, true); |
| 163 | break; |
| 164 | case ANIM_DOWN: |
| 165 | args.putBoolean(EXTRA_IGNORE_STATE, true); |
| 166 | ft.setCustomAnimations(R.animator.dir_down, R.animator.dir_frozen); |
| 167 | break; |
| 168 | case ANIM_UP: |
| 169 | ft.setCustomAnimations(R.animator.dir_frozen, R.animator.dir_up); |
| 170 | break; |
| 171 | } |
| 172 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 173 | final DirectoryFragment fragment = new DirectoryFragment(); |
| 174 | fragment.setArguments(args); |
| 175 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 176 | ft.replace(R.id.container_directory, fragment); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 177 | ft.commitAllowingStateLoss(); |
| 178 | } |
| 179 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 180 | private static String buildStateKey(RootInfo root, DocumentInfo doc) { |
| 181 | final StringBuilder builder = new StringBuilder(); |
| 182 | builder.append(root != null ? root.authority : "null").append(';'); |
| 183 | builder.append(root != null ? root.rootId : "null").append(';'); |
| 184 | builder.append(doc != null ? doc.documentId : "null"); |
| 185 | return builder.toString(); |
| 186 | } |
| 187 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 188 | public static DirectoryFragment get(FragmentManager fm) { |
| 189 | // TODO: deal with multiple directories shown at once |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 190 | return (DirectoryFragment) fm.findFragmentById(R.id.container_directory); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 193 | @Override |
| 194 | public View onCreateView( |
| 195 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 196 | final Context context = inflater.getContext(); |
Jeff Sharkey | 083d7e1 | 2014-07-27 21:01:45 -0700 | [diff] [blame] | 197 | final Resources res = context.getResources(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 198 | final View view = inflater.inflate(R.layout.fragment_directory, container, false); |
| 199 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 200 | mEmptyView = view.findViewById(android.R.id.empty); |
| 201 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 202 | mListView = (ListView) view.findViewById(R.id.list); |
| 203 | mListView.setOnItemClickListener(mItemListener); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 204 | mListView.setMultiChoiceModeListener(mMultiListener); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 205 | mListView.setRecyclerListener(mRecycleListener); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 206 | |
Jeff Sharkey | 083d7e1 | 2014-07-27 21:01:45 -0700 | [diff] [blame] | 207 | // Indent our list divider to align with text |
| 208 | final Drawable divider = mListView.getDivider(); |
| 209 | final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left); |
| 210 | final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset); |
| 211 | if (insetLeft) { |
| 212 | mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0)); |
| 213 | } else { |
| 214 | mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0)); |
| 215 | } |
| 216 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 217 | mGridView = (GridView) view.findViewById(R.id.grid); |
| 218 | mGridView.setOnItemClickListener(mItemListener); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 219 | mGridView.setMultiChoiceModeListener(mMultiListener); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 220 | mGridView.setRecyclerListener(mRecycleListener); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 221 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 222 | return view; |
| 223 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 224 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 225 | @Override |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 226 | public void onDestroyView() { |
| 227 | super.onDestroyView(); |
| 228 | |
| 229 | // Cancel any outstanding thumbnail requests |
| 230 | final ViewGroup target = (mListView.getAdapter() != null) ? mListView : mGridView; |
| 231 | final int count = target.getChildCount(); |
| 232 | for (int i = 0; i < count; i++) { |
| 233 | final View view = target.getChildAt(i); |
| 234 | mRecycleListener.onMovedToScrapHeap(view); |
| 235 | } |
Jeff Sharkey | faaeb39 | 2013-10-04 14:44:56 -0700 | [diff] [blame] | 236 | |
| 237 | // Tear down any selection in progress |
| 238 | mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE); |
| 239 | mGridView.setChoiceMode(AbsListView.CHOICE_MODE_NONE); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 243 | public void onActivityCreated(Bundle savedInstanceState) { |
| 244 | super.onActivityCreated(savedInstanceState); |
| 245 | |
| 246 | final Context context = getActivity(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 247 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 248 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 249 | final RootInfo root = getArguments().getParcelable(EXTRA_ROOT); |
| 250 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 251 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 252 | mAdapter = new DocumentsAdapter(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 253 | mType = getArguments().getInt(EXTRA_TYPE); |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 254 | mStateKey = buildStateKey(root, doc); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 255 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 256 | if (mType == TYPE_RECENT_OPEN) { |
| 257 | // Hide titles when showing recents for picking images/videos |
| 258 | mHideGridTitles = MimePredicate.mimeMatches( |
| 259 | MimePredicate.VISUAL_MIMES, state.acceptMimes); |
| 260 | } else { |
| 261 | mHideGridTitles = (doc != null) && doc.isGridTitlesHidden(); |
| 262 | } |
| 263 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 264 | final ActivityManager am = (ActivityManager) context.getSystemService( |
| 265 | Context.ACTIVITY_SERVICE); |
| 266 | mSvelteRecents = am.isLowRamDevice() && (mType == TYPE_RECENT_OPEN); |
| 267 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 268 | mCallbacks = new LoaderCallbacks<DirectoryResult>() { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 269 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 270 | public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 271 | final String query = getArguments().getString(EXTRA_QUERY); |
Jeff Sharkey | 46165b5 | 2013-07-31 20:53:22 -0700 | [diff] [blame] | 272 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 273 | Uri contentsUri; |
| 274 | switch (mType) { |
| 275 | case TYPE_NORMAL: |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 276 | contentsUri = DocumentsContract.buildChildDocumentsUri( |
| 277 | doc.authority, doc.documentId); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 278 | if (state.action == ACTION_MANAGE) { |
| 279 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 280 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 281 | return new DirectoryLoader( |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 282 | context, mType, root, doc, contentsUri, state.userSortOrder); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 283 | case TYPE_SEARCH: |
| 284 | contentsUri = DocumentsContract.buildSearchDocumentsUri( |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 285 | root.authority, root.rootId, query); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 286 | if (state.action == ACTION_MANAGE) { |
| 287 | contentsUri = DocumentsContract.setManageMode(contentsUri); |
| 288 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 289 | return new DirectoryLoader( |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 290 | context, mType, root, doc, contentsUri, state.userSortOrder); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 291 | case TYPE_RECENT_OPEN: |
Jeff Sharkey | 1c903cc | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 292 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 293 | return new RecentLoader(context, roots, state); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 294 | default: |
| 295 | throw new IllegalStateException("Unknown type " + mType); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 296 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 300 | public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 301 | if (!isAdded()) return; |
| 302 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 303 | mAdapter.swapResult(result); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 304 | |
| 305 | // Push latest state up to UI |
| 306 | // TODO: if mode change was racing with us, don't overwrite it |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 307 | if (result.mode != MODE_UNKNOWN) { |
| 308 | state.derivedMode = result.mode; |
| 309 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 310 | state.derivedSortOrder = result.sortOrder; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 311 | ((BaseActivity) context).onStateChanged(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 312 | |
| 313 | updateDisplayState(); |
| 314 | |
Jeff Sharkey | 25f10b3 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 315 | // When launched into empty recents, show drawer |
| 316 | if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched) { |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 317 | ((BaseActivity) context).setRootsDrawerOpen(true); |
Jeff Sharkey | 25f10b3 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 320 | // Restore any previous instance state |
| 321 | final SparseArray<Parcelable> container = state.dirState.remove(mStateKey); |
| 322 | if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) { |
| 323 | getView().restoreHierarchyState(container); |
| 324 | } else if (mLastSortOrder != state.derivedSortOrder) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 325 | mListView.smoothScrollToPosition(0); |
| 326 | mGridView.smoothScrollToPosition(0); |
| 327 | } |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 328 | |
| 329 | mLastSortOrder = state.derivedSortOrder; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 333 | public void onLoaderReset(Loader<DirectoryResult> loader) { |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 334 | mAdapter.swapResult(null); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 335 | } |
| 336 | }; |
| 337 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 338 | // Kick off loader at least once |
| 339 | getLoaderManager().restartLoader(mLoaderId, null, mCallbacks); |
| 340 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 341 | updateDisplayState(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 344 | @Override |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 345 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 346 | final Context context = getActivity(); |
| 347 | final Resources res = context.getResources(); |
| 348 | |
| 349 | // There's only one request code right now. Replace this with a switch statement or |
| 350 | // something more scalable when more codes are added. |
| 351 | if (requestCode != REQUEST_COPY_DESTINATION) { |
| 352 | return; |
| 353 | } |
| 354 | if (resultCode == Activity.RESULT_CANCELED || data == null) { |
| 355 | // User pressed the back button or otherwise cancelled the destination pick. Don't |
| 356 | // proceed with the copy. |
| 357 | return; |
| 358 | } |
| 359 | |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame^] | 360 | // Because the destination picker is launched using an open tree intent, the URI returned is |
| 361 | // a tree URI. Convert it to a document URI. |
| 362 | // TODO: Remove this step when the destination picker returns a document URI. |
| 363 | final Uri destinationTree = data.getData(); |
| 364 | final Uri destination = DocumentsContract.buildDocumentUriUsingTree(destinationTree, |
| 365 | DocumentsContract.getTreeDocumentId(destinationTree)); |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 366 | |
| 367 | List<DocumentInfo> docs = mSelectedDocumentsForCopy; |
| 368 | Intent copyIntent = new Intent(context, CopyService.class); |
| 369 | copyIntent.putParcelableArrayListExtra(CopyService.EXTRA_SRC_LIST, |
| 370 | new ArrayList<DocumentInfo>(docs)); |
| 371 | copyIntent.setData(destination); |
| 372 | |
| 373 | Toast.makeText(context, |
| 374 | res.getQuantityString(R.plurals.copy_begin, docs.size(), docs.size()), |
| 375 | Toast.LENGTH_SHORT).show(); |
| 376 | context.startService(copyIntent); |
| 377 | } |
| 378 | |
| 379 | @Override |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 380 | public void onStop() { |
| 381 | super.onStop(); |
| 382 | |
| 383 | // Remember last scroll location |
| 384 | final SparseArray<Parcelable> container = new SparseArray<Parcelable>(); |
| 385 | getView().saveHierarchyState(container); |
| 386 | final State state = getDisplayState(this); |
| 387 | state.dirState.put(mStateKey, container); |
| 388 | } |
| 389 | |
| 390 | @Override |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 391 | public void onResume() { |
| 392 | super.onResume(); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 393 | updateDisplayState(); |
| 394 | } |
| 395 | |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 396 | public void onDisplayStateChanged() { |
| 397 | updateDisplayState(); |
| 398 | } |
| 399 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 400 | public void onUserSortOrderChanged() { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 401 | // Sort order change always triggers reload; we'll trigger state change |
| 402 | // on the flip side. |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 403 | getLoaderManager().restartLoader(mLoaderId, null, mCallbacks); |
| 404 | } |
| 405 | |
| 406 | public void onUserModeChanged() { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 407 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 408 | final State state = getDisplayState(this); |
| 409 | |
| 410 | final RootInfo root = getArguments().getParcelable(EXTRA_ROOT); |
| 411 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 412 | |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 413 | if (root != null && doc != null) { |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 414 | final Uri stateUri = RecentsProvider.buildState( |
| 415 | root.authority, root.rootId, doc.documentId); |
| 416 | final ContentValues values = new ContentValues(); |
| 417 | values.put(StateColumns.MODE, state.userMode); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 418 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 419 | new AsyncTask<Void, Void, Void>() { |
| 420 | @Override |
| 421 | protected Void doInBackground(Void... params) { |
| 422 | resolver.insert(stateUri, values); |
| 423 | return null; |
| 424 | } |
| 425 | }.execute(); |
| 426 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 427 | |
| 428 | // Mode change is just visual change; no need to kick loader, and |
| 429 | // deliver change event immediately. |
| 430 | state.derivedMode = state.userMode; |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 431 | ((BaseActivity) getActivity()).onStateChanged(); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 432 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 433 | updateDisplayState(); |
| 434 | } |
| 435 | |
| 436 | private void updateDisplayState() { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 437 | final State state = getDisplayState(this); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 438 | |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 439 | if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 440 | mLastMode = state.derivedMode; |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 441 | mLastShowSize = state.showSize; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 442 | |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 443 | mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE); |
| 444 | mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 445 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 446 | final int choiceMode; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 447 | if (state.allowMultiple) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 448 | choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL; |
| 449 | } else { |
| 450 | choiceMode = ListView.CHOICE_MODE_NONE; |
| 451 | } |
| 452 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 453 | final int thumbSize; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 454 | if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 455 | thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 456 | mListView.setAdapter(null); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 457 | mListView.setChoiceMode(ListView.CHOICE_MODE_NONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 458 | mGridView.setAdapter(mAdapter); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 459 | mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width)); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 460 | mGridView.setNumColumns(GridView.AUTO_FIT); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 461 | mGridView.setChoiceMode(choiceMode); |
| 462 | mCurrentView = mGridView; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 463 | } else if (state.derivedMode == MODE_LIST) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 464 | thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 465 | mGridView.setAdapter(null); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 466 | mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 467 | mListView.setAdapter(mAdapter); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 468 | mListView.setChoiceMode(choiceMode); |
| 469 | mCurrentView = mListView; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 470 | } else { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 471 | throw new IllegalStateException("Unknown state " + state.derivedMode); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 472 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 473 | |
| 474 | mThumbSize = new Point(thumbSize, thumbSize); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | private OnItemClickListener mItemListener = new OnItemClickListener() { |
| 478 | @Override |
| 479 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 480 | final Cursor cursor = mAdapter.getItem(position); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 481 | if (cursor != null) { |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 482 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 483 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 484 | if (isDocumentEnabled(docMimeType, docFlags)) { |
| 485 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 486 | ((BaseActivity) getActivity()).onDocumentPicked(doc); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 487 | } |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 488 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 489 | } |
| 490 | }; |
| 491 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 492 | private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() { |
| 493 | @Override |
| 494 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 495 | mode.getMenuInflater().inflate(R.menu.mode_directory, menu); |
Stefan Wysocki | 43c97ea | 2014-03-04 11:27:55 +0100 | [diff] [blame] | 496 | mode.setTitle(getResources() |
| 497 | .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount())); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 498 | return true; |
| 499 | } |
| 500 | |
| 501 | @Override |
| 502 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 503 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 504 | |
| 505 | final MenuItem open = menu.findItem(R.id.menu_open); |
| 506 | final MenuItem share = menu.findItem(R.id.menu_share); |
| 507 | final MenuItem delete = menu.findItem(R.id.menu_delete); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 508 | final MenuItem copy = menu.findItem(R.id.menu_copy); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 509 | |
| 510 | final boolean manageMode = state.action == ACTION_MANAGE; |
| 511 | open.setVisible(!manageMode); |
| 512 | share.setVisible(manageMode); |
| 513 | delete.setVisible(manageMode); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame^] | 514 | // Hide the copy menu item in the recents folder. For now, also hide it by default |
| 515 | // unless the debug flag is enabled. |
| 516 | copy.setVisible((mType != TYPE_RECENT_OPEN) && |
| 517 | SystemProperties.getBoolean("debug.documentsui.enable_copy", false)); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 518 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 519 | return true; |
| 520 | } |
| 521 | |
| 522 | @Override |
| 523 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 524 | final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions(); |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 525 | final ArrayList<DocumentInfo> docs = Lists.newArrayList(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 526 | final int size = checked.size(); |
| 527 | for (int i = 0; i < size; i++) { |
| 528 | if (checked.valueAt(i)) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 529 | final Cursor cursor = mAdapter.getItem(checked.keyAt(i)); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 530 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 531 | docs.add(doc); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 532 | } |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 533 | } |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 534 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 535 | final int id = item.getItemId(); |
| 536 | if (id == R.id.menu_open) { |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 537 | BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 538 | mode.finish(); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 539 | return true; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 540 | |
| 541 | } else if (id == R.id.menu_share) { |
| 542 | onShareDocuments(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 543 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 544 | return true; |
| 545 | |
| 546 | } else if (id == R.id.menu_delete) { |
| 547 | onDeleteDocuments(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 548 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 549 | return true; |
| 550 | |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 551 | } else if (id == R.id.menu_copy) { |
| 552 | onCopyDocuments(docs); |
| 553 | mode.finish(); |
| 554 | return true; |
| 555 | |
Ben Kwa | 512a6ba | 2015-03-31 08:15:21 -0700 | [diff] [blame] | 556 | } else if (id == R.id.menu_select_all) { |
| 557 | int count = mCurrentView.getCount(); |
| 558 | for (int i = 0; i < count; i++) { |
| 559 | mCurrentView.setItemChecked(i, true); |
| 560 | } |
| 561 | updateDisplayState(); |
| 562 | return true; |
| 563 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 564 | } else { |
| 565 | return false; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | @Override |
| 570 | public void onDestroyActionMode(ActionMode mode) { |
| 571 | // ignored |
| 572 | } |
| 573 | |
| 574 | @Override |
| 575 | public void onItemCheckedStateChanged( |
| 576 | ActionMode mode, int position, long id, boolean checked) { |
| 577 | if (checked) { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 578 | // Directories and footer items cannot be checked |
| 579 | boolean valid = false; |
| 580 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 581 | final Cursor cursor = mAdapter.getItem(position); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 582 | if (cursor != null) { |
| 583 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 584 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame^] | 585 | valid = isDocumentEnabled(docMimeType, docFlags); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | if (!valid) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 589 | mCurrentView.setItemChecked(position, false); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | mode.setTitle(getResources() |
| 594 | .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount())); |
| 595 | } |
| 596 | }; |
| 597 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 598 | private RecyclerListener mRecycleListener = new RecyclerListener() { |
| 599 | @Override |
| 600 | public void onMovedToScrapHeap(View view) { |
| 601 | final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb); |
| 602 | if (iconThumb != null) { |
| 603 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag(); |
| 604 | if (oldTask != null) { |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 605 | oldTask.preempt(); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 606 | iconThumb.setTag(null); |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | }; |
| 611 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 612 | private void onShareDocuments(List<DocumentInfo> docs) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 613 | Intent intent; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame^] | 614 | |
| 615 | // Filter out directories - those can't be shared. |
| 616 | List<DocumentInfo> docsForSend = Lists.newArrayList(); |
| 617 | for (DocumentInfo doc: docs) { |
| 618 | if (!Document.MIME_TYPE_DIR.equals(doc.mimeType)) { |
| 619 | docsForSend.add(doc); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (docsForSend.size() == 1) { |
| 624 | final DocumentInfo doc = docsForSend.get(0); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 625 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 626 | intent = new Intent(Intent.ACTION_SEND); |
| 627 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 628 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 629 | intent.setType(doc.mimeType); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 630 | intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 631 | |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame^] | 632 | } else if (docsForSend.size() > 1) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 633 | intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 634 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 635 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 636 | |
| 637 | final ArrayList<String> mimeTypes = Lists.newArrayList(); |
| 638 | final ArrayList<Uri> uris = Lists.newArrayList(); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame^] | 639 | for (DocumentInfo doc : docsForSend) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 640 | mimeTypes.add(doc.mimeType); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 641 | uris.add(doc.derivedUri); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | intent.setType(findCommonMimeType(mimeTypes)); |
| 645 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 646 | |
| 647 | } else { |
| 648 | return; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 651 | intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via)); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 652 | startActivity(intent); |
| 653 | } |
| 654 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 655 | private void onDeleteDocuments(List<DocumentInfo> docs) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 656 | final Context context = getActivity(); |
| 657 | final ContentResolver resolver = context.getContentResolver(); |
| 658 | |
| 659 | boolean hadTrouble = false; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 660 | for (DocumentInfo doc : docs) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 661 | if (!doc.isDeleteSupported()) { |
| 662 | Log.w(TAG, "Skipping " + doc); |
| 663 | hadTrouble = true; |
| 664 | continue; |
| 665 | } |
| 666 | |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 667 | ContentProviderClient client = null; |
| 668 | try { |
| 669 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 670 | resolver, doc.derivedUri.getAuthority()); |
| 671 | DocumentsContract.deleteDocument(client, doc.derivedUri); |
| 672 | } catch (Exception e) { |
Jeff Sharkey | 4c5fe52 | 2013-09-03 14:17:06 -0700 | [diff] [blame] | 673 | Log.w(TAG, "Failed to delete " + doc); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 674 | hadTrouble = true; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 675 | } finally { |
| 676 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
| 680 | if (hadTrouble) { |
| 681 | Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show(); |
| 682 | } |
| 683 | } |
| 684 | |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 685 | private void onCopyDocuments(List<DocumentInfo> docs) { |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 686 | mSelectedDocumentsForCopy = docs; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 687 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 688 | // Pop up a dialog to pick a destination. This is inadequate but works for now. |
| 689 | // TODO: Implement a picker that is to spec. |
| 690 | Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); |
| 691 | startActivityForResult(intent, REQUEST_COPY_DESTINATION); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 694 | private static State getDisplayState(Fragment fragment) { |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 695 | return ((BaseActivity) fragment.getActivity()).getDisplayState(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 698 | private static abstract class Footer { |
| 699 | private final int mItemViewType; |
| 700 | |
| 701 | public Footer(int itemViewType) { |
| 702 | mItemViewType = itemViewType; |
| 703 | } |
| 704 | |
| 705 | public abstract View getView(View convertView, ViewGroup parent); |
| 706 | |
| 707 | public int getItemViewType() { |
| 708 | return mItemViewType; |
| 709 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 712 | private class LoadingFooter extends Footer { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 713 | public LoadingFooter() { |
| 714 | super(1); |
| 715 | } |
| 716 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 717 | @Override |
| 718 | public View getView(View convertView, ViewGroup parent) { |
| 719 | final Context context = parent.getContext(); |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 720 | final State state = getDisplayState(DirectoryFragment.this); |
| 721 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 722 | if (convertView == null) { |
| 723 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 724 | if (state.derivedMode == MODE_LIST) { |
| 725 | convertView = inflater.inflate(R.layout.item_loading_list, parent, false); |
| 726 | } else if (state.derivedMode == MODE_GRID) { |
| 727 | convertView = inflater.inflate(R.layout.item_loading_grid, parent, false); |
| 728 | } else { |
| 729 | throw new IllegalStateException(); |
| 730 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 731 | } |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 732 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 733 | return convertView; |
| 734 | } |
| 735 | } |
| 736 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 737 | private class MessageFooter extends Footer { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 738 | private final int mIcon; |
| 739 | private final String mMessage; |
| 740 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 741 | public MessageFooter(int itemViewType, int icon, String message) { |
| 742 | super(itemViewType); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 743 | mIcon = icon; |
| 744 | mMessage = message; |
| 745 | } |
| 746 | |
| 747 | @Override |
| 748 | public View getView(View convertView, ViewGroup parent) { |
| 749 | final Context context = parent.getContext(); |
| 750 | final State state = getDisplayState(DirectoryFragment.this); |
| 751 | |
| 752 | if (convertView == null) { |
| 753 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 754 | if (state.derivedMode == MODE_LIST) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 755 | convertView = inflater.inflate(R.layout.item_message_list, parent, false); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 756 | } else if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 757 | convertView = inflater.inflate(R.layout.item_message_grid, parent, false); |
| 758 | } else { |
| 759 | throw new IllegalStateException(); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
| 764 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 765 | icon.setImageResource(mIcon); |
| 766 | title.setText(mMessage); |
| 767 | return convertView; |
| 768 | } |
| 769 | } |
| 770 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 771 | private class DocumentsAdapter extends BaseAdapter { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 772 | private Cursor mCursor; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 773 | private int mCursorCount; |
| 774 | |
| 775 | private List<Footer> mFooters = Lists.newArrayList(); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 776 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 777 | public void swapResult(DirectoryResult result) { |
| 778 | mCursor = result != null ? result.cursor : null; |
| 779 | mCursorCount = mCursor != null ? mCursor.getCount() : 0; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 780 | |
| 781 | mFooters.clear(); |
| 782 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 783 | final Bundle extras = mCursor != null ? mCursor.getExtras() : null; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 784 | if (extras != null) { |
| 785 | final String info = extras.getString(DocumentsContract.EXTRA_INFO); |
| 786 | if (info != null) { |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 787 | mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info)); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 788 | } |
| 789 | final String error = extras.getString(DocumentsContract.EXTRA_ERROR); |
| 790 | if (error != null) { |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 791 | mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error)); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 792 | } |
| 793 | if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) { |
| 794 | mFooters.add(new LoadingFooter()); |
| 795 | } |
| 796 | } |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 797 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 798 | if (result != null && result.exception != null) { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 799 | mFooters.add(new MessageFooter( |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 800 | 3, R.drawable.ic_dialog_alert, getString(R.string.query_error))); |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 803 | if (isEmpty()) { |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 804 | mEmptyView.setVisibility(View.VISIBLE); |
| 805 | } else { |
| 806 | mEmptyView.setVisibility(View.GONE); |
| 807 | } |
| 808 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 809 | notifyDataSetChanged(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | @Override |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 813 | public View getView(int position, View convertView, ViewGroup parent) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 814 | if (position < mCursorCount) { |
| 815 | return getDocumentView(position, convertView, parent); |
| 816 | } else { |
| 817 | position -= mCursorCount; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 818 | convertView = mFooters.get(position).getView(convertView, parent); |
| 819 | // Only the view itself is disabled; contents inside shouldn't |
| 820 | // be dimmed. |
| 821 | convertView.setEnabled(false); |
| 822 | return convertView; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
| 826 | private View getDocumentView(int position, View convertView, ViewGroup parent) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 827 | final Context context = parent.getContext(); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 828 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 829 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 830 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 831 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 832 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
| 833 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 834 | context, mThumbSize); |
| 835 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 836 | if (convertView == null) { |
| 837 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 838 | if (state.derivedMode == MODE_LIST) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 839 | convertView = inflater.inflate(R.layout.item_doc_list, parent, false); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 840 | } else if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 841 | convertView = inflater.inflate(R.layout.item_doc_grid, parent, false); |
| 842 | } else { |
| 843 | throw new IllegalStateException(); |
| 844 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 845 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 846 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 847 | final Cursor cursor = getItem(position); |
| 848 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 849 | final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY); |
| 850 | final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 851 | final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID); |
| 852 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 853 | final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME); |
| 854 | final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED); |
| 855 | final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON); |
| 856 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 857 | final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY); |
| 858 | final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 859 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 860 | final View line1 = convertView.findViewById(R.id.line1); |
| 861 | final View line2 = convertView.findViewById(R.id.line2); |
| 862 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 863 | final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime); |
| 864 | final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 865 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 866 | final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 867 | final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 868 | final TextView summary = (TextView) convertView.findViewById(android.R.id.summary); |
| 869 | final TextView date = (TextView) convertView.findViewById(R.id.date); |
| 870 | final TextView size = (TextView) convertView.findViewById(R.id.size); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 871 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 872 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 873 | if (oldTask != null) { |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 874 | oldTask.preempt(); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 875 | iconThumb.setTag(null); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 878 | iconMime.animate().cancel(); |
| 879 | iconThumb.animate().cancel(); |
| 880 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 881 | final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 882 | final boolean allowThumbnail = (state.derivedMode == MODE_GRID) |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 883 | || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 884 | final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 885 | |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 886 | final boolean enabled = isDocumentEnabled(docMimeType, docFlags); |
| 887 | final float iconAlpha = (state.derivedMode == MODE_LIST && !enabled) ? 0.5f : 1f; |
| 888 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 889 | boolean cacheHit = false; |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 890 | if (showThumbnail) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 891 | final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 892 | final Bitmap cachedResult = thumbs.get(uri); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 893 | if (cachedResult != null) { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 894 | iconThumb.setImageBitmap(cachedResult); |
| 895 | cacheHit = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 896 | } else { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 897 | iconThumb.setImageDrawable(null); |
| 898 | final ThumbnailAsyncTask task = new ThumbnailAsyncTask( |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 899 | uri, iconMime, iconThumb, mThumbSize, iconAlpha); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 900 | iconThumb.setTag(task); |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 901 | ProviderExecutor.forAuthority(docAuthority).execute(task); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 902 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | // Always throw MIME icon into place, even when a thumbnail is being |
| 906 | // loaded in background. |
| 907 | if (cacheHit) { |
| 908 | iconMime.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 909 | iconMime.setImageDrawable(null); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 910 | iconThumb.setAlpha(1f); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 911 | } else { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 912 | iconMime.setAlpha(1f); |
| 913 | iconThumb.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 914 | iconThumb.setImageDrawable(null); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 915 | if (docIcon != 0) { |
| 916 | iconMime.setImageDrawable( |
| 917 | IconUtils.loadPackageIcon(context, docAuthority, docIcon)); |
| 918 | } else { |
Jeff Sharkey | 2ceff51 | 2013-09-18 18:03:49 -0700 | [diff] [blame] | 919 | iconMime.setImageDrawable(IconUtils.loadMimeIcon( |
| 920 | context, docMimeType, docAuthority, docId, state.derivedMode)); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 921 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 924 | boolean hasLine1 = false; |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 925 | boolean hasLine2 = false; |
| 926 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 927 | final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles; |
| 928 | if (!hideTitle) { |
| 929 | title.setText(docDisplayName); |
| 930 | hasLine1 = true; |
| 931 | } |
| 932 | |
| 933 | Drawable iconDrawable = null; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 934 | if (mType == TYPE_RECENT_OPEN) { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 935 | // We've already had to enumerate roots before any results can |
| 936 | // be shown, so this will never block. |
| 937 | final RootInfo root = roots.getRootBlocking(docAuthority, docRootId); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 938 | if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 939 | iconDrawable = root.loadGridIcon(context); |
Jeff Sharkey | 93cdbc2 | 2014-07-29 17:33:36 -0700 | [diff] [blame] | 940 | } else { |
| 941 | iconDrawable = root.loadIcon(context); |
| 942 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 943 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 944 | if (summary != null) { |
| 945 | final boolean alwaysShowSummary = getResources() |
| 946 | .getBoolean(R.bool.always_show_summary); |
| 947 | if (alwaysShowSummary) { |
| 948 | summary.setText(root.getDirectoryString()); |
| 949 | summary.setVisibility(View.VISIBLE); |
| 950 | hasLine2 = true; |
| 951 | } else { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 952 | if (iconDrawable != null && roots.isIconUniqueBlocking(root)) { |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 953 | // No summary needed if icon speaks for itself |
| 954 | summary.setVisibility(View.INVISIBLE); |
| 955 | } else { |
| 956 | summary.setText(root.getDirectoryString()); |
| 957 | summary.setVisibility(View.VISIBLE); |
| 958 | summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END); |
| 959 | hasLine2 = true; |
| 960 | } |
| 961 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 962 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 963 | } else { |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 964 | // Directories showing thumbnails in grid mode get a little icon |
| 965 | // hint to remind user they're a directory. |
| 966 | if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID |
| 967 | && showThumbnail) { |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 968 | iconDrawable = IconUtils.applyTintAttr(context, R.drawable.ic_doc_folder, |
Jeff Sharkey | 34c5409 | 2014-08-08 13:08:56 -0700 | [diff] [blame] | 969 | android.R.attr.textColorPrimaryInverse); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 972 | if (summary != null) { |
| 973 | if (docSummary != null) { |
| 974 | summary.setText(docSummary); |
| 975 | summary.setVisibility(View.VISIBLE); |
| 976 | hasLine2 = true; |
| 977 | } else { |
| 978 | summary.setVisibility(View.INVISIBLE); |
| 979 | } |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 980 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 981 | } |
| 982 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 983 | if (icon1 != null) icon1.setVisibility(View.GONE); |
| 984 | if (icon2 != null) icon2.setVisibility(View.GONE); |
| 985 | |
| 986 | if (iconDrawable != null) { |
| 987 | if (hasLine1) { |
| 988 | icon1.setVisibility(View.VISIBLE); |
| 989 | icon1.setImageDrawable(iconDrawable); |
| 990 | } else { |
| 991 | icon2.setVisibility(View.VISIBLE); |
| 992 | icon2.setImageDrawable(iconDrawable); |
| 993 | } |
| 994 | } |
| 995 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 996 | if (docLastModified == -1) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 997 | date.setText(null); |
| 998 | } else { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 999 | date.setText(formatTime(context, docLastModified)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1000 | hasLine2 = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1001 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1002 | |
| 1003 | if (state.showSize) { |
| 1004 | size.setVisibility(View.VISIBLE); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1005 | if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) { |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1006 | size.setText(null); |
| 1007 | } else { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1008 | size.setText(Formatter.formatFileSize(context, docSize)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1009 | hasLine2 = true; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 1010 | } |
| 1011 | } else { |
| 1012 | size.setVisibility(View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 1013 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1014 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1015 | if (line1 != null) { |
| 1016 | line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE); |
| 1017 | } |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 1018 | if (line2 != null) { |
| 1019 | line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE); |
| 1020 | } |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 1021 | |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1022 | setEnabledRecursive(convertView, enabled); |
| 1023 | |
| 1024 | iconMime.setAlpha(iconAlpha); |
| 1025 | iconThumb.setAlpha(iconAlpha); |
| 1026 | if (icon1 != null) icon1.setAlpha(iconAlpha); |
| 1027 | if (icon2 != null) icon2.setAlpha(iconAlpha); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1028 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1029 | return convertView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1030 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 1031 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1032 | @Override |
| 1033 | public int getCount() { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1034 | return mCursorCount + mFooters.size(); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1035 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1036 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1037 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1038 | public Cursor getItem(int position) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1039 | if (position < mCursorCount) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1040 | mCursor.moveToPosition(position); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1041 | return mCursor; |
| 1042 | } else { |
| 1043 | return null; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1044 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1045 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 1046 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1047 | @Override |
| 1048 | public long getItemId(int position) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 1049 | return position; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1050 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1051 | |
| 1052 | @Override |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1053 | public int getViewTypeCount() { |
| 1054 | return 4; |
| 1055 | } |
| 1056 | |
| 1057 | @Override |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1058 | public int getItemViewType(int position) { |
| 1059 | if (position < mCursorCount) { |
| 1060 | return 0; |
| 1061 | } else { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1062 | position -= mCursorCount; |
| 1063 | return mFooters.get(position).getItemViewType(); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 1064 | } |
| 1065 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1066 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1067 | |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 1068 | private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> |
| 1069 | implements Preemptable { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1070 | private final Uri mUri; |
| 1071 | private final ImageView mIconMime; |
| 1072 | private final ImageView mIconThumb; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 1073 | private final Point mThumbSize; |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1074 | private final float mTargetAlpha; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1075 | private final CancellationSignal mSignal; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1076 | |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1077 | public ThumbnailAsyncTask(Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize, |
| 1078 | float targetAlpha) { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1079 | mUri = uri; |
| 1080 | mIconMime = iconMime; |
| 1081 | mIconThumb = iconThumb; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 1082 | mThumbSize = thumbSize; |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1083 | mTargetAlpha = targetAlpha; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1084 | mSignal = new CancellationSignal(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Jeff Sharkey | 753a3ae | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 1087 | @Override |
| 1088 | public void preempt() { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1089 | cancel(false); |
| 1090 | mSignal.cancel(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | @Override |
| 1094 | protected Bitmap doInBackground(Uri... params) { |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 1095 | if (isCancelled()) return null; |
| 1096 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1097 | final Context context = mIconThumb.getContext(); |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1098 | final ContentResolver resolver = context.getContentResolver(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1099 | |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1100 | ContentProviderClient client = null; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1101 | Bitmap result = null; |
| 1102 | try { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1103 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 1104 | resolver, mUri.getAuthority()); |
| 1105 | result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1106 | if (result != null) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 1107 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 1108 | context, mThumbSize); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1109 | thumbs.put(mUri, result); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1110 | } |
| 1111 | } catch (Exception e) { |
Jeff Sharkey | e39a89b | 2013-10-29 11:56:37 -0700 | [diff] [blame] | 1112 | if (!(e instanceof OperationCanceledException)) { |
| 1113 | Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e); |
| 1114 | } |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1115 | } finally { |
| 1116 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1117 | } |
| 1118 | return result; |
| 1119 | } |
| 1120 | |
| 1121 | @Override |
| 1122 | protected void onPostExecute(Bitmap result) { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1123 | if (mIconThumb.getTag() == this && result != null) { |
| 1124 | mIconThumb.setTag(null); |
| 1125 | mIconThumb.setImageBitmap(result); |
| 1126 | |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1127 | mIconMime.setAlpha(mTargetAlpha); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1128 | mIconMime.animate().alpha(0f).start(); |
| 1129 | mIconThumb.setAlpha(0f); |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 1130 | mIconThumb.animate().alpha(mTargetAlpha).start(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | private static String formatTime(Context context, long when) { |
| 1136 | // TODO: DateUtils should make this easier |
| 1137 | Time then = new Time(); |
| 1138 | then.set(when); |
| 1139 | Time now = new Time(); |
| 1140 | now.setToNow(); |
| 1141 | |
| 1142 | int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT |
| 1143 | | DateUtils.FORMAT_ABBREV_ALL; |
| 1144 | |
| 1145 | if (then.year != now.year) { |
| 1146 | flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE; |
| 1147 | } else if (then.yearDay != now.yearDay) { |
| 1148 | flags |= DateUtils.FORMAT_SHOW_DATE; |
| 1149 | } else { |
| 1150 | flags |= DateUtils.FORMAT_SHOW_TIME; |
| 1151 | } |
| 1152 | |
| 1153 | return DateUtils.formatDateTime(context, when, flags); |
| 1154 | } |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 1155 | |
| 1156 | private String findCommonMimeType(List<String> mimeTypes) { |
| 1157 | String[] commonType = mimeTypes.get(0).split("/"); |
| 1158 | if (commonType.length != 2) { |
| 1159 | return "*/*"; |
| 1160 | } |
| 1161 | |
| 1162 | for (int i = 1; i < mimeTypes.size(); i++) { |
| 1163 | String[] type = mimeTypes.get(i).split("/"); |
| 1164 | if (type.length != 2) continue; |
| 1165 | |
| 1166 | if (!commonType[1].equals(type[1])) { |
| 1167 | commonType[1] = "*"; |
| 1168 | } |
| 1169 | |
| 1170 | if (!commonType[0].equals(type[0])) { |
| 1171 | commonType[0] = "*"; |
| 1172 | commonType[1] = "*"; |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | return commonType[0] + "/" + commonType[1]; |
| 1178 | } |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1179 | |
| 1180 | private void setEnabledRecursive(View v, boolean enabled) { |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1181 | if (v == null) return; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1182 | if (v.isEnabled() == enabled) return; |
| 1183 | v.setEnabled(enabled); |
| 1184 | |
| 1185 | if (v instanceof ViewGroup) { |
| 1186 | final ViewGroup vg = (ViewGroup) v; |
| 1187 | for (int i = vg.getChildCount() - 1; i >= 0; i--) { |
| 1188 | setEnabledRecursive(vg.getChildAt(i), enabled); |
| 1189 | } |
| 1190 | } |
| 1191 | } |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1192 | |
| 1193 | private boolean isDocumentEnabled(String docMimeType, int docFlags) { |
| 1194 | final State state = getDisplayState(DirectoryFragment.this); |
| 1195 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1196 | // Directories are always enabled |
| 1197 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 1198 | return true; |
| 1199 | } |
| 1200 | |
Jeff Sharkey | 783ebc2 | 2013-09-26 19:42:52 -0700 | [diff] [blame] | 1201 | // Read-only files are disabled when creating |
| 1202 | if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) { |
| 1203 | return false; |
| 1204 | } |
| 1205 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1206 | return MimePredicate.mimeMatches(state.acceptMimes, docMimeType); |
| 1207 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1208 | } |