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
; 
  38 import android
.util
.Log
; 
  41  * The ContentProvider for the ownCloud App. 
  43  * @author Bartek Przybylski 
  46 public class FileContentProvider 
extends ContentProvider 
{ 
  48     private DataBaseHelper mDbHelper
; 
  50     private static HashMap
<String
, String
> mProjectionMap
; 
  52         mProjectionMap 
= new HashMap
<String
, String
>(); 
  53         mProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
); 
  54         mProjectionMap
.put(ProviderTableMeta
.FILE_PARENT
, 
  55                 ProviderTableMeta
.FILE_PARENT
); 
  56         mProjectionMap
.put(ProviderTableMeta
.FILE_PATH
, 
  57                 ProviderTableMeta
.FILE_PATH
); 
  58         mProjectionMap
.put(ProviderTableMeta
.FILE_NAME
, 
  59                 ProviderTableMeta
.FILE_NAME
); 
  60         mProjectionMap
.put(ProviderTableMeta
.FILE_CREATION
, 
  61                 ProviderTableMeta
.FILE_CREATION
); 
  62         mProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED
, 
  63                 ProviderTableMeta
.FILE_MODIFIED
); 
  64         mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 
  65                 ProviderTableMeta
.FILE_CONTENT_LENGTH
); 
  66         mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, 
  67                 ProviderTableMeta
.FILE_CONTENT_TYPE
); 
  68         mProjectionMap
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, 
  69                 ProviderTableMeta
.FILE_STORAGE_PATH
); 
  70         mProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, 
  71                 ProviderTableMeta
.FILE_LAST_SYNC_DATE
); 
  72         mProjectionMap
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, 
  73                 ProviderTableMeta
.FILE_KEEP_IN_SYNC
); 
  76     private static final int SINGLE_FILE 
= 1; 
  77     private static final int DIRECTORY 
= 2; 
  78     private static final int ROOT_DIRECTORY 
= 3; 
  79     private static final UriMatcher mUriMatcher
; 
  81         mUriMatcher 
= new UriMatcher(UriMatcher
.NO_MATCH
); 
  82         mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "/", ROOT_DIRECTORY
); 
  83         mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/", SINGLE_FILE
); 
  84         mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "file/#", SINGLE_FILE
); 
  85         mUriMatcher
.addURI(ProviderMeta
.AUTHORITY_FILES
, "dir/#", DIRECTORY
); 
  89     public int delete(Uri uri
, String where
, String
[] whereArgs
) { 
  90         SQLiteDatabase db 
= mDbHelper
.getWritableDatabase(); 
  92         switch (mUriMatcher
.match(uri
)) { 
  94             count 
= db
.delete(ProviderTableMeta
.DB_NAME
, 
  97                             + uri
.getPathSegments().get(1) 
  98                             + (!TextUtils
.isEmpty(where
) ? 
" AND (" + where
 
  99                                     + ")" : ""), whereArgs
); 
 102             count 
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
); 
 105             throw new IllegalArgumentException("Unknown uri: " + uri
.toString()); 
 107         getContext().getContentResolver().notifyChange(uri
, null
); 
 112     public String 
getType(Uri uri
) { 
 113         switch (mUriMatcher
.match(uri
)) { 
 115             return ProviderTableMeta
.CONTENT_TYPE
; 
 117             return ProviderTableMeta
.CONTENT_TYPE_ITEM
; 
 119             throw new IllegalArgumentException("Unknown Uri id." 
 125     public Uri 
insert(Uri uri
, ContentValues values
) { 
 126         if (mUriMatcher
.match(uri
) != SINGLE_FILE 
&& 
 127             mUriMatcher
.match(uri
) != ROOT_DIRECTORY
) { 
 129             throw new IllegalArgumentException("Unknown uri id: " + uri
); 
 132         SQLiteDatabase db 
= mDbHelper
.getWritableDatabase(); 
 133         long rowId 
= db
.insert(ProviderTableMeta
.DB_NAME
, null
, values
); 
 135             Uri insertedFileUri 
= ContentUris
.withAppendedId( 
 136                     ProviderTableMeta
.CONTENT_URI_FILE
, rowId
); 
 137             getContext().getContentResolver().notifyChange(insertedFileUri
, 
 139             return insertedFileUri
; 
 141         throw new SQLException("ERROR " + uri
); 
 145     public boolean onCreate() { 
 146         mDbHelper 
= new DataBaseHelper(getContext()); 
 151     public Cursor 
query(Uri uri
, String
[] projection
, String selection
, 
 152             String
[] selectionArgs
, String sortOrder
) { 
 153         SQLiteQueryBuilder sqlQuery 
= new SQLiteQueryBuilder(); 
 155         sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
); 
 156         sqlQuery
.setProjectionMap(mProjectionMap
); 
 158         switch (mUriMatcher
.match(uri
)) { 
 162             sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT 
+ "=" 
 163                     + uri
.getPathSegments().get(1)); 
 166             if (uri
.getPathSegments().size() > 1) { 
 167                 sqlQuery
.appendWhere(ProviderTableMeta
._ID 
+ "=" 
 168                         + uri
.getPathSegments().get(1)); 
 172             throw new IllegalArgumentException("Unknown uri id: " + uri
); 
 176         if (TextUtils
.isEmpty(sortOrder
)) { 
 177             order 
= ProviderTableMeta
.DEFAULT_SORT_ORDER
; 
 182         SQLiteDatabase db 
= mDbHelper
.getReadableDatabase(); 
 183         Cursor c 
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
, 
 186         c
.setNotificationUri(getContext().getContentResolver(), uri
); 
 192     public int update(Uri uri
, ContentValues values
, String selection
, 
 193             String
[] selectionArgs
) { 
 194         return mDbHelper
.getWritableDatabase().update( 
 195                 ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
); 
 198     class DataBaseHelper 
extends SQLiteOpenHelper 
{ 
 200         public DataBaseHelper(Context context
) { 
 201             super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
); 
 206         public void onCreate(SQLiteDatabase db
) { 
 208             Log
.i("SQL", "Entering in onCreate"); 
 209             db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME 
+ "(" 
 210                     + ProviderTableMeta
._ID 
+ " INTEGER PRIMARY KEY, " 
 211                     + ProviderTableMeta
.FILE_NAME 
+ " TEXT, " 
 212                     + ProviderTableMeta
.FILE_PATH 
+ " TEXT, " 
 213                     + ProviderTableMeta
.FILE_PARENT 
+ " INTEGER, " 
 214                     + ProviderTableMeta
.FILE_CREATION 
+ " INTEGER, " 
 215                     + ProviderTableMeta
.FILE_MODIFIED 
+ " INTEGER, " 
 216                     + ProviderTableMeta
.FILE_CONTENT_TYPE 
+ " TEXT, " 
 217                     + ProviderTableMeta
.FILE_CONTENT_LENGTH 
+ " INTEGER, " 
 218                     + ProviderTableMeta
.FILE_STORAGE_PATH 
+ " TEXT, " 
 219                     + ProviderTableMeta
.FILE_ACCOUNT_OWNER 
+ " TEXT, " 
 220                     + ProviderTableMeta
.FILE_LAST_SYNC_DATE 
+ " INTEGER, " 
 221                     + ProviderTableMeta
.FILE_KEEP_IN_SYNC 
+ " INTEGER );"); 
 225         public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) { 
 226             Log
.i("SQL", "Entering in onUpgrade"); 
 227             if (oldVersion 
== 1 && newVersion 
>= 2) { 
 228                 Log
.i("SQL", "Entering in the ADD in onUpgrade"); 
 229                 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME 
+ 
 230                            " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC  
+ " INTEGER " + 
 232             } else Log
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion 
+ ", newVersion == " + newVersion
);