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