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
;
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.
41 * @author Bartek Przybylski
44 public class cp
extends ContentProvider
{
46 private DataBaseHelper mDbHelper
;
48 private static HashMap
<String
, String
> mProjectionMap
;
50 mProjectionMap
= new HashMap
<String
, String
>();
51 mProjectionMap
.put(ProviderTableMeta
._ID
,
52 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
);
83 private static final String TAG
= "OC_ContentProvider";
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
,
92 ProviderTableMeta
._ID
+ "=" + uri
.getPathSegments().get(1)
93 + (!TextUtils
.isEmpty(where
)?
" AND (" + where
+")" : ""),
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." + uri
.toString());
119 public Uri
insert(Uri uri
, ContentValues values
) {
120 if (mUriMatcher
.match(uri
) != SINGLE_FILE
) {
121 throw new IllegalArgumentException("Unknown uri id: " + uri
);
124 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
125 long rowId
= db
.insert(ProviderTableMeta
.DB_NAME
, ProviderTableMeta
.FILE_NAME
, values
);
127 Uri insertedFileUri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
128 getContext().getContentResolver().notifyChange(insertedFileUri
, null
);
129 return insertedFileUri
;
131 throw new SQLException("ERROR " + uri
);
135 public boolean onCreate() {
136 mDbHelper
= new DataBaseHelper(getContext());
141 public Cursor
query(Uri uri
, String
[] projection
, String selection
,
142 String
[] selectionArgs
, String sortOrder
) {
143 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
145 sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
);
146 sqlQuery
.setProjectionMap(mProjectionMap
);
148 switch (mUriMatcher
.match(uri
)) {
150 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ " is null");
153 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="+uri
.getPathSegments().get(1));
156 if (uri
.getPathSegments().size() > 1) {
157 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "=" +
158 uri
.getPathSegments().get(1));
162 throw new IllegalArgumentException("Unknown uri id: " + uri
);
166 if (TextUtils
.isEmpty(sortOrder
)) {
167 order
= ProviderTableMeta
.DEFAULT_SORT_ORDER
;
172 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
173 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
, null
, null
, order
);
175 c
.setNotificationUri(getContext().getContentResolver(), uri
);
181 public int update(Uri uri
, ContentValues values
, String selection
,
182 String
[] selectionArgs
) {
183 return mDbHelper
.getWritableDatabase().update(ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
186 class DataBaseHelper
extends SQLiteOpenHelper
{
188 public DataBaseHelper(Context context
) {
189 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
194 public void onCreate(SQLiteDatabase db
) {
195 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "(" +
196 ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, " +
197 ProviderTableMeta
.FILE_NAME
+ " TEXT, " +
198 ProviderTableMeta
.FILE_PATH
+ " TEXT, " +
199 ProviderTableMeta
.FILE_PARENT
+ " INTEGER, " +
200 ProviderTableMeta
.FILE_CREATION
+ " INTEGER, " +
201 ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, " +
202 ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, " +
203 ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, " +
204 ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, " +
205 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT);");
209 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {