/* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *总体分析 这段 Java 代码定义了DataUtils类,提供诸多处理笔记相关数据的实用方法,借助ContentResolver与对应数据存储交互,涵盖笔记删除、移动、存在性判断及数据获取等功能,便于安卓笔记应用的数据操作管理。 函数分析 batchDeleteNotes方法 所属类:DataUtils 功能:对传入要删笔记的ID集合判空、长度判断,构建操作列表批量删笔记,按执行结果返回布尔值表成败,遇异常记日志并返回false。 moveNoteToFoler方法 所属类:DataUtils 功能:创建ContentValues存更新数据,用ContentResolver按传入ID更新对应笔记信息,实现单笔记移至指定文件夹。 batchMoveToFolder方法 所属类:DataUtils 功能:判传入移动笔记ID集合是否为空,构建操作列表批量更新操作,依执行结果返回布尔值,异常时记日志返回false。 getUserFolderCount方法 所属类:DataUtils 功能:用ContentResolver查询符合条件的文件夹记录数量,游标获取并返回数量,用于统计有效文件夹个数。 visibleInNoteDatabase方法 所属类:DataUtils 功能:通过ContentResolver查询判断笔记是否符合条件在数据库中可见,依游标记录数量返回布尔值。 existInNoteDatabase方法 所属类:DataUtils 功能:借ContentResolver查询笔记是否存在,据游标记录数量返回布尔值。 existInDataDatabase方法 所属类:DataUtils 功能:类似查笔记存在的方式,查数据在对应数据库中是否存在,依游标返回布尔值。 checkVisibleFolderName方法 所属类:DataUtils 功能:用ContentResolver查指定名称文件夹是否符合条件存在,依游标记录数量返回布尔值。 getFolderNoteWidget方法 所属类:DataUtils 功能:ContentResolver查指定文件夹下笔记部件属性,游标遍历封装属性到集合后返回。 getCallNumberByNoteId方法 所属类:DataUtils 功能:ContentResolver按条件查对应笔记关联电话号码,游标获取返回,无则返回空字符串。 getNoteIdByPhoneNumberAndCallDate方法 所属类:DataUtils 功能:ContentResolver依通话相关条件查对应笔记ID,游标获取返回,无则返回0。 getSnippetById方法 所属类:DataUtils 功能:ContentResolver查指定笔记内容片段,游标获取返回,无则抛异常。 getFormattedSnippet方法 所属类:DataUtils 功能:格式化传入笔记内容片段,去除两端空白,遇换行截取前面部分后返回。 */ package net.micode.notes.tool; import android.content.ContentProviderOperation; import android.content.ContentProviderResult; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.content.OperationApplicationException; import android.database.Cursor; import android.os.RemoteException; import android.util.Log; import net.micode.notes.data.Notes; import net.micode.notes.data.Notes.CallNote; import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; import java.util.ArrayList; import java.util.HashSet; public class DataUtils { public static final String TAG = "DataUtils"; public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { if (ids == null) { Log.d(TAG, "the ids is null"); return true; } if (ids.size() == 0) { Log.d(TAG, "no id is in the hashset"); return true; } ArrayList operationList = new ArrayList(); for (long id : ids) { if(id == Notes.ID_ROOT_FOLDER) { Log.e(TAG, "Don't delete system folder root"); continue; } ContentProviderOperation.Builder builder = ContentProviderOperation .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); operationList.add(builder.build()); } try { ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); if (results == null || results.length == 0 || results[0] == null) { Log.d(TAG, "delete notes failed, ids:" + ids.toString()); return false; } return true; } catch (RemoteException e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } catch (OperationApplicationException e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } return false; } public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { ContentValues values = new ContentValues(); values.put(NoteColumns.PARENT_ID, desFolderId); values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); values.put(NoteColumns.LOCAL_MODIFIED, 1); resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); } public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { if (ids == null) { Log.d(TAG, "the ids is null"); return true; } ArrayList operationList = new ArrayList(); for (long id : ids) { ContentProviderOperation.Builder builder = ContentProviderOperation .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); builder.withValue(NoteColumns.PARENT_ID, folderId); builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); operationList.add(builder.build()); } try { ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); if (results == null || results.length == 0 || results[0] == null) { Log.d(TAG, "delete notes failed, ids:" + ids.toString()); return false; } return true; } catch (RemoteException e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } catch (OperationApplicationException e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } return false; } /** * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} */ public static int getUserFolderCount(ContentResolver resolver) { Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, new String[] { "COUNT(*)" }, NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)}, null); int count = 0; if(cursor != null) { if(cursor.moveToFirst()) { try { count = cursor.getInt(0); } catch (IndexOutOfBoundsException e) { Log.e(TAG, "get folder count failed:" + e.toString()); } finally { cursor.close(); } } } return count; } public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null, NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, new String [] {String.valueOf(type)}, null); boolean exist = false; if (cursor != null) { if (cursor.getCount() > 0) { exist = true; } cursor.close(); } return exist; } public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null, null, null, null); boolean exist = false; if (cursor != null) { if (cursor.getCount() > 0) { exist = true; } cursor.close(); } return exist; } public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null, null, null, null); boolean exist = false; if (cursor != null) { if (cursor.getCount() > 0) { exist = true; } cursor.close(); } return exist; } public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + " AND " + NoteColumns.SNIPPET + "=?", new String[] { name }, null); boolean exist = false; if(cursor != null) { if(cursor.getCount() > 0) { exist = true; } cursor.close(); } return exist; } public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, NoteColumns.PARENT_ID + "=?", new String[] { String.valueOf(folderId) }, null); HashSet set = null; if (c != null) { if (c.moveToFirst()) { set = new HashSet(); do { try { AppWidgetAttribute widget = new AppWidgetAttribute(); widget.widgetId = c.getInt(0); widget.widgetType = c.getInt(1); set.add(widget); } catch (IndexOutOfBoundsException e) { Log.e(TAG, e.toString()); } } while (c.moveToNext()); } c.close(); } return set; } public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.PHONE_NUMBER }, CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, null); if (cursor != null && cursor.moveToFirst()) { try { return cursor.getString(0); } catch (IndexOutOfBoundsException e) { Log.e(TAG, "Get call number fails " + e.toString()); } finally { cursor.close(); } } return ""; } public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.NOTE_ID }, CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" + CallNote.PHONE_NUMBER + ",?)", new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, null); if (cursor != null) { if (cursor.moveToFirst()) { try { return cursor.getLong(0); } catch (IndexOutOfBoundsException e) { Log.e(TAG, "Get call note id fails " + e.toString()); } } cursor.close(); } return 0; } public static String getSnippetById(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, new String [] { NoteColumns.SNIPPET }, NoteColumns.ID + "=?", new String [] { String.valueOf(noteId)}, null); if (cursor != null) { String snippet = ""; if (cursor.moveToFirst()) { snippet = cursor.getString(0); } cursor.close(); return snippet; } throw new IllegalArgumentException("Note is not found with id: " + noteId); } public static String getFormattedSnippet(String snippet) { if (snippet != null) { snippet = snippet.trim(); int index = snippet.indexOf('\n'); if (index != -1) { snippet = snippet.substring(0, index); } } return snippet; } }