blob: 05766f5409d4dbf9023144c8b54d99a77dd5f9bc [file] [log] [blame]
Jeff Sharkeyb156f4b2013-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 Sharkey0c2d31b2013-08-07 18:33:33 -070019import static com.android.documentsui.DocumentsActivity.TAG;
20
Jeff Sharkeyb156f4b2013-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 Sharkey3f4c2052013-09-09 16:51:06 -070029import android.graphics.drawable.Drawable;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -070030import android.net.Uri;
31import android.os.Bundle;
32import android.os.CancellationSignal;
Jeff Sharkey3f4c2052013-09-09 16:51:06 -070033import android.text.Spannable;
34import android.text.SpannableStringBuilder;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -070035import android.text.TextUtils.TruncateAt;
Jeff Sharkey3f4c2052013-09-09 16:51:06 -070036import android.text.style.ImageSpan;
Jeff Sharkey0c2d31b2013-08-07 18:33:33 -070037import android.util.Log;
Jeff Sharkeyb156f4b2013-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 Sharkeydeffade2013-09-24 12:07:12 -070048import com.android.documentsui.DocumentsActivity.State;
Jeff Sharkeyd182bb62013-09-07 14:45:03 -070049import com.android.documentsui.RecentsProvider.RecentColumns;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -070050import com.android.documentsui.model.DocumentStack;
Jeff Sharkey6a20e572013-09-25 14:39:14 -070051import com.android.documentsui.model.DurableUtils;
Jeff Sharkeydeffade2013-09-24 12:07:12 -070052import com.android.documentsui.model.RootInfo;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -070053import com.google.android.collect.Lists;
54
55import libcore.io.IoUtils;
56
Jeff Sharkeyb5133112013-09-01 18:41:04 -070057import java.io.ByteArrayInputStream;
58import java.io.DataInputStream;
59import java.io.IOException;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -070060import java.util.ArrayList;
Jeff Sharkeydeffade2013-09-24 12:07:12 -070061import java.util.Collection;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -070062import java.util.List;
63
64/**
65 * Display directories where recent creates took place.
66 */
67public class RecentsCreateFragment extends Fragment {
68
Jeff Sharkey6efba222013-09-27 16:44:11 -070069 private View mEmptyView;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -070070 private ListView mListView;
71
72 private DocumentStackAdapter mAdapter;
73 private LoaderCallbacks<List<DocumentStack>> mCallbacks;
74
75 private static final int LOADER_RECENTS = 3;
76
77 public static void show(FragmentManager fm) {
78 final RecentsCreateFragment fragment = new RecentsCreateFragment();
79 final FragmentTransaction ft = fm.beginTransaction();
80 ft.replace(R.id.container_directory, fragment);
81 ft.commitAllowingStateLoss();
82 }
83
84 @Override
85 public View onCreateView(
86 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
87 final Context context = inflater.getContext();
88
89 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
90
Jeff Sharkey6efba222013-09-27 16:44:11 -070091 mEmptyView = view.findViewById(android.R.id.empty);
92
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -070093 mListView = (ListView) view.findViewById(R.id.list);
94 mListView.setOnItemClickListener(mItemListener);
95
96 mAdapter = new DocumentStackAdapter();
97 mListView.setAdapter(mAdapter);
98
Jeff Sharkeydeffade2013-09-24 12:07:12 -070099 final RootsCache roots = DocumentsApplication.getRootsCache(context);
100 final State state = ((DocumentsActivity) getActivity()).getDisplayState();
101
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700102 mCallbacks = new LoaderCallbacks<List<DocumentStack>>() {
103 @Override
104 public Loader<List<DocumentStack>> onCreateLoader(int id, Bundle args) {
Jeff Sharkeydeffade2013-09-24 12:07:12 -0700105 return new RecentsCreateLoader(context, roots, state);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700106 }
107
108 @Override
109 public void onLoadFinished(
110 Loader<List<DocumentStack>> loader, List<DocumentStack> data) {
111 mAdapter.swapStacks(data);
Jeff Sharkeya82c2e22013-10-07 14:08:17 -0700112
113 // When launched into empty recents, show drawer
114 if (mAdapter.isEmpty() && !state.stackTouched) {
115 ((DocumentsActivity) context).setRootsDrawerOpen(true);
116 }
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700117 }
118
119 @Override
120 public void onLoaderReset(Loader<List<DocumentStack>> loader) {
121 mAdapter.swapStacks(null);
122 }
123 };
124
125 return view;
126 }
127
128 @Override
129 public void onStart() {
130 super.onStart();
131 getLoaderManager().restartLoader(LOADER_RECENTS, getArguments(), mCallbacks);
132 }
133
134 @Override
135 public void onStop() {
136 super.onStop();
137 getLoaderManager().destroyLoader(LOADER_RECENTS);
138 }
139
140 private OnItemClickListener mItemListener = new OnItemClickListener() {
141 @Override
142 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
143 final DocumentStack stack = mAdapter.getItem(position);
144 ((DocumentsActivity) getActivity()).onStackPicked(stack);
145 }
146 };
147
Jeff Sharkeyb4486602013-08-18 22:26:48 -0700148 public static class RecentsCreateLoader extends UriDerivativeLoader<Uri, List<DocumentStack>> {
Jeff Sharkeydeffade2013-09-24 12:07:12 -0700149 private final RootsCache mRoots;
150 private final State mState;
151
152 public RecentsCreateLoader(Context context, RootsCache roots, State state) {
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700153 super(context, RecentsProvider.buildRecent());
Jeff Sharkeydeffade2013-09-24 12:07:12 -0700154 mRoots = roots;
155 mState = state;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700156 }
157
158 @Override
159 public List<DocumentStack> loadInBackground(Uri uri, CancellationSignal signal) {
Jeff Sharkeydeffade2013-09-24 12:07:12 -0700160 final Collection<RootInfo> matchingRoots = mRoots.getMatchingRootsBlocking(mState);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700161 final ArrayList<DocumentStack> result = Lists.newArrayList();
162
163 final ContentResolver resolver = getContext().getContentResolver();
164 final Cursor cursor = resolver.query(
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700165 uri, null, null, null, RecentColumns.TIMESTAMP + " DESC", signal);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700166 try {
167 while (cursor != null && cursor.moveToNext()) {
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700168 final byte[] rawStack = cursor.getBlob(
169 cursor.getColumnIndex(RecentColumns.STACK));
Jeff Sharkey0c2d31b2013-08-07 18:33:33 -0700170 try {
Jeff Sharkeyb5133112013-09-01 18:41:04 -0700171 final DocumentStack stack = new DocumentStack();
Jeff Sharkey6a20e572013-09-25 14:39:14 -0700172 DurableUtils.readFromArray(rawStack, stack);
Jeff Sharkeydeffade2013-09-24 12:07:12 -0700173
174 // Only update root here to avoid spinning up all
175 // providers; we update the stack during the actual
176 // restore. This also filters away roots that don't
177 // match current filter.
178 stack.updateRoot(matchingRoots);
Jeff Sharkey0c2d31b2013-08-07 18:33:33 -0700179 result.add(stack);
Jeff Sharkeyb5133112013-09-01 18:41:04 -0700180 } catch (IOException e) {
Jeff Sharkey0c2d31b2013-08-07 18:33:33 -0700181 Log.w(TAG, "Failed to resolve stack: " + e);
182 }
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700183 }
184 } finally {
185 IoUtils.closeQuietly(cursor);
186 }
187
188 return result;
189 }
190 }
191
192 private class DocumentStackAdapter extends BaseAdapter {
193 private List<DocumentStack> mStacks;
194
195 public DocumentStackAdapter() {
196 }
197
198 public void swapStacks(List<DocumentStack> stacks) {
199 mStacks = stacks;
Jeff Sharkey6efba222013-09-27 16:44:11 -0700200
201 if (isEmpty()) {
202 mEmptyView.setVisibility(View.VISIBLE);
203 } else {
204 mEmptyView.setVisibility(View.GONE);
205 }
206
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700207 notifyDataSetChanged();
208 }
209
210 @Override
211 public View getView(int position, View convertView, ViewGroup parent) {
212 final Context context = parent.getContext();
213
214 if (convertView == null) {
215 final LayoutInflater inflater = LayoutInflater.from(context);
216 convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
217 }
218
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700219 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700220 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700221 final View line2 = convertView.findViewById(R.id.line2);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700222
223 final DocumentStack stack = getItem(position);
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700224 iconMime.setImageDrawable(stack.root.loadIcon(context));
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700225
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700226 final Drawable crumb = context.getResources()
227 .getDrawable(R.drawable.ic_breadcrumb_arrow);
228 crumb.setBounds(0, 0, crumb.getIntrinsicWidth(), crumb.getIntrinsicHeight());
229
230 final SpannableStringBuilder builder = new SpannableStringBuilder();
231 builder.append(stack.root.title);
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700232 for (int i = stack.size() - 2; i >= 0; i--) {
Jeff Sharkey5545f562013-09-21 13:57:33 -0700233 appendDrawable(builder, crumb);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700234 builder.append(stack.get(i).displayName);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700235 }
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700236 title.setText(builder);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700237 title.setEllipsize(TruncateAt.MIDDLE);
238
Jeff Sharkeyf8abf2e32013-09-19 11:25:56 -0700239 if (line2 != null) line2.setVisibility(View.GONE);
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700240
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700241 return convertView;
242 }
243
244 @Override
245 public int getCount() {
246 return mStacks != null ? mStacks.size() : 0;
247 }
248
249 @Override
250 public DocumentStack getItem(int position) {
251 return mStacks.get(position);
252 }
253
254 @Override
255 public long getItemId(int position) {
256 return getItem(position).hashCode();
257 }
258 }
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700259
260 private static void appendDrawable(SpannableStringBuilder b, Drawable d) {
261 final int length = b.length();
262 b.append("\u232a");
263 b.setSpan(new ImageSpan(d), length, b.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
264 }
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700265}