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
);
69 mProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
,
70 ProviderTableMeta
.FILE_LAST_SYNC_DATE
);
73 private static final int SINGLE_FILE
= 1;
74 private static final int DIRECTORY
= 2;
75 private static final int ROOT_DIRECTORY
= 3;
76 private static final UriMatcher mUriMatcher
;
78 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
79 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "/", ROOT_DIRECTORY
);
80 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/", SINGLE_FILE
);
81 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/#", SINGLE_FILE
);
82 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "dir/#", DIRECTORY
);
86 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
87 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
89 switch (mUriMatcher
.match(uri
)) {
91 count
= db
.delete(ProviderTableMeta
.DB_NAME
,
94 + uri
.getPathSegments().get(1)
95 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
96 + ")" : ""), whereArgs
);
99 count
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
);
102 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
104 getContext().getContentResolver().notifyChange(uri
, null
);
109 public String
getType(Uri uri
) {
110 switch (mUriMatcher
.match(uri
)) {
112 return ProviderTableMeta
.CONTENT_TYPE
;
114 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
116 throw new IllegalArgumentException("Unknown Uri id."
122 public Uri
insert(Uri uri
, ContentValues values
) {
123 if (mUriMatcher
.match(uri
) != SINGLE_FILE
) {
124 throw new IllegalArgumentException("Unknown uri id: " + uri
);
127 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
128 long rowId
= db
.insert(ProviderTableMeta
.DB_NAME
, null
, values
);
130 Uri insertedFileUri
= ContentUris
.withAppendedId(
131 ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
132 getContext().getContentResolver().notifyChange(insertedFileUri
,
134 return insertedFileUri
;
136 throw new SQLException("ERROR " + uri
);
140 public boolean onCreate() {
141 mDbHelper
= new DataBaseHelper(getContext());
146 public Cursor
query(Uri uri
, String
[] projection
, String selection
,
147 String
[] selectionArgs
, String sortOrder
) {
148 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
150 sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
);
151 sqlQuery
.setProjectionMap(mProjectionMap
);
153 switch (mUriMatcher
.match(uri
)) {
157 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
158 + uri
.getPathSegments().get(1));
161 if (uri
.getPathSegments().size() > 1) {
162 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
163 + uri
.getPathSegments().get(1));
167 throw new IllegalArgumentException("Unknown uri id: " + uri
);
171 if (TextUtils
.isEmpty(sortOrder
)) {
172 order
= ProviderTableMeta
.DEFAULT_SORT_ORDER
;
177 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
178 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
,
181 c
.setNotificationUri(getContext().getContentResolver(), uri
);
187 public int update(Uri uri
, ContentValues values
, String selection
,
188 String
[] selectionArgs
) {
189 return mDbHelper
.getWritableDatabase().update(
190 ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
193 class DataBaseHelper
extends SQLiteOpenHelper
{
195 public DataBaseHelper(Context context
) {
196 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
201 public void onCreate(SQLiteDatabase db
) {
203 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "("
204 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
205 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
206 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
207 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
208 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
209 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
210 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
211 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
212 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
213 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
214 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER );");
218 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {