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 | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 20 | import static com.android.documentsui.DocumentsActivity.DisplayState.ACTION_MANAGE; |
| 21 | import static com.android.documentsui.DocumentsActivity.DisplayState.MODE_GRID; |
| 22 | import static com.android.documentsui.DocumentsActivity.DisplayState.MODE_LIST; |
| 23 | import static com.android.documentsui.DocumentsActivity.DisplayState.SORT_ORDER_DATE; |
| 24 | import static com.android.documentsui.DocumentsActivity.DisplayState.SORT_ORDER_NAME; |
| 25 | import static com.android.documentsui.DocumentsActivity.DisplayState.SORT_ORDER_SIZE; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 26 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 27 | import android.app.Fragment; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 28 | import android.app.FragmentManager; |
| 29 | import android.app.FragmentTransaction; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 30 | import android.app.LoaderManager.LoaderCallbacks; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 31 | import android.content.ContentResolver; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 32 | import android.content.Context; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 33 | import android.content.Intent; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 34 | import android.content.Loader; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 35 | import android.database.Cursor; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 36 | import android.graphics.Bitmap; |
| 37 | import android.graphics.Point; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 38 | import android.net.Uri; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 39 | import android.os.AsyncTask; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 40 | import android.os.Bundle; |
| 41 | import android.provider.DocumentsContract; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 42 | import android.text.format.DateUtils; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 43 | import android.text.format.Formatter; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 44 | import android.text.format.Time; |
| 45 | import android.util.Log; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 46 | import android.util.SparseBooleanArray; |
| 47 | import android.view.ActionMode; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 48 | import android.view.LayoutInflater; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 49 | import android.view.Menu; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 50 | import android.view.MenuItem; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 51 | import android.view.View; |
| 52 | import android.view.ViewGroup; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 53 | import android.widget.AbsListView; |
| 54 | import android.widget.AbsListView.MultiChoiceModeListener; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 55 | import android.widget.AdapterView; |
| 56 | import android.widget.AdapterView.OnItemClickListener; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 57 | import android.widget.BaseAdapter; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 58 | import android.widget.Button; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 59 | import android.widget.GridView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 60 | import android.widget.ImageView; |
| 61 | import android.widget.ListView; |
| 62 | import android.widget.TextView; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 63 | import android.widget.Toast; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 64 | |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 65 | import com.android.documentsui.DocumentsActivity.DisplayState; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 66 | import com.android.documentsui.model.Document; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 67 | import com.android.documentsui.model.Root; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 68 | import com.android.internal.util.Predicate; |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 69 | import com.google.android.collect.Lists; |
| 70 | |
| 71 | import java.util.ArrayList; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 72 | import java.util.Comparator; |
| 73 | import java.util.List; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 74 | import java.util.concurrent.atomic.AtomicInteger; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Display the documents inside a single directory. |
| 78 | */ |
| 79 | public class DirectoryFragment extends Fragment { |
| 80 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 81 | private View mEmptyView; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 82 | private ListView mListView; |
| 83 | private GridView mGridView; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 84 | private Button mMoreView; |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 85 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 86 | private AbsListView mCurrentView; |
| 87 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 88 | private Predicate<Document> mFilter; |
| 89 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 90 | public static final int TYPE_NORMAL = 1; |
| 91 | public static final int TYPE_SEARCH = 2; |
| 92 | public static final int TYPE_RECENT_OPEN = 3; |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 93 | |
| 94 | private int mType = TYPE_NORMAL; |
| 95 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 96 | private Point mThumbSize; |
| 97 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 98 | private DocumentsAdapter mAdapter; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 99 | private LoaderCallbacks<DirectoryResult> mCallbacks; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 100 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 101 | private static final String EXTRA_TYPE = "type"; |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 102 | private static final String EXTRA_URI = "uri"; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 103 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 104 | private static AtomicInteger sLoaderId = new AtomicInteger(4000); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 105 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 106 | private final int mLoaderId = sLoaderId.incrementAndGet(); |
| 107 | |
| 108 | public static void showNormal(FragmentManager fm, Uri uri) { |
| 109 | show(fm, TYPE_NORMAL, uri); |
| 110 | } |
| 111 | |
| 112 | public static void showSearch(FragmentManager fm, Uri uri, String query) { |
| 113 | final Uri searchUri = DocumentsContract.buildSearchUri(uri, query); |
| 114 | show(fm, TYPE_SEARCH, searchUri); |
| 115 | } |
| 116 | |
| 117 | public static void showRecentsOpen(FragmentManager fm) { |
| 118 | show(fm, TYPE_RECENT_OPEN, null); |
| 119 | } |
| 120 | |
| 121 | private static void show(FragmentManager fm, int type, Uri uri) { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 122 | final Bundle args = new Bundle(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 123 | args.putInt(EXTRA_TYPE, type); |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 124 | args.putParcelable(EXTRA_URI, uri); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 125 | |
| 126 | final DirectoryFragment fragment = new DirectoryFragment(); |
| 127 | fragment.setArguments(args); |
| 128 | |
| 129 | final FragmentTransaction ft = fm.beginTransaction(); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 130 | ft.replace(R.id.container_directory, fragment); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 131 | ft.commitAllowingStateLoss(); |
| 132 | } |
| 133 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 134 | public static DirectoryFragment get(FragmentManager fm) { |
| 135 | // TODO: deal with multiple directories shown at once |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 136 | return (DirectoryFragment) fm.findFragmentById(R.id.container_directory); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 139 | @Override |
| 140 | public View onCreateView( |
| 141 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 142 | final Context context = inflater.getContext(); |
| 143 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 144 | final View view = inflater.inflate(R.layout.fragment_directory, container, false); |
| 145 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 146 | mEmptyView = view.findViewById(android.R.id.empty); |
| 147 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 148 | mListView = (ListView) view.findViewById(R.id.list); |
| 149 | mListView.setOnItemClickListener(mItemListener); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 150 | mListView.setMultiChoiceModeListener(mMultiListener); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 151 | |
| 152 | mGridView = (GridView) view.findViewById(R.id.grid); |
| 153 | mGridView.setOnItemClickListener(mItemListener); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 154 | mGridView.setMultiChoiceModeListener(mMultiListener); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 155 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 156 | mMoreView = (Button) view.findViewById(R.id.more); |
| 157 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 158 | mAdapter = new DocumentsAdapter(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 159 | |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 160 | final Uri uri = getArguments().getParcelable(EXTRA_URI); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 161 | mType = getArguments().getInt(EXTRA_TYPE); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 162 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 163 | mCallbacks = new LoaderCallbacks<DirectoryResult>() { |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 164 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 165 | public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 166 | final DisplayState state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 167 | mFilter = new MimePredicate(state.acceptMimes); |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 168 | |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 169 | Uri contentsUri; |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 170 | if (mType == TYPE_NORMAL) { |
| 171 | contentsUri = DocumentsContract.buildContentsUri(uri); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 172 | } else if (mType == TYPE_RECENT_OPEN) { |
| 173 | contentsUri = RecentsProvider.buildRecentOpen(); |
Jeff Sharkey | 46165b5 | 2013-07-31 20:53:22 -0700 | [diff] [blame] | 174 | } else { |
Jeff Sharkey | 5b53592 | 2013-08-02 15:55:26 -0700 | [diff] [blame] | 175 | contentsUri = uri; |
Jeff Sharkey | 46165b5 | 2013-07-31 20:53:22 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 178 | if (state.localOnly) { |
| 179 | contentsUri = DocumentsContract.setLocalOnly(contentsUri); |
| 180 | } |
| 181 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 182 | final Comparator<Document> sortOrder; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 183 | if (state.sortOrder == SORT_ORDER_DATE || mType == TYPE_RECENT_OPEN) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 184 | sortOrder = new Document.DateComparator(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 185 | } else if (state.sortOrder == SORT_ORDER_NAME) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 186 | sortOrder = new Document.NameComparator(); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 187 | } else if (state.sortOrder == SORT_ORDER_SIZE) { |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 188 | sortOrder = new Document.SizeComparator(); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 189 | } else { |
| 190 | throw new IllegalArgumentException("Unknown sort order " + state.sortOrder); |
| 191 | } |
| 192 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 193 | return new DirectoryLoader(context, contentsUri, mType, null, sortOrder); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 197 | public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) { |
| 198 | mAdapter.swapDocuments(result.contents); |
| 199 | |
| 200 | final Cursor cursor = result.cursor; |
| 201 | if (cursor != null && cursor.getExtras() |
| 202 | .getBoolean(DocumentsContract.EXTRA_HAS_MORE, false)) { |
| 203 | mMoreView.setText(R.string.more); |
| 204 | mMoreView.setVisibility(View.VISIBLE); |
| 205 | mMoreView.setOnClickListener(new View.OnClickListener() { |
| 206 | @Override |
| 207 | public void onClick(View v) { |
| 208 | mMoreView.setText(R.string.loading); |
| 209 | final Bundle bundle = new Bundle(); |
| 210 | bundle.putBoolean(DocumentsContract.EXTRA_REQUEST_MORE, true); |
| 211 | try { |
| 212 | cursor.respond(bundle); |
| 213 | } catch (Exception e) { |
| 214 | Log.w(TAG, "Failed to respond: " + e); |
| 215 | } |
| 216 | } |
| 217 | }); |
| 218 | } else { |
| 219 | mMoreView.setVisibility(View.GONE); |
| 220 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 224 | public void onLoaderReset(Loader<DirectoryResult> loader) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 225 | mAdapter.swapDocuments(null); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 226 | } |
| 227 | }; |
| 228 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 229 | updateDisplayState(); |
| 230 | |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 231 | return view; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void onStart() { |
| 236 | super.onStart(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 237 | getLoaderManager().restartLoader(mLoaderId, getArguments(), mCallbacks); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void onStop() { |
| 242 | super.onStop(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 243 | getLoaderManager().destroyLoader(mLoaderId); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 246 | public void updateDisplayState() { |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 247 | final DisplayState state = getDisplayState(this); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 248 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 249 | // TODO: avoid kicking loader when nothing changed |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 250 | getLoaderManager().restartLoader(mLoaderId, getArguments(), mCallbacks); |
| 251 | mListView.smoothScrollToPosition(0); |
| 252 | mGridView.smoothScrollToPosition(0); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 253 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 254 | mListView.setVisibility(state.mode == MODE_LIST ? View.VISIBLE : View.GONE); |
| 255 | mGridView.setVisibility(state.mode == MODE_GRID ? View.VISIBLE : View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 256 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 257 | final int choiceMode; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 258 | if (state.allowMultiple) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 259 | choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL; |
| 260 | } else { |
| 261 | choiceMode = ListView.CHOICE_MODE_NONE; |
| 262 | } |
| 263 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 264 | final int thumbSize; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 265 | if (state.mode == MODE_GRID) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 266 | thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 267 | mListView.setAdapter(null); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 268 | mListView.setChoiceMode(ListView.CHOICE_MODE_NONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 269 | mGridView.setAdapter(mAdapter); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 270 | mGridView.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.grid_width)); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 271 | mGridView.setNumColumns(GridView.AUTO_FIT); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 272 | mGridView.setChoiceMode(choiceMode); |
| 273 | mCurrentView = mGridView; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 274 | } else if (state.mode == MODE_LIST) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 275 | thumbSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 276 | mGridView.setAdapter(null); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 277 | mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 278 | mListView.setAdapter(mAdapter); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 279 | mListView.setChoiceMode(choiceMode); |
| 280 | mCurrentView = mListView; |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 281 | } else { |
| 282 | throw new IllegalStateException(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 283 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 284 | |
| 285 | mThumbSize = new Point(thumbSize, thumbSize); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | private OnItemClickListener mItemListener = new OnItemClickListener() { |
| 289 | @Override |
| 290 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 291 | final Document doc = mAdapter.getItem(position); |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 292 | if (mFilter.apply(doc)) { |
| 293 | ((DocumentsActivity) getActivity()).onDocumentPicked(doc); |
| 294 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 295 | } |
| 296 | }; |
| 297 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 298 | private MultiChoiceModeListener mMultiListener = new MultiChoiceModeListener() { |
| 299 | @Override |
| 300 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 301 | mode.getMenuInflater().inflate(R.menu.mode_directory, menu); |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 307 | final DisplayState state = getDisplayState(DirectoryFragment.this); |
| 308 | |
| 309 | final MenuItem open = menu.findItem(R.id.menu_open); |
| 310 | final MenuItem share = menu.findItem(R.id.menu_share); |
| 311 | final MenuItem delete = menu.findItem(R.id.menu_delete); |
| 312 | |
| 313 | final boolean manageMode = state.action == ACTION_MANAGE; |
| 314 | open.setVisible(!manageMode); |
| 315 | share.setVisible(manageMode); |
| 316 | delete.setVisible(manageMode); |
| 317 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 318 | return true; |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 323 | final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions(); |
| 324 | final ArrayList<Document> docs = Lists.newArrayList(); |
| 325 | final int size = checked.size(); |
| 326 | for (int i = 0; i < size; i++) { |
| 327 | if (checked.valueAt(i)) { |
| 328 | final Document doc = mAdapter.getItem(checked.keyAt(i)); |
| 329 | docs.add(doc); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 330 | } |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 331 | } |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 332 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 333 | final int id = item.getItemId(); |
| 334 | if (id == R.id.menu_open) { |
| 335 | DocumentsActivity.get(DirectoryFragment.this).onDocumentsPicked(docs); |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 336 | return true; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 337 | |
| 338 | } else if (id == R.id.menu_share) { |
| 339 | onShareDocuments(docs); |
| 340 | return true; |
| 341 | |
| 342 | } else if (id == R.id.menu_delete) { |
| 343 | onDeleteDocuments(docs); |
| 344 | return true; |
| 345 | |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 346 | } else { |
| 347 | return false; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public void onDestroyActionMode(ActionMode mode) { |
| 353 | // ignored |
| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public void onItemCheckedStateChanged( |
| 358 | ActionMode mode, int position, long id, boolean checked) { |
| 359 | if (checked) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 360 | // Directories cannot be checked |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 361 | final Document doc = mAdapter.getItem(position); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 362 | if (doc.isDirectory()) { |
Jeff Sharkey | c317af8 | 2013-07-01 16:56:54 -0700 | [diff] [blame] | 363 | mCurrentView.setItemChecked(position, false); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | mode.setTitle(getResources() |
| 368 | .getString(R.string.mode_selected_count, mCurrentView.getCheckedItemCount())); |
| 369 | } |
| 370 | }; |
| 371 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 372 | private void onShareDocuments(List<Document> docs) { |
| 373 | final ArrayList<Uri> uris = Lists.newArrayList(); |
| 374 | for (Document doc : docs) { |
| 375 | uris.add(doc.uri); |
| 376 | } |
| 377 | |
| 378 | final Intent intent; |
| 379 | if (uris.size() > 1) { |
| 380 | intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 381 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 382 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 383 | // TODO: find common mimetype |
| 384 | intent.setType("*/*"); |
| 385 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 386 | } else { |
| 387 | intent = new Intent(Intent.ACTION_SEND); |
| 388 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 389 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 390 | intent.setData(uris.get(0)); |
| 391 | } |
| 392 | |
| 393 | startActivity(intent); |
| 394 | } |
| 395 | |
| 396 | private void onDeleteDocuments(List<Document> docs) { |
| 397 | final Context context = getActivity(); |
| 398 | final ContentResolver resolver = context.getContentResolver(); |
| 399 | |
| 400 | boolean hadTrouble = false; |
| 401 | for (Document doc : docs) { |
| 402 | if (!doc.isDeleteSupported()) { |
| 403 | Log.w(TAG, "Skipping " + doc); |
| 404 | hadTrouble = true; |
| 405 | continue; |
| 406 | } |
| 407 | |
| 408 | try { |
| 409 | if (resolver.delete(doc.uri, null, null) != 1) { |
| 410 | Log.w(TAG, "Failed to delete " + doc); |
| 411 | hadTrouble = true; |
| 412 | } |
| 413 | } catch (Exception e) { |
| 414 | Log.w(TAG, "Failed to delete " + doc + ": " + e); |
| 415 | hadTrouble = true; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (hadTrouble) { |
| 420 | Toast.makeText(context, R.string.toast_failed_delete, Toast.LENGTH_SHORT).show(); |
| 421 | } |
| 422 | } |
| 423 | |
Jeff Sharkey | 3c28b79 | 2013-07-01 17:22:02 -0700 | [diff] [blame] | 424 | private static DisplayState getDisplayState(Fragment fragment) { |
| 425 | return ((DocumentsActivity) fragment.getActivity()).getDisplayState(); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 428 | private class DocumentsAdapter extends BaseAdapter { |
| 429 | private List<Document> mDocuments; |
| 430 | |
| 431 | public DocumentsAdapter() { |
| 432 | } |
| 433 | |
| 434 | public void swapDocuments(List<Document> documents) { |
| 435 | mDocuments = documents; |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 436 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 437 | if (mDocuments != null && mDocuments.isEmpty()) { |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 438 | mEmptyView.setVisibility(View.VISIBLE); |
| 439 | } else { |
| 440 | mEmptyView.setVisibility(View.GONE); |
| 441 | } |
| 442 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 443 | notifyDataSetChanged(); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | @Override |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 447 | public View getView(int position, View convertView, ViewGroup parent) { |
| 448 | final Context context = parent.getContext(); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 449 | final DisplayState state = getDisplayState(DirectoryFragment.this); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 450 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 451 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
| 452 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 453 | context, mThumbSize); |
| 454 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 455 | if (convertView == null) { |
| 456 | final LayoutInflater inflater = LayoutInflater.from(context); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 457 | if (state.mode == MODE_LIST) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 458 | convertView = inflater.inflate(R.layout.item_doc_list, parent, false); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 459 | } else if (state.mode == MODE_GRID) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 460 | convertView = inflater.inflate(R.layout.item_doc_grid, parent, false); |
| 461 | } else { |
| 462 | throw new IllegalStateException(); |
| 463 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 464 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 465 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 466 | final Document doc = getItem(position); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 467 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 468 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 469 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 470 | final View summaryGrid = convertView.findViewById(R.id.summary_grid); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 471 | final ImageView icon1 = (ImageView) convertView.findViewById(android.R.id.icon1); |
| 472 | final TextView summary = (TextView) convertView.findViewById(android.R.id.summary); |
| 473 | final TextView date = (TextView) convertView.findViewById(R.id.date); |
| 474 | final TextView size = (TextView) convertView.findViewById(R.id.size); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 475 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 476 | final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) icon.getTag(); |
| 477 | if (oldTask != null) { |
| 478 | oldTask.cancel(false); |
| 479 | } |
| 480 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 481 | if (doc.isThumbnailSupported()) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 482 | final Bitmap cachedResult = thumbs.get(doc.uri); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 483 | if (cachedResult != null) { |
| 484 | icon.setImageBitmap(cachedResult); |
| 485 | } else { |
| 486 | final ThumbnailAsyncTask task = new ThumbnailAsyncTask(icon, mThumbSize); |
| 487 | icon.setImageBitmap(null); |
| 488 | icon.setTag(task); |
| 489 | task.execute(doc.uri); |
| 490 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 491 | } else { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 492 | icon.setImageDrawable(roots.resolveDocumentIcon( |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 493 | context, doc.uri.getAuthority(), doc.mimeType)); |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 496 | title.setText(doc.displayName); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 497 | |
| 498 | if (mType == TYPE_NORMAL || mType == TYPE_SEARCH) { |
| 499 | icon1.setVisibility(View.GONE); |
| 500 | if (doc.summary != null) { |
| 501 | summary.setText(doc.summary); |
| 502 | summary.setVisibility(View.VISIBLE); |
| 503 | } else { |
| 504 | summary.setVisibility(View.INVISIBLE); |
| 505 | } |
| 506 | } else if (mType == TYPE_RECENT_OPEN) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 507 | final Root root = roots.findRoot(doc); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 508 | icon1.setVisibility(View.VISIBLE); |
| 509 | icon1.setImageDrawable(root.icon); |
| 510 | summary.setText(root.getDirectoryString()); |
| 511 | summary.setVisibility(View.VISIBLE); |
| 512 | } |
| 513 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 514 | if (summaryGrid != null) { |
| 515 | summaryGrid.setVisibility( |
| 516 | (summary.getVisibility() == View.VISIBLE) ? View.VISIBLE : View.GONE); |
| 517 | } |
| 518 | |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 519 | if (doc.lastModified == -1) { |
| 520 | date.setText(null); |
| 521 | } else { |
| 522 | date.setText(formatTime(context, doc.lastModified)); |
| 523 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 524 | |
| 525 | if (state.showSize) { |
| 526 | size.setVisibility(View.VISIBLE); |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 527 | if (doc.isDirectory() || doc.size == -1) { |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 528 | size.setText(null); |
| 529 | } else { |
| 530 | size.setText(Formatter.formatFileSize(context, doc.size)); |
| 531 | } |
| 532 | } else { |
| 533 | size.setVisibility(View.GONE); |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 534 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 535 | |
| 536 | return convertView; |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 537 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 538 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 539 | @Override |
| 540 | public int getCount() { |
| 541 | return mDocuments != null ? mDocuments.size() : 0; |
| 542 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 543 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 544 | @Override |
| 545 | public Document getItem(int position) { |
| 546 | return mDocuments.get(position); |
| 547 | } |
Jeff Sharkey | 09c10bf | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 548 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 549 | @Override |
| 550 | public long getItemId(int position) { |
| 551 | return getItem(position).uri.hashCode(); |
| 552 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 553 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 554 | |
| 555 | private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> { |
| 556 | private final ImageView mTarget; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 557 | private final Point mThumbSize; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 558 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 559 | public ThumbnailAsyncTask(ImageView target, Point thumbSize) { |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 560 | mTarget = target; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 561 | mThumbSize = thumbSize; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | @Override |
| 565 | protected void onPreExecute() { |
| 566 | mTarget.setTag(this); |
| 567 | } |
| 568 | |
| 569 | @Override |
| 570 | protected Bitmap doInBackground(Uri... params) { |
| 571 | final Context context = mTarget.getContext(); |
| 572 | final Uri uri = params[0]; |
| 573 | |
| 574 | Bitmap result = null; |
| 575 | try { |
| 576 | result = DocumentsContract.getThumbnail( |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 577 | context.getContentResolver(), uri, mThumbSize); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 578 | if (result != null) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 579 | final ThumbnailCache thumbs = DocumentsApplication.getThumbnailsCache( |
| 580 | context, mThumbSize); |
| 581 | thumbs.put(uri, result); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 582 | } |
| 583 | } catch (Exception e) { |
| 584 | Log.w(TAG, "Failed to load thumbnail: " + e); |
| 585 | } |
| 586 | return result; |
| 587 | } |
| 588 | |
| 589 | @Override |
| 590 | protected void onPostExecute(Bitmap result) { |
| 591 | if (mTarget.getTag() == this) { |
| 592 | mTarget.setImageBitmap(result); |
| 593 | mTarget.setTag(null); |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | private static String formatTime(Context context, long when) { |
| 599 | // TODO: DateUtils should make this easier |
| 600 | Time then = new Time(); |
| 601 | then.set(when); |
| 602 | Time now = new Time(); |
| 603 | now.setToNow(); |
| 604 | |
| 605 | int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT |
| 606 | | DateUtils.FORMAT_ABBREV_ALL; |
| 607 | |
| 608 | if (then.year != now.year) { |
| 609 | flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE; |
| 610 | } else if (then.yearDay != now.yearDay) { |
| 611 | flags |= DateUtils.FORMAT_SHOW_DATE; |
| 612 | } else { |
| 613 | flags |= DateUtils.FORMAT_SHOW_TIME; |
| 614 | } |
| 615 | |
| 616 | return DateUtils.formatDateTime(context, when, flags); |
| 617 | } |
Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 618 | } |