-
Notifications
You must be signed in to change notification settings - Fork 0
/
SafDocument.java
120 lines (103 loc) · 3.75 KB
/
SafDocument.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.example.safexample;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.DocumentsContract;
import android.provider.OpenableColumns;
import android.util.Log;
import com.google.common.base.Charsets;
import com.google.common.io.ByteSink;
import com.google.common.io.ByteSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class SafDocument implements Parcelable {
public static final Creator<SafDocument> CREATOR = new Creator<SafDocument>() {
@Override
public SafDocument createFromParcel(Parcel in) {
return new SafDocument(in);
}
@Override
public SafDocument[] newArray(int size) {
return new SafDocument[size];
}
};
private final Uri document;
public SafDocument(Uri document) {
this.document = document;
}
protected SafDocument(Parcel in) {
document = in.readParcelable(Uri.class.getClassLoader());
}
public String filename(ContentResolver context) {
try (Cursor cursor = context.query(document, null, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
}
String path = document.getPath();
int cut = path.lastIndexOf('/');
return cut != -1 ? path.substring(cut + 1) : path;
}
public SafDocument rename(ContentResolver context, String newFilename) {
Uri newLocation = DocumentsContract.renameDocument(context, document, newFilename);
return new SafDocument(newLocation);
}
public ByteSource source(final ContentResolver reader) {
return new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return reader.openInputStream(document);
}
};
}
public String read(ContentResolver reader) {
try {
return source(reader).asCharSource(Charsets.UTF_8).read();
} catch (IOException e) {
Log.w("grok", e);
try {
return tryAsFileDescriptor(reader);
} catch (IOException e2) {
throw new IllegalArgumentException("Cannot read from " + document, e2);
}
}
}
private String tryAsFileDescriptor(final ContentResolver resolver) throws IOException {
return new ByteSource() {
@Override
public InputStream openStream() throws IOException {
AssetFileDescriptor r = resolver.openAssetFileDescriptor(document, "r");
return new FileInputStream(r.getFileDescriptor());
}
}.asCharSource(Charsets.UTF_8).read();
}
public ByteSink sink(final ContentResolver destination) {
return new ByteSink() {
@Override
public OutputStream openStream() throws IOException {
return destination.openOutputStream(document);
}
};
}
public void write(final ContentResolver writer, String newContent) {
try {
ByteSource.wrap(newContent.getBytes()).copyTo(sink(writer));
} catch (IOException e) {
throw new IllegalArgumentException("Cannot write to " + document, e);
}
}
@Override
public int describeContents() {
return document.describeContents();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
document.writeToParcel(dest, flags);
}
}