Compare commits

...

No commits in common. 'master' and 'main' have entirely different histories.
master ... main

@ -0,0 +1,2 @@
# 123ab456

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -1,731 +0,0 @@
/*
* 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.model; // 定义包名,表示这个类属于哪个包
import android.content.ContentProviderOperation; // 导入ContentProviderOperation类用于构建批量操作
import android.content.ContentProviderResult; // 导入ContentProviderResult类表示ContentProvider操作的结果
import android.content.ContentUris; // 导入ContentUris类用于处理URI
import android.content.ContentValues; // 导入ContentValues类用于存储插入或更新的数据
import android.content.Context; // 导入Context类用于获取系统服务和资源
import android.content.OperationApplicationException; // 导入OperationApplicationException类表示批量操作异常
import android.net.Uri; // 导入Uri类用于表示数据的统一资源标识符
import android.os.RemoteException; // 导入RemoteException类表示远程操作异常
import android.util.Log; // 导入Log类用于打印日志
import net.micode.notes.data.Notes; // 导入Notes类包含数据库相关常量和方法
import net.micode.notes.data.Notes.CallNote; // 导入CallNote常量类表示语音通话类型的笔记
import net.micode.notes.data.Notes.DataColumns; // 导入DataColumns常量类表示数据表的列名
import net.micode.notes.data.Notes.NoteColumns; // 导入NoteColumns常量类表示笔记表的列名
import net.micode.notes.data.Notes.TextNote; // 导入TextNote常量类表示文本类型的笔记
import java.util.ArrayList; // 导入ArrayList类用于存储对象的动态数组
public class Note { // 定义Note类代表一个笔记对象
private ContentValues mNoteDiffValues; // 定义ContentValues对象用于存储笔记的差异值
private NoteData mNoteData; // 定义NoteData对象用于存储笔记的数据
private static final String TAG = "Note"; // 定义日志标签,便于调试时查找日志
<<<<<<< HEAD
<<<<<<< HEAD
/**
* ID
*/
public static synchronized long getNewNoteId(Context context, long folderId) { // 定义静态方法getNewNoteId获取新的笔记ID
// 创建一个新笔记在数据库
ContentValues values = new ContentValues(); // 创建ContentValues对象用于存储插入的数据
long createdTime = System.currentTimeMillis(); // 获取当前时间戳,用于设置笔记的创建时间和修改时间
values.put(NoteColumns.CREATED_DATE, createdTime); // 将创建时间插入到ContentValues对象中
values.put(NoteColumns.MODIFIED_DATE, createdTime); // 将修改时间插入到ContentValues对象中
values.put(NoteColumns.TYPE, Notes.TYPE_NOTE); // 将笔记类型插入到ContentValues对象中
values.put(NoteColumns.LOCAL_MODIFIED, 1); // 将本地修改标志插入到ContentValues对象中
values.put(NoteColumns.PARENT_ID, folderId); // 将笔记所属文件夹ID插入到ContentValues对象中
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); // 向数据库插入新的笔记并获取返回的URI
=======
=======
>>>>>>> 09123a02b97342736defdf68e66e1e84c33b9160
public class Note {
// 使用ContentValues来存储笔记的差异值
private ContentValues mNoteDiffValues;
// 存储笔记数据的实例
private NoteData mNoteData;
// 定义日志标签
private static final String TAG = "Note";
/**
* ID
*/
public static synchronized long getNewNoteId(Context context, long folderId) {
// 创建一个ContentValues对象用于存储新笔记的信息
ContentValues values = new ContentValues();
// 获取当前时间戳作为笔记的创建和修改时间
long createdTime = System.currentTimeMillis();
values.put(NoteColumns.CREATED_DATE, createdTime);
values.put(NoteColumns.MODIFIED_DATE, createdTime);
// 设置笔记的类型为普通笔记
values.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
// 标记笔记已被本地修改
values.put(NoteColumns.LOCAL_MODIFIED, 1);
// 设置笔记的父文件夹ID
values.put(NoteColumns.PARENT_ID, folderId);
// 向笔记内容URI插入新笔记的信息并获取返回的URI
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values);
<<<<<<< HEAD
>>>>>>> ma_branch
=======
=======
/**
* ID
*/
public static synchronized long getNewNoteId(Context context, long folderId) { // 定义静态方法getNewNoteId获取新的笔记ID
// 创建一个新笔记在数据库
ContentValues values = new ContentValues(); // 创建ContentValues对象用于存储插入的数据
long createdTime = System.currentTimeMillis(); // 获取当前时间戳,用于设置笔记的创建时间和修改时间
values.put(NoteColumns.CREATED_DATE, createdTime); // 将创建时间插入到ContentValues对象中
values.put(NoteColumns.MODIFIED_DATE, createdTime); // 将修改时间插入到ContentValues对象中
values.put(NoteColumns.TYPE, Notes.TYPE_NOTE); // 将笔记类型插入到ContentValues对象中
values.put(NoteColumns.LOCAL_MODIFIED, 1); // 将本地修改标志插入到ContentValues对象中
values.put(NoteColumns.PARENT_ID, folderId); // 将笔记所属文件夹ID插入到ContentValues对象中
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); // 向数据库插入新的笔记并获取返回的URI
>>>>>>> a833c30ae3837c963347e2551db3b20613de574a
>>>>>>> 09123a02b97342736defdf68e66e1e84c33b9160
long noteId = 0; // 初始化笔记ID为0
try {
<<<<<<< HEAD
<<<<<<< HEAD
noteId = Long.valueOf(uri.getPathSegments().get(1)); // 从返回的URI中解析出笔记ID
} catch (NumberFormatException e) { // 捕获NumberFormatException异常
Log.e(TAG, "Get note id error :" + e.toString()); // 打印错误日志
noteId = 0; // 将笔记ID重置为0
}
if (noteId == -1) { // 检查笔记ID是否为-1
throw new IllegalStateException("Wrong note id:" + noteId); // 抛出IllegalStateException异常表示错误的笔记ID
}
return noteId; // 返回新的笔记ID
}
public Note() { // 定义构造方法初始化Note对象
mNoteDiffValues = new ContentValues(); // 初始化mNoteDiffValues对象
mNoteData = new NoteData(); // 初始化mNoteData对象
}
public void setNoteValue(String key, String value) { // 定义方法setNoteValue设置笔记的差异值
mNoteDiffValues.put(key, value); // 将键值对插入到mNoteDiffValues对象中
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 设置本地修改标志为1
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间
}
public void setTextData(String key, String value) { // 定义方法setTextData设置文本数据
mNoteData.setTextData(key, value); // 调用NoteData对象的setTextData方法设置文本数据
}
public void setTextDataId(long id) { // 定义方法setTextDataId设置文本数据ID
mNoteData.setTextDataId(id); // 调用NoteData对象的setTextDataId方法设置文本数据ID
}
public long getTextDataId() { // 定义方法getTextDataId获取文本数据ID
return mNoteData.mTextDataId; // 返回文本数据ID
}
public void setCallDataId(long id) { // 定义方法setCallDataId设置通话数据ID
mNoteData.setCallDataId(id); // 调用NoteData对象的setCallDataId方法设置通话数据ID
}
public void setCallData(String key, String value) { // 定义方法setCallData设置通话数据
mNoteData.setCallData(key, value); // 调用NoteData对象的setCallData方法设置通话数据
}
public boolean isLocalModified() { // 定义方法isLocalModified检查笔记是否被本地修改
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); // 如果mNoteDiffValues不为空或mNoteData被本地修改则返回true
}
public boolean syncNote(Context context, long noteId) { // 定义方法syncNote同步笔记到数据库
if (noteId <= 0) { // 检查笔记ID是否合法
throw new IllegalArgumentException("Wrong note id:" + noteId); // 抛出IllegalArgumentException异常表示错误的笔记ID
}
if (!isLocalModified()) { // 如果笔记没有被本地修改
return true; // 返回true表示不需要同步
}
/**
* {@link NoteColumns#LOCAL_MODIFIED}
* {@link NoteColumns#MODIFIED_DATE}
*
*/
if (context.getContentResolver().update( // 更新笔记信息
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
null) == 0) {
Log.e(TAG, "Update note error, should not happen"); // 打印错误日志,表示不应该出现的更新错误
// 不要返回,继续执行
}
mNoteDiffValues.clear(); // 清空mNoteDiffValues对象
if (mNoteData.isLocalModified() // 如果笔记数据被本地修改
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) { // 调用NoteData对象的pushIntoContentResolver方法同步数据并检查返回值是否为空
return false; // 返回false表示同步失败
}
return true; // 返回true表示同步成功
}
private class NoteData { // 定义内部类NoteData表示笔记数据
private long mTextDataId; // 定义文本数据ID
private ContentValues mTextDataValues; // 定义文本数据ContentValues对象
private long mCallDataId; // 定义通话数据ID
private ContentValues mCallDataValues; // 定义通话数据ContentValues对象
private static final String TAG = "NoteData"; // 定义日志标签,便于调试时查找日志
public NoteData() { // 定义构造方法初始化NoteData对象
mTextDataValues = new ContentValues(); // 初始化文本数据ContentValues对象
mCallDataValues = new ContentValues(); // 初始化通话数据ContentValues对象
mTextDataId = 0; // 初始化文本数据ID为0
mCallDataId = 0; // 初始化通话数据ID为0
}
boolean isLocalModified() { // 定义方法isLocalModified检查笔记数据是否被本地修改
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; // 如果文本数据或通话数据ContentValues对象不为空则返回true
}
void setTextDataId(long id) { // 定义方法setTextDataId设置文本数据ID
if(id <= 0) { // 检查ID是否合法
throw new IllegalArgumentException("Text data id should larger than 0"); // 抛出IllegalArgumentException异常表示错误的文本数据ID
=======
=======
>>>>>>> 09123a02b97342736defdf68e66e1e84c33b9160
// 从返回的URI中解析出笔记ID
noteId = Long.valueOf(uri.getPathSegments().get(1));
} catch (NumberFormatException e) {
// 如果解析失败输出错误日志并将noteId设为0
Log.e(TAG, "Get note id error :" + e.toString());
noteId = 0;
}
// 如果noteId为-1抛出异常表示获取ID失败
if (noteId == -1) {
throw new IllegalStateException("Wrong note id:" + noteId);
}
// 返回新笔记的ID
return noteId;
}
// 构造函数初始化mNoteDiffValues和mNoteData
public Note() {
mNoteDiffValues = new ContentValues();
mNoteData = new NoteData();
}
// 设置笔记的某个字段值,并更新本地修改标志和修改时间
public void setNoteValue(String key, String value) {
mNoteDiffValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
}
// 设置笔记文本数据的某个字段值
public void setTextData(String key, String value) {
mNoteData.setTextData(key, value);
}
// 设置文本数据ID
public void setTextDataId(long id) {
mNoteData.setTextDataId(id);
}
// 获取文本数据ID
public long getTextDataId() {
return mNoteData.mTextDataId;
}
// 设置通话数据ID
public void setCallDataId(long id) {
mNoteData.setCallDataId(id);
}
// 设置通话数据的某个字段值
public void setCallData(String key, String value) {
mNoteData.setCallData(key, value);
}
// 判断笔记是否被本地修改过如果mNoteDiffValues或mNoteData中有数据即为修改过
public boolean isLocalModified() {
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified();
}
// 同步笔记到数据库
public boolean syncNote(Context context, long noteId) {
if (noteId <= 0) {
// 如果笔记ID无效抛出异常
throw new IllegalArgumentException("Wrong note id:" + noteId);
}
if (!isLocalModified()) {
// 如果笔记没有被本地修改过直接返回true表示同步成功
return true;
}
// 尝试更新笔记信息如果更新失败即返回结果为0输出错误日志
if (context.getContentResolver().update(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
null) == 0) {
Log.e(TAG, "Update note error, should not happen");
// 不要在此处返回,继续执行后续代码
}
// 清空mNoteDiffValues准备下次更新
mNoteDiffValues.clear();
// 尝试同步文本数据和通话数据到数据库如果同步失败返回false
if (mNoteData.isLocalModified()
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
return false;
}
// 如果所有操作成功返回true表示同步成功
return true;
}
// 定义NoteData内部类用于存储笔记的详细数据
private class NoteData {
// 文本数据的ID
private long mTextDataId;
// 用于存储文本数据的ContentValues对象
private ContentValues mTextDataValues;
// 通话数据的ID
private long mCallDataId;
// 用于存储通话数据的ContentValues对象
private ContentValues mCallDataValues;
// 定义日志标签
private static final String TAG = "NoteData";
// 构造函数,初始化各个变量
public NoteData() {
mTextDataValues = new ContentValues();
mCallDataValues = new ContentValues();
mTextDataId = 0;
mCallDataId = 0;
}
// 判断文本数据或通话数据是否被修改过
boolean isLocalModified() {
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
}
// 设置文本数据ID如果ID无效抛出异常
void setTextDataId(long id) {
if(id <= 0) {
throw new IllegalArgumentException("Text data id should larger than 0");
<<<<<<< HEAD
>>>>>>> ma_branch
=======
=======
noteId = Long.valueOf(uri.getPathSegments().get(1)); // 从返回的URI中解析出笔记ID
} catch (NumberFormatException e) { // 捕获NumberFormatException异常
Log.e(TAG, "Get note id error :" + e.toString()); // 打印错误日志
noteId = 0; // 将笔记ID重置为0
}
if (noteId == -1) { // 检查笔记ID是否为-1
throw new IllegalStateException("Wrong note id:" + noteId); // 抛出IllegalStateException异常表示错误的笔记ID
}
return noteId; // 返回新的笔记ID
}
public Note() { // 定义构造方法初始化Note对象
mNoteDiffValues = new ContentValues(); // 初始化mNoteDiffValues对象
mNoteData = new NoteData(); // 初始化mNoteData对象
}
public void setNoteValue(String key, String value) { // 定义方法setNoteValue设置笔记的差异值
mNoteDiffValues.put(key, value); // 将键值对插入到mNoteDiffValues对象中
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 设置本地修改标志为1
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间
}
public void setTextData(String key, String value) { // 定义方法setTextData设置文本数据
mNoteData.setTextData(key, value); // 调用NoteData对象的setTextData方法设置文本数据
}
public void setTextDataId(long id) { // 定义方法setTextDataId设置文本数据ID
mNoteData.setTextDataId(id); // 调用NoteData对象的setTextDataId方法设置文本数据ID
}
public long getTextDataId() { // 定义方法getTextDataId获取文本数据ID
return mNoteData.mTextDataId; // 返回文本数据ID
}
public void setCallDataId(long id) { // 定义方法setCallDataId设置通话数据ID
mNoteData.setCallDataId(id); // 调用NoteData对象的setCallDataId方法设置通话数据ID
}
public void setCallData(String key, String value) { // 定义方法setCallData设置通话数据
mNoteData.setCallData(key, value); // 调用NoteData对象的setCallData方法设置通话数据
}
public boolean isLocalModified() { // 定义方法isLocalModified检查笔记是否被本地修改
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); // 如果mNoteDiffValues不为空或mNoteData被本地修改则返回true
}
public boolean syncNote(Context context, long noteId) { // 定义方法syncNote同步笔记到数据库
if (noteId <= 0) { // 检查笔记ID是否合法
throw new IllegalArgumentException("Wrong note id:" + noteId); // 抛出IllegalArgumentException异常表示错误的笔记ID
}
if (!isLocalModified()) { // 如果笔记没有被本地修改
return true; // 返回true表示不需要同步
}
/**
* {@link NoteColumns#LOCAL_MODIFIED}
* {@link NoteColumns#MODIFIED_DATE}
*
*/
if (context.getContentResolver().update( // 更新笔记信息
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
null) == 0) {
Log.e(TAG, "Update note error, should not happen"); // 打印错误日志,表示不应该出现的更新错误
// 不要返回,继续执行
}
mNoteDiffValues.clear(); // 清空mNoteDiffValues对象
if (mNoteData.isLocalModified() // 如果笔记数据被本地修改
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) { // 调用NoteData对象的pushIntoContentResolver方法同步数据并检查返回值是否为空
return false; // 返回false表示同步失败
}
return true; // 返回true表示同步成功
}
private class NoteData { // 定义内部类NoteData表示笔记数据
private long mTextDataId; // 定义文本数据ID
private ContentValues mTextDataValues; // 定义文本数据ContentValues对象
private long mCallDataId; // 定义通话数据ID
private ContentValues mCallDataValues; // 定义通话数据ContentValues对象
private static final String TAG = "NoteData"; // 定义日志标签,便于调试时查找日志
public NoteData() { // 定义构造方法初始化NoteData对象
mTextDataValues = new ContentValues(); // 初始化文本数据ContentValues对象
mCallDataValues = new ContentValues(); // 初始化通话数据ContentValues对象
mTextDataId = 0; // 初始化文本数据ID为0
mCallDataId = 0; // 初始化通话数据ID为0
}
boolean isLocalModified() { // 定义方法isLocalModified检查笔记数据是否被本地修改
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; // 如果文本数据或通话数据ContentValues对象不为空则返回true
}
void setTextDataId(long id) { // 定义方法setTextDataId设置文本数据ID
if(id <= 0) { // 检查ID是否合法
throw new IllegalArgumentException("Text data id should larger than 0"); // 抛出IllegalArgumentException异常表示错误的文本数据ID
>>>>>>> a833c30ae3837c963347e2551db3b20613de574a
>>>>>>> 09123a02b97342736defdf68e66e1e84c33b9160
}
mTextDataId = id; // 设置文本数据ID
}
<<<<<<< HEAD
<<<<<<< HEAD
void setCallDataId(long id) { // 定义方法setCallDataId设置通话数据ID
if (id <= 0) { // 检查ID是否合法
throw new IllegalArgumentException("Call data id should larger than 0"); // 抛出IllegalArgumentException异常表示错误的通话数据ID
=======
=======
>>>>>>> 09123a02b97342736defdf68e66e1e84c33b9160
// 设置通话数据ID如果ID无效抛出异常
void setCallDataId(long id) {
if (id <= 0) {
throw new IllegalArgumentException("Call data id should larger than 0");
<<<<<<< HEAD
>>>>>>> ma_branch
=======
=======
void setCallDataId(long id) { // 定义方法setCallDataId设置通话数据ID
if (id <= 0) { // 检查ID是否合法
throw new IllegalArgumentException("Call data id should larger than 0"); // 抛出IllegalArgumentException异常表示错误的通话数据ID
>>>>>>> a833c30ae3837c963347e2551db3b20613de574a
>>>>>>> 09123a02b97342736defdf68e66e1e84c33b9160
}
mCallDataId = id; // 设置通话数据ID
}
<<<<<<< HEAD
<<<<<<< HEAD
void setCallData(String key, String value) { // 定义方法setCallData设置通话数据
mCallDataValues.put(key, value); // 将键值对插入到通话数据ContentValues对象中
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 设置本地修改标志为1
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间
}
void setTextData(String key, String value) { // 定义方法setTextData设置文本数据
mTextDataValues.put(key, value); // 将键值对插入到文本数据ContentValues对象中
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 设置本地修改标志为1
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间
}
Uri pushIntoContentResolver(Context context, long noteId) { // 定义方法pushIntoContentResolver将数据同步到数据库
/**
*
*/
if (noteId <= 0) { // 检查笔记ID是否合法
throw new IllegalArgumentException("Wrong note id:" + noteId); // 抛出IllegalArgumentException异常表示错误的笔记ID
}
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); // 创建ArrayList对象用于存储ContentProviderOperation对象
ContentProviderOperation.Builder builder = null; // 初始化ContentProviderOperation.Builder对象
if(mTextDataValues.size() > 0) { // 如果文本数据ContentValues对象不为空
mTextDataValues.put(DataColumns.NOTE_ID, noteId); // 将笔记ID插入到ContentValues对象中
if (mTextDataId == 0) { // 如果文本数据ID为0
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); // 设置MIME类型为文本笔记类型
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, // 向数据库插入新的文本数据并获取返回的URI
mTextDataValues);
try {
setTextDataId(Long.valueOf(uri.getPathSegments().get(1))); // 从返回的URI中解析出文本数据ID并设置
} catch (NumberFormatException e) { // 捕获NumberFormatException异常
Log.e(TAG, "Insert new text data fail with noteId" + noteId); // 打印错误日志,表示插入文本数据失败
mTextDataValues.clear(); // 清空文本数据ContentValues对象
return null; // 返回null表示插入失败
}
} else {
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( // 创建更新操作的Builder对象
Notes.CONTENT_DATA_URI, mTextDataId));
builder.withValues(mTextDataValues); // 将文本数据ContentValues对象插入到Builder对象中
operationList.add(builder.build()); // 将Builder对象转换为ContentProviderOperation对象并添加到ArrayList对象中
}
mTextDataValues.clear(); // 清空文本数据ContentValues对象
}
if(mCallDataValues.size() > 0) { // 如果通话数据ContentValues对象不为空
mCallDataValues.put(DataColumns.NOTE_ID, noteId); // 将笔记ID插入到ContentValues对象中
if (mCallDataId == 0) { // 如果通话数据ID为0
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); // 设置MIME类型为通话笔记类型
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, // 向数据库插入新的通话数据并获取返回的URI
=======
=======
>>>>>>> 09123a02b97342736defdf68e66e1e84c33b9160
// 设置通话数据的某个字段值,并更新本地修改标志和修改时间
void setCallData(String key, String value) {
mCallDataValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
}
// 设置文本数据的某个字段值
void setTextData(String key, String value) {
mTextDataValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
}
// 将文本数据和通话数据推入内容解析器ContentResolver
Uri pushIntoContentResolver(Context context, long noteId) {
// 检查笔记ID是否有效
if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId);
}
// 创建操作列表
ArrayList<ContentProviderOperation> operationList = new ArrayList<>();
ContentProviderOperation.Builder builder = null;
// 如果文本数据有变化
if(mTextDataValues.size() > 0) {
// 将笔记ID加入文本数据的ContentValues中
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
// 如果文本数据ID为0表示这是新插入的数据
if (mTextDataId == 0) {
// 设置MIME类型为文本笔记类型
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE);
// 向数据URI插入新文本数据并获取返回的URI
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
mTextDataValues);
try {
// 从返回的URI中解析出文本数据ID并设置给mTextDataId
setTextDataId(Long.valueOf(uri.getPathSegments().get(1)));
} catch (NumberFormatException e) {
// 如果解析失败输出错误日志并清空文本数据的ContentValues
Log.e(TAG, "Insert new text data fail with noteId" + noteId);
mTextDataValues.clear();
return null;
}
} else {
// 否则,这是更新操作,创建一个更新操作
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
Notes.CONTENT_DATA_URI, mTextDataId));
builder.withValues(mTextDataValues);
// 将更新操作加入操作列表
operationList.add(builder.build());
}
// 清空文本数据的ContentValues准备下次更新
mTextDataValues.clear();
}
// 如果通话数据有变化,与文本数据的操作类似
if(mCallDataValues.size() > 0) {
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
if (mCallDataId == 0) {
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
<<<<<<< HEAD
>>>>>>> ma_branch
=======
=======
void setCallData(String key, String value) { // 定义方法setCallData设置通话数据
mCallDataValues.put(key, value); // 将键值对插入到通话数据ContentValues对象中
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 设置本地修改标志为1
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间
}
void setTextData(String key, String value) { // 定义方法setTextData设置文本数据
mTextDataValues.put(key, value); // 将键值对插入到文本数据ContentValues对象中
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 设置本地修改标志为1
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间
}
Uri pushIntoContentResolver(Context context, long noteId) { // 定义方法pushIntoContentResolver将数据同步到数据库
/**
*
*/
if (noteId <= 0) { // 检查笔记ID是否合法
throw new IllegalArgumentException("Wrong note id:" + noteId); // 抛出IllegalArgumentException异常表示错误的笔记ID
}
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); // 创建ArrayList对象用于存储ContentProviderOperation对象
ContentProviderOperation.Builder builder = null; // 初始化ContentProviderOperation.Builder对象
if(mTextDataValues.size() > 0) { // 如果文本数据ContentValues对象不为空
mTextDataValues.put(DataColumns.NOTE_ID, noteId); // 将笔记ID插入到ContentValues对象中
if (mTextDataId == 0) { // 如果文本数据ID为0
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); // 设置MIME类型为文本笔记类型
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, // 向数据库插入新的文本数据并获取返回的URI
mTextDataValues);
try {
setTextDataId(Long.valueOf(uri.getPathSegments().get(1))); // 从返回的URI中解析出文本数据ID并设置
} catch (NumberFormatException e) { // 捕获NumberFormatException异常
Log.e(TAG, "Insert new text data fail with noteId" + noteId); // 打印错误日志,表示插入文本数据失败
mTextDataValues.clear(); // 清空文本数据ContentValues对象
return null; // 返回null表示插入失败
}
} else {
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( // 创建更新操作的Builder对象
Notes.CONTENT_DATA_URI, mTextDataId));
builder.withValues(mTextDataValues); // 将文本数据ContentValues对象插入到Builder对象中
operationList.add(builder.build()); // 将Builder对象转换为ContentProviderOperation对象并添加到ArrayList对象中
}
mTextDataValues.clear(); // 清空文本数据ContentValues对象
}
if(mCallDataValues.size() > 0) { // 如果通话数据ContentValues对象不为空
mCallDataValues.put(DataColumns.NOTE_ID, noteId); // 将笔记ID插入到ContentValues对象中
if (mCallDataId == 0) { // 如果通话数据ID为0
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); // 设置MIME类型为通话笔记类型
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, // 向数据库插入新的通话数据并获取返回的URI
>>>>>>> a833c30ae3837c963347e2551db3b20613de574a
>>>>>>> 09123a02b97342736defdf68e66e1e84c33b9160
mCallDataValues);
try {
setCallDataId(Long.valueOf(uri.getPathSegments().get(1))); // 从返回的URI中解析出通话数据ID并设置
} catch (NumberFormatException e) { // 捕获NumberFormatException异常
Log.e(TAG, "Insert new call data fail with noteId" + noteId); // 打印错误日志,表示插入通话数据失败
mCallDataValues.clear(); // 清空通话数据ContentValues对象
return null; // 返回null表示插入失败
}
} else {
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( // 创建更新操作的Builder对象
Notes.CONTENT_DATA_URI, mCallDataId));
builder.withValues(mCallDataValues); // 将通话数据ContentValues对象插入到Builder对象中
operationList.add(builder.build()); // 将Builder对象转换为ContentProviderOperation对象并添加到ArrayList对象中
}
mCallDataValues.clear(); // 清空通话数据ContentValues对象
}
<<<<<<< HEAD
<<<<<<< HEAD
if (operationList.size() > 0) { // 如果ArrayList对象不为空
=======
// 如果操作列表中有操作,尝试批量应用这些操作
if (operationList.size() > 0) {
>>>>>>> ma_branch
=======
// 如果操作列表中有操作,尝试批量应用这些操作
if (operationList.size() > 0) {
=======
if (operationList.size() > 0) { // 如果ArrayList对象不为空
>>>>>>> a833c30ae3837c963347e2551db3b20613de574a
>>>>>>> 09123a02b97342736defdf68e66e1e84c33b9160
try {
ContentProviderResult[] results = context.getContentResolver().applyBatch( // 批量应用操作,并获取返回的结果
Notes.AUTHORITY, operationList);
<<<<<<< HEAD
<<<<<<< HEAD
return (results == null || results.length == 0 || results[0] == null) ? null // 如果返回的结果为空则返回null
: ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId); // 否则返回包含笔记ID的URI
} catch (RemoteException e) { // 捕获RemoteException异常
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); // 打印错误日志,表示远程操作失败
return null; // 返回null表示同步失败
} catch (OperationApplicationException e) { // 捕获OperationApplicationException异常
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); // 打印错误日志,表示批量操作失败
return null; // 返回null表示同步失败
}
}
return null; // 返回null表示没有需要同步的操作
=======
=======
>>>>>>> 09123a02b97342736defdf68e66e1e84c33b9160
// 如果应用操作失败返回null否则返回笔记内容URI
return (results == null || results.length == 0 || results[0] == null) ? null
: ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId);
} catch (RemoteException e) {
// 如果发生远程异常输出错误日志并返回null
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
return null;
} catch (OperationApplicationException e) {
// 如果操作应用异常输出错误日志并返回null
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
return null;
}
}
// 如果没有操作需要应用返回null
return null;
<<<<<<< HEAD
>>>>>>> ma_branch
=======
=======
return (results == null || results.length == 0 || results[0] == null) ? null // 如果返回的结果为空则返回null
: ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId); // 否则返回包含笔记ID的URI
} catch (RemoteException e) { // 捕获RemoteException异常
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); // 打印错误日志,表示远程操作失败
return null; // 返回null表示同步失败
} catch (OperationApplicationException e) { // 捕获OperationApplicationException异常
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); // 打印错误日志,表示批量操作失败
return null; // 返回null表示同步失败
}
}
return null; // 返回null表示没有需要同步的操作
>>>>>>> a833c30ae3837c963347e2551db3b20613de574a
>>>>>>> 09123a02b97342736defdf68e66e1e84c33b9160
}
}
}

@ -1,415 +0,0 @@
/*
* 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.model;
import android.appwidget.AppWidgetManager;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.util.Log;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.CallNote;
import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.Notes.TextNote;
import net.micode.notes.tool.ResourceParser.NoteBgResources;
public class WorkingNote {
// 存储笔记的实例
private Note mNote;
// 笔记ID
private long mNoteId;
// 笔记内容
private String mContent;
// 笔记模式
private int mMode;
// 笔记提醒日期
private long mAlertDate;
// 笔记修改日期
private long mModifiedDate;
// 笔记背景颜色ID
private int mBgColorId;
// 小部件ID
private int mWidgetId;
// 小部件类型
private int mWidgetType;
// 笔记所在文件夹ID
private long mFolderId;
// 上下文环境
private Context mContext;
// 定义日志标签
private static final String TAG = "WorkingNote";
// 笔记是否已被删除标志
private boolean mIsDeleted;
// 笔记设置改变监听器
private NoteSettingChangedListener mNoteSettingStatusListener;
// 定义数据查询投影
public static final String[] DATA_PROJECTION = new String[] {
DataColumns.ID,
DataColumns.CONTENT,
DataColumns.MIME_TYPE,
DataColumns.DATA1,
DataColumns.DATA2,
DataColumns.DATA3,
DataColumns.DATA4,
};
// 定义笔记查询投影
public static final String[] NOTE_PROJECTION = new String[] {
NoteColumns.PARENT_ID,
NoteColumns.ALERTED_DATE,
NoteColumns.BG_COLOR_ID,
NoteColumns.WIDGET_ID,
NoteColumns.WIDGET_TYPE,
NoteColumns.MODIFIED_DATE
};
// 定义数据列索引
private static final int DATA_ID_COLUMN = 0;
private static final int DATA_CONTENT_COLUMN = 1;
private static final int DATA_MIME_TYPE_COLUMN = 2;
private static final int DATA_MODE_COLUMN = 3;
// 定义笔记列索引
private static final int NOTE_PARENT_ID_COLUMN = 0;
private static final int NOTE_ALERTED_DATE_COLUMN = 1;
private static final int NOTE_BG_COLOR_ID_COLUMN = 2;
private static final int NOTE_WIDGET_ID_COLUMN = 3;
private static final int NOTE_WIDGET_TYPE_COLUMN = 4;
private static final int NOTE_MODIFIED_DATE_COLUMN = 5;
// 新笔记构造函数,用于创建一个新的笔记
private WorkingNote(Context context, long folderId) {
mContext = context;
mAlertDate = 0;
mModifiedDate = System.currentTimeMillis();
mFolderId = folderId;
mNote = new Note();
mNoteId = 0;
mIsDeleted = false;
mMode = 0;
mWidgetType = Notes.TYPE_WIDGET_INVALIDE;
}
// 已有笔记构造函数,用于加载已存在的笔记
private WorkingNote(Context context, long noteId, long folderId) {
mContext = context;
mNoteId = noteId;
mFolderId = folderId;
mIsDeleted = false;
mNote = new Note();
loadNote();
}
// 从数据库加载笔记信息
private void loadNote() {
// 根据笔记ID查询笔记信息
Cursor cursor = mContext.getContentResolver().query(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null,
null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
// 从查询结果中读取笔记信息并赋值给相应的成员变量
mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN);
mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN);
mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN);
mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN);
mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN);
mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN);
}
// 关闭游标
cursor.close();
} else {
// 如果查询结果为空,输出错误日志并抛出异常
Log.e(TAG, "No note with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note with id " + mNoteId);
}
// 加载笔记详细数据
loadNoteData();
}
// 从数据库加载笔记详细数据
private void loadNoteData() {
// 查询笔记ID对应的数据信息
Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION,
DataColumns.NOTE_ID + "=?", new String[] {
String.valueOf(mNoteId)
}, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
// 遍历查询结果根据MIME类型读取数据并赋值给相应的成员变量
do {
String type = cursor.getString(DATA_MIME_TYPE_COLUMN);
if (DataConstants.NOTE.equals(type)) {
mContent = cursor.getString(DATA_CONTENT_COLUMN);
mMode = cursor.getInt(DATA_MODE_COLUMN);
mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN));
} else if (DataConstants.CALL_NOTE.equals(type)) {
mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN));
} else {
// 如果MIME类型不匹配输出调试日志
Log.d(TAG, "Wrong note type with type:" + type);
}
} while (cursor.moveToNext());
}
// 关闭游标
cursor.close();
} else {
// 如果查询结果为空,输出错误日志并抛出异常
Log.e(TAG, "No data with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId);
}
}
// 创建一个空的笔记实例
public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId,
int widgetType, int defaultBgColorId) {
WorkingNote note = new WorkingNote(context, folderId);
note.setBgColorId(defaultBgColorId);
note.setWidgetId(widgetId);
note.setWidgetType(widgetType);
return note;
}
// 从数据库加载一个已存在的笔记
public static WorkingNote load(Context context, long id) {
return new WorkingNote(context, id, 0);
}
// 保存笔记到数据库
public synchronized boolean saveNote() {
// 判断笔记是否值得保存
if (isWorthSaving()) {
// 如果笔记不存在于数据库中创建一个新的笔记ID
if (!existInDatabase()) {
if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) {
Log.e(TAG, "Create new note fail with id:" + mNoteId);
return false;
}
}
// 同步笔记信息到数据库
mNote.syncNote(mContext, mNoteId);
// 如果笔记关联了小部件,更新小部件内容
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE
&& mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged();
}
return true;
} else {
// 如果笔记不值得保存返回false
return false;
}
}
// 判断笔记是否存在于数据库中
public boolean existInDatabase() {
return mNoteId > 0;
}
// 判断笔记是否值得保存,即是否被删除或是否有内容修改
private boolean isWorthSaving() {
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent))
|| (existInDatabase() && !mNote.isLocalModified())) {
return false;
} else {
return true;
}
}
// 设置笔记设置改变监听器
public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) {
mNoteSettingStatusListener = l;
}
// 设置笔记提醒日期,并通知监听器
public void setAlertDate(long date, boolean set) {
if (date != mAlertDate) {
mAlertDate = date;
mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate));
}
if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onClockAlertChanged(date, set);
}
}
// 标记笔记为删除状态,并通知监听器更新小部件内容
public void markDeleted(boolean mark) {
mIsDeleted = mark;
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged();
}
}
// 设置笔记背景颜色ID并通知监听器
public void setBgColorId(int id) {
if (id != mBgColorId) {
mBgColorId = id;
if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onBackgroundColorChanged();
}
mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id));
}
}
// 设置笔记模式,并通知监听器
public void setCheckListMode(int mode) {
if (mMode != mode) {
if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode);
}
mMode = mode;
mNote.setTextData(TextNote.MODE, String.valueOf(mMode));
}
}
// 设置小部件类型
public void setWidgetType(int type) {
if (type != mWidgetType) {
mWidgetType = type;
mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType));
}
}
// 设置小部件ID
public void setWidgetId(int id) {
if (id != mWidgetId) {
mWidgetId = id;
mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(id));
}
}
// 设置笔记内容文本,并更新笔记的修改标志和修改时间
public void setWorkingText(String text) {
if (!TextUtils.equals(mContent, text)) {
mContent = text;
mNote.setTextData(DataColumns.CONTENT, mContent);
}
}
// 将笔记转换为通话笔记,并设置通话日期和电话号码
public void convertToCallNote(String phoneNumber, long callDate) {
mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate));
mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber);
mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER));
}
// 判断笔记是否有提醒
public boolean hasClockAlert() {
return mAlertDate > 0;
}
// 获取笔记内容
public String getContent() {
return mContent;
}
// 获取笔记提醒日期
public long getAlertDate() {
return mAlertDate;
}
// 获取笔记修改日期
public long getModifiedDate() {
return mModifiedDate;
}
// 获取笔记背景颜色资源ID
public int getBgColorResId() {
return NoteBgResources.getNoteBgResource(mBgColorId);
}
// 获取笔记背景颜色ID
public int getBgColorId() {
return mBgColorId;
}
// 获取笔记标题背景颜色资源ID
public int getTitleBgResId() {
return NoteBgResources.getNoteTitleBgResource(mBgColorId);
}
// 获取笔记模式
public int getCheckListMode() {
return mMode;
}
// 获取笔记ID
public long getNoteId() {
return mNoteId;
}
// 获取笔记所在文件夹ID
public long getFolderId() {
return mFolderId;
}
// 获取小部件ID
public int getWidgetId() {
return mWidgetId;
}
// 获取小部件类型
public int getWidgetType() {
return mWidgetType;
}
// 定义笔记设置改变监听器接口
public interface NoteSettingChangedListener {
/**
*
*/
void onBackgroundColorChanged();
/**
*
*/
void onClockAlertChanged(long date, boolean set);
/**
*
*/
void onWidgetChanged();
/**
*
* @param oldMode
* @param newMode
*/
void onCheckListModeChanged(int oldMode, int newMode);
}
}

@ -1,197 +0,0 @@
/*
* 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.widget;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.widget.RemoteViews;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.tool.ResourceParser;
import net.micode.notes.ui.NoteEditActivity;
import net.micode.notes.ui.NotesListActivity;
// 定义一个抽象类 NoteWidgetProvider该类继承自 AppWidgetProvider 类。
// 该抽象类用于提供小部件的基本功能和数据处理逻辑。
public abstract class NoteWidgetProvider extends AppWidgetProvider {
// 定义一个公共静态常量数组 PROJECTION用于指定查询时返回的列。
// 这些列包括小部件的 ID、背景颜色 ID 和摘要snippet
public static final String[] PROJECTION = new String[]{
NoteColumns.ID, // 小部件的唯一标识符 ID
NoteColumns.BG_COLOR_ID, // 小部件的背景颜色 ID
NoteColumns.SNIPPET // 小部件的摘要内容
};
// 定义三个公共静态常量,分别表示 PROJECTION 数组中各列的索引位置。
// 这些常量用于方便地访问查询结果中的特定列。
public static final int COLUMN_ID = 0; // ID 列的索引位置
public static final int COLUMN_BG_COLOR_ID = 1; // 背景颜色 ID 列的索引位置
public static final int COLUMN_SNIPPET = 2; // 摘要列的索引位置
// 定义一个私有静态常量 TAG用于在日志中标识当前类。
// 这有助于在调试时区分不同的日志来源。
private static final String TAG = "NoteWidgetProvider";
// 重写父类 AppWidgetProvider 中的 onDeleted 方法。
// 此方法在小部件被从主屏幕删除时会被调用。
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// 创建一个 ContentValues 对象,用于存储要更新的数据。
ContentValues values = new ContentValues();
// 将 NoteColumns.WIDGET_ID 列的值设置为 AppWidgetManager.INVALID_APPWIDGET_ID
// 表示该小部件已被删除。
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
// 遍历被删除的小部件 ID 数组。
for (int i = 0; i < appWidgetIds.length; i++) {
// 使用 ContentResolver 更新 Notes.CONTENT_NOTE_URI 数据库中的记录。
// 更新条件是 NoteColumns.WIDGET_ID 列的值等于当前小部件的 ID。
// 传递要更新的值、更新条件和条件参数。
context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
values,
NoteColumns.WIDGET_ID + "=?",
new String[]{String.valueOf(appWidgetIds[i])});
}
}
// 定义一个私有方法 getNoteWidgetInfo用于获取指定小部件 ID 的小部件信息。
// 参数 context 为当前上下文widgetId 为小部件的 ID。
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
// 使用 ContentResolver 查询 Notes.CONTENT_NOTE_URI 数据库中的记录。
// 指定查询的列PROJECTION、查询条件和条件参数。
// 查询条件是 NoteColumns.WIDGET_ID 列的值等于 widgetId并且 NoteColumns.PARENT_ID 列的值不等于 Notes.ID_TRASH_FOLER。
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION,
NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "!=?",
new String[]{String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER)},
null);
}
// 定义一个受保护的方法 update用于更新小部件。
// 参数 context 为当前上下文appWidgetManager 为 AppWidgetManager 实例appWidgetIds 为小部件的 ID 数组。
// 此方法默认调用另一个 update 方法,并传递 privacyMode 为 false。
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
update(context, appWidgetManager, appWidgetIds, false);
}
// 定义一个私有方法 update用于更新小部件。
// 参数 context 为当前上下文appWidgetManager 为 AppWidgetManager 实例appWidgetIds 为小部件的 ID 数组privacyMode 为布尔值,表示是否处于隐私模式。
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
boolean privacyMode) {
// 遍历要更新的小部件 ID 数组。
for (int i = 0; i < appWidgetIds.length; i++) {
// 检查当前小部件 ID 是否有效(即不等于 AppWidgetManager.INVALID_APPWIDGET_ID
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
// 获取默认的背景 ID。
int bgId = ResourceParser.getDefaultBgId(context);
// 初始化摘要字符串。
String snippet = "";
// 创建一个 Intent 对象,用于启动 NoteEditActivity。
Intent intent = new Intent(context, NoteEditActivity.class);
// 设置 Intent 的标志为 FLAG_ACTIVITY_SINGLE_TOP表示如果目标任务已在运行则不重新创建 Activity 实例。
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// 将小部件的 ID 作为额外数据传递给 Intent。
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]);
// 将小部件的类型作为额外数据传递给 Intent。
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());
// 获取指定小部件 ID 的小部件信息。
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
// 检查 Cursor 是否为空,并且移动到第一行。
if (c != null && c.moveToFirst()) {
// 如果查询结果有多于一条记录,记录错误日志。
if (c.getCount() > 1) {
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
// 关闭 Cursor释放资源。
c.close();
// 返回,停止进一步的更新操作。
return;
}
// 获取摘要内容。
snippet = c.getString(COLUMN_SNIPPET);
// 获取背景颜色 ID。
bgId = c.getInt(COLUMN_BG_COLOR_ID);
// 将笔记的 ID 作为额外数据传递给 Intent。
intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID));
// 设置 Intent 的动作为 ACTION_VIEW表示查看笔记。
intent.setAction(Intent.ACTION_VIEW);
} else {
// 如果没有查询到记录,设置摘要内容为提示信息。
snippet = context.getResources().getString(R.string.widget_havenot_content);
// 设置 Intent 的动作为 ACTION_INSERT_OR_EDIT表示插入或编辑笔记。
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
}
// 如果 Cursor 不为空,关闭 Cursor释放资源。
if (c != null) {
c.close();
}
// 创建一个 RemoteViews 对象,用于定义小部件的外观。
// 传递当前上下文的包名和布局资源 ID。
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
// 设置 RemoteViews 中的背景图像资源 ID。
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
// 将背景 ID 作为额外数据传递给 Intent。
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
/**
* 宿 Activity PendingIntent
*/
// 定义一个 PendingIntent 对象,用于处理小部件的点击事件。
PendingIntent pendingIntent = null;
// 如果处于隐私模式,设置文本内容为隐私模式提示,并创建 PendingIntent 启动 NotesListActivity。
if (privacyMode) {
rv.setTextViewText(R.id.widget_text,
context.getString(R.string.widget_under_visit_mode));
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent(
context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
} else {
// 如果不处于隐私模式,设置文本内容为摘要内容,并创建 PendingIntent 启动 NoteEditActivity。
rv.setTextViewText(R.id.widget_text, snippet);
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
// 设置 RemoteViews 中的文本视图的点击事件为 PendingIntent。
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
// 使用 AppWidgetManager 更新指定 ID 的小部件。
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
}
}
// 定义一个抽象方法 getBgResourceId用于获取背景资源 ID。
// 参数 bgId 为背景 ID。
// 子类需要实现此方法。
protected abstract int getBgResourceId(int bgId);
// 定义一个抽象方法 getLayoutId用于获取小部件的布局资源 ID。
// 子类需要实现此方法。
protected abstract int getLayoutId();
// 定义一个抽象方法 getWidgetType用于获取小部件的类型。
// 子类需要实现此方法。
protected abstract int getWidgetType();
}

@ -1,63 +0,0 @@
/*
* 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.widget;//faadadadad
import android.appwidget.AppWidgetManager;//sfdfsfwerwr
import android.content.Context;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.tool.ResourceParser;
// 定义一个名为 NoteWidgetProvider_2x 的公共类,该类继承自 NoteWidgetProvider 类。
public class NoteWidgetProvider_2x extends NoteWidgetProvider {
// 重写父类 NoteWidgetProvider 中的 onUpdate 方法。
// 此方法在小部件被添加到主屏幕、小部件被更新或小部件的尺寸发生变化时会被调用。
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// 调用父类的 update 方法,这里应该是 super.onUpdate(...) 的正确写法,但代码中写成了 super.update(...)
// 传递当前上下文、AppWidgetManager 实例以及小部件的 ID 数组,以便更新小部件。
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
// 重写父类 NoteWidgetProvider 中的 getLayoutId 方法。
// 此方法用于返回当前小部件的布局资源 ID。
@Override
protected int getLayoutId() {
// 返回 R.layout.widget_2x即使用 widget_2x 布局文件来显示此小部件。
return R.layout.widget_2x;
}
// 重写父类 NoteWidgetProvider 中的 getBgResourceId 方法。
// 此方法用于根据背景 ID 获取相应的背景资源 ID。
@Override
protected int getBgResourceId(int bgId) {
// 调用 ResourceParser.WidgetBgResources 类的 getWidget2xBgResource 方法,
// 传递背景 ID 参数,返回对应 2x 尺寸小部件的背景资源 ID。
return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId);
}
// 重写父类 NoteWidgetProvider 中的 getWidgetType 方法。
// 此方法用于返回当前小部件的类型。
@Override
protected int getWidgetType() {
// 返回 Notes.TYPE_WIDGET_2X表示此小部件的类型为 2x 类型的小部件。
return Notes.TYPE_WIDGET_2X;
}
}

@ -1,63 +0,0 @@
/*
* 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.widget;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.tool.ResourceParser;
// 定义一个名为 NoteWidgetProvider_4x 的公共类,该类继承自 NoteWidgetProvider 类。
public class NoteWidgetProvider_4x extends NoteWidgetProvider {
// 重写父类 NoteWidgetProvider 中的 onUpdate 方法。
// 此方法在小部件被添加到主屏幕、小部件被更新或小部件的尺寸发生变化时会被调用。
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// 调用父类的 update 方法传递当前上下文、AppWidgetManager 实例以及小部件的 ID 数组,
// 以便更新小部件。
super.update(context, appWidgetManager, appWidgetIds);
}
// 重写父类 NoteWidgetProvider 中的 getLayoutId 方法。
// 此方法用于返回当前小部件的布局资源 ID。
@Override
protected int getLayoutId() {
// 返回 R.layout.widget_4x即使用 widget_4x 布局文件来显示此小部件。
return R.layout.widget_4x;
}
// 重写父类 NoteWidgetProvider 中的 getBgResourceId 方法。
// 此方法用于根据背景 ID 获取相应的背景资源 ID。
@Override
protected int getBgResourceId(int bgId) {
// 调用 ResourceParser.WidgetBgResources 类的 getWidget4xBgResource 方法,
// 传递背景 ID 参数,返回对应 4x 尺寸小部件的背景资源 ID。
return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId);
}
// 重写父类 NoteWidgetProvider 中的 getWidgetType 方法。
// 此方法用于返回当前小部件的类型。
@Override
protected int getWidgetType() {
// 返回 Notes.TYPE_WIDGET_4X表示此小部件的类型为 4x 类型的小部件。
return Notes.TYPE_WIDGET_4X;
}
}
Loading…
Cancel
Save