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 eu
.alefzero
.owncloud
.providers
;
21 import java
.util
.HashMap
;
23 import eu
.alefzero
.owncloud
.db
.ProviderMeta
;
24 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
26 import android
.content
.ContentProvider
;
27 import android
.content
.ContentUris
;
28 import android
.content
.ContentValues
;
29 import android
.content
.Context
;
30 import android
.content
.UriMatcher
;
31 import android
.database
.Cursor
;
32 import android
.database
.SQLException
;
33 import android
.database
.sqlite
.SQLiteDatabase
;
34 import android
.database
.sqlite
.SQLiteOpenHelper
;
35 import android
.database
.sqlite
.SQLiteQueryBuilder
;
36 import android
.net
.Uri
;
37 import android
.text
.TextUtils
;
40 * The ContentProvider for the ownCloud App.
42 * @author Bartek Przybylski
45 public class FileContentProvider
extends ContentProvider
{
47 private DataBaseHelper mDbHelper
;
49 private static HashMap
<String
, String
> mProjectionMap
;
51 mProjectionMap
= new HashMap
<String
, String
>();
52 mProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
);
53 mProjectionMap
.put(ProviderTableMeta
.FILE_PARENT
,
54 ProviderTableMeta
.FILE_PARENT
);
55 mProjectionMap
.put(ProviderTableMeta
.FILE_PATH
,
56 ProviderTableMeta
.FILE_PATH
);
57 mProjectionMap
.put(ProviderTableMeta
.FILE_NAME
,
58 ProviderTableMeta
.FILE_NAME
);
59 mProjectionMap
.put(ProviderTableMeta
.FILE_CREATION
,
60 ProviderTableMeta
.FILE_CREATION
);
61 mProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED
,
62 ProviderTableMeta
.FILE_MODIFIED
);
63 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
,
64 ProviderTableMeta
.FILE_CONTENT_LENGTH
);
65 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
,
66 ProviderTableMeta
.FILE_CONTENT_TYPE
);
67 mProjectionMap
.put(ProviderTableMeta
.FILE_STORAGE_PATH
,
68 ProviderTableMeta
.FILE_STORAGE_PATH
);
71 private static final int SINGLE_FILE
= 1;
72 private static final int DIRECTORY
= 2;
73 private static final int ROOT_DIRECTORY
= 3;
74 private static final UriMatcher mUriMatcher
;
76 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
77 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "/", ROOT_DIRECTORY
);
78 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/", SINGLE_FILE
);
79 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/#", SINGLE_FILE
);
80 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "dir/#", DIRECTORY
);
84 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
85 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
87 switch (mUriMatcher
.match(uri
)) {
89 count
= db
.delete(ProviderTableMeta
.DB_NAME
,
92 + uri
.getPathSegments().get(1)
93 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
94 + ")" : ""), whereArgs
);
97 count
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
);
100 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
102 getContext().getContentResolver().notifyChange(uri
, null
);
107 public String
getType(Uri uri
) {
108 switch (mUriMatcher
.match(uri
)) {
110 return ProviderTableMeta
.CONTENT_TYPE
;
112 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
114 throw new IllegalArgumentException("Unknown Uri id."
120 public Uri
insert(Uri uri
, ContentValues values
) {
121 if (mUriMatcher
.match(uri
) != SINGLE_FILE
) {
122 throw new IllegalArgumentException("Unknown uri id: " + uri
);
125 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
126 long rowId
= db
.insert(ProviderTableMeta
.DB_NAME
, null
, values
);
128 Uri insertedFileUri
= ContentUris
.withAppendedId(
129 ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
130 getContext().getContentResolver().notifyChange(insertedFileUri
,
132 return insertedFileUri
;
134 throw new SQLException("ERROR " + uri
);
138 public boolean onCreate() {
139 mDbHelper
= new DataBaseHelper(getContext());
144 public Cursor
query(Uri uri
, String
[] projection
, String selection
,
145 String
[] selectionArgs
, String sortOrder
) {
146 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
148 sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
);
149 sqlQuery
.setProjectionMap(mProjectionMap
);
151 switch (mUriMatcher
.match(uri
)) {
155 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
156 + uri
.getPathSegments().get(1));
159 if (uri
.getPathSegments().size() > 1) {
160 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
161 + uri
.getPathSegments().get(1));
165 throw new IllegalArgumentException("Unknown uri id: " + uri
);
169 if (TextUtils
.isEmpty(sortOrder
)) {
170 order
= ProviderTableMeta
.DEFAULT_SORT_ORDER
;
175 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
176 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
,
179 c
.setNotificationUri(getContext().getContentResolver(), uri
);
185 public int update(Uri uri
, ContentValues values
, String selection
,
186 String
[] selectionArgs
) {
187 return mDbHelper
.getWritableDatabase().update(
188 ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
191 class DataBaseHelper
extends SQLiteOpenHelper
{
193 public DataBaseHelper(Context context
) {
194 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
199 public void onCreate(SQLiteDatabase db
) {
200 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "("
201 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
202 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
203 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
204 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
205 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
206 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
207 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
208 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
209 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
210 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT);");
214 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {