OSDN Git Service

Instance state, fix sharing, Durable objects.
[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 com.android.documentsui.RootsCache;
20
21 import java.io.DataInputStream;
22 import java.io.DataOutputStream;
23 import java.io.IOException;
24 import java.net.ProtocolException;
25 import java.util.LinkedList;
26
27 /**
28  * Representation of a stack of {@link DocumentInfo}, usually the result of a
29  * user-driven traversal.
30  */
31 public class DocumentStack extends LinkedList<DocumentInfo> implements Durable {
32     private static final int VERSION_INIT = 1;
33
34     public RootInfo getRoot(RootsCache roots) {
35         return roots.findRoot(getLast().uri);
36     }
37
38     public String getTitle(RootsCache roots) {
39         if (size() == 1) {
40             return getRoot(roots).title;
41         } else if (size() > 1) {
42             return peek().displayName;
43         } else {
44             return null;
45         }
46     }
47
48     @Override
49     public void reset() {
50         clear();
51     }
52
53     @Override
54     public void read(DataInputStream in) throws IOException {
55         final int version = in.readInt();
56         switch (version) {
57             case VERSION_INIT:
58                 final int size = in.readInt();
59                 for (int i = 0; i < size; i++) {
60                     final DocumentInfo doc = new DocumentInfo();
61                     doc.read(in);
62                     add(doc);
63                 }
64                 break;
65             default:
66                 throw new ProtocolException("Unknown version " + version);
67         }
68     }
69
70     @Override
71     public void write(DataOutputStream out) throws IOException {
72         out.writeInt(VERSION_INIT);
73         final int size = size();
74         out.writeInt(size);
75         for (int i = 0; i < size; i++) {
76             final DocumentInfo doc = get(i);
77             doc.write(out);
78         }
79     }
80 }