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
;
39 public class cp
extends ContentProvider
{
41 private DataBaseHelper mDbHelper
;
43 private static HashMap
<String
, String
> mProjectionMap
;
45 mProjectionMap
= new HashMap
<String
, String
>();
46 mProjectionMap
.put(ProviderTableMeta
._ID
,
47 ProviderTableMeta
._ID
);
48 mProjectionMap
.put(ProviderTableMeta
.FILE_PARENT
,
49 ProviderTableMeta
.FILE_PARENT
);
50 mProjectionMap
.put(ProviderTableMeta
.FILE_PATH
,
51 ProviderTableMeta
.FILE_PATH
);
52 mProjectionMap
.put(ProviderTableMeta
.FILE_NAME
,
53 ProviderTableMeta
.FILE_NAME
);
54 mProjectionMap
.put(ProviderTableMeta
.FILE_CREATION
,
55 ProviderTableMeta
.FILE_CREATION
);
56 mProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED
,
57 ProviderTableMeta
.FILE_MODIFIED
);
58 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
,
59 ProviderTableMeta
.FILE_CONTENT_LENGTH
);
60 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
,
61 ProviderTableMeta
.FILE_CONTENT_TYPE
);
62 mProjectionMap
.put(ProviderTableMeta
.FILE_STORAGE_PATH
,
63 ProviderTableMeta
.FILE_STORAGE_PATH
);
66 private static final int SINGLE_FILE
= 1;
67 private static final int DIRECTORY
= 2;
68 private static final int ROOT_DIRECTORY
= 3;
69 private static final UriMatcher mUriMatcher
;
71 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
72 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "/", ROOT_DIRECTORY
);
73 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/", SINGLE_FILE
);
74 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/#", SINGLE_FILE
);
75 mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "dir/#", DIRECTORY
);
78 private static final String TAG
= "OC_ContentProvider";
81 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
82 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
84 switch (mUriMatcher
.match(uri
)) {
86 count
= db
.delete(ProviderTableMeta
.DB_NAME
,
87 ProviderTableMeta
._ID
+ "=" + uri
.getPathSegments().get(1)
88 + (!TextUtils
.isEmpty(where
)?
" AND (" + where
+")" : ""),
92 count
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
);
95 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
97 getContext().getContentResolver().notifyChange(uri
, null
);
102 public String
getType(Uri uri
) {
103 switch (mUriMatcher
.match(uri
)) {
105 return ProviderTableMeta
.CONTENT_TYPE
;
107 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
109 throw new IllegalArgumentException("Unknown Uri id." + uri
.toString());
114 public Uri
insert(Uri uri
, ContentValues values
) {
115 if (mUriMatcher
.match(uri
) != SINGLE_FILE
) {
116 throw new IllegalArgumentException("Unknown uri id: " + uri
);
119 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
120 long rowId
= db
.insert(ProviderTableMeta
.DB_NAME
, ProviderTableMeta
.FILE_NAME
, values
);
122 Uri insertedFileUri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
123 getContext().getContentResolver().notifyChange(insertedFileUri
, null
);
124 return insertedFileUri
;
126 throw new SQLException("ERROR " + uri
);
130 public boolean onCreate() {
131 mDbHelper
= new DataBaseHelper(getContext());
136 public Cursor
query(Uri uri
, String
[] projection
, String selection
,
137 String
[] selectionArgs
, String sortOrder
) {
138 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
140 sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
);
141 sqlQuery
.setProjectionMap(mProjectionMap
);
143 switch (mUriMatcher
.match(uri
)) {
145 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ " is null");
148 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="+uri
.getPathSegments().get(1));
151 if (uri
.getPathSegments().size() > 1) {
152 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "=" +
153 uri
.getPathSegments().get(1));
157 throw new IllegalArgumentException("Unknown uri id: " + uri
);
161 if (TextUtils
.isEmpty(sortOrder
)) {
162 order
= ProviderTableMeta
.DEFAULT_SORT_ORDER
;
167 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
168 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
, null
, null
, order
);
170 c
.setNotificationUri(getContext().getContentResolver(), uri
);
176 public int update(Uri uri
, ContentValues values
, String selection
,
177 String
[] selectionArgs
) {
178 return mDbHelper
.getWritableDatabase().update(ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
181 class DataBaseHelper
extends SQLiteOpenHelper
{
183 public DataBaseHelper(Context context
) {
184 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
189 public void onCreate(SQLiteDatabase db
) {
190 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "(" +
191 ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, " +
192 ProviderTableMeta
.FILE_NAME
+ " TEXT, " +
193 ProviderTableMeta
.FILE_PATH
+ " TEXT, " +
194 ProviderTableMeta
.FILE_PARENT
+ " INTEGER, " +
195 ProviderTableMeta
.FILE_CREATION
+ " INTEGER, " +
196 ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, " +
197 ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, " +
198 ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, " +
199 ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, " +
200 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT);");
204 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {