Git-tzf
pj36foInf 3 years ago
parent 8e050b9c45
commit 252d72e6ec

@ -0,0 +1,345 @@
/*
* 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.Context;
import android.database.Cursor;
import android.os.Environment;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class BackupUtils {
private static final String TAG = "BackupUtils";//定义一个常量,用于日志输出的标签
// Singleton stuff
private static BackupUtils sInstance; // 定义一个静态变量,用于保存单例对象的引用
public static synchronized BackupUtils getInstance(Context context) {// 定义一个静态同步方法,用于获取单例对象的实例
if (sInstance == null) {// 如果单例对象还没有创建
sInstance = new BackupUtils(context);// 就用传入的上下文参数创建一个新的单例对象
}
return sInstance;// 返回单例对象的引用
}
/**
* Following states are signs to represents backup or restore
* status
*/
// Currently, the sdcard is not mounted
public static final int STATE_SD_CARD_UNMOUONTED = 0;// 定义一个常量,表示当前的状态是 SD 卡没有挂载
// The backup file not exist
public static final int STATE_BACKUP_FILE_NOT_EXIST = 1;// 定义一个常量,表示当前的状态是备份文件不存在
// The data is not well formated, may be changed by other programs
public static final int STATE_DATA_DESTROIED = 2;// 定义一个常量,表示当前的状态是数据格式不正确,可能被其他程序修改
// Some run-time exception which causes restore or backup fails
public static final int STATE_SYSTEM_ERROR = 3;// 定义一个常量,表示当前的状态是系统错误,导致恢复或备份失败
// Backup or restore success
public static final int STATE_SUCCESS = 4;// 定义一个常量,表示当前的状态是恢复或备份成功
private TextExport mTextExport;// 定义一个私有变量,用于保存一个 TextExport 对象的引用
private BackupUtils(Context context) {
// 定义一个私有构造器,用于创建 BackupUtils 对象mTextExport = new TextExport(context);// 用传入的上下文参数创建一个 TextExport 对象,并赋值给 mTextExport 变量
}
private static boolean externalStorageAvailable() {// 定义一个私有静态方法,用于判断外部存储是否可用
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}
public int exportToText() {
// 定义一个公有方法,用于导出文本文件
return mTextExport.exportToText();//调用 mTextExport 对象的 exportToText 方法,并返回其结果
}
public String getExportedTextFileName() {// 定义一个公有方法,用于获取导出的文本文件名
return mTextExport.mFileName;// 返回 mTextExport 对象的 mFileName 变量的值
}
public String getExportedTextFileDir() {// 定义一个公有方法,用于获取导出的文本文件目录
return mTextExport.mFileDirectory;// 返回 mTextExport 对象的 mFileDirectory 变量的值
}
private static class TextExport {// 定义一个私有静态内部类,用于实现文本文件的导出功能
private static final String[] NOTE_PROJECTION = { // 定义一个私有静态常量数组,用于指定查询笔记表时需要返回的列名
NoteColumns.ID,
NoteColumns.MODIFIED_DATE,
NoteColumns.SNIPPET,
NoteColumns.TYPE
};
private static final int NOTE_COLUMN_ID = 0;// 定义一个私有静态常量,表示笔记 ID 列在 NOTE_PROJECTION 数组中的索引
private static final int NOTE_COLUMN_MODIFIED_DATE = 1;// 定义一个私有静态常量,表示笔记修改日期列在 NOTE_PROJECTION 数组中的索引
private static final int NOTE_COLUMN_SNIPPET = 2; // 定义一个私有静态常量,表示笔记摘要列在 NOTE_PROJECTION 数组中的索引
private static final String[] DATA_PROJECTION = {// 定义一个私有静态常量数组,用于指定查询数据表时需要返回的列名
DataColumns.CONTENT,
DataColumns.MIME_TYPE,
DataColumns.DATA1,
DataColumns.DATA2,
DataColumns.DATA3,
DataColumns.DATA4,
};
private static final int DATA_COLUMN_CONTENT = 0;// 定义一个私有静态常量,表示数据内容列在 DATA_PROJECTION 数组中的索引
private static final int DATA_COLUMN_MIME_TYPE = 1;// 定义一个私有静态常量,表示数据 MIME 类型列在 DATA_PROJECTION 数组中的索引
private static final int DATA_COLUMN_CALL_DATE = 2;// 定义一个私有静态常量,表示数据 MIME 类型列在 DATA_PROJECTION 数组中的索引
private static final int DATA_COLUMN_PHONE_NUMBER = 4;// 定义一个私有静态常量,表示数据 3 列在 DATA_PROJECTION 数组中的索引,用于存储电话号码
private final String [] TEXT_FORMAT;// 定义一个私有不可变数组,用于存储文本文件的格式字符串
private static final int FORMAT_FOLDER_NAME = 0;// 定义一个私有静态常量,表示文件夹名称格式在 TEXT_FORMAT 数组中的索引
private static final int FORMAT_NOTE_DATE = 1;// 定义一个私有静态常量,表示笔记日期格式在 TEXT_FORMAT 数组中的索引
private static final int FORMAT_NOTE_CONTENT = 2;// 定义一个私有静态常量,表示笔记内容格式在 TEXT_FORMAT 数组中的索引
private Context mContext;// 定义一个私有变量,用于保存上下文对象的引用
private String mFileName;// 定义一个私有变量,用于保存导出的文本文件名
private String mFileDirectory;// 定义一个私有变量,用于保存导出的文本文件目录
public TextExport(Context context) {// 定义一个公有构造器,用于创建 TextExport 对象
TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note);// 用传入的上下文参数获取资源数组,并赋值给 TEXT_FORMAT 数组
mContext = context; // 用传入的上下文参数赋值给 mContext 变量
mFileName = "";// 初始化 mFileName 变量为空字符串
mFileDirectory = "";// 初始化 mFileDirectory 变量为空字符串
}
private String getFormat(int id) {
return TEXT_FORMAT[id];
}// 定义一个私有方法,用于根据索引获取格式字符串
/**
* Export the folder identified by folder id to text
*/
private void exportFolderToText(String folderId, PrintStream ps) {// 定义一个私有方法,用于导出指定文件夹 ID 的文本文件,需要传入文件夹 ID 和打印流对象作为参数
// Query notes belong to this folder
Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI,// 用 mContext 变量获取内容解析器,并查询笔记表的 URI
NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] {// 指定需要返回的列名数组为 NOTE_PROJECTION
folderId
}, null);
if (notesCursor != null) {
if (notesCursor.moveToFirst()) {
do {
// Print note's last modified date
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(// 用打印流对象打印一行字符串,格式化为笔记日期格式,内容为游标当前行的修改日期列的值,转换为指定的日期时间格式
mContext.getString(R.string.format_datetime_mdhm),
notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));// 用游标获取当前行的修改日期列的值,转换为长整型
// Query data belong to this note
String noteId = notesCursor.getString(NOTE_COLUMN_ID);// 用游标获取当前行的笔记 ID 列的值,转换为字符串,并赋值给 noteId 变量
exportNoteToText(noteId, ps);// 调用 exportNoteToText 方法,传入笔记 ID 和打印流对象作为参数,导出该笔记的文本文件
} while (notesCursor.moveToNext());// 循环条件为游标移动到下一行,直到没有更多行为止
}
notesCursor.close();//关闭游标对象,释放资源
}
}
/**
* Export note identified by id to a print stream
*/
private void exportNoteToText(String noteId, PrintStream ps) { // 定义一个私有方法,用于导出指定笔记 ID 的文本文件,需要传入笔记 ID 和打印流对象作为参数
Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI,// 用 mContext 变量获取内容解析器,并查询数据表的 URI
DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {// 指定查询条件为笔记 ID 等于传入的笔记 ID noteId }, null); // 指定排序方式为 null
noteId
}, null);
if (dataCursor != null) { // 如果数据游标不为空
if (dataCursor.moveToFirst()) { // 如果数据游标移动到第一行
do {
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); // 获取数据的类型
if (DataConstants.CALL_NOTE.equals(mimeType)) { // 如果数据是通话记录
// 打印电话号码
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); // 获取电话号码
long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); // 获取通话日期
String location = dataCursor.getString(DATA_COLUMN_CONTENT); // 获取通话附件位置
if (!TextUtils.isEmpty(phoneNumber)) { // 如果电话号码不为空
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
phoneNumber)); // 格式化并打印电话号码
}
// 打印通话日期
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat
.format(mContext.getString(R.string.format_datetime_mdhm),
callDate))); // 格式化并打印通话日期
// 打印通话附件位置
if (!TextUtils.isEmpty(location)) { // 如果通话附件位置不为空
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
location)); // 格式化并打印通话附件位置
}
} else if (DataConstants.NOTE.equals(mimeType)) { // 如果数据是便签
String content = dataCursor.getString(DATA_COLUMN_CONTENT); // 获取便签内容
if (!TextUtils.isEmpty(content)) { // 如果便签内容不为空
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
content)); // 格式化并打印便签内容
}
}
} while (dataCursor.moveToNext()); // 循环直到数据游标移动到最后一行
}
dataCursor.close(); // 关闭数据游标
}
// print a line separator between note
try {
ps.write(new byte[] {
Character.LINE_SEPARATOR, Character.LETTER_NUMBER
}); // 尝试写入一个字节数组,包含换行符和字母数字
} catch (IOException e) { // 如果发生输入输出异常
Log.e(TAG, e.toString()); // 打印错误日志
}
}
/**
* Note will be exported as text which is user readable
*/
public int exportToText() { // 定义一个导出文本的方法
if (!externalStorageAvailable()) { // 如果外部存储不可用
Log.d(TAG, "Media was not mounted"); // 打印调试日志
return STATE_SD_CARD_UNMOUONTED; // 返回SD卡未挂载的状态
}
PrintStream ps = getExportToTextPrintStream(); // 获取导出文本的打印流
if (ps == null) { // 如果打印流为空
Log.e(TAG, "get print stream error"); // 打印错误日志
return STATE_SYSTEM_ERROR; // 返回系统错误的状态
}
// 首先导出文件夹和它们的便签
Cursor folderCursor = mContext.getContentResolver().query(
Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION,
"(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND "
+ NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR "
+ NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); // 查询文件夹类型的便签,排除回收站和通话记录文件夹
if (folderCursor != null) { // 如果文件夹游标不为空
if (folderCursor.moveToFirst()) { // 如果文件夹游标移动到第一行
do {
// 打印文件夹的名字
String folderName = "";
if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { // 如果文件夹是通话记录文件夹
folderName = mContext.getString(R.string.call_record_folder_name); // 获取通话记录文件夹的名字
} else { // 否则
folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); // 获取文件夹的摘要作为名字
}
if (!TextUtils.isEmpty(folderName)) { // 如果文件夹名字不为空
ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); // 格式化并打印文件夹名字
}
String folderId = folderCursor.getString(NOTE_COLUMN_ID); // 获取文件夹的ID
exportFolderToText(folderId, ps); // 调用导出文件夹到文本的方法传入文件夹ID和打印流
} while (folderCursor.moveToNext()); // 循环直到文件夹游标移动到最后一行
}
folderCursor.close(); // 关闭文件夹游标
}
// Export notes in root's folder
Cursor noteCursor = mContext.getContentResolver().query(
Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION,
NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID
+ "=0", null, null); // 查询便签类型的便签,且没有父文件夹
if (noteCursor != null) { // 如果便签游标不为空
if (noteCursor.moveToFirst()) { // 如果便签游标移动到第一行
do {
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
mContext.getString(R.string.format_datetime_mdhm),
noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); // 格式化并打印便签的修改日期
// 查询属于这个便签的数据
String noteId = noteCursor.getString(NOTE_COLUMN_ID); // 获取便签的ID
exportNoteToText(noteId, ps); // 调用导出便签到文本的方法传入便签ID和打印流
} while (noteCursor.moveToNext()); // 循环直到便签游标移动到最后一行
}
noteCursor.close(); // 关闭便签游标
}
ps.close(); // 关闭打印流
return STATE_SUCCESS; // 返回成功的状态
}
/**
* Get a print stream pointed to the file {@generateExportedTextFile}
*/
private PrintStream getExportToTextPrintStream() { // 定义一个获取导出文本的打印流的方法
File file = generateFileMountedOnSDcard(mContext, R.string.file_path,
R.string.file_name_txt_format); // 调用生成挂载在SD卡上的文件的方法传入上下文文件路径和文件名格式
if (file == null) { // 如果文件为空
Log.e(TAG, "create file to exported failed"); // 打印错误日志
return null; // 返回空值
}
mFileName = file.getName(); // 获取文件的名字
mFileDirectory = mContext.getString(R.string.file_path); // 获取文件的目录
PrintStream ps = null; // 声明一个打印流变量
try {
FileOutputStream fos = new FileOutputStream(file); // 创建一个文件输出流,传入文件
ps = new PrintStream(fos); // 创建一个打印流,传入文件输出流
} catch (FileNotFoundException e) { // 如果发生文件未找到异常
e.printStackTrace(); // 打印异常堆栈
return null; // 返回空值
} catch (NullPointerException e) { // 如果发生空指针异常
e.printStackTrace(); // 打印异常堆栈
return null; // 返回空值
}
return ps; // 返回打印流
}
}
/**
* Generate the text file to store imported data
*/
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { // 定义一个生成挂载在SD卡上的文件的方法传入上下文文件路径资源ID和文件名格式资源ID
StringBuilder sb = new StringBuilder(); // 创建一个字符串构建器
sb.append(Environment.getExternalStorageDirectory()); // 追加外部存储目录
sb.append(context.getString(filePathResId)); // 追加文件路径字符串
File filedir = new File(sb.toString()); // 创建一个文件目录对象,传入字符串构建器的内容
sb.append(context.getString(
fileNameFormatResId,
DateFormat.format(context.getString(R.string.format_date_ymd),
System.currentTimeMillis()))); // 追加文件名字符串,根据日期格式化
File file = new File(sb.toString()); // 创建一个文件对象,传入字符串构建器的内容
try {
if (!filedir.exists()) { // 如果文件目录不存在
filedir.mkdir(); // 创建文件目录
}
if (!file.exists()) { // 如果文件不存在
file.createNewFile(); // 创建新文件
}
return file; // 返回文件对象
} catch (SecurityException e) { // 如果发生安全异常
e.printStackTrace(); // 打印异常堆栈
} catch (IOException e) { // 如果发生输入输出异常
e.printStackTrace(); // 打印异常堆栈
}
return null; // 返回空值
}
}

@ -0,0 +1,306 @@
/*
* 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) { // 定义一个静态方法批量删除便签传入内容解析器和便签ID的集合
if (ids == null) { // 如果ID集合为空
Log.d(TAG, "the ids is null"); // 打印调试日志
return true; // 返回真值
}
if (ids.size() == 0) { // 如果ID集合的大小为零
Log.d(TAG, "no id is in the hashset"); // 打印调试日志
return true; // 返回真值
}
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); // 创建一个内容提供者操作的列表
for (long id : ids) { // 遍历ID集合中的每个ID
if(id == Notes.ID_ROOT_FOLDER) { // 如果ID是根文件夹的ID
Log.e(TAG, "Don't delete system folder root"); // 打印错误日志
continue; // 跳过本次循环
}
ContentProviderOperation.Builder builder = ContentProviderOperation
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); // 创建一个内容提供者操作的构建器指定删除便签的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()); // 打印调试日志显示失败的ID集合
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) { // 定义一个静态方法移动便签到文件夹传入内容解析器便签ID源文件夹ID和目标文件夹ID
ContentValues values = new ContentValues(); // 创建一个内容值对象
values.put(NoteColumns.PARENT_ID, desFolderId); // 把目标文件夹ID作为父ID放入内容值中
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); // 把源文件夹ID作为原始父ID放入内容值中
values.put(NoteColumns.LOCAL_MODIFIED, 1); // 把本地修改标志设为1放入内容值中
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); // 调用内容解析器的更新方法传入便签的URI和ID内容值空的选择和选择参数
}
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
long folderId) { // 定义一个静态方法批量移动到文件夹传入内容解析器便签ID的集合和文件夹ID
if (ids == null) { // 如果ID集合为空
Log.d(TAG, "the ids is null"); // 打印调试日志
return true; // 返回真值
}
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); // 创建一个内容提供者操作的列表
for (long id : ids) { // 遍历ID集合中的每个ID
ContentProviderOperation.Builder builder = ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); // 创建一个内容提供者操作的构建器指定更新便签的URI和ID
builder.withValue(NoteColumns.PARENT_ID, folderId); // 把文件夹ID作为父ID放入构建器中
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); // 把本地修改标志设为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()); // 打印调试日志显示失败的ID集合
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); // 查询便签的URI返回文件夹类型且不在回收站的便签的数量
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) { // 定义一个静态方法判断便签是否在数据库中可见传入内容解析器便签ID和类型
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); // 查询便签的URI和ID返回指定类型且不在回收站的便签
boolean exist = false; // 声明一个布尔型变量,表示是否存在
if (cursor != null) { // 如果游标不为空
if (cursor.getCount() > 0) { // 如果游标的数量大于零
exist = true; // 把存在设为真值
}
cursor.close(); // 关闭游标
}
return exist; // 返回是否存在
}
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {
// 查询便签的URI和ID返回便签的所有列
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) {
// 查询数据的URI和ID返回数据的所有列
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) {
// 查询便签的URI返回文件夹类型且不在回收站且名称等于指定值的便签
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) {
// 查询便签的URI返回指定父ID的便签的小部件ID和类型
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
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) {
// 查询数据的URI返回指定便签ID和MIME类型的数据的电话号码
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) {
// 查询数据的URI返回指定通话日期、MIME类型和电话号码的数据的便签ID
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); // 返回游标的第一列的值即便签ID
} catch (IndexOutOfBoundsException e) { // 如果发生索引越界异常
Log.e(TAG, "Get call note id fails " + e.toString()); // 打印错误日志,显示异常信息
}
}
cursor.close(); // 关闭游标
}
return 0; // 返回0
}
public static String getSnippetById(ContentResolver resolver, long noteId) {
// 查询便签的URI返回指定ID的便签的摘要
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; // 返回格式化后的摘要字符串
}
}

@ -0,0 +1,115 @@
/*
* 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;
public class GTaskStringUtils {
// 定义一些常量字符串表示Google任务的JSON格式的属性名
public final static String GTASK_JSON_ACTION_ID = "action_id"; // 动作ID
public final static String GTASK_JSON_ACTION_LIST = "action_list"; // 动作列表
public final static String GTASK_JSON_ACTION_TYPE = "action_type"; // 动作类型
public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; // 创建动作
public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; // 获取所有动作
public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; // 移动动作
public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; // 更新动作
public final static String GTASK_JSON_CREATOR_ID = "creator_id"; // 创建者ID
public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; // 子实体
public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; // 客户端版本
public final static String GTASK_JSON_COMPLETED = "completed"; // 完成状态
public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; // 当前列表ID
public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; // 默认列表ID
public final static String GTASK_JSON_DELETED = "deleted"; // 删除状态
public final static String GTASK_JSON_DEST_LIST = "dest_list"; // 目标列表
public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; // 目标父实体
public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; // 目标父实体类型
public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; // 实体变化
public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; // 实体类型
public final static String GTASK_JSON_GET_DELETED = "get_deleted"; // 获取删除状态
public final static String GTASK_JSON_ID = "id"; // ID
public final static String GTASK_JSON_INDEX = "index"; // 索引
public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; // 最后修改时间
public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; // 最新同步点
public final static String GTASK_JSON_LIST_ID = "list_id"; // 列表ID
public final static String GTASK_JSON_LISTS = "lists"; // 列表
public final static String GTASK_JSON_NAME = "name"; // 名称
public final static String GTASK_JSON_NEW_ID = "new_id"; // 新ID
public final static String GTASK_JSON_NOTES = "notes"; // 便签
public final static String GTASK_JSON_PARENT_ID = "parent_id"; // 父ID
public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; // 前一个兄弟ID
public final static String GTASK_JSON_RESULTS = "results"; // 结果
public final static String GTASK_JSON_SOURCE_LIST = "source_list"; // 源列表
public final static String GTASK_JSON_TASKS = "tasks"; // 任务
public final static String GTASK_JSON_TYPE = "type"; // 类型
public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; // 组类型
public final static String GTASK_JSON_TYPE_TASK = "TASK"; // 任务类型
public final static String GTASK_JSON_USER = "user"; // 用户
public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; // MIUI文件夹的前缀
public final static String FOLDER_DEFAULT = "Default"; // 默认文件夹的名称
public final static String FOLDER_CALL_NOTE = "Call_Note"; // 通话便签文件夹的名称
public final static String FOLDER_META = "METADATA"; // 元数据文件夹的名称
public final static String META_HEAD_GTASK_ID = "meta_gid"; // 元数据中的Google任务ID的头部
public final static String META_HEAD_NOTE = "meta_note"; // 元数据中的便签的头部
public final static String META_HEAD_DATA = "meta_data"; // 元数据中的数据的头部
public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; // 元数据便签的名称,提示不要更新和删除
}

@ -0,0 +1,214 @@
/*
* 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.Context;
import android.preference.PreferenceManager;
import net.micode.notes.R;
import net.micode.notes.ui.NotesPreferenceActivity;
public class ResourceParser {
// 定义一些常量整型,表示便签的背景颜色
public static final int YELLOW = 0;
public static final int BLUE = 1;
public static final int WHITE = 2;
public static final int GREEN = 3;
public static final int RED = 4;
// 定义一个常量整型,表示默认的背景颜色
public static final int BG_DEFAULT_COLOR = YELLOW;
// 定义一些常量整型,表示便签的文字大小
public static final int TEXT_SMALL = 0;
public static final int TEXT_MEDIUM = 1;
public static final int TEXT_LARGE = 2;
public static final int TEXT_SUPER = 3;
// 定义一个常量整型,表示默认的文字大小
public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM;
// 定义一个静态内部类,用于存储便签的背景资源
public static class NoteBgResources {
// 定义一个静态整型数组,表示编辑模式下的便签背景资源
private final static int [] BG_EDIT_RESOURCES = new int [] {
R.drawable.edit_yellow,
R.drawable.edit_blue,
R.drawable.edit_white,
R.drawable.edit_green,
R.drawable.edit_red
};
// 定义一个静态整型数组,表示编辑模式下的便签标题背景资源
private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] {
R.drawable.edit_title_yellow,
R.drawable.edit_title_blue,
R.drawable.edit_title_white,
R.drawable.edit_title_green,
R.drawable.edit_title_red
};
// 定义一个静态方法根据ID获取便签背景资源
public static int getNoteBgResource(int id) {
return BG_EDIT_RESOURCES[id];
}
// 定义一个静态方法根据ID获取便签标题背景资源
public static int getNoteTitleBgResource(int id) {
return BG_EDIT_TITLE_RESOURCES[id];
}
}
// 定义一个静态方法获取默认的背景ID
public static int getDefaultBgId(Context context) {
// 如果用户设置了随机背景颜色
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) {
// 返回一个随机的背景ID
return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length);
} else {
// 否则返回默认的背景ID
return BG_DEFAULT_COLOR;
}
}
// 这是一个公共静态类,用于存储笔记项的背景资源
public static class NoteItemBgResources {
// 这是一个整型数组,用于存储第一个笔记项的背景资源
private final static int [] BG_FIRST_RESOURCES = new int [] {
R.drawable.list_yellow_up,
R.drawable.list_blue_up,
R.drawable.list_white_up,
R.drawable.list_green_up,
R.drawable.list_red_up
};
// 这是一个整型数组,用于存储中间笔记项的背景资源
private final static int [] BG_NORMAL_RESOURCES = new int [] {
R.drawable.list_yellow_middle,
R.drawable.list_blue_middle,
R.drawable.list_white_middle,
R.drawable.list_green_middle,
R.drawable.list_red_middle
};
// 这是一个整型数组,用于存储最后一个笔记项的背景资源
private final static int [] BG_LAST_RESOURCES = new int [] {
R.drawable.list_yellow_down,
R.drawable.list_blue_down,
R.drawable.list_white_down,
R.drawable.list_green_down,
R.drawable.list_red_down,
};
// 这是一个整型数组,用于存储单个笔记项的背景资源
private final static int [] BG_SINGLE_RESOURCES = new int [] {
R.drawable.list_yellow_single,
R.drawable.list_blue_single,
R.drawable.list_white_single,
R.drawable.list_green_single,
R.drawable.list_red_single
};
// 这是一个公共静态方法用于根据id返回第一个笔记项的背景资源
public static int getNoteBgFirstRes(int id) {
return BG_FIRST_RESOURCES[id];
}
// 这是一个公共静态方法用于根据id返回最后一个笔记项的背景资源
public static int getNoteBgLastRes(int id) {
return BG_LAST_RESOURCES[id];
}
// 这是一个公共静态方法用于根据id返回单个笔记项的背景资源
public static int getNoteBgSingleRes(int id) {
return BG_SINGLE_RESOURCES[id];
}
// 这是一个公共静态方法用于根据id返回中间笔记项的背景资源
public static int getNoteBgNormalRes(int id) {
return BG_NORMAL_RESOURCES[id];
}
// 这是一个公共静态方法,用于返回文件夹的背景资源
// 这是一个公共静态方法,用于返回文件夹的背景资源
public static int getFolderBgRes() {
return R.drawable.list_folder;
}
}
// 这是一个公共静态类,用于存储小部件的背景资源
public static class WidgetBgResources {
// 这是一个整型数组用于存储2x2小部件的背景资源
private final static int [] BG_2X_RESOURCES = new int [] {
R.drawable.widget_2x_yellow,
R.drawable.widget_2x_blue,
R.drawable.widget_2x_white,
R.drawable.widget_2x_green,
R.drawable.widget_2x_red,
};
// 这是一个公共静态方法用于根据id返回2x2小部件的背景资源
public static int getWidget2xBgResource(int id) {
return BG_2X_RESOURCES[id];
}
// 这是一个整型数组用于存储4x4小部件的背景资源
private final static int [] BG_4X_RESOURCES = new int [] {
R.drawable.widget_4x_yellow,
R.drawable.widget_4x_blue,
R.drawable.widget_4x_white,
R.drawable.widget_4x_green,
R.drawable.widget_4x_red
};
// 这是一个公共静态方法用于根据id返回4x4小部件的背景资源
public static int getWidget4xBgResource(int id) {
return BG_4X_RESOURCES[id];
}
}
// 这是一个公共静态类,用于存储文本外观的资源
public static class TextAppearanceResources {
// 这是一个整型数组,用于存储不同大小的文本外观资源
private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] {
R.style.TextAppearanceNormal,
R.style.TextAppearanceMedium,
R.style.TextAppearanceLarge,
R.style.TextAppearanceSuper
};
// 这是一个公共静态方法用于根据id返回文本外观资源
public static int getTexAppearanceResource(int id) {
/**
* HACKME: Fix bug of store the resource id in shared preference.
* The id may larger than the length of resources, in this case,
* return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE}
*/
if (id >= TEXTAPPEARANCE_RESOURCES.length) {
return BG_DEFAULT_FONT_SIZE;
}
return TEXTAPPEARANCE_RESOURCES[id];
}
// 这是一个公共静态方法,用于返回文本外观资源的大小
public static int getResourcesSize() {
return TEXTAPPEARANCE_RESOURCES.length;
}
}
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,169 @@
/*
* 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;
public abstract class NoteWidgetProvider extends AppWidgetProvider {
//查询便签数据库时使用的列名
public static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.BG_COLOR_ID,
NoteColumns.SNIPPET
};
//PROJECTION中对应列的下标
public static final int COLUMN_ID = 0;
public static final int COLUMN_BG_COLOR_ID = 1;
public static final int COLUMN_SNIPPET = 2;
//Log输出的标签
private static final String TAG = "NoteWidgetProvider";
/*
* 重写onDeleted()方法,实现删除 Widget 时清除相应 Widget 对应的便签数据库中的记录
* value用于更新的ContentValues将 Widget ID 设为无效值
* appWidgetIds被删除的所有 Widget 的 ID 数组
*/
@Override
// 定义一个方法用于在widget被删除时更新数据库中的widget_id字段
public void onDeleted(Context context, int[] appWidgetIds) {
// 创建一个ContentValues对象用于存放要更新的字段和值
ContentValues values = new ContentValues();
// 把widget_id设置为无效的值
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
//遍历所有被删除的widget的id
for (int i = 0; i < appWidgetIds.length; i++) {
// 根据widget_id更新数据库中对应的笔记记录
context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
values,
NoteColumns.WIDGET_ID + "=?",
new String[] { String.valueOf(appWidgetIds[i])});
}
}
// 定义一个方法用于根据widget_id查询数据库中的笔记信息
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
// 使用ContentResolver查询笔记表返回一个Cursor对象
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);
}
// 定义一个方法用于更新widget的视图
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// 调用另一个重载的update方法传入false表示不是访客模式
update(context, appWidgetManager, appWidgetIds, false);
}
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
boolean privacyMode) {
for (int i = 0; i < appWidgetIds.length; i++) {
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
// 获取 Widget 的默认背景 ID
int bgId = ResourceParser.getDefaultBgId(context);
// 默认便签摘要为空字符串
String snippet = "";
// 创建用于启动编辑页面的 Intent并添加必要的参数
Intent intent = new Intent(context, NoteEditActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]);
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());
// 从数据库中查询指定 Widget ID 对应的便签信息
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
if (c != null && c.moveToFirst()) {
// 检查查询结果是否出现异常,如出现异常则打印错误日志和异常信息
if (c.getCount() > 1) {
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
c.close();
return;
}
// 设置 Widget 中需要显示的便签摘要、背景 ID、绑定的便签 ID 和点击跳转功能
snippet = c.getString(COLUMN_SNIPPET);
bgId = c.getInt(COLUMN_BG_COLOR_ID);
intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID));
intent.setAction(Intent.ACTION_VIEW);
} else {
// 若查询结果为空,则打开编辑页面并设置其模式为插入模式
snippet = context.getResources().getString(R.string.widget_havenot_content);
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
}
// 关闭查询结果的 Cursor 对象
if (c != null) {
c.close();
}
// 创建 RemoteViews 对象,并为其设置相应布局及需要显示的参数
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
/**
* Generate the pending intent to start host for the widget
*/
// 定义一个变量pendingIntent初始化为null
PendingIntent pendingIntent = null;
// 如果privacyMode为真表示用户处于访客模式
if (privacyMode) {
// 设置widget_text的文本为“访客模式”
rv.setTextViewText(R.id.widget_text,
context.getString(R.string.widget_under_visit_mode));
// 创建一个PendingIntent用于启动NotesListActivity
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent(
context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
} else {
// 否则设置widget_text的文本为snippet即笔记的摘要
rv.setTextViewText(R.id.widget_text, snippet);
// 创建一个PendingIntent用于启动intent指定的Activity
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
// 设置widget_text的点击事件为pendingIntent
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
// 更新widget的视图
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
}
}
// 定义一个抽象方法用于根据bgId返回背景资源的id
protected abstract int getBgResourceId(int bgId);
// 定义一个抽象方法用于返回布局资源的id
protected abstract int getLayoutId();
// 定义一个抽象方法用于返回widget的类型
protected abstract int getWidgetType();
}

@ -0,0 +1,48 @@
/*
* 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; //声明package
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; //引用类
public class NoteWidgetProvider_2x extends NoteWidgetProvider {//继承NoteWidgetProvider类
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.update(context, appWidgetManager, appWidgetIds); //调用NoteWidgetProvider的update方法
}
@Override
protected int getLayoutId() { //重写NoteWidgetProvider中的getLayoutId方法
return R.layout.widget_2x; //返回 widget_2x 布局文件的ID
}
@Override
protected int getBgResourceId(int bgId) { //重写NoteWidgetProvider中的getBgResourceId方法接受传入的bgId参数
return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); //调用ResourceParser类中的getWidget2xBgResource(bgId)方法返回该bgId对应的Widget2xBg资源ID
}
@Override
protected int getWidgetType() { //重写NoteWidgetProvider中的getWidgetType方法
return Notes.TYPE_WIDGET_2X; //返回Note中的TYPE_WIDGET_2X常量值
}
}

@ -0,0 +1,58 @@
/*
* 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;
public class NoteWidgetProvider_4x extends NoteWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// 调用父类中的 update 方法,进行具体的 Widget 内容更新操作
super.update(context, appWidgetManager, appWidgetIds);
}
protected int getLayoutId() {
return R.layout.widget_4x;
}
/**
* 根据传入的背景颜色 ID 获取对应的背景资源 ID
* @param bgId 背景颜色 ID
* @return 对应的背景资源 ID
*/
@Override
protected int getBgResourceId(int bgId) {
return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId);
}
/**
* 获取该 Widget 对应的类型常量,用于在程序中进行判断和处理
* @return 4x4 Widget 对应的类型常量
*/
@Override
protected int getWidgetType() {
return Notes.TYPE_WIDGET_4X;
}
}
Loading…
Cancel
Save