OSDN Git Service

b123a46f40c5314aa45cc3d2cab15a74a25ecb9a
[android-x86/frameworks-base.git] / packages / DocumentsUI / src / com / android / documentsui / model / DocumentStack.java
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
17 package com.android.documentsui.model;
18
19 import static com.android.documentsui.DocumentsActivity.TAG;
20 import static com.android.documentsui.model.DocumentInfo.asFileNotFoundException;
21
22 import android.content.ContentResolver;
23 import android.net.Uri;
24 import android.util.Log;
25
26 import com.android.documentsui.RootsCache;
27
28 import org.json.JSONArray;
29 import org.json.JSONException;
30
31 import java.io.FileNotFoundException;
32 import java.util.LinkedList;
33
34 /**
35  * Representation of a stack of {@link DocumentInfo}, usually the result of a
36  * user-driven traversal.
37  */
38 public class DocumentStack extends LinkedList<DocumentInfo> {
39
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);
44         }
45         return json.toString();
46     }
47
48     public static DocumentStack deserialize(ContentResolver resolver, String raw)
49             throws FileNotFoundException {
50         Log.d(TAG, "deserialize: " + raw);
51
52         final DocumentStack stack = new DocumentStack();
53         try {
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);
58                 stack.add(doc);
59             }
60         } catch (JSONException e) {
61             throw asFileNotFoundException(e);
62         }
63
64         // TODO: handle roots that have gone missing
65         return stack;
66     }
67
68     public RootInfo getRoot(RootsCache roots) {
69         return roots.findRoot(getLast().uri);
70     }
71
72     public String getTitle(RootsCache roots) {
73         if (size() == 1) {
74             return getRoot(roots).title;
75         } else if (size() > 1) {
76             return peek().displayName;
77         } else {
78             return null;
79         }
80     }
81 }