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();
193 db
.execSQL("PRAGMA case_sensitive_like = true");
194 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
,
197 c
.setNotificationUri(getContext().getContentResolver(), uri
);
203 public int update(Uri uri
, ContentValues values
, String selection
,
204 String
[] selectionArgs
) {
205 return mDbHelper
.getWritableDatabase().update(
206 ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
209 class DataBaseHelper
extends SQLiteOpenHelper
{
211 public DataBaseHelper(Context context
) {
212 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
217 public void onCreate(SQLiteDatabase db
) {
219 Log_OC
.i("SQL", "Entering in onCreate");
220 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "("
221 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
222 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
223 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
224 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
225 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
226 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
227 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
228 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
229 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
230 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
231 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
232 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER, "
233 + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER, "
234 + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER, "
235 + ProviderTableMeta
.FILE_ETAG
+ " TEXT );"
240 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
241 Log_OC
.i("SQL", "Entering in onUpgrade");
242 boolean upgraded
= false
;
243 if (oldVersion
== 1 && newVersion
>= 2) {
244 Log_OC
.i("SQL", "Entering in the #1 ADD in onUpgrade");
245 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
246 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
250 if (oldVersion
< 3 && newVersion
>= 3) {
251 Log_OC
.i("SQL", "Entering in the #2 ADD in onUpgrade");
252 db
.beginTransaction();
254 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
255 " ADD COLUMN " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER " +
258 // assume there are not local changes pending to upload
259 db
.execSQL("UPDATE " + ProviderTableMeta
.DB_NAME
+
260 " SET " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " = " + System
.currentTimeMillis() +
261 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
264 db
.setTransactionSuccessful();
269 if (oldVersion
< 4 && newVersion
>= 4) {
270 Log_OC
.i("SQL", "Entering in the #3 ADD in onUpgrade");
271 db
.beginTransaction();
273 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
274 " ADD COLUMN " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER " +
277 db
.execSQL("UPDATE " + ProviderTableMeta
.DB_NAME
+
278 " SET " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " = " + ProviderTableMeta
.FILE_MODIFIED
+
279 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
282 db
.setTransactionSuccessful();
288 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);
290 if (oldVersion
< 5 && newVersion
>= 5) {
291 Log_OC
.i("SQL", "Entering in the #4 ADD in onUpgrade");
292 db
.beginTransaction();
294 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
295 " ADD COLUMN " + ProviderTableMeta
.FILE_ETAG
+ " TEXT " +
299 db
.setTransactionSuccessful();
305 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);