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
; 
  40 * The ContentProvider for the ownCloud App. 
  41 * @author Bartek Przybylski 
  44 public class FileContentProvider 
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
); 
  84   public int delete(Uri uri
, String where
, String
[] whereArgs
) { 
  85     SQLiteDatabase db 
= mDbHelper
.getWritableDatabase(); 
  87     switch (mUriMatcher
.match(uri
)) { 
  89         count 
= db
.delete(ProviderTableMeta
.DB_NAME
, 
  90                           ProviderTableMeta
._ID 
+ "=" + uri
.getPathSegments().get(1) 
  91                           + (!TextUtils
.isEmpty(where
)?
" AND (" + where 
+")" : ""), 
  95         count 
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
); 
  98         throw new IllegalArgumentException("Unknown uri: " + uri
.toString()); 
 100     getContext().getContentResolver().notifyChange(uri
, null
); 
 105   public String 
getType(Uri uri
) { 
 106     switch (mUriMatcher
.match(uri
)) { 
 108         return ProviderTableMeta
.CONTENT_TYPE
; 
 110         return ProviderTableMeta
.CONTENT_TYPE_ITEM
; 
 112         throw new IllegalArgumentException("Unknown Uri id." + uri
.toString()); 
 117   public Uri 
insert(Uri uri
, ContentValues values
) { 
 118     if (mUriMatcher
.match(uri
) != SINGLE_FILE
) { 
 119       throw new IllegalArgumentException("Unknown uri id: " + uri
); 
 122     SQLiteDatabase db 
= mDbHelper
.getWritableDatabase(); 
 123     long rowId 
= db
.insert(ProviderTableMeta
.DB_NAME
, null
, values
); 
 125       Uri insertedFileUri 
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, rowId
); 
 126       getContext().getContentResolver().notifyChange(insertedFileUri
, null
); 
 127       return insertedFileUri
; 
 129     throw new SQLException("ERROR " + uri
); 
 133   public boolean onCreate() { 
 134     mDbHelper 
= new DataBaseHelper(getContext()); 
 139   public Cursor 
query(Uri uri
, String
[] projection
, String selection
, 
 140       String
[] selectionArgs
, String sortOrder
) { 
 141     SQLiteQueryBuilder sqlQuery 
= new SQLiteQueryBuilder(); 
 143     sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
); 
 144     sqlQuery
.setProjectionMap(mProjectionMap
); 
 146     switch (mUriMatcher
.match(uri
)) { 
 148         sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT 
+ " is null"); 
 151         sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT 
+ "="+uri
.getPathSegments().get(1)); 
 154         if (uri
.getPathSegments().size() > 1) { 
 155           sqlQuery
.appendWhere(ProviderTableMeta
._ID 
+ "=" + 
 156                                uri
.getPathSegments().get(1)); 
 160         throw new IllegalArgumentException("Unknown uri id: " + uri
); 
 164     if (TextUtils
.isEmpty(sortOrder
)) { 
 165       order 
= ProviderTableMeta
.DEFAULT_SORT_ORDER
; 
 170     SQLiteDatabase db 
= mDbHelper
.getReadableDatabase(); 
 171     Cursor c 
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
, null
, null
, order
); 
 173     c
.setNotificationUri(getContext().getContentResolver(), uri
); 
 179   public int update(Uri uri
, ContentValues values
, String selection
, 
 180       String
[] selectionArgs
) { 
 181     return mDbHelper
.getWritableDatabase().update(ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
); 
 184   class DataBaseHelper 
extends SQLiteOpenHelper 
{ 
 186     public DataBaseHelper(Context context
) { 
 187       super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
); 
 192     public void onCreate(SQLiteDatabase db
) { 
 193       db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME 
+ "(" + 
 194                  ProviderTableMeta
._ID 
+ " INTEGER PRIMARY KEY, " + 
 195                  ProviderTableMeta
.FILE_NAME 
+ " TEXT, " + 
 196                  ProviderTableMeta
.FILE_PATH 
+ " TEXT, " + 
 197                  ProviderTableMeta
.FILE_PARENT 
+ " INTEGER, " + 
 198                  ProviderTableMeta
.FILE_CREATION 
+ " INTEGER, " + 
 199                  ProviderTableMeta
.FILE_MODIFIED 
+ " INTEGER, " + 
 200                  ProviderTableMeta
.FILE_CONTENT_TYPE 
+ " TEXT, " + 
 201                  ProviderTableMeta
.FILE_CONTENT_LENGTH 
+ " INTEGER, " + 
 202                  ProviderTableMeta
.FILE_STORAGE_PATH 
+ " TEXT, " + 
 203                  ProviderTableMeta
.FILE_ACCOUNT_OWNER 
+ " TEXT);"); 
 207     public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {