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
);
77 private static final int SINGLE_FILE
= 1;
78 private static final int DIRECTORY
= 2;
79 private static final int ROOT_DIRECTORY
= 3;
80 private static final UriMatcher mUriMatcher
;
82 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
83 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "/", ROOT_DIRECTORY
);
84 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/", SINGLE_FILE
);
85 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/#", SINGLE_FILE
);
86 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "dir/#", DIRECTORY
);
90 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
91 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
93 switch (mUriMatcher
.match(uri
)) {
95 count
= db
.delete(ProviderTableMeta
.DB_NAME
,
98 + uri
.getPathSegments().get(1)
99 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
100 + ")" : ""), whereArgs
);
103 count
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
);
106 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
108 getContext().getContentResolver().notifyChange(uri
, null
);
113 public String
getType(Uri uri
) {
114 switch (mUriMatcher
.match(uri
)) {
116 return ProviderTableMeta
.CONTENT_TYPE
;
118 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
120 throw new IllegalArgumentException("Unknown Uri id."
126 public Uri
insert(Uri uri
, ContentValues values
) {
127 if (mUriMatcher
.match(uri
) != SINGLE_FILE
&&
128 mUriMatcher
.match(uri
) != ROOT_DIRECTORY
) {
130 throw new IllegalArgumentException("Unknown uri id: " + uri
);
133 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
134 long rowId
= db
.insert(ProviderTableMeta
.DB_NAME
, null
, values
);
136 Uri insertedFileUri
= ContentUris
.withAppendedId(
137 ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
138 getContext().getContentResolver().notifyChange(insertedFileUri
,
140 return insertedFileUri
;
142 throw new SQLException("ERROR " + uri
);
146 public boolean onCreate() {
147 mDbHelper
= new DataBaseHelper(getContext());
152 public Cursor
query(Uri uri
, String
[] projection
, String selection
,
153 String
[] selectionArgs
, String sortOrder
) {
154 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
156 sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
);
157 sqlQuery
.setProjectionMap(mProjectionMap
);
159 switch (mUriMatcher
.match(uri
)) {
163 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
164 + uri
.getPathSegments().get(1));
167 if (uri
.getPathSegments().size() > 1) {
168 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
169 + uri
.getPathSegments().get(1));
173 throw new IllegalArgumentException("Unknown uri id: " + uri
);
177 if (TextUtils
.isEmpty(sortOrder
)) {
178 order
= ProviderTableMeta
.DEFAULT_SORT_ORDER
;
183 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
184 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
,
187 c
.setNotificationUri(getContext().getContentResolver(), uri
);
193 public int update(Uri uri
, ContentValues values
, String selection
,
194 String
[] selectionArgs
) {
195 return mDbHelper
.getWritableDatabase().update(
196 ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
199 class DataBaseHelper
extends SQLiteOpenHelper
{
201 public DataBaseHelper(Context context
) {
202 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
207 public void onCreate(SQLiteDatabase db
) {
209 Log
.i("SQL", "Entering in onCreate");
210 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "("
211 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
212 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
213 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
214 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
215 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
216 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
217 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
218 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
219 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
220 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
221 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
222 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER );");
226 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
227 Log
.i("SQL", "Entering in onUpgrade");
228 if (oldVersion
== 1 && newVersion
>= 2) {
229 Log
.i("SQL", "Entering in the ADD in onUpgrade");
230 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
231 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
233 } else Log
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);