blob: 0e2762291b094b3fb3d537d8c8ac79a9f0f280d1 [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;
Jeff Sharkey744dbbd2013-08-07 18:33:33 -070020
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;
Steve McKayfad3d4a2015-09-22 15:09:21 -070033import android.support.annotation.Nullable;
34import android.support.v7.widget.LinearLayoutManager;
35import android.support.v7.widget.RecyclerView;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -070036import android.text.Spannable;
37import android.text.SpannableStringBuilder;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070038import android.text.TextUtils.TruncateAt;
Jeff Sharkeyaed873d2013-09-09 16:51:06 -070039import android.text.style.ImageSpan;
Jeff Sharkey744dbbd2013-08-07 18:33:33 -070040import android.util.Log;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070041import android.view.LayoutInflater;
Steve McKayfad3d4a2015-09-22 15:09:21 -070042import android.view.MotionEvent;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070043import android.view.View;
44import android.view.ViewGroup;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070045import android.widget.ImageView;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070046import 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 Sharkey08293432013-09-25 14:39:14 -070050import com.android.documentsui.model.DurableUtils;
Jeff Sharkey38ec2522013-09-24 12:07:12 -070051import com.android.documentsui.model.RootInfo;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070052
53import libcore.io.IoUtils;
54
Jeff Sharkeyb3620442013-09-01 18:41:04 -070055import java.io.IOException;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070056import java.util.ArrayList;
Jeff Sharkey38ec2522013-09-24 12:07:12 -070057import java.util.Collection;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070058import java.util.List;
59
60/**
61 * Display directories where recent creates took place.
62 */
63public class RecentsCreateFragment extends Fragment {
64
Jeff Sharkey9dd02622013-09-27 16:44:11 -070065 private View mEmptyView;
Steve McKayfad3d4a2015-09-22 15:09:21 -070066 private RecyclerView mRecView;
Jeff Sharkey2e694f82013-08-06 16:26:14 -070067 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
Ben Kwa74e5d412016-02-10 07:46:35 -080086 mRecView = (RecyclerView) view.findViewById(R.id.dir_list);
Steve McKayfad3d4a2015-09-22 15:09:21 -070087 mRecView.setLayoutManager(new LinearLayoutManager(getContext()));
88 mRecView.addOnItemTouchListener(mItemListener);
89
Jeff Sharkey9dd02622013-09-27 16:44:11 -070090 mEmptyView = view.findViewById(android.R.id.empty);
91
Jeff Sharkey2e694f82013-08-06 16:26:14 -070092 mAdapter = new DocumentStackAdapter();
Steve McKayfad3d4a2015-09-22 15:09:21 -070093 mRecView.setAdapter(mAdapter);
Jeff Sharkey2e694f82013-08-06 16:26:14 -070094
Jeff Sharkey38ec2522013-09-24 12:07:12 -070095 final RootsCache roots = DocumentsApplication.getRootsCache(context);
Steve McKayd0a2a2c2015-03-25 14:35:33 -070096 final State state = ((BaseActivity) getActivity()).getDisplayState();
Jeff Sharkey38ec2522013-09-24 12:07:12 -070097
Jeff Sharkey2e694f82013-08-06 16:26:14 -070098 mCallbacks = new LoaderCallbacks<List<DocumentStack>>() {
99 @Override
100 public Loader<List<DocumentStack>> onCreateLoader(int id, Bundle args) {
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700101 return new RecentsCreateLoader(context, roots, state);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700102 }
103
104 @Override
105 public void onLoadFinished(
106 Loader<List<DocumentStack>> loader, List<DocumentStack> data) {
Steve McKayfad3d4a2015-09-22 15:09:21 -0700107 mAdapter.update(data);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700108
109 // When launched into empty recents, show drawer
Daichi Hirono2806beb2016-01-07 15:29:12 +0900110 if (mAdapter.isEmpty() && !state.hasLocationChanged() &&
Steve McKayb68dd222015-04-20 17:18:15 -0700111 context instanceof DocumentsActivity) {
112 ((DocumentsActivity) context).setRootsDrawerOpen(true);
Jeff Sharkey25f10b32013-10-07 14:08:17 -0700113 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700114 }
115
116 @Override
117 public void onLoaderReset(Loader<List<DocumentStack>> loader) {
Steve McKayfad3d4a2015-09-22 15:09:21 -0700118 mAdapter.update(null);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700119 }
120 };
121
122 return view;
123 }
124
125 @Override
126 public void onStart() {
127 super.onStart();
128 getLoaderManager().restartLoader(LOADER_RECENTS, getArguments(), mCallbacks);
129 }
130
131 @Override
132 public void onStop() {
133 super.onStop();
134 getLoaderManager().destroyLoader(LOADER_RECENTS);
135 }
136
Steve McKayfad3d4a2015-09-22 15:09:21 -0700137 private RecyclerView.OnItemTouchListener mItemListener =
138 new RecyclerView.OnItemTouchListener() {
139 @Override
140 public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
141 Events.MotionInputEvent event = new Events.MotionInputEvent(e, mRecView);
142 if (event.isOverItem() && event.isActionUp()) {
143 final DocumentStack stack = mAdapter.getItem(event.getItemPosition());
144 ((BaseActivity) getActivity()).onStackPicked(stack);
145 return true;
146 }
147 return false;
148 }
149
150 @Override
151 public void onTouchEvent(RecyclerView rv, MotionEvent e) {}
152 @Override
153 public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}
154 };
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700155
Jeff Sharkey46899c82013-08-18 22:26:48 -0700156 public static class RecentsCreateLoader extends UriDerivativeLoader<Uri, List<DocumentStack>> {
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700157 private final RootsCache mRoots;
158 private final State mState;
159
160 public RecentsCreateLoader(Context context, RootsCache roots, State state) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700161 super(context, RecentsProvider.buildRecent());
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700162 mRoots = roots;
163 mState = state;
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700164 }
165
166 @Override
167 public List<DocumentStack> loadInBackground(Uri uri, CancellationSignal signal) {
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700168 final Collection<RootInfo> matchingRoots = mRoots.getMatchingRootsBlocking(mState);
Steve McKayfefcd702015-08-20 16:19:38 +0000169 final ArrayList<DocumentStack> result = new ArrayList<>();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700170
171 final ContentResolver resolver = getContext().getContentResolver();
172 final Cursor cursor = resolver.query(
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700173 uri, null, null, null, RecentColumns.TIMESTAMP + " DESC", signal);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700174 try {
175 while (cursor != null && cursor.moveToNext()) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700176 final byte[] rawStack = cursor.getBlob(
177 cursor.getColumnIndex(RecentColumns.STACK));
Jeff Sharkey744dbbd2013-08-07 18:33:33 -0700178 try {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700179 final DocumentStack stack = new DocumentStack();
Jeff Sharkey08293432013-09-25 14:39:14 -0700180 DurableUtils.readFromArray(rawStack, stack);
Jeff Sharkey38ec2522013-09-24 12:07:12 -0700181
182 // Only update root here to avoid spinning up all
183 // providers; we update the stack during the actual
184 // restore. This also filters away roots that don't
185 // match current filter.
186 stack.updateRoot(matchingRoots);
Jeff Sharkey744dbbd2013-08-07 18:33:33 -0700187 result.add(stack);
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700188 } catch (IOException e) {
Jeff Sharkey744dbbd2013-08-07 18:33:33 -0700189 Log.w(TAG, "Failed to resolve stack: " + e);
190 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700191 }
192 } finally {
193 IoUtils.closeQuietly(cursor);
194 }
195
196 return result;
197 }
198 }
199
Steve McKayfad3d4a2015-09-22 15:09:21 -0700200 private static final class StackHolder extends RecyclerView.ViewHolder {
201 public View view;
202 public StackHolder(View view) {
203 super(view);
204 this.view = view;
205 }
206 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700207
Steve McKayfad3d4a2015-09-22 15:09:21 -0700208 private class DocumentStackAdapter extends RecyclerView.Adapter<StackHolder> {
209 @Nullable private List<DocumentStack> mItems;
210
211 DocumentStack getItem(int position) {
212 return mItems.get(position);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700213 }
214
Steve McKayfad3d4a2015-09-22 15:09:21 -0700215 @Override
216 public int getItemCount() {
217 return mItems == null ? 0 : mItems.size();
218 }
219
220 boolean isEmpty() {
221 return mItems == null ? true : mItems.isEmpty();
222 }
223
224 void update(@Nullable List<DocumentStack> items) {
225 mItems = items;
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700226
227 if (isEmpty()) {
228 mEmptyView.setVisibility(View.VISIBLE);
229 } else {
230 mEmptyView.setVisibility(View.GONE);
231 }
232
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700233 notifyDataSetChanged();
234 }
235
236 @Override
Steve McKayfad3d4a2015-09-22 15:09:21 -0700237 public StackHolder onCreateViewHolder(ViewGroup parent, int viewType) {
238 final Context context = parent.getContext();
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700239
Steve McKayfad3d4a2015-09-22 15:09:21 -0700240 final LayoutInflater inflater = LayoutInflater.from(context);
241 return new StackHolder(
242 (View) inflater.inflate(R.layout.item_doc_list, parent, false));
243 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700244
Steve McKayfad3d4a2015-09-22 15:09:21 -0700245 @Override
246 public void onBindViewHolder(StackHolder holder, int position) {
247 Context context = getContext();
248 View view = holder.view;
249
250 final ImageView iconMime = (ImageView) view.findViewById(R.id.icon_mime);
251 final TextView title = (TextView) view.findViewById(android.R.id.title);
252 final View line2 = view.findViewById(R.id.line2);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700253
254 final DocumentStack stack = getItem(position);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700255 iconMime.setImageDrawable(stack.root.loadIcon(context));
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700256
Alan Viverette61791442014-08-14 12:59:10 -0700257 final Drawable crumb = context.getDrawable(R.drawable.ic_breadcrumb_arrow);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700258 crumb.setBounds(0, 0, crumb.getIntrinsicWidth(), crumb.getIntrinsicHeight());
259
260 final SpannableStringBuilder builder = new SpannableStringBuilder();
261 builder.append(stack.root.title);
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700262 for (int i = stack.size() - 2; i >= 0; i--) {
Jeff Sharkey40457802013-09-21 13:57:33 -0700263 appendDrawable(builder, crumb);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700264 builder.append(stack.get(i).displayName);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700265 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700266 title.setText(builder);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700267 title.setEllipsize(TruncateAt.MIDDLE);
268
Jeff Sharkey6a99ec82013-09-19 11:25:56 -0700269 if (line2 != null) line2.setVisibility(View.GONE);
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700270 }
271 }
Jeff Sharkeyaed873d2013-09-09 16:51:06 -0700272
273 private static void appendDrawable(SpannableStringBuilder b, Drawable d) {
274 final int length = b.length();
275 b.append("\u232a");
276 b.setSpan(new ImageSpan(d), length, b.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
277 }
Jeff Sharkey2e694f82013-08-06 16:26:14 -0700278}