blob: 82c3048ae2bc0c2786b2438907dad8605fcc42aa [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.IOException;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -070058import java.util.ArrayList;
Jeff Sharkeydeffade2013-09-24 12:07:12 -070059import java.util.Collection;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -070060import java.util.List;
61
62/**
63 * Display directories where recent creates took place.
64 */
65public class RecentsCreateFragment extends Fragment {
66
Jeff Sharkey6efba222013-09-27 16:44:11 -070067 private View mEmptyView;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -070068 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 Sharkey6efba222013-09-27 16:44:11 -070089 mEmptyView = view.findViewById(android.R.id.empty);
90
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -070091 mListView = (ListView) view.findViewById(R.id.list);
92 mListView.setOnItemClickListener(mItemListener);
93
94 mAdapter = new DocumentStackAdapter();
95 mListView.setAdapter(mAdapter);
96
Jeff Sharkeydeffade2013-09-24 12:07:12 -070097 final RootsCache roots = DocumentsApplication.getRootsCache(context);
98 final State state = ((DocumentsActivity) getActivity()).getDisplayState();
99
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700100 mCallbacks = new LoaderCallbacks<List<DocumentStack>>() {
101 @Override
102 public Loader<List<DocumentStack>> onCreateLoader(int id, Bundle args) {
Jeff Sharkeydeffade2013-09-24 12:07:12 -0700103 return new RecentsCreateLoader(context, roots, state);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700104 }
105
106 @Override
107 public void onLoadFinished(
108 Loader<List<DocumentStack>> loader, List<DocumentStack> data) {
109 mAdapter.swapStacks(data);
Jeff Sharkeya82c2e22013-10-07 14:08:17 -0700110
111 // When launched into empty recents, show drawer
112 if (mAdapter.isEmpty() && !state.stackTouched) {
113 ((DocumentsActivity) context).setRootsDrawerOpen(true);
114 }
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700115 }
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 Sharkeyb4486602013-08-18 22:26:48 -0700146 public static class RecentsCreateLoader extends UriDerivativeLoader<Uri, List<DocumentStack>> {
Jeff Sharkeydeffade2013-09-24 12:07:12 -0700147 private final RootsCache mRoots;
148 private final State mState;
149
150 public RecentsCreateLoader(Context context, RootsCache roots, State state) {
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700151 super(context, RecentsProvider.buildRecent());
Jeff Sharkeydeffade2013-09-24 12:07:12 -0700152 mRoots = roots;
153 mState = state;
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700154 }
155
156 @Override
157 public List<DocumentStack> loadInBackground(Uri uri, CancellationSignal signal) {
Jeff Sharkeydeffade2013-09-24 12:07:12 -0700158 final Collection<RootInfo> matchingRoots = mRoots.getMatchingRootsBlocking(mState);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700159 final ArrayList<DocumentStack> result = Lists.newArrayList();
160
161 final ContentResolver resolver = getContext().getContentResolver();
162 final Cursor cursor = resolver.query(
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700163 uri, null, null, null, RecentColumns.TIMESTAMP + " DESC", signal);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700164 try {
165 while (cursor != null && cursor.moveToNext()) {
Jeff Sharkeyd182bb62013-09-07 14:45:03 -0700166 final byte[] rawStack = cursor.getBlob(
167 cursor.getColumnIndex(RecentColumns.STACK));
Jeff Sharkey0c2d31b2013-08-07 18:33:33 -0700168 try {
Jeff Sharkeyb5133112013-09-01 18:41:04 -0700169 final DocumentStack stack = new DocumentStack();
Jeff Sharkey6a20e572013-09-25 14:39:14 -0700170 DurableUtils.readFromArray(rawStack, stack);
Jeff Sharkeydeffade2013-09-24 12:07:12 -0700171
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 Sharkey0c2d31b2013-08-07 18:33:33 -0700177 result.add(stack);
Jeff Sharkeyb5133112013-09-01 18:41:04 -0700178 } catch (IOException e) {
Jeff Sharkey0c2d31b2013-08-07 18:33:33 -0700179 Log.w(TAG, "Failed to resolve stack: " + e);
180 }
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700181 }
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 Sharkey6efba222013-09-27 16:44:11 -0700198
199 if (isEmpty()) {
200 mEmptyView.setVisibility(View.VISIBLE);
201 } else {
202 mEmptyView.setVisibility(View.GONE);
203 }
204
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700205 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 Sharkey4ec97392013-09-10 12:04:26 -0700217 final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700218 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700219 final View line2 = convertView.findViewById(R.id.line2);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700220
221 final DocumentStack stack = getItem(position);
Jeff Sharkey4ec97392013-09-10 12:04:26 -0700222 iconMime.setImageDrawable(stack.root.loadIcon(context));
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700223
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700224 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 Sharkey3f4c2052013-09-09 16:51:06 -0700230 for (int i = stack.size() - 2; i >= 0; i--) {
Jeff Sharkey5545f562013-09-21 13:57:33 -0700231 appendDrawable(builder, crumb);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700232 builder.append(stack.get(i).displayName);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700233 }
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700234 title.setText(builder);
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700235 title.setEllipsize(TruncateAt.MIDDLE);
236
Jeff Sharkeyf8abf2e32013-09-19 11:25:56 -0700237 if (line2 != null) line2.setVisibility(View.GONE);
Jeff Sharkey3f4c2052013-09-09 16:51:06 -0700238
Jeff Sharkeyb156f4b2013-08-06 16:26:14 -0700239 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 Sharkey3f4c2052013-09-09 16:51:06 -0700257
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 Sharkeyb156f4b2013-08-06 16:26:14 -0700263}