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_KEEP_IN_SYNC
,
74 ProviderTableMeta
.FILE_KEEP_IN_SYNC
);
75 mProjectionMap
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
,
76 ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
79 private static final int SINGLE_FILE
= 1;
80 private static final int DIRECTORY
= 2;
81 private static final int ROOT_DIRECTORY
= 3;
82 private static final UriMatcher mUriMatcher
;
84 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
85 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "/", ROOT_DIRECTORY
);
86 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/", SINGLE_FILE
);
87 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/#", SINGLE_FILE
);
88 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "dir/#", DIRECTORY
);
92 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
93 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
95 switch (mUriMatcher
.match(uri
)) {
97 count
= db
.delete(ProviderTableMeta
.DB_NAME
,
100 + uri
.getPathSegments().get(1)
101 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
102 + ")" : ""), whereArgs
);
105 count
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
);
108 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
110 getContext().getContentResolver().notifyChange(uri
, null
);
115 public String
getType(Uri uri
) {
116 switch (mUriMatcher
.match(uri
)) {
118 return ProviderTableMeta
.CONTENT_TYPE
;
120 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
122 throw new IllegalArgumentException("Unknown Uri id."
128 public Uri
insert(Uri uri
, ContentValues values
) {
129 if (mUriMatcher
.match(uri
) != SINGLE_FILE
&&
130 mUriMatcher
.match(uri
) != ROOT_DIRECTORY
) {
132 throw new IllegalArgumentException("Unknown uri id: " + uri
);
135 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
136 long rowId
= db
.insert(ProviderTableMeta
.DB_NAME
, null
, values
);
138 Uri insertedFileUri
= ContentUris
.withAppendedId(
139 ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
140 getContext().getContentResolver().notifyChange(insertedFileUri
,
142 return insertedFileUri
;
144 throw new SQLException("ERROR " + uri
);
148 public boolean onCreate() {
149 mDbHelper
= new DataBaseHelper(getContext());
154 public Cursor
query(Uri uri
, String
[] projection
, String selection
,
155 String
[] selectionArgs
, String sortOrder
) {
156 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
158 sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
);
159 sqlQuery
.setProjectionMap(mProjectionMap
);
161 switch (mUriMatcher
.match(uri
)) {
165 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
166 + uri
.getPathSegments().get(1));
169 if (uri
.getPathSegments().size() > 1) {
170 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
171 + uri
.getPathSegments().get(1));
175 throw new IllegalArgumentException("Unknown uri id: " + uri
);
179 if (TextUtils
.isEmpty(sortOrder
)) {
180 order
= ProviderTableMeta
.DEFAULT_SORT_ORDER
;
185 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
186 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
,
189 c
.setNotificationUri(getContext().getContentResolver(), uri
);
195 public int update(Uri uri
, ContentValues values
, String selection
,
196 String
[] selectionArgs
) {
197 return mDbHelper
.getWritableDatabase().update(
198 ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
201 class DataBaseHelper
extends SQLiteOpenHelper
{
203 public DataBaseHelper(Context context
) {
204 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
209 public void onCreate(SQLiteDatabase db
) {
211 Log
.i("SQL", "Entering in onCreate");
212 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "("
213 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
214 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
215 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
216 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
217 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
218 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
219 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
220 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
221 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
222 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
223 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
224 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER );");
228 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
229 Log
.i("SQL", "Entering in onUpgrade");
230 if (oldVersion
== 1 && newVersion
>= 2) {
231 Log
.i("SQL", "Entering in the ADD in onUpgrade");
232 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
233 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
235 } else Log
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);