2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.documentsui.model;
19 import static com.android.documentsui.DocumentsActivity.TAG;
20 import static com.android.documentsui.model.DocumentInfo.asFileNotFoundException;
22 import android.content.ContentResolver;
23 import android.net.Uri;
24 import android.util.Log;
26 import com.android.documentsui.RootsCache;
28 import org.json.JSONArray;
29 import org.json.JSONException;
31 import java.io.FileNotFoundException;
32 import java.util.LinkedList;
35 * Representation of a stack of {@link DocumentInfo}, usually the result of a
36 * user-driven traversal.
38 public class DocumentStack extends LinkedList<DocumentInfo> {
40 public static String serialize(DocumentStack stack) {
41 final JSONArray json = new JSONArray();
42 for (int i = 0; i < stack.size(); i++) {
43 json.put(stack.get(i).uri);
45 return json.toString();
48 public static DocumentStack deserialize(ContentResolver resolver, String raw)
49 throws FileNotFoundException {
50 Log.d(TAG, "deserialize: " + raw);
52 final DocumentStack stack = new DocumentStack();
54 final JSONArray json = new JSONArray(raw);
55 for (int i = 0; i < json.length(); i++) {
56 final Uri uri = Uri.parse(json.getString(i));
57 final DocumentInfo doc = DocumentInfo.fromUri(resolver, uri);
60 } catch (JSONException e) {
61 throw asFileNotFoundException(e);
64 // TODO: handle roots that have gone missing
68 public RootInfo getRoot(RootsCache roots) {
69 return roots.findRoot(getLast().uri);
72 public String getTitle(RootsCache roots) {
74 return getRoot(roots).title;
75 } else if (size() > 1) {
76 return peek().displayName;