2 * Copyright (C) 2012 The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.cyanogenmod.filemanager.preferences;
19 import android.content.ContentResolver;
20 import android.content.ContentUris;
21 import android.content.ContentValues;
22 import android.content.Context;
23 import android.database.Cursor;
24 import android.net.Uri;
26 import com.cyanogenmod.filemanager.model.Bookmark;
29 * A class for deal with user-defined bookmarks
31 public class Bookmarks {
33 private final static int INVALID_BOOKMARKS_ID = -1;
36 * Method that add a new bookmark
38 * @param context The current context
39 * @param bookmark The bookmark to add or update
40 * @return Bookmark The bookmark added
42 public static Bookmark addBookmark(Context context, Bookmark bookmark) {
43 // Check that has a valid information
44 if (bookmark.mPath == null) return null;
46 // Retrieve the content resolver
47 ContentResolver contentResolver = context.getContentResolver();
49 // Check that the bookmarks not exists
50 Bookmark b = getBookmark(contentResolver, bookmark.mPath);
51 if (b != null) return b;
53 // Create the content values
54 ContentValues values = createContentValues(bookmark);
55 Uri uri = context.getContentResolver().insert(Bookmark.Columns.CONTENT_URI, values);
56 bookmark.mId = (int) ContentUris.parseId(uri);
57 if (bookmark.mId == INVALID_BOOKMARKS_ID) {
64 * Method that removes a bookmark
66 * @param context The current context
67 * @param bookmark The bookmark to delete
68 * @return boolean If the bookmarks was remove
70 public static boolean removeBookmark(Context context, Bookmark bookmark) {
71 // Check that has a valid information
72 if (bookmark.mId == INVALID_BOOKMARKS_ID) return false;
74 // Retrieve the content resolver
75 ContentResolver contentResolver = context.getContentResolver();
76 Uri uri = ContentUris.withAppendedId(Bookmark.Columns.CONTENT_URI, bookmark.mId);
77 return contentResolver.delete(uri, "", null) == 1; //$NON-NLS-1$
81 * Method that return the bookmark from his identifier
83 * @param contentResolver The content resolver
84 * @param bookmarkId The bookmark identifier
85 * @return Bookmark The bookmark. null if no bookmark exists.
87 public static Bookmark getBookmark(ContentResolver contentResolver, int bookmarkId) {
88 Cursor cursor = contentResolver.query(
89 ContentUris.withAppendedId(Bookmark.Columns.CONTENT_URI, bookmarkId),
90 Bookmark.Columns.BOOKMARK_QUERY_COLUMNS,
92 Bookmark bookmark = null;
95 if (cursor.moveToFirst()) {
96 bookmark = new Bookmark(cursor);
101 if (cursor != null) {
104 } catch (Exception e) {/**NON BLOCK**/}
111 * Method that return all the bookmarks in the database
113 * @param contentResolver The content resolver
114 * @return Cursor The bookmarks cursor
116 public static Cursor getAllBookmarks(ContentResolver contentResolver) {
117 return contentResolver.query(
118 Bookmark.Columns.CONTENT_URI,
119 Bookmark.Columns.BOOKMARK_QUERY_COLUMNS,
124 * Method that return the bookmark from his path
126 * @param contentResolver The content resolver
127 * @param path The bookmark path
128 * @return Bookmark The bookmark. null if no bookmark exists.
130 public static Bookmark getBookmark(ContentResolver contentResolver, String path) {
131 final String where = Bookmark.Columns.PATH + " = ?"; //$NON-NLS-1$
132 Cursor cursor = contentResolver.query(
133 Bookmark.Columns.CONTENT_URI,
134 Bookmark.Columns.BOOKMARK_QUERY_COLUMNS,
135 where, new String[]{path}, null);
136 Bookmark bookmark = null;
138 if (cursor != null) {
139 if (cursor.moveToFirst()) {
140 bookmark = new Bookmark(cursor);
146 if (cursor != null) {
149 } catch (Exception e) {/**NON BLOCK**/}
155 * Method that create the {@link ContentValues} from the bookmark
157 * @param bookmark The bookmark
158 * @return ContentValues The content
160 private static ContentValues createContentValues(Bookmark bookmark) {
161 ContentValues values = new ContentValues(1);
162 values.put(Bookmark.Columns.PATH, bookmark.mPath);