OSDN Git Service

Merge "CMFM: Remove orphan bookmarks" into cm-11.0
[android-x86/packages-apps-CMFileManager.git] / src / com / cyanogenmod / filemanager / preferences / Bookmarks.java
1 /*
2  * Copyright (C) 2012 The CyanogenMod Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.cyanogenmod.filemanager.preferences;
18
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;
25
26 import com.cyanogenmod.filemanager.model.Bookmark;
27
28 /**
29  * A class for deal with user-defined bookmarks
30  */
31 public class Bookmarks {
32
33     private final static int INVALID_BOOKMARKS_ID = -1;
34
35     /**
36      * Method that add a new bookmark
37      *
38      * @param context The current context
39      * @param bookmark The bookmark to add or update
40      * @return Bookmark The bookmark added
41      */
42     public static Bookmark addBookmark(Context context, Bookmark bookmark) {
43         // Check that has a valid information
44         if (bookmark.mPath == null) return null;
45
46         // Retrieve the content resolver
47         ContentResolver contentResolver = context.getContentResolver();
48
49         // Check that the bookmarks not exists
50         Bookmark b = getBookmark(contentResolver, bookmark.mPath);
51         if (b != null) return b;
52
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) {
58             return null;
59         }
60         return bookmark;
61     }
62
63     /**
64      * Method that removes a bookmark
65      *
66      * @param context The current context
67      * @param bookmark The bookmark to delete
68      * @return boolean If the bookmarks was remove
69      */
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;
73
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$
78     }
79
80     /**
81      * Method that return the bookmark from his identifier
82      *
83      * @param contentResolver The content resolver
84      * @param bookmarkId The bookmark identifier
85      * @return Bookmark The bookmark. null if no bookmark exists.
86      */
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,
91                 null, null, null);
92         Bookmark bookmark = null;
93         try {
94             if (cursor != null) {
95                 if (cursor.moveToFirst()) {
96                     bookmark = new Bookmark(cursor);
97                 }
98             }
99         } finally {
100             try {
101                 if (cursor != null) {
102                     cursor.close();
103                 }
104             } catch (Exception e) {/**NON BLOCK**/}
105         }
106
107         return bookmark;
108     }
109
110     /**
111      * Method that return all the bookmarks in the database
112      *
113      * @param contentResolver The content resolver
114      * @return Cursor The bookmarks cursor
115      */
116     public static Cursor getAllBookmarks(ContentResolver contentResolver) {
117         return contentResolver.query(
118                 Bookmark.Columns.CONTENT_URI,
119                 Bookmark.Columns.BOOKMARK_QUERY_COLUMNS,
120                 null, null, null);
121     }
122
123     /**
124      * Method that return the bookmark from his path
125      *
126      * @param contentResolver The content resolver
127      * @param path The bookmark path
128      * @return Bookmark The bookmark. null if no bookmark exists.
129      */
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;
137         try {
138             if (cursor != null) {
139                 if (cursor.moveToFirst()) {
140                     bookmark = new Bookmark(cursor);
141                 }
142                 cursor.close();
143             }
144         } finally {
145             try {
146                 if (cursor != null) {
147                     cursor.close();
148                 }
149             } catch (Exception e) {/**NON BLOCK**/}
150         }
151         return bookmark;
152     }
153
154     /**
155      * Method that remove all orphan bookmarks derived from "path"
156      *
157      * @param ctx The current context
158      * @param path The path to check
159      */
160     public static void deleteOrphanBookmarks(Context ctx, String path) {
161         ContentResolver cr = ctx.getContentResolver();
162         Cursor cursor = Bookmarks.getAllBookmarks(cr);
163         try {
164             if (cursor != null && cursor.moveToFirst()) {
165                 do {
166                     Bookmark bm = new Bookmark(cursor);
167                     if (bm.mPath.startsWith(path)) {
168                         removeBookmark(ctx, bm);
169                     }
170                 } while (cursor.moveToNext());
171             }
172         } finally {
173             try {
174                 if (cursor != null) {
175                     cursor.close();
176                 }
177             } catch (Exception e) {/**NON BLOCK**/}
178         }
179     }
180
181     /**
182      * Method that create the {@link ContentValues} from the bookmark
183      *
184      * @param bookmark The bookmark
185      * @return ContentValues The content
186      */
187     private static ContentValues createContentValues(Bookmark bookmark) {
188         ContentValues values = new ContentValues(1);
189         values.put(Bookmark.Columns.PATH, bookmark.mPath);
190         return values;
191     }
192 }