Signed-off-by: dyq <2379414960@qq.com>
DUANYIQUN_BRANCH
dyq 2 years ago
parent fb9d1d63d7
commit 1500746927

@ -33,8 +33,8 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;//////
//添加包。类。对象等
public class BackupUtils { public class BackupUtils {
private static final String TAG = "BackupUtils"; private static final String TAG = "BackupUtils";
@ -42,9 +42,9 @@ public class BackupUtils {
private static BackupUtils sInstance; private static BackupUtils sInstance;
public static synchronized BackupUtils getInstance(Context context) { public static synchronized BackupUtils getInstance(Context context) {
if (sInstance == null) { if (sInstance == null) {//如果实例为空则创建新实例
sInstance = new BackupUtils(context); sInstance = new BackupUtils(context);
} }//返回实例
return sInstance; return sInstance;
} }
@ -72,19 +72,19 @@ public class BackupUtils {
private static boolean externalStorageAvailable() { private static boolean externalStorageAvailable() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
} }
//将笔记·导出文本,返回导出时的状态码
public int exportToText() { public int exportToText() {
return mTextExport.exportToText(); return mTextExport.exportToText();
} }
//获取导出文本文件名
public String getExportedTextFileName() { public String getExportedTextFileName() {
return mTextExport.mFileName; return mTextExport.mFileName;
} }
//获得到处文本文件目录
public String getExportedTextFileDir() { public String getExportedTextFileDir() {
return mTextExport.mFileDirectory; return mTextExport.mFileDirectory;
} }
//负责将笔记导出为文本
private static class TextExport { private static class TextExport {
private static final String[] NOTE_PROJECTION = { private static final String[] NOTE_PROJECTION = {
NoteColumns.ID, NoteColumns.ID,
@ -93,11 +93,11 @@ public class BackupUtils {
NoteColumns.TYPE NoteColumns.TYPE
}; };
private static final int NOTE_COLUMN_ID = 0; private static final int NOTE_COLUMN_ID = 0;//查询结果中笔记id所在列的索引
private static final int NOTE_COLUMN_MODIFIED_DATE = 1; private static final int NOTE_COLUMN_MODIFIED_DATE = 1;//查询结果中笔记上次修改时间所在的索引
private static final int NOTE_COLUMN_SNIPPET = 2; private static final int NOTE_COLUMN_SNIPPET = 2;//查询摘要所在索引
private static final String[] DATA_PROJECTION = { private static final String[] DATA_PROJECTION = {
DataColumns.CONTENT, DataColumns.CONTENT,
@ -106,7 +106,7 @@ public class BackupUtils {
DataColumns.DATA2, DataColumns.DATA2,
DataColumns.DATA3, DataColumns.DATA3,
DataColumns.DATA4, DataColumns.DATA4,
}; };//定义静态不可变数据列
private static final int DATA_COLUMN_CONTENT = 0; private static final int DATA_COLUMN_CONTENT = 0;
@ -114,24 +114,24 @@ public class BackupUtils {
private static final int DATA_COLUMN_CALL_DATE = 2; private static final int DATA_COLUMN_CALL_DATE = 2;
private static final int DATA_COLUMN_PHONE_NUMBER = 4; private static final int DATA_COLUMN_PHONE_NUMBER = 4;//定义以上数据列MIME类呼叫日期号码的索引初始值
private final String [] TEXT_FORMAT; private final String [] TEXT_FORMAT;//定义带有3个元素字符串数组TEXT_FORMAT
private static final int FORMAT_FOLDER_NAME = 0; private static final int FORMAT_FOLDER_NAME = 0;//格式化后目录名称FORMAT_FOLDER_NAME= 0
private static final int FORMAT_NOTE_DATE = 1; private static final int FORMAT_NOTE_DATE = 1;
private static final int FORMAT_NOTE_CONTENT = 2; private static final int FORMAT_NOTE_CONTENT = 2;//格式化后的笔记内容索引为2
private Context mContext; private Context mContext;//声名Context类型和字符串类型
private String mFileName; private String mFileName;//文件名称
private String mFileDirectory; private String mFileDirectory;//文件目录路径
public TextExport(Context context) { public TextExport(Context context) {
TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note);//初始化文件名称变量为控字符串
mContext = context; mContext = context;
mFileName = ""; mFileName = "";
mFileDirectory = ""; mFileDirectory = "";
} }//初始化文件路径mFileDirectory = ""为空
//创建TextExport对象
private String getFormat(int id) { private String getFormat(int id) {
return TEXT_FORMAT[id]; return TEXT_FORMAT[id];
} }
@ -166,39 +166,39 @@ public class BackupUtils {
* Export note identified by id to a print stream * Export note identified by id to a print stream
*/ */
private void exportNoteToText(String noteId, PrintStream ps) { private void exportNoteToText(String noteId, PrintStream ps) {
Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI,//查询CONTENT_DATA_URI对应的数据表
DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {
noteId noteId
}, null); }, null);
//使用DataColumns.NOTE_ID是否为限制查询条件并查询noted对应的数据行
if (dataCursor != null) { if (dataCursor != null) {//若查询不为空,则执行以下代码:
if (dataCursor.moveToFirst()) { if (dataCursor.moveToFirst()) {//移动游标为第一条记录并循环记录
do { do {
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);//获取MIME_TYPE的值
if (DataConstants.CALL_NOTE.equals(mimeType)) { if (DataConstants.CALL_NOTE.equals(mimeType)) {
// Print phone number // Print phone number
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE);
String location = dataCursor.getString(DATA_COLUMN_CONTENT); String location = dataCursor.getString(DATA_COLUMN_CONTENT);//从记录中获取phonenumber和calldate的值并输出号码
if (!TextUtils.isEmpty(phoneNumber)) { if (!TextUtils.isEmpty(phoneNumber)) {//若电话号码不为空
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
phoneNumber)); phoneNumber));//输出电话号码输出到PrintStream实例PS中
} }
// Print call date // Print call date
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat
.format(mContext.getString(R.string.format_datetime_mdhm), .format(mContext.getString(R.string.format_datetime_mdhm),
callDate))); callDate)));//输出通话日期信息到PrintStream中
// Print call attachment location // Print call attachment location
if (!TextUtils.isEmpty(location)) { if (!TextUtils.isEmpty(location)) {//若位置信息不为空
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
location)); location));
} }//输出位置信息到PrintStream实例PS中
} else if (DataConstants.NOTE.equals(mimeType)) { } else if (DataConstants.NOTE.equals(mimeType)) {//若数据类型为Note则执行以下语句
String content = dataCursor.getString(DATA_COLUMN_CONTENT); String content = dataCursor.getString(DATA_COLUMN_CONTENT);//从Cursor中获取信息
if (!TextUtils.isEmpty(content)) { if (!TextUtils.isEmpty(content)) {
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
content)); content));//将内容信息格式化输出rintStream实例PS中
} }
} }
} while (dataCursor.moveToNext()); } while (dataCursor.moveToNext());
@ -210,74 +210,74 @@ public class BackupUtils {
ps.write(new byte[] { ps.write(new byte[] {
Character.LINE_SEPARATOR, Character.LETTER_NUMBER Character.LINE_SEPARATOR, Character.LETTER_NUMBER
}); });
} catch (IOException e) { } catch (IOException e) {//若出现IO异常则廖永Log类的e方法将异常信息输出日志中
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
} }
//使用rintStream实例PS的write方法写入字节数组到输出流中包括换行和字母数字
/** /**
* Note will be exported as text which is user readable * Note will be exported as text which is user readable
*/ */
public int exportToText() { public int exportToText() {//定义exportToText(),返回值类型为整型
if (!externalStorageAvailable()) { if (!externalStorageAvailable()) {
Log.d(TAG, "Media was not mounted"); Log.d(TAG, "Media was not mounted");
return STATE_SD_CARD_UNMOUONTED; return STATE_SD_CARD_UNMOUONTED;
} }
//若外部存储不可用输出日记信息返回表示Media was not mounted
PrintStream ps = getExportToTextPrintStream(); PrintStream ps = getExportToTextPrintStream();//获取打印输出
if (ps == null) { if (ps == null) {
Log.e(TAG, "get print stream error"); Log.e(TAG, "get print stream error");
return STATE_SYSTEM_ERROR; return STATE_SYSTEM_ERROR;
} }//若获取不到打印输出输出get print stream error",并返回错误
// First export folder and its notes // First export folder and its notes
Cursor folderCursor = mContext.getContentResolver().query( Cursor folderCursor = mContext.getContentResolver().query(//声名Cursor类的变量folder存储调查结果
Notes.CONTENT_NOTE_URI, Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION, NOTE_PROJECTION,//调用getContentResolver()查询目标为Note表中数据和NOTE_PROJECTION指定列
"(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND "
+ NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR "
+ NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null);
//限定查询条件为文件夹且父类文件夹不是回收站
if (folderCursor != null) { if (folderCursor != null) {//判断folder是否为空
if (folderCursor.moveToFirst()) { if (folderCursor.moveToFirst()) {
do { do {
// Print folder's name // Print folder's name
String folderName = ""; String folderName = "";//遍历查询文件夹中所有文件夹记录
if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) {
folderName = mContext.getString(R.string.call_record_folder_name); folderName = mContext.getString(R.string.call_record_folder_name);//若记录对应通话记录文件夹给folder赋值Notes.ID_CALL_RECORD_FOLDER
} else { } else {//否则folder赋值为记录的摘要字段值
folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET);
} }
if (!TextUtils.isEmpty(folderName)) { if (!TextUtils.isEmpty(folderName)) {
ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName));
} }
String folderId = folderCursor.getString(NOTE_COLUMN_ID); String folderId = folderCursor.getString(NOTE_COLUMN_ID);//获取当前记录对应文件夹id并赋值给folder
exportFolderToText(folderId, ps); exportFolderToText(folderId, ps);
} while (folderCursor.moveToNext()); } while (folderCursor.moveToNext());//将当前记录文件夹导出为文本形式结果写入PS中
} }
folderCursor.close(); folderCursor.close();
} }
// Export notes in root's folder // Export notes in root's folder
Cursor noteCursor = mContext.getContentResolver().query( Cursor noteCursor = mContext.getContentResolver().query(//通过getContentResolver()获取resolver对象并调用querty方法查询
Notes.CONTENT_NOTE_URI, Notes.CONTENT_NOTE_URI,//查询URI
NOTE_PROJECTION, NOTE_PROJECTION,//查询列
NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID
+ "=0", null, null); + "=0", null, null);//限制查询类型为Notes.TYPE_NOTE且父id为0的note记录
if (noteCursor != null) { if (noteCursor != null) {
if (noteCursor.moveToFirst()) { if (noteCursor.moveToFirst()) {//将游标移到查询结果的第一条记录
do { do {
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
mContext.getString(R.string.format_datetime_mdhm), mContext.getString(R.string.format_datetime_mdhm),
noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));//根据时间格式化字符串的时间戳,将时间转换成字符串格式
// Query data belong to this note // Query data belong to this note
String noteId = noteCursor.getString(NOTE_COLUMN_ID); String noteId = noteCursor.getString(NOTE_COLUMN_ID);
exportNoteToText(noteId, ps); exportNoteToText(noteId, ps);
} while (noteCursor.moveToNext()); } while (noteCursor.moveToNext());
} }//或i去当前Note记录的id吧id对应数据写入输出流中
noteCursor.close(); noteCursor.close();
} }
ps.close(); ps.close();//关闭游标
return STATE_SUCCESS; return STATE_SUCCESS;
} }
@ -286,21 +286,21 @@ public class BackupUtils {
* Get a print stream pointed to the file {@generateExportedTextFile} * Get a print stream pointed to the file {@generateExportedTextFile}
*/ */
private PrintStream getExportToTextPrintStream() { private PrintStream getExportToTextPrintStream() {
File file = generateFileMountedOnSDcard(mContext, R.string.file_path, File file = generateFileMountedOnSDcard(mContext, R.string.file_path,//使用getExportToTextPrintStream()再sd卡上生成指定名称和路径的文本文件
R.string.file_name_txt_format); R.string.file_name_txt_format);
if (file == null) { if (file == null) {
Log.e(TAG, "create file to exported failed"); Log.e(TAG, "create file to exported failed");
return null; return null;//若生成文件失败返回null再logcat输出错误信息
} }
mFileName = file.getName(); mFileName = file.getName();
mFileDirectory = mContext.getString(R.string.file_path); mFileDirectory = mContext.getString(R.string.file_path);//将成功创建文件保存为FoleNAMe路径柏村委mFileDiectory
PrintStream ps = null; PrintStream ps = null;
try { try {
FileOutputStream fos = new FileOutputStream(file); FileOutputStream fos = new FileOutputStream(file);
ps = new PrintStream(fos); ps = new PrintStream(fos);//创建对象fos用于写入文件
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {//使用fos创建PrintStream对象PS一边向文件夹写入数据
e.printStackTrace(); e.printStackTrace();
return null; return null;//若无法找到写入目标文件再logcat输出错误信息并返回null
} catch (NullPointerException e) { } catch (NullPointerException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
@ -314,28 +314,28 @@ public class BackupUtils {
*/ */
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(Environment.getExternalStorageDirectory()); sb.append(Environment.getExternalStorageDirectory());//再strtingbuilder中添加SD卡根目录路径
sb.append(context.getString(filePathResId)); sb.append(context.getString(filePathResId));
File filedir = new File(sb.toString()); File filedir = new File(sb.toString());//添加文件路径字符串资源id并返回file对象表示该目录
sb.append(context.getString( sb.append(context.getString(
fileNameFormatResId, fileNameFormatResId,
DateFormat.format(context.getString(R.string.format_date_ymd), DateFormat.format(context.getString(R.string.format_date_ymd),
System.currentTimeMillis()))); System.currentTimeMillis())));//再StringBuilder添加文件名格式字符串资源id并使用dateFormat替换为当前日期格式化字符串
File file = new File(sb.toString()); File file = new File(sb.toString());
//返回表示最终创建的文件夹对象
try { try {
if (!filedir.exists()) { if (!filedir.exists()) {
filedir.mkdir(); filedir.mkdir();
} }//判断指定目录是否存在,若不存在创建
if (!file.exists()) { if (!file.exists()) {
file.createNewFile(); file.createNewFile();
} }//判断指定目录是否存在,若不存在创建
return file; return file;//返回创建对象
} catch (SecurityException e) { } catch (SecurityException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }//如果不能在指定目录下创建新的文件则抛出IOException异常
return null; return null;
} }

@ -25,18 +25,18 @@ import android.content.OperationApplicationException;
import android.database.Cursor; import android.database.Cursor;
import android.os.RemoteException; import android.os.RemoteException;
import android.util.Log; import android.util.Log;
//导入android 包content、database等
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.CallNote; import net.micode.notes.data.Notes.CallNote;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute;
//导入net-micode包下数据
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
//导入JAVA ArrayList和HashSet类
public class DataUtils {//对DataUtils类进行定义
public class DataUtils { public static final String TAG = "DataUtils";//定义静态不可变字TAG符串常量"DataUtils"
public static final String TAG = "DataUtils";
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) { public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
if (ids == null) { if (ids == null) {
Log.d(TAG, "the ids is null"); Log.d(TAG, "the ids is null");
@ -46,69 +46,71 @@ public class DataUtils {
Log.d(TAG, "no id is in the hashset"); Log.d(TAG, "no id is in the hashset");
return true; return true;
} }
//定义批量删除笔记。若ids参数为null输出"the ids is null"并返回true若ids参数为0输出"no id is in the hashset"并返回true
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
for (long id : ids) { //定义操作列表存储ContentProviderOperation对象
for (long id : ids) {//遍历id列表
if(id == Notes.ID_ROOT_FOLDER) { if(id == Notes.ID_ROOT_FOLDER) {
Log.e(TAG, "Don't delete system folder root"); Log.e(TAG, "Don't delete system folder root");
continue; continue;
} }//如果id为系统根目录打印"不要删除系统根目录"并进行下一次循环
ContentProviderOperation.Builder builder = ContentProviderOperation ContentProviderOperation.Builder builder = ContentProviderOperation
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
operationList.add(builder.build()); operationList.add(builder.build());//创造builder对象删除指定id的ContentProviderOperation
} }
try { try {//调用applybatch
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
if (results == null || results.length == 0 || results[0] == null) { if (results == null || results.length == 0 || results[0] == null) {
Log.d(TAG, "delete notes failed, ids:" + ids.toString()); Log.d(TAG, "delete notes failed, ids:" + ids.toString());
return false; return false;
} }
return true; return true;//若返回为空或第一个结果为null输出删除失败返回false否则返回true
} catch (RemoteException e) { } 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) { } 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()));
} }//若remoteexeption或operationapplicationexception异常返回false
return false; return false;
} }
public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();//创建contentValues对象
values.put(NoteColumns.PARENT_ID, desFolderId); values.put(NoteColumns.PARENT_ID, desFolderId);//移动笔记至目标文件夹
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);//更新笔记的原始父类文件夹id
values.put(NoteColumns.LOCAL_MODIFIED, 1); values.put(NoteColumns.LOCAL_MODIFIED, 1);
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
//更新笔记
} }
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids, public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
long folderId) { long folderId) {//若ids为空打印log并返回true
if (ids == null) { if (ids == null) {
Log.d(TAG, "the ids is null"); Log.d(TAG, "the ids is null");
return true; return true;
} }
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
for (long id : ids) { for (long id : ids) {//遍历笔记id集合
ContentProviderOperation.Builder builder = ContentProviderOperation ContentProviderOperation.Builder builder = ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
builder.withValue(NoteColumns.PARENT_ID, folderId); builder.withValue(NoteColumns.PARENT_ID, folderId);//移动笔记至目标文件夹
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); builder.withValue(NoteColumns.LOCAL_MODIFIED, 1);//添加键值对,表示本地有效
operationList.add(builder.build()); operationList.add(builder.build());//添加操作至操作列表
} }
try { try {
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);//应用操作列表
if (results == null || results.length == 0 || results[0] == null) { if (results == null || results.length == 0 || results[0] == null) {//如果结果为空或者第一个结果为空打印log并返回false
Log.d(TAG, "delete notes failed, ids:" + ids.toString()); Log.d(TAG, "delete notes failed, ids:" + ids.toString());
return false; return false;
} }
return true; return true;//true表示操作成功
} catch (RemoteException e) { } 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) { } 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; return false;//false表示操作失败
} }
/** /**
@ -134,7 +136,7 @@ public class DataUtils {
} }
} }
return count; return count;
} }//检查笔记是否在数据库中可见,若不可见输出"get folder count failed:"
public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) {
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
@ -152,7 +154,7 @@ public class DataUtils {
} }
return exist; return exist;
} }
//检查数据是否在数据库中存在
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
null, null, null, null); null, null, null, null);
@ -166,7 +168,7 @@ public class DataUtils {
} }
return exist; return exist;
} }
//检查文件名称是否可见
public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { public static boolean existInDataDatabase(ContentResolver resolver, long dataId) {
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId),
null, null, null, null); null, null, null, null);
@ -198,6 +200,7 @@ public class DataUtils {
} }
public static HashSet<AppWidgetAttribute> getFolderNoteWidget(ContentResolver resolver, long folderId) { public static HashSet<AppWidgetAttribute> getFolderNoteWidget(ContentResolver resolver, long folderId) {
//查询文件夹中所有小部件id和类型
Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, Cursor c = resolver.query(Notes.CONTENT_NOTE_URI,
new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE },
NoteColumns.PARENT_ID + "=?", NoteColumns.PARENT_ID + "=?",
@ -208,6 +211,7 @@ public class DataUtils {
if (c != null) { if (c != null) {
if (c.moveToFirst()) { if (c.moveToFirst()) {
set = new HashSet<AppWidgetAttribute>(); set = new HashSet<AppWidgetAttribute>();
//遍历查询结果将小部件id和类型存入hashSET中
do { do {
try { try {
AppWidgetAttribute widget = new AppWidgetAttribute(); AppWidgetAttribute widget = new AppWidgetAttribute();
@ -220,11 +224,11 @@ public class DataUtils {
} while (c.moveToNext()); } while (c.moveToNext());
} }
c.close(); c.close();
} }//返回文件夹中所有小部件HashSet集合
return set; return set;
} }
public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) {//获取一段笔记的getCallNumberByNoteId
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
new String [] { CallNote.PHONE_NUMBER }, new String [] { CallNote.PHONE_NUMBER },
CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?",
@ -234,20 +238,21 @@ public class DataUtils {
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
try { try {
return cursor.getString(0); return cursor.getString(0);
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {//如果查询结果不为空且有数据,则返回该行的电话号码
Log.e(TAG, "Get call number fails " + e.toString()); Log.e(TAG, "Get call number fails " + e.toString());//如果获取电话号码失败,则打印错误日志
} finally { } finally {
cursor.close(); cursor.close();
} } //关闭游标,释放资源
} }
return ""; return "";//如果没有查询结果,则返回空串
} }
public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) {
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, //使用ContentResover对象来查询数据并返回Cursor对象
new String [] { CallNote.NOTE_ID }, Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,//查询Note id字段
new String [] { CallNote.NOTE_ID },//通过童话日期,电话号码扽给查询
CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL("
+ CallNote.PHONE_NUMBER + ",?)", + CallNote.PHONE_NUMBER + ",?)",//传入查询参数
new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber },
null); null);
@ -264,8 +269,8 @@ public class DataUtils {
return 0; return 0;
} }
public static String getSnippetById(ContentResolver resolver, long noteId) { public static String getSnippetById(ContentResolver resolver, long noteId) {//使用ContentResovler对象查询数据返回Cursor对象
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,//查询snippet字段
new String [] { NoteColumns.SNIPPET }, new String [] { NoteColumns.SNIPPET },
NoteColumns.ID + "=?", NoteColumns.ID + "=?",
new String [] { String.valueOf(noteId)}, new String [] { String.valueOf(noteId)},
@ -273,14 +278,14 @@ public class DataUtils {
if (cursor != null) { if (cursor != null) {
String snippet = ""; String snippet = "";
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {//获取查询结果中第0列的值即snippet字段值
snippet = cursor.getString(0); snippet = cursor.getString(0);
} }
cursor.close(); cursor.close();
return snippet; return snippet;
} }
throw new IllegalArgumentException("Note is not found with id: " + noteId); throw new IllegalArgumentException("Note is not found with id: " + noteId);
} }//通过noteld获取信息返回字符串
public static String getFormattedSnippet(String snippet) { public static String getFormattedSnippet(String snippet) {
if (snippet != null) { if (snippet != null) {

@ -18,96 +18,96 @@ package net.micode.notes.tool;
public class GTaskStringUtils { public class GTaskStringUtils {
public final static String GTASK_JSON_ACTION_ID = "action_id"; public final static String GTASK_JSON_ACTION_ID = "action_id";//定义字符串常量,在类的外部直接访问,表示"action_id"的字符串键名
public final static String GTASK_JSON_ACTION_LIST = "action_list"; public final static String GTASK_JSON_ACTION_LIST = "action_list";//定义字符串常量,在类的外部直接访问,表示"action_list的字符串键名
public final static String GTASK_JSON_ACTION_TYPE = "action_type"; public final static String GTASK_JSON_ACTION_TYPE = "action_type";//定义字符串常量在类的外部直接访问表示action_type的字符串键名
public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create";//定义字符串常量在类的外部直接访问表示create的字符串键名
public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all";//定义字符串常量在类的外部直接访问表示get_all的字符串键名
public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move";//定义字符串常量,在类的外部直接访问,表示"move的字符串键名
public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update";//定义字符串常量在类的外部直接访问表示update的字符串键名
public final static String GTASK_JSON_CREATOR_ID = "creator_id"; public final static String GTASK_JSON_CREATOR_ID = "creator_id";//定义字符串常量在类的外部直接访问表示creator_id的字符串键名
public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; public final static String GTASK_JSON_CHILD_ENTITY = "child_entity";//定义字符串常量在类的外部直接访问表示child_entity的字符串键名
public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; public final static String GTASK_JSON_CLIENT_VERSION = "client_version";//定义字符串常量在类的外部直接访问表示client_version的字符串键名
public final static String GTASK_JSON_COMPLETED = "completed"; public final static String GTASK_JSON_COMPLETED = "completed";//定义字符串常量在类的外部直接访问表示completed的字符串键名
public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id";
//定义字符串常量在类的外部直接访问表示current_list_id的字符串键名
public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id";//定义字符串常量在类的外部直接访问表示default_list_id的字符串键名
public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; public final static String GTASK_JSON_DELETED = "deleted";//定义字符串常量在类的外部直接访问表示deleted的字符串键名
public final static String GTASK_JSON_DELETED = "deleted"; public final static String GTASK_JSON_DEST_LIST = "dest_list";//定义字符串常量在类的外部直接访问表示dest_list的字符串键名
public final static String GTASK_JSON_DEST_LIST = "dest_list"; public final static String GTASK_JSON_DEST_PARENT = "dest_parent";//定义字符串常量在类的外部直接访问表示dest_parent的字符串键名
public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type";//定义字符串常量在类的外部直接访问表示dest_parent_type的字符串键名
public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta";//定义字符串常量在类的外部直接访问表示entity_delta的字符串键名
public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; public final static String GTASK_JSON_ENTITY_TYPE = "entity_type";//定义字符串常量在类的外部直接访问表示entity_type的字符串键名
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_GET_DELETED = "get_deleted";
//定义字符串常量在类的外部直接访问表示get_deleted的字符串键名
public final static String GTASK_JSON_ID = "id";//定义字符串常量在类的外部直接访问表示id的字符串键名
public final static String GTASK_JSON_ID = "id"; public final static String GTASK_JSON_INDEX = "index";//定义字符串常量在类的外部直接访问表示index的字符串键名
public final static String GTASK_JSON_INDEX = "index";
public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; public final static String GTASK_JSON_LAST_MODIFIED = "last_modified";//定义字符串常量在类的外部直接访问表示last_modified的字符串键名
public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point";//定义字符串常量在类的外部直接访问表示latest_sync_point的字符串键名
public final static String GTASK_JSON_LIST_ID = "list_id"; public final static String GTASK_JSON_LIST_ID = "list_id";//定义字符串常量在类的外部直接访问表示list_id的字符串键名
public final static String GTASK_JSON_LISTS = "lists"; public final static String GTASK_JSON_LISTS = "lists";//定义字符串常量在类的外部直接访问表示lists的字符串键名
public final static String GTASK_JSON_NAME = "name"; public final static String GTASK_JSON_NAME = "name";//定义字符串常量在类的外部直接访问表示name的字符串键名
public final static String GTASK_JSON_NEW_ID = "new_id"; public final static String GTASK_JSON_NEW_ID = "new_id";
//定义字符串常量在类的外部直接访问表示new_id的字符串键名
public final static String GTASK_JSON_NOTES = "notes";//定义字符串常量在类的外部直接访问表示notes的字符串键名
public final static String GTASK_JSON_NOTES = "notes"; public final static String GTASK_JSON_PARENT_ID = "parent_id";//定义字符串常量在类的外部直接访问表示parent_id的字符串键名
public final static String GTASK_JSON_PARENT_ID = "parent_id";
public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id";//定义字符串常量在类的外部直接访问表示prior_sibling_id的字符串键名
public final static String GTASK_JSON_RESULTS = "results"; public final static String GTASK_JSON_RESULTS = "results";//定义字符串常量在类的外部直接访问表示results的字符串键名
public final static String GTASK_JSON_SOURCE_LIST = "source_list"; public final static String GTASK_JSON_SOURCE_LIST = "source_list";
public final static String GTASK_JSON_TASKS = "tasks"; public final static String GTASK_JSON_TASKS = "tasks";//定义字符串常量在类的外部直接访问表示tasks的字符串键名
public final static String GTASK_JSON_TYPE = "type"; public final static String GTASK_JSON_TYPE = "type";//定义字符串常量在类的外部直接访问表示type的字符串键名
public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; public final static String GTASK_JSON_TYPE_GROUP = "GROUP";//定义字符串常量在类的外部直接访问表示GROUP的字符串键名
public final static String GTASK_JSON_TYPE_TASK = "TASK"; public final static String GTASK_JSON_TYPE_TASK = "TASK";//定义字符串常量在类的外部直接访问表示TASK的字符串键名
public final static String GTASK_JSON_USER = "user"; public final static String GTASK_JSON_USER = "user";//定义字符串常量在类的外部直接访问表示user的字符串键名
public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]";//定义字符串常量在类的外部直接访问表示MIUI_Notes的字符串键名
public final static String FOLDER_DEFAULT = "Default"; public final static String FOLDER_DEFAULT = "Default";//定义字符串常量在类的外部直接访问表示Default的字符串键名
public final static String FOLDER_CALL_NOTE = "Call_Note"; public final static String FOLDER_CALL_NOTE = "Call_Note";//定义字符串常量在类的外部直接访问表示Call_Note的字符串键名
public final static String FOLDER_META = "METADATA"; public final static String FOLDER_META = "METADATA";//定义字符串常量在类的外部直接访问表示METADATA的字符串键名
public final static String META_HEAD_GTASK_ID = "meta_gid"; public final static String META_HEAD_GTASK_ID = "meta_gid";//定义字符串常量在类的外部直接访问表示meta_gid的字符串键名
public final static String META_HEAD_NOTE = "meta_note"; public final static String META_HEAD_NOTE = "meta_note";//定义字符串常量在类的外部直接访问表示meta_note的字符串键名
public final static String META_HEAD_DATA = "meta_data"; public final static String META_HEAD_DATA = "meta_data";//定义字符串常量在类的外部直接访问表示meta_data的字符串键名
public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE";//定义字符串常量,在类的外部直接访问,表示[META INFO] DON'T UPDATE AND DELETE的字符串键名
} }

@ -22,22 +22,22 @@ import android.preference.PreferenceManager;
import net.micode.notes.R; import net.micode.notes.R;
import net.micode.notes.ui.NotesPreferenceActivity; import net.micode.notes.ui.NotesPreferenceActivity;
public class ResourceParser { public class ResourceParser {
//定义常量,用于表示颜色和字体大小
public static final int YELLOW = 0; public static final int YELLOW = 0;//黄色
public static final int BLUE = 1; public static final int BLUE = 1;//蓝
public static final int WHITE = 2; public static final int WHITE = 2;//bai
public static final int GREEN = 3; public static final int GREEN = 3;
public static final int RED = 4; public static final int RED = 4;
public static final int BG_DEFAULT_COLOR = YELLOW; public static final int BG_DEFAULT_COLOR = YELLOW;//默认背景颜色为黄色
public static final int TEXT_SMALL = 0; public static final int TEXT_SMALL = 0;//字体小号
public static final int TEXT_MEDIUM = 1; public static final int TEXT_MEDIUM = 1;//中
public static final int TEXT_LARGE = 2; public static final int TEXT_LARGE = 2;//da
public static final int TEXT_SUPER = 3; public static final int TEXT_SUPER = 3;//chaoda
public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM;//默认大小为中
public static class NoteBgResources { public static class NoteBgResources {
private final static int [] BG_EDIT_RESOURCES = new int [] { private final static int [] BG_EDIT_RESOURCES = new int [] {
@ -46,7 +46,7 @@ public class ResourceParser {
R.drawable.edit_white, R.drawable.edit_white,
R.drawable.edit_green, R.drawable.edit_green,
R.drawable.edit_red R.drawable.edit_red
}; };//定义私有静态常量G_EDIT_RESOURCES 用于储存图片资源id
private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] { private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] {
R.drawable.edit_title_yellow, R.drawable.edit_title_yellow,
@ -54,7 +54,7 @@ public class ResourceParser {
R.drawable.edit_title_white, R.drawable.edit_title_white,
R.drawable.edit_title_green, R.drawable.edit_title_green,
R.drawable.edit_title_red R.drawable.edit_title_red
}; };//定义另一个私有静态常量用于储存笔记编辑页面标题背景资源id
public static int getNoteBgResource(int id) { public static int getNoteBgResource(int id) {
return BG_EDIT_RESOURCES[id]; return BG_EDIT_RESOURCES[id];
@ -63,40 +63,40 @@ public class ResourceParser {
public static int getNoteTitleBgResource(int id) { public static int getNoteTitleBgResource(int id) {
return BG_EDIT_TITLE_RESOURCES[id]; return BG_EDIT_TITLE_RESOURCES[id];
} }
} }//初始化编辑页面标题背景资源id
public static int getDefaultBgId(Context context) { public static int getDefaultBgId(Context context) {//获取应用配置信息,判断用户是否自定义背景颜色
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) {
return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length);
} else { } else {
return BG_DEFAULT_COLOR; return BG_DEFAULT_COLOR;//若没有定义,返回默认颜色白色
} }
} }
public static class NoteItemBgResources { public static class NoteItemBgResources {
private final static int [] BG_FIRST_RESOURCES = new int [] { private final static int [] BG_FIRST_RESOURCES = new int [] {//定义final整型数组 BG_FIRST_RESOURCES
R.drawable.list_yellow_up, R.drawable.list_yellow_up,//黄色背景上边框图案id
R.drawable.list_blue_up, R.drawable.list_blue_up,//蓝色背景上边框图案id
R.drawable.list_white_up, R.drawable.list_white_up,//白色背景边框图案id
R.drawable.list_green_up, R.drawable.list_green_up,//绿色背景边框图案id
R.drawable.list_red_up R.drawable.list_red_up//红色背景边框图案id
}; };
private final static int [] BG_NORMAL_RESOURCES = new int [] { private final static int [] BG_NORMAL_RESOURCES = new int [] {
R.drawable.list_yellow_middle, R.drawable.list_yellow_middle,//黄色背景中间边框图案id
R.drawable.list_blue_middle, R.drawable.list_blue_middle,//蓝色背景中间边框图案id
R.drawable.list_white_middle, R.drawable.list_white_middle,//白色背景中间边框图案id
R.drawable.list_green_middle, R.drawable.list_green_middle,//绿色背景中间边框图案id
R.drawable.list_red_middle R.drawable.list_red_middle//红色背景中间边框图案id
}; };
private final static int [] BG_LAST_RESOURCES = new int [] { private final static int [] BG_LAST_RESOURCES = new int [] {
R.drawable.list_yellow_down, R.drawable.list_yellow_down,//黄色背景中下间边框图案id
R.drawable.list_blue_down, R.drawable.list_blue_down,//蓝色背景中下间边框图案id
R.drawable.list_white_down, R.drawable.list_white_down,//白色背景中下间边框图案id
R.drawable.list_green_down, R.drawable.list_green_down,//绿色背景中下间边框图案id
R.drawable.list_red_down, R.drawable.list_red_down,//红色背景中下间边框图案id
}; };
private final static int [] BG_SINGLE_RESOURCES = new int [] { private final static int [] BG_SINGLE_RESOURCES = new int [] {
@ -110,33 +110,30 @@ public class ResourceParser {
public static int getNoteBgFirstRes(int id) { public static int getNoteBgFirstRes(int id) {
return BG_FIRST_RESOURCES[id]; return BG_FIRST_RESOURCES[id];
} }
//定义getNoteBgFirstRes方法用于返回G_FIRST_RESOURCES[id]
public static int getNoteBgLastRes(int id) { public static int getNoteBgLastRes(int id) {
return BG_LAST_RESOURCES[id]; return BG_LAST_RESOURCES[id];
} }
//定义getNoteBgLastRes方法用于返回 BG_LAST_RESOURCES[id]
public static int getNoteBgSingleRes(int id) { public static int getNoteBgSingleRes(int id) {
return BG_SINGLE_RESOURCES[id]; return BG_SINGLE_RESOURCES[id];
} }
//定义getNoteBgSingleRes方法用于返回BG_SINGLE_RESOURCES[id]
public static int getNoteBgNormalRes(int id) { public static int getNoteBgNormalRes(int id) {
return BG_NORMAL_RESOURCES[id]; return BG_NORMAL_RESOURCES[id];
} }
//定义etNoteBgNormalRes方法用于返回BG_NORMAL_RESOURCES[id]
public static int getFolderBgRes() { public static int getFolderBgRes() {return R.drawable.list_folder;}//定义getFolderBgRes方法用于返回 R.drawable.list_folder
return R.drawable.list_folder;
}
} }
public static class WidgetBgResources { public static class WidgetBgResources {//定义静态内部类WidgetBgResources
private final static int [] BG_2X_RESOURCES = new int [] { private final static int [] BG_2X_RESOURCES = new int [] {
R.drawable.widget_2x_yellow, R.drawable.widget_2x_yellow,
R.drawable.widget_2x_blue, R.drawable.widget_2x_blue,
R.drawable.widget_2x_white, R.drawable.widget_2x_white,
R.drawable.widget_2x_green, R.drawable.widget_2x_green,
R.drawable.widget_2x_red, R.drawable.widget_2x_red,
}; };// 管理小部件2x2格式不同颜色资源图片
public static int getWidget2xBgResource(int id) { public static int getWidget2xBgResource(int id) {
return BG_2X_RESOURCES[id]; return BG_2X_RESOURCES[id];
} }
@ -147,20 +144,20 @@ public class ResourceParser {
R.drawable.widget_4x_white, R.drawable.widget_4x_white,
R.drawable.widget_4x_green, R.drawable.widget_4x_green,
R.drawable.widget_4x_red R.drawable.widget_4x_red
}; };// 管理小部件4x4格式不同颜色资源图片
public static int getWidget4xBgResource(int id) { public static int getWidget4xBgResource(int id) {
return BG_4X_RESOURCES[id]; return BG_4X_RESOURCES[id];
} }
} }
public static class TextAppearanceResources { public static class TextAppearanceResources {//定义TextAppearanceResources 静态内部类
private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] {
R.style.TextAppearanceNormal, R.style.TextAppearanceNormal,
R.style.TextAppearanceMedium, R.style.TextAppearanceMedium,
R.style.TextAppearanceLarge, R.style.TextAppearanceLarge,
R.style.TextAppearanceSuper R.style.TextAppearanceSuper
}; };//分别对应中等,大号,超大的文本外观样式
public static int getTexAppearanceResource(int id) { public static int getTexAppearanceResource(int id) {
/** /**

@ -37,33 +37,33 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
NoteColumns.ID, NoteColumns.ID,
NoteColumns.BG_COLOR_ID, NoteColumns.BG_COLOR_ID,
NoteColumns.SNIPPET NoteColumns.SNIPPET
}; };//定义字符串数组PROJECTION包含IDB G_COLOR_ID SNIPPET三个元素
public static final int COLUMN_ID = 0; public static final int COLUMN_ID = 0;
public static final int COLUMN_BG_COLOR_ID = 1; public static final int COLUMN_BG_COLOR_ID = 1;
public static final int COLUMN_SNIPPET = 2; public static final int COLUMN_SNIPPET = 2;//定义三个元素初始量
private static final String TAG = "NoteWidgetProvider"; private static final String TAG = "NoteWidgetProvider";
@Override @Override
public void onDeleted(Context context, int[] appWidgetIds) { public void onDeleted(Context context, int[] appWidgetIds) {//创建一个ContentValues对象values存储键值对数据
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);//将无效小部件id存储ContentValues中
for (int i = 0; i < appWidgetIds.length; i++) { for (int i = 0; i < appWidgetIds.length; i++) {
context.getContentResolver().update(Notes.CONTENT_NOTE_URI, context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
values, values,
NoteColumns.WIDGET_ID + "=?", NoteColumns.WIDGET_ID + "=?",
new String[] { String.valueOf(appWidgetIds[i])}); new String[] { String.valueOf(appWidgetIds[i])});//通过getcontent函数获取程序contentValues对象用update方法更新
} }
} }
private Cursor getNoteWidgetInfo(Context context, int widgetId) { private Cursor getNoteWidgetInfo(Context context, int widgetId) {
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI, return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION, PROJECTION,//获取ContentResovler对象并用querty方法查询数据·
NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?", NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?",
new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) }, new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) },
null); null);
} }//null表示默认排序方式
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
update(context, appWidgetManager, appWidgetIds, false); update(context, appWidgetManager, appWidgetIds, false);
@ -71,12 +71,12 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
boolean privacyMode) { boolean privacyMode) {
for (int i = 0; i < appWidgetIds.length; i++) { for (int i = 0; i < appWidgetIds.length; i++) {//判断当前小部件id是否合法若合法才可进行操作
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) { if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
int bgId = ResourceParser.getDefaultBgId(context); int bgId = ResourceParser.getDefaultBgId(context);//获取默认背景图片id并将其作为当前部件的背景
String snippet = ""; String snippet = "";
Intent intent = new Intent(context, NoteEditActivity.class); Intent intent = new Intent(context, NoteEditActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//创建internet对象设置flag标志
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]); intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]);
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());
@ -86,14 +86,14 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]); Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
c.close(); c.close();
return; return;
} }//判断数据库查询结果c是否为空若查询结果大于1则记录日志错误并返回
snippet = c.getString(COLUMN_SNIPPET); snippet = c.getString(COLUMN_SNIPPET);
bgId = c.getInt(COLUMN_BG_COLOR_ID); bgId = c.getInt(COLUMN_BG_COLOR_ID);
intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID));
intent.setAction(Intent.ACTION_VIEW); intent.setAction(Intent.ACTION_VIEW);//将当前记录id作为extra数据存入启动internet对象中
} else { } else {
snippet = context.getResources().getString(R.string.widget_havenot_content); snippet = context.getResources().getString(R.string.widget_havenot_content);
intent.setAction(Intent.ACTION_INSERT_OR_EDIT); intent.setAction(Intent.ACTION_INSERT_OR_EDIT);//// 数据库查询结果为空或者未能成功移动到第一条记录时,将片段内容设置为默认字符串
} }
if (c != null) { if (c != null) {
@ -107,16 +107,16 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
* Generate the pending intent to start host for the widget * Generate the pending intent to start host for the widget
*/ */
PendingIntent pendingIntent = null; PendingIntent pendingIntent = null;
if (privacyMode) { if (privacyMode) {// 判断是否处于隐私模式
rv.setTextViewText(R.id.widget_text, rv.setTextViewText(R.id.widget_text,
context.getString(R.string.widget_under_visit_mode)); context.getString(R.string.widget_under_visit_mode));
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent( pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent(
context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);// 如果是将小部件文本设置为“访问模式下”并创建一个PendingIntent使点击小部件时跳转至NotesListActivity
} else { } else {
rv.setTextViewText(R.id.widget_text, snippet); rv.setTextViewText(R.id.widget_text, snippet);
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent,
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
} }// 如果不是将小部件文本设置为片段内容并创建一个PendingIntent使点击小部件时跳转至NoteEditActivity
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent); rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv); appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
@ -128,5 +128,5 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
protected abstract int getLayoutId(); protected abstract int getLayoutId();
protected abstract int getWidgetType(); protected abstract int getWidgetType();//获取背景资源id布局资源id小部件类型
} }

@ -28,20 +28,21 @@ public class NoteWidgetProvider_2x extends NoteWidgetProvider {
@Override @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.update(context, appWidgetManager, appWidgetIds); super.update(context, appWidgetManager, appWidgetIds);
} }//NoteWidgetProvider_2x是一个继承自NoteWidgetProvider的小部件提供者用于显示2x大小的便签小部件。
//调用父类的update方法更新小部件UI界面
@Override @Override
protected int getLayoutId() { protected int getLayoutId() {
return R.layout.widget_2x; return R.layout.widget_2x;
} }//获取2x大小便签小部件布局资源ID由子类具体实现
@Override @Override
protected int getBgResourceId(int bgId) { protected int getBgResourceId(int bgId) {
return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId);
} }//获取2x大小便签小部件背景资源ID由子类具体实现。
@Override @Override
protected int getWidgetType() { protected int getWidgetType() {
return Notes.TYPE_WIDGET_2X; return Notes.TYPE_WIDGET_2X;
} }//获取小部件类型由子类具体实现返回值为Notes.TYPE_WIDGET_2X
} }

@ -28,19 +28,20 @@ public class NoteWidgetProvider_4x extends NoteWidgetProvider {
@Override @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.update(context, appWidgetManager, appWidgetIds); super.update(context, appWidgetManager, appWidgetIds);
} }//NoteWidgetProvider_4x是一个继承自NoteWidgetProvider的小部件提供者用于显示4x大小的便签小部件。
//调用父类的update方法更新小部件UI界面
protected int getLayoutId() { protected int getLayoutId() {
return R.layout.widget_4x; return R.layout.widget_4x;
} }//获取4x大小便签小部件布局资源ID由子类具体实现
@Override @Override
protected int getBgResourceId(int bgId) { protected int getBgResourceId(int bgId) {
return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId);
} }//获取4x大小便签小部件背景资源ID由子类具体实现。
@Override @Override
protected int getWidgetType() { protected int getWidgetType() {
return Notes.TYPE_WIDGET_4X; return Notes.TYPE_WIDGET_4X;
} }//获取小部件类型由子类具体实现返回值为Notes.TYPE_WIDGET_4X
} }

Loading…
Cancel
Save