1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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
.Log_OC
;
24 import com
.owncloud
.android
.R
;
25 import com
.owncloud
.android
.db
.ProviderMeta
;
26 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
30 import android
.content
.ContentProvider
;
31 import android
.content
.ContentUris
;
32 import android
.content
.ContentValues
;
33 import android
.content
.Context
;
34 import android
.content
.UriMatcher
;
35 import android
.database
.Cursor
;
36 import android
.database
.SQLException
;
37 import android
.database
.sqlite
.SQLiteDatabase
;
38 import android
.database
.sqlite
.SQLiteOpenHelper
;
39 import android
.database
.sqlite
.SQLiteQueryBuilder
;
40 import android
.net
.Uri
;
41 import android
.text
.TextUtils
;
44 * The ContentProvider for the ownCloud App.
46 * @author Bartek Przybylski
49 public class FileContentProvider
extends ContentProvider
{
51 private DataBaseHelper mDbHelper
;
53 private static HashMap
<String
, String
> mProjectionMap
;
55 mProjectionMap
= new HashMap
<String
, String
>();
56 mProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
);
57 mProjectionMap
.put(ProviderTableMeta
.FILE_PARENT
,
58 ProviderTableMeta
.FILE_PARENT
);
59 mProjectionMap
.put(ProviderTableMeta
.FILE_PATH
,
60 ProviderTableMeta
.FILE_PATH
);
61 mProjectionMap
.put(ProviderTableMeta
.FILE_NAME
,
62 ProviderTableMeta
.FILE_NAME
);
63 mProjectionMap
.put(ProviderTableMeta
.FILE_CREATION
,
64 ProviderTableMeta
.FILE_CREATION
);
65 mProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED
,
66 ProviderTableMeta
.FILE_MODIFIED
);
67 mProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
68 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
);
69 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
,
70 ProviderTableMeta
.FILE_CONTENT_LENGTH
);
71 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
,
72 ProviderTableMeta
.FILE_CONTENT_TYPE
);
73 mProjectionMap
.put(ProviderTableMeta
.FILE_STORAGE_PATH
,
74 ProviderTableMeta
.FILE_STORAGE_PATH
);
75 mProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
,
76 ProviderTableMeta
.FILE_LAST_SYNC_DATE
);
77 mProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
78 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
);
79 mProjectionMap
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
,
80 ProviderTableMeta
.FILE_KEEP_IN_SYNC
);
81 mProjectionMap
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
,
82 ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
85 private static final int SINGLE_FILE
= 1;
86 private static final int DIRECTORY
= 2;
87 private static final int ROOT_DIRECTORY
= 3;
89 private UriMatcher mUriMatcher
;
91 // mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
92 // mUriMatcher.addURI(ProviderMeta.AUTHORITY_FILES, null, ROOT_DIRECTORY);
93 // mUriMatcher.addURI(ProviderMeta.AUTHORITY_FILES, "file/", SINGLE_FILE);
94 // mUriMatcher.addURI(ProviderMeta.AUTHORITY_FILES, "file/#", SINGLE_FILE);
95 // mUriMatcher.addURI(ProviderMeta.AUTHORITY_FILES, "dir/#", DIRECTORY);
100 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
101 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
103 switch (mUriMatcher
.match(uri
)) {
105 count
= db
.delete(ProviderTableMeta
.DB_NAME
,
106 ProviderTableMeta
._ID
108 + uri
.getPathSegments().get(1)
109 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
110 + ")" : ""), whereArgs
);
113 count
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
);
116 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
118 getContext().getContentResolver().notifyChange(uri
, null
);
123 public String
getType(Uri uri
) {
124 switch (mUriMatcher
.match(uri
)) {
126 return ProviderTableMeta
.CONTENT_TYPE
;
128 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
130 throw new IllegalArgumentException("Unknown Uri id."
136 public Uri
insert(Uri uri
, ContentValues values
) {
137 if (mUriMatcher
.match(uri
) != SINGLE_FILE
&&
138 mUriMatcher
.match(uri
) != ROOT_DIRECTORY
) {
140 throw new IllegalArgumentException("Unknown uri id: " + uri
);
143 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
144 long rowId
= db
.insert(ProviderTableMeta
.DB_NAME
, null
, values
);
146 Uri insertedFileUri
= ContentUris
.withAppendedId(
147 ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
148 getContext().getContentResolver().notifyChange(insertedFileUri
,
150 return insertedFileUri
;
152 throw new SQLException("ERROR " + uri
);
156 public boolean onCreate() {
157 mDbHelper
= new DataBaseHelper(getContext());
159 String authority
= getContext().getResources().getString(R
.string
.authority
);
160 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
161 mUriMatcher
.addURI(authority
, null
, ROOT_DIRECTORY
);
162 mUriMatcher
.addURI(authority
, "file/", SINGLE_FILE
);
163 mUriMatcher
.addURI(authority
, "file/#", SINGLE_FILE
);
164 mUriMatcher
.addURI(authority
, "dir/#", DIRECTORY
);
170 public Cursor
query(Uri uri
, String
[] projection
, String selection
,
171 String
[] selectionArgs
, String sortOrder
) {
172 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
174 sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
);
175 sqlQuery
.setProjectionMap(mProjectionMap
);
177 switch (mUriMatcher
.match(uri
)) {
181 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
182 + uri
.getPathSegments().get(1));
185 if (uri
.getPathSegments().size() > 1) {
186 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
187 + uri
.getPathSegments().get(1));
191 throw new IllegalArgumentException("Unknown uri id: " + uri
);
195 if (TextUtils
.isEmpty(sortOrder
)) {
196 order
= ProviderTableMeta
.DEFAULT_SORT_ORDER
;
201 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
203 db
.execSQL("PRAGMA case_sensitive_like = true");
204 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
,
207 c
.setNotificationUri(getContext().getContentResolver(), uri
);
213 public int update(Uri uri
, ContentValues values
, String selection
,
214 String
[] selectionArgs
) {
215 return mDbHelper
.getWritableDatabase().update(
216 ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
219 class DataBaseHelper
extends SQLiteOpenHelper
{
221 public DataBaseHelper(Context context
) {
222 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
227 public void onCreate(SQLiteDatabase db
) {
229 Log_OC
.i("SQL", "Entering in onCreate");
230 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "("
231 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
232 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
233 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
234 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
235 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
236 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
237 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
238 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
239 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
240 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
241 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
242 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER, "
243 + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER, "
244 + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER );"
249 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
250 Log_OC
.i("SQL", "Entering in onUpgrade");
251 boolean upgraded
= false
;
252 if (oldVersion
== 1 && newVersion
>= 2) {
253 Log_OC
.i("SQL", "Entering in the #1 ADD in onUpgrade");
254 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
255 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
259 if (oldVersion
< 3 && newVersion
>= 3) {
260 Log_OC
.i("SQL", "Entering in the #2 ADD in onUpgrade");
261 db
.beginTransaction();
263 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
264 " ADD COLUMN " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER " +
267 // assume there are not local changes pending to upload
268 db
.execSQL("UPDATE " + ProviderTableMeta
.DB_NAME
+
269 " SET " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " = " + System
.currentTimeMillis() +
270 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
273 db
.setTransactionSuccessful();
278 if (oldVersion
< 4 && newVersion
>= 4) {
279 Log_OC
.i("SQL", "Entering in the #3 ADD in onUpgrade");
280 db
.beginTransaction();
282 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
283 " ADD COLUMN " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER " +
286 db
.execSQL("UPDATE " + ProviderTableMeta
.DB_NAME
+
287 " SET " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " = " + ProviderTableMeta
.FILE_MODIFIED
+
288 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
291 db
.setTransactionSuccessful();
297 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);