|
|
/*
|
|
|
* 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) { //为空返回处理处理完成
|
|
|
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(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<Long> ids,
|
|
|
long folderId) {
|
|
|
if (ids == null) {
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
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, //光标指向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(); //最终关闭,可能存在问题,cursor可能无法正常释放,从而导致程序崩溃
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return count;
|
|
|
}
|
|
|
//可视化操作数据库,搭建ui界面方便用户操作本地数据库
|
|
|
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<AppWidgetAttribute> 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<AppWidgetAttribute> set = null;
|
|
|
if (c != null) { //光标不指向空,意味着存在
|
|
|
if (c.moveToFirst()) { //移动到第一位
|
|
|
set = new HashSet<AppWidgetAttribute>();
|
|
|
do {
|
|
|
try {
|
|
|
AppWidgetAttribute widget = new AppWidgetAttribute();
|
|
|
widget.widgetId = c.getInt(0); //得到部件id,0是请求代码
|
|
|
widget.widgetType = c.getInt(1); //得到部件种类,1是请求代码
|
|
|
set.add(widget); //将部件加入到哈希设置中
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
Log.e(TAG, e.toString()); //检测异常,加入到错误日志文件中去
|
|
|
}
|
|
|
} while (c.moveToNext());
|
|
|
}
|
|
|
c.close();
|
|
|
}
|
|
|
return set;
|
|
|
}
|
|
|
//得到电话号码笔记id
|
|
|
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;
|
|
|
}
|
|
|
//得到片段id
|
|
|
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); //通过请求代码0,请求获取字符串,并传递给snippet
|
|
|
}
|
|
|
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;
|
|
|
}
|
|
|
}
|