aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/TemporaryStorageProvider.java
blob: 87c0cc0a6610077fb2a7e78bcc7d044d26722db6 (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
164
165
166
167
168
169
170
171
/*
 * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sufficientlysecure.keychain.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.OpenableColumns;

import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.util.DatabaseUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class TemporaryStorageProvider extends ContentProvider {

    private static final String DB_NAME = "tempstorage.db";
    private static final String TABLE_FILES = "files";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_TIME = "time";
    private static final Uri BASE_URI = Uri.parse("content://org.sufficientlysecure.keychain.tempstorage/");
    private static final int DB_VERSION = 1;

    public static Uri createFile(Context context, String targetName) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_NAME, targetName);
        return context.getContentResolver().insert(BASE_URI, contentValues);
    }

    public static int cleanUp(Context context) {
        return context.getContentResolver().delete(BASE_URI, COLUMN_TIME + "< ?",
                new String[]{Long.toString(System.currentTimeMillis() - Constants.TEMPFILE_TTL)});
    }

    private class TemporaryStorageDatabase extends SQLiteOpenHelper {

        public TemporaryStorageDatabase(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_FILES + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_NAME + " TEXT, " +
                    COLUMN_TIME + " INTEGER" +
                    ");");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }

    private TemporaryStorageDatabase db;

    private File getFile(Uri uri) throws FileNotFoundException {
        try {
            return getFile(Integer.parseInt(uri.getLastPathSegment()));
        } catch (NumberFormatException e) {
            throw new FileNotFoundException();
        }
    }

    private File getFile(int id) {
        return new File(getContext().getCacheDir(), "temp/" + id);
    }

    @Override
    public boolean onCreate() {
        db = new TemporaryStorageDatabase(getContext());
        return new File(getContext().getCacheDir(), "temp").mkdirs();
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        File file;
        try {
            file = getFile(uri);
        } catch (FileNotFoundException e) {
            return null;
        }
        Cursor fileName = db.getReadableDatabase().query(TABLE_FILES, new String[]{COLUMN_NAME}, COLUMN_ID + "=?",
                new String[]{uri.getLastPathSegment()}, null, null, null);
        if (fileName != null) {
            if (fileName.moveToNext()) {
                MatrixCursor cursor =
                        new MatrixCursor(new String[]{OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE, "_data"});
                cursor.newRow().add(fileName.getString(0)).add(file.length()).add(file.getAbsolutePath());
                fileName.close();
                return cursor;
            }
            fileName.close();
        }
        return null;
    }

    @Override
    public String getType(Uri uri) {
        // Note: If we can find a files mime type, we can decrypt it to temp storage and open it after
        //       encryption. The mime type is needed, else UI really sucks and some apps break.
        return "*/*";
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        if (!values.containsKey(COLUMN_TIME)) {
            values.put(COLUMN_TIME, System.currentTimeMillis());
        }
        int insert = (int) db.getWritableDatabase().insert(TABLE_FILES, null, values);
        try {
            getFile(insert).createNewFile();
        } catch (IOException e) {
            return null;
        }
        return Uri.withAppendedPath(BASE_URI, Long.toString(insert));
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        if (uri.getLastPathSegment() != null) {
            selection = DatabaseUtil.concatenateWhere(selection, COLUMN_ID + "=?");
            selectionArgs = DatabaseUtil.appendSelectionArgs(selectionArgs, new String[]{uri.getLastPathSegment()});
        }
        Cursor files = db.getReadableDatabase().query(TABLE_FILES, new String[]{COLUMN_ID}, selection,
                selectionArgs, null, null, null);
        if (files != null) {
            while (files.moveToNext()) {
                getFile(files.getInt(0)).delete();
            }
            files.close();
            return db.getWritableDatabase().delete(TABLE_FILES, selection, selectionArgs);
        }
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        throw new UnsupportedOperationException("Update not supported");
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        return openFileHelper(uri, mode);
    }
}