1/*
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
19import static com.android.documentsui.DocumentsActivity.TAG;
20
21import 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;
29import android.graphics.drawable.Drawable;
30import android.net.Uri;
31import android.os.Bundle;
32import android.os.CancellationSignal;
33import android.text.Spannable;
34import android.text.SpannableStringBuilder;
35import android.text.TextUtils.TruncateAt;
36import android.text.style.ImageSpan;
37import android.util.Log;
38import 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
48import com.android.documentsui.DocumentsActivity.State;
49import com.android.documentsui.RecentsProvider.RecentColumns;
50import com.android.documentsui.model.DocumentStack;
51import com.android.documentsui.model.DurableUtils;
52import com.android.documentsui.model.RootInfo;
53import com.google.android.collect.Lists;
54
55import libcore.io.IoUtils;
56
57import java.io.IOException;
58import java.util.ArrayList;
59import java.util.Collection;
60import java.util.List;
61
62/**
63 * Display directories where recent creates took place.
64 */
65public class RecentsCreateFragment extends Fragment {
66
67    private View mEmptyView;
68    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
89        mEmptyView = view.findViewById(android.R.id.empty);
90
91        mListView = (ListView) view.findViewById(R.id.list);
92        mListView.setOnItemClickListener(mItemListener);
93
94        mAdapter = new DocumentStackAdapter();
95        mListView.setAdapter(mAdapter);
96
97        final RootsCache roots = DocumentsApplication.getRootsCache(context);
98        final State state = ((DocumentsActivity) getActivity()).getDisplayState();
99
100        mCallbacks = new LoaderCallbacks<List<DocumentStack>>() {
101            @Override
102            public Loader<List<DocumentStack>> onCreateLoader(int id, Bundle args) {
103                return new RecentsCreateLoader(context, roots, state);
104            }
105
106            @Override
107            public void onLoadFinished(
108                    Loader<List<DocumentStack>> loader, List<DocumentStack> data) {
109                mAdapter.swapStacks(data);
110
111                // When launched into empty recents, show drawer
112                if (mAdapter.isEmpty() && !state.stackTouched) {
113                    ((DocumentsActivity) context).setRootsDrawerOpen(true);
114                }
115            }
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
146    public static class RecentsCreateLoader extends UriDerivativeLoader<Uri, List<DocumentStack>> {
147        private final RootsCache mRoots;
148        private final State mState;
149
150        public RecentsCreateLoader(Context context, RootsCache roots, State state) {
151            super(context, RecentsProvider.buildRecent());
152            mRoots = roots;
153            mState = state;
154        }
155
156        @Override
157        public List<DocumentStack> loadInBackground(Uri uri, CancellationSignal signal) {
158            final Collection<RootInfo> matchingRoots = mRoots.getMatchingRootsBlocking(mState);
159            final ArrayList<DocumentStack> result = Lists.newArrayList();
160
161            final ContentResolver resolver = getContext().getContentResolver();
162            final Cursor cursor = resolver.query(
163                    uri, null, null, null, RecentColumns.TIMESTAMP + " DESC", signal);
164            try {
165                while (cursor != null && cursor.moveToNext()) {
166                    final byte[] rawStack = cursor.getBlob(
167                            cursor.getColumnIndex(RecentColumns.STACK));
168                    try {
169                        final DocumentStack stack = new DocumentStack();
170                        DurableUtils.readFromArray(rawStack, stack);
171
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);
177                        result.add(stack);
178                    } catch (IOException e) {
179                        Log.w(TAG, "Failed to resolve stack: " + e);
180                    }
181                }
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;
198
199            if (isEmpty()) {
200                mEmptyView.setVisibility(View.VISIBLE);
201            } else {
202                mEmptyView.setVisibility(View.GONE);
203            }
204
205            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
217            final ImageView iconMime = (ImageView) convertView.findViewById(R.id.icon_mime);
218            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
219            final View line2 = convertView.findViewById(R.id.line2);
220
221            final DocumentStack stack = getItem(position);
222            iconMime.setImageDrawable(stack.root.loadIcon(context));
223
224            final Drawable crumb = context.getDrawable(R.drawable.ic_breadcrumb_arrow);
225            crumb.setBounds(0, 0, crumb.getIntrinsicWidth(), crumb.getIntrinsicHeight());
226
227            final SpannableStringBuilder builder = new SpannableStringBuilder();
228            builder.append(stack.root.title);
229            for (int i = stack.size() - 2; i >= 0; i--) {
230                appendDrawable(builder, crumb);
231                builder.append(stack.get(i).displayName);
232            }
233            title.setText(builder);
234            title.setEllipsize(TruncateAt.MIDDLE);
235
236            if (line2 != null) line2.setVisibility(View.GONE);
237
238            return convertView;
239        }
240
241        @Override
242        public int getCount() {
243            return mStacks != null ? mStacks.size() : 0;
244        }
245
246        @Override
247        public DocumentStack getItem(int position) {
248            return mStacks.get(position);
249        }
250
251        @Override
252        public long getItemId(int position) {
253            return getItem(position).hashCode();
254        }
255    }
256
257    private static void appendDrawable(SpannableStringBuilder b, Drawable d) {
258        final int length = b.length();
259        b.append("\u232a");
260        b.setSpan(new ImageSpan(d), length, b.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
261    }
262}
263