1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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
.db
.ProviderMeta
;
24 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
27 import android
.content
.ContentProvider
;
28 import android
.content
.ContentUris
;
29 import android
.content
.ContentValues
;
30 import android
.content
.Context
;
31 import android
.content
.UriMatcher
;
32 import android
.database
.Cursor
;
33 import android
.database
.SQLException
;
34 import android
.database
.sqlite
.SQLiteDatabase
;
35 import android
.database
.sqlite
.SQLiteOpenHelper
;
36 import android
.database
.sqlite
.SQLiteQueryBuilder
;
37 import android
.net
.Uri
;
38 import android
.text
.TextUtils
;
39 import android
.util
.Log
;
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_CONTENT_LENGTH
,
66 ProviderTableMeta
.FILE_CONTENT_LENGTH
);
67 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
,
68 ProviderTableMeta
.FILE_CONTENT_TYPE
);
69 mProjectionMap
.put(ProviderTableMeta
.FILE_STORAGE_PATH
,
70 ProviderTableMeta
.FILE_STORAGE_PATH
);
71 mProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
,
72 ProviderTableMeta
.FILE_LAST_SYNC_DATE
);
73 mProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
74 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
);
75 mProjectionMap
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
,
76 ProviderTableMeta
.FILE_KEEP_IN_SYNC
);
77 mProjectionMap
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
,
78 ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
81 private static final int SINGLE_FILE
= 1;
82 private static final int DIRECTORY
= 2;
83 private static final int ROOT_DIRECTORY
= 3;
84 private static final UriMatcher mUriMatcher
;
86 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
87 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "/", ROOT_DIRECTORY
);
88 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/", SINGLE_FILE
);
89 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/#", SINGLE_FILE
);
90 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "dir/#", DIRECTORY
);
94 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
95 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
97 switch (mUriMatcher
.match(uri
)) {
99 count
= db
.delete(ProviderTableMeta
.DB_NAME
,
100 ProviderTableMeta
._ID
102 + uri
.getPathSegments().get(1)
103 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
104 + ")" : ""), whereArgs
);
107 count
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
);
110 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
112 getContext().getContentResolver().notifyChange(uri
, null
);
117 public String
getType(Uri uri
) {
118 switch (mUriMatcher
.match(uri
)) {
120 return ProviderTableMeta
.CONTENT_TYPE
;
122 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
124 throw new IllegalArgumentException("Unknown Uri id."
130 public Uri
insert(Uri uri
, ContentValues values
) {
131 if (mUriMatcher
.match(uri
) != SINGLE_FILE
&&
132 mUriMatcher
.match(uri
) != ROOT_DIRECTORY
) {
134 throw new IllegalArgumentException("Unknown uri id: " + uri
);
137 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
138 long rowId
= db
.insert(ProviderTableMeta
.DB_NAME
, null
, values
);
140 Uri insertedFileUri
= ContentUris
.withAppendedId(
141 ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
142 getContext().getContentResolver().notifyChange(insertedFileUri
,
144 return insertedFileUri
;
146 throw new SQLException("ERROR " + uri
);
150 public boolean onCreate() {
151 mDbHelper
= new DataBaseHelper(getContext());
156 public Cursor
query(Uri uri
, String
[] projection
, String selection
,
157 String
[] selectionArgs
, String sortOrder
) {
158 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
160 sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
);
161 sqlQuery
.setProjectionMap(mProjectionMap
);
163 switch (mUriMatcher
.match(uri
)) {
167 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
168 + uri
.getPathSegments().get(1));
171 if (uri
.getPathSegments().size() > 1) {
172 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
173 + uri
.getPathSegments().get(1));
177 throw new IllegalArgumentException("Unknown uri id: " + uri
);
181 if (TextUtils
.isEmpty(sortOrder
)) {
182 order
= ProviderTableMeta
.DEFAULT_SORT_ORDER
;
187 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
188 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
,
191 c
.setNotificationUri(getContext().getContentResolver(), uri
);
197 public int update(Uri uri
, ContentValues values
, String selection
,
198 String
[] selectionArgs
) {
199 return mDbHelper
.getWritableDatabase().update(
200 ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
203 class DataBaseHelper
extends SQLiteOpenHelper
{
205 public DataBaseHelper(Context context
) {
206 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
211 public void onCreate(SQLiteDatabase db
) {
213 Log
.i("SQL", "Entering in onCreate");
214 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "("
215 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
216 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
217 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
218 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
219 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
220 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
221 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
222 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
223 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
224 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
225 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
226 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER, "
227 + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER );"
232 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
233 Log
.i("SQL", "Entering in onUpgrade");
234 boolean upgraded
= false
;
235 if (oldVersion
== 1 && newVersion
>= 2) {
236 Log
.i("SQL", "Entering in the #1 ADD in onUpgrade");
237 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
238 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
242 if (oldVersion
< 3 && newVersion
>= 3) {
243 Log
.i("SQL", "Entering in the #2 ADD in onUpgrade");
244 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
245 " ADD COLUMN " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER " +
250 Log
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);