Jeff Sharkey | 2e694f8 | 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 | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.DocumentsActivity.TAG; |
| 20 | |
Jeff Sharkey | 2e694f8 | 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 | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | 2e694f8 | 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 | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 33 | import android.text.Spannable; |
| 34 | import android.text.SpannableStringBuilder; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 35 | import android.text.TextUtils.TruncateAt; |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 36 | import android.text.style.ImageSpan; |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Jeff Sharkey | 2e694f8 | 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 | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 48 | import com.android.documentsui.RecentsProvider.RecentColumns; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 49 | import com.android.documentsui.model.DocumentStack; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 50 | import com.google.android.collect.Lists; |
| 51 | |
| 52 | import libcore.io.IoUtils; |
| 53 | |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 54 | import java.io.ByteArrayInputStream; |
| 55 | import java.io.DataInputStream; |
| 56 | import java.io.IOException; |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 57 | import java.util.ArrayList; |
| 58 | import java.util.List; |
| 59 | |
| 60 | /** |
| 61 | * Display directories where recent creates took place. |
| 62 | */ |
| 63 | public class RecentsCreateFragment extends Fragment { |
| 64 | |
| 65 | private ListView mListView; |
| 66 | |
| 67 | private DocumentStackAdapter mAdapter; |
| 68 | private LoaderCallbacks<List<DocumentStack>> mCallbacks; |
| 69 | |
| 70 | private static final int LOADER_RECENTS = 3; |
| 71 | |
| 72 | public static void show(FragmentManager fm) { |
| 73 | final RecentsCreateFragment fragment = new RecentsCreateFragment(); |
| 74 | final FragmentTransaction ft = fm.beginTransaction(); |
| 75 | ft.replace(R.id.container_directory, fragment); |
| 76 | ft.commitAllowingStateLoss(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public View onCreateView( |
| 81 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 82 | final Context context = inflater.getContext(); |
| 83 | |
| 84 | final View view = inflater.inflate(R.layout.fragment_directory, container, false); |
| 85 | |
| 86 | mListView = (ListView) view.findViewById(R.id.list); |
| 87 | mListView.setOnItemClickListener(mItemListener); |
| 88 | |
| 89 | mAdapter = new DocumentStackAdapter(); |
| 90 | mListView.setAdapter(mAdapter); |
| 91 | |
| 92 | mCallbacks = new LoaderCallbacks<List<DocumentStack>>() { |
| 93 | @Override |
| 94 | public Loader<List<DocumentStack>> onCreateLoader(int id, Bundle args) { |
| 95 | return new RecentsCreateLoader(context); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void onLoadFinished( |
| 100 | Loader<List<DocumentStack>> loader, List<DocumentStack> data) { |
| 101 | mAdapter.swapStacks(data); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void onLoaderReset(Loader<List<DocumentStack>> loader) { |
| 106 | mAdapter.swapStacks(null); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | return view; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void onStart() { |
| 115 | super.onStart(); |
| 116 | getLoaderManager().restartLoader(LOADER_RECENTS, getArguments(), mCallbacks); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onStop() { |
| 121 | super.onStop(); |
| 122 | getLoaderManager().destroyLoader(LOADER_RECENTS); |
| 123 | } |
| 124 | |
| 125 | private OnItemClickListener mItemListener = new OnItemClickListener() { |
| 126 | @Override |
| 127 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 128 | final DocumentStack stack = mAdapter.getItem(position); |
| 129 | ((DocumentsActivity) getActivity()).onStackPicked(stack); |
| 130 | } |
| 131 | }; |
| 132 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 133 | public static class RecentsCreateLoader extends UriDerivativeLoader<Uri, List<DocumentStack>> { |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 134 | public RecentsCreateLoader(Context context) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 135 | super(context, RecentsProvider.buildRecent()); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public List<DocumentStack> loadInBackground(Uri uri, CancellationSignal signal) { |
| 140 | final ArrayList<DocumentStack> result = Lists.newArrayList(); |
| 141 | |
| 142 | final ContentResolver resolver = getContext().getContentResolver(); |
| 143 | final Cursor cursor = resolver.query( |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 144 | uri, null, null, null, RecentColumns.TIMESTAMP + " DESC", signal); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 145 | try { |
| 146 | while (cursor != null && cursor.moveToNext()) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 147 | final byte[] rawStack = cursor.getBlob( |
| 148 | cursor.getColumnIndex(RecentColumns.STACK)); |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 149 | try { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 150 | final DocumentStack stack = new DocumentStack(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 151 | stack.read(new DataInputStream(new ByteArrayInputStream(rawStack))); |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 152 | result.add(stack); |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 153 | } catch (IOException e) { |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 154 | Log.w(TAG, "Failed to resolve stack: " + e); |
| 155 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 156 | } |
| 157 | } finally { |
| 158 | IoUtils.closeQuietly(cursor); |
| 159 | } |
| 160 | |
| 161 | return result; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private class DocumentStackAdapter extends BaseAdapter { |
| 166 | private List<DocumentStack> mStacks; |
| 167 | |
| 168 | public DocumentStackAdapter() { |
| 169 | } |
| 170 | |
| 171 | public void swapStacks(List<DocumentStack> stacks) { |
| 172 | mStacks = stacks; |
| 173 | notifyDataSetChanged(); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public View getView(int position, View convertView, ViewGroup parent) { |
| 178 | final Context context = parent.getContext(); |
| 179 | |
| 180 | if (convertView == null) { |
| 181 | final LayoutInflater inflater = LayoutInflater.from(context); |
| 182 | convertView = inflater.inflate(R.layout.item_doc_list, parent, false); |
| 183 | } |
| 184 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 185 | final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 186 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 187 | final View line2 = convertView.findViewById(R.id.line2); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 188 | |
| 189 | final DocumentStack stack = getItem(position); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 190 | iconMime.setImageDrawable(stack.root.loadIcon(context)); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 191 | |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 192 | final Drawable crumb = context.getResources() |
| 193 | .getDrawable(R.drawable.ic_breadcrumb_arrow); |
| 194 | crumb.setBounds(0, 0, crumb.getIntrinsicWidth(), crumb.getIntrinsicHeight()); |
| 195 | |
| 196 | final SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 197 | builder.append(stack.root.title); |
| 198 | appendDrawable(builder, crumb); |
| 199 | for (int i = stack.size() - 2; i >= 0; i--) { |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 200 | builder.append(stack.get(i).displayName); |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 201 | if (i > 0) { |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 202 | appendDrawable(builder, crumb); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 205 | title.setText(builder); |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 206 | title.setEllipsize(TruncateAt.MIDDLE); |
| 207 | |
Jeff Sharkey | 6a99ec8 | 2013-09-19 11:25:56 -0700 | [diff] [blame] | 208 | if (line2 != null) line2.setVisibility(View.GONE); |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 209 | |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 210 | return convertView; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public int getCount() { |
| 215 | return mStacks != null ? mStacks.size() : 0; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public DocumentStack getItem(int position) { |
| 220 | return mStacks.get(position); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public long getItemId(int position) { |
| 225 | return getItem(position).hashCode(); |
| 226 | } |
| 227 | } |
Jeff Sharkey | aed873d | 2013-09-09 16:51:06 -0700 | [diff] [blame] | 228 | |
| 229 | private static void appendDrawable(SpannableStringBuilder b, Drawable d) { |
| 230 | final int length = b.length(); |
| 231 | b.append("\u232a"); |
| 232 | b.setSpan(new ImageSpan(d), length, b.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 233 | } |
Jeff Sharkey | 2e694f8 | 2013-08-06 16:26:14 -0700 | [diff] [blame] | 234 | } |