summaryrefslogtreecommitdiff
path: root/android/source/src/java/org/libreoffice/storage/external/ExternalFile.java
blob: aff33e4413efd7e498f236735fef09b46aca0642 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package org.libreoffice.storage.external;

import android.content.Context;
import android.support.v4.provider.DocumentFile;
import android.util.Log;

import org.libreoffice.storage.IFile;
import org.libreoffice.storage.IOUtils;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Implementation of IFile for the external file system, for Android 4.4+
 *
 * Uses the DocumentFile class.
 *
 * The DocumentFile class obfuscates the path of the files it wraps,
 * preventing usage of LOK's documentLoad method. A copy of the DocumentFile's contents
 * will be created in the cache when files are opened, allowing use of documentLoad.
 */
public class ExternalFile implements IFile{
    private final static String LOGTAG = "ExternalFile";

    private ExtsdDocumentsProvider provider;
    private DocumentFile docFile;
    private File duplicateFile;
    private Context context;

    public ExternalFile(ExtsdDocumentsProvider provider, DocumentFile docFile, Context context) {
        this.provider = provider;
        this.context = context;
        this.docFile = docFile;
    }

    @Override
    public URI getUri() {
        try{
            return new URI(docFile.toString());
        } catch (URISyntaxException e) {
            Log.e(LOGTAG, e.getMessage(), e.getCause());
            return null;
        }
    }

    @Override
    public String getName() {
        return docFile.getName();
    }

    @Override
    public boolean isDirectory() {
        return docFile.isDirectory();
    }

    @Override
    public long getSize() {
        return docFile.length();
    }

    @Override
    public Date getLastModified() {
        return new Date(docFile.lastModified());
    }

    @Override
    public List<IFile> listFiles() {
        List<IFile> children = new ArrayList<IFile>();
        for (DocumentFile child : docFile.listFiles()) {
            children.add(new ExternalFile(provider, child, context));
        }
        return children;
    }

    @Override
    public List<IFile> listFiles(FileFilter filter) {
        File file;
        try{
            List<IFile> children = new ArrayList<IFile>();
            for (DocumentFile child : docFile.listFiles()) {
                file = new File(new URI(child.getUri().toString()));
                if(filter.accept(file))
                    children.add(new ExternalFile(provider, child, context));
            }
            return children;

        }catch (Exception e){
            e.printStackTrace();
        }
        /* if something goes wrong */
        return listFiles();

    }

    @Override
    public IFile getParent(Context context) {
        // this is the root node
        if(docFile.getParentFile() == null) return null;

        return new ExternalFile(provider, docFile.getParentFile(), this.context);
    }

    @Override
    public File getDocument() {
        if(isDirectory()) {
            return null;
        } else {
            duplicateFile = duplicateInCache();
            return duplicateFile;
        }
    }

    private File duplicateInCache() {
        try{
            InputStream istream = context.getContentResolver().
                    openInputStream(docFile.getUri());

            File storageFolder = provider.getCacheDir();
            File fileCopy = new File(storageFolder, docFile.getName());
            OutputStream ostream = new FileOutputStream(fileCopy);

            IOUtils.copy(istream, ostream);
            return fileCopy;
        } catch (Exception e) {
            Log.e(LOGTAG, e.getMessage(), e.getCause());
            return null;
        }
    }

    @Override
    public void saveDocument(File file) {
        try{
            OutputStream ostream = context.getContentResolver().
                    openOutputStream(docFile.getUri());
            InputStream istream = new FileInputStream(file);

            IOUtils.copy(istream, ostream);

        } catch (Exception e) {
            Log.e(LOGTAG, e.getMessage(), e.getCause());
        }
    }

    @Override
    public boolean equals(Object object) {
        if (this == object)
            return true;
        if (!(object instanceof ExternalFile))
            return false;
        ExternalFile file = (ExternalFile) object;
        return file.getUri().equals(getUri());
    }

}