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
;
29 import android
.content
.ContentProvider
;
30 import android
.content
.ContentUris
;
31 import android
.content
.ContentValues
;
32 import android
.content
.Context
;
33 import android
.content
.UriMatcher
;
34 import android
.database
.Cursor
;
35 import android
.database
.SQLException
;
36 import android
.database
.sqlite
.SQLiteDatabase
;
37 import android
.database
.sqlite
.SQLiteOpenHelper
;
38 import android
.database
.sqlite
.SQLiteQueryBuilder
;
39 import android
.net
.Uri
;
40 import android
.text
.TextUtils
;
43 * The ContentProvider for the ownCloud App.
45 * @author Bartek Przybylski
48 public class FileContentProvider
extends ContentProvider
{
50 private DataBaseHelper mDbHelper
;
52 private static HashMap
<String
, String
> mProjectionMap
;
54 mProjectionMap
= new HashMap
<String
, String
>();
55 mProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
);
56 mProjectionMap
.put(ProviderTableMeta
.FILE_PARENT
,
57 ProviderTableMeta
.FILE_PARENT
);
58 mProjectionMap
.put(ProviderTableMeta
.FILE_PATH
,
59 ProviderTableMeta
.FILE_PATH
);
60 mProjectionMap
.put(ProviderTableMeta
.FILE_NAME
,
61 ProviderTableMeta
.FILE_NAME
);
62 mProjectionMap
.put(ProviderTableMeta
.FILE_CREATION
,
63 ProviderTableMeta
.FILE_CREATION
);
64 mProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED
,
65 ProviderTableMeta
.FILE_MODIFIED
);
66 mProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
67 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
);
68 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
,
69 ProviderTableMeta
.FILE_CONTENT_LENGTH
);
70 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
,
71 ProviderTableMeta
.FILE_CONTENT_TYPE
);
72 mProjectionMap
.put(ProviderTableMeta
.FILE_STORAGE_PATH
,
73 ProviderTableMeta
.FILE_STORAGE_PATH
);
74 mProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
,
75 ProviderTableMeta
.FILE_LAST_SYNC_DATE
);
76 mProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
77 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
);
78 mProjectionMap
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
,
79 ProviderTableMeta
.FILE_KEEP_IN_SYNC
);
80 mProjectionMap
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
,
81 ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
84 private static final int SINGLE_FILE
= 1;
85 private static final int DIRECTORY
= 2;
86 private static final int ROOT_DIRECTORY
= 3;
88 private UriMatcher mUriMatcher
;
90 // mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
91 // mUriMatcher.addURI(ProviderMeta.AUTHORITY_FILES, null, ROOT_DIRECTORY);
92 // mUriMatcher.addURI(ProviderMeta.AUTHORITY_FILES, "file/", SINGLE_FILE);
93 // mUriMatcher.addURI(ProviderMeta.AUTHORITY_FILES, "file/#", SINGLE_FILE);
94 // mUriMatcher.addURI(ProviderMeta.AUTHORITY_FILES, "dir/#", DIRECTORY);
99 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
100 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
102 switch (mUriMatcher
.match(uri
)) {
104 count
= db
.delete(ProviderTableMeta
.DB_NAME
,
105 ProviderTableMeta
._ID
107 + uri
.getPathSegments().get(1)
108 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
109 + ")" : ""), whereArgs
);
112 count
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
);
115 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
117 getContext().getContentResolver().notifyChange(uri
, null
);
122 public String
getType(Uri uri
) {
123 switch (mUriMatcher
.match(uri
)) {
125 return ProviderTableMeta
.CONTENT_TYPE
;
127 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
129 throw new IllegalArgumentException("Unknown Uri id."
135 public Uri
insert(Uri uri
, ContentValues values
) {
136 if (mUriMatcher
.match(uri
) != SINGLE_FILE
&&
137 mUriMatcher
.match(uri
) != ROOT_DIRECTORY
) {
139 throw new IllegalArgumentException("Unknown uri id: " + uri
);
142 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
143 long rowId
= db
.insert(ProviderTableMeta
.DB_NAME
, null
, values
);
145 Uri insertedFileUri
= ContentUris
.withAppendedId(
146 ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
147 getContext().getContentResolver().notifyChange(insertedFileUri
,
149 return insertedFileUri
;
151 throw new SQLException("ERROR " + uri
);
155 public boolean onCreate() {
156 mDbHelper
= new DataBaseHelper(getContext());
158 String authority
= getContext().getResources().getString(R
.string
.authority
);
159 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
160 mUriMatcher
.addURI(authority
, null
, ROOT_DIRECTORY
);
161 mUriMatcher
.addURI(authority
, "file/", SINGLE_FILE
);
162 mUriMatcher
.addURI(authority
, "file/#", SINGLE_FILE
);
163 mUriMatcher
.addURI(authority
, "dir/#", DIRECTORY
);
169 public Cursor
query(Uri uri
, String
[] projection
, String selection
,
170 String
[] selectionArgs
, String sortOrder
) {
171 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
173 sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
);
174 sqlQuery
.setProjectionMap(mProjectionMap
);
176 switch (mUriMatcher
.match(uri
)) {
180 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
181 + uri
.getPathSegments().get(1));
184 if (uri
.getPathSegments().size() > 1) {
185 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
186 + uri
.getPathSegments().get(1));
190 throw new IllegalArgumentException("Unknown uri id: " + uri
);
194 if (TextUtils
.isEmpty(sortOrder
)) {
195 order
= ProviderTableMeta
.DEFAULT_SORT_ORDER
;
200 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
202 db
.execSQL("PRAGMA case_sensitive_like = true");
203 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
,
206 c
.setNotificationUri(getContext().getContentResolver(), uri
);
212 public int update(Uri uri
, ContentValues values
, String selection
,
213 String
[] selectionArgs
) {
214 return mDbHelper
.getWritableDatabase().update(
215 ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
218 class DataBaseHelper
extends SQLiteOpenHelper
{
220 public DataBaseHelper(Context context
) {
221 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
226 public void onCreate(SQLiteDatabase db
) {
228 Log_OC
.i("SQL", "Entering in onCreate");
229 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "("
230 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
231 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
232 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
233 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
234 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
235 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
236 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
237 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
238 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
239 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
240 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
241 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER, "
242 + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER, "
243 + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER );"
248 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
249 Log_OC
.i("SQL", "Entering in onUpgrade");
250 boolean upgraded
= false
;
251 if (oldVersion
== 1 && newVersion
>= 2) {
252 Log_OC
.i("SQL", "Entering in the #1 ADD in onUpgrade");
253 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
254 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
258 if (oldVersion
< 3 && newVersion
>= 3) {
259 Log_OC
.i("SQL", "Entering in the #2 ADD in onUpgrade");
260 db
.beginTransaction();
262 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
263 " ADD COLUMN " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER " +
266 // assume there are not local changes pending to upload
267 db
.execSQL("UPDATE " + ProviderTableMeta
.DB_NAME
+
268 " SET " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " = " + System
.currentTimeMillis() +
269 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
272 db
.setTransactionSuccessful();
277 if (oldVersion
< 4 && newVersion
>= 4) {
278 Log_OC
.i("SQL", "Entering in the #3 ADD in onUpgrade");
279 db
.beginTransaction();
281 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
282 " ADD COLUMN " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER " +
285 db
.execSQL("UPDATE " + ProviderTableMeta
.DB_NAME
+
286 " SET " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " = " + ProviderTableMeta
.FILE_MODIFIED
+
287 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
290 db
.setTransactionSuccessful();
296 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);