diff options
Diffstat (limited to 'packages/DocumentsUI/src/com/android/documentsui/model')
4 files changed, 222 insertions, 41 deletions
diff --git a/packages/DocumentsUI/src/com/android/documentsui/model/DocumentInfo.java b/packages/DocumentsUI/src/com/android/documentsui/model/DocumentInfo.java index d571971..feccadc 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/model/DocumentInfo.java +++ b/packages/DocumentsUI/src/com/android/documentsui/model/DocumentInfo.java @@ -30,13 +30,19 @@ import com.android.documentsui.RecentsProvider; import libcore.io.IoUtils; +import java.io.DataInputStream; +import java.io.DataOutputStream; import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.ProtocolException; import java.util.Comparator; /** * Representation of a {@link Document}. */ -public class DocumentInfo { +public class DocumentInfo implements Durable { + private static final int VERSION_INIT = 1; + public Uri uri; public String mimeType; public String displayName; @@ -46,6 +52,55 @@ public class DocumentInfo { public long size; public int icon; + public DocumentInfo() { + reset(); + } + + @Override + public void reset() { + uri = null; + mimeType = null; + displayName = null; + lastModified = -1; + flags = 0; + summary = null; + size = -1; + icon = 0; + } + + @Override + public void read(DataInputStream in) throws IOException { + final int version = in.readInt(); + switch (version) { + case VERSION_INIT: + final String rawUri = DurableUtils.readNullableString(in); + uri = rawUri != null ? Uri.parse(rawUri) : null; + mimeType = DurableUtils.readNullableString(in); + displayName = DurableUtils.readNullableString(in); + lastModified = in.readLong(); + flags = in.readInt(); + summary = DurableUtils.readNullableString(in); + size = in.readLong(); + icon = in.readInt(); + break; + default: + throw new ProtocolException("Unknown version " + version); + } + } + + @Override + public void write(DataOutputStream out) throws IOException { + out.writeInt(VERSION_INIT); + DurableUtils.writeNullableString(out, uri.toString()); + DurableUtils.writeNullableString(out, mimeType); + DurableUtils.writeNullableString(out, displayName); + out.writeLong(lastModified); + out.writeInt(flags); + DurableUtils.writeNullableString(out, summary); + out.writeLong(size); + out.writeInt(icon); + } + public static DocumentInfo fromDirectoryCursor(Uri parent, Cursor cursor) { final DocumentInfo doc = new DocumentInfo(); final String authority = parent.getAuthority(); diff --git a/packages/DocumentsUI/src/com/android/documentsui/model/DocumentStack.java b/packages/DocumentsUI/src/com/android/documentsui/model/DocumentStack.java index b123a46..64631ab 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/model/DocumentStack.java +++ b/packages/DocumentsUI/src/com/android/documentsui/model/DocumentStack.java @@ -16,54 +16,20 @@ package com.android.documentsui.model; -import static com.android.documentsui.DocumentsActivity.TAG; -import static com.android.documentsui.model.DocumentInfo.asFileNotFoundException; - -import android.content.ContentResolver; -import android.net.Uri; -import android.util.Log; - import com.android.documentsui.RootsCache; -import org.json.JSONArray; -import org.json.JSONException; - -import java.io.FileNotFoundException; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.ProtocolException; import java.util.LinkedList; /** * Representation of a stack of {@link DocumentInfo}, usually the result of a * user-driven traversal. */ -public class DocumentStack extends LinkedList<DocumentInfo> { - - public static String serialize(DocumentStack stack) { - final JSONArray json = new JSONArray(); - for (int i = 0; i < stack.size(); i++) { - json.put(stack.get(i).uri); - } - return json.toString(); - } - - public static DocumentStack deserialize(ContentResolver resolver, String raw) - throws FileNotFoundException { - Log.d(TAG, "deserialize: " + raw); - - final DocumentStack stack = new DocumentStack(); - try { - final JSONArray json = new JSONArray(raw); - for (int i = 0; i < json.length(); i++) { - final Uri uri = Uri.parse(json.getString(i)); - final DocumentInfo doc = DocumentInfo.fromUri(resolver, uri); - stack.add(doc); - } - } catch (JSONException e) { - throw asFileNotFoundException(e); - } - - // TODO: handle roots that have gone missing - return stack; - } +public class DocumentStack extends LinkedList<DocumentInfo> implements Durable { + private static final int VERSION_INIT = 1; public RootInfo getRoot(RootsCache roots) { return roots.findRoot(getLast().uri); @@ -78,4 +44,37 @@ public class DocumentStack extends LinkedList<DocumentInfo> { return null; } } + + @Override + public void reset() { + clear(); + } + + @Override + public void read(DataInputStream in) throws IOException { + final int version = in.readInt(); + switch (version) { + case VERSION_INIT: + final int size = in.readInt(); + for (int i = 0; i < size; i++) { + final DocumentInfo doc = new DocumentInfo(); + doc.read(in); + add(doc); + } + break; + default: + throw new ProtocolException("Unknown version " + version); + } + } + + @Override + public void write(DataOutputStream out) throws IOException { + out.writeInt(VERSION_INIT); + final int size = size(); + out.writeInt(size); + for (int i = 0; i < size; i++) { + final DocumentInfo doc = get(i); + doc.write(out); + } + } } diff --git a/packages/DocumentsUI/src/com/android/documentsui/model/Durable.java b/packages/DocumentsUI/src/com/android/documentsui/model/Durable.java new file mode 100644 index 0000000..01633ed --- /dev/null +++ b/packages/DocumentsUI/src/com/android/documentsui/model/Durable.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.documentsui.model; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public interface Durable { + public void reset(); + public void read(DataInputStream in) throws IOException; + public void write(DataOutputStream out) throws IOException; +} diff --git a/packages/DocumentsUI/src/com/android/documentsui/model/DurableUtils.java b/packages/DocumentsUI/src/com/android/documentsui/model/DurableUtils.java new file mode 100644 index 0000000..214fb14 --- /dev/null +++ b/packages/DocumentsUI/src/com/android/documentsui/model/DurableUtils.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.documentsui.model; + +import static com.android.documentsui.DocumentsActivity.TAG; + +import android.os.BadParcelableException; +import android.os.Parcel; +import android.util.Log; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class DurableUtils { + public static <D extends Durable> byte[] writeToArray(D d) throws IOException { + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + d.write(new DataOutputStream(out)); + return out.toByteArray(); + } + + public static <D extends Durable> D readFromArray(byte[] data, D d) throws IOException { + final ByteArrayInputStream in = new ByteArrayInputStream(data); + d.reset(); + try { + d.read(new DataInputStream(in)); + } catch (IOException e) { + d.reset(); + throw e; + } + return d; + } + + public static <D extends Durable> byte[] writeToArrayOrNull(D d) { + try { + return writeToArray(d); + } catch (IOException e) { + Log.w(TAG, "Failed to write", e); + return null; + } + } + + public static <D extends Durable> D readFromArrayOrNull(byte[] data, D d) { + try { + return readFromArray(data, d); + } catch (IOException e) { + Log.w(TAG, "Failed to read", e); + return null; + } + } + + public static <D extends Durable> void writeToParcel(Parcel parcel, D d) { + try { + parcel.writeByteArray(writeToArray(d)); + } catch (IOException e) { + throw new BadParcelableException(e); + } + } + + public static <D extends Durable> D readFromParcel(Parcel parcel, D d) { + try { + return readFromArray(parcel.createByteArray(), d); + } catch (IOException e) { + throw new BadParcelableException(e); + } + } + + public static void writeNullableString(DataOutputStream out, String value) throws IOException { + if (value != null) { + out.write(1); + out.writeUTF(value); + } else { + out.write(0); + } + } + + public static String readNullableString(DataInputStream in) throws IOException { + if (in.read() != 0) { + return in.readUTF(); + } else { + return null; + } + } +} |