summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/provider/SnapshotProvider.java
blob: 923613cb6d88d3907f8b2bfde8c73102bef1b02b (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Copyright (C) 2011 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.browser.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import com.android.browser.BrowserConfig;
import com.android.browser.platformsupport.BrowserContract;

import android.text.TextUtils;

import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;

public class SnapshotProvider extends ContentProvider {

    public static interface Snapshots {

        public static final Uri CONTENT_URI = Uri.withAppendedPath(
                SnapshotProvider.AUTHORITY_URI, "snapshots");
        public static final String _ID = "_id";
        @Deprecated
        public static final String VIEWSTATE = "view_state";
        public static final String BACKGROUND = "background";
        public static final String TITLE = "title";
        public static final String URL = "url";
        public static final String FAVICON = "favicon";
        public static final String THUMBNAIL = "thumbnail";
        public static final String DATE_CREATED = "date_created";
        public static final String VIEWSTATE_PATH = "viewstate_path";
        public static final String VIEWSTATE_SIZE = "viewstate_size";
    }

    public static final String AUTHORITY = BrowserConfig.AUTHORITY + ".snapshots";
    public static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY);

    static final String TABLE_SNAPSHOTS = "snapshots";
    static final int SNAPSHOTS = 10;
    static final int SNAPSHOTS_ID = 11;
    static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    // Workaround that we can't remove the "NOT NULL" constraint on VIEWSTATE
    static final byte[] NULL_BLOB_HACK = new byte[0];

    SnapshotDatabaseHelper mOpenHelper;

    static {
        URI_MATCHER.addURI(AUTHORITY, "snapshots", SNAPSHOTS);
        URI_MATCHER.addURI(AUTHORITY, "snapshots/#", SNAPSHOTS_ID);
    }

    final static class SnapshotDatabaseHelper extends SQLiteOpenHelper {

        static final String DATABASE_NAME = "snapshots.db";
        static final int DATABASE_VERSION = 3;

        public SnapshotDatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_SNAPSHOTS + "(" +
                    Snapshots._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    Snapshots.TITLE + " TEXT," +
                    Snapshots.URL + " TEXT NOT NULL," +
                    Snapshots.DATE_CREATED + " INTEGER," +
                    Snapshots.FAVICON + " BLOB," +
                    Snapshots.THUMBNAIL + " BLOB," +
                    Snapshots.BACKGROUND + " INTEGER," +
                    Snapshots.VIEWSTATE + " BLOB NOT NULL," +
                    Snapshots.VIEWSTATE_PATH + " TEXT," +
                    Snapshots.VIEWSTATE_SIZE + " INTEGER" +
                    ");");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if (oldVersion < 2) {
                db.execSQL("DROP TABLE " + TABLE_SNAPSHOTS);
                onCreate(db);
            }
            if (oldVersion < 3) {
                db.execSQL("ALTER TABLE " + TABLE_SNAPSHOTS + " ADD COLUMN "
                        + Snapshots.VIEWSTATE_PATH + " TEXT");
                db.execSQL("ALTER TABLE " + TABLE_SNAPSHOTS + " ADD COLUMN "
                        + Snapshots.VIEWSTATE_SIZE + " INTEGER");
                db.execSQL("UPDATE " + TABLE_SNAPSHOTS + " SET "
                        + Snapshots.VIEWSTATE_SIZE + " = length("
                        + Snapshots.VIEWSTATE + ")");
            }
        }

    }

    static File getOldDatabasePath(Context context) {
        File dir = context.getExternalFilesDir(null);
        return new File(dir, SnapshotDatabaseHelper.DATABASE_NAME);
    }

    private static boolean copyFile(File srcFile, File destFile) {
        try {
            if (destFile.exists()) {
                destFile.delete();
            }

            FileInputStream in = new FileInputStream(srcFile);
            FileOutputStream out = new FileOutputStream(destFile);

            try {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = in.read(buffer)) >= 0) {
                    out.write(buffer, 0, bytesRead);
                }
            } finally {
                out.flush();
                try {
                    out.getFD().sync();
                } catch (IOException e) {
                }
                in.close();
                out.close();
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    private void migrateToDataFolder() {
        File dbPath = getContext().getDatabasePath(SnapshotDatabaseHelper.DATABASE_NAME);
        if (dbPath.exists()) return;
        File oldPath = getOldDatabasePath(getContext());
        if (oldPath.exists()) {
            // Try to move
            if (!oldPath.renameTo(dbPath)) {
                // Failed, do a copy
                copyFile(oldPath, dbPath);
            }
            // Cleanup
            oldPath.delete();
        }
    }

    @Override
    public boolean onCreate() {
        migrateToDataFolder();
        mOpenHelper = new SnapshotDatabaseHelper(getContext());
        return true;
    }

    SQLiteDatabase getWritableDatabase() {
        return mOpenHelper.getWritableDatabase();
    }

    SQLiteDatabase getReadableDatabase() {
        return mOpenHelper.getReadableDatabase();
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = getReadableDatabase();
        if (db == null) {
            return null;
        }
        final int match = URI_MATCHER.match(uri);
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        String limit = uri.getQueryParameter(BrowserContract.PARAM_LIMIT);
        switch (match) {
        case SNAPSHOTS_ID:
            selection = DatabaseUtils.concatenateWhere(selection, "_id=?");
            selectionArgs = DatabaseUtils.appendSelectionArgs(selectionArgs,
                    new String[] { Long.toString(ContentUris.parseId(uri)) });
            // fall through
        case SNAPSHOTS:
            qb.setTables(TABLE_SNAPSHOTS);
            break;

        default:
            throw new UnsupportedOperationException("Unknown URL " + uri.toString());
        }
        Cursor cursor = qb.query(db, projection, selection, selectionArgs,
                null, null, sortOrder, limit);
        cursor.setNotificationUri(getContext().getContentResolver(),
                AUTHORITY_URI);
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = getWritableDatabase();
        if (db == null) {
            return null;
        }
        int match = URI_MATCHER.match(uri);
        long id = -1;
        switch (match) {
        case SNAPSHOTS:
            if (!values.containsKey(Snapshots.VIEWSTATE)) {
                values.put(Snapshots.VIEWSTATE, NULL_BLOB_HACK);
            }
            id = db.insert(TABLE_SNAPSHOTS, Snapshots.TITLE, values);
            break;
        default:
            throw new UnsupportedOperationException("Unknown insert URI " + uri);
        }
        if (id < 0) {
            return null;
        }
        Uri inserted = ContentUris.withAppendedId(uri, id);
        getContext().getContentResolver().notifyChange(inserted, null, false);
        return inserted;
    }

    static final String[] DELETE_PROJECTION = new String[] {
        Snapshots.VIEWSTATE_PATH,
    };
    private void deleteDataFiles(SQLiteDatabase db, String selection,
            String[] selectionArgs) {
        Cursor c = db.query(TABLE_SNAPSHOTS, DELETE_PROJECTION, selection,
                selectionArgs, null, null, null);
        final Context context = getContext();
        while (c.moveToNext()) {
            String filename = c.getString(0);
            if (TextUtils.isEmpty(filename)) {
                continue;
            }
            File f = context.getFileStreamPath(filename);
            if (f.exists()) {
                if (!f.delete()) {
                    f.deleteOnExit();
                }
            }
        }
        c.close();
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = getWritableDatabase();
        if (db == null) {
            return 0;
        }
        int match = URI_MATCHER.match(uri);
        int deleted = 0;
        switch (match) {
        case SNAPSHOTS_ID: {
            selection = DatabaseUtils.concatenateWhere(selection, TABLE_SNAPSHOTS + "._id=?");
            selectionArgs = DatabaseUtils.appendSelectionArgs(selectionArgs,
                    new String[] { Long.toString(ContentUris.parseId(uri)) });
            // fall through
        }
        case SNAPSHOTS:
            deleteDataFiles(db, selection, selectionArgs);
            deleted = db.delete(TABLE_SNAPSHOTS, selection, selectionArgs);
            break;
        default:
            throw new UnsupportedOperationException("Unknown delete URI " + uri);
        }
        if (deleted > 0) {
            getContext().getContentResolver().notifyChange(uri, null, false);
        }
        return deleted;
    }

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

}