blob: 2be93b83fbae5e983206d4a4d76477e678426a9f [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
Steve McKayfefcd702015-08-20 16:19:38 +000019import static com.android.documentsui.Shared.TAG;
Ben Lina0c7c742016-04-13 16:33:20 -070020import static com.android.documentsui.State.ACTION_CREATE;
Jeff Sharkey744dbbd2013-08-07 18:33:33 -070021
Jeff Sharkey2e694f82013-08-06 16:26:14 -070022import android.app.Fragment;
23import android.app.FragmentManager;
24import android.app.FragmentTransaction;
25import android.app.LoaderManager.LoaderCallbacks;
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Loader;
29import android.database.Cursor;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -070030import android.graphics.drawable.Drawable;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070031import android.net.Uri;
32import android.os.Bundle;
33import android.os.CancellationSignal;
Steve McKayfad3d4a2015-09-22 15:09:21 -070034import android.support.annotation.Nullable;
35import android.support.v7.widget.LinearLayoutManager;
36import android.support.v7.widget.RecyclerView;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -070037import android.text.Spannable;
38import android.text.SpannableStringBuilder;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070039import android.text.TextUtils.TruncateAt;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -070040import android.text.style.ImageSpan;
Jeff Sharkey744dbbd2013-08-07 18:33:33 -070041import android.util.Log;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070042import android.view.LayoutInflater;
Steve McKayfad3d4a2015-09-22 15:09:21 -070043import android.view.MotionEvent;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070044import android.view.View;
45import android.view.ViewGroup;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070046import android.widget.ImageView;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070047import android.widget.TextView;
48
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070049import com.android.documentsui.RecentsProvider.RecentColumns;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070050import com.android.documentsui.model.DocumentStack;
Jeff Sharkey08293432013-09-25 14:39:14 -070051import com.android.documentsui.model.DurableUtils;
Jeff Sharkey38ec2522013-09-24 12:07:12 -070052import com.android.documentsui.model.RootInfo;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070053
54import libcore.io.IoUtils;
55
Jeff Sharkeyb3620442013-09-01 18:41:04 -070056import java.io.IOException;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070057import java.util.ArrayList;
Jeff Sharkey38ec2522013-09-24 12:07:12 -070058import java.util.Collection;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070059import java.util.List;
60
61/**
62 * Display directories where recent creates took place.
63 */
64public class RecentsCreateFragment extends Fragment {
65
Jeff Sharkey9dd02622013-09-27 16:44:11 -070066 private View mEmptyView;
Steve McKayfad3d4a2015-09-22 15:09:21 -070067 private RecyclerView mRecView;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070068 private DocumentStackAdapter mAdapter;
69 private LoaderCallbacks<List<DocumentStack>> mCallbacks;
70
71 private static final int LOADER_RECENTS = 3;
72
73 public static void show(FragmentManager fm) {
74 final RecentsCreateFragment fragment = new RecentsCreateFragment();
75 final FragmentTransaction ft = fm.beginTransaction();
76 ft.replace(R.id.container_directory, fragment);
77 ft.commitAllowingStateLoss();
78 }
79
80 @Override
81 public View onCreateView(
82 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
83 final Context context = inflater.getContext();
84
85 final View view = inflater.inflate(R.layout.fragment_directory, container, false);
86
Ben Kwa74e5d412016-02-10 07:46:35 -080087 mRecView = (RecyclerView) view.findViewById(R.id.dir_list);
Steve McKayfad3d4a2015-09-22 15:09:21 -070088 mRecView.setLayoutManager(new LinearLayoutManager(getContext()));
89 mRecView.addOnItemTouchListener(mItemListener);
90
Jeff Sharkey9dd02622013-09-27 16:44:11 -070091 mEmptyView = view.findViewById(android.R.id.empty);
92
Jeff Sharkey2e694f82013-08-06 16:26:14 -070093 mAdapter = new DocumentStackAdapter();
Steve McKayfad3d4a2015-09-22 15:09:21 -070094 mRecView.setAdapter(mAdapter);
Jeff Sharkey2e694f82013-08-06 16:26:14 -070095
Jeff Sharkey38ec2522013-09-24 12:07:12 -070096 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Steve McKayd0a2a2c2015-03-25 14:35:33 -070097 final State state = ((BaseActivity) getActivity()).getDisplayState();
Jeff Sharkey38ec2522013-09-24 12:07:12 -070098
Jeff Sharkey2e694f82013-08-06 16:26:14 -070099 mCallbacks = new LoaderCallbacks<List<DocumentStack>>() {
100 @Override
101 public Loader<List<DocumentStack>> onCreateLoader(int id, Bundle args) {
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700102 return new RecentsCreateLoader(context, roots, state);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700103 }
104
105 @Override
106 public void onLoadFinished(
107 Loader<List<DocumentStack>> loader, List<DocumentStack> data) {
Steve McKayfad3d4a2015-09-22 15:09:21 -0700108 mAdapter.update(data);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700109
110 // When launched into empty recents, show drawer
Ben Lina0c7c742016-04-13 16:33:20 -0700111 if (mAdapter.isEmpty() && !state.hasLocationChanged()
112 && state.action != ACTION_CREATE
113 && context instanceof DocumentsActivity) {
Steve McKayb68dd222015-04-20 17:18:15 -0700114 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700115 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700116 }
117
118 @Override
119 public void onLoaderReset(Loader<List<DocumentStack>> loader) {
Steve McKayfad3d4a2015-09-22 15:09:21 -0700120 mAdapter.update(null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700121 }
122 };
123
124 return view;
125 }
126
127 @Override
128 public void onStart() {
129 super.onStart();
130 getLoaderManager().restartLoader(LOADER_RECENTS, getArguments(), mCallbacks);
131 }
132
133 @Override
134 public void onStop() {
135 super.onStop();
136 getLoaderManager().destroyLoader(LOADER_RECENTS);
137 }
138
Steve McKayfad3d4a2015-09-22 15:09:21 -0700139 private RecyclerView.OnItemTouchListener mItemListener =
140 new RecyclerView.OnItemTouchListener() {
141 @Override
142 public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
143 Events.MotionInputEvent event = new Events.MotionInputEvent(e, mRecView);
144 if (event.isOverItem() && event.isActionUp()) {
145 final DocumentStack stack = mAdapter.getItem(event.getItemPosition());
146 ((BaseActivity) getActivity()).onStackPicked(stack);
147 return true;
148 }
149 return false;
150 }
151
152 @Override
153 public void onTouchEvent(RecyclerView rv, MotionEvent e) {}
154 @Override
155 public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}
156 };
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700157
Jeff Sharkey46899c82013-08-18 22:26:48 -0700158 public static class RecentsCreateLoader extends UriDerivativeLoader<Uri, List<DocumentStack>> {
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700159 private final RootsCache mRoots;
160 private final State mState;
161
162 public RecentsCreateLoader(Context context, RootsCache roots, State state) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700163 super(context, RecentsProvider.buildRecent());
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700164 mRoots = roots;
165 mState = state;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700166 }
167
168 @Override
169 public List<DocumentStack> loadInBackground(Uri uri, CancellationSignal signal) {
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700170 final Collection<RootInfo> matchingRoots = mRoots.getMatchingRootsBlocking(mState);
Steve McKayfefcd702015-08-20 16:19:38 +0000171 final ArrayList<DocumentStack> result = new ArrayList<>();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700172
173 final ContentResolver resolver = getContext().getContentResolver();
174 final Cursor cursor = resolver.query(
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700175 uri, null, null, null, RecentColumns.TIMESTAMP + " DESC", signal);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700176 try {
177 while (cursor != null && cursor.moveToNext()) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700178 final byte[] rawStack = cursor.getBlob(
179 cursor.getColumnIndex(RecentColumns.STACK));
Jeff Sharkey744dbbd2013-08-07 18:33:33 -0700180 try {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700181 final DocumentStack stack = new DocumentStack();
Jeff Sharkey08293432013-09-25 14:39:14 -0700182 DurableUtils.readFromArray(rawStack, stack);
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700183
184 // Only update root here to avoid spinning up all
185 // providers; we update the stack during the actual
186 // restore. This also filters away roots that don't
187 // match current filter.
188 stack.updateRoot(matchingRoots);
Jeff Sharkey744dbbd2013-08-07 18:33:33 -0700189 result.add(stack);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700190 } catch (IOException e) {
Jeff Sharkey744dbbd2013-08-07 18:33:33 -0700191 Log.w(TAG, "Failed to resolve stack: " + e);
192 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700193 }
194 } finally {
195 IoUtils.closeQuietly(cursor);
196 }
197
198 return result;
199 }
200 }
201
Steve McKayfad3d4a2015-09-22 15:09:21 -0700202 private static final class StackHolder extends RecyclerView.ViewHolder {
203 public View view;
204 public StackHolder(View view) {
205 super(view);
206 this.view = view;
207 }
208 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700209
Steve McKayfad3d4a2015-09-22 15:09:21 -0700210 private class DocumentStackAdapter extends RecyclerView.Adapter<StackHolder> {
211 @Nullable private List<DocumentStack> mItems;
212
213 DocumentStack getItem(int position) {
214 return mItems.get(position);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700215 }
216
Steve McKayfad3d4a2015-09-22 15:09:21 -0700217 @Override
218 public int getItemCount() {
219 return mItems == null ? 0 : mItems.size();
220 }
221
222 boolean isEmpty() {
223 return mItems == null ? true : mItems.isEmpty();
224 }
225
226 void update(@Nullable List<DocumentStack> items) {
227 mItems = items;
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700228
229 if (isEmpty()) {
230 mEmptyView.setVisibility(View.VISIBLE);
231 } else {
232 mEmptyView.setVisibility(View.GONE);
233 }
234
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700235 notifyDataSetChanged();
236 }
237
238 @Override
Steve McKayfad3d4a2015-09-22 15:09:21 -0700239 public StackHolder onCreateViewHolder(ViewGroup parent, int viewType) {
240 final Context context = parent.getContext();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700241
Steve McKayfad3d4a2015-09-22 15:09:21 -0700242 final LayoutInflater inflater = LayoutInflater.from(context);
243 return new StackHolder(
244 (View) inflater.inflate(R.layout.item_doc_list, parent, false));
245 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700246
Steve McKayfad3d4a2015-09-22 15:09:21 -0700247 @Override
248 public void onBindViewHolder(StackHolder holder, int position) {
249 Context context = getContext();
250 View view = holder.view;
251
252 final ImageView iconMime = (ImageView) view.findViewById(R.id.icon_mime);
253 final TextView title = (TextView) view.findViewById(android.R.id.title);
254 final View line2 = view.findViewById(R.id.line2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700255
256 final DocumentStack stack = getItem(position);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700257 iconMime.setImageDrawable(stack.root.loadIcon(context));
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700258
Alan Viverette61791442014-08-14 12:59:10 -0700259 final Drawable crumb = context.getDrawable(R.drawable.ic_breadcrumb_arrow);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700260 crumb.setBounds(0, 0, crumb.getIntrinsicWidth(), crumb.getIntrinsicHeight());
261
262 final SpannableStringBuilder builder = new SpannableStringBuilder();
263 builder.append(stack.root.title);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700264 for (int i = stack.size() - 2; i >= 0; i--) {
Jeff Sharkey40457802013-09-21 13:57:33 -0700265 appendDrawable(builder, crumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700266 builder.append(stack.get(i).displayName);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700267 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700268 title.setText(builder);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700269 title.setEllipsize(TruncateAt.MIDDLE);
270
Jeff Sharkey6a99ec82013-09-19 11:25:56 -0700271 if (line2 != null) line2.setVisibility(View.GONE);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700272 }
273 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700274
275 private static void appendDrawable(SpannableStringBuilder b, Drawable d) {
276 final int length = b.length();
277 b.append("\u232a");
278 b.setSpan(new ImageSpan(d), length, b.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
279 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700280}