blob: 50763703c56a098ea30eebc53ec4c034f3212033 [file] [log] [blame]
Jeff Sharkey2e694f82013-08-06 16:26:14 -07001/*
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
17package com.android.documentsui;
18
Jeff Sharkey744dbbd2013-08-07 18:33:33 -070019import static com.android.documentsui.DocumentsActivity.TAG;
20
Jeff Sharkey2e694f82013-08-06 16:26:14 -070021import android.app.Fragment;
22import android.app.FragmentManager;
23import android.app.FragmentTransaction;
24import android.app.LoaderManager.LoaderCallbacks;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Loader;
28import android.database.Cursor;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -070029import android.graphics.drawable.Drawable;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070030import android.net.Uri;
31import android.os.Bundle;
32import android.os.CancellationSignal;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -070033import android.text.Spannable;
34import android.text.SpannableStringBuilder;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070035import android.text.TextUtils.TruncateAt;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -070036import android.text.style.ImageSpan;
Jeff Sharkey744dbbd2013-08-07 18:33:33 -070037import android.util.Log;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070038import android.view.LayoutInflater;
39import android.view.View;
40import android.view.ViewGroup;
41import android.widget.AdapterView;
42import android.widget.AdapterView.OnItemClickListener;
43import android.widget.BaseAdapter;
44import android.widget.ImageView;
45import android.widget.ListView;
46import android.widget.TextView;
47
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070048import com.android.documentsui.RecentsProvider.RecentColumns;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070049import com.android.documentsui.model.DocumentStack;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070050import com.google.android.collect.Lists;
51
52import libcore.io.IoUtils;
53
Jeff Sharkeyb3620442013-09-01 18:41:04 -070054import java.io.ByteArrayInputStream;
55import java.io.DataInputStream;
56import java.io.IOException;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070057import java.util.ArrayList;
58import java.util.List;
59
60/**
61 * Display directories where recent creates took place.
62 */
63public 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 Sharkey46899c82013-08-18 22:26:48 -0700133 public static class RecentsCreateLoader extends UriDerivativeLoader<Uri, List<DocumentStack>> {
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700134 public RecentsCreateLoader(Context context) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700135 super(context, RecentsProvider.buildRecent());
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700136 }
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 Sharkeya4d1f222013-09-07 14:45:03 -0700144 uri, null, null, null, RecentColumns.TIMESTAMP + " DESC", signal);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700145 try {
146 while (cursor != null && cursor.moveToNext()) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700147 final byte[] rawStack = cursor.getBlob(
148 cursor.getColumnIndex(RecentColumns.STACK));
Jeff Sharkey744dbbd2013-08-07 18:33:33 -0700149 try {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700150 final DocumentStack stack = new DocumentStack();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700151 stack.read(new DataInputStream(new ByteArrayInputStream(rawStack)));
Jeff Sharkey744dbbd2013-08-07 18:33:33 -0700152 result.add(stack);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700153 } catch (IOException e) {
Jeff Sharkey744dbbd2013-08-07 18:33:33 -0700154 Log.w(TAG, "Failed to resolve stack: " + e);
155 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700156 }
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 Sharkeya35ac2d2013-09-10 12:04:26 -0700185 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700186 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700187 final View line2 = convertView.findViewById(R.id.line2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700188
189 final DocumentStack stack = getItem(position);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700190 iconMime.setImageDrawable(stack.root.loadIcon(context));
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700191
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700192 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 Sharkey2e694f82013-08-06 16:26:14 -0700200 builder.append(stack.get(i).displayName);
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700201 if (i > 0) {
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700202 appendDrawable(builder, crumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700203 }
204 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700205 title.setText(builder);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700206 title.setEllipsize(TruncateAt.MIDDLE);
207
Jeff Sharkey6a99ec82013-09-19 11:25:56 -0700208 if (line2 != null) line2.setVisibility(View.GONE);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700209
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700210 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 Sharkeyaed873d2013-09-09 16:51:06 -0700228
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 Sharkey2e694f82013-08-06 16:26:14 -0700234}