|
|
|
@ -13,7 +13,7 @@
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 包声明,标识该类属于 net.micode.notes.tool 包
|
|
|
|
|
package net.micode.notes.tool;
|
|
|
|
|
|
|
|
|
|
import android.content.ContentProviderOperation;
|
|
|
|
@ -26,17 +26,17 @@ 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 net.micode.notes.data.Notes; // 导入 Notes 数据结构
|
|
|
|
|
import net.micode.notes.data.Notes.CallNote; // 导入 CallNote 数据结构
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns; // 导入 NoteColumn 数据结构
|
|
|
|
|
import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; // 导入小部件属性结构
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DataUtils 类,提供与数据相关的实用方法
|
|
|
|
|
public class DataUtils {
|
|
|
|
|
public static final String TAG = "DataUtils";
|
|
|
|
|
public static final String TAG = "DataUtils";// 日志标签
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量删除笔记
|
|
|
|
@ -47,11 +47,11 @@ public class DataUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
|
|
|
|
|
if (ids == null) {
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
|
Log.d(TAG, "the ids is null"); // 记录ID为空的情况
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (ids.size() == 0) {
|
|
|
|
|
Log.d(TAG, "no id is in the hashset");
|
|
|
|
|
Log.d(TAG, "no id is in the hashset"); // 记录ID集合为空的情况
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -60,11 +60,11 @@ public class DataUtils {
|
|
|
|
|
for (long id : ids) {
|
|
|
|
|
if (id == Notes.ID_ROOT_FOLDER) {
|
|
|
|
|
Log.e(TAG, "Don't delete system folder root");
|
|
|
|
|
continue;
|
|
|
|
|
continue;// 跳过此ID
|
|
|
|
|
}
|
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
|
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));// 创建删除操作
|
|
|
|
|
operationList.add(builder.build());// 添加到操作列表
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
|
|
|
|
@ -75,7 +75,7 @@ public class DataUtils {
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
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()));
|
|
|
|
|
}
|
|
|
|
@ -91,11 +91,11 @@ public class DataUtils {
|
|
|
|
|
* @param desFolderId 目标文件夹ID
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
ContentValues values = new ContentValues();// 存储更新的内容
|
|
|
|
|
values.put(NoteColumns.PARENT_ID, desFolderId);// 更新笔记的父级文件夹ID
|
|
|
|
|
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);// 存储原始文件夹ID
|
|
|
|
|
values.put(NoteColumns.LOCAL_MODIFIED, 1);// 标记为已修改
|
|
|
|
|
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);// 更新笔记
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -117,10 +117,10 @@ public class DataUtils {
|
|
|
|
|
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());
|
|
|
|
|
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));// 创建更新操作
|
|
|
|
|
builder.withValue(NoteColumns.PARENT_ID, folderId);// 更新目标文件夹ID
|
|
|
|
|
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1);// 标记为已修改
|
|
|
|
|
operationList.add(builder.build());// 添加到操作列表
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
@ -132,9 +132,9 @@ public class DataUtils {
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
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()));
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));// 处理应用操作异常
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -147,8 +147,8 @@ public class DataUtils {
|
|
|
|
|
*/
|
|
|
|
|
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[]{"COUNT(*)"},// 查询计数
|
|
|
|
|
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?",// 条件:文件夹且不为回收站
|
|
|
|
|
new String[]{String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)},
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
@ -158,7 +158,7 @@ public class DataUtils {
|
|
|
|
|
try {
|
|
|
|
|
count = cursor.getInt(0);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
Log.e(TAG, "get folder count failed:" + e.toString());
|
|
|
|
|
Log.e(TAG, "get folder count failed:" + e.toString());// 处理索引越界异常
|
|
|
|
|
} finally {
|
|
|
|
|
cursor.close();
|
|
|
|
|
}
|
|
|
|
@ -274,12 +274,12 @@ public class DataUtils {
|
|
|
|
|
HashSet<AppWidgetAttribute> set = null;
|
|
|
|
|
if (c != null) {
|
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
|
set = new HashSet<AppWidgetAttribute>();
|
|
|
|
|
set = new HashSet<AppWidgetAttribute>();// 维护一个小部件信息的集合
|
|
|
|
|
do {
|
|
|
|
|
try {
|
|
|
|
|
AppWidgetAttribute widget = new AppWidgetAttribute();
|
|
|
|
|
widget.widgetId = c.getInt(0);
|
|
|
|
|
widget.widgetType = c.getInt(1);
|
|
|
|
|
AppWidgetAttribute widget = new AppWidgetAttribute();// 创建小部件属性实例
|
|
|
|
|
widget.widgetId = c.getInt(0);// 获取小部件ID
|
|
|
|
|
widget.widgetType = c.getInt(1);// 获取小部件类型
|
|
|
|
|
set.add(widget);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
@ -307,14 +307,14 @@ public class DataUtils {
|
|
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
|
try {
|
|
|
|
|
return cursor.getString(0);
|
|
|
|
|
return cursor.getString(0);// 返回通话号码
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
Log.e(TAG, "Get call number fails " + e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
cursor.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
return "";// 若未找到返回空字符串
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|