Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -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 | 0c2d31b | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.DocumentsActivity.TAG; |
| 20 | |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 21 | import android.app.Fragment; |
| 22 | import android.app.FragmentManager; |
| 23 | import android.app.FragmentTransaction; |
| 24 | import android.app.LoaderManager.LoaderCallbacks; |
| 25 | import android.content.ContentResolver; |
| 26 | import android.content.Context; |
| 27 | import android.content.Loader; |
| 28 | import android.database.Cursor; |
Jeff Sharkey | 3f4c205 | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 30 | import android.net.Uri; |
| 31 | import android.os.Bundle; |
| 32 | import android.os.CancellationSignal; |
Jeff Sharkey | 3f4c205 | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 33 | import android.text.Spannable; |
| 34 | import android.text.SpannableStringBuilder; |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 35 | import android.text.TextUtils.TruncateAt; |
Jeff Sharkey | 3f4c205 | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 36 | import android.text.style.ImageSpan; |
Jeff Sharkey | 0c2d31b | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 38 | import android.view.LayoutInflater; |
| 39 | import android.view.View; |
| 40 | import android.view.ViewGroup; |
| 41 | import android.widget.AdapterView; |
| 42 | import android.widget.AdapterView.OnItemClickListener; |
| 43 | import android.widget.BaseAdapter; |
| 44 | import android.widget.ImageView; |
| 45 | import android.widget.ListView; |
| 46 | import android.widget.TextView; |
| 47 | |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 48 | import com.android.documentsui.DocumentsActivity.State; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 49 | import com.android.documentsui.RecentsProvider.RecentColumns; |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 50 | import com.android.documentsui.model.DocumentStack; |
Jeff Sharkey | 6a20e57 | 2013-09-25 14:39:14 -0700 | [diff] [blame] | 51 | import com.android.documentsui.model.DurableUtils; |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 52 | import com.android.documentsui.model.RootInfo; |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 53 | import com.google.android.collect.Lists; |
| 54 | |
| 55 | import libcore.io.IoUtils; |
| 56 | |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 57 | import java.io.IOException; |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 58 | import java.util.ArrayList; |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 59 | import java.util.Collection; |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 60 | import java.util.List; |
| 61 | |
| 62 | /** |
| 63 | * Display directories where recent creates took place. |
| 64 | */ |
| 65 | public class RecentsCreateFragment extends Fragment { |
| 66 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 67 | private View mEmptyView; |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 68 | private ListView mListView; |
| 69 | |
| 70 | private DocumentStackAdapter mAdapter; |
| 71 | private LoaderCallbacks<List<DocumentStack>> mCallbacks; |
| 72 | |
| 73 | private static final int LOADER_RECENTS = 3; |
| 74 | |
| 75 | public static void show(FragmentManager fm) { |
| 76 | final RecentsCreateFragment fragment = new RecentsCreateFragment(); |
| 77 | final FragmentTransaction ft = fm.beginTransaction(); |
| 78 | ft.replace(R.id.container_directory, fragment); |
| 79 | ft.commitAllowingStateLoss(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public View onCreateView( |
| 84 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 85 | final Context context = inflater.getContext(); |
| 86 | |
| 87 | final View view = inflater.inflate(R.layout.fragment_directory, container, false); |
| 88 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 89 | mEmptyView = view.findViewById(android.R.id.empty); |
| 90 | |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 91 | mListView = (ListView) view.findViewById(R.id.list); |
| 92 | mListView.setOnItemClickListener(mItemListener); |
| 93 | |
| 94 | mAdapter = new DocumentStackAdapter(); |
| 95 | mListView.setAdapter(mAdapter); |
| 96 | |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 97 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
| 98 | final State state = ((DocumentsActivity) getActivity()).getDisplayState(); |
| 99 | |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 100 | mCallbacks = new LoaderCallbacks<List<DocumentStack>>() { |
| 101 | @Override |
| 102 | public Loader<List<DocumentStack>> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 103 | return new RecentsCreateLoader(context, roots, state); |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void onLoadFinished( |
| 108 | Loader<List<DocumentStack>> loader, List<DocumentStack> data) { |
| 109 | mAdapter.swapStacks(data); |
Jeff Sharkey | a82c2e2 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 110 | |
| 111 | // When launched into empty recents, show drawer |
| 112 | if (mAdapter.isEmpty() && !state.stackTouched) { |
| 113 | ((DocumentsActivity) context).setRootsDrawerOpen(true); |
| 114 | } |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void onLoaderReset(Loader<List<DocumentStack>> loader) { |
| 119 | mAdapter.swapStacks(null); |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | return view; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void onStart() { |
| 128 | super.onStart(); |
| 129 | getLoaderManager().restartLoader(LOADER_RECENTS, getArguments(), mCallbacks); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void onStop() { |
| 134 | super.onStop(); |
| 135 | getLoaderManager().destroyLoader(LOADER_RECENTS); |
| 136 | } |
| 137 | |
| 138 | private OnItemClickListener mItemListener = new OnItemClickListener() { |
| 139 | @Override |
| 140 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 141 | final DocumentStack stack = mAdapter.getItem(position); |
| 142 | ((DocumentsActivity) getActivity()).onStackPicked(stack); |
| 143 | } |
| 144 | }; |
| 145 | |
Jeff Sharkey | b448660 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 146 | public static class RecentsCreateLoader extends UriDerivativeLoader<Uri, List<DocumentStack>> { |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 147 | private final RootsCache mRoots; |
| 148 | private final State mState; |
| 149 | |
| 150 | public RecentsCreateLoader(Context context, RootsCache roots, State state) { |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 151 | super(context, RecentsProvider.buildRecent()); |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 152 | mRoots = roots; |
| 153 | mState = state; |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public List<DocumentStack> loadInBackground(Uri uri, CancellationSignal signal) { |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 158 | final Collection<RootInfo> matchingRoots = mRoots.getMatchingRootsBlocking(mState); |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 159 | final ArrayList<DocumentStack> result = Lists.newArrayList(); |
| 160 | |
| 161 | final ContentResolver resolver = getContext().getContentResolver(); |
| 162 | final Cursor cursor = resolver.query( |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 163 | uri, null, null, null, RecentColumns.TIMESTAMP + " DESC", signal); |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 164 | try { |
| 165 | while (cursor != null && cursor.moveToNext()) { |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 166 | final byte[] rawStack = cursor.getBlob( |
| 167 | cursor.getColumnIndex(RecentColumns.STACK)); |
Jeff Sharkey | 0c2d31b | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 168 | try { |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 169 | final DocumentStack stack = new DocumentStack(); |
Jeff Sharkey | 6a20e57 | 2013-09-25 14:39:14 -0700 | [diff] [blame] | 170 | DurableUtils.readFromArray(rawStack, stack); |
Jeff Sharkey | deffade | 2013-09-24 12:07:12 -0700 | [diff] [blame] | 171 | |
| 172 | // Only update root here to avoid spinning up all |
| 173 | // providers; we update the stack during the actual |
| 174 | // restore. This also filters away roots that don't |
| 175 | // match current filter. |
| 176 | stack.updateRoot(matchingRoots); |
Jeff Sharkey | 0c2d31b | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 177 | result.add(stack); |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 178 | } catch (IOException e) { |
Jeff Sharkey | 0c2d31b | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 179 | Log.w(TAG, "Failed to resolve stack: " + e); |
| 180 | } |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 181 | } |
| 182 | } finally { |
| 183 | IoUtils.closeQuietly(cursor); |
| 184 | } |
| 185 | |
| 186 | return result; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private class DocumentStackAdapter extends BaseAdapter { |
| 191 | private List<DocumentStack> mStacks; |
| 192 | |
| 193 | public DocumentStackAdapter() { |
| 194 | } |
| 195 | |
| 196 | public void swapStacks(List<DocumentStack> stacks) { |
| 197 | mStacks = stacks; |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 198 | |
| 199 | if (isEmpty()) { |
| 200 | mEmptyView.setVisibility(View.VISIBLE); |
| 201 | } else { |
| 202 | mEmptyView.setVisibility(View.GONE); |
| 203 | } |
| 204 | |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 205 | notifyDataSetChanged(); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public View getView(int position, View convertView, ViewGroup parent) { |
| 210 | final Context context = parent.getContext(); |
| 211 | |
| 212 | if (convertView == null) { |
| 213 | final LayoutInflater inflater = LayoutInflater.from(context); |
| 214 | convertView = inflater.inflate(R.layout.item_doc_list, parent, false); |
| 215 | } |
| 216 | |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 217 | final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime); |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 218 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
Jeff Sharkey | 3f4c205 | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 219 | final View line2 = convertView.findViewById(R.id.line2); |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 220 | |
| 221 | final DocumentStack stack = getItem(position); |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 222 | iconMime.setImageDrawable(stack.root.loadIcon(context)); |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 223 | |
Jeff Sharkey | 3f4c205 | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 224 | final Drawable crumb = context.getResources() |
| 225 | .getDrawable(R.drawable.ic_breadcrumb_arrow); |
| 226 | crumb.setBounds(0, 0, crumb.getIntrinsicWidth(), crumb.getIntrinsicHeight()); |
| 227 | |
| 228 | final SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 229 | builder.append(stack.root.title); |
Jeff Sharkey | 3f4c205 | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 230 | for (int i = stack.size() - 2; i >= 0; i--) { |
Jeff Sharkey | 5545f56 | 2013-09-21 13:57:33 -0700 | [diff] [blame] | 231 | appendDrawable(builder, crumb); |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 232 | builder.append(stack.get(i).displayName); |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 233 | } |
Jeff Sharkey | 3f4c205 | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 234 | title.setText(builder); |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 235 | title.setEllipsize(TruncateAt.MIDDLE); |
| 236 | |
Jeff Sharkey | f8abf2e3 | 2013-09-19 11:25:56 -0700 | [diff] [blame] | 237 | if (line2 != null) line2.setVisibility(View.GONE); |
Jeff Sharkey | 3f4c205 | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 238 | |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 239 | return convertView; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public int getCount() { |
| 244 | return mStacks != null ? mStacks.size() : 0; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public DocumentStack getItem(int position) { |
| 249 | return mStacks.get(position); |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public long getItemId(int position) { |
| 254 | return getItem(position).hashCode(); |
| 255 | } |
| 256 | } |
Jeff Sharkey | 3f4c205 | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 257 | |
| 258 | private static void appendDrawable(SpannableStringBuilder b, Drawable d) { |
| 259 | final int length = b.length(); |
| 260 | b.append("\u232a"); |
| 261 | b.setSpan(new ImageSpan(d), length, b.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 262 | } |
Jeff Sharkey | b156f4b | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 263 | } |