Merge branch 'master' of https://bdgit.educoder.net/pycalf7qb/MINOTES into GAOYUAN_BRANCH

# Conflicts:
#	src/data/Contact.java
#	src/data/NotesDatabaseHelper.java
#	src/data/NotesProvider.java
GAOYUAN_BRANCH
高源 2 years ago
commit 30179b6ac6

@ -25,6 +25,7 @@ import android.util.Log;
import java.util.HashMap;
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD:app/src/main/java/net/micode/notes/data/Contact.java
public class Contact {
@ -40,12 +41,18 @@ public class Contact {
private static HashMap<String, String> sContactCache; //定义连接哈希表
private static final String TAG = "Contact"; //设置TAG的值为'连接'
>>>>>>> b12b9a8449498593360310c554053628b31cc8c9
=======
public class Contact { // 定义连接类
private static HashMap<String, String> sContactCache; // 定义连接哈希表
private static final String TAG = "Contact"; // 设置TAG的值为'连接'
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
+ " AND " + Data.RAW_CONTACT_ID + " IN "
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
+ " AND " + Data.RAW_CONTACT_ID + " IN "
+ "(SELECT raw_contact_id "
+ " FROM phone_lookup"
<<<<<<< HEAD
+ " WHERE min_match = '+')"; //SQL语句
public static String getContact(Context context, String phoneNumber) { //获取连接
@ -80,6 +87,43 @@ public class Contact {
} else {
Log.d(TAG, "No contact matched with number:" + phoneNumber); //日志记录信息
return null; //没找到返回NULL
=======
+ " WHERE min_match = '+')"; // SQL语句
public static String getContact(Context context, String phoneNumber) { // 获取连接
if (sContactCache == null) {
sContactCache = new HashMap<String, String>(); // 连接哈希表为空时创建连接哈希表
}
if (sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber); // 连接哈希表存在电话号码时获取值
}
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); // 将电话号码进行函数处理并代替'+'符号
Cursor cursor = context.getContentResolver().query( // 用于查询的结果集
Data.CONTENT_URI, // 通话记录uri
new String[] { Phone.DISPLAY_NAME },
selection,
new String[] { phoneNumber },
null); // 查询指定号码相关联系人名字
if (cursor != null && cursor.moveToFirst()) {
try { // 有结果时
String name = cursor.getString(0); // 获取联系人名字
sContactCache.put(phoneNumber, name); // 缓存电话号码与联系人名字
return name;
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, " Cursor get string error " + e.toString()); // 日志记录信息
return null;
} finally {
cursor.close(); // 关闭结果集
}
} else {
Log.d(TAG, "No contact matched with number:" + phoneNumber); // 日志记录信息
return null; // 没找到返回NULL
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
}
}
}

@ -17,263 +17,326 @@
package net.micode.notes.data;
import android.net.Uri;
public class Notes { //定义类,为模块的其他类提供变量定义
public static final String AUTHORITY = "micode_notes"; //设置AUTHORITY
public static final String TAG = "Notes"; //设置TAG
public static final int TYPE_NOTE = 0;
public static final int TYPE_FOLDER = 1;
public static final int TYPE_SYSTEM = 2;
/**
* Following IDs are system folders' identifiers
* {@link Notes#ID_ROOT_FOLDER } is default folder
* {@link Notes#ID_TEMPARAY_FOLDER } is for notes belonging no folder
* {@link Notes#ID_CALL_RECORD_FOLDER} is to store call records
*/
public static final int ID_ROOT_FOLDER = 0;
public static final int ID_TEMPARAY_FOLDER = -1;
public static final int ID_CALL_RECORD_FOLDER = -2;
public static final int ID_TRASH_FOLER = -3;
public static final String INTENT_EXTRA_ALERT_DATE = "net.micode.notes.alert_date";
public static final String INTENT_EXTRA_BACKGROUND_ID = "net.micode.notes.background_color_id";
public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id";
public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type";
public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id";
public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date";
public static final int TYPE_WIDGET_INVALIDE = -1;
public static final int TYPE_WIDGET_2X = 0;
public static final int TYPE_WIDGET_4X = 1;
public static class DataConstants { //获取记录
public static final String NOTE = TextNote.CONTENT_ITEM_TYPE;
public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE;
}
/**
* Uri to query all notes and folders
*/
public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note");//查询所有笔记与文件的uri
/**
* Uri to query data
*/
public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data");//查询数据的uri
public interface NoteColumns { //定义便签变量接口
/**
* The unique ID for a row
* <P> Type: INTEGER (long) </P>
*/
public static final String ID = "_id";
/**
* The parent's id for note or folder
* <P> Type: INTEGER (long) </P>
*/
public static final String PARENT_ID = "parent_id"; //父文件夹的id
/**
* Created data for note or folder
* <P> Type: INTEGER (long) </P>
*/
public static final String CREATED_DATE = "created_date"; //表示文件夹或便签创建日期
/**
* Latest modified date
* <P> Type: INTEGER (long) </P>
*/
public static final String MODIFIED_DATE = "modified_date"; //便签的最新的修改日期
/**
* Alert date
* <P> Type: INTEGER (long) </P>
*/
public static final String ALERTED_DATE = "alert_date"; //提醒日期
/**
* Folder's name or text content of note
* <P> Type: TEXT </P>
*/
public static final String SNIPPET = "snippet"; //文件夹名称或便签内容
/**
* Note's widget id
* <P> Type: INTEGER (long) </P>
*/
public static final String WIDGET_ID = "widget_id"; //便签的widget的id
/**
* Note's widget type
* <P> Type: INTEGER (long) </P>
*/
public static final String WIDGET_TYPE = "widget_type"; //便签widget的类型
/**
* Note's background color's id
* <P> Type: INTEGER (long) </P>
*/
public static final String BG_COLOR_ID = "bg_color_id"; //背景颜色代号
/**
* For text note, it doesn't has attachment, for multi-media
* note, it has at least one attachment
* <P> Type: INTEGER </P>
*/
public static final String HAS_ATTACHMENT = "has_attachment";
/**
* Folder's count of notes
* <P> Type: INTEGER (long) </P>
*/
public static final String NOTES_COUNT = "notes_count"; //文件夹内的便签数
/**
* The file type: folder or note
* <P> Type: INTEGER </P>
*/
public static final String TYPE = "type"; //文件类型,是文件夹或便签
/**
* The last sync id
* <P> Type: INTEGER (long) </P>
*/
public static final String SYNC_ID = "sync_id"; //最后一次同步id
/**
* Sign to indicate local modified or not
* <P> Type: INTEGER </P>
*/
public static final String LOCAL_MODIFIED = "local_modified"; //本地修改名称标识
/**
* Original parent id before moving into temporary folder
* <P> Type : INTEGER </P>
*/
public static final String ORIGIN_PARENT_ID = "origin_parent_id"; //移动文件时文件的前父类文件夹
/**
* The gtask id
* <P> Type : TEXT </P>
*/
public static final String GTASK_ID = "gtask_id";
/**
* The version code
* <P> Type : INTEGER (long) </P>
*/
public static final String VERSION = "version"; //版本名称
}
public interface DataColumns { //定义数据变量接口
/**
* The unique ID for a row
* <P> Type: INTEGER (long) </P>
*/
public static final String ID = "_id";
/**
* The MIME type of the item represented by this row.
* <P> Type: Text </P>
*/
public static final String MIME_TYPE = "mime_type";
public class Notes { // 定义类,给模块的其他类提供变量定义
public static final String AUTHORITY = "micode_notes"; // 设置AUTHORITY
public static final String TAG = "Notes"; // 设置TAG
public static final int TYPE_NOTE = 0;
public static final int TYPE_FOLDER = 1;
public static final int TYPE_SYSTEM = 2;
/**
* The reference id to note that this data belongs to
* <P> Type: INTEGER (long) </P>
* Following IDs are system folders' identifiers
* {@link Notes#ID_ROOT_FOLDER } is default folder
* {@link Notes#ID_TEMPARAY_FOLDER } is for notes belonging no folder
* {@link Notes#ID_CALL_RECORD_FOLDER} is to store call records
*/
public static final String NOTE_ID = "note_id"; //便签名称
public static final int ID_ROOT_FOLDER = 0;
public static final int ID_TEMPARAY_FOLDER = -1;
public static final int ID_CALL_RECORD_FOLDER = -2;
public static final int ID_TRASH_FOLER = -3;
public static final String INTENT_EXTRA_ALERT_DATE = "net.micode.notes.alert_date";
public static final String INTENT_EXTRA_BACKGROUND_ID = "net.micode.notes.background_color_id";
public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id";
public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type";
public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id";
public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date";
public static final int TYPE_WIDGET_INVALIDE = -1;
public static final int TYPE_WIDGET_2X = 0;
public static final int TYPE_WIDGET_4X = 1;
public static class DataConstants { // 获取记录
public static final String NOTE = TextNote.CONTENT_ITEM_TYPE;
public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE;
}
/**
* Created data for note or folder
* <P> Type: INTEGER (long) </P>
* Uri to query all notes and folders
*/
public static final String CREATED_DATE = "created_date"; //创建文件夹或者便签的名称
public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note");// 查询所有笔记与文件的uri
/**
* Latest modified date
* <P> Type: INTEGER (long) </P>
* Uri to query data
*/
public static final String MODIFIED_DATE = "modified_date"; //最后修改日期
/**
* Data's content
* <P> Type: TEXT </P>
*/
public static final String CONTENT = "content"; //便签内容数据
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* integer data type
* <P> Type: INTEGER </P>
*/
public static final String DATA1 = "data1"; //不同类型的通用数据列,用于可能的存储与查找
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* integer data type
* <P> Type: INTEGER </P>
*/
public static final String DATA2 = "data2";
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* TEXT data type
* <P> Type: TEXT </P>
*/
public static final String DATA3 = "data3";
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* TEXT data type
* <P> Type: TEXT </P>
*/
public static final String DATA4 = "data4";
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* TEXT data type
* <P> Type: TEXT </P>
*/
public static final String DATA5 = "data5";
}
public static final class TextNote implements DataColumns {
/**
* Mode to indicate the text in check list mode or not
* <P> Type: Integer 1:check list mode 0: normal mode </P> //1为列表检查模式0为普通模式
*/
public static final String MODE = DATA1; //模式
public static final int MODE_CHECK_LIST = 1; //列表检查模式
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note");//访问数据表uri
}
public static final class CallNote implements DataColumns {
/**
* Call date for this record
* <P> Type: INTEGER (long) </P>
*/
public static final String CALL_DATE = DATA1; //呼叫数据
/**
* Phone number for this record
* <P> Type: TEXT </P>
*/
public static final String PHONE_NUMBER = DATA3; //呼叫号码
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note";
public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data");// 查询数据的uri
public interface NoteColumns { // 定义便签变量接口
/**
* The unique ID for a row
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String ID = "_id";
/**
* The parent's id for note or folder
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String PARENT_ID = "parent_id"; // 父文件夹的id
/**
* Created data for note or folder
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String CREATED_DATE = "created_date"; // 表示文件夹或便签创建日期
/**
* Latest modified date
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String MODIFIED_DATE = "modified_date"; // 便签的最新的修改日期
/**
* Alert date
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String ALERTED_DATE = "alert_date"; // 提醒日期
/**
* Folder's name or text content of note
* <P>
* Type: TEXT
* </P>
*/
public static final String SNIPPET = "snippet"; // 文件夹名称或便签内容
/**
* Note's widget id
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String WIDGET_ID = "widget_id"; // 便签的widget的id
/**
* Note's widget type
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String WIDGET_TYPE = "widget_type"; // 便签widget的类型
/**
* Note's background color's id
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String BG_COLOR_ID = "bg_color_id"; // 背景颜色代号
/**
* For text note, it doesn't has attachment, for multi-media
* note, it has at least one attachment
* <P>
* Type: INTEGER
* </P>
*/
public static final String HAS_ATTACHMENT = "has_attachment";
/**
* Folder's count of notes
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String NOTES_COUNT = "notes_count"; // 文件夹内的便签数
/**
* The file type: folder or note
* <P>
* Type: INTEGER
* </P>
*/
public static final String TYPE = "type"; // 文件类型,是文件夹或便签
/**
* The last sync id
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String SYNC_ID = "sync_id"; // 最后一次同步id
/**
* Sign to indicate local modified or not
* <P>
* Type: INTEGER
* </P>
*/
public static final String LOCAL_MODIFIED = "local_modified"; // 本地修改名称标识
/**
* Original parent id before moving into temporary folder
* <P>
* Type : INTEGER
* </P>
*/
public static final String ORIGIN_PARENT_ID = "origin_parent_id"; // 移动文件时文件的前父类文件夹
/**
* The gtask id
* <P>
* Type : TEXT
* </P>
*/
public static final String GTASK_ID = "gtask_id";
/**
* The version code
* <P>
* Type : INTEGER (long)
* </P>
*/
public static final String VERSION = "version"; // 版本名称
}
public interface DataColumns { // 定义数据变量接口
/**
* The unique ID for a row
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String ID = "_id";
/**
* The MIME type of the item represented by this row.
* <P>
* Type: Text
* </P>
*/
public static final String MIME_TYPE = "mime_type";
/**
* The reference id to note that this data belongs to
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String NOTE_ID = "note_id"; // 便签名称
/**
* Created data for note or folder
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String CREATED_DATE = "created_date"; // 创建文件夹或者便签的名称
/**
* Latest modified date
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String MODIFIED_DATE = "modified_date"; // 最后修改日期
/**
* Data's content
* <P>
* Type: TEXT
* </P>
*/
public static final String CONTENT = "content"; // 便签内容数据
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* integer data type
* <P>
* Type: INTEGER
* </P>
*/
public static final String DATA1 = "data1"; // 不同类型的通用数据列,用于可能的存储与查找
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* integer data type
* <P>
* Type: INTEGER
* </P>
*/
public static final String DATA2 = "data2";
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* TEXT data type
* <P>
* Type: TEXT
* </P>
*/
public static final String DATA3 = "data3";
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* TEXT data type
* <P>
* Type: TEXT
* </P>
*/
public static final String DATA4 = "data4";
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* TEXT data type
* <P>
* Type: TEXT
* </P>
*/
public static final String DATA5 = "data5";
}
public static final class TextNote implements DataColumns {
/**
* Mode to indicate the text in check list mode or not
* <P>
* Type: Integer 1:check list mode 0: normal mode
* </P>
* //1为列表检查模式0为普通模式
*/
public static final String MODE = DATA1; // 模式
public static final int MODE_CHECK_LIST = 1; // 列表检查模式
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note");// 访问数据表uri
}
public static final class CallNote implements DataColumns {
/**
* Call date for this record
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String CALL_DATE = DATA1; // 呼叫数据
/**
* Phone number for this record
* <P>
* Type: TEXT
* </P>
*/
public static final String PHONE_NUMBER = DATA3; // 呼叫号码
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note");// 访问电话记录uri
}
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note");//访问电话记录uri
}
}

@ -26,6 +26,7 @@ import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns;
<<<<<<< HEAD
//引入Notes中接口
public class NotesDatabaseHelper extends SQLiteOpenHelper { //该类为便签SQL表的定义以及操作便于进行文件夹与便签的各种操作例如添加或删除的行为
private static final String DB_NAME = "note.db"; //数据库名称
@ -81,10 +82,67 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
private static final String CREATE_DATA_NOTE_ID_INDEX_SQL =
"CREATE INDEX IF NOT EXISTS note_id_index ON " +
TABLE.DATA + "(" + DataColumns.NOTE_ID + ");"; //创建查询操作表索引,用于查找
=======
//引入Notes中接口
public class NotesDatabaseHelper extends SQLiteOpenHelper { // 该类为便签SQL表的定义以及操作易于进行文件夹与便签的各种操作例如添加或删除的行为
private static final String DB_NAME = "note.db"; // 数据库名称
private static final int DB_VERSION = 4; // 数据库版本
public interface TABLE { // 定义一个数据库表接口
public static final String NOTE = "note"; // 便签名
public static final String DATA = "data"; // 便签数据
}
private static final String TAG = "NotesDatabaseHelper"; // 日志类型
private static NotesDatabaseHelper mInstance; // 创建NotesDatabaseHelper类对象
private static final String CREATE_NOTE_TABLE_SQL = // SQL语句创建便签表包含ID、最后修改日期、背景颜色、便签数等在Notes类中的内容
"CREATE TABLE " + TABLE.NOTE + "(" +
NoteColumns.ID + " INTEGER PRIMARY KEY," +
NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," +
NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," +
NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," +
NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" +
")";
private static final String CREATE_DATA_TABLE_SQL = // 创建数据表的SQL语句
"CREATE TABLE " + TABLE.DATA + "(" +
DataColumns.ID + " INTEGER PRIMARY KEY," +
DataColumns.MIME_TYPE + " TEXT NOT NULL," +
DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," +
DataColumns.DATA1 + " INTEGER," +
DataColumns.DATA2 + " INTEGER," +
DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," +
DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," +
DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" +
")";
private static final String CREATE_DATA_NOTE_ID_INDEX_SQL = "CREATE INDEX IF NOT EXISTS note_id_index ON " +
TABLE.DATA + "(" + DataColumns.NOTE_ID + ");"; // 创建查询操作表索引,用于查找
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* Increase folder's note count when move note to the folder
*/
<<<<<<< HEAD
private static final String NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER =
"CREATE TRIGGER increase_folder_count_on_update "+
" AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE +
@ -93,10 +151,21 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +
" WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +
" END"; //创建触发器,方便进行操作,当移动便签时统计新的父文件夹内便签数量
=======
private static final String NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = "CREATE TRIGGER increase_folder_count_on_update "
+
" AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE +
" BEGIN " +
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +
" WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +
" END"; // 创建触发器,方便进行操作,当移动便签时统计新的父文件夹内便签数量
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* Decrease folder's note count when move note from folder
*/
<<<<<<< HEAD
private static final String NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER =
"CREATE TRIGGER decrease_folder_count_on_update " +
" AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE +
@ -106,10 +175,22 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
" AND " + NoteColumns.NOTES_COUNT + ">0" + ";" +
" END"; //与上一个方法作用相反,统计旧的父文件夹便签数量
=======
private static final String NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = "CREATE TRIGGER decrease_folder_count_on_update "
+
" AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE +
" BEGIN " +
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" +
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
" AND " + NoteColumns.NOTES_COUNT + ">0" + ";" +
" END"; // 与上一个方法作用相反,统计旧的父文件夹便签数量
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* Increase folder's note count when insert new note to the folder
*/
<<<<<<< HEAD
private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER =
"CREATE TRIGGER increase_folder_count_on_insert " +
" AFTER INSERT ON " + TABLE.NOTE +
@ -118,10 +199,21 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +
" WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +
" END"; //创建便签时增加统计数量
=======
private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER = "CREATE TRIGGER increase_folder_count_on_insert "
+
" AFTER INSERT ON " + TABLE.NOTE +
" BEGIN " +
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +
" WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +
" END"; // 创建便签时增加统计数量
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* Decrease folder's note count when delete note from the folder
*/
<<<<<<< HEAD
private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER =
"CREATE TRIGGER decrease_folder_count_on_delete " +
" AFTER DELETE ON " + TABLE.NOTE +
@ -131,10 +223,22 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
" AND " + NoteColumns.NOTES_COUNT + ">0;" +
" END"; //删除时减少数量
=======
private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER = "CREATE TRIGGER decrease_folder_count_on_delete "
+
" AFTER DELETE ON " + TABLE.NOTE +
" BEGIN " +
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" +
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
" AND " + NoteColumns.NOTES_COUNT + ">0;" +
" END"; // 删除时减少数量
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* Update note's content when insert data with type {@link DataConstants#NOTE}
*/
<<<<<<< HEAD
private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER =
"CREATE TRIGGER update_note_content_on_insert " +
" AFTER INSERT ON " + TABLE.DATA +
@ -144,10 +248,23 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT +
" WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +
" END"; //更新便签数据时更改table表内容
=======
private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER = "CREATE TRIGGER update_note_content_on_insert "
+
" AFTER INSERT ON " + TABLE.DATA +
" WHEN new." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" +
" BEGIN" +
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT +
" WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +
" END"; // 更新便签数据时更改table表内容
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* Update note's content when data with {@link DataConstants#NOTE} type has changed
* Update note's content when data with {@link DataConstants#NOTE} type has
* changed
*/
<<<<<<< HEAD
private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER =
"CREATE TRIGGER update_note_content_on_update " +
" AFTER UPDATE ON " + TABLE.DATA +
@ -157,10 +274,23 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT +
" WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +
" END"; //改变便签数据时更改table表内容
=======
private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER = "CREATE TRIGGER update_note_content_on_update "
+
" AFTER UPDATE ON " + TABLE.DATA +
" WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" +
" BEGIN" +
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT +
" WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +
" END"; // 改变便签数据时更改table表内容
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* Update note's content when data with {@link DataConstants#NOTE} type has deleted
* Update note's content when data with {@link DataConstants#NOTE} type has
* deleted
*/
<<<<<<< HEAD
private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER =
"CREATE TRIGGER update_note_content_on_delete " +
" AFTER delete ON " + TABLE.DATA +
@ -170,10 +300,22 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" SET " + NoteColumns.SNIPPET + "=''" +
" WHERE " + NoteColumns.ID + "=old." + DataColumns.NOTE_ID + ";" +
" END"; //删除便签数据时改变table表内容
=======
private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER = "CREATE TRIGGER update_note_content_on_delete "
+
" AFTER delete ON " + TABLE.DATA +
" WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" +
" BEGIN" +
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.SNIPPET + "=''" +
" WHERE " + NoteColumns.ID + "=old." + DataColumns.NOTE_ID + ";" +
" END"; // 删除便签数据时改变table表内容
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* Delete datas belong to note which has been deleted
*/
<<<<<<< HEAD
private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER =
"CREATE TRIGGER delete_data_on_delete " +
" AFTER DELETE ON " + TABLE.NOTE +
@ -181,10 +323,19 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" DELETE FROM " + TABLE.DATA +
" WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" +
" END"; //删除便签时删除便签内容
=======
private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER = "CREATE TRIGGER delete_data_on_delete " +
" AFTER DELETE ON " + TABLE.NOTE +
" BEGIN" +
" DELETE FROM " + TABLE.DATA +
" WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" +
" END"; // 删除便签时删除便签内容
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* Delete notes belong to folder which has been deleted
*/
<<<<<<< HEAD
private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER =
"CREATE TRIGGER folder_delete_notes_on_delete " +
" AFTER DELETE ON " + TABLE.NOTE +
@ -192,10 +343,20 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" DELETE FROM " + TABLE.NOTE +
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
" END"; //删除文件夹时删除内部的便签
=======
private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER = "CREATE TRIGGER folder_delete_notes_on_delete "
+
" AFTER DELETE ON " + TABLE.NOTE +
" BEGIN" +
" DELETE FROM " + TABLE.NOTE +
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
" END"; // 删除文件夹时删除内部的便签
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* Move notes belong to folder which has been moved to trash folder
*/
<<<<<<< HEAD
private static final String FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER =
"CREATE TRIGGER folder_move_notes_on_trash " +
" AFTER UPDATE ON " + TABLE.NOTE +
@ -218,6 +379,29 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
}
private void reCreateNoteTableTriggers(SQLiteDatabase db) { //数据库对象DB
=======
private static final String FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER = "CREATE TRIGGER folder_move_notes_on_trash " +
" AFTER UPDATE ON " + TABLE.NOTE +
" WHEN new." + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER +
" BEGIN" +
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER +
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
" END"; // 移动便签的父文件夹时便签也一起更改
public NotesDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
} // 创建一个数据库
public void createNoteTable(SQLiteDatabase db) {
db.execSQL(CREATE_NOTE_TABLE_SQL);
reCreateNoteTableTriggers(db); // 重新创建触发器
createSystemFolder(db); // 创建系统文件夹
Log.d(TAG, "note table has been created"); // 创建一个新的便签表,返回日志
}
private void reCreateNoteTableTriggers(SQLiteDatabase db) { // 数据库对象DB
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update");
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update");
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_delete");
@ -233,17 +417,28 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER);
db.execSQL(FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER);
db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER);
<<<<<<< HEAD
} //用于执行SQL语句运行重新创建便签表触发器函数
private void createSystemFolder(SQLiteDatabase db) { //创建系统级文件夹
ContentValues values = new ContentValues(); //为字典类创建对象
=======
} // 用于执行SQL语句运行重新创建便签表触发器函数
private void createSystemFolder(SQLiteDatabase db) { // 创建系统级文件夹
ContentValues values = new ContentValues(); // 为字典类创建对象
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* call record foler for call notes
*/
values.put(NoteColumns.ID, Notes.ID_CALL_RECORD_FOLDER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
<<<<<<< HEAD
db.insert(TABLE.NOTE, null, values); //为通讯便签创建记录文件夹,并添加键值对
=======
db.insert(TABLE.NOTE, null, values); // 为通讯便签创建记录文件夹,并添加键值对
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* root folder which is default folder
@ -251,7 +446,11 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
values.clear();
values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
<<<<<<< HEAD
db.insert(TABLE.NOTE, null, values); //创建根文件夹,是默认的、最终的父文件夹
=======
db.insert(TABLE.NOTE, null, values); // 创建根文件夹,是默认的、最终的父文件夹
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* temporary folder which is used for moving note
@ -259,7 +458,11 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
values.clear();
values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
<<<<<<< HEAD
db.insert(TABLE.NOTE, null, values); //为移动中的便签创建的临时文件夹
=======
db.insert(TABLE.NOTE, null, values); // 为移动中的便签创建的临时文件夹
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* create trash folder
@ -267,6 +470,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
values.clear();
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
<<<<<<< HEAD
db.insert(TABLE.NOTE, null, values); //创建垃圾桶
}
@ -285,6 +489,26 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER);
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER);
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER); //重新创建被删除的触发器
=======
db.insert(TABLE.NOTE, null, values); // 创建垃圾桶
}
public void createDataTable(SQLiteDatabase db) { // 创建数据表
db.execSQL(CREATE_DATA_TABLE_SQL); // SQL输入保存的语句
reCreateDataTableTriggers(db);
db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL);
Log.d(TAG, "data table has been created"); // 日志记录
}
private void reCreateDataTableTriggers(SQLiteDatabase db) { // 为数据表重新创建触发器
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_insert");
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_update");
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_delete"); // 删除触发器
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER);
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER);
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER); // 重新创建被删除的触发器
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
}
static synchronized NotesDatabaseHelper getInstance(Context context) {
@ -292,6 +516,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
mInstance = new NotesDatabaseHelper(context);
}
return mInstance;
<<<<<<< HEAD
} //静态同步方法用于返回NotesDatabaseHelper的对象实例mInstance
@Override
@ -302,6 +527,18 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //用于检查并升级数据库版本
=======
} // 静态同步方法用于返回NotesDatabaseHelper的对象实例mInstance
@Override
public void onCreate(SQLiteDatabase db) {
createNoteTable(db); // 创建便签表
createDataTable(db); // 创建数据表
} // 用于初始化表
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // 用于检查并升级数据库版本
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
boolean reCreateTriggers = false;
boolean skipV2 = false;
@ -309,22 +546,36 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
upgradeToV2(db);
skipV2 = true; // this upgrade including the upgrade from v2 to v3
oldVersion++;
<<<<<<< HEAD
} //检测到版本为1升级版本
if (oldVersion == 2 && !skipV2) {
upgradeToV3(db);
reCreateTriggers = true; //重建触发器标志
oldVersion++; //检测到版本为2升级版本
=======
} // 检测到版本为1升级版本
if (oldVersion == 2 && !skipV2) {
upgradeToV3(db);
reCreateTriggers = true; // 重建触发器标志
oldVersion++; // 检测到版本为2升级版本
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
}
if (oldVersion == 3) {
upgradeToV4(db);
oldVersion++;
<<<<<<< HEAD
} //检测到版本为3升级版本
=======
} // 检测到版本为3升级版本
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
if (reCreateTriggers) {
reCreateNoteTableTriggers(db);
reCreateDataTableTriggers(db);
<<<<<<< HEAD
} //进行触发器的重建
if (oldVersion != newVersion) {
@ -334,6 +585,17 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
}
private void upgradeToV2(SQLiteDatabase db) { //升级版本并重建表
=======
} // 进行触发器的重建
if (oldVersion != newVersion) {
throw new IllegalStateException("Upgrade notes database to version " + newVersion
+ "fails"); // 版本不匹配则抛出异常
}
}
private void upgradeToV2(SQLiteDatabase db) { // 升级版本并重建表
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA);
createNoteTable(db);
@ -349,14 +611,23 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID
+ " TEXT NOT NULL DEFAULT ''");
// add a trash system folder
ContentValues values = new ContentValues();
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
db.insert(TABLE.NOTE, null, values);
<<<<<<< HEAD
} //删除不用的触发器,并添加不为空的新列,添加键值对
=======
} // 删除不用的触发器,并添加不为空的新列,添加键值对
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
private void upgradeToV4(SQLiteDatabase db) {
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION
+ " INTEGER NOT NULL DEFAULT 0");
<<<<<<< HEAD
} //添加新的不为空的列
=======
} // 添加新的不为空的列
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
}

@ -16,7 +16,6 @@
package net.micode.notes.data;
import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentUris;
@ -34,7 +33,10 @@ import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
public class NotesProvider extends ContentProvider {// uri操作相关类用于对数据库进行操作
private static final UriMatcher mMatcher; // UriMatcher类用于分辨数据操作类型用于匹配不同的uri
<<<<<<< HEAD
public class NotesProvider extends ContentProvider {
private static final UriMatcher mMatcher; //UriMatcher类用于分辨数据表
@ -52,6 +54,22 @@ public class NotesProvider extends ContentProvider {
static {
mMatcher = new UriMatcher(UriMatcher.NO_MATCH); //进行初始化
=======
private NotesDatabaseHelper mHelper; // 建立NotesDatabaseHelper类对象用于数据库操作
private static final String TAG = "NotesProvider"; // 设置类名标签,用于调试
private static final int URI_NOTE = 1; // 设置便签uri
private static final int URI_NOTE_ITEM = 2;
private static final int URI_DATA = 3; // 设置数据uri
private static final int URI_DATA_ITEM = 4;
private static final int URI_SEARCH = 5; // 设置搜索uri
private static final int URI_SEARCH_SUGGEST = 6;
static {
mMatcher = new UriMatcher(UriMatcher.NO_MATCH); // 进行初始化
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM);
mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA);
@ -59,11 +77,17 @@ public class NotesProvider extends ContentProvider {
mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH);
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, URI_SEARCH_SUGGEST);
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST);
<<<<<<< HEAD
} //将不同的uri与数值相匹配
=======
} // 将不同的uri与数值相匹配
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* x'0A' represents the '\n' character in sqlite. For title and content in the search result,
* x'0A' represents the '\n' character in sqlite. For title and content in the
* search result,
* we will trim '\n' and white space in order to show more information.
<<<<<<< HEAD
*/ //用0A代表\n为搜索结果换行
private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + ","
+ NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + ","
@ -78,15 +102,36 @@ public class NotesProvider extends ContentProvider {
+ " WHERE " + NoteColumns.SNIPPET + " LIKE ?"
+ " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER
+ " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE; //检索便签的sql语句在列表中搜寻。使用Notes类中的接口与变量
=======
*/ // 用0A代表\n为搜索结果换行
private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + ","
+ NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + ","
+ "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_1 + ","
+ "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + ","
+ R.drawable.search_result + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1 + ","
+ "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + ","
+ "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA; // ...
private static String NOTES_SNIPPET_SEARCH_QUERY = "SELECT " + NOTES_SEARCH_PROJECTION
+ " FROM " + TABLE.NOTE
+ " WHERE " + NoteColumns.SNIPPET + " LIKE ?"
+ " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER
+ " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE; // 检索便签的sql语句在列表中搜寻。使用Notes类中的接口与变量
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
@Override
public boolean onCreate() {
mHelper = NotesDatabaseHelper.getInstance(getContext());
<<<<<<< HEAD
return true; //创建NotesDatabaseHelper类并返回成果标识
=======
return true; // 创建NotesDatabaseHelper类并返回成果标识
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
<<<<<<< HEAD
String sortOrder) { //负责查询数据接受uri、返回烈、选择、排列方式
Cursor c = null; //创建空游标,表示数据库操作结果集合
SQLiteDatabase db = mHelper.getReadableDatabase(); //获取数据库对象
@ -95,12 +140,26 @@ public class NotesProvider extends ContentProvider {
case URI_NOTE:
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
sortOrder); //传参,进行标准查询
=======
String sortOrder) { // 负责查询数据接受uri、返回烈、选择、排列方式
Cursor c = null; // 创建空游标,表示数据库操作结果集合
SQLiteDatabase db = mHelper.getReadableDatabase(); // 获取数据库对象
String id = null;
switch (mMatcher.match(uri)) { // 确定uri类型
case URI_NOTE:
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
sortOrder); // 传参,进行标准查询
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
break;
case URI_NOTE_ITEM:
id = uri.getPathSegments().get(1);
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
<<<<<<< HEAD
break; //传参对id进行查询
=======
break; // 传参对id进行查询
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
case URI_DATA:
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null,
sortOrder);
@ -112,13 +171,21 @@ public class NotesProvider extends ContentProvider {
break;
case URI_SEARCH:
case URI_SEARCH_SUGGEST:
<<<<<<< HEAD
if (sortOrder != null || projection != null) { //检查是否指定排序方式
=======
if (sortOrder != null || projection != null) { // 检查是否指定排序方式
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
throw new IllegalArgumentException(
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");
}
String searchString = null;
<<<<<<< HEAD
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) { //提取uri中搜索模式
=======
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) { // 提取uri中搜索模式
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
if (uri.getPathSegments().size() > 1) {
searchString = uri.getPathSegments().get(1);
}
@ -133,6 +200,7 @@ public class NotesProvider extends ContentProvider {
try {
searchString = String.format("%%%s%%", searchString);
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
<<<<<<< HEAD
new String[] { searchString }); //在Notes表进行搜索
} catch (IllegalStateException ex) {
Log.e(TAG, "got exception: " + ex.toString()); //日志
@ -144,10 +212,24 @@ public class NotesProvider extends ContentProvider {
if (c != null) {
c.setNotificationUri(getContext().getContentResolver(), uri); //在游标查询的数据变化时进行通知
} //通知uri是标识查询结果的urisetNotificationUri方法可注册通知uri与对象c相关联
=======
new String[] { searchString }); // 在Notes表进行搜索
} catch (IllegalStateException ex) {
Log.e(TAG, "got exception: " + ex.toString()); // 日志
}
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri); // 抛出异常
}
if (c != null) {
c.setNotificationUri(getContext().getContentResolver(), uri); // 在游标查询的数据变化时进行通知
} // 通知uri是标识查询结果的urisetNotificationUri方法可注册通知uri与对象c相关联
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
return c;
}
@Override
<<<<<<< HEAD
public Uri insert(Uri uri, ContentValues values) { //uri表示要插入的urivalues表示要插入的数据
SQLiteDatabase db = mHelper.getWritableDatabase(); //打开或创建一个数据库
long dataId = 0, noteId = 0, insertedId = 0;
@ -162,14 +244,36 @@ public class NotesProvider extends ContentProvider {
Log.d(TAG, "Wrong data format without note id:" + values.toString()); //日志记录
}
insertedId = dataId = db.insert(TABLE.DATA, null, values); //数据库插入新行的id返回到两个变量中
=======
public Uri insert(Uri uri, ContentValues values) { // uri表示要插入的urivalues表示要插入的数据
SQLiteDatabase db = mHelper.getWritableDatabase(); // 打开或创建一个数据库
long dataId = 0, noteId = 0, insertedId = 0;
switch (mMatcher.match(uri)) { // 匹配uri
case URI_NOTE:
insertedId = noteId = db.insert(TABLE.NOTE, null, values); // 将数据插入到Notes表中
break;
case URI_DATA:
if (values.containsKey(DataColumns.NOTE_ID)) { // 检查values是否含有NOTE_ID内容返回bool值
noteId = values.getAsLong(DataColumns.NOTE_ID); // 关联noteId
} else {
Log.d(TAG, "Wrong data format without note id:" + values.toString()); // 日志记录
}
insertedId = dataId = db.insert(TABLE.DATA, null, values); // 数据库插入新行的id返回到两个变量中
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
// Notify the note uri
<<<<<<< HEAD
if (noteId > 0) { //不为零表示插入成功,
getContext().getContentResolver().notifyChange(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); //通知uri发生变化
=======
if (noteId > 0) { // 不为零表示插入成功,
getContext().getContentResolver().notifyChange(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); // 通知uri发生变化
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
}
// Notify the data uri
@ -178,6 +282,7 @@ public class NotesProvider extends ContentProvider {
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
}
<<<<<<< HEAD
return ContentUris.withAppendedId(uri, insertedId); //返回新的uri表示插入位置
}
@ -194,6 +299,25 @@ public class NotesProvider extends ContentProvider {
break;
case URI_NOTE_ITEM:
id = uri.getPathSegments().get(1); //获取id
=======
return ContentUris.withAppendedId(uri, insertedId); // 返回新的uri表示插入位置
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) { // 表示要删除的uri、SQL中的WHERE与参数
int count = 0; // 表示删除记录数
String id = null;
SQLiteDatabase db = mHelper.getWritableDatabase(); // 打开数据库
boolean deleteData = false;
switch (mMatcher.match(uri)) { // uri匹配
case URI_NOTE:
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 "; // 设置WHERE
count = db.delete(TABLE.NOTE, selection, selectionArgs); // 删除,并计数
break;
case URI_NOTE_ITEM:
id = uri.getPathSegments().get(1); // 获取id
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
/**
* ID that smaller than 0 is system folder which is not allowed to
* trash
@ -204,6 +328,7 @@ public class NotesProvider extends ContentProvider {
}
count = db.delete(TABLE.NOTE,
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
<<<<<<< HEAD
break; //删除指定id并计数
case URI_DATA:
count = db.delete(TABLE.DATA, selection, selectionArgs); //删除,并计数
@ -219,11 +344,30 @@ public class NotesProvider extends ContentProvider {
throw new IllegalArgumentException("Unknown URI " + uri); //报错
}
if (count > 0) { //表示有删除操作通知uri发生变化
=======
break; // 删除指定id并计数
case URI_DATA:
count = db.delete(TABLE.DATA, selection, selectionArgs); // 删除,并计数
deleteData = true;
break;
case URI_DATA_ITEM:
id = uri.getPathSegments().get(1); // 获取id
count = db.delete(TABLE.DATA,
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
deleteData = true; // 删除指定id并计数
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri); // 报错
}
if (count > 0) { // 表示有删除操作通知uri发生变化
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
if (deleteData) {
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
}
getContext().getContentResolver().notifyChange(uri, null);
}
<<<<<<< HEAD
return count; //返回删除的记录数
}
@ -243,6 +387,27 @@ public class NotesProvider extends ContentProvider {
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
+ parseSelection(selection), selectionArgs); //增加指定的id条件
=======
return count; // 返回删除的记录数
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // 表示更新的uri、values为新的列值、where以及参数
int count = 0; // 更新条数
String id = null;
SQLiteDatabase db = mHelper.getWritableDatabase(); // 打开数据库
boolean updateData = false; // 设置数据表更新表示
switch (mMatcher.match(uri)) { // 匹配uri
case URI_NOTE:
increaseNoteVersion(-1, selection, selectionArgs); // 跟新便签
count = db.update(TABLE.NOTE, values, selection, selectionArgs); // 更新指定表的行,并计数
break;
case URI_NOTE_ITEM:
id = uri.getPathSegments().get(1); // 提取id
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
+ parseSelection(selection), selectionArgs); // 增加指定的id条件
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
break;
case URI_DATA:
count = db.update(TABLE.DATA, values, selection, selectionArgs);
@ -255,50 +420,89 @@ public class NotesProvider extends ContentProvider {
updateData = true;
break;
default:
<<<<<<< HEAD
throw new IllegalArgumentException("Unknown URI " + uri); //抛出异常
=======
throw new IllegalArgumentException("Unknown URI " + uri); // 抛出异常
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
}
if (count > 0) {
if (updateData) {
<<<<<<< HEAD
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);//通知指定uri数据更改
}
getContext().getContentResolver().notifyChange(uri, null); //通知uri数据更改
=======
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);// 通知指定uri数据更改
}
getContext().getContentResolver().notifyChange(uri, null); // 通知uri数据更改
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
}
return count;
}
<<<<<<< HEAD
private String parseSelection(String selection) { //检查selection是否为空
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); //为空则返回“”
}
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) { //使用数据库更新便签版本
=======
private String parseSelection(String selection) { // 检查selection是否为空
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); // 为空则返回“”
}
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) { // 使用数据库更新便签版本
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
StringBuilder sql = new StringBuilder(120);
sql.append("UPDATE ");
sql.append(TABLE.NOTE);
sql.append(" SET ");
sql.append(NoteColumns.VERSION);
<<<<<<< HEAD
sql.append("=" + NoteColumns.VERSION + "+1 "); //升级便签版本号
if (id > 0 || !TextUtils.isEmpty(selection)) { //selection不为空或存在id则增加where条件
=======
sql.append("=" + NoteColumns.VERSION + "+1 "); // 升级便签版本号
if (id > 0 || !TextUtils.isEmpty(selection)) { // selection不为空或存在id则增加where条件
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
sql.append(" WHERE ");
}
if (id > 0) {
sql.append(NoteColumns.ID + "=" + String.valueOf(id));
}
if (!TextUtils.isEmpty(selection)) {
<<<<<<< HEAD
String selectString = id > 0 ? parseSelection(selection) : selection; //parseSelection将传入的参数中的占位符替换为实际的值
for (String args : selectionArgs) { //
selectString = selectString.replaceFirst("\\?", args); //replaceFirst选择参数数组中的值替换语句中的占位符
=======
String selectString = id > 0 ? parseSelection(selection) : selection; // parseSelection将传入的参数中的占位符替换为实际的值
for (String args : selectionArgs) { //
selectString = selectString.replaceFirst("\\?", args); // replaceFirst选择参数数组中的值替换语句中的占位符
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
}
sql.append(selectString);
}
<<<<<<< HEAD
mHelper.getWritableDatabase().execSQL(sql.toString()); //执行SQL语句
}
@Override
public String getType(Uri uri) { //获取指定uri的MIME类型
// TODO Auto-generated method stub //表示该方法没有实现
=======
mHelper.getWritableDatabase().execSQL(sql.toString()); // 执行SQL语句
}
@Override
public String getType(Uri uri) { // 获取指定uri的MIME类型
// TODO Auto-generated method stub //表示该方法没有实现
>>>>>>> 17cc1b00f633e1a6fec3efd17e03a80bcf9060a1
return null;
}
}

@ -24,31 +24,33 @@ import net.micode.notes.tool.GTaskStringUtils;
import org.json.JSONException;
import org.json.JSONObject;
public class MetaData extends Task {
private final static String TAG = MetaData.class.getSimpleName();
// 通过getSimpleName()获得类的gid并存入字符串中
private final static String TAG = MetaData.class.getSimpleName();
private String mRelatedGid = null;
// 设置元数据,生成元数据库
public void setMeta(String gid, JSONObject metaInfo) {
try {
// 注释方法块
metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);
} catch (JSONException e) {
// 生成无法创建关联gid的日志
Log.e(TAG, "failed to put related gid");
}
setNotes(metaInfo.toString());
setName(GTaskStringUtils.META_NOTE_NAME);
}
// 获取关联的gid
public String getRelatedGid() {
return mRelatedGid;
}
// 判断数据是否为空是否值得保存
@Override
public boolean isWorthSaving() {
return getNotes() != null;
}
// 使用远程json设置元数据内容
@Override
public void setContentByRemoteJSON(JSONObject js) {
super.setContentByRemoteJSON(js);
@ -62,18 +64,18 @@ public class MetaData extends Task {
}
}
}
// 使用本地json对象设置数据内容该方法不应该被调用若调用则抛出异常
@Override
public void setContentByLocalJSON(JSONObject js) {
// this function should not be called
throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
}
// 从元数据中获取本地json对象该方法不应该被调用若调用则抛出异常
@Override
public JSONObject getLocalJSONFromContent() {
throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
}
// 获取同步动作状态,该方法不应该被调用,若调用则抛出异常
@Override
public int getSyncAction(Cursor c) {
throw new IllegalAccessError("MetaData:getSyncAction should not be called");

@ -19,32 +19,33 @@ package net.micode.notes.gtask.data;
import android.database.Cursor;
import org.json.JSONObject;
// 对同步操作的基本节点进行一个定义
public abstract class Node {
// 本地和云端内容一致
public static final int SYNC_ACTION_NONE = 0;
// 需要在远程云端增加内容
public static final int SYNC_ACTION_ADD_REMOTE = 1;
// 需要在本地增加内容
public static final int SYNC_ACTION_ADD_LOCAL = 2;
// 需要在远程云端和删除内容
public static final int SYNC_ACTION_DEL_REMOTE = 3;
// 需要在本地删除内容
public static final int SYNC_ACTION_DEL_LOCAL = 4;
// 需要将本地内容更新至云端
public static final int SYNC_ACTION_UPDATE_REMOTE = 5;
// 需要将云端内容更新至本地
public static final int SYNC_ACTION_UPDATE_LOCAL = 6;
// 同步冲突
public static final int SYNC_ACTION_UPDATE_CONFLICT = 7;
// 同步错误
public static final int SYNC_ACTION_ERROR = 8;
private String mGid;
private String mName;
// 记录最后一次修改的时间
private long mLastModified;
// 表征是否被删除
private boolean mDeleted;
public Node() {

@ -13,7 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Description用于支持小米便签最底层的数据库相关操作和sqlnote的关系上是子集关系即data是note的子集节点
// SqlData其实就是也就是所谓数据中的数据
package net.micode.notes.gtask.data;
import android.content.ContentResolver;
@ -34,17 +35,17 @@ import net.micode.notes.gtask.exception.ActionFailureException;
import org.json.JSONException;
import org.json.JSONObject;
public class SqlData {
// 将得到的内容写入字符串tag中
private static final String TAG = SqlData.class.getSimpleName();
// 设置无效id为-99999
private static final int INVALID_ID = -99999;
// 集合interface DataColumns中所有sf常量
public static final String[] PROJECTION_DATA = new String[] {
DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1,
DataColumns.DATA3
};
// 为sql表编号
public static final int DATA_ID_COLUMN = 0;
public static final int DATA_MIME_TYPE_COLUMN = 1;
@ -56,7 +57,7 @@ public class SqlData {
public static final int DATA_CONTENT_DATA_3_COLUMN = 4;
private ContentResolver mContentResolver;
// 判断是否用content生成真为true假为false
private boolean mIsCreate;
private long mDataId;
@ -70,7 +71,7 @@ public class SqlData {
private String mDataContentData3;
private ContentValues mDiffDataValues;
// 初始化数据库的构造函数
public SqlData(Context context) {
mContentResolver = context.getContentResolver();
mIsCreate = true;
@ -81,14 +82,14 @@ public class SqlData {
mDataContentData3 = "";
mDiffDataValues = new ContentValues();
}
// 初始化数据库的构造函数
public SqlData(Context context, Cursor c) {
mContentResolver = context.getContentResolver();
mIsCreate = false;
loadFromCursor(c);
mDiffDataValues = new ContentValues();
}
// 从游标处加载数据
private void loadFromCursor(Cursor c) {
mDataId = c.getLong(DATA_ID_COLUMN);
mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN);
@ -96,7 +97,7 @@ public class SqlData {
mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN);
mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN);
}
// 设置用于共享的数据
public void setContent(JSONObject js) throws JSONException {
long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID;
if (mIsCreate || mDataId != dataId) {
@ -129,7 +130,7 @@ public class SqlData {
}
mDataContentData3 = dataContentData3;
}
// 获取共享数据的内容
public JSONObject getContent() throws JSONException {
if (mIsCreate) {
Log.e(TAG, "it seems that we haven't created this in database yet");
@ -143,7 +144,7 @@ public class SqlData {
js.put(DataColumns.DATA3, mDataContentData3);
return js;
}
// 提交更改至数据库
public void commit(long noteId, boolean validateVersion, long version) {
if (mIsCreate) {
@ -182,7 +183,7 @@ public class SqlData {
mDiffDataValues.clear();
mIsCreate = false;
}
// 获取当前id
public long getId() {
return mDataId;
}

@ -37,12 +37,15 @@ import org.json.JSONObject;
import java.util.ArrayList;
// 用于支持小米便签最底层的数据库相关操作和sqldata的关系上是父集关系即note是data的子父集
// 和SqlData相比SqlNote算是真正意义上的数据
public class SqlNote {
// 得到的类名写入tag中
private static final String TAG = SqlNote.class.getSimpleName();
private static final int INVALID_ID = -99999;
// 集合了interface NoteColumns中所有SF常量
public static final String[] PROJECTION_NOTE = new String[] {
NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID,
NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE,
@ -121,7 +124,7 @@ public class SqlNote {
private ContentValues mDiffNoteValues;
private ArrayList<SqlData> mDataList;
// 构造函数初始化context参数
public SqlNote(Context context) {
mContext = context;
mContentResolver = context.getContentResolver();
@ -142,7 +145,7 @@ public class SqlNote {
mDiffNoteValues = new ContentValues();
mDataList = new ArrayList<SqlData>();
}
// 通过游标初始化context
public SqlNote(Context context, Cursor c) {
mContext = context;
mContentResolver = context.getContentResolver();
@ -165,7 +168,7 @@ public class SqlNote {
mDiffNoteValues = new ContentValues();
}
// 从游标加载数据
private void loadFromCursor(long id) {
Cursor c = null;
try {
@ -184,7 +187,7 @@ public class SqlNote {
c.close();
}
}
// 从游标加载数据
private void loadFromCursor(Cursor c) {
mId = c.getLong(ID_COLUMN);
mAlertDate = c.getLong(ALERTED_DATE_COLUMN);
@ -199,7 +202,7 @@ public class SqlNote {
mWidgetType = c.getInt(WIDGET_TYPE_COLUMN);
mVersion = c.getLong(VERSION_COLUMN);
}
// 获取共享数据并加载到数据库当前游标处
private void loadDataContent() {
Cursor c = null;
mDataList.clear();
@ -225,7 +228,7 @@ public class SqlNote {
c.close();
}
}
// 设置通过content机制用于共享的数据信息
public boolean setContent(JSONObject js) {
try {
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
@ -358,7 +361,7 @@ public class SqlNote {
}
return true;
}
// 获取content机制提供的数据并加载到note中
public JSONObject getContent() {
try {
JSONObject js = new JSONObject();
@ -406,40 +409,40 @@ public class SqlNote {
}
return null;
}
//给当前id设置父id
public void setParentId(long id) {
mParentId = id;
mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
}
// 给当前id设置Gtaskid
public void setGtaskId(String gid) {
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
}
// 给当前id设置同步id
public void setSyncId(long syncId) {
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
}
// 初始化本地修改,即撤销
public void resetLocalModified() {
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
}
// 获得当前id
public long getId() {
return mId;
}
// 获得id的父id
public long getParentId() {
return mParentId;
}
// 获取小部分内容以显示部分便签内容
public String getSnippet() {
return mSnippet;
}
// 判断是否为便签类型
public boolean isNoteType() {
return mType == Notes.TYPE_NOTE;
}
// 提交更改到数据库
public void commit(boolean validateVersion) {
if (mIsCreate) {
if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {

@ -34,17 +34,17 @@ import org.json.JSONObject;
public class Task extends Node {
private static final String TAG = Task.class.getSimpleName();
// 完成判断
private boolean mCompleted;
private String mNotes;
// 实例化存储的数据类型
private JSONObject mMetaInfo;
// 兄弟任务的指针
private Task mPriorSibling;
// 父类指针
private TaskList mParent;
// 初始化构造方法
public Task() {
super();
mCompleted = false;
@ -53,7 +53,7 @@ public class Task extends Node {
mParent = null;
mMetaInfo = null;
}
// 用于创建jsonobject对象获取创建新行为的操作
public JSONObject getCreateAction(int actionId) {
JSONObject js = new JSONObject();
@ -102,7 +102,7 @@ public class Task extends Node {
return js;
}
// 用于创建jsonobject对象实现创建更新行为的操作
public JSONObject getUpdateAction(int actionId) {
JSONObject js = new JSONObject();
@ -134,7 +134,7 @@ public class Task extends Node {
return js;
}
// 用于从远端目录创建
public void setContentByRemoteJSON(JSONObject js) {
if (js != null) {
try {
@ -174,7 +174,7 @@ public class Task extends Node {
}
}
}
// 本地创建
public void setContentByLocalJSON(JSONObject js) {
if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)
|| !js.has(GTaskStringUtils.META_HEAD_DATA)) {
@ -203,7 +203,7 @@ public class Task extends Node {
e.printStackTrace();
}
}
// 从目录获取本地json
public JSONObject getLocalJSONFromContent() {
String name = getName();
try {
@ -246,7 +246,7 @@ public class Task extends Node {
return null;
}
}
// 设置meta信息
public void setMetaInfo(MetaData metaData) {
if (metaData != null && metaData.getNotes() != null) {
try {
@ -257,7 +257,7 @@ public class Task extends Node {
}
}
}
// 获取同步行为
public int getSyncAction(Cursor c) {
try {
JSONObject noteInfo = null;

@ -32,9 +32,9 @@ import java.util.ArrayList;
public class TaskList extends Node {
private static final String TAG = TaskList.class.getSimpleName();
// 当前TaskList的指针
private int mIndex;
// 类中主要的保存数据的单元用来实现一个以Task为元素的ArrayList
private ArrayList<Task> mChildren;
public TaskList() {
@ -42,7 +42,7 @@ public class TaskList extends Node {
mChildren = new ArrayList<Task>();
mIndex = 1;
}
// 生成并返回一个包含了一定数据的JSONObject实体
public JSONObject getCreateAction(int actionId) {
JSONObject js = new JSONObject();
@ -73,7 +73,7 @@ public class TaskList extends Node {
return js;
}
// 更新一个包含了一定数据的JSONObject实体
public JSONObject getUpdateAction(int actionId) {
JSONObject js = new JSONObject();
@ -215,11 +215,11 @@ public class TaskList extends Node {
return SYNC_ACTION_ERROR;
}
// 获得TaskList的大小
public int getChildTaskCount() {
return mChildren.size();
}
// 在当前任务表末尾添加新的任务
public boolean addChildTask(Task task) {
boolean ret = false;
if (task != null && !mChildren.contains(task)) {
@ -233,7 +233,7 @@ public class TaskList extends Node {
}
return ret;
}
// 在当前任务表的指定位置添加新的任务
public boolean addChildTask(Task task, int index) {
if (index < 0 || index > mChildren.size()) {
Log.e(TAG, "add child task: invalid index");
@ -259,7 +259,7 @@ public class TaskList extends Node {
return true;
}
// 删除TaskList中的一个Task
public boolean removeChildTask(Task task) {
boolean ret = false;
int index = mChildren.indexOf(task);
@ -280,7 +280,7 @@ public class TaskList extends Node {
}
return ret;
}
// 移动TaskList中的一个Task
public boolean moveChildTask(Task task, int index) {
if (index < 0 || index >= mChildren.size()) {
@ -298,7 +298,7 @@ public class TaskList extends Node {
return true;
return (removeChildTask(task) && addChildTask(task, index));
}
// 按gid寻找Task
public Task findChildTaskByGid(String gid) {
for (int i = 0; i < mChildren.size(); i++) {
Task t = mChildren.get(i);
@ -308,11 +308,11 @@ public class TaskList extends Node {
}
return null;
}
// 返回指定Task的index
public int getChildTaskIndex(Task task) {
return mChildren.indexOf(task);
}
// 返回指定index的Task
public Task getChildTaskByIndex(int index) {
if (index < 0 || index >= mChildren.size()) {
Log.e(TAG, "getTaskByIndex: invalid index");
@ -320,7 +320,7 @@ public class TaskList extends Node {
}
return mChildren.get(index);
}
// 返回指定gid的task
public Task getChilTaskByGid(String gid) {
for (Task task : mChildren) {
if (task.getGid().equals(gid))

@ -14,15 +14,17 @@
* limitations under the License.
*/
// 小米便签运行过程中的运行异常处理
package net.micode.notes.gtask.exception;
public class ActionFailureException extends RuntimeException {
// 版本控制用,反序列化版本升级时仍保持对象唯一性
private static final long serialVersionUID = 4425249765923293627L;
// 通过super()来引用父类成分
public ActionFailureException() {
super();
}
public ActionFailureException(String paramString) {
super(paramString);
}

@ -14,11 +14,14 @@
* limitations under the License.
*/
// 运行过程中的网络异常处理
package net.micode.notes.gtask.exception;
public class NetworkFailureException extends Exception {
// 版本控制用,反序列化版本升级时仍保持对象唯一性
private static final long serialVersionUID = 2107610287180234136L;
// 通过super()来引用父类成分
public NetworkFailureException() {
super();
}

@ -15,6 +15,14 @@
* limitations under the License.
*/
/*
*Gtask
*
*private void showNotification(int tickerId, String content)
*protected Integer doInBackground(Void... unused) 线
*protected void onProgressUpdate(String... progress) 线
*protected void onPostExecute(Integer result) UI
*/
package net.micode.notes.gtask.remote;
import android.app.Notification;
@ -44,7 +52,7 @@ public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
private GTaskManager mTaskManager;
private OnCompleteListener mOnCompleteListener;
// GTask的一个同步进程类包含上下文信息事件完成进度监听方法通知管理方法和进程管理方法
public GTaskASyncTask(Context context, OnCompleteListener listener) {
mContext = context;
mOnCompleteListener = listener;
@ -52,56 +60,70 @@ public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
.getSystemService(Context.NOTIFICATION_SERVICE);
mTaskManager = GTaskManager.getInstance();
}
//取消同步
public void cancelSync() {
mTaskManager.cancelSync();
}
// 发布并更新事件处理进程
public void publishProgess(String message) {
publishProgress(new String[] {
message
});
}
// 该方法用于为用户同步当前事件状态
private void showNotification(int tickerId, String content) {
Notification notification = new Notification(R.drawable.notification, mContext
.getString(tickerId), System.currentTimeMillis());
// 调用系统灯光
notification.defaults = Notification.DEFAULT_LIGHTS;
// 操作后清除通知栏信息
notification.flags = Notification.FLAG_AUTO_CANCEL;
// 事件挂起意向
PendingIntent pendingIntent;
// 同步失败挂起活动信息
if (tickerId != R.string.ticker_success) {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
NotesPreferenceActivity.class), 0);
// 同步成功显示活动信息列表
} else {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
NotesListActivity.class), 0);
}
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
pendingIntent);
// 该方法疑似为设置显示最新事件信息,但运行时编译器报错,暂时注释以规避报错选项
/*notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
pendingIntent);*/
//
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
}
@Override
protected Integer doInBackground(Void... unused) {
//利用getString将NotesPreferenceActivity.getSyncAccountName(mContext)的字符串内容
//传进sync_progress_login中
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
.getSyncAccountName(mContext)));
//返回后台同步的具体操作
return mTaskManager.sync(mContext, this);
}
@Override
protected void onProgressUpdate(String... progress) {
showNotification(R.string.ticker_syncing, progress[0]);
//判断mContext是否为GTaskSyncService的实例
if (mContext instanceof GTaskSyncService) {
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
}
}
@Override
//后台运行完后更新ui显示更新后结果
protected void onPostExecute(Integer result) {
if (result == GTaskManager.STATE_SUCCESS) {
showNotification(R.string.ticker_success, mContext.getString(
R.string.success_sync_account, mTaskManager.getSyncAccount()));
//更新修改时间
NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis());
} else if (result == GTaskManager.STATE_NETWORK_ERROR) {
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network));
@ -111,9 +133,11 @@ public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
showNotification(R.string.ticker_cancel, mContext
.getString(R.string.error_sync_cancelled));
}
//不同状态下的结果显示
if (mOnCompleteListener != null) {
//新增可运行线程
new Thread(new Runnable() {
//线程运行,初始化操作
public void run() {
mOnCompleteListener.onComplete();
}

@ -60,10 +60,13 @@ import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
/*
*GTaskGTask
*accountManager JSONObject HttpParams authToken Gid
*/
public class GTaskClient {
private static final String TAG = GTaskClient.class.getSimpleName();
//指定登录URL地址
private static final String GTASK_URL = "https://mail.google.com/tasks/";
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
@ -89,7 +92,7 @@ public class GTaskClient {
private Account mAccount;
private JSONArray mUpdateArray;
// 创建GTask构造方法
private GTaskClient() {
mHttpClient = null;
mGetUrl = GTASK_GET_URL;
@ -101,35 +104,38 @@ public class GTaskClient {
mAccount = null;
mUpdateArray = null;
}
// 实例化并锁定GTaskClient使用getInstance()返回mInstance
public static synchronized GTaskClient getInstance() {
if (mInstance == null) {
mInstance = new GTaskClient();
}
return mInstance;
}
// 用于实现登录操作的布尔方法,提供了账号URL密码登录和谷歌官方邮箱URL登录两种登陆方法
public boolean login(Activity activity) {
// we suppose that the cookie would expire after 5 minutes
// then we need to re-login
// 五分钟后需要重新登录
final long interval = 1000 * 60 * 5;
if (mLastLoginTime + interval < System.currentTimeMillis()) {
mLoggedin = false;
}
// need to re-login after account switch
// 切换账号后需要重新登录
if (mLoggedin
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
.getSyncAccountName(activity))) {
mLoggedin = false;
}
// 检测登录状态
if (mLoggedin) {
Log.d(TAG, "already logged in");
return true;
}
// 获取上一次登陆时间
mLastLoginTime = System.currentTimeMillis();
// 返回的Token为空则登陆失败
String authToken = loginGoogleAccount(activity, false);
if (authToken == null) {
Log.e(TAG, "login google account failed");
@ -137,6 +143,7 @@ public class GTaskClient {
}
// login with custom domain if necessary
// 必要时使用本地输入谷歌邮箱登录
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
.endsWith("googlemail.com"))) {
StringBuilder url = new StringBuilder(GTASK_URL).append("a/");
@ -152,6 +159,7 @@ public class GTaskClient {
}
// try to login with google official url
// 调取谷歌邮箱官方URL登录
if (!mLoggedin) {
mGetUrl = GTASK_GET_URL;
mPostUrl = GTASK_POST_URL;
@ -163,25 +171,30 @@ public class GTaskClient {
mLoggedin = true;
return true;
}
// 实现谷歌登录的方法以AccountManager管理账号Token登录
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
// 登录Token
String authToken;
// 账号管理,提供账号注册接口
AccountManager accountManager = AccountManager.get(activity);
// 获取以com.google结尾的账号列表
Account[] accounts = accountManager.getAccountsByType("com.google");
// 如果账号长度为空则返回无可用账号
if (accounts.length == 0) {
Log.e(TAG, "there is no available google account");
return null;
}
// 获取账户名称
String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
Account account = null;
// 遍历返回的账户信息,寻找记录的账户信息
for (Account a : accounts) {
if (a.name.equals(accountName)) {
account = a;
break;
}
}
if (account != null) {
mAccount = account;
} else {
@ -190,11 +203,13 @@ public class GTaskClient {
}
// get the token now
// 取得登录Token
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
"goanna_mobile", null, activity, null, null);
try {
Bundle authTokenBundle = accountManagerFuture.getResult();
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
// 若Token无效则通过invalidateAuthToken方法废除该Token
if (invalidateToken) {
accountManager.invalidateAuthToken("com.google", authToken);
loginGoogleAccount(activity, false);
@ -206,11 +221,12 @@ public class GTaskClient {
return authToken;
}
// 尝试登录的布尔方法需要预先判断Token是否有效
private boolean tryToLoginGtask(Activity activity, String authToken) {
if (!loginGtask(authToken)) {
// maybe the auth token is out of date, now let's invalidate the
// token and try again
// 若Token则废弃Token且重试
authToken = loginGoogleAccount(activity, true);
if (authToken == null) {
Log.e(TAG, "login google account failed");
@ -224,26 +240,35 @@ public class GTaskClient {
}
return true;
}
// 登录GTask具体操作
private boolean loginGtask(String authToken) {
int timeoutConnection = 10000;
// socket为一种通信数据交换的端口
int timeoutSocket = 15000;
// 实现一个新的HTTP参数类
HttpParams httpParameters = new BasicHttpParams();
// 设置链接超时时间
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// 设置设置端口超时时间
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
mHttpClient = new DefaultHttpClient(httpParameters);
// 存储新的本地cookie
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
mHttpClient.setCookieStore(localBasicCookieStore);
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
// login gtask
// 登录GTask具体操作
try {
// 设置登录URL
String loginUrl = mGetUrl + "?auth=" + authToken;
// 通过已实例化URL网页中的资源查找
HttpGet httpGet = new HttpGet(loginUrl);
HttpResponse response = null;
response = mHttpClient.execute(httpGet);
// get the cookie now
// 遍历已存储的cookie以验证是否与登录cookie相符若相符则匹配
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
boolean hasAuthCookie = false;
for (Cookie cookie : cookies) {
@ -256,6 +281,7 @@ public class GTaskClient {
}
// get the client version
// 以脚本获取返回的Content中GTask_Url的内容
String resString = getResponseContent(response.getEntity());
String jsBegin = "_setup(";
String jsEnd = ")}</script>";
@ -273,40 +299,47 @@ public class GTaskClient {
return false;
} catch (Exception e) {
// simply catch all exceptions
// 仅捕获到异常则返回捕获失败
Log.e(TAG, "httpget gtask_url failed");
return false;
}
return true;
}
// 获取行为ID
private int getActionId() {
return mActionId++;
}
// 实例化创建用于网络传输的对向使用HTTPpost类来创建并返回一个空的的对向
private HttpPost createHttpPost() {
HttpPost httpPost = new HttpPost(mPostUrl);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
httpPost.setHeader("AT", "1");
return httpPost;
}
// 获取响应的资源目录
private String getResponseContent(HttpEntity entity) throws IOException {
String contentEncoding = null;
// 荣国URL获得HttpEntity对象若返回值不为空则创建数据流获取返回对象
if (entity.getContentEncoding() != null) {
contentEncoding = entity.getContentEncoding().getValue();
Log.d(TAG, "encoding: " + contentEncoding);
}
InputStream input = entity.getContent();
// Gzip为使用DEFLATE压缩数据的另一个压缩库
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
input = new GZIPInputStream(entity.getContent());
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
}
// Deflate为一个无专利的无损数据压缩算法
else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
Inflater inflater = new Inflater(true);
input = new InflaterInputStream(entity.getContent(), inflater);
}
try {
// 包装类,用于提高读取的运行效率
InputStreamReader isr = new InputStreamReader(input);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
@ -322,7 +355,10 @@ public class GTaskClient {
input.close();
}
}
/*
* jsonjsonjsontask
* jspostpostrequesttask_setGidtaskid
*/
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
if (!mLoggedin) {
Log.e(TAG, "please login first");
@ -360,6 +396,8 @@ public class GTaskClient {
}
}
// 创建单个任务传入task类的对象、使用json获取task中的内容并创建相应的jspost利用postRequest获得任务返回信
// 息并且使用task.setGid设置task的new_id
public void createTask(Task task) throws NetworkFailureException {
commitUpdate();
try {
@ -385,7 +423,7 @@ public class GTaskClient {
throw new ActionFailureException("create task: handing jsonobject failed");
}
}
// 创建任务列表设置tasklist_Gid
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
commitUpdate();
try {
@ -411,7 +449,8 @@ public class GTaskClient {
throw new ActionFailureException("create tasklist: handing jsonobject failed");
}
}
// 提交更新使用JSONobject进行存储使用jsPost.putPut的信息包括UpdateArray和ClientVersion使用
// postRequest发送这个jspost,进行处理
public void commitUpdate() throws NetworkFailureException {
if (mUpdateArray != null) {
try {
@ -432,7 +471,7 @@ public class GTaskClient {
}
}
}
// 添加更新事项,通过调用 commitUpdate()实现
public void addUpdateNode(Node node) throws NetworkFailureException {
if (node != null) {
// too many update items may result in an error
@ -446,7 +485,9 @@ public class GTaskClient {
mUpdateArray.put(node.getUpdateAction(getActionId()));
}
}
// 移动task至不同的任务列表中去通过getgid获取所属不同任务列表的gid通过
// JSONObject.put(String name, Object value)函数设置移动后的task的相关属性值从而达到移动的目的
// 最后通过postRequest进行更新后的任务列表的发送
public void moveTask(Task task, TaskList preParent, TaskList curParent)
throws NetworkFailureException {
commitUpdate();
@ -463,6 +504,7 @@ public class GTaskClient {
if (preParent == curParent && task.getPriorSibling() != null) {
// put prioring_sibing_id only if moving within the tasklist and
// it is not the first one
//设置优先级ID只有当移动是发生在文件中
action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
}
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());
@ -472,6 +514,7 @@ public class GTaskClient {
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
}
actionList.put(action);
//最后将ACTION_LIST加入到jsPost中
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// client_version
@ -486,6 +529,7 @@ public class GTaskClient {
}
}
//删除操作节点利用JSON删除后使用postRequest发送删除后的结果
public void deleteNode(Node node) throws NetworkFailureException {
commitUpdate();
try {
@ -494,6 +538,7 @@ public class GTaskClient {
// action_list
node.setDeleted(true);
// 这里会获取到删除操作的ID加入到actionLiast中
actionList.put(node.getUpdateAction(getActionId()));
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
@ -508,7 +553,8 @@ public class GTaskClient {
throw new ActionFailureException("delete node: handing jsonobject failed");
}
}
//获取任务列表首先通过GetURL使用getResponseContent联网获取数据然后筛选出"_setup("到)}</script>的部分,
// 并且从中获取GTASK_JSON_LISTS的内容返回
public JSONArray getTaskLists() throws NetworkFailureException {
if (!mLoggedin) {
Log.e(TAG, "please login first");
@ -521,6 +567,7 @@ public class GTaskClient {
response = mHttpClient.execute(httpGet);
// get the task list
// 获取任务列表并存储进jsString中
String resString = getResponseContent(response.getEntity());
String jsBegin = "_setup(";
String jsEnd = ")}</script>";
@ -547,6 +594,7 @@ public class GTaskClient {
}
}
// 通过传入的TASKList的gid,从网络上获取相应属于这个任务列表的任务
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
commitUpdate();
try {
@ -558,6 +606,7 @@ public class GTaskClient {
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
// 设置为传入的listGid
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
actionList.put(action);
@ -578,7 +627,7 @@ public class GTaskClient {
public Account getSyncAccount() {
return mAccount;
}
// 重装更新后的内容
public void resetUpdateArray() {
mUpdateArray = null;
}

@ -86,33 +86,44 @@ public class GTaskManager {
private HashMap<String, Long> mGidToNid;
private HashMap<Long, String> mNidToGid;
// 初始化Google任务管理器的函数
private GTaskManager() {
// 初始化同步状态为未执行
mSyncing = false;
// 全局状态标识为可执行
mCancelled = false;
// java泛型类用于创建一个用类作为参数的类
mGTaskListHashMap = new HashMap<String, TaskList>();
mGTaskHashMap = new HashMap<String, Node>();
mMetaHashMap = new HashMap<String, MetaData>();
mMetaList = null;
mLocalDeleteIdMap = new HashSet<Long>();
// 将Gid转换为Noteid通过哈希表建立映射
mGidToNid = new HashMap<String, Long>();
// 将Noteid转换为Gid通过哈希表建立映射
mNidToGid = new HashMap<Long, String>();
}
// synchronizedy语言级同步用于表述该方法可能运行于多线程环境之下
// 该方法用于初始化mInstance
// @return GTaskManager
public static synchronized GTaskManager getInstance() {
if (mInstance == null) {
mInstance = new GTaskManager();
}
return mInstance;
}
// synchronizedy语言级同步用于表述该方法可能运行于多线程环境之下
public synchronized void setActivityContext(Activity activity) {
// used for getting authtoken
mActivity = activity;
}
// 用于本地化和远端同步的方法
// @param context-----获取上下文
// @param assyncTask-----用于同步的异步操作类
// @return int
public int sync(Context context, GTaskASyncTask asyncTask) {
if (mSyncing) {
// 创建同步的日志文件
Log.d(TAG, "Sync is in progress");
return STATE_SYNC_IN_PROGRESS;
}
@ -128,11 +139,14 @@ public class GTaskManager {
mNidToGid.clear();
try {
// 创建用户机实例
GTaskClient client = GTaskClient.getInstance();
// JSON类型用于置空NULL
client.resetUpdateArray();
// login google task
if (!mCancelled) {
// 谷歌登录操作,若非则抛出登陆失败异常
if (!client.login(mActivity)) {
throw new NetworkFailureException("login google task failed");
}
@ -140,14 +154,17 @@ public class GTaskManager {
// get the task list from google
asyncTask.publishProgess(mContext.getString(R.string.sync_progress_init_list));
// 将谷歌上获取的JSONTasklist转换为本地Tasklist
initGTaskList();
// do content sync work
asyncTask.publishProgess(mContext.getString(R.string.sync_progress_syncing));
syncContent();
// 抓取网络异常并创建调试日志抛出error
} catch (NetworkFailureException e) {
Log.e(TAG, e.toString());
return STATE_NETWORK_ERROR;
// 抓取操作异常并创建调试日志抛出error
} catch (ActionFailureException e) {
Log.e(TAG, e.toString());
return STATE_INTERNAL_ERROR;
@ -167,32 +184,44 @@ public class GTaskManager {
return mCancelled ? STATE_SYNC_CANCELLED : STATE_SUCCESS;
}
// 初始化GTaskList获取Google上JSONTaskList并转换为本地TaskList
// 将获取的数据储存在mMetaListmGTaskListHashMapMGTaskHashmap中
// @exception NetworkFailureException
// @return void
private void initGTaskList() throws NetworkFailureException {
if (mCancelled)
return;
GTaskClient client = GTaskClient.getInstance();
try {
// Json对象是子元素的无序集合相当于创建一个Map对象
// bantouyan-json库对Json对象的抽象概念提供操纵json对象的各种方法
// 其格式为("key1": value1,"key2": value2...)key为字符串
// ajax请求不刷新页面因此配合js可实现局部刷新所以json常用作异步请求返回对象使用
JSONArray jsTaskLists = client.getTaskLists();
// init meta list first
// TaskList类型
mMetaList = null;
for (int i = 0; i < jsTaskLists.length(); i++) {
// JSONObject与JSONArray一个为对象一个为数组。此处取出单个JSONObject
JSONObject object = jsTaskLists.getJSONObject(i);
String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);
if (name
.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
if (name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
// MetaList为元表此处为初始化TaskList
mMetaList = new TaskList();
// 由远端JSON获取对象信息
mMetaList.setContentByRemoteJSON(object);
// load meta data
// 获取用户端的TaskList的gid
JSONArray jsMetas = client.getTaskList(gid);
for (int j = 0; j < jsMetas.length(); j++) {
object = (JSONObject) jsMetas.getJSONObject(j);
MetaData metaData = new MetaData();
metaData.setContentByRemoteJSON(object);
// 判断值是否值得存储为否则不加入mMetaList
if (metaData.isWorthSaving()) {
mMetaList.addChildTask(metaData);
if (metaData.getGid() != null) {
@ -214,18 +243,21 @@ public class GTaskManager {
// init task list
for (int i = 0; i < jsTaskLists.length(); i++) {
JSONObject object = jsTaskLists.getJSONObject(i);
// 通过GetString传入本地某标志数据的名称获取其在远端的名称
String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);
if (name.startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX)
&& !name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX
+ GTaskStringUtils.FOLDER_META)) {
// 由本地tasklist获取内容用于和远端同步
TaskList tasklist = new TaskList();
tasklist.setContentByRemoteJSON(object);
mGTaskListHashMap.put(gid, tasklist);
mGTaskHashMap.put(gid, tasklist);
// load tasks
// 加载tasks
JSONArray jsTasks = client.getTaskList(gid);
for (int j = 0; j < jsTasks.length(); j++) {
object = (JSONObject) jsTasks.getJSONObject(j);
@ -246,7 +278,8 @@ public class GTaskManager {
throw new ActionFailureException("initGTaskList: handing JSONObject failed");
}
}
// 本地内容同步操作
// @throw NetWorkFailureException
private void syncContent() throws NetworkFailureException {
int syncType;
Cursor c = null;
@ -300,6 +333,7 @@ public class GTaskManager {
gid = c.getString(SqlNote.GTASK_ID_COLUMN);
node = mGTaskHashMap.get(gid);
if (node != null) {
// 通过哈希表获取gid
mGTaskHashMap.remove(gid);
mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN));
mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);
@ -350,7 +384,7 @@ public class GTaskManager {
}
}
// 同步文件夹
private void syncFolder() throws NetworkFailureException {
Cursor c = null;
String gid;
@ -475,7 +509,7 @@ public class GTaskManager {
if (!mCancelled)
GTaskClient.getInstance().commitUpdate();
}
// 为同步类型分类
private void doContentSync(int syncType, Node node, Cursor c) throws NetworkFailureException {
if (mCancelled) {
return;
@ -521,7 +555,7 @@ public class GTaskManager {
throw new ActionFailureException("unkown sync action type");
}
}
// 本地增加新的Node
private void addLocalNode(Node node) throws NetworkFailureException {
if (mCancelled) {
return;
@ -595,7 +629,7 @@ public class GTaskManager {
// update meta
updateRemoteMeta(node.getGid(), sqlNote);
}
// 更新本地Node
private void updateLocalNode(Node node, Cursor c) throws NetworkFailureException {
if (mCancelled) {
return;
@ -618,12 +652,12 @@ public class GTaskManager {
// update meta info
updateRemoteMeta(node.getGid(), sqlNote);
}
// 添加远端node
private void addRemoteNode(Node node, Cursor c) throws NetworkFailureException {
if (mCancelled) {
return;
}
// 从本地数据库中获取内容
SqlNote sqlNote = new SqlNote(mContext, c);
Node n;
@ -637,8 +671,9 @@ public class GTaskManager {
Log.e(TAG, "cannot find task's parent tasklist");
throw new ActionFailureException("cannot add remote task");
}
// 在本地生成的GTaskList中添加子节点
mGTaskListHashMap.get(parentGid).addChildTask(task);
// 登录远端服务器创建task
GTaskClient.getInstance().createTask(task);
n = (Node) task;
@ -655,7 +690,7 @@ public class GTaskManager {
folderName += GTaskStringUtils.FOLDER_CALL_NOTE;
else
folderName += sqlNote.getSnippet();
// 迭代器通过统一接口迭代所有map元素
Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, TaskList> entry = iter.next();
@ -691,7 +726,7 @@ public class GTaskManager {
mGidToNid.put(n.getGid(), sqlNote.getId());
mNidToGid.put(sqlNote.getId(), n.getGid());
}
// 更新远端node
private void updateRemoteNode(Node node, Cursor c) throws NetworkFailureException {
if (mCancelled) {
return;
@ -709,15 +744,17 @@ public class GTaskManager {
// move task if necessary
if (sqlNote.isNoteType()) {
Task task = (Task) node;
// preParentList通过node获取的父节点列表
TaskList preParentList = task.getParent();
// curParentGid为通过光标在数据库中找到sqlNote的mParentId再通过mNidToGid由long类型转为String类型的Gid
String curParentGid = mNidToGid.get(sqlNote.getParentId());
if (curParentGid == null) {
Log.e(TAG, "cannot find task's parent tasklist");
throw new ActionFailureException("cannot update remote task");
}
// 通过HashMap找到对应Gid的TaskList
TaskList curParentList = mGTaskListHashMap.get(curParentGid);
// 判断父上一个节点是否与本节点相符,用于更新操作
if (preParentList != curParentList) {
preParentList.removeChildTask(task);
curParentList.addChildTask(task);
@ -729,7 +766,7 @@ public class GTaskManager {
sqlNote.resetLocalModified();
sqlNote.commit(true);
}
// 升级远程meta
private void updateRemoteMeta(String gid, SqlNote sqlNote) throws NetworkFailureException {
if (sqlNote != null && sqlNote.isNoteType()) {
MetaData metaData = mMetaHashMap.get(gid);
@ -745,7 +782,7 @@ public class GTaskManager {
}
}
}
// 刷新本地syncid以对应更改后的对象
private void refreshLocalSyncId() throws NetworkFailureException {
if (mCancelled) {
return;
@ -769,8 +806,10 @@ public class GTaskManager {
Node node = mGTaskHashMap.get(gid);
if (node != null) {
mGTaskHashMap.remove(gid);
// 在ContentValues中创建键值对。准备通过contentResolver写入数据
ContentValues values = new ContentValues();
values.put(NoteColumns.SYNC_ID, node.getLastModified());
// 进行批量更改选择参数为NULL应该可以用insert替换参数分别为表名和需要更新的value对象。
mContentResolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI,
c.getLong(SqlNote.ID_COLUMN)), values, null, null);
} else {

@ -16,6 +16,23 @@
package net.micode.notes.gtask.remote;
/*
* Service
*
* private void startSync()
* private void cancelSync()
* public void onCreate()
* public int onStartCommand(Intent intent, int flags, int startId) serviceserviceservice
* public void onLowMemory() serviceservice
* public IBinder onBind()
* public void sendBroadcast(String msg)
* public static void startSync(Activity activity)
* public static void cancelSync(Context context)
* public static boolean isSyncing()
* public static String getProgressString()
*/
import android.app.Activity;
import android.app.Service;
import android.content.Context;
@ -41,7 +58,7 @@ public class GTaskSyncService extends Service {
private static GTaskASyncTask mSyncTask = null;
private static String mSyncProgress = "";
// 开始进行一个同的步
private void startSync() {
if (mSyncTask == null) {
mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() {
@ -52,6 +69,7 @@ public class GTaskSyncService extends Service {
}
});
sendBroadcast("");
// 该函数主打的是一个让任务以单线程队列或者线程池队列方式运行
mSyncTask.execute();
}
}
@ -63,6 +81,7 @@ public class GTaskSyncService extends Service {
}
@Override
// 对Service进行一个初始化
public void onCreate() {
mSyncTask = null;
}
@ -71,6 +90,7 @@ public class GTaskSyncService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getExtras();
if (bundle != null && bundle.containsKey(ACTION_STRING_NAME)) {
// 对于开始同步和取消同步分情况表示
switch (bundle.getInt(ACTION_STRING_NAME, ACTION_INVALID)) {
case ACTION_START_SYNC:
startSync();
@ -92,26 +112,28 @@ public class GTaskSyncService extends Service {
mSyncTask.cancelSync();
}
}
// 重制客户端的服务绑定
public IBinder onBind(Intent intent) {
return null;
}
public void sendBroadcast(String msg) {
mSyncProgress = msg;
// 创建新的新意图
Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME);
intent.putExtra(GTASK_SERVICE_BROADCAST_IS_SYNCING, mSyncTask != null);
intent.putExtra(GTASK_SERVICE_BROADCAST_PROGRESS_MSG, msg);
sendBroadcast(intent);
}
// 对同步进行一个开始
public static void startSync(Activity activity) {
GTaskManager.getInstance().setActivityContext(activity);
Intent intent = new Intent(activity, GTaskSyncService.class);
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC);
// 发送通知
activity.startService(intent);
}
// 对同步进行一个取消
public static void cancelSync(Context context) {
Intent intent = new Intent(context, GTaskSyncService.class);
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC);
@ -125,4 +147,4 @@ public class GTaskSyncService extends Service {
public static String getProgressString() {
return mSyncProgress;
}
}
}

@ -15,6 +15,7 @@
*/
package net.micode.notes.model;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentUris;
@ -33,88 +34,90 @@ import net.micode.notes.data.Notes.TextNote;
import java.util.ArrayList;
public class Note {// 便签类
private ContentValues mNoteDiffValues; // 用于保存便签修改值
private NoteData mNoteData; // 保存便签的数据
private static final String TAG = "Note"; // 设置TAG为Note
public class Note {
private ContentValues mNoteDiffValues;
private NoteData mNoteData;
private static final String TAG = "Note";
/**
* Create a new note id for adding a new note to databases
*/
public static synchronized long getNewNoteId(Context context, long folderId) {
public static synchronized long getNewNoteId(Context context, long folderId) { // 为一个新的便签创建id返回数据库
// Create a new note in the database
ContentValues values = new ContentValues();
long createdTime = System.currentTimeMillis();
ContentValues values = new ContentValues(); // 用于保存创建日期等字段的值
long createdTime = System.currentTimeMillis(); // 获取创建时间
values.put(NoteColumns.CREATED_DATE, createdTime);
values.put(NoteColumns.MODIFIED_DATE, createdTime);
values.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
values.put(NoteColumns.LOCAL_MODIFIED, 1);
values.put(NoteColumns.PARENT_ID, folderId);
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values);
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); // 将values插入数据库中并返回uri
long noteId = 0;
try {
noteId = Long.valueOf(uri.getPathSegments().get(1));
noteId = Long.valueOf(uri.getPathSegments().get(1)); // 解析uri获取新笔记id
} catch (NumberFormatException e) {
Log.e(TAG, "Get note id error :" + e.toString());
Log.e(TAG, "Get note id error :" + e.toString()); // 解析失败日志
noteId = 0;
}
if (noteId == -1) {
throw new IllegalStateException("Wrong note id:" + noteId);
throw new IllegalStateException("Wrong note id:" + noteId); // 获取id出错抛出异常
}
return noteId;
}
public Note() {
mNoteDiffValues = new ContentValues();
mNoteData = new NoteData();
mNoteDiffValues = new ContentValues(); // 创建新对象
mNoteData = new NoteData(); // 创建新对象
}
public void setNoteValue(String key, String value) {
mNoteDiffValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
public void setNoteValue(String key, String value) { // 设置便签的值
mNoteDiffValues.put(key, value); // key为属性values为新值
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 本地修改标识为1标识被修改
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 表示修改时间,用于服务器时间比较
}
public void setTextData(String key, String value) {
mNoteData.setTextData(key, value);
}
mNoteData.setTextData(key, value); // 添加键值对
} // 设置文本数据
public void setTextDataId(long id) {
mNoteData.setTextDataId(id);
}
} // 设置文本数据id
public long getTextDataId() {
return mNoteData.mTextDataId;
}
} // 获取文本数据id
public void setCallDataId(long id) {
mNoteData.setCallDataId(id);
}
} // 设置通讯数据id
public void setCallData(String key, String value) {
mNoteData.setCallData(key, value);
}
} // 设置通讯数据,添加键值对
public boolean isLocalModified() {
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified();
}
} // 检查是否被修改
public boolean syncNote(Context context, long noteId) {
public boolean syncNote(Context context, long noteId) { // 用于同步到数据库中
if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId);
throw new IllegalArgumentException("Wrong note id:" + noteId); // 抛出异常
}
if (!isLocalModified()) {
return true;
if (!isLocalModified()) { // 判断是否修改过
return true; // 没修改则不需要同步
}
/**
* In theory, once data changed, the note should be updated on {@link NoteColumns#LOCAL_MODIFIED} and
* {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails, we also update the
* In theory, once data changed, the note should be updated on
* {@link NoteColumns#LOCAL_MODIFIED} and
* {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails,
* we also update the
* note data info
*/
if (context.getContentResolver().update(
if (context.getContentResolver().update( // 打包并同步
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
null) == 0) {
Log.e(TAG, "Update note error, should not happen");
@ -124,13 +127,13 @@ public class Note {
if (mNoteData.isLocalModified()
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
return false;
return false; // 表示同步失败
}
return true;
}
private class NoteData {
private class NoteData { // 私有内部类,用于便签数据管理
private long mTextDataId;
private ContentValues mTextDataValues;
@ -141,77 +144,76 @@ public class Note {
private static final String TAG = "NoteData";
public NoteData() {
mTextDataValues = new ContentValues();
mCallDataValues = new ContentValues();
public NoteData() { // 构造函数
mTextDataValues = new ContentValues(); // 保存便签文本
mCallDataValues = new ContentValues(); // 保存便签通讯记录
mTextDataId = 0;
mCallDataId = 0;
}
boolean isLocalModified() {
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
}
} // 检查便签是否再本地修改
void setTextDataId(long id) {
if(id <= 0) {
if (id <= 0) {
throw new IllegalArgumentException("Text data id should larger than 0");
}
mTextDataId = id;
}
} // 设置便签文本标识符
void setCallDataId(long id) {
if (id <= 0) {
throw new IllegalArgumentException("Call data id should larger than 0");
}
mCallDataId = id;
}
} // 设置便签通讯标识符
void setCallData(String key, String value) {
mCallDataValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
}
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 更新修改状态
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改日期
} // 设置文本数据
void setTextData(String key, String value) {
void setTextData(String key, String value) { // 设置通讯数据
mTextDataValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
}
Uri pushIntoContentResolver(Context context, long noteId) {
Uri pushIntoContentResolver(Context context, long noteId) { // 将文本数据与通讯数据添加到context
/**
* Check for safety
*/
if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId);
throw new IllegalArgumentException("Wrong note id:" + noteId); // 抛出异常
}
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
ContentProviderOperation.Builder builder = null;
if(mTextDataValues.size() > 0) {
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
if (mTextDataValues.size() > 0) { // 检查文本数据
mTextDataValues.put(DataColumns.NOTE_ID, noteId); // 将便签id与数据值相关联
if (mTextDataId == 0) {
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE);
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); // 并将键值对添加到mTextDataValues中
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
mTextDataValues);
mTextDataValues); // 插入新的数据行保存返回的uri
try {
setTextDataId(Long.valueOf(uri.getPathSegments().get(1)));
} catch (NumberFormatException e) {
Log.e(TAG, "Insert new text data fail with noteId" + noteId);
Log.e(TAG, "Insert new text data fail with noteId" + noteId); // 异常日志
mTextDataValues.clear();
return null;
}
} else {
} else { // 表示需要更新现有数据行
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
Notes.CONTENT_DATA_URI, mTextDataId));
builder.withValues(mTextDataValues);
operationList.add(builder.build());
}
mTextDataValues.clear();
mTextDataValues.clear(); // 清空mTextDataValues
}
if(mCallDataValues.size() > 0) {
if (mCallDataValues.size() > 0) { // 检查通讯数据,代码内容功能同上
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
if (mCallDataId == 0) {
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
@ -233,7 +235,7 @@ public class Note {
mCallDataValues.clear();
}
if (operationList.size() > 0) {
if (operationList.size() > 0) { // 检查operationList对载入的操作进行批量提交
try {
ContentProviderResult[] results = context.getContentResolver().applyBatch(
Notes.AUTHORITY, operationList);

@ -31,38 +31,37 @@ import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.Notes.TextNote;
import net.micode.notes.tool.ResourceParser.NoteBgResources;
public class WorkingNote {
public class WorkingNote {// 表示工作便签
// Note for the working note
private Note mNote;
private Note mNote; // 表示工作便签
// Note Id
private long mNoteId;
private long mNoteId; // 便签id
// Note content
private String mContent;
private String mContent; // 便签内容
// Note mode
private int mMode;
private int mMode; // 便签模式
private long mAlertDate;
private long mAlertDate; // 提醒日期
private long mModifiedDate;
private long mModifiedDate; // 最近修改日期
private int mBgColorId;
private int mBgColorId; // 背景颜色id
private int mWidgetId;
private int mWidgetId; // 小部件id
private int mWidgetType;
private int mWidgetType; // 小部件类型
private long mFolderId;
private long mFolderId; // 所在文件夹id
private Context mContext;
private Context mContext; // 表示上下文
private static final String TAG = "WorkingNote";
private static final String TAG = "WorkingNote"; // 设置TAG
private boolean mIsDeleted;
private boolean mIsDeleted; // 表示是否被删除
private NoteSettingChangedListener mNoteSettingStatusListener;
private NoteSettingChangedListener mNoteSettingStatusListener; // 表示便签设置状态
public static final String[] DATA_PROJECTION = new String[] {
public static final String[] DATA_PROJECTION = new String[] { // 用于指定需要从数据库查询的数据列
DataColumns.ID,
DataColumns.CONTENT,
DataColumns.MIME_TYPE,
@ -81,28 +80,28 @@ public class WorkingNote {
NoteColumns.MODIFIED_DATE
};
private static final int DATA_ID_COLUMN = 0;
private static final int DATA_ID_COLUMN = 0; // 表示查询数据的id列
private static final int DATA_CONTENT_COLUMN = 1;
private static final int DATA_CONTENT_COLUMN = 1; // 表示查询数据的内容列
private static final int DATA_MIME_TYPE_COLUMN = 2;
private static final int DATA_MIME_TYPE_COLUMN = 2; // 表示查询数据的MIME类型列
private static final int DATA_MODE_COLUMN = 3;
private static final int DATA_MODE_COLUMN = 3; // 表示查询数据的模式列
private static final int NOTE_PARENT_ID_COLUMN = 0;
private static final int NOTE_PARENT_ID_COLUMN = 0; // 表示查询便签的父文件夹id列
private static final int NOTE_ALERTED_DATE_COLUMN = 1;
private static final int NOTE_ALERTED_DATE_COLUMN = 1; // 表示便签的提醒日期列
private static final int NOTE_BG_COLOR_ID_COLUMN = 2;
private static final int NOTE_BG_COLOR_ID_COLUMN = 2; // 表示便签的背景颜色id列
private static final int NOTE_WIDGET_ID_COLUMN = 3;
private static final int NOTE_WIDGET_ID_COLUMN = 3; // 表示便签小组件id列
private static final int NOTE_WIDGET_TYPE_COLUMN = 4;
private static final int NOTE_WIDGET_TYPE_COLUMN = 4; // 表示便签小组件类型列
private static final int NOTE_MODIFIED_DATE_COLUMN = 5;
private static final int NOTE_MODIFIED_DATE_COLUMN = 5; // 表示便签最后修改日期列
// New note construct
private WorkingNote(Context context, long folderId) {
private WorkingNote(Context context, long folderId) { // 新建便签的结构
mContext = context;
mAlertDate = 0;
mModifiedDate = System.currentTimeMillis();
@ -115,7 +114,7 @@ public class WorkingNote {
}
// Existing note construct
private WorkingNote(Context context, long noteId, long folderId) {
private WorkingNote(Context context, long noteId, long folderId) { // 存在的的便签结构
mContext = context;
mNoteId = noteId;
mFolderId = folderId;
@ -124,13 +123,13 @@ public class WorkingNote {
loadNote();
}
private void loadNote() {
private void loadNote() { // 查询并加载当前便签的详细信息
Cursor cursor = mContext.getContentResolver().query(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null,
null, null);
null, null); // 返回一个Cursor对象
if (cursor != null) {
if (cursor.moveToFirst()) {
if (cursor.moveToFirst()) { // 将查询结构的列值返回当前便签
mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN);
mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN);
mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN);
@ -141,70 +140,70 @@ public class WorkingNote {
cursor.close();
} else {
Log.e(TAG, "No note with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note with id " + mNoteId);
throw new IllegalArgumentException("Unable to find note with id " + mNoteId); // 抛出异常表示未找到指定id的便签
}
loadNoteData();
loadNoteData(); // 加载便签内容和其他数据
}
private void loadNoteData() {
private void loadNoteData() { // 加载便签内容数据
Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION,
DataColumns.NOTE_ID + "=?", new String[] {
String.valueOf(mNoteId)
}, null);
DataColumns.NOTE_ID + "=?", new String[] { // 获取与便签id相对应的所有数据
String.valueOf(mNoteId)
}, null); // 遍历Cursor对象所有数据
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String type = cursor.getString(DATA_MIME_TYPE_COLUMN);
if (DataConstants.NOTE.equals(type)) {
if (DataConstants.NOTE.equals(type)) { // 类型相对应则设置WorkingNote对象成员变量
mContent = cursor.getString(DATA_CONTENT_COLUMN);
mMode = cursor.getInt(DATA_MODE_COLUMN);
mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN));
} else if (DataConstants.CALL_NOTE.equals(type)) {
mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN));
mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); // 设置为Notes对象的成员变量
} else {
Log.d(TAG, "Wrong note type with type:" + type);
Log.d(TAG, "Wrong note type with type:" + type); // 抛出异常,错误的数据类型
}
} while (cursor.moveToNext());
}
cursor.close();
} else {
Log.e(TAG, "No data with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId);
throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId); // 抛出异常,无查询结果
}
}
public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId,
int widgetType, int defaultBgColorId) {
WorkingNote note = new WorkingNote(context, folderId);
public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId, // 上下文对象、文件夹id、组件id、组件类型、默认背景颜色id
int widgetType, int defaultBgColorId) { // 创建空白便签
WorkingNote note = new WorkingNote(context, folderId); // 用上下文对象、父文件夹id创建WorkingNote
note.setBgColorId(defaultBgColorId);
note.setWidgetId(widgetId);
note.setWidgetType(widgetType);
return note;
return note; // 设置变量并返回
}
public static WorkingNote load(Context context, long id) {
public static WorkingNote load(Context context, long id) { // 用上下文对象和id创建新的WorkingNote对象
return new WorkingNote(context, id, 0);
}
public synchronized boolean saveNote() {
if (isWorthSaving()) {
if (!existInDatabase()) {
if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) {
Log.e(TAG, "Create new note fail with id:" + mNoteId);
public synchronized boolean saveNote() { // 保存便签
if (isWorthSaving()) { // 判断是否保存
if (!existInDatabase()) { // 是否存在与数据库
if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { // 不存在则获取id
Log.e(TAG, "Create new note fail with id:" + mNoteId); // 日志记录
return false;
}
}
mNote.syncNote(mContext, mNoteId);
mNote.syncNote(mContext, mNoteId); // 同步到数据库中
/**
* Update widget content if there exist any widget of this note
*/
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID // 诺该便签与组件关联
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE
&& mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged();
mNoteSettingStatusListener.onWidgetChanged(); // 更新组件
}
return true;
} else {
@ -214,155 +213,156 @@ public class WorkingNote {
public boolean existInDatabase() {
return mNoteId > 0;
}
} // 判断便签是否存在数据库
private boolean isWorthSaving() {
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent))
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) // 不存在于数据库且为空,或者没本地修改
|| (existInDatabase() && !mNote.isLocalModified())) {
return false;
} else {
return true;
}
}
} // 判断是否值得保存
public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) {
mNoteSettingStatusListener = l;
}
} // 设置便签状态监视器
public void setAlertDate(long date, boolean set) {
if (date != mAlertDate) {
mAlertDate = date;
mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate));
public void setAlertDate(long date, boolean set) { // 设置提醒
if (date != mAlertDate) { // 判断传入是否相同
mAlertDate = date; // 更新
mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); // 更新数据库
}
if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onClockAlertChanged(date, set);
mNoteSettingStatusListener.onClockAlertChanged(date, set); // 调用监听器
}
}
public void markDeleted(boolean mark) {
public void markDeleted(boolean mark) { // 标记删除标志
mIsDeleted = mark;
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged();
mNoteSettingStatusListener.onWidgetChanged(); // 已删除则更新组件
}
}
public void setBgColorId(int id) {
if (id != mBgColorId) {
mBgColorId = id;
public void setBgColorId(int id) { // 设置背景颜色
if (id != mBgColorId) { // 检查
mBgColorId = id; // 更新设置
if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onBackgroundColorChanged();
mNoteSettingStatusListener.onBackgroundColorChanged(); // 通知监听器
}
mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id));
mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id)); // 更新数据库
}
}
public void setCheckListMode(int mode) {
if (mMode != mode) {
public void setCheckListMode(int mode) { // 设置便签的检查清单模式
if (mMode != mode) { // 检查
if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode);
mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); // 通知
}
mMode = mode;
mNote.setTextData(TextNote.MODE, String.valueOf(mMode));
mNote.setTextData(TextNote.MODE, String.valueOf(mMode)); // 更新数据库
}
}
public void setWidgetType(int type) {
public void setWidgetType(int type) { // 设置组件类型
if (type != mWidgetType) {
mWidgetType = type;
mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType));
mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType)); // 更新数据库
}
}
public void setWidgetId(int id) {
public void setWidgetId(int id) { // 设置组件id
if (id != mWidgetId) {
mWidgetId = id;
mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId));
mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); // 更新数据库
}
}
public void setWorkingText(String text) {
public void setWorkingText(String text) { // 设置文本
if (!TextUtils.equals(mContent, text)) {
mContent = text;
mNote.setTextData(DataColumns.CONTENT, mContent);
mNote.setTextData(DataColumns.CONTENT, mContent); // 更新
}
}
public void convertToCallNote(String phoneNumber, long callDate) {
public void convertToCallNote(String phoneNumber, long callDate) { // 转换为呼叫类型便签
mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate));
mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber);
mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER));
mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER)); // 修改父文件夹id为呼叫记录id
}
public boolean hasClockAlert() {
return (mAlertDate > 0 ? true : false);
}
} // 是否设置提醒数据
public String getContent() {
return mContent;
}
} // 获取内容
public long getAlertDate() {
return mAlertDate;
}
} // 获取提醒时间
public long getModifiedDate() {
return mModifiedDate;
}
} // 获取最后修改时间
public int getBgColorResId() {
return NoteBgResources.getNoteBgResource(mBgColorId);
}
} // 获取背景资源id
public int getBgColorId() {
return mBgColorId;
}
} // 获取背景颜色id
public int getTitleBgResId() {
return NoteBgResources.getNoteTitleBgResource(mBgColorId);
}
} // 获取标题栏背景颜色资源id
public int getCheckListMode() {
return mMode;
}
} // 获取检查清单模式
public long getNoteId() {
return mNoteId;
}
} // 获取便签id
public long getFolderId() {
return mFolderId;
}
} // 获取父文件夹id
public int getWidgetId() {
return mWidgetId;
}
} // 获取组件id
public int getWidgetType() {
return mWidgetType;
}
} // 获取组件类型
public interface NoteSettingChangedListener {
public interface NoteSettingChangedListener { // 接口
/**
* Called when the background color of current note has just changed
*/
void onBackgroundColorChanged();
void onBackgroundColorChanged(); // 便签背景颜色变化时启动
/**
* Called when user set clock
*/
void onClockAlertChanged(long date, boolean set);
void onClockAlertChanged(long date, boolean set); // 设置提醒时间时启动
/**
* Call when user create note from widget
*/
void onWidgetChanged();
void onWidgetChanged(); // 创建组件时启动
/**
* Call when switch between check list mode and normal mode
*
* @param oldMode is previous mode before change
* @param newMode is new mode
*/
void onCheckListModeChanged(int oldMode, int newMode);
void onCheckListModeChanged(int oldMode, int newMode); // 清单模式转为笔记模式时启动
}
}

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

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

@ -18,96 +18,96 @@ package net.micode.notes.tool;
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";
//定义字符串常量在类的外部直接访问表示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";
public final static String GTASK_JSON_ENTITY_TYPE = "entity_type";//定义字符串常量在类的外部直接访问表示entity_type的字符串键名
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";
public final static String GTASK_JSON_INDEX = "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";
//定义字符串常量在类的外部直接访问表示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";
public final static String GTASK_JSON_PARENT_ID = "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_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.ui.NotesPreferenceActivity;
public class ResourceParser {
public static final int YELLOW = 0;
public static final int BLUE = 1;
public static final int WHITE = 2;
public class ResourceParser {
//定义常量,用于表示颜色和字体大小
public static final int YELLOW = 0;//黄色
public static final int BLUE = 1;//蓝
public static final int WHITE = 2;//bai
public static final int GREEN = 3;
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_MEDIUM = 1;
public static final int TEXT_LARGE = 2;
public static final int TEXT_SUPER = 3;
public static final int TEXT_SMALL = 0;//字体小号
public static final int TEXT_MEDIUM = 1;//中
public static final int TEXT_LARGE = 2;//da
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 {
private final static int [] BG_EDIT_RESOURCES = new int [] {
@ -46,7 +46,7 @@ public class ResourceParser {
R.drawable.edit_white,
R.drawable.edit_green,
R.drawable.edit_red
};
};//定义私有静态常量G_EDIT_RESOURCES 用于储存图片资源id
private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] {
R.drawable.edit_title_yellow,
@ -54,7 +54,7 @@ public class ResourceParser {
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];
@ -63,40 +63,40 @@ public class ResourceParser {
public static int getNoteTitleBgResource(int 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(
NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) {
return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length);
} else {
return BG_DEFAULT_COLOR;
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_FIRST_RESOURCES = new int [] {//定义final整型数组 BG_FIRST_RESOURCES
R.drawable.list_yellow_up,//黄色背景上边框图案id
R.drawable.list_blue_up,//蓝色背景上边框图案id
R.drawable.list_white_up,//白色背景边框图案id
R.drawable.list_green_up,//绿色背景边框图案id
R.drawable.list_red_up//红色背景边框图案id
};
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
R.drawable.list_yellow_middle,//黄色背景中间边框图案id
R.drawable.list_blue_middle,//蓝色背景中间边框图案id
R.drawable.list_white_middle,//白色背景中间边框图案id
R.drawable.list_green_middle,//绿色背景中间边框图案id
R.drawable.list_red_middle//红色背景中间边框图案id
};
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,
R.drawable.list_yellow_down,//黄色背景中下间边框图案id
R.drawable.list_blue_down,//蓝色背景中下间边框图案id
R.drawable.list_white_down,//白色背景中下间边框图案id
R.drawable.list_green_down,//绿色背景中下间边框图案id
R.drawable.list_red_down,//红色背景中下间边框图案id
};
private final static int [] BG_SINGLE_RESOURCES = new int [] {
@ -110,33 +110,30 @@ public class ResourceParser {
public static int getNoteBgFirstRes(int id) {
return BG_FIRST_RESOURCES[id];
}
//定义getNoteBgFirstRes方法用于返回G_FIRST_RESOURCES[id]
public static int getNoteBgLastRes(int id) {
return BG_LAST_RESOURCES[id];
}
//定义getNoteBgLastRes方法用于返回 BG_LAST_RESOURCES[id]
public static int getNoteBgSingleRes(int id) {
return BG_SINGLE_RESOURCES[id];
}
//定义getNoteBgSingleRes方法用于返回BG_SINGLE_RESOURCES[id]
public static int getNoteBgNormalRes(int id) {
return BG_NORMAL_RESOURCES[id];
}
public static int getFolderBgRes() {
return R.drawable.list_folder;
}
//定义etNoteBgNormalRes方法用于返回BG_NORMAL_RESOURCES[id]
public static int getFolderBgRes() {return R.drawable.list_folder;}//定义getFolderBgRes方法用于返回 R.drawable.list_folder
}
public static class WidgetBgResources {
public static class WidgetBgResources {//定义静态内部类WidgetBgResources
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,
};
};// 管理小部件2x2格式不同颜色资源图片
public static int getWidget2xBgResource(int id) {
return BG_2X_RESOURCES[id];
}
@ -147,20 +144,20 @@ public class ResourceParser {
R.drawable.widget_4x_white,
R.drawable.widget_4x_green,
R.drawable.widget_4x_red
};
};// 管理小部件4x4格式不同颜色资源图片
public static int getWidget4xBgResource(int id) {
return BG_4X_RESOURCES[id];
}
}
public static class TextAppearanceResources {
public static class TextAppearanceResources {//定义TextAppearanceResources 静态内部类
private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] {
R.style.TextAppearanceNormal,
R.style.TextAppearanceMedium,
R.style.TextAppearanceLarge,
R.style.TextAppearanceSuper
};
};//分别对应中等,大号,超大的文本外观样式
public static int getTexAppearanceResource(int id) {
/**

@ -37,33 +37,33 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
NoteColumns.ID,
NoteColumns.BG_COLOR_ID,
NoteColumns.SNIPPET
};
};//定义字符串数组PROJECTION包含IDB G_COLOR_ID SNIPPET三个元素
public static final int COLUMN_ID = 0;
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";
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
public void onDeleted(Context context, int[] appWidgetIds) {//创建一个ContentValues对象values存储键值对数据
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++) {
context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
values,
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) {
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION,
PROJECTION,//获取ContentResovler对象并用querty方法查询数据·
NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?",
new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) },
null);
}
}//null表示默认排序方式
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
update(context, appWidgetManager, appWidgetIds, false);
@ -71,12 +71,12 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
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) {
int bgId = ResourceParser.getDefaultBgId(context);
int bgId = ResourceParser.getDefaultBgId(context);//获取默认背景图片id并将其作为当前部件的背景
String snippet = "";
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_TYPE, getWidgetType());
@ -86,14 +86,14 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
c.close();
return;
}
}//判断数据库查询结果c是否为空若查询结果大于1则记录日志错误并返回
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);
intent.setAction(Intent.ACTION_VIEW);//将当前记录id作为extra数据存入启动internet对象中
} else {
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) {
@ -107,16 +107,16 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
* Generate the pending intent to start host for the widget
*/
PendingIntent pendingIntent = null;
if (privacyMode) {
if (privacyMode) {// 判断是否处于隐私模式
rv.setTextViewText(R.id.widget_text,
context.getString(R.string.widget_under_visit_mode));
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent(
context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);// 如果是将小部件文本设置为“访问模式下”并创建一个PendingIntent使点击小部件时跳转至NotesListActivity
} else {
rv.setTextViewText(R.id.widget_text, snippet);
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
}// 如果不是将小部件文本设置为片段内容并创建一个PendingIntent使点击小部件时跳转至NoteEditActivity
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
@ -128,5 +128,5 @@ public abstract class NoteWidgetProvider extends AppWidgetProvider {
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
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.update(context, appWidgetManager, appWidgetIds);
}
}//NoteWidgetProvider_2x是一个继承自NoteWidgetProvider的小部件提供者用于显示2x大小的便签小部件。
//调用父类的update方法更新小部件UI界面
@Override
protected int getLayoutId() {
return R.layout.widget_2x;
}
}//获取2x大小便签小部件布局资源ID由子类具体实现
@Override
protected int getBgResourceId(int bgId) {
return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId);
}
}//获取2x大小便签小部件背景资源ID由子类具体实现。
@Override
protected int getWidgetType() {
return Notes.TYPE_WIDGET_2X;
}
}//获取小部件类型由子类具体实现返回值为Notes.TYPE_WIDGET_2X
}

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

Loading…
Cancel
Save