1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.providers
;
21 import java
.util
.HashMap
;
23 import com
.owncloud
.android
.Log_OC
;
24 import com
.owncloud
.android
.db
.ProviderMeta
;
25 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
28 import android
.content
.ContentProvider
;
29 import android
.content
.ContentUris
;
30 import android
.content
.ContentValues
;
31 import android
.content
.Context
;
32 import android
.content
.UriMatcher
;
33 import android
.database
.Cursor
;
34 import android
.database
.SQLException
;
35 import android
.database
.sqlite
.SQLiteDatabase
;
36 import android
.database
.sqlite
.SQLiteOpenHelper
;
37 import android
.database
.sqlite
.SQLiteQueryBuilder
;
38 import android
.net
.Uri
;
39 import android
.text
.TextUtils
;
42 * The ContentProvider for the ownCloud App.
44 * @author Bartek Przybylski
47 public class FileContentProvider
extends ContentProvider
{
49 private DataBaseHelper mDbHelper
;
51 private static HashMap
<String
, String
> mProjectionMap
;
53 mProjectionMap
= new HashMap
<String
, String
>();
54 mProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
);
55 mProjectionMap
.put(ProviderTableMeta
.FILE_PARENT
,
56 ProviderTableMeta
.FILE_PARENT
);
57 mProjectionMap
.put(ProviderTableMeta
.FILE_PATH
,
58 ProviderTableMeta
.FILE_PATH
);
59 mProjectionMap
.put(ProviderTableMeta
.FILE_NAME
,
60 ProviderTableMeta
.FILE_NAME
);
61 mProjectionMap
.put(ProviderTableMeta
.FILE_CREATION
,
62 ProviderTableMeta
.FILE_CREATION
);
63 mProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED
,
64 ProviderTableMeta
.FILE_MODIFIED
);
65 mProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
66 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
);
67 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
,
68 ProviderTableMeta
.FILE_CONTENT_LENGTH
);
69 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
,
70 ProviderTableMeta
.FILE_CONTENT_TYPE
);
71 mProjectionMap
.put(ProviderTableMeta
.FILE_STORAGE_PATH
,
72 ProviderTableMeta
.FILE_STORAGE_PATH
);
73 mProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
,
74 ProviderTableMeta
.FILE_LAST_SYNC_DATE
);
75 mProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
76 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
);
77 mProjectionMap
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
,
78 ProviderTableMeta
.FILE_KEEP_IN_SYNC
);
79 mProjectionMap
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
,
80 ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
81 mProjectionMap
.put(ProviderTableMeta
.FILE_ETAG
,
82 ProviderTableMeta
.FILE_ETAG
);
85 private static final int SINGLE_FILE
= 1;
86 private static final int DIRECTORY
= 2;
87 private static final int ROOT_DIRECTORY
= 3;
88 private static final UriMatcher mUriMatcher
;
90 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
91 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, null
, ROOT_DIRECTORY
);
92 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/", SINGLE_FILE
);
93 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/#", SINGLE_FILE
);
94 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "dir/#", DIRECTORY
);
98 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
99 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
101 switch (mUriMatcher
.match(uri
)) {
103 count
= db
.delete(ProviderTableMeta
.DB_NAME
,
104 ProviderTableMeta
._ID
106 + uri
.getPathSegments().get(1)
107 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
108 + ")" : ""), whereArgs
);
111 count
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
);
114 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
116 getContext().getContentResolver().notifyChange(uri
, null
);
121 public String
getType(Uri uri
) {
122 switch (mUriMatcher
.match(uri
)) {
124 return ProviderTableMeta
.CONTENT_TYPE
;
126 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
128 throw new IllegalArgumentException("Unknown Uri id."
134 public Uri
insert(Uri uri
, ContentValues values
) {
135 if (mUriMatcher
.match(uri
) != SINGLE_FILE
&&
136 mUriMatcher
.match(uri
) != ROOT_DIRECTORY
) {
138 throw new IllegalArgumentException("Unknown uri id: " + uri
);
141 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
142 long rowId
= db
.insert(ProviderTableMeta
.DB_NAME
, null
, values
);
144 Uri insertedFileUri
= ContentUris
.withAppendedId(
145 ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
146 getContext().getContentResolver().notifyChange(insertedFileUri
,
148 return insertedFileUri
;
150 throw new SQLException("ERROR " + uri
);
154 public boolean onCreate() {
155 mDbHelper
= new DataBaseHelper(getContext());
160 public Cursor
query(Uri uri
, String
[] projection
, String selection
,
161 String
[] selectionArgs
, String sortOrder
) {
162 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
164 sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
);
165 sqlQuery
.setProjectionMap(mProjectionMap
);
167 switch (mUriMatcher
.match(uri
)) {
171 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
172 + uri
.getPathSegments().get(1));
175 if (uri
.getPathSegments().size() > 1) {
176 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
177 + uri
.getPathSegments().get(1));
181 throw new IllegalArgumentException("Unknown uri id: " + uri
);
185 if (TextUtils
.isEmpty(sortOrder
)) {
186 order
= ProviderTableMeta
.DEFAULT_SORT_ORDER
;
191 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
192 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
,
195 c
.setNotificationUri(getContext().getContentResolver(), uri
);
201 public int update(Uri uri
, ContentValues values
, String selection
,
202 String
[] selectionArgs
) {
203 return mDbHelper
.getWritableDatabase().update(
204 ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
207 class DataBaseHelper
extends SQLiteOpenHelper
{
209 public DataBaseHelper(Context context
) {
210 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
215 public void onCreate(SQLiteDatabase db
) {
217 Log_OC
.i("SQL", "Entering in onCreate");
218 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "("
219 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
220 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
221 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
222 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
223 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
224 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
225 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
226 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
227 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
228 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
229 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
230 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER, "
231 + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER, "
232 + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER, "
233 + ProviderTableMeta
.FILE_ETAG
+ " TEXT );"
238 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
239 Log_OC
.i("SQL", "Entering in onUpgrade");
240 boolean upgraded
= false
;
241 if (oldVersion
== 1 && newVersion
>= 2) {
242 Log_OC
.i("SQL", "Entering in the #1 ADD in onUpgrade");
243 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
244 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
248 if (oldVersion
< 3 && newVersion
>= 3) {
249 Log_OC
.i("SQL", "Entering in the #2 ADD in onUpgrade");
250 db
.beginTransaction();
252 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
253 " ADD COLUMN " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER " +
256 // assume there are not local changes pending to upload
257 db
.execSQL("UPDATE " + ProviderTableMeta
.DB_NAME
+
258 " SET " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " = " + System
.currentTimeMillis() +
259 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
262 db
.setTransactionSuccessful();
267 if (oldVersion
< 4 && newVersion
>= 4) {
268 Log_OC
.i("SQL", "Entering in the #3 ADD in onUpgrade");
269 db
.beginTransaction();
271 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
272 " ADD COLUMN " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER " +
275 db
.execSQL("UPDATE " + ProviderTableMeta
.DB_NAME
+
276 " SET " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " = " + ProviderTableMeta
.FILE_MODIFIED
+
277 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
280 db
.setTransactionSuccessful();
286 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);
288 if (oldVersion
< 5 && newVersion
>= 5) {
289 Log_OC
.i("SQL", "Entering in the #4 ADD in onUpgrade");
290 db
.beginTransaction();
292 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
293 " ADD COLUMN " + ProviderTableMeta
.FILE_ETAG
+ " TEXT " +
297 db
.setTransactionSuccessful();
303 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);