| 1 | package org.openintents.filemanager; |
| 2 | |
| 3 | import android.content.ContentProvider; |
| 4 | import android.content.ContentUris; |
| 5 | import android.content.ContentValues; |
| 6 | import android.content.Context; |
| 7 | import android.content.UriMatcher; |
| 8 | import android.database.Cursor; |
| 9 | import android.database.SQLException; |
| 10 | import android.database.sqlite.SQLiteDatabase; |
| 11 | import android.database.sqlite.SQLiteOpenHelper; |
| 12 | import android.database.sqlite.SQLiteQueryBuilder; |
| 13 | import android.net.Uri; |
| 14 | import android.provider.BaseColumns; |
| 15 | import android.text.TextUtils; |
| 16 | |
| 17 | public class BookmarksProvider extends ContentProvider implements BaseColumns{ |
| 18 | public static final String TB_NAME = "bookmarks"; |
| 19 | public static final String NAME = "name"; |
| 20 | public static final String PATH = "path"; |
| 21 | public static final String CHECKED = "checked"; //Only because of multiple choice delete dialog |
| 22 | public static final String PROVIDER_NAME = "org.openintents.filemanager.bookmarks"; |
| 23 | public static final Uri CONTENT_URI = |
| 24 | Uri.parse("content://"+PROVIDER_NAME); |
| 25 | public static final String BOOKMARK_MIMETYPE = |
| 26 | "vnd.android.cursor.item/vnd.openintents.bookmark"; |
| 27 | public static final String BOOKMARKS_MIMETYPE = |
| 28 | "vnd.android.cursor.dir/vnd.openintents.bookmark"; |
| 29 | |
| 30 | |
| 31 | private static final int BOOKMARKS = 1; |
| 32 | private static final int BOOKMARK_ID = 2; |
| 33 | private static final UriMatcher uriMatcher; |
| 34 | static{ |
| 35 | uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
| 36 | uriMatcher.addURI(PROVIDER_NAME, null, BOOKMARKS); |
| 37 | uriMatcher.addURI(PROVIDER_NAME, "#", BOOKMARK_ID); |
| 38 | } |
| 39 | |
| 40 | private DatabaseHelper dbHelper; |
| 41 | private SQLiteDatabase db; |
| 42 | |
| 43 | |
| 44 | private static final String DATABASE_CREATE = |
| 45 | String.format("CREATE TABLE %s (%s integer primary key autoincrement, " |
| 46 | + "%s text not null, %s text not null, %s integer default 0);", |
| 47 | TB_NAME, _ID, NAME, PATH, CHECKED); |
| 48 | |
| 49 | private static final String DATABASE_NAME = "org.openintents.filemanager"; |
| 50 | private static final int DATABASE_VERSION = 2; |
| 51 | |
| 52 | private static class DatabaseHelper extends SQLiteOpenHelper { |
| 53 | |
| 54 | DatabaseHelper(Context context) { |
| 55 | super(context, DATABASE_NAME, null, DATABASE_VERSION); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void onCreate(SQLiteDatabase db) { |
| 60 | db.execSQL(DATABASE_CREATE); |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * !!! |
| 65 | * When changing database version, you MUST change this method. |
| 66 | * Currently, it would delete all users' bookmarks |
| 67 | */ |
| 68 | @Override |
| 69 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 70 | db.execSQL("DROP TABLE IF EXISTS "+TB_NAME); |
| 71 | onCreate(db); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | |
| 76 | @Override |
| 77 | public int delete(Uri arg0, String arg1, String[] arg2) { |
| 78 | int count = 0; |
| 79 | switch (uriMatcher.match(arg0)) { |
| 80 | case BOOKMARKS: |
| 81 | count = db.delete(TB_NAME, arg1, arg2); |
| 82 | break; |
| 83 | case BOOKMARK_ID: |
| 84 | String id = arg0.getPathSegments().get(0); |
| 85 | count = db.delete(TB_NAME, _ID + " = " + id |
| 86 | + (!TextUtils.isEmpty(arg1) ? " AND (" + arg1 + ')' : ""), |
| 87 | arg2); |
| 88 | break; |
| 89 | default: |
| 90 | throw new IllegalArgumentException("Unknown URI " + arg0); |
| 91 | } |
| 92 | getContext().getContentResolver().notifyChange(arg0, null); |
| 93 | return count; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public String getType(Uri uri) { |
| 98 | switch (uriMatcher.match(uri)){ |
| 99 | case BOOKMARKS: |
| 100 | return BOOKMARKS_MIMETYPE; |
| 101 | case BOOKMARK_ID: |
| 102 | return BOOKMARK_MIMETYPE; |
| 103 | default: |
| 104 | throw new IllegalArgumentException("Unsupported URI: " + uri); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public Uri insert(Uri uri, ContentValues values){ |
| 110 | long rowID = db.insert(TB_NAME, "", values); |
| 111 | if (rowID > 0){ |
| 112 | Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID); |
| 113 | getContext().getContentResolver().notifyChange(_uri, null); |
| 114 | return _uri; |
| 115 | } |
| 116 | throw new SQLException("Failed to insert row into " + uri); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean onCreate() { |
| 121 | dbHelper = new DatabaseHelper(getContext()); |
| 122 | db = dbHelper.getWritableDatabase(); |
| 123 | return (db == null)? false:true; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Cursor query(Uri uri, String[] projection, String selection, |
| 128 | String[] selectionArgs, String sortOrder) { |
| 129 | SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder(); |
| 130 | sqlBuilder.setTables(TB_NAME); |
| 131 | if (uriMatcher.match(uri) == BOOKMARK_ID){ |
| 132 | sqlBuilder.appendWhere(_ID + " = " + uri.getPathSegments().get(0)); |
| 133 | } |
| 134 | |
| 135 | if (sortOrder==null || sortOrder=="") |
| 136 | sortOrder = _ID; |
| 137 | |
| 138 | Cursor c = sqlBuilder.query(db, |
| 139 | projection, |
| 140 | selection, |
| 141 | selectionArgs, |
| 142 | null, |
| 143 | null, |
| 144 | sortOrder); |
| 145 | c.setNotificationUri(getContext().getContentResolver(), uri); |
| 146 | return c; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public int update(Uri uri, ContentValues values, String selection, |
| 151 | String[] selectionArgs) { |
| 152 | int count = 0; |
| 153 | switch (uriMatcher.match(uri)){ |
| 154 | case BOOKMARKS: |
| 155 | count = db.update( |
| 156 | TB_NAME, |
| 157 | values, |
| 158 | selection, |
| 159 | selectionArgs); |
| 160 | break; |
| 161 | case BOOKMARK_ID: |
| 162 | count = db.update( |
| 163 | TB_NAME, |
| 164 | values, |
| 165 | _ID + " = " + uri.getPathSegments().get(0) |
| 166 | + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), |
| 167 | selectionArgs); |
| 168 | break; |
| 169 | default: throw new IllegalArgumentException("Unknown URI " + uri); |
| 170 | } |
| 171 | getContext().getContentResolver().notifyChange(uri, null); |
| 172 | return count; |
| 173 | } |
| 174 | |
| 175 | } |