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 | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 294 | // Restore any previous instance state |
| 295 | final SparseArray<Parcelable> container = state.dirState.remove(mStateKey); |
| 296 | if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) { |
| 297 | getView().restoreHierarchyState(container); |
| 298 | } else if (mLastSortOrder != state.derivedSortOrder) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 299 | mListView.smoothScrollToPosition(0); |
| 300 | mGridView.smoothScrollToPosition(0); |
| 301 | } |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 302 | |
| 303 | mLastSortOrder = state.derivedSortOrder; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 307 | public void onLoaderReset(Loader<DirectoryResult> loader) { |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame^] | 308 | mAdapter.swapResult(null); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 309 | } |
| 310 | }; |
| 311 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 312 | // Kick off loader at least once |
| 313 | getLoaderManager().restartLoader(mLoaderId, null, mCallbacks); |
| 314 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 315 | updateDisplayState(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 318 | @Override |
Jeff Sharkey | c8ae7a5 | 2013-09-18 16:26:49 -0700 | [diff] [blame] | 319 | public void onStop() { |
| 320 | super.onStop(); |
| 321 | |
| 322 | // Remember last scroll location |
| 323 | final SparseArray<Parcelable> container = new SparseArray<Parcelable>(); |
| 324 | getView().saveHierarchyState(container); |
| 325 | final State state = getDisplayState(this); |
| 326 | state.dirState.put(mStateKey, container); |
| 327 | } |
| 328 | |
| 329 | @Override |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 330 | public void onResume() { |
| 331 | super.onResume(); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 332 | updateDisplayState(); |
| 333 | } |
| 334 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 335 | public void onUserSortOrderChanged() { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 336 | // Sort order change always triggers reload; we'll trigger state change |
| 337 | // on the flip side. |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 338 | getLoaderManager().restartLoader(mLoaderId, null, mCallbacks); |
| 339 | } |
| 340 | |
| 341 | public void onUserModeChanged() { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 342 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 343 | final State state = getDisplayState(this); |
| 344 | |
| 345 | final RootInfo root = getArguments().getParcelable(EXTRA_ROOT); |
| 346 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 347 | |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 348 | if (root != null && doc != null) { |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 349 | final Uri stateUri = RecentsProvider.buildState( |
| 350 | root.authority, root.rootId, doc.documentId); |
| 351 | final ContentValues values = new ContentValues(); |
| 352 | values.put(StateColumns.MODE, state.userMode); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 353 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 354 | new AsyncTask<Void, Void, Void>() { |
| 355 | @Override |
| 356 | protected Void doInBackground(Void... params) { |
| 357 | resolver.insert(stateUri, values); |
| 358 | return null; |
| 359 | } |
| 360 | }.execute(); |
| 361 | } |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 362 | |
| 363 | // Mode change is just visual change; no need to kick loader, and |
| 364 | // deliver change event immediately. |
| 365 | state.derivedMode = state.userMode; |
| 366 | ((DocumentsActivity) getActivity()).onStateChanged(); |
| 367 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 368 | updateDisplayState(); |
| 369 | } |
| 370 | |
| 371 | private void updateDisplayState() { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 372 | final State state = getDisplayState(this); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 373 | |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 374 | if (mLastMode == state.derivedMode && mLastShowSize == state.showSize) return; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 375 | mLastMode = state.derivedMode; |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 376 | mLastShowSize = state.showSize; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 377 | |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 378 | mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE); |
| 379 | mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 380 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 381 | final int choiceMode; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 382 | if (state.allowMultiple) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 383 | choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL; |
| 384 | } else { |
| 385 | choiceMode = ListView.CHOICE_MODE_NONE; |
| 386 | } |
| 387 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 388 | final int thumbSize; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 389 | if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 390 | thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 391 | mListView.setAdapter(null); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 392 | mListView.setChoiceMode(ListView.CHOICE_MODE_NONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 393 | mGridView.setAdapter(mAdapter); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 394 | mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width)); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 395 | mGridView.setNumColumns(GridView.AUTO_FIT); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 396 | mGridView.setChoiceMode(choiceMode); |
| 397 | mCurrentView = mGridView; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 398 | } else if (state.derivedMode == MODE_LIST) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 399 | thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 400 | mGridView.setAdapter(null); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 401 | mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 402 | mListView.setAdapter(mAdapter); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 403 | mListView.setChoiceMode(choiceMode); |
| 404 | mCurrentView = mListView; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 405 | } else { |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 406 | throw new IllegalStateException("Unknown state " + state.derivedMode); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 407 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 408 | |
| 409 | mThumbSize = new Point(thumbSize, thumbSize); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | private OnItemClickListener mItemListener = new OnItemClickListener() { |
| 413 | @Override |
| 414 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 415 | final Cursor cursor = mAdapter.getItem(position); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 416 | if (cursor != null) { |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 417 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 418 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 419 | if (isDocumentEnabled(docMimeType, docFlags)) { |
| 420 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 421 | ((DocumentsActivity) getActivity()).onDocumentPicked(doc); |
| 422 | } |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 423 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 424 | } |
| 425 | }; |
| 426 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 427 | private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() { |
| 428 | @Override |
| 429 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 430 | mode.getMenuInflater().inflate(R.menu.mode_directory, menu); |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 436 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 437 | |
| 438 | final MenuItem open = menu.findItem(R.id.menu_open); |
| 439 | final MenuItem share = menu.findItem(R.id.menu_share); |
| 440 | final MenuItem delete = menu.findItem(R.id.menu_delete); |
| 441 | |
| 442 | final boolean manageMode = state.action == ACTION_MANAGE; |
| 443 | open.setVisible(!manageMode); |
| 444 | share.setVisible(manageMode); |
| 445 | delete.setVisible(manageMode); |
| 446 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 447 | return true; |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 452 | final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions(); |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 453 | final ArrayList<DocumentInfo> docs = Lists.newArrayList(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 454 | final int size = checked.size(); |
| 455 | for (int i = 0; i < size; i++) { |
| 456 | if (checked.valueAt(i)) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 457 | final Cursor cursor = mAdapter.getItem(checked.keyAt(i)); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 458 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 459 | docs.add(doc); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 460 | } |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 461 | } |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 462 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 463 | final int id = item.getItemId(); |
| 464 | if (id == R.id.menu_open) { |
| 465 | DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 466 | mode.finish(); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 467 | return true; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 468 | |
| 469 | } else if (id == R.id.menu_share) { |
| 470 | onShareDocuments(docs); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 471 | mode.finish(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 472 | return true; |
| 473 | |
| 474 | } else if (id == R.id.menu_delete) { |
| 475 | onDeleteDocuments(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 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 479 | } else { |
| 480 | return false; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public void onDestroyActionMode(ActionMode mode) { |
| 486 | // ignored |
| 487 | } |
| 488 | |
| 489 | @Override |
| 490 | public void onItemCheckedStateChanged( |
| 491 | ActionMode mode, int position, long id, boolean checked) { |
| 492 | if (checked) { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 493 | // Directories and footer items cannot be checked |
| 494 | boolean valid = false; |
| 495 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 496 | final Cursor cursor = mAdapter.getItem(position); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 497 | if (cursor != null) { |
| 498 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 499 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 500 | if (!Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 501 | valid = isDocumentEnabled(docMimeType, docFlags); |
| 502 | } |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (!valid) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 506 | mCurrentView.setItemChecked(position, false); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | mode.setTitle(getResources() |
| 511 | .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount())); |
| 512 | } |
| 513 | }; |
| 514 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 515 | private RecyclerListener mRecycleListener = new RecyclerListener() { |
| 516 | @Override |
| 517 | public void onMovedToScrapHeap(View view) { |
| 518 | final ImageView iconThumb = (ImageView) view.findViewById(R.id.icon_thumb); |
| 519 | if (iconThumb != null) { |
| 520 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag(); |
| 521 | if (oldTask != null) { |
| 522 | oldTask.reallyCancel(); |
| 523 | iconThumb.setTag(null); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | }; |
| 528 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 529 | private void onShareDocuments(List<DocumentInfo> docs) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 530 | Intent intent; |
| 531 | if (docs.size() == 1) { |
| 532 | final DocumentInfo doc = docs.get(0); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 533 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 534 | intent = new Intent(Intent.ACTION_SEND); |
| 535 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 536 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 537 | intent.setType(doc.mimeType); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 538 | intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 539 | |
| 540 | } else if (docs.size() > 1) { |
| 541 | intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 542 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 543 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 544 | |
| 545 | final ArrayList<String> mimeTypes = Lists.newArrayList(); |
| 546 | final ArrayList<Uri> uris = Lists.newArrayList(); |
| 547 | for (DocumentInfo doc : docs) { |
| 548 | mimeTypes.add(doc.mimeType); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 549 | uris.add(doc.derivedUri); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | intent.setType(findCommonMimeType(mimeTypes)); |
| 553 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 554 | |
| 555 | } else { |
| 556 | return; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 559 | intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via)); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 560 | startActivity(intent); |
| 561 | } |
| 562 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 563 | private void onDeleteDocuments(List<DocumentInfo> docs) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 564 | final Context context = getActivity(); |
| 565 | final ContentResolver resolver = context.getContentResolver(); |
| 566 | |
| 567 | boolean hadTrouble = false; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 568 | for (DocumentInfo doc : docs) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 569 | if (!doc.isDeleteSupported()) { |
| 570 | Log.w(TAG, "Skipping " + doc); |
| 571 | hadTrouble = true; |
| 572 | continue; |
| 573 | } |
| 574 | |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 575 | ContentProviderClient client = null; |
| 576 | try { |
| 577 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 578 | resolver, doc.derivedUri.getAuthority()); |
| 579 | DocumentsContract.deleteDocument(client, doc.derivedUri); |
| 580 | } catch (Exception e) { |
Jeff Sharkey | 4c5fe52 | 2013-09-03 14:17:06 -0700 | [diff] [blame] | 581 | Log.w(TAG, "Failed to delete " + doc); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 582 | hadTrouble = true; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 583 | } finally { |
| 584 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
| 588 | if (hadTrouble) { |
| 589 | Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show(); |
| 590 | } |
| 591 | } |
| 592 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 593 | private static State getDisplayState(Fragment fragment) { |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 594 | return ((DocumentsActivity) fragment.getActivity()).getDisplayState(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 597 | private static abstract class Footer { |
| 598 | private final int mItemViewType; |
| 599 | |
| 600 | public Footer(int itemViewType) { |
| 601 | mItemViewType = itemViewType; |
| 602 | } |
| 603 | |
| 604 | public abstract View getView(View convertView, ViewGroup parent); |
| 605 | |
| 606 | public int getItemViewType() { |
| 607 | return mItemViewType; |
| 608 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 611 | private class LoadingFooter extends Footer { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 612 | public LoadingFooter() { |
| 613 | super(1); |
| 614 | } |
| 615 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 616 | @Override |
| 617 | public View getView(View convertView, ViewGroup parent) { |
| 618 | final Context context = parent.getContext(); |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 619 | final State state = getDisplayState(DirectoryFragment.this); |
| 620 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 621 | if (convertView == null) { |
| 622 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 623 | if (state.derivedMode == MODE_LIST) { |
| 624 | convertView = inflater.inflate(R.layout.item_loading_list, parent, false); |
| 625 | } else if (state.derivedMode == MODE_GRID) { |
| 626 | convertView = inflater.inflate(R.layout.item_loading_grid, parent, false); |
| 627 | } else { |
| 628 | throw new IllegalStateException(); |
| 629 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 630 | } |
Jeff Sharkey | 5e1884d | 2013-09-10 17:56:39 -0700 | [diff] [blame] | 631 | |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 632 | return convertView; |
| 633 | } |
| 634 | } |
| 635 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 636 | private class MessageFooter extends Footer { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 637 | private final int mIcon; |
| 638 | private final String mMessage; |
| 639 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 640 | public MessageFooter(int itemViewType, int icon, String message) { |
| 641 | super(itemViewType); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 642 | mIcon = icon; |
| 643 | mMessage = message; |
| 644 | } |
| 645 | |
| 646 | @Override |
| 647 | public View getView(View convertView, ViewGroup parent) { |
| 648 | final Context context = parent.getContext(); |
| 649 | final State state = getDisplayState(DirectoryFragment.this); |
| 650 | |
| 651 | if (convertView == null) { |
| 652 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 653 | if (state.derivedMode == MODE_LIST) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 654 | convertView = inflater.inflate(R.layout.item_message_list, parent, false); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 655 | } else if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 656 | convertView = inflater.inflate(R.layout.item_message_grid, parent, false); |
| 657 | } else { |
| 658 | throw new IllegalStateException(); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
| 663 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 664 | icon.setImageResource(mIcon); |
| 665 | title.setText(mMessage); |
| 666 | return convertView; |
| 667 | } |
| 668 | } |
| 669 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 670 | private class DocumentsAdapter extends BaseAdapter { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 671 | private Cursor mCursor; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 672 | private int mCursorCount; |
| 673 | |
| 674 | private List<Footer> mFooters = Lists.newArrayList(); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 675 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame^] | 676 | public void swapResult(DirectoryResult result) { |
| 677 | mCursor = result != null ? result.cursor : null; |
| 678 | mCursorCount = mCursor != null ? mCursor.getCount() : 0; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 679 | |
| 680 | mFooters.clear(); |
| 681 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame^] | 682 | final Bundle extras = mCursor != null ? mCursor.getExtras() : null; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 683 | if (extras != null) { |
| 684 | final String info = extras.getString(DocumentsContract.EXTRA_INFO); |
| 685 | if (info != null) { |
Jeff Sharkey | db884f5 | 2013-09-21 15:11:19 -0700 | [diff] [blame] | 686 | mFooters.add(new MessageFooter(2, R.drawable.ic_dialog_info, info)); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 687 | } |
| 688 | final String error = extras.getString(DocumentsContract.EXTRA_ERROR); |
| 689 | if (error != null) { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 690 | mFooters.add(new MessageFooter(3, R.drawable.ic_dialog_alert, error)); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 691 | } |
| 692 | if (extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) { |
| 693 | mFooters.add(new LoadingFooter()); |
| 694 | } |
| 695 | } |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 696 | |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame^] | 697 | if (result != null && result.exception != null) { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 698 | mFooters.add(new MessageFooter( |
| 699 | 3, R.drawable.ic_dialog_alert, getString(R.string.query_error))); |
| 700 | } |
| 701 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 702 | if (isEmpty()) { |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 703 | mEmptyView.setVisibility(View.VISIBLE); |
| 704 | } else { |
| 705 | mEmptyView.setVisibility(View.GONE); |
| 706 | } |
| 707 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 708 | notifyDataSetChanged(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | @Override |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 712 | public View getView(int position, View convertView, ViewGroup parent) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 713 | if (position < mCursorCount) { |
| 714 | return getDocumentView(position, convertView, parent); |
| 715 | } else { |
| 716 | position -= mCursorCount; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 717 | convertView = mFooters.get(position).getView(convertView, parent); |
| 718 | // Only the view itself is disabled; contents inside shouldn't |
| 719 | // be dimmed. |
| 720 | convertView.setEnabled(false); |
| 721 | return convertView; |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
| 725 | private View getDocumentView(int position, View convertView, ViewGroup parent) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 726 | final Context context = parent.getContext(); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 727 | final State state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 728 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 729 | final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC); |
| 730 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 731 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
| 732 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 733 | context, mThumbSize); |
| 734 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 735 | if (convertView == null) { |
| 736 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 737 | if (state.derivedMode == MODE_LIST) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 738 | convertView = inflater.inflate(R.layout.item_doc_list, parent, false); |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 739 | } else if (state.derivedMode == MODE_GRID) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 740 | convertView = inflater.inflate(R.layout.item_doc_grid, parent, false); |
Jeff Sharkey | db884f5 | 2013-09-21 15:11:19 -0700 | [diff] [blame] | 741 | |
| 742 | // Apply padding to grid items |
| 743 | final FrameLayout grid = (FrameLayout) convertView; |
| 744 | final int gridPadding = getResources() |
| 745 | .getDimensionPixelSize(R.dimen.grid_padding); |
Jeff Sharkey | 38ec252 | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 746 | |
| 747 | // Tricksy hobbitses! We need to fully clear the drawable so |
| 748 | // the view doesn't clobber the new InsetDrawable callback |
| 749 | // when setting back later. |
| 750 | final Drawable fg = grid.getForeground(); |
| 751 | final Drawable bg = grid.getBackground(); |
| 752 | grid.setForeground(null); |
| 753 | grid.setBackground(null); |
| 754 | grid.setForeground(new InsetDrawable(fg, gridPadding)); |
| 755 | grid.setBackground(new InsetDrawable(bg, gridPadding)); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 756 | } else { |
| 757 | throw new IllegalStateException(); |
| 758 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 759 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 760 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 761 | final Cursor cursor = getItem(position); |
| 762 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 763 | final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY); |
| 764 | final String docRootId = getCursorString(cursor, RootCursorWrapper.COLUMN_ROOT_ID); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 765 | final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID); |
| 766 | final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 767 | final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME); |
| 768 | final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED); |
| 769 | final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON); |
| 770 | final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); |
| 771 | final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY); |
| 772 | final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 773 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 774 | final View line1 = convertView.findViewById(R.id.line1); |
| 775 | final View line2 = convertView.findViewById(R.id.line2); |
| 776 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 777 | final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime); |
| 778 | final ImageView iconThumb = (ImageView) convertView.findViewById(R.id.icon_thumb); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 779 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 780 | final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 781 | final ImageView icon2 = (ImageView) convertView.findViewById(android.R.id.icon2); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 782 | final TextView summary = (TextView) convertView.findViewById(android.R.id.summary); |
| 783 | final TextView date = (TextView) convertView.findViewById(R.id.date); |
| 784 | final TextView size = (TextView) convertView.findViewById(R.id.size); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 785 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 786 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 787 | if (oldTask != null) { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 788 | oldTask.reallyCancel(); |
| 789 | iconThumb.setTag(null); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 792 | iconMime.animate().cancel(); |
| 793 | iconThumb.animate().cancel(); |
| 794 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 795 | final boolean supportsThumbnail = (docFlags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 796 | final boolean allowThumbnail = (state.derivedMode == MODE_GRID) |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 797 | || MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, docMimeType); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame^] | 798 | final boolean showThumbnail = supportsThumbnail && allowThumbnail && !mSvelteRecents; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 799 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 800 | boolean cacheHit = false; |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 801 | if (showThumbnail) { |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 802 | final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 803 | final Bitmap cachedResult = thumbs.get(uri); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 804 | if (cachedResult != null) { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 805 | iconThumb.setImageBitmap(cachedResult); |
| 806 | cacheHit = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 807 | } else { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 808 | iconThumb.setImageDrawable(null); |
| 809 | final ThumbnailAsyncTask task = new ThumbnailAsyncTask( |
| 810 | uri, iconMime, iconThumb, mThumbSize); |
| 811 | iconThumb.setTag(task); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame^] | 812 | task.executeOnExecutor(ProviderExecutor.forAuthority(docAuthority)); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 813 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | // Always throw MIME icon into place, even when a thumbnail is being |
| 817 | // loaded in background. |
| 818 | if (cacheHit) { |
| 819 | iconMime.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 820 | iconMime.setImageDrawable(null); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 821 | iconThumb.setAlpha(1f); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 822 | } else { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 823 | iconMime.setAlpha(1f); |
| 824 | iconThumb.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 825 | iconThumb.setImageDrawable(null); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 826 | if (docIcon != 0) { |
| 827 | iconMime.setImageDrawable( |
| 828 | IconUtils.loadPackageIcon(context, docAuthority, docIcon)); |
| 829 | } else { |
Jeff Sharkey | 2ceff51 | 2013-09-18 18:03:49 -0700 | [diff] [blame] | 830 | iconMime.setImageDrawable(IconUtils.loadMimeIcon( |
| 831 | context, docMimeType, docAuthority, docId, state.derivedMode)); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 832 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 835 | boolean hasLine1 = false; |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 836 | boolean hasLine2 = false; |
| 837 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 838 | final boolean hideTitle = (state.derivedMode == MODE_GRID) && mHideGridTitles; |
| 839 | if (!hideTitle) { |
| 840 | title.setText(docDisplayName); |
| 841 | hasLine1 = true; |
| 842 | } |
| 843 | |
| 844 | Drawable iconDrawable = null; |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 845 | if (mType == TYPE_RECENT_OPEN) { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 846 | // We've already had to enumerate roots before any results can |
| 847 | // be shown, so this will never block. |
| 848 | final RootInfo root = roots.getRootBlocking(docAuthority, docRootId); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 849 | iconDrawable = root.loadIcon(context); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 850 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 851 | if (summary != null) { |
| 852 | final boolean alwaysShowSummary = getResources() |
| 853 | .getBoolean(R.bool.always_show_summary); |
| 854 | if (alwaysShowSummary) { |
| 855 | summary.setText(root.getDirectoryString()); |
| 856 | summary.setVisibility(View.VISIBLE); |
| 857 | hasLine2 = true; |
| 858 | } else { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 859 | if (iconDrawable != null && roots.isIconUniqueBlocking(root)) { |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 860 | // No summary needed if icon speaks for itself |
| 861 | summary.setVisibility(View.INVISIBLE); |
| 862 | } else { |
| 863 | summary.setText(root.getDirectoryString()); |
| 864 | summary.setVisibility(View.VISIBLE); |
| 865 | summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END); |
| 866 | hasLine2 = true; |
| 867 | } |
| 868 | } |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 869 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 870 | } else { |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 871 | // Directories showing thumbnails in grid mode get a little icon |
| 872 | // hint to remind user they're a directory. |
| 873 | if (Document.MIME_TYPE_DIR.equals(docMimeType) && state.derivedMode == MODE_GRID |
| 874 | && showThumbnail) { |
| 875 | iconDrawable = context.getResources().getDrawable(R.drawable.ic_root_folder); |
| 876 | } |
| 877 | |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 878 | if (summary != null) { |
| 879 | if (docSummary != null) { |
| 880 | summary.setText(docSummary); |
| 881 | summary.setVisibility(View.VISIBLE); |
| 882 | hasLine2 = true; |
| 883 | } else { |
| 884 | summary.setVisibility(View.INVISIBLE); |
| 885 | } |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 886 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 887 | } |
| 888 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 889 | if (icon1 != null) icon1.setVisibility(View.GONE); |
| 890 | if (icon2 != null) icon2.setVisibility(View.GONE); |
| 891 | |
| 892 | if (iconDrawable != null) { |
| 893 | if (hasLine1) { |
| 894 | icon1.setVisibility(View.VISIBLE); |
| 895 | icon1.setImageDrawable(iconDrawable); |
| 896 | } else { |
| 897 | icon2.setVisibility(View.VISIBLE); |
| 898 | icon2.setImageDrawable(iconDrawable); |
| 899 | } |
| 900 | } |
| 901 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 902 | if (docLastModified == -1) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 903 | date.setText(null); |
| 904 | } else { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 905 | date.setText(formatTime(context, docLastModified)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 906 | hasLine2 = true; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 907 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 908 | |
| 909 | if (state.showSize) { |
| 910 | size.setVisibility(View.VISIBLE); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 911 | if (Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) { |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 912 | size.setText(null); |
| 913 | } else { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 914 | size.setText(Formatter.formatFileSize(context, docSize)); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 915 | hasLine2 = true; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 916 | } |
| 917 | } else { |
| 918 | size.setVisibility(View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 919 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 920 | |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 921 | if (line1 != null) { |
| 922 | line1.setVisibility(hasLine1 ? View.VISIBLE : View.GONE); |
| 923 | } |
Jeff Sharkey | 7d58fc6 | 2013-09-12 16:25:02 -0700 | [diff] [blame] | 924 | if (line2 != null) { |
| 925 | line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE); |
| 926 | } |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 927 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 928 | final boolean enabled = isDocumentEnabled(docMimeType, docFlags); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 929 | if (enabled) { |
| 930 | setEnabledRecursive(convertView, true); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 931 | iconMime.setAlpha(1f); |
| 932 | iconThumb.setAlpha(1f); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 933 | if (icon1 != null) icon1.setAlpha(1f); |
| 934 | if (icon2 != null) icon2.setAlpha(1f); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 935 | } else { |
| 936 | setEnabledRecursive(convertView, false); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 937 | iconMime.setAlpha(0.5f); |
| 938 | iconThumb.setAlpha(0.5f); |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 939 | if (icon1 != null) icon1.setAlpha(0.5f); |
| 940 | if (icon2 != null) icon2.setAlpha(0.5f); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 943 | return convertView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 944 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 945 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 946 | @Override |
| 947 | public int getCount() { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 948 | return mCursorCount + mFooters.size(); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 949 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 950 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 951 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 952 | public Cursor getItem(int position) { |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 953 | if (position < mCursorCount) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 954 | mCursor.moveToPosition(position); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 955 | return mCursor; |
| 956 | } else { |
| 957 | return null; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 958 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 959 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 960 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 961 | @Override |
| 962 | public long getItemId(int position) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 963 | return position; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 964 | } |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 965 | |
| 966 | @Override |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 967 | public int getViewTypeCount() { |
| 968 | return 4; |
| 969 | } |
| 970 | |
| 971 | @Override |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 972 | public int getItemViewType(int position) { |
| 973 | if (position < mCursorCount) { |
| 974 | return 0; |
| 975 | } else { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 976 | position -= mCursorCount; |
| 977 | return mFooters.get(position).getItemViewType(); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 978 | } |
| 979 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 980 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 981 | |
| 982 | private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 983 | private final Uri mUri; |
| 984 | private final ImageView mIconMime; |
| 985 | private final ImageView mIconThumb; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 986 | private final Point mThumbSize; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 987 | private final CancellationSignal mSignal; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 988 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 989 | public ThumbnailAsyncTask( |
| 990 | Uri uri, ImageView iconMime, ImageView iconThumb, Point thumbSize) { |
| 991 | mUri = uri; |
| 992 | mIconMime = iconMime; |
| 993 | mIconThumb = iconThumb; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 994 | mThumbSize = thumbSize; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 995 | mSignal = new CancellationSignal(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 998 | public void reallyCancel() { |
| 999 | cancel(false); |
| 1000 | mSignal.cancel(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | @Override |
| 1004 | protected Bitmap doInBackground(Uri... params) { |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame^] | 1005 | if (isCancelled()) return null; |
| 1006 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1007 | final Context context = mIconThumb.getContext(); |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1008 | final ContentResolver resolver = context.getContentResolver(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1009 | |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1010 | ContentProviderClient client = null; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1011 | Bitmap result = null; |
| 1012 | try { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1013 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 1014 | resolver, mUri.getAuthority()); |
| 1015 | result = DocumentsContract.getDocumentThumbnail(client, mUri, mThumbSize, mSignal); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1016 | if (result != null) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 1017 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 1018 | context, mThumbSize); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1019 | thumbs.put(mUri, result); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1020 | } |
| 1021 | } catch (Exception e) { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1022 | Log.w(TAG, "Failed to load thumbnail for " + mUri + ": " + e); |
| 1023 | } finally { |
| 1024 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1025 | } |
| 1026 | return result; |
| 1027 | } |
| 1028 | |
| 1029 | @Override |
| 1030 | protected void onPostExecute(Bitmap result) { |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1031 | if (mIconThumb.getTag() == this && result != null) { |
| 1032 | mIconThumb.setTag(null); |
| 1033 | mIconThumb.setImageBitmap(result); |
| 1034 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 1035 | final float targetAlpha = mIconMime.isEnabled() ? 1f : 0.5f; |
| 1036 | mIconMime.setAlpha(targetAlpha); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 1037 | mIconMime.animate().alpha(0f).start(); |
| 1038 | mIconThumb.setAlpha(0f); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 1039 | mIconThumb.animate().alpha(targetAlpha).start(); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | private static String formatTime(Context context, long when) { |
| 1045 | // TODO: DateUtils should make this easier |
| 1046 | Time then = new Time(); |
| 1047 | then.set(when); |
| 1048 | Time now = new Time(); |
| 1049 | now.setToNow(); |
| 1050 | |
| 1051 | int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT |
| 1052 | | DateUtils.FORMAT_ABBREV_ALL; |
| 1053 | |
| 1054 | if (then.year != now.year) { |
| 1055 | flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE; |
| 1056 | } else if (then.yearDay != now.yearDay) { |
| 1057 | flags |= DateUtils.FORMAT_SHOW_DATE; |
| 1058 | } else { |
| 1059 | flags |= DateUtils.FORMAT_SHOW_TIME; |
| 1060 | } |
| 1061 | |
| 1062 | return DateUtils.formatDateTime(context, when, flags); |
| 1063 | } |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 1064 | |
| 1065 | private String findCommonMimeType(List<String> mimeTypes) { |
| 1066 | String[] commonType = mimeTypes.get(0).split("/"); |
| 1067 | if (commonType.length != 2) { |
| 1068 | return "*/*"; |
| 1069 | } |
| 1070 | |
| 1071 | for (int i = 1; i < mimeTypes.size(); i++) { |
| 1072 | String[] type = mimeTypes.get(i).split("/"); |
| 1073 | if (type.length != 2) continue; |
| 1074 | |
| 1075 | if (!commonType[1].equals(type[1])) { |
| 1076 | commonType[1] = "*"; |
| 1077 | } |
| 1078 | |
| 1079 | if (!commonType[0].equals(type[0])) { |
| 1080 | commonType[0] = "*"; |
| 1081 | commonType[1] = "*"; |
| 1082 | break; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | return commonType[0] + "/" + commonType[1]; |
| 1087 | } |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1088 | |
| 1089 | private void setEnabledRecursive(View v, boolean enabled) { |
Jeff Sharkey | 9656a53 | 2013-09-13 13:42:19 -0700 | [diff] [blame] | 1090 | if (v == null) return; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 1091 | if (v.isEnabled() == enabled) return; |
| 1092 | v.setEnabled(enabled); |
| 1093 | |
| 1094 | if (v instanceof ViewGroup) { |
| 1095 | final ViewGroup vg = (ViewGroup) v; |
| 1096 | for (int i = vg.getChildCount() - 1; i >= 0; i--) { |
| 1097 | setEnabledRecursive(vg.getChildAt(i), enabled); |
| 1098 | } |
| 1099 | } |
| 1100 | } |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1101 | |
| 1102 | private boolean isDocumentEnabled(String docMimeType, int docFlags) { |
| 1103 | final State state = getDisplayState(DirectoryFragment.this); |
| 1104 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1105 | // Directories are always enabled |
| 1106 | if (Document.MIME_TYPE_DIR.equals(docMimeType)) { |
| 1107 | return true; |
| 1108 | } |
| 1109 | |
Jeff Sharkey | 783ebc2 | 2013-09-26 19:42:52 -0700 | [diff] [blame] | 1110 | // Read-only files are disabled when creating |
| 1111 | if (state.action == ACTION_CREATE && (docFlags & Document.FLAG_SUPPORTS_WRITE) == 0) { |
| 1112 | return false; |
| 1113 | } |
| 1114 | |
Jeff Sharkey | 7cf4903 | 2013-09-26 10:54:16 -0700 | [diff] [blame] | 1115 | return MimePredicate.mimeMatches(state.acceptMimes, docMimeType); |
| 1116 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1117 | } |