|
|
|
@ -0,0 +1,307 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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<Long> ids) {
|
|
|
|
|
//方法:实现了批量删除便签
|
|
|
|
|
if (ids == null) {//判断笔记id是否为空
|
|
|
|
|
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<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();//提供一个事件的列表
|
|
|
|
|
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进行删除
|
|
|
|
|
.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();//实例化一个contentValues类
|
|
|
|
|
values.put(NoteColumns.PARENT_ID, desFolderId);//将PARENT_ID更改为目标目录ID
|
|
|
|
|
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);//设置origin也即原本的父节点为原本的文件夹的id
|
|
|
|
|
values.put(NoteColumns.LOCAL_MODIFIED, 1);//设置修改符号为1
|
|
|
|
|
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
|
|
|
|
|
//对需要移动的便签进行数据更新,然后用update实现
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
|
|
|
|
|
long folderId) {//批量的将标签移动到另一个目录下
|
|
|
|
|
if (ids == null) {//判断便签ID是否为空
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
//将ids里包含的每一列的数据逐次加入到operationList中,等待最后的批量处理
|
|
|
|
|
for (long id : ids) {//遍历所有选中的便签的id
|
|
|
|
|
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());//将ids里的数据添加到operationlist中以便接下来批量处理
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {//利用Log输出信息来进行错误检测
|
|
|
|
|
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
|
|
|
|
|
//applyBatch一次性处理一个操作列表
|
|
|
|
|
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,
|
|
|
|
|
//resolver.query()方法第二个参数是要返回的列,第三个参数是Section,查询where字句;第四个是查询条件属性值,第五个是筛选规则
|
|
|
|
|
new String[] { "COUNT(*)" },
|
|
|
|
|
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?",
|
|
|
|
|
new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)},
|
|
|
|
|
null);//String.valueof将形参转成字符串返回
|
|
|
|
|
|
|
|
|
|
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),//通过withappendedid的方法为uri加上id
|
|
|
|
|
null,
|
|
|
|
|
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER,
|
|
|
|
|
new String [] {String.valueOf(type)},
|
|
|
|
|
null);//sql语句,表示筛选出列中type等于string数组中type,且每一项的PARENT_ID不等于Notes.ID.TRAXH_FOLDER
|
|
|
|
|
|
|
|
|
|
boolean exist = false;//查询文件
|
|
|
|
|
if (cursor != null) {//用getcount函数判断cursor是否为空
|
|
|
|
|
if (cursor.getCount() > 0) {//如果有满足条件的条目,那么就是可见,exist为真,否则不可见,exist为假
|
|
|
|
|
exist = true;
|
|
|
|
|
}
|
|
|
|
|
cursor.close();//关闭游标
|
|
|
|
|
}
|
|
|
|
|
return exist;//在数据数据库中是否存在
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {//判断该note是否在数据库中存在
|
|
|
|
|
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
|
|
|
|
|
null, null, null, null);
|
|
|
|
|
//相比于上面,这个只是存在性判定,因此不需要过多筛选条件
|
|
|
|
|
boolean exist = false;//初始化存在状态
|
|
|
|
|
if (cursor != null) {//根据getcount此时的值可以判断dataID的存在性
|
|
|
|
|
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),
|
|
|
|
|
//通过URI与dataId在数据库中查找数据
|
|
|
|
|
null, null, null, null);
|
|
|
|
|
|
|
|
|
|
boolean exist = false;//根据数据的有无返回相应的布尔值
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
if (cursor.getCount() > 0) {//调用对应的uri的数据值进行查询
|
|
|
|
|
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) {//判断找到的文件名返回文件是否存在的bool值
|
|
|
|
|
if(cursor.getCount() > 0) {
|
|
|
|
|
exist = true;
|
|
|
|
|
}
|
|
|
|
|
cursor.close();//关闭游标
|
|
|
|
|
}
|
|
|
|
|
return exist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static HashSet<AppWidgetAttribute> getFolderNoteWidget(ContentResolver resolver, long folderId) {
|
|
|
|
|
//使用hashset来存储不同窗口的id和type并且建立对应关系
|
|
|
|
|
Cursor c = resolver.query(Notes.CONTENT_NOTE_URI,//父id为传入的文件夹id
|
|
|
|
|
new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE },
|
|
|
|
|
NoteColumns.PARENT_ID + "=?",
|
|
|
|
|
new String[] { String.valueOf(folderId) },//hash集合为空的情况
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
HashSet<AppWidgetAttribute> set = null;//根据窗口的记录一一添加对应的属性值
|
|
|
|
|
if (c != null) {//将app窗口的属性加入到HashSet中
|
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
|
set = new HashSet<AppWidgetAttribute>();
|
|
|
|
|
do {
|
|
|
|
|
try {//把每一个条目对应的窗口id和type记录下来,放到set里面。每一行的第0个int和第1个int分别对应widgetId和widgetType
|
|
|
|
|
AppWidgetAttribute widget = new AppWidgetAttribute();//新建一个区块
|
|
|
|
|
widget.widgetId = c.getInt(0);//0对应的NoteColumns.WIDGET_ID
|
|
|
|
|
widget.widgetType = c.getInt(1);//1对应的NoteColumns.WIDGET_TYPE
|
|
|
|
|
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) {//通过笔记ID获取号码
|
|
|
|
|
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) {
|
|
|
|
|
//同样的,通过映射关系,通过号码和日期获取ID
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,//通过数据库操作,查询条件是(callDate和phoneNumber匹配传入参数的值)
|
|
|
|
|
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);//通过数据库操作,查询条件(callDate和phoneNumber匹配传入参数的值),找到对应的note
|
|
|
|
|
|
|
|
|
|
if (cursor != null) {//得到该note的系统属性,并以Long值的形式来保存
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
try {
|
|
|
|
|
return cursor.getLong(0);//0对应的CallNote.NOTE_ID
|
|
|
|
|
} 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) {//按ID获取片段
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,//通过ID查询
|
|
|
|
|
new String [] { NoteColumns.SNIPPET },
|
|
|
|
|
NoteColumns.ID + "=?",
|
|
|
|
|
new String [] { String.valueOf(noteId)},
|
|
|
|
|
null);//通过数据库操作,查询条件是(callDate和phoneNumber匹配传入参数的值
|
|
|
|
|
|
|
|
|
|
if (cursor != null) {//以string的形式获取该对象当前行指定列的值。
|
|
|
|
|
String snippet = "";//对字符串进行格式处理,将字符串两头的空格去掉同时将换行符去掉
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
snippet = cursor.getString(0);
|
|
|
|
|
}
|
|
|
|
|
cursor.close();
|
|
|
|
|
return snippet;
|
|
|
|
|
}
|
|
|
|
|
throw new IllegalArgumentException("Note is not found with id: " + noteId);
|
|
|
|
|
//IllegalArgumentException是非法传参异常,也就是参数传的类型冲突,属于RunTimeException运行时异常
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getFormattedSnippet(String snippet) {//对字符串进行格式处理,将字符串两头的空格去掉,同时将换行符去掉
|
|
|
|
|
if (snippet != null) {
|
|
|
|
|
snippet = snippet.trim();// trim()函数: 移除字符串两侧的空白字符或其他预定义字符
|
|
|
|
|
int index = snippet.indexOf('\n');
|
|
|
|
|
if (index != -1) {
|
|
|
|
|
snippet = snippet.substring(0, index);//截取到第一个换行符
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return snippet;
|
|
|
|
|
}
|
|
|
|
|
}
|