Compare commits

..

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

@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="jbr-17" /> <option name="gradleJvm" value="jbr-17" />
<option name="resolveExternalAnnotations" value="false" />
</GradleProjectSettings> </GradleProjectSettings>
</option> </option>
</component> </component>

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectType"> <component name="ProjectType">

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/src.iml" filepath="$PROJECT_DIR$/.idea/src.iml" />
</modules>
</component>
</project>

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.data; package net.micode.notes.data;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.CommonDataKinds.Phone;
@ -22,11 +24,11 @@ import android.telephony.PhoneNumberUtils;
import android.util.Log; import android.util.Log;
import java.util.HashMap; import java.util.HashMap;
public class Contact { public class Contact {
private static HashMap<String, String> sContactCache; private static HashMap<String, String> sContactCache;
//用于缓存已经查询过的电话号码和对应的联系人姓名,目的是避免重复查询,提高性能。
private static final String TAG = "Contact"; private static final String TAG = "Contact";
//用于日志输出的标签,方便在日志中识别该类相关的日志信息
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" + ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
+ " AND " + Data.RAW_CONTACT_ID + " IN " + " AND " + Data.RAW_CONTACT_ID + " IN "
@ -38,38 +40,34 @@ public class Contact {
if(sContactCache == null) { if(sContactCache == null) {
sContactCache = new HashMap<String, String>(); sContactCache = new HashMap<String, String>();
} }
//检查缓存sContactCache是否已经初始化如果没有则进行初始化
if(sContactCache.containsKey(phoneNumber)) { if(sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber); return sContactCache.get(phoneNumber);
} }
//检查传入的电话号码是否已经在缓存中,如果在缓存中则直接返回对应的联系人姓名
String selection = CALLER_ID_SELECTION.replace("+", String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
//如果不在缓存中则构建查询条件。通过CALLER_ID_SELECTION字符串构建查询语句其中将+替换为通过PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)生成的匹配字符串
Cursor cursor = context.getContentResolver().query( Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI, Data.CONTENT_URI,
new String [] { Phone.DISPLAY_NAME }, new String [] { Phone.DISPLAY_NAME },
selection, selection,
new String[] { phoneNumber }, new String[] { phoneNumber },
null); null);
//执行查询操作,查询的结果集包含联系人姓名
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
try { try {
String name = cursor.getString(0); String name = cursor.getString(0);
sContactCache.put(phoneNumber, name); sContactCache.put(phoneNumber, name);
return name; return name;
//如果查询结果不为空且游标可以移动到第一条记录(表示找到了匹配的联系人),则从游标中获取联系人姓名,并将其放入缓存中,然后返回该姓名
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
Log.e(TAG, " Cursor get string error " + e.toString()); Log.e(TAG, " Cursor get string error " + e.toString());
return null; return null;
//如果在获取姓名过程中发生索引越界异常则在日志中记录错误信息并返回null
} finally { } finally {
cursor.close(); cursor.close();
}//无论是否发生异常都需要在finally块中关闭游标以释放资源 }
} else { } else {
Log.d(TAG, "No contact matched with number:" + phoneNumber); Log.d(TAG, "No contact matched with number:" + phoneNumber);
return null; return null;
}//如果查询结果为空,表示没有找到匹配的联系人 }
} }
} }

@ -14,277 +14,266 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.data; package net.micode.notes.data;
import android.net.Uri; import android.net.Uri;
public class Notes { public class Notes {
public static final String AUTHORITY = "micode_notes"; public static final String AUTHORITY = "micode_notes";
public static final String TAG = "Notes"; public static final String TAG = "Notes";
public static final int TYPE_NOTE = 0; public static final int TYPE_NOTE = 0;
public static final int TYPE_FOLDER = 1; public static final int TYPE_FOLDER = 1;
public static final int TYPE_SYSTEM = 2; public static final int TYPE_SYSTEM = 2;
/** /**
* Following IDs are system folders' identifiers * Following IDs are system folders' identifiers
* {@link Notes#ID_ROOT_FOLDER } is default folder * {@link Notes#ID_ROOT_FOLDER } is default folder
* {@link Notes#ID_TEMPARAY_FOLDER } is for notes belonging no folder * {@link Notes#ID_TEMPARAY_FOLDER } is for notes belonging no folder
* {@link Notes#ID_CALL_RECORD_FOLDER} is to store call records * {@link Notes#ID_CALL_RECORD_FOLDER} is to store call records
*/ */
public static final int ID_ROOT_FOLDER = 0; public static final int ID_ROOT_FOLDER = 0;
public static final int ID_TEMPARAY_FOLDER = -1; public static final int ID_TEMPARAY_FOLDER = -1;
public static final int ID_CALL_RECORD_FOLDER = -2; public static final int ID_CALL_RECORD_FOLDER = -2;
public static final int ID_TRASH_FOLER = -3; 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_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_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_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_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_FOLDER_ID = "net.micode.notes.folder_id";
public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date"; 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_INVALIDE = -1;
public static final int TYPE_WIDGET_2X = 0; public static final int TYPE_WIDGET_2X = 0;
public static final int TYPE_WIDGET_4X = 1; public static final int TYPE_WIDGET_4X = 1;
public static class DataConstants { public static class DataConstants {
public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; public static final String NOTE = TextNote.CONTENT_ITEM_TYPE;
public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE;
} }
/** /**
* Uri to query all notes and folders * Uri to query all notes and folders
*/ */
public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note"); public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note");
/** /**
* Uri to query data * Uri to query data
*/ */
public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data");
public interface NoteColumns { public interface NoteColumns {
/** /**
* The unique ID for a row * The unique ID for a row
* <P> Type: INTEGER (long) </P> * <P> Type: INTEGER (long) </P>
*/ */
public static final String ID = "_id"; public static final String ID = "_id";
//行的唯一标识
/** /**
* The parent's id for note or folder * The parent's id for note or folder
* <P> Type: INTEGER (long) </P> * <P> Type: INTEGER (long) </P>
*/ */
public static final String PARENT_ID = "parent_id"; public static final String PARENT_ID = "parent_id";
//笔记或文件夹的父级 ID
/** /**
* Created data for note or folder * Created data for note or folder
* <P> Type: INTEGER (long) </P> * <P> Type: INTEGER (long) </P>
*/ */
public static final String CREATED_DATE = "created_date"; public static final String CREATED_DATE = "created_date";
//创建日期
/** /**
* Latest modified date * Latest modified date
* <P> Type: INTEGER (long) </P> * <P> Type: INTEGER (long) </P>
*/ */
public static final String MODIFIED_DATE = "modified_date"; public static final String MODIFIED_DATE = "modified_date";
//最后修改日期
/** /**
* Alert date * Alert date
* <P> Type: INTEGER (long) </P> * <P> Type: INTEGER (long) </P>
*/ */
public static final String ALERTED_DATE = "alert_date"; public static final String ALERTED_DATE = "alert_date";
//提醒日期
/** /**
* Folder's name or text content of note * Folder's name or text content of note
* <P> Type: TEXT </P> * <P> Type: TEXT </P>
*/ */
public static final String SNIPPET = "snippet"; public static final String SNIPPET = "snippet";
//文件夹名称或者笔记的文本内容片段
/** /**
* Note's widget id * Note's widget id
* <P> Type: INTEGER (long) </P> * <P> Type: INTEGER (long) </P>
*/ */
public static final String WIDGET_ID = "widget_id"; public static final String WIDGET_ID = "widget_id";
//与笔记关联的小部件 ID
/** /**
* Note's widget type * Note's widget type
* <P> Type: INTEGER (long) </P> * <P> Type: INTEGER (long) </P>
*/ */
public static final String WIDGET_TYPE = "widget_type"; public static final String WIDGET_TYPE = "widget_type";
//与笔记关联的小部件类型
/** /**
* Note's background color's id * Note's background color's id
* <P> Type: INTEGER (long) </P> * <P> Type: INTEGER (long) </P>
*/ */
public static final String BG_COLOR_ID = "bg_color_id"; public static final String BG_COLOR_ID = "bg_color_id";
//笔记背景颜色 ID
/** /**
* For text note, it doesn't has attachment, for multi-media * For text note, it doesn't has attachment, for multi-media
* note, it has at least one attachment * note, it has at least one attachment
* <P> Type: INTEGER </P> * <P> Type: INTEGER </P>
*/ */
public static final String HAS_ATTACHMENT = "has_attachment"; public static final String HAS_ATTACHMENT = "has_attachment";
//表示笔记是否有附件
/** /**
* Folder's count of notes * Folder's count of notes
* <P> Type: INTEGER (long) </P> * <P> Type: INTEGER (long) </P>
*/ */
public static final String NOTES_COUNT = "notes_count"; public static final String NOTES_COUNT = "notes_count";
//文件夹中笔记的数量
/** /**
* The file type: folder or note * The file type: folder or note
* <P> Type: INTEGER </P> * <P> Type: INTEGER </P>
*/ */
public static final String TYPE = "type"; public static final String TYPE = "type";
//文件类型
/** /**
* The last sync id * The last sync id
* <P> Type: INTEGER (long) </P> * <P> Type: INTEGER (long) </P>
*/ */
public static final String SYNC_ID = "sync_id"; public static final String SYNC_ID = "sync_id";
//最后同步 ID
/** /**
* Sign to indicate local modified or not * Sign to indicate local modified or not
* <P> Type: INTEGER </P> * <P> Type: INTEGER </P>
*/ */
public static final String LOCAL_MODIFIED = "local_modified"; public static final String LOCAL_MODIFIED = "local_modified";
//是否本地修改
/** /**
* Original parent id before moving into temporary folder * Original parent id before moving into temporary folder
* <P> Type : INTEGER </P> * <P> Type : INTEGER </P>
*/ */
public static final String ORIGIN_PARENT_ID = "origin_parent_id"; public static final String ORIGIN_PARENT_ID = "origin_parent_id";
//在移动到临时文件夹之前的原始父 ID
/** /**
* The gtask id * The gtask id
* <P> Type : TEXT </P> * <P> Type : TEXT </P>
*/ */
public static final String GTASK_ID = "gtask_id"; public static final String GTASK_ID = "gtask_id";
//与GTASK相关的 ID
/** /**
* The version code * The version code
* <P> Type : INTEGER (long) </P> * <P> Type : INTEGER (long) </P>
*/ */
public static final String VERSION = "version"; public static final String VERSION = "version";
//版本号 }
}//这个接口定义了与笔记相关的列名和对应的数据类型,这些列用于数据库表或者内容提供者中存储笔记的相关信息
public interface DataColumns {
public interface DataColumns { /**
/** * The unique ID for a row
* The unique ID for a row * <P> Type: INTEGER (long) </P>
* <P> Type: INTEGER (long) </P> */
*/ public static final String ID = "_id";
public static final String ID = "_id";
//标识 /**
* The MIME type of the item represented by this row.
/** * <P> Type: Text </P>
* The MIME type of the item represented by this row. */
* <P> Type: Text </P> public static final String MIME_TYPE = "mime_type";
*/
public static final String MIME_TYPE = "mime_type"; /**
//数据项的 MIME 类型 * The reference id to note that this data belongs to
/** * <P> Type: INTEGER (long) </P>
* The reference id to note that this data belongs to */
* <P> Type: INTEGER (long) </P> public static final String NOTE_ID = "note_id";
*/
public static final String NOTE_ID = "note_id"; /**
//数据所属笔记的引用 ID * Created data for note or folder
/** * <P> Type: INTEGER (long) </P>
* Created data for note or folder */
* <P> Type: INTEGER (long) </P> public static final String CREATED_DATE = "created_date";
*/
public static final String CREATED_DATE = "created_date"; /**
//创建日期 * Latest modified date
* <P> Type: INTEGER (long) </P>
/** */
* Latest modified date public static final String MODIFIED_DATE = "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";
* 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> * Generic data column, the meaning is {@link #MIMETYPE} specific, used for
*/ * integer data type
public static final String DATA1 = "data1"; * <P> Type: INTEGER </P>
// */
public static final String DATA2 = "data2";
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for /**
* integer data type * Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* <P> Type: INTEGER </P> * TEXT data type
*/ * <P> Type: TEXT </P>
public static final String DATA2 = "data2"; */
// public static final String DATA3 = "data3";
/** /**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for * Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* TEXT data type * TEXT data type
* <P> Type: TEXT </P> * <P> Type: TEXT </P>
*/ */
public static final String DATA3 = "data3"; public static final String DATA4 = "data4";
//
/**
/** * Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for * TEXT data type
* TEXT data type * <P> Type: TEXT </P>
* <P> Type: TEXT </P> */
*/ public static final String DATA5 = "data5";
public static final String DATA4 = "data4"; }
//
public static final class TextNote implements DataColumns {
/** /**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for * Mode to indicate the text in check list mode or not
* TEXT data type * <P> Type: Integer 1:check list mode 0: normal mode </P>
* <P> Type: TEXT </P> */
*/ public static final String MODE = DATA1;
public static final String DATA5 = "data5";
// public static final int MODE_CHECK_LIST = 1;
}//这个接口定义了与数据相关的列名和对应的数据类型,用于存储与笔记相关的数据信息
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note";
public static final class TextNote implements DataColumns {
/** public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note";
* Mode to indicate the text in check list mode or not
* <P> Type: Integer 1:check list mode 0: normal mode </P> public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note");
*/ }
public static final String MODE = DATA1;
public static final class CallNote implements DataColumns {
public static final int MODE_CHECK_LIST = 1; /**
* Call date for this record
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note"; * <P> Type: INTEGER (long) </P>
*/
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note"; public static final String CALL_DATE = DATA1;
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); /**
}//实现了DataColumns接口用于表示文本笔记相关的数据 * Phone number for this record
* <P> Type: TEXT </P>
public static final class CallNote implements DataColumns { */
/** public static final String PHONE_NUMBER = DATA3;
* Call date for this record
* <P> Type: INTEGER (long) </P> public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note";
*/
public static final String CALL_DATE = DATA1; 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");
* 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");
}//实现了DataColumns接口用于表示通话记录笔记相关的数据
}

@ -14,358 +14,349 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.data; package net.micode.notes.data;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log; import android.util.Log;
import net.micode.notes.data.Notes.DataColumns; import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants; import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
public class NotesDatabaseHelper extends SQLiteOpenHelper { public class NotesDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "note.db"; private static final String DB_NAME = "note.db";
//数据库的名称
private static final int DB_VERSION = 4;
private static final int DB_VERSION = 4;
//数据库的版本号 public interface TABLE {
public static final String NOTE = "note";
public interface TABLE {
public static final String NOTE = "note"; public static final String DATA = "data";
}
public static final String DATA = "data";
}//存储笔记信息和相关数据的表 private static final String TAG = "NotesDatabaseHelper";
private static final String TAG = "NotesDatabaseHelper"; private static NotesDatabaseHelper mInstance;
private static NotesDatabaseHelper mInstance; private static final String CREATE_NOTE_TABLE_SQL =
"CREATE TABLE " + TABLE.NOTE + "(" +
private static final String CREATE_NOTE_TABLE_SQL = NoteColumns.ID + " INTEGER PRIMARY KEY," +
"CREATE TABLE " + TABLE.NOTE + "(" + NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.ID + " INTEGER PRIMARY KEY," + NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.HAS_ATTACHMENT + " 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)," +
NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," +
NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," + NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," +
NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," + NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," +
NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," +
NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.VERSION + " 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 =
"CREATE TABLE " + TABLE.DATA + "(" +
private static final String CREATE_DATA_TABLE_SQL = DataColumns.ID + " INTEGER PRIMARY KEY," +
"CREATE TABLE " + TABLE.DATA + "(" + DataColumns.MIME_TYPE + " TEXT NOT NULL," +
DataColumns.ID + " INTEGER PRIMARY KEY," + DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," +
DataColumns.MIME_TYPE + " TEXT NOT NULL," + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," +
NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + DataColumns.DATA1 + " INTEGER," +
DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," + DataColumns.DATA2 + " INTEGER," +
DataColumns.DATA1 + " INTEGER," + DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," +
DataColumns.DATA2 + " INTEGER," + DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," +
DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," + DataColumns.DATA5 + " 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 " +
private static final String CREATE_DATA_NOTE_ID_INDEX_SQL = TABLE.DATA + "(" + DataColumns.NOTE_ID + ");";
"CREATE INDEX IF NOT EXISTS note_id_index ON " +
TABLE.DATA + "(" + DataColumns.NOTE_ID + ");"; /**
* Increase folder's note count when move note to the folder
/** */
* Increase folder's note count when move note to the folder private static final String NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER =
*/ "CREATE TRIGGER increase_folder_count_on_update "+
private static final String NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = " AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE +
"CREATE TRIGGER increase_folder_count_on_update "+ " BEGIN " +
" AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE + " UPDATE " + TABLE.NOTE +
" BEGIN " + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +
" UPDATE " + TABLE.NOTE + " WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" + " END";
" WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +
" END"; /**
* Decrease folder's note count when move note from folder
/** */
* Decrease folder's note count when move note from folder private static final String NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER =
*/ "CREATE TRIGGER decrease_folder_count_on_update " +
private static final String NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = " AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE +
"CREATE TRIGGER decrease_folder_count_on_update " + " BEGIN " +
" AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE + " UPDATE " + TABLE.NOTE +
" BEGIN " + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" +
" UPDATE " + TABLE.NOTE + " WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" + " AND " + NoteColumns.NOTES_COUNT + ">0" + ";" +
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + " END";
" AND " + NoteColumns.NOTES_COUNT + ">0" + ";" +
" END"; /**
* Increase folder's note count when insert new note to the folder
/** */
* Increase folder's note count when insert new note to the folder private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER =
*/ "CREATE TRIGGER increase_folder_count_on_insert " +
private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER = " AFTER INSERT ON " + TABLE.NOTE +
"CREATE TRIGGER increase_folder_count_on_insert " + " BEGIN " +
" AFTER INSERT ON " + TABLE.NOTE + " UPDATE " + TABLE.NOTE +
" BEGIN " + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +
" UPDATE " + TABLE.NOTE + " WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" + " END";
" WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +
" END"; /**
* Decrease folder's note count when delete note from the folder
/** */
* Decrease folder's note count when delete note from the folder private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER =
*/ "CREATE TRIGGER decrease_folder_count_on_delete " +
private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER = " AFTER DELETE ON " + TABLE.NOTE +
"CREATE TRIGGER decrease_folder_count_on_delete " + " BEGIN " +
" AFTER DELETE ON " + TABLE.NOTE + " UPDATE " + TABLE.NOTE +
" BEGIN " + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" +
" UPDATE " + TABLE.NOTE + " WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" + " AND " + NoteColumns.NOTES_COUNT + ">0;" +
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + " END";
" AND " + NoteColumns.NOTES_COUNT + ">0;" +
" END"; /**
* Update note's content when insert data with type {@link DataConstants#NOTE}
/** */
* Update note's content when insert data with type {@link DataConstants#NOTE} private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER =
*/ "CREATE TRIGGER update_note_content_on_insert " +
private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER = " AFTER INSERT ON " + TABLE.DATA +
"CREATE TRIGGER update_note_content_on_insert " + " WHEN new." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" +
" AFTER INSERT ON " + TABLE.DATA + " BEGIN" +
" WHEN new." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + " UPDATE " + TABLE.NOTE +
" BEGIN" + " SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT +
" UPDATE " + TABLE.NOTE + " WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +
" SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT + " END";
" WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +
" END"; /**
* 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 private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER =
*/ "CREATE TRIGGER update_note_content_on_update " +
private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER = " AFTER UPDATE ON " + TABLE.DATA +
"CREATE TRIGGER update_note_content_on_update " + " WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" +
" AFTER UPDATE ON " + TABLE.DATA + " BEGIN" +
" WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + " UPDATE " + TABLE.NOTE +
" BEGIN" + " SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT +
" UPDATE " + TABLE.NOTE + " WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +
" SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT + " END";
" WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +
" END"; /**
* 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 private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER =
*/ "CREATE TRIGGER update_note_content_on_delete " +
private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER = " AFTER delete ON " + TABLE.DATA +
"CREATE TRIGGER update_note_content_on_delete " + " WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" +
" AFTER delete ON " + TABLE.DATA + " BEGIN" +
" WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + " UPDATE " + TABLE.NOTE +
" BEGIN" + " SET " + NoteColumns.SNIPPET + "=''" +
" UPDATE " + TABLE.NOTE + " WHERE " + NoteColumns.ID + "=old." + DataColumns.NOTE_ID + ";" +
" SET " + NoteColumns.SNIPPET + "=''" + " END";
" WHERE " + NoteColumns.ID + "=old." + DataColumns.NOTE_ID + ";" +
" END"; /**
* Delete datas belong to note which has been deleted
/** */
* Delete datas belong to note which has been deleted private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER =
*/ "CREATE TRIGGER delete_data_on_delete " +
private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER = " AFTER DELETE ON " + TABLE.NOTE +
"CREATE TRIGGER delete_data_on_delete " + " BEGIN" +
" AFTER DELETE ON " + TABLE.NOTE + " DELETE FROM " + TABLE.DATA +
" BEGIN" + " WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" +
" DELETE FROM " + TABLE.DATA + " END";
" WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" +
" END"; /**
* Delete notes belong to folder which has been deleted
/** */
* Delete notes belong to folder which has been deleted private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER =
*/ "CREATE TRIGGER folder_delete_notes_on_delete " +
private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER = " AFTER DELETE ON " + TABLE.NOTE +
"CREATE TRIGGER folder_delete_notes_on_delete " + " BEGIN" +
" AFTER DELETE ON " + TABLE.NOTE + " DELETE FROM " + TABLE.NOTE +
" BEGIN" + " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
" DELETE FROM " + TABLE.NOTE + " END";
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
" END"; /**
* Move notes belong to folder which has been moved to trash folder
/** */
* Move notes belong to folder which has been moved to trash folder private static final String FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER =
*/ "CREATE TRIGGER folder_move_notes_on_trash " +
private static final String FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER = " AFTER UPDATE ON " + TABLE.NOTE +
"CREATE TRIGGER folder_move_notes_on_trash " + " WHEN new." + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER +
" AFTER UPDATE ON " + TABLE.NOTE + " BEGIN" +
" WHEN new." + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER + " UPDATE " + TABLE.NOTE +
" BEGIN" + " SET " + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER +
" UPDATE " + TABLE.NOTE + " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
" SET " + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER + " END";
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
" END"; public NotesDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
public NotesDatabaseHelper(Context context) { }
super(context, DB_NAME, null, DB_VERSION);
} public void createNoteTable(SQLiteDatabase db) {
db.execSQL(CREATE_NOTE_TABLE_SQL);
public void createNoteTable(SQLiteDatabase db) { reCreateNoteTableTriggers(db);
db.execSQL(CREATE_NOTE_TABLE_SQL); createSystemFolder(db);
//执行CREATE_NOTE_TABLE_SQL语句创建表 Log.d(TAG, "note table has been created");
reCreateNoteTableTriggers(db); }
//重新创建与note表相关的触发器
createSystemFolder(db); private void reCreateNoteTableTriggers(SQLiteDatabase db) {
//创建系统文件夹相关的记录 db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update");
Log.d(TAG, "note table has been created"); db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update");
} db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_delete");
db.execSQL("DROP TRIGGER IF EXISTS delete_data_on_delete");
private void reCreateNoteTableTriggers(SQLiteDatabase db) { db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_insert");
db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update"); db.execSQL("DROP TRIGGER IF EXISTS folder_delete_notes_on_delete");
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update"); db.execSQL("DROP TRIGGER IF EXISTS folder_move_notes_on_trash");
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_delete");
db.execSQL("DROP TRIGGER IF EXISTS delete_data_on_delete"); db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER);
db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_insert"); db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER);
db.execSQL("DROP TRIGGER IF EXISTS folder_delete_notes_on_delete"); db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER);
db.execSQL("DROP TRIGGER IF EXISTS folder_move_notes_on_trash"); db.execSQL(NOTE_DELETE_DATA_ON_DELETE_TRIGGER);
//首先删除可能存在的旧触发器 db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER);
db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER); db.execSQL(FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER);
db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER); db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER);
db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER); }
db.execSQL(NOTE_DELETE_DATA_ON_DELETE_TRIGGER);
db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER); private void createSystemFolder(SQLiteDatabase db) {
db.execSQL(FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER); ContentValues values = new ContentValues();
db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER);
//按照定义的顺序执行创建触发器的 SQL 语句,这些触发器用于处理文件夹中笔记数量的增减、笔记内容的更新以及删除相关操作 /**
} * call record foler for call notes
*/
private void createSystemFolder(SQLiteDatabase db) { values.put(NoteColumns.ID, Notes.ID_CALL_RECORD_FOLDER);
ContentValues values = new ContentValues(); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
db.insert(TABLE.NOTE, null, values);
/**
* call record foler for call notes /**
*/ * root folder which is default folder
values.put(NoteColumns.ID, Notes.ID_CALL_RECORD_FOLDER); */
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); values.clear();
db.insert(TABLE.NOTE, null, values); values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
/** db.insert(TABLE.NOTE, null, values);
* root folder which is default folder
*/ /**
values.clear(); * temporary folder which is used for moving note
values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER); */
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); values.clear();
db.insert(TABLE.NOTE, null, values); values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
/** db.insert(TABLE.NOTE, null, values);
* temporary folder which is used for moving note
*/ /**
values.clear(); * create trash folder
values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER); */
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); values.clear();
db.insert(TABLE.NOTE, null, values); values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
/** db.insert(TABLE.NOTE, null, values);
* create trash folder }
*/
values.clear(); public void createDataTable(SQLiteDatabase db) {
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); db.execSQL(CREATE_DATA_TABLE_SQL);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); reCreateDataTableTriggers(db);
db.insert(TABLE.NOTE, null, values); db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL);
} Log.d(TAG, "data table has been created");
}
public void createDataTable(SQLiteDatabase db) {
db.execSQL(CREATE_DATA_TABLE_SQL); private void reCreateDataTableTriggers(SQLiteDatabase db) {
reCreateDataTableTriggers(db); db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_insert");
db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL); db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_update");
Log.d(TAG, "data table has been created"); db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_delete");
}
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER);
private void reCreateDataTableTriggers(SQLiteDatabase db) { db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER);
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_insert"); db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER);
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_update"); }
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_delete");
static synchronized NotesDatabaseHelper getInstance(Context context) {
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER); if (mInstance == null) {
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER); mInstance = new NotesDatabaseHelper(context);
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER); }
} return mInstance;
}
static synchronized NotesDatabaseHelper getInstance(Context context) {
if (mInstance == null) { @Override
mInstance = new NotesDatabaseHelper(context); public void onCreate(SQLiteDatabase db) {
} createNoteTable(db);
return mInstance; createDataTable(db);
} }
//这是一个静态方法实现了单例模式。它确保在整个应用程序中只有一个NotesDatabaseHelper实例被创建和使用避免了多次创建数据库连接对象
@Override
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
public void onCreate(SQLiteDatabase db) { boolean reCreateTriggers = false;
createNoteTable(db); boolean skipV2 = false;
createDataTable(db);
} if (oldVersion == 1) {
//在数据库首次创建时被调用它调用createNoteTable和createDataTable方法分别创建note表和data表 upgradeToV2(db);
skipV2 = true; // this upgrade including the upgrade from v2 to v3
@Override oldVersion++;
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
boolean reCreateTriggers = false;
boolean skipV2 = false; if (oldVersion == 2 && !skipV2) {
upgradeToV3(db);
if (oldVersion == 1) { reCreateTriggers = true;
upgradeToV2(db); oldVersion++;
skipV2 = true; // this upgrade including the upgrade from v2 to v3 }
oldVersion++;
} if (oldVersion == 3) {
upgradeToV4(db);
if (oldVersion == 2 && !skipV2) { oldVersion++;
upgradeToV3(db); }
reCreateTriggers = true;
oldVersion++; if (reCreateTriggers) {
} reCreateNoteTableTriggers(db);
reCreateDataTableTriggers(db);
if (oldVersion == 3) { }
upgradeToV4(db);
oldVersion++; if (oldVersion != newVersion) {
} throw new IllegalStateException("Upgrade notes database to version " + newVersion
+ "fails");
if (reCreateTriggers) { }
reCreateNoteTableTriggers(db); }
reCreateDataTableTriggers(db);
} private void upgradeToV2(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE);
if (oldVersion != newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA);
throw new IllegalStateException("Upgrade notes database to version " + newVersion createNoteTable(db);
+ "fails"); createDataTable(db);
} }
}
//用于处理数据库升级操作 private void upgradeToV3(SQLiteDatabase db) {
private void upgradeToV2(SQLiteDatabase db) { // drop unused triggers
db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE); db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_insert");
db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA); db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_delete");
createNoteTable(db); db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_update");
createDataTable(db); // add a column for gtask id
} db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID
+ " TEXT NOT NULL DEFAULT ''");
private void upgradeToV3(SQLiteDatabase db) { // add a trash system folder
// drop unused triggers ContentValues values = new ContentValues();
db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_insert"); values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_delete"); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_update"); db.insert(TABLE.NOTE, null, values);
// add a column for gtask id }
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID
+ " TEXT NOT NULL DEFAULT ''"); private void upgradeToV4(SQLiteDatabase db) {
// add a trash system folder db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION
ContentValues values = new ContentValues(); + " INTEGER NOT NULL DEFAULT 0");
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); }
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); }
db.insert(TABLE.NOTE, null, values);
}
private void upgradeToV4(SQLiteDatabase db) {
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION
+ " INTEGER NOT NULL DEFAULT 0");
}
}//NotesDatabaseHelper类是一个SQLiteOpenHelper的子类用于管理与笔记相关的 SQLite 数据库。它负责数据库的创建、升级以及一些与数据库表和触发器相关的操作

@ -14,296 +14,292 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.data; package net.micode.notes.data;
import android.app.SearchManager; import android.app.SearchManager;
import android.content.ContentProvider; import android.content.ContentProvider;
import android.content.ContentUris; import android.content.ContentUris;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Intent; import android.content.Intent;
import android.content.UriMatcher; import android.content.UriMatcher;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.net.Uri; import android.net.Uri;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import net.micode.notes.R; import net.micode.notes.R;
import net.micode.notes.data.Notes.DataColumns; import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.NotesDatabaseHelper.TABLE; import net.micode.notes.data.NotesDatabaseHelper.TABLE;
public class NotesProvider extends ContentProvider { public class NotesProvider extends ContentProvider {
private static final UriMatcher mMatcher; private static final UriMatcher mMatcher;
//1个静态的UriMatcher对象用于匹配传入的Uri以便根据不同的Uri执行不同的操作
private NotesDatabaseHelper mHelper; private NotesDatabaseHelper mHelper;
//NotesDatabaseHelper类型的对象用于辅助数据库操作
private static final String TAG = "NotesProvider"; private static final String TAG = "NotesProvider";
//用于日志输出的标签
private static final int URI_NOTE = 1; private static final int URI_NOTE = 1;
private static final int URI_NOTE_ITEM = 2; private static final int URI_NOTE_ITEM = 2;
private static final int URI_DATA = 3; private static final int URI_DATA = 3;
private static final int URI_DATA_ITEM = 4; private static final int URI_DATA_ITEM = 4;
private static final int URI_SEARCH = 5; private static final int URI_SEARCH = 5;
private static final int URI_SEARCH_SUGGEST = 6; private static final int URI_SEARCH_SUGGEST = 6;
static { static {
mMatcher = new UriMatcher(UriMatcher.NO_MATCH); mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE); mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM); mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM);
mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA); mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA);
mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM); mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM);
mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH); 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);
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST); mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST);
}//在静态代码块中初始化mMatcher将不同的Uri模式与对应的整数值进行匹配这些模式包括对笔记note、数据data、搜索search和搜索建议search_suggest相关的Uri }
/** /**
* 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. * we will trim '\n' and white space in order to show more information.
*/ */
private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + "," private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + ","
+ NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + "," + 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_1 + ","
+ "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + "," + "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + ","
+ R.drawable.search_result + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1 + "," + R.drawable.search_result + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1 + ","
+ "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + "," + "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + ","
+ "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA; + "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA;
private static String NOTES_SNIPPET_SEARCH_QUERY = "SELECT " + NOTES_SEARCH_PROJECTION private static String NOTES_SNIPPET_SEARCH_QUERY = "SELECT " + NOTES_SEARCH_PROJECTION
+ " FROM " + TABLE.NOTE + " FROM " + TABLE.NOTE
+ " WHERE " + NoteColumns.SNIPPET + " LIKE ?" + " WHERE " + NoteColumns.SNIPPET + " LIKE ?"
+ " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER
+ " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE; + " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE;
@Override @Override
public boolean onCreate() { public boolean onCreate() {
mHelper = NotesDatabaseHelper.getInstance(getContext()); mHelper = NotesDatabaseHelper.getInstance(getContext());
return true; return true;
}//在ContentProvider被创建时调用用于初始化mHelper通过NotesDatabaseHelper.getInstance(getContext())获取NotesDatabaseHelper的实例 }
@Override @Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) { String sortOrder) {
Cursor c = null; Cursor c = null;
SQLiteDatabase db = mHelper.getReadableDatabase(); SQLiteDatabase db = mHelper.getReadableDatabase();
String id = null; String id = null;
switch (mMatcher.match(uri)) { switch (mMatcher.match(uri)) {
//根据mMatcher.match(uri)的结果来判断Uri的类型然后针对不同类型的Uri在相应的表上执行查询操作 case URI_NOTE:
case URI_NOTE: c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null, sortOrder);
sortOrder); break;
break; case URI_NOTE_ITEM:
case URI_NOTE_ITEM: id = uri.getPathSegments().get(1);
id = uri.getPathSegments().get(1); c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder);
+ parseSelection(selection), selectionArgs, null, null, sortOrder); break;
break; case URI_DATA:
case URI_DATA: c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null,
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null, sortOrder);
sortOrder); break;
break; case URI_DATA_ITEM:
case URI_DATA_ITEM: id = uri.getPathSegments().get(1);
id = uri.getPathSegments().get(1); c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder);
+ parseSelection(selection), selectionArgs, null, null, sortOrder); break;
break; case URI_SEARCH:
case URI_SEARCH: case URI_SEARCH_SUGGEST:
case URI_SEARCH_SUGGEST: if (sortOrder != null || projection != null) {
if (sortOrder != null || projection != null) { throw new IllegalArgumentException(
throw new IllegalArgumentException( "do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query"); }
}
String searchString = null;
String searchString = null; if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) { if (uri.getPathSegments().size() > 1) {
if (uri.getPathSegments().size() > 1) { searchString = uri.getPathSegments().get(1);
searchString = uri.getPathSegments().get(1); }
} } else {
} else { searchString = uri.getQueryParameter("pattern");
searchString = uri.getQueryParameter("pattern"); }
}
if (TextUtils.isEmpty(searchString)) {
if (TextUtils.isEmpty(searchString)) { return null;
return null; }
}
try {
try { searchString = String.format("%%%s%%", searchString);
searchString = String.format("%%%s%%", searchString); c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY, new String[] { searchString });
new String[] { searchString }); } catch (IllegalStateException ex) {
} catch (IllegalStateException ex) { Log.e(TAG, "got exception: " + ex.toString());
Log.e(TAG, "got exception: " + ex.toString()); }
} break;
break; default:
default: throw new IllegalArgumentException("Unknown URI " + uri);
throw new IllegalArgumentException("Unknown URI " + uri); }
} if (c != null) {
if (c != null) { c.setNotificationUri(getContext().getContentResolver(), uri);
c.setNotificationUri(getContext().getContentResolver(), uri); }
}//如果查询结果游标不为空则设置通知Uri以便在数据发生变化时通知相关的监听器 return c;
return c; }
}//根据传入的Uri执行查询操作
@Override
@Override public Uri insert(Uri uri, ContentValues values) {
public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = mHelper.getWritableDatabase();
SQLiteDatabase db = mHelper.getWritableDatabase(); long dataId = 0, noteId = 0, insertedId = 0;
long dataId = 0, noteId = 0, insertedId = 0; switch (mMatcher.match(uri)) {
switch (mMatcher.match(uri)) { case URI_NOTE:
case URI_NOTE: insertedId = noteId = db.insert(TABLE.NOTE, null, values);
insertedId = noteId = db.insert(TABLE.NOTE, null, values); break;
break; case URI_DATA:
case URI_DATA: if (values.containsKey(DataColumns.NOTE_ID)) {
if (values.containsKey(DataColumns.NOTE_ID)) { noteId = values.getAsLong(DataColumns.NOTE_ID);
noteId = values.getAsLong(DataColumns.NOTE_ID); } else {
} else { Log.d(TAG, "Wrong data format without note id:" + values.toString());
Log.d(TAG, "Wrong data format without note id:" + values.toString()); }
} insertedId = dataId = db.insert(TABLE.DATA, null, values);
insertedId = dataId = db.insert(TABLE.DATA, null, values); break;
break; default:
default: throw new IllegalArgumentException("Unknown URI " + uri);
throw new IllegalArgumentException("Unknown URI " + uri); }
}//根据mMatcher.match(uri)的结果判断Uri的类型如果是URI_NOTE则向note表插入数据如果是URI_DATA则向data表插入数据 // Notify the note uri
// Notify the note uri if (noteId > 0) {
if (noteId > 0) { getContext().getContentResolver().notifyChange(
getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); }
}
// Notify the data uri
// Notify the data uri if (dataId > 0) {
if (dataId > 0) { getContext().getContentResolver().notifyChange(
getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null); }
}
//在插入数据后,根据插入的记录 IDnoteId或dataId通知相关的UriNotes.CONTENT_NOTE_URI或Notes.CONTENT_DATA_URI数据发生了变化 return ContentUris.withAppendedId(uri, insertedId);
return ContentUris.withAppendedId(uri, insertedId); }
}//根据传入的Uri执行插入操作
@Override
@Override public int delete(Uri uri, String selection, String[] selectionArgs) {
public int delete(Uri uri, String selection, String[] selectionArgs) { int count = 0;
int count = 0; String id = null;
String id = null; SQLiteDatabase db = mHelper.getWritableDatabase();
SQLiteDatabase db = mHelper.getWritableDatabase(); boolean deleteData = false;
boolean deleteData = false; switch (mMatcher.match(uri)) {
switch (mMatcher.match(uri)) { case URI_NOTE:
case URI_NOTE: selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 ";
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 "; count = db.delete(TABLE.NOTE, selection, selectionArgs);
count = db.delete(TABLE.NOTE, selection, selectionArgs); break;
break; case URI_NOTE_ITEM:
case URI_NOTE_ITEM: id = uri.getPathSegments().get(1);
id = uri.getPathSegments().get(1); /**
/** * ID that smaller than 0 is system folder which is not allowed to
* ID that smaller than 0 is system folder which is not allowed to * trash
* trash */
*/ long noteId = Long.valueOf(id);
long noteId = Long.valueOf(id); if (noteId <= 0) {
if (noteId <= 0) { break;
break; }
} count = db.delete(TABLE.NOTE,
count = db.delete(TABLE.NOTE, NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs); break;
break; case URI_DATA:
case URI_DATA: count = db.delete(TABLE.DATA, selection, selectionArgs);
count = db.delete(TABLE.DATA, selection, selectionArgs); deleteData = true;
deleteData = true; break;
break; case URI_DATA_ITEM:
case URI_DATA_ITEM: id = uri.getPathSegments().get(1);
id = uri.getPathSegments().get(1); count = db.delete(TABLE.DATA,
count = db.delete(TABLE.DATA, DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs); deleteData = true;
deleteData = true; break;
break; default:
default: throw new IllegalArgumentException("Unknown URI " + uri);
throw new IllegalArgumentException("Unknown URI " + uri); }
}//根据mMatcher.match(uri)的结果判断Uri的类型然后在相应的表TABLE.NOTE或TABLE.DATA上执行删除操作 if (count > 0) {
if (count > 0) { if (deleteData) {
if (deleteData) { getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); }
} getContext().getContentResolver().notifyChange(uri, null);
getContext().getContentResolver().notifyChange(uri, null); }
}//对于删除操作,如果删除的行数大于 0则根据情况通知相关的UriNotes.CONTENT_NOTE_URI或uri本身数据发生了变化 return count;
return count; }
}//根据传入的Uri执行删除操作
@Override
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int count = 0;
int count = 0; String id = null;
String id = null; SQLiteDatabase db = mHelper.getWritableDatabase();
SQLiteDatabase db = mHelper.getWritableDatabase(); boolean updateData = false;
boolean updateData = false; switch (mMatcher.match(uri)) {
switch (mMatcher.match(uri)) { case URI_NOTE:
case URI_NOTE: increaseNoteVersion(-1, selection, selectionArgs);
increaseNoteVersion(-1, selection, selectionArgs); count = db.update(TABLE.NOTE, values, selection, selectionArgs);
count = db.update(TABLE.NOTE, values, selection, selectionArgs); break;
break; case URI_NOTE_ITEM:
case URI_NOTE_ITEM: id = uri.getPathSegments().get(1);
id = uri.getPathSegments().get(1); increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs); count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
+ parseSelection(selection), selectionArgs); break;
break; case URI_DATA:
case URI_DATA: count = db.update(TABLE.DATA, values, selection, selectionArgs);
count = db.update(TABLE.DATA, values, selection, selectionArgs); updateData = true;
updateData = true; break;
break; case URI_DATA_ITEM:
case URI_DATA_ITEM: id = uri.getPathSegments().get(1);
id = uri.getPathSegments().get(1); count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
+ parseSelection(selection), selectionArgs); updateData = true;
updateData = true; break;
break; default:
default: throw new IllegalArgumentException("Unknown URI " + uri);
throw new IllegalArgumentException("Unknown URI " + uri); }
}//根据mMatcher.match(uri)的结果判断Uri的类型然后在相应的表TABLE.NOTE或TABLE.DATA上执行更新操作
if (count > 0) {
if (count > 0) { if (updateData) {
if (updateData) { getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); }
} getContext().getContentResolver().notifyChange(uri, null);
getContext().getContentResolver().notifyChange(uri, null); }
}//如果更新的行数大于 0则根据情况通知相关的UriNotes.CONTENT_NOTE_URI或uri本身数据发生了变化 return count;
return count; }
}//根据传入的Uri执行更新操作
private String parseSelection(String selection) {
private String parseSelection(String selection) { return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); }
}//用于解析查询条件字符串如果原始的查询条件字符串不为空则在前面添加AND并加上括号以便在查询语句中正确使用
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) { StringBuilder sql = new StringBuilder(120);
StringBuilder sql = new StringBuilder(120); sql.append("UPDATE ");
sql.append("UPDATE "); sql.append(TABLE.NOTE);
sql.append(TABLE.NOTE); sql.append(" SET ");
sql.append(" SET "); sql.append(NoteColumns.VERSION);
sql.append(NoteColumns.VERSION); sql.append("=" + NoteColumns.VERSION + "+1 ");
sql.append("=" + NoteColumns.VERSION + "+1 ");
//构建一个SQL语句根据传入的id和查询条件selection来更新note表中的version列。 if (id > 0 || !TextUtils.isEmpty(selection)) {
if (id > 0 || !TextUtils.isEmpty(selection)) { sql.append(" WHERE ");
sql.append(" WHERE "); }
} if (id > 0) {
if (id > 0) { sql.append(NoteColumns.ID + "=" + String.valueOf(id));
sql.append(NoteColumns.ID + "=" + String.valueOf(id)); }
} if (!TextUtils.isEmpty(selection)) {
String selectString = id > 0 ? parseSelection(selection) : selection;
if (!TextUtils.isEmpty(selection)) { for (String args : selectionArgs) {
String selectString = id > 0 ? parseSelection(selection) : selection; selectString = selectString.replaceFirst("\\?", args);
for (String args : selectionArgs) { }
selectString = selectString.replaceFirst("\\?", args); sql.append(selectString);
} }
sql.append(selectString);
} mHelper.getWritableDatabase().execSQL(sql.toString());
}
mHelper.getWritableDatabase().execSQL(sql.toString());
//通过mHelper.getWritableDatabase().execSQL执行构建的SQL语句 @Override
}//用于增加笔记的版本号 public String getType(Uri uri) {
// TODO Auto-generated method stub
@Override return null;
public String getType(Uri uri) { }
// TODO Auto-generated method stub
return null; }
}
}//负责处理与笔记相关的数据的查询、插入、删除和更新操作并通过UriMatcher来匹配不同的Uri以执行相应的操作

@ -14,247 +14,240 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.model; package net.micode.notes.model;
import android.content.ContentProviderOperation; import android.content.ContentProviderOperation;
import android.content.ContentProviderResult; import android.content.ContentProviderResult;
import android.content.ContentUris; import android.content.ContentUris;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.OperationApplicationException; import android.content.OperationApplicationException;
import android.net.Uri; import android.net.Uri;
import android.os.RemoteException; import android.os.RemoteException;
import android.util.Log; import android.util.Log;
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.CallNote; import net.micode.notes.data.Notes.CallNote;
import net.micode.notes.data.Notes.DataColumns; import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.Notes.TextNote; import net.micode.notes.data.Notes.TextNote;
import java.util.ArrayList; import java.util.ArrayList;
public class Note { public class Note {
private ContentValues mNoteDiffValues; private ContentValues mNoteDiffValues;
//一个ContentValues对象用于存储笔记的基本信息如创建日期、修改日期、类型等的变化 private NoteData mNoteData;
private NoteData mNoteData; private static final String TAG = "Note";
//NoteData类型的对象用于处理笔记中的数据相关信息 /**
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) {
* Create a new note id for adding a new note to databases // Create a new note in the database
*/ ContentValues values = new ContentValues();
public static synchronized long getNewNoteId(Context context, long folderId) { long createdTime = System.currentTimeMillis();
// Create a new note in the database values.put(NoteColumns.CREATED_DATE, createdTime);
ContentValues values = new ContentValues(); values.put(NoteColumns.MODIFIED_DATE, createdTime);
long createdTime = System.currentTimeMillis(); values.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
values.put(NoteColumns.CREATED_DATE, createdTime); values.put(NoteColumns.LOCAL_MODIFIED, 1);
values.put(NoteColumns.MODIFIED_DATE, createdTime); values.put(NoteColumns.PARENT_ID, folderId);
values.put(NoteColumns.TYPE, Notes.TYPE_NOTE); Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values);
values.put(NoteColumns.LOCAL_MODIFIED, 1);
values.put(NoteColumns.PARENT_ID, folderId); long noteId = 0;
//创建一个ContentValues对象设置一些默认的笔记信息如创建时间、修改时间、类型、是否本地修改以及父文件夹 ID 等) try {
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); noteId = Long.valueOf(uri.getPathSegments().get(1));
//通过context.getContentResolver().insert方法将这些信息插入到数据库中并获取插入后的Uri } catch (NumberFormatException e) {
long noteId = 0; Log.e(TAG, "Get note id error :" + e.toString());
try { noteId = 0;
noteId = Long.valueOf(uri.getPathSegments().get(1)); }
} catch (NumberFormatException e) { if (noteId == -1) {
Log.e(TAG, "Get note id error :" + e.toString()); throw new IllegalStateException("Wrong note id:" + noteId);
noteId = 0; }
} return noteId;
if (noteId == -1) { }
throw new IllegalStateException("Wrong note id:" + noteId);
}//从Uri中提取出笔记 ID如果提取过程中发生NumberFormatException或者笔记 ID 为 -1则抛出异常或者记录错误信息 public Note() {
return noteId; mNoteDiffValues = new ContentValues();
}//用于创建一个新的笔记 ID mNoteData = new NoteData();
}
public Note() {
mNoteDiffValues = new ContentValues(); public void setNoteValue(String key, String value) {
mNoteData = new NoteData(); 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);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); public void setTextData(String key, String value) {
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); mNoteData.setTextData(key, value);
//将传入的键值对放入mNoteDiffValues中并同时设置LOCAL_MODIFIED和MODIFIED_DATE表示笔记已被本地修改以及修改的时间 }
}//用于设置笔记的基本信息
public void setTextDataId(long id) {
public void setTextData(String key, String value) { mNoteData.setTextDataId(id);
mNoteData.setTextData(key, value); }
}//用于设置笔记中的文本数据
public long getTextDataId() {
public void setTextDataId(long id) { return mNoteData.mTextDataId;
mNoteData.setTextDataId(id); }
}
public void setCallDataId(long id) {
public long getTextDataId() { mNoteData.setCallDataId(id);
return mNoteData.mTextDataId; }
}
public void setCallData(String key, String value) {
public void setCallDataId(long id) { mNoteData.setCallData(key, value);
mNoteData.setCallDataId(id); }
}
public boolean isLocalModified() {
public void setCallData(String key, String value) { return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified();
mNoteData.setCallData(key, value); }
}
//用于设置笔记中的通话数据 public boolean syncNote(Context context, long noteId) {
if (noteId <= 0) {
public boolean isLocalModified() { throw new IllegalArgumentException("Wrong note id:" + noteId);
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); }
}//用于判断笔记是否被本地修改
if (!isLocalModified()) {
public boolean syncNote(Context context, long noteId) { return true;
if (noteId <= 0) { }
throw new IllegalArgumentException("Wrong note id:" + noteId);
} /**
* In theory, once data changed, the note should be updated on {@link NoteColumns#LOCAL_MODIFIED} and
if (!isLocalModified()) { * {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails, we also update the
return true; * note data info
} */
if (context.getContentResolver().update(
/** ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
* In theory, once data changed, the note should be updated on {@link NoteColumns#LOCAL_MODIFIED} and null) == 0) {
* {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails, we also update the Log.e(TAG, "Update note error, should not happen");
* note data info // Do not return, fall through
*/ }
if (context.getContentResolver().update( mNoteDiffValues.clear();
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
null) == 0) { if (mNoteData.isLocalModified()
Log.e(TAG, "Update note error, should not happen"); && (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
// Do not return, fall through return false;
} }
mNoteDiffValues.clear();
return true;
if (mNoteData.isLocalModified() }
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
return false; private class NoteData {
} private long mTextDataId;
return true; private ContentValues mTextDataValues;
}//用于同步笔记
private long mCallDataId;
private class NoteData {
private long mTextDataId;//存储文本数据 ID private ContentValues mCallDataValues;
private ContentValues mTextDataValues; private static final String TAG = "NoteData";
private long mCallDataId; public NoteData() {
mTextDataValues = new ContentValues();
private ContentValues mCallDataValues; mCallDataValues = new ContentValues();
mTextDataId = 0;
private static final String TAG = "NoteData"; mCallDataId = 0;
}
public NoteData() {
mTextDataValues = new ContentValues(); boolean isLocalModified() {
mCallDataValues = new ContentValues(); return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
mTextDataId = 0; }
mCallDataId = 0;
} void setTextDataId(long id) {
if(id <= 0) {
boolean isLocalModified() { throw new IllegalArgumentException("Text data id should larger than 0");
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; }
} mTextDataId = id;
}
void setTextDataId(long id) {
if(id <= 0) { void setCallDataId(long id) {
throw new IllegalArgumentException("Text data id should larger than 0"); if (id <= 0) {
} throw new IllegalArgumentException("Call data id should larger than 0");
mTextDataId = id; }
} mCallDataId = id;
}
void setCallDataId(long id) {
if (id <= 0) { void setCallData(String key, String value) {
throw new IllegalArgumentException("Call data id should larger than 0"); mCallDataValues.put(key, value);
} mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mCallDataId = id; mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
} }
void setCallData(String key, String value) { void setTextData(String key, String value) {
mCallDataValues.put(key, value); mTextDataValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
} }
void setTextData(String key, String value) { Uri pushIntoContentResolver(Context context, long noteId) {
mTextDataValues.put(key, value); /**
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); * Check for safety
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); */
} if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId);
Uri pushIntoContentResolver(Context context, long noteId) { }
/**
* Check for safety ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
*/ ContentProviderOperation.Builder builder = null;
if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId); if(mTextDataValues.size() > 0) {
} mTextDataValues.put(DataColumns.NOTE_ID, noteId);
if (mTextDataId == 0) {
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE);
ContentProviderOperation.Builder builder = null; Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
mTextDataValues);
if(mTextDataValues.size() > 0) { try {
mTextDataValues.put(DataColumns.NOTE_ID, noteId); setTextDataId(Long.valueOf(uri.getPathSegments().get(1)));
if (mTextDataId == 0) { } catch (NumberFormatException e) {
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); Log.e(TAG, "Insert new text data fail with noteId" + noteId);
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, mTextDataValues.clear();
mTextDataValues); return null;
try { }
setTextDataId(Long.valueOf(uri.getPathSegments().get(1))); } else {
} catch (NumberFormatException e) { builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
Log.e(TAG, "Insert new text data fail with noteId" + noteId); Notes.CONTENT_DATA_URI, mTextDataId));
mTextDataValues.clear(); builder.withValues(mTextDataValues);
return null; operationList.add(builder.build());
} }
} else { mTextDataValues.clear();
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( }
Notes.CONTENT_DATA_URI, mTextDataId));
builder.withValues(mTextDataValues); if(mCallDataValues.size() > 0) {
operationList.add(builder.build()); mCallDataValues.put(DataColumns.NOTE_ID, noteId);
} if (mCallDataId == 0) {
mTextDataValues.clear(); mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
} Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
mCallDataValues);
if(mCallDataValues.size() > 0) { try {
mCallDataValues.put(DataColumns.NOTE_ID, noteId); setCallDataId(Long.valueOf(uri.getPathSegments().get(1)));
if (mCallDataId == 0) { } catch (NumberFormatException e) {
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); Log.e(TAG, "Insert new call data fail with noteId" + noteId);
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, mCallDataValues.clear();
mCallDataValues); return null;
try { }
setCallDataId(Long.valueOf(uri.getPathSegments().get(1))); } else {
} catch (NumberFormatException e) { builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
Log.e(TAG, "Insert new call data fail with noteId" + noteId); Notes.CONTENT_DATA_URI, mCallDataId));
mCallDataValues.clear(); builder.withValues(mCallDataValues);
return null; operationList.add(builder.build());
} }
} else { mCallDataValues.clear();
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( }
Notes.CONTENT_DATA_URI, mCallDataId));
builder.withValues(mCallDataValues); if (operationList.size() > 0) {
operationList.add(builder.build()); try {
} ContentProviderResult[] results = context.getContentResolver().applyBatch(
mCallDataValues.clear(); Notes.AUTHORITY, operationList);
} return (results == null || results.length == 0 || results[0] == null) ? null
: ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId);
if (operationList.size() > 0) { } catch (RemoteException e) {
try { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
ContentProviderResult[] results = context.getContentResolver().applyBatch( return null;
Notes.AUTHORITY, operationList); } catch (OperationApplicationException e) {
return (results == null || results.length == 0 || results[0] == null) ? null Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
: ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId); return null;
} catch (RemoteException e) { }
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); }
return null; return null;
} catch (OperationApplicationException e) { }
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); }
return null; }
}
}
return null;
}
}//用于处理笔记中的数据相关操作
}

@ -14,356 +14,355 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.model; package net.micode.notes.model;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
import android.content.ContentUris; import android.content.ContentUris;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.CallNote; import net.micode.notes.data.Notes.CallNote;
import net.micode.notes.data.Notes.DataColumns; import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants; import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.Notes.TextNote; import net.micode.notes.data.Notes.TextNote;
import net.micode.notes.tool.ResourceParser.NoteBgResources; import net.micode.notes.tool.ResourceParser.NoteBgResources;
public class WorkingNote { public class WorkingNote {
// Note for the working note // Note for the working note
private Note mNote; private Note mNote;
// Note Id // Note Id
private long mNoteId; private long mNoteId;
// Note content // Note content
private String mContent; private String mContent;
// Note mode // 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;
private int mWidgetId; private int mWidgetId;
private int mWidgetType; private int mWidgetType;
private long mFolderId; private long mFolderId;
private Context mContext; private Context mContext;
private static final String TAG = "WorkingNote"; private static final String TAG = "WorkingNote";
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.ID,
DataColumns.CONTENT, DataColumns.CONTENT,
DataColumns.MIME_TYPE, DataColumns.MIME_TYPE,
DataColumns.DATA1, DataColumns.DATA1,
DataColumns.DATA2, DataColumns.DATA2,
DataColumns.DATA3, DataColumns.DATA3,
DataColumns.DATA4, DataColumns.DATA4,
}; };
public static final String[] NOTE_PROJECTION = new String[] { public static final String[] NOTE_PROJECTION = new String[] {
NoteColumns.PARENT_ID, NoteColumns.PARENT_ID,
NoteColumns.ALERTED_DATE, NoteColumns.ALERTED_DATE,
NoteColumns.BG_COLOR_ID, NoteColumns.BG_COLOR_ID,
NoteColumns.WIDGET_ID, NoteColumns.WIDGET_ID,
NoteColumns.WIDGET_TYPE, NoteColumns.WIDGET_TYPE,
NoteColumns.MODIFIED_DATE NoteColumns.MODIFIED_DATE
}; };
private static final int DATA_ID_COLUMN = 0; private static final int DATA_ID_COLUMN = 0;
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;
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;
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;
private static final int NOTE_WIDGET_ID_COLUMN = 3; private static final int NOTE_WIDGET_ID_COLUMN = 3;
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 // New note construct
private WorkingNote(Context context, long folderId) { private WorkingNote(Context context, long folderId) {
mContext = context; mContext = context;
mAlertDate = 0; mAlertDate = 0;
mModifiedDate = System.currentTimeMillis(); mModifiedDate = System.currentTimeMillis();
mFolderId = folderId; mFolderId = folderId;
mNote = new Note(); mNote = new Note();
mNoteId = 0; mNoteId = 0;
mIsDeleted = false; mIsDeleted = false;
mMode = 0; mMode = 0;
mWidgetType = Notes.TYPE_WIDGET_INVALIDE; mWidgetType = Notes.TYPE_WIDGET_INVALIDE;
} }
//用于新建笔记初始化了一些默认值并创建了一个新的Note对象。
// Existing note construct // Existing note construct
private WorkingNote(Context context, long noteId, long folderId) { private WorkingNote(Context context, long noteId, long folderId) {
mContext = context; mContext = context;
mNoteId = noteId; mNoteId = noteId;
mFolderId = folderId; mFolderId = folderId;
mIsDeleted = false; mIsDeleted = false;
mNote = new Note(); mNote = new Note();
loadNote(); loadNote();
}//用于加载已存在的笔记,通过查询数据库加载笔记的属性和数据。 }
private void loadNote() { private void loadNote() {
Cursor cursor = mContext.getContentResolver().query( Cursor cursor = mContext.getContentResolver().query(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null, ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null,
null, null); null, null);
if (cursor != null) { if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN); mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN);
mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN); mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN);
mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN); mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN);
mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN); mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN);
mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN); mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN);
mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN); mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN);
} }
cursor.close(); cursor.close();
} else { } else {
Log.e(TAG, "No note with id:" + mNoteId); 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);
} }
loadNoteData(); loadNoteData();
}//用于加载笔记的基本属性,如父文件夹 ID、提醒日期、背景颜色等。 }
private void loadNoteData() { private void loadNoteData() {
Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION,
DataColumns.NOTE_ID + "=?", new String[] { DataColumns.NOTE_ID + "=?", new String[] {
String.valueOf(mNoteId) String.valueOf(mNoteId)
}, null); }, null);
if (cursor != null) { if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
String type = cursor.getString(DATA_MIME_TYPE_COLUMN); String type = cursor.getString(DATA_MIME_TYPE_COLUMN);
if (DataConstants.NOTE.equals(type)) { if (DataConstants.NOTE.equals(type)) {
mContent = cursor.getString(DATA_CONTENT_COLUMN); mContent = cursor.getString(DATA_CONTENT_COLUMN);
mMode = cursor.getInt(DATA_MODE_COLUMN); mMode = cursor.getInt(DATA_MODE_COLUMN);
mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN)); mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN));
} else if (DataConstants.CALL_NOTE.equals(type)) { } else if (DataConstants.CALL_NOTE.equals(type)) {
mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN));
} else { } else {
Log.d(TAG, "Wrong note type with type:" + type); Log.d(TAG, "Wrong note type with type:" + type);
} }
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
cursor.close(); cursor.close();
} else { } else {
Log.e(TAG, "No data with id:" + mNoteId); 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, public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId,
int widgetType, int defaultBgColorId) { int widgetType, int defaultBgColorId) {
WorkingNote note = new WorkingNote(context, folderId); WorkingNote note = new WorkingNote(context, folderId);
note.setBgColorId(defaultBgColorId); note.setBgColorId(defaultBgColorId);
note.setWidgetId(widgetId); note.setWidgetId(widgetId);
note.setWidgetType(widgetType); note.setWidgetType(widgetType);
return note; return note;
} }
public static WorkingNote load(Context context, long id) { public static WorkingNote load(Context context, long id) {
return new WorkingNote(context, id, 0); return new WorkingNote(context, id, 0);
} }
public synchronized boolean saveNote() { public synchronized boolean saveNote() {
if (isWorthSaving()) { if (isWorthSaving()) {
if (!existInDatabase()) { if (!existInDatabase()) {
if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) {
Log.e(TAG, "Create new note fail with id:" + mNoteId); Log.e(TAG, "Create new note fail with id:" + mNoteId);
return false; return false;
} }
} }
mNote.syncNote(mContext, mNoteId); mNote.syncNote(mContext, mNoteId);
/** /**
* Update widget content if there exist any widget of this note * 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 && mWidgetType != Notes.TYPE_WIDGET_INVALIDE
&& mNoteSettingStatusListener != null) { && mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged(); mNoteSettingStatusListener.onWidgetChanged();
} }
return true; return true;
} else { } else {
return false; return false;
} }
}//判断笔记是否值得保存,如果值得保存则将笔记保存到数据库。如果笔记是新创建的,会获取一个新的笔记 ID。保存成功后如果有相关的小部件会通知监听器更新小部件内容 }
public boolean existInDatabase() { public boolean existInDatabase() {
return mNoteId > 0; return mNoteId > 0;
} }
private boolean isWorthSaving() { private boolean isWorthSaving() {
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent))
|| (existInDatabase() && !mNote.isLocalModified())) { || (existInDatabase() && !mNote.isLocalModified())) {
return false; return false;
} else { } else {
return true; return true;
} }
} }
public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) { public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) {
mNoteSettingStatusListener = l; mNoteSettingStatusListener = l;
} }
public void setAlertDate(long date, boolean set) { public void setAlertDate(long date, boolean set) {
if (date != mAlertDate) { if (date != mAlertDate) {
mAlertDate = date; mAlertDate = date;
mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate));
} }
if (mNoteSettingStatusListener != null) { if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onClockAlertChanged(date, set); mNoteSettingStatusListener.onClockAlertChanged(date, set);
} }
} }
public void markDeleted(boolean mark) { public void markDeleted(boolean mark) {
mIsDeleted = mark; mIsDeleted = mark;
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged(); mNoteSettingStatusListener.onWidgetChanged();
} }
} }
public void setBgColorId(int id) { public void setBgColorId(int id) {
if (id != mBgColorId) { if (id != mBgColorId) {
mBgColorId = id; mBgColorId = id;
if (mNoteSettingStatusListener != null) { 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) { public void setCheckListMode(int mode) {
if (mMode != mode) { if (mMode != mode) {
if (mNoteSettingStatusListener != null) { if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode);
} }
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) { if (type != mWidgetType) {
mWidgetType = type; 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) {
if (id != mWidgetId) { if (id != mWidgetId) {
mWidgetId = id; 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)) { if (!TextUtils.equals(mContent, text)) {
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.CALL_DATE, String.valueOf(callDate));
mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber); 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));
} }
public boolean hasClockAlert() { public boolean hasClockAlert() {
return (mAlertDate > 0 ? true : false); return (mAlertDate > 0 ? true : false);
} }
public String getContent() { public String getContent() {
return mContent; return mContent;
} }
public long getAlertDate() { public long getAlertDate() {
return mAlertDate; return mAlertDate;
} }
public long getModifiedDate() { public long getModifiedDate() {
return mModifiedDate; return mModifiedDate;
} }
public int getBgColorResId() { public int getBgColorResId() {
return NoteBgResources.getNoteBgResource(mBgColorId); return NoteBgResources.getNoteBgResource(mBgColorId);
} }
public int getBgColorId() { public int getBgColorId() {
return mBgColorId; return mBgColorId;
} }
public int getTitleBgResId() { public int getTitleBgResId() {
return NoteBgResources.getNoteTitleBgResource(mBgColorId); return NoteBgResources.getNoteTitleBgResource(mBgColorId);
} }
public int getCheckListMode() { public int getCheckListMode() {
return mMode; return mMode;
} }
public long getNoteId() { public long getNoteId() {
return mNoteId; return mNoteId;
} }
public long getFolderId() { public long getFolderId() {
return mFolderId; return mFolderId;
} }
public int getWidgetId() { public int getWidgetId() {
return mWidgetId; return mWidgetId;
} }
public int getWidgetType() { public int getWidgetType() {
return mWidgetType; return mWidgetType;
} }
public interface NoteSettingChangedListener { public interface NoteSettingChangedListener {
/** /**
* Called when the background color of current note has just changed * Called when the background color of current note has just changed
*/ */
void onBackgroundColorChanged(); void onBackgroundColorChanged();
/** /**
* Called when user set clock * Called when user set clock
*/ */
void onClockAlertChanged(long date, boolean set); void onClockAlertChanged(long date, boolean set);
/** /**
* Call when user create note from widget * Call when user create note from widget
*/ */
void onWidgetChanged(); void onWidgetChanged();
/** /**
* Call when switch between check list mode and normal mode * Call when switch between check list mode and normal mode
* @param oldMode is previous mode before change * @param oldMode is previous mode before change
* @param newMode is new mode * @param newMode is new mode
*/ */
void onCheckListModeChanged(int oldMode, int newMode); void onCheckListModeChanged(int oldMode, int newMode);
} }
} }

@ -14,171 +14,145 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener; import android.content.DialogInterface.OnDismissListener;
import android.content.Intent; import android.content.Intent;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerManager; import android.os.PowerManager;
import android.provider.Settings; import android.provider.Settings;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import net.micode.notes.R; import net.micode.notes.R;
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import net.micode.notes.tool.DataUtils; import net.micode.notes.tool.DataUtils;
import java.io.IOException; import java.io.IOException;
// 定义一个名为 AlarmAlertActivity 的类,继承自 Activity并实现了 OnClickListener 和 OnDismissListener 接口 public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener { private long mNoteId;
// 存储笔记的 ID private String mSnippet;
private long mNoteId; private static final int SNIPPET_PREW_MAX_LEN = 60;
// 存储笔记的片段内容 MediaPlayer mPlayer;
private String mSnippet;
// 定义一个常量,表示片段预览的最大长度 @Override
private static final int SNIPPET_PREW_MAX_LEN = 60; protected void onCreate(Bundle savedInstanceState) {
// 媒体播放器对象 super.onCreate(savedInstanceState);
MediaPlayer mPlayer; requestWindowFeature(Window.FEATURE_NO_TITLE);
@Override final Window win = getWindow();
protected void onCreate(Bundle savedInstanceState) { win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
super.onCreate(savedInstanceState);
// 请求无标题栏的窗口特征 if (!isScreenOn()) {
requestWindowFeature(Window.FEATURE_NO_TITLE); win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
final Window win = getWindow(); | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
// 设置窗口标志,使其在锁定时显示 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); }
if (!isScreenOn()) { Intent intent = getIntent();
// 如果屏幕未点亮,设置一系列窗口标志以保持屏幕常亮、点亮屏幕等
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON try {
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR); mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0,
} SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info)
: mSnippet;
Intent intent = getIntent(); } catch (IllegalArgumentException e) {
e.printStackTrace();
try { return;
// 从 Intent 的数据路径中获取笔记 ID并转换为长整型 }
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
// 根据笔记 ID 获取片段内容 mPlayer = new MediaPlayer();
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
// 如果片段内容长度大于最大长度,截取并添加提示信息,否则保持不变 showActionDialog();
mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN? mSnippet.substring(0, playAlarmSound();
SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info) } else {
: mSnippet; finish();
} catch (IllegalArgumentException e) { }
// 打印异常信息 }
e.printStackTrace();
return; private boolean isScreenOn() {
} PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
mPlayer = new MediaPlayer(); }
// 如果笔记在数据库中可见且类型为 Notes.TYPE_NOTE
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) { private void playAlarmSound() {
// 显示操作对话框 Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
showActionDialog();
// 播放闹钟声音 int silentModeStreams = Settings.System.getInt(getContentResolver(),
playAlarmSound(); Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
} else {
// 如果不满足条件,结束活动 if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
finish(); mPlayer.setAudioStreamType(silentModeStreams);
} } else {
} mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
}
// 判断屏幕是否点亮的方法 try {
private boolean isScreenOn() { mPlayer.setDataSource(this, url);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mPlayer.prepare();
return pm.isScreenOn(); mPlayer.setLooping(true);
} mPlayer.start();
} catch (IllegalArgumentException e) {
// 播放闹钟声音的方法 // TODO Auto-generated catch block
private void playAlarmSound() { e.printStackTrace();
// 获取系统默认的闹钟铃声 Uri } catch (SecurityException e) {
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM); // TODO Auto-generated catch block
e.printStackTrace();
int silentModeStreams = Settings.System.getInt(getContentResolver(), } catch (IllegalStateException e) {
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); // TODO Auto-generated catch block
e.printStackTrace();
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM))!= 0) { } catch (IOException e) {
// 根据系统设置设置媒体播放器的音频流类型 // TODO Auto-generated catch block
mPlayer.setAudioStreamType(silentModeStreams); e.printStackTrace();
} else { }
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); }
}
try { private void showActionDialog() {
// 设置数据源、准备播放、设置循环播放并开始播放 AlertDialog.Builder dialog = new AlertDialog.Builder(this);
mPlayer.setDataSource(this, url); dialog.setTitle(R.string.app_name);
mPlayer.prepare(); dialog.setMessage(mSnippet);
mPlayer.setLooping(true); dialog.setPositiveButton(R.string.notealert_ok, this);
mPlayer.start(); if (isScreenOn()) {
} catch (IllegalArgumentException e) { dialog.setNegativeButton(R.string.notealert_enter, this);
// 打印非法参数异常信息 }
e.printStackTrace(); dialog.show().setOnDismissListener(this);
} catch (SecurityException e) { }
// 打印安全异常信息
e.printStackTrace(); public void onClick(DialogInterface dialog, int which) {
} catch (IllegalStateException e) { switch (which) {
// 打印非法状态异常信息 case DialogInterface.BUTTON_NEGATIVE:
e.printStackTrace(); Intent intent = new Intent(this, NoteEditActivity.class);
} catch (IOException e) { intent.setAction(Intent.ACTION_VIEW);
// 打印输入输出异常信息 intent.putExtra(Intent.EXTRA_UID, mNoteId);
e.printStackTrace(); startActivity(intent);
} break;
} default:
break;
// 显示操作对话框的方法 }
private void showActionDialog() { }
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.app_name); public void onDismiss(DialogInterface dialog) {
dialog.setMessage(mSnippet); stopAlarmSound();
dialog.setPositiveButton(R.string.notealert_ok, this); finish();
if (isScreenOn()) { }
dialog.setNegativeButton(R.string.notealert_enter, this);
} private void stopAlarmSound() {
// 显示对话框并设置其消失监听器 if (mPlayer != null) {
dialog.show().setOnDismissListener(this); mPlayer.stop();
} mPlayer.release();
mPlayer = null;
// 实现 OnClickListener 接口的方法,处理对话框按钮点击事件 }
public void onClick(DialogInterface dialog, int which) { }
switch (which) { }
case DialogInterface.BUTTON_NEGATIVE:
Intent intent = new Intent(this, NoteEditActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_UID, mNoteId);
startActivity(intent);
break;
default:
break;
}
}
// 实现 OnDismissListener 接口的方法,处理对话框消失事件
public void onDismiss(DialogInterface dialog) {
stopAlarmSound();
finish();
}
// 停止闹钟声音的方法
private void stopAlarmSound() {
if (mPlayer!= null) {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
}

@ -14,63 +14,52 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ContentUris; import android.content.ContentUris;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
// 定义一个名为 AlarmInitReceiver 的类,继承自 BroadcastReceiver
public class AlarmInitReceiver extends BroadcastReceiver { public class AlarmInitReceiver extends BroadcastReceiver {
// 定义查询数据库时的投影数组,包含笔记 ID 和提醒日期两个字段 private static final String [] PROJECTION = new String [] {
private static final String[] PROJECTION = new String[]{ NoteColumns.ID,
NoteColumns.ID, NoteColumns.ALERTED_DATE
NoteColumns.ALERTED_DATE };
};
private static final int COLUMN_ID = 0;
// 定义常量,表示投影数组中 ID 和提醒日期字段的索引 private static final int COLUMN_ALERTED_DATE = 1;
private static final int COLUMN_ID = 0;
private static final int COLUMN_ALERTED_DATE = 1; @Override
public void onReceive(Context context, Intent intent) {
@Override long currentDate = System.currentTimeMillis();
public void onReceive(Context context, Intent intent) { Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
// 获取当前时间 PROJECTION,
long currentDate = System.currentTimeMillis(); NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
// 查询数据库,获取提醒日期大于当前时间且类型为笔记的记录 new String[] { String.valueOf(currentDate) },
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI, null);
PROJECTION,
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE, if (c != null) {
new String[]{String.valueOf(currentDate)}, if (c.moveToFirst()) {
null); do {
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
if (c!= null) { Intent sender = new Intent(context, AlarmReceiver.class);
if (c.moveToFirst()) { sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
do { PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 获取每条记录的提醒日期 AlarmManager alermManager = (AlarmManager) context
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
// 创建 Intent用于发送给 AlarmReceiver
Intent sender = new Intent(context, AlarmReceiver.class);
// 设置 Intent 的数据为带有笔记 ID 的 Uri
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 创建 PendingIntent用于触发广播
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 获取系统的 AlarmManager 服务
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE); .getSystemService(Context.ALARM_SERVICE);
// 设置闹钟,在指定时间触发 PendingIntent alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent); } while (c.moveToNext());
} while (c.moveToNext()); }
} c.close();
// 关闭游标 }
c.close(); }
} }
}
}

@ -14,22 +14,17 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
// 定义一个名为 AlarmReceiver 的类,它继承自 BroadcastReceiver public class AlarmReceiver extends BroadcastReceiver {
public class AlarmReceiver extends BroadcastReceiver { @Override
// 重写 onReceive 方法,当接收到广播时会调用这个方法 public void onReceive(Context context, Intent intent) {
@Override intent.setClass(context, AlarmAlertActivity.class);
public void onReceive(Context context, Intent intent) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 设置 Intent 的目标类为 AlarmAlertActivity context.startActivity(intent);
intent.setClass(context, AlarmAlertActivity.class); }
// 为 Intent 添加标志,表示启动一个新的任务栈 }
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 以给定的 Intent 启动一个新的 Activity
context.startActivity(intent);
}
}

@ -14,101 +14,77 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import java.util.Calendar; import java.util.Calendar;
import net.micode.notes.R; import net.micode.notes.R;
import net.micode.notes.ui.DateTimePicker; import net.micode.notes.ui.DateTimePicker;
import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener; import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.text.format.DateUtils; import android.text.format.DateUtils;
// 定义一个名为 DateTimePickerDialog 的类,它继承自 AlertDialog 并实现了 OnClickListener 接口 public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
private Calendar mDate = Calendar.getInstance();
// 存储日期的 Calendar 对象 private boolean mIs24HourView;
private Calendar mDate = Calendar.getInstance(); private OnDateTimeSetListener mOnDateTimeSetListener;
// 标记是否为 24 小时制显示 private DateTimePicker mDateTimePicker;
private boolean mIs24HourView;
// 日期时间设置监听器 public interface OnDateTimeSetListener {
private OnDateTimeSetListener mOnDateTimeSetListener; void OnDateTimeSet(AlertDialog dialog, long date);
// 日期时间选择器 }
private DateTimePicker mDateTimePicker;
public DateTimePickerDialog(Context context, long date) {
// 定义日期时间设置监听器接口 super(context);
public interface OnDateTimeSetListener { mDateTimePicker = new DateTimePicker(context);
void OnDateTimeSet(AlertDialog dialog, long date); setView(mDateTimePicker);
} mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
public void onDateTimeChanged(DateTimePicker view, int year, int month,
// 构造函数,接受上下文和一个日期时间的长整型值 int dayOfMonth, int hourOfDay, int minute) {
public DateTimePickerDialog(Context context, long date) { mDate.set(Calendar.YEAR, year);
super(context); mDate.set(Calendar.MONTH, month);
// 创建一个 DateTimePicker 对象 mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
mDateTimePicker = new DateTimePicker(context); mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
// 设置对话框的视图为 DateTimePicker mDate.set(Calendar.MINUTE, minute);
setView(mDateTimePicker); updateTitle(mDate.getTimeInMillis());
// 设置日期时间改变监听器 }
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() { });
public void onDateTimeChanged(DateTimePicker view, int year, int month, mDate.setTimeInMillis(date);
int dayOfMonth, int hourOfDay, int minute) { mDate.set(Calendar.SECOND, 0);
// 设置 Calendar 对象的年、月、日、时、分 mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
mDate.set(Calendar.YEAR, year); setButton(context.getString(R.string.datetime_dialog_ok), this);
mDate.set(Calendar.MONTH, month); setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); set24HourView(DateFormat.is24HourFormat(this.getContext()));
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); updateTitle(mDate.getTimeInMillis());
mDate.set(Calendar.MINUTE, minute); }
// 更新对话框标题
updateTitle(mDate.getTimeInMillis()); public void set24HourView(boolean is24HourView) {
} mIs24HourView = is24HourView;
}); }
// 设置 Calendar 对象的时间为传入的日期时间
mDate.setTimeInMillis(date); public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
// 将秒设置为 0 mOnDateTimeSetListener = callBack;
mDate.set(Calendar.SECOND, 0); }
// 设置 DateTimePicker 的当前日期时间
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); private void updateTitle(long date) {
// 设置确定按钮的文本和点击监听器 int flag =
setButton(context.getString(R.string.datetime_dialog_ok), this); DateUtils.FORMAT_SHOW_YEAR |
// 设置取消按钮的文本和点击监听器为 null DateUtils.FORMAT_SHOW_DATE |
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null); DateUtils.FORMAT_SHOW_TIME;
// 根据上下文设置是否为 24 小时制显示 flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;
set24HourView(DateFormat.is24HourFormat(this.getContext())); setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
// 更新对话框标题 }
updateTitle(mDate.getTimeInMillis());
} public void onClick(DialogInterface arg0, int arg1) {
if (mOnDateTimeSetListener != null) {
// 设置是否为 24 小时制显示的方法 mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
public void set24HourView(boolean is24HourView) { }
mIs24HourView = is24HourView; }
}
}
// 设置日期时间设置监听器的方法
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
mOnDateTimeSetListener = callBack;
}
// 更新对话框标题的私有方法
private void updateTitle(long date) {
int flag =
DateUtils.FORMAT_SHOW_YEAR |
DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_TIME;
flag |= mIs24HourView? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
}
// 实现 OnClickListener 接口的方法,当点击确定按钮时调用
public void onClick(DialogInterface arg0, int arg1) {
if (mOnDateTimeSetListener!= null) {
// 调用日期时间设置监听器的方法
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
}
}
}

@ -14,60 +14,48 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.content.Context; import android.content.Context;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.Button; import android.widget.Button;
import android.widget.PopupMenu; import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener; import android.widget.PopupMenu.OnMenuItemClickListener;
import net.micode.notes.R; import net.micode.notes.R;
// 定义一个名为 DropdownMenu 的类 public class DropdownMenu {
public class DropdownMenu { private Button mButton;
// 下拉菜单的按钮 private PopupMenu mPopupMenu;
private Button mButton; private Menu mMenu;
// 弹出菜单对象
private PopupMenu mPopupMenu; public DropdownMenu(Context context, Button button, int menuId) {
// 菜单对象 mButton = button;
private Menu mMenu; mButton.setBackgroundResource(R.drawable.dropdown_icon);
mPopupMenu = new PopupMenu(context, mButton);
// 构造函数,接受上下文、按钮和菜单资源 ID mMenu = mPopupMenu.getMenu();
public DropdownMenu(Context context, Button button, int menuId) { mPopupMenu.getMenuInflater().inflate(menuId, mMenu);
mButton = button; mButton.setOnClickListener(new OnClickListener() {
// 设置按钮的背景为下拉图标 public void onClick(View v) {
mButton.setBackgroundResource(R.drawable.dropdown_icon); mPopupMenu.show();
// 创建一个弹出菜单,关联到按钮 }
mPopupMenu = new PopupMenu(context, mButton); });
mMenu = mPopupMenu.getMenu(); }
// 从给定的菜单资源 ID 中填充菜单
mPopupMenu.getMenuInflater().inflate(menuId, mMenu); public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {
// 为按钮设置点击监听器,点击时显示弹出菜单 if (mPopupMenu != null) {
mButton.setOnClickListener(new OnClickListener() { mPopupMenu.setOnMenuItemClickListener(listener);
public void onClick(View v) { }
mPopupMenu.show(); }
}
}); public MenuItem findItem(int id) {
} return mMenu.findItem(id);
}
// 设置弹出菜单项点击监听器的方法
public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) { public void setTitle(CharSequence title) {
if (mPopupMenu!= null) { mButton.setText(title);
mPopupMenu.setOnMenuItemClickListener(listener); }
} }
}
// 根据 ID 查找菜单项的方法
public MenuItem findItem(int id) {
return mMenu.findItem(id);
}
// 设置按钮标题的方法
public void setTitle(CharSequence title) {
mButton.setText(title);
}
}

@ -14,79 +14,67 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.CursorAdapter; import android.widget.CursorAdapter;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import net.micode.notes.R; import net.micode.notes.R;
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
// 定义一个名为 FoldersListAdapter 的类,继承自 CursorAdapter
public class FoldersListAdapter extends CursorAdapter { public class FoldersListAdapter extends CursorAdapter {
// 定义查询投影,包含 ID 和片段SNIPPET public static final String [] PROJECTION = {
public static final String[] PROJECTION = { NoteColumns.ID,
NoteColumns.ID, NoteColumns.SNIPPET
NoteColumns.SNIPPET };
};
// ID 列的索引 public static final int ID_COLUMN = 0;
public static final int ID_COLUMN = 0; public static final int NAME_COLUMN = 1;
// 名称列的索引(实际上是片段列在这个上下文中用作名称)
public static final int NAME_COLUMN = 1; public FoldersListAdapter(Context context, Cursor c) {
super(context, c);
// 构造函数,接受上下文和游标 // TODO Auto-generated constructor stub
public FoldersListAdapter(Context context, Cursor c) { }
super(context, c);
// TODO Auto-generated constructor stub @Override
} public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new FolderListItem(context);
// 创建新视图的方法 }
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) { @Override
return new FolderListItem(context); public void bindView(View view, Context context, Cursor cursor) {
} if (view instanceof FolderListItem) {
String folderName = (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
// 绑定数据到视图的方法
@Override
public void bindView(View view, Context context, Cursor cursor) {
if (view instanceof FolderListItem) {
// 根据 ID 判断是否为根文件夹,设置不同的文件夹名称
String folderName = (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER)? context
.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN); .getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
((FolderListItem) view).bind(folderName); ((FolderListItem) view).bind(folderName);
} }
} }
// 获取指定位置的文件夹名称的方法 public String getFolderName(Context context, int position) {
public String getFolderName(Context context, int position) { Cursor cursor = (Cursor) getItem(position);
Cursor cursor = (Cursor) getItem(position); return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER)? context
.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN); .getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
} }
// 内部类 FolderListItem代表列表中的单个文件夹项视图 private class FolderListItem extends LinearLayout {
private class FolderListItem extends LinearLayout { private TextView mName;
private TextView mName;
public FolderListItem(Context context) {
// 构造函数,接受上下文 super(context);
public FolderListItem(Context context) { inflate(context, R.layout.folder_list_item, this);
super(context); mName = (TextView) findViewById(R.id.tv_folder_name);
// 从布局文件中填充视图 }
inflate(context, R.layout.folder_list_item, this);
// 获取显示文件夹名称的 TextView public void bind(String name) {
mName = (TextView) findViewById(R.id.tv_folder_name); mName.setText(name);
} }
}
// 绑定文件夹名称到视图的方法
public void bind(String name) { }
mName.setText(name);
}
}
}

@ -14,218 +14,204 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.content.Context; import android.content.Context;
import android.graphics.Rect; import android.graphics.Rect;
import android.text.Layout; import android.text.Layout;
import android.text.Selection; import android.text.Selection;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.URLSpan; import android.text.style.URLSpan;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener; import android.view.MenuItem.OnMenuItemClickListener;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.widget.EditText; import android.widget.EditText;
import net.micode.notes.R; import net.micode.notes.R;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
// 定义一个名为 NoteEditText 的自定义 EditText 类 public class NoteEditText extends EditText {
public class NoteEditText extends EditText { private static final String TAG = "NoteEditText";
private static final String TAG = "NoteEditText"; private int mIndex;
// 当前 EditText 的索引 private int mSelectionStartBeforeDelete;
private int mIndex;
// 删除操作前的选中文本起始位置 private static final String SCHEME_TEL = "tel:" ;
private int mSelectionStartBeforeDelete; private static final String SCHEME_HTTP = "http:" ;
private static final String SCHEME_EMAIL = "mailto:" ;
// 不同的链接方案常量
private static final String SCHEME_TEL = "tel:"; private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>();
private static final String SCHEME_HTTP = "http:"; static {
private static final String SCHEME_EMAIL = "mailto:"; sSchemaActionResMap.put(SCHEME_TEL, R.string.note_link_tel);
sSchemaActionResMap.put(SCHEME_HTTP, R.string.note_link_web);
// 存储链接方案和对应的资源 ID 的映射 sSchemaActionResMap.put(SCHEME_EMAIL, R.string.note_link_email);
private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>(); }
static {
sSchemaActionResMap.put(SCHEME_TEL, R.string.note_link_tel); /**
sSchemaActionResMap.put(SCHEME_HTTP, R.string.note_link_web); * Call by the {@link NoteEditActivity} to delete or add edit text
sSchemaActionResMap.put(SCHEME_EMAIL, R.string.note_link_email); */
} public interface OnTextViewChangeListener {
/**
// 定义接口,用于在 NoteEditActivity 中监听 EditText 的变化 * Delete current edit text when {@link KeyEvent#KEYCODE_DEL} happens
public interface OnTextViewChangeListener { * and the text is null
// 当按下删除键且文本为空时,删除当前 EditText */
void onEditTextDelete(int index, String text); void onEditTextDelete(int index, String text);
// 当按下回车键时,在当前 EditText 后添加一个新的 EditText /**
void onEditTextEnter(int index, String text); * Add edit text after current edit text when {@link KeyEvent#KEYCODE_ENTER}
* happen
// 根据文本是否存在显示或隐藏选项 */
void onTextChange(int index, boolean hasText); void onEditTextEnter(int index, String text);
}
/**
// 用于监听文本变化的接口实例 * Hide or show item option when text change
private OnTextViewChangeListener mOnTextViewChangeListener; */
void onTextChange(int index, boolean hasText);
// 构造函数,接受上下文 }
public NoteEditText(Context context) {
super(context, null); private OnTextViewChangeListener mOnTextViewChangeListener;
mIndex = 0;
} public NoteEditText(Context context) {
super(context, null);
// 设置当前 EditText 的索引 mIndex = 0;
public void setIndex(int index) { }
mIndex = index;
} public void setIndex(int index) {
mIndex = index;
// 设置文本变化监听器 }
public void setOnTextViewChangeListener(OnTextViewChangeListener listener) {
mOnTextViewChangeListener = listener; public void setOnTextViewChangeListener(OnTextViewChangeListener listener) {
} mOnTextViewChangeListener = listener;
}
// 构造函数,接受上下文和属性集
public NoteEditText(Context context, AttributeSet attrs) { public NoteEditText(Context context, AttributeSet attrs) {
super(context, attrs, android.R.attr.editTextStyle); super(context, attrs, android.R.attr.editTextStyle);
} }
// 构造函数,接受上下文、属性集和样式 public NoteEditText(Context context, AttributeSet attrs, int defStyle) {
public NoteEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle);
super(context, attrs, defStyle); // TODO Auto-generated constructor stub
// TODO Auto-generated constructor stub }
}
@Override
// 处理触摸事件 public boolean onTouchEvent(MotionEvent event) {
@Override switch (event.getAction()) {
public boolean onTouchEvent(MotionEvent event) { case MotionEvent.ACTION_DOWN:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: int x = (int) event.getX();
// 获取触摸点坐标并进行调整 int y = (int) event.getY();
int x = (int) event.getX(); x -= getTotalPaddingLeft();
int y = (int) event.getY(); y -= getTotalPaddingTop();
x -= getTotalPaddingLeft(); x += getScrollX();
y -= getTotalPaddingTop(); y += getScrollY();
x += getScrollX();
y += getScrollY(); Layout layout = getLayout();
int line = layout.getLineForVertical(y);
Layout layout = getLayout(); int off = layout.getOffsetForHorizontal(line, x);
// 获取触摸点所在的行 Selection.setSelection(getText(), off);
int line = layout.getLineForVertical(y); break;
// 获取触摸点在行中的偏移量 }
int off = layout.getOffsetForHorizontal(line, x);
// 设置选中文本 return super.onTouchEvent(event);
Selection.setSelection(getText(), off); }
break;
} @Override
return super.onTouchEvent(event); public boolean onKeyDown(int keyCode, KeyEvent event) {
} switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
// 处理按键按下事件 if (mOnTextViewChangeListener != null) {
@Override return false;
public boolean onKeyDown(int keyCode, KeyEvent event) { }
switch (keyCode) { break;
case KeyEvent.KEYCODE_ENTER: case KeyEvent.KEYCODE_DEL:
if (mOnTextViewChangeListener!= null) { mSelectionStartBeforeDelete = getSelectionStart();
return false; break;
} default:
break; break;
case KeyEvent.KEYCODE_DEL: }
// 记录删除操作前的选中文本起始位置 return super.onKeyDown(keyCode, event);
mSelectionStartBeforeDelete = getSelectionStart(); }
break;
default: @Override
break; public boolean onKeyUp(int keyCode, KeyEvent event) {
} switch(keyCode) {
return super.onKeyDown(keyCode, event); case KeyEvent.KEYCODE_DEL:
} if (mOnTextViewChangeListener != null) {
if (0 == mSelectionStartBeforeDelete && mIndex != 0) {
// 处理按键抬起事件 mOnTextViewChangeListener.onEditTextDelete(mIndex, getText().toString());
@Override return true;
public boolean onKeyUp(int keyCode, KeyEvent event) { }
switch (keyCode) { } else {
case KeyEvent.KEYCODE_DEL: Log.d(TAG, "OnTextViewChangeListener was not seted");
if (mOnTextViewChangeListener!= null) { }
// 如果起始位置为 0 且不是第一个 EditText则调用删除方法 break;
if (0 == mSelectionStartBeforeDelete && mIndex!= 0) { case KeyEvent.KEYCODE_ENTER:
mOnTextViewChangeListener.onEditTextDelete(mIndex, getText().toString()); if (mOnTextViewChangeListener != null) {
return true; int selectionStart = getSelectionStart();
} String text = getText().subSequence(selectionStart, length()).toString();
} else { setText(getText().subSequence(0, selectionStart));
Log.d(TAG, "OnTextViewChangeListener was not seted"); mOnTextViewChangeListener.onEditTextEnter(mIndex + 1, text);
} } else {
break; Log.d(TAG, "OnTextViewChangeListener was not seted");
case KeyEvent.KEYCODE_ENTER: }
if (mOnTextViewChangeListener!= null) { break;
int selectionStart = getSelectionStart(); default:
// 获取选中文本后的剩余文本 break;
String text = getText().subSequence(selectionStart, length()).toString(); }
// 设置 EditText 的文本为选中前的部分 return super.onKeyUp(keyCode, event);
setText(getText().subSequence(0, selectionStart)); }
// 调用添加新 EditText 的方法
mOnTextViewChangeListener.onEditTextEnter(mIndex + 1, text); @Override
} else { protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
Log.d(TAG, "OnTextViewChangeListener was not seted"); if (mOnTextViewChangeListener != null) {
} if (!focused && TextUtils.isEmpty(getText())) {
break; mOnTextViewChangeListener.onTextChange(mIndex, false);
default: } else {
break; mOnTextViewChangeListener.onTextChange(mIndex, true);
} }
return super.onKeyUp(keyCode, event); }
} super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
// 处理焦点变化事件
@Override @Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { protected void onCreateContextMenu(ContextMenu menu) {
if (mOnTextViewChangeListener!= null) { if (getText() instanceof Spanned) {
// 根据是否有焦点和文本是否为空,调用文本变化监听器方法 int selStart = getSelectionStart();
if (!focused && TextUtils.isEmpty(getText())) { int selEnd = getSelectionEnd();
mOnTextViewChangeListener.onTextChange(mIndex, false);
} else { int min = Math.min(selStart, selEnd);
mOnTextViewChangeListener.onTextChange(mIndex, true); int max = Math.max(selStart, selEnd);
}
} final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class);
super.onFocusChanged(focused, direction, previouslyFocusedRect); if (urls.length == 1) {
} int defaultResId = 0;
for(String schema: sSchemaActionResMap.keySet()) {
// 创建上下文菜单 if(urls[0].getURL().indexOf(schema) >= 0) {
@Override defaultResId = sSchemaActionResMap.get(schema);
protected void onCreateContextMenu(ContextMenu menu) { break;
if (getText() instanceof Spanned) { }
int selStart = getSelectionStart(); }
int selEnd = getSelectionEnd();
if (defaultResId == 0) {
int min = Math.min(selStart, selEnd); defaultResId = R.string.note_link_other;
int max = Math.max(selStart, selEnd); }
final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class); menu.add(0, 0, 0, defaultResId).setOnMenuItemClickListener(
if (urls.length == 1) { new OnMenuItemClickListener() {
int defaultResId = 0; public boolean onMenuItemClick(MenuItem item) {
for (String schema : sSchemaActionResMap.keySet()) { // goto a new intent
if (urls[0].getURL().indexOf(schema) >= 0) { urls[0].onClick(NoteEditText.this);
defaultResId = sSchemaActionResMap.get(schema); return true;
break; }
} });
} }
}
if (defaultResId == 0) { super.onCreateContextMenu(menu);
defaultResId = R.string.note_link_other; }
} }
menu.add(0, 0, 0, defaultResId).setOnMenuItemClickListener(
new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// 点击菜单项时触发链接的点击事件
urls[0].onClick(NoteEditText.this);
return true;
}
});
}
}
super.onCreateContextMenu(menu);
}
}

@ -14,247 +14,211 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.text.TextUtils; import android.text.TextUtils;
import net.micode.notes.data.Contact; import net.micode.notes.data.Contact;
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.tool.DataUtils; import net.micode.notes.tool.DataUtils;
// 定义一个名为 NoteItemData 的类,用于存储笔记项的数据
public class NoteItemData { public class NoteItemData {
// 定义查询投影,包含多个列 static final String [] PROJECTION = new String [] {
static final String[] PROJECTION = new String[]{ NoteColumns.ID,
NoteColumns.ID, NoteColumns.ALERTED_DATE,
NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID,
NoteColumns.BG_COLOR_ID, NoteColumns.CREATED_DATE,
NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT,
NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE,
NoteColumns.MODIFIED_DATE, NoteColumns.NOTES_COUNT,
NoteColumns.NOTES_COUNT, NoteColumns.PARENT_ID,
NoteColumns.PARENT_ID, NoteColumns.SNIPPET,
NoteColumns.SNIPPET, NoteColumns.TYPE,
NoteColumns.TYPE, NoteColumns.WIDGET_ID,
NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE,
NoteColumns.WIDGET_TYPE, };
};
private static final int ID_COLUMN = 0;
// 各个列的索引常量 private static final int ALERTED_DATE_COLUMN = 1;
private static final int ID_COLUMN = 0; private static final int BG_COLOR_ID_COLUMN = 2;
private static final int ALERTED_DATE_COLUMN = 1; private static final int CREATED_DATE_COLUMN = 3;
private static final int BG_COLOR_ID_COLUMN = 2; private static final int HAS_ATTACHMENT_COLUMN = 4;
private static final int CREATED_DATE_COLUMN = 3; private static final int MODIFIED_DATE_COLUMN = 5;
private static final int HAS_ATTACHMENT_COLUMN = 4; private static final int NOTES_COUNT_COLUMN = 6;
private static final int MODIFIED_DATE_COLUMN = 5; private static final int PARENT_ID_COLUMN = 7;
private static final int NOTES_COUNT_COLUMN = 6; private static final int SNIPPET_COLUMN = 8;
private static final int PARENT_ID_COLUMN = 7; private static final int TYPE_COLUMN = 9;
private static final int SNIPPET_COLUMN = 8; private static final int WIDGET_ID_COLUMN = 10;
private static final int TYPE_COLUMN = 9; private static final int WIDGET_TYPE_COLUMN = 11;
private static final int WIDGET_ID_COLUMN = 10;
private static final int WIDGET_TYPE_COLUMN = 11; private long mId;
private long mAlertDate;
// 笔记项的各个属性 private int mBgColorId;
private long mId; private long mCreatedDate;
private long mAlertDate; private boolean mHasAttachment;
private int mBgColorId; private long mModifiedDate;
private long mCreatedDate; private int mNotesCount;
private boolean mHasAttachment; private long mParentId;
private long mModifiedDate; private String mSnippet;
private int mNotesCount; private int mType;
private long mParentId; private int mWidgetId;
private String mSnippet; private int mWidgetType;
private int mType; private String mName;
private int mWidgetId; private String mPhoneNumber;
private int mWidgetType;
private String mName; private boolean mIsLastItem;
private String mPhoneNumber; private boolean mIsFirstItem;
private boolean mIsOnlyOneItem;
// 用于标记笔记项在列表中的位置相关属性 private boolean mIsOneNoteFollowingFolder;
private boolean mIsLastItem; private boolean mIsMultiNotesFollowingFolder;
private boolean mIsFirstItem;
private boolean mIsOnlyOneItem; public NoteItemData(Context context, Cursor cursor) {
private boolean mIsOneNoteFollowingFolder; mId = cursor.getLong(ID_COLUMN);
private boolean mIsMultiNotesFollowingFolder; mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN);
mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN);
// 构造函数,接受上下文和游标,用于从游标中提取数据初始化笔记项 mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN);
public NoteItemData(Context context, Cursor cursor) { mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0) ? true : false;
// 从游标中获取各个列的值并初始化相应的属性 mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN);
mId = cursor.getLong(ID_COLUMN); mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN);
mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN); mParentId = cursor.getLong(PARENT_ID_COLUMN);
mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN); mSnippet = cursor.getString(SNIPPET_COLUMN);
mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN); mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace(
mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0)? true : false; NoteEditActivity.TAG_UNCHECKED, "");
mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN); mType = cursor.getInt(TYPE_COLUMN);
mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN); mWidgetId = cursor.getInt(WIDGET_ID_COLUMN);
mParentId = cursor.getLong(PARENT_ID_COLUMN); mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN);
mSnippet = cursor.getString(SNIPPET_COLUMN);
// 处理片段内容,去除特定标记 mPhoneNumber = "";
mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace( if (mParentId == Notes.ID_CALL_RECORD_FOLDER) {
NoteEditActivity.TAG_UNCHECKED, ""); mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId);
mType = cursor.getInt(TYPE_COLUMN); if (!TextUtils.isEmpty(mPhoneNumber)) {
mWidgetId = cursor.getInt(WIDGET_ID_COLUMN); mName = Contact.getContact(context, mPhoneNumber);
mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN); if (mName == null) {
mName = mPhoneNumber;
mPhoneNumber = ""; }
// 如果父 ID 是通话记录文件夹的 ID则获取通话号码并设置名称 }
if (mParentId == Notes.ID_CALL_RECORD_FOLDER) { }
mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId);
if (!TextUtils.isEmpty(mPhoneNumber)) { if (mName == null) {
mName = Contact.getContact(context, mPhoneNumber); mName = "";
if (mName == null) { }
mName = mPhoneNumber; checkPostion(cursor);
} }
}
} private void checkPostion(Cursor cursor) {
mIsLastItem = cursor.isLast() ? true : false;
if (mName == null) { mIsFirstItem = cursor.isFirst() ? true : false;
mName = ""; mIsOnlyOneItem = (cursor.getCount() == 1);
} mIsMultiNotesFollowingFolder = false;
// 检查笔记项在游标中的位置 mIsOneNoteFollowingFolder = false;
checkPostion(cursor);
} if (mType == Notes.TYPE_NOTE && !mIsFirstItem) {
int position = cursor.getPosition();
// 检查笔记项在游标中的位置的私有方法 if (cursor.moveToPrevious()) {
private void checkPostion(Cursor cursor) { if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER
// 设置是否为最后一项、第一项、唯一项等标志 || cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) {
mIsLastItem = cursor.isLast()? true : false; if (cursor.getCount() > (position + 1)) {
mIsFirstItem = cursor.isFirst()? true : false; mIsMultiNotesFollowingFolder = true;
mIsOnlyOneItem = (cursor.getCount() == 1); } else {
mIsMultiNotesFollowingFolder = false; mIsOneNoteFollowingFolder = true;
mIsOneNoteFollowingFolder = false; }
}
// 如果笔记类型为普通笔记且不是第一项 if (!cursor.moveToNext()) {
if (mType == Notes.TYPE_NOTE &&!mIsFirstItem) { throw new IllegalStateException("cursor move to previous but can't move back");
int position = cursor.getPosition(); }
// 移动游标到前一项并检查其类型 }
if (cursor.moveToPrevious()) { }
if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER }
|| cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) {
if (cursor.getCount() > (position + 1)) { public boolean isOneFollowingFolder() {
mIsMultiNotesFollowingFolder = true; return mIsOneNoteFollowingFolder;
} else { }
mIsOneNoteFollowingFolder = true;
} public boolean isMultiFollowingFolder() {
} return mIsMultiNotesFollowingFolder;
// 尝试将游标移回原位,如果失败则抛出异常 }
if (!cursor.moveToNext()) {
throw new IllegalStateException("cursor move to previous but can't move back"); public boolean isLast() {
} return mIsLastItem;
} }
}
} public String getCallName() {
return mName;
// 判断是否有一个笔记项跟随在文件夹后面 }
public boolean isOneFollowingFolder() {
return mIsOneNoteFollowingFolder; public boolean isFirst() {
} return mIsFirstItem;
}
// 判断是否有多个笔记项跟随在文件夹后面
public boolean isMultiFollowingFolder() { public boolean isSingle() {
return mIsMultiNotesFollowingFolder; return mIsOnlyOneItem;
} }
// 判断是否为最后一项 public long getId() {
public boolean isLast() { return mId;
return mIsLastItem; }
}
public long getAlertDate() {
// 获取通话记录的名称 return mAlertDate;
public String getCallName() { }
return mName;
} public long getCreatedDate() {
return mCreatedDate;
// 判断是否为第一项 }
public boolean isFirst() {
return mIsFirstItem; public boolean hasAttachment() {
} return mHasAttachment;
}
// 判断是否为唯一项
public boolean isSingle() { public long getModifiedDate() {
return mIsOnlyOneItem; return mModifiedDate;
} }
// 获取笔记项的 ID public int getBgColorId() {
public long getId() { return mBgColorId;
return mId; }
}
public long getParentId() {
// 获取提醒日期 return mParentId;
public long getAlertDate() { }
return mAlertDate;
} public int getNotesCount() {
return mNotesCount;
// 获取创建日期 }
public long getCreatedDate() {
return mCreatedDate; public long getFolderId () {
} return mParentId;
}
// 判断是否有附件
public boolean hasAttachment() { public int getType() {
return mHasAttachment; return mType;
} }
// 获取修改日期 public int getWidgetType() {
public long getModifiedDate() { return mWidgetType;
return mModifiedDate; }
}
public int getWidgetId() {
// 获取背景颜色 ID return mWidgetId;
public int getBgColorId() { }
return mBgColorId;
} public String getSnippet() {
return mSnippet;
// 获取父 ID文件夹 ID }
public long getParentId() {
return mParentId; public boolean hasAlert() {
} return (mAlertDate > 0);
}
// 获取笔记数量
public int getNotesCount() { public boolean isCallRecord() {
return mNotesCount; return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));
} }
// 获取文件夹 ID public static int getNoteType(Cursor cursor) {
public long getFolderId() { return cursor.getInt(TYPE_COLUMN);
return mParentId; }
} }
// 获取笔记类型
public int getType() {
return mType;
}
// 获取小部件类型
public int getWidgetType() {
return mWidgetType;
}
// 获取小部件 ID
public int getWidgetId() {
return mWidgetId;
}
// 获取片段内容
public String getSnippet() {
return mSnippet;
}
// 判断是否有提醒
public boolean hasAlert() {
return (mAlertDate > 0);
}
// 判断是否为通话记录
public boolean isCallRecord() {
return (mParentId == Notes.ID_CALL_RECORD_FOLDER &&!TextUtils.isEmpty(mPhoneNumber));
}
// 静态方法,从游标中获取笔记类型
public static int getNoteType(Cursor cursor) {
return cursor.getInt(TYPE_COLUMN);
}
}

@ -14,198 +14,171 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.CursorAdapter; import android.widget.CursorAdapter;
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
// 定义一个名为 NotesListAdapter 的类,继承自 CursorAdapter
public class NotesListAdapter extends CursorAdapter { public class NotesListAdapter extends CursorAdapter {
private static final String TAG = "NotesListAdapter"; private static final String TAG = "NotesListAdapter";
private Context mContext; private Context mContext;
// 用于存储选中项位置和选中状态的哈希表 private HashMap<Integer, Boolean> mSelectedIndex;
private HashMap<Integer, Boolean> mSelectedIndex; private int mNotesCount;
// 笔记数量 private boolean mChoiceMode;
private int mNotesCount;
// 是否处于选择模式 public static class AppWidgetAttribute {
private boolean mChoiceMode; public int widgetId;
public int widgetType;
// 内部类,用于存储小部件的属性 };
public static class AppWidgetAttribute {
public int widgetId; public NotesListAdapter(Context context) {
public int widgetType; super(context, null);
}; mSelectedIndex = new HashMap<Integer, Boolean>();
mContext = context;
// 构造函数,接受上下文 mNotesCount = 0;
public NotesListAdapter(Context context) { }
super(context, null);
// 初始化选中项的哈希表 @Override
mSelectedIndex = new HashMap<Integer, Boolean>(); public View newView(Context context, Cursor cursor, ViewGroup parent) {
mContext = context; return new NotesListItem(context);
mNotesCount = 0; }
}
@Override
// 创建新视图的方法 public void bindView(View view, Context context, Cursor cursor) {
@Override if (view instanceof NotesListItem) {
public View newView(Context context, Cursor cursor, ViewGroup parent) { NoteItemData itemData = new NoteItemData(context, cursor);
return new NotesListItem(context); ((NotesListItem) view).bind(context, itemData, mChoiceMode,
} isSelectedItem(cursor.getPosition()));
}
// 绑定数据到视图的方法 }
@Override
public void bindView(View view, Context context, Cursor cursor) { public void setCheckedItem(final int position, final boolean checked) {
if (view instanceof NotesListItem) { mSelectedIndex.put(position, checked);
// 创建 NoteItemData 对象,用于存储笔记项的数据 notifyDataSetChanged();
NoteItemData itemData = new NoteItemData(context, cursor); }
// 绑定数据到 NotesListItem 视图
((NotesListItem) view).bind(context, itemData, mChoiceMode, public boolean isInChoiceMode() {
isSelectedItem(cursor.getPosition())); return mChoiceMode;
} }
}
public void setChoiceMode(boolean mode) {
// 设置指定位置的项为选中或未选中状态的方法 mSelectedIndex.clear();
public void setCheckedItem(final int position, final boolean checked) { mChoiceMode = mode;
mSelectedIndex.put(position, checked); }
// 通知数据改变,刷新视图
notifyDataSetChanged(); public void selectAll(boolean checked) {
} Cursor cursor = getCursor();
for (int i = 0; i < getCount(); i++) {
// 判断是否处于选择模式 if (cursor.moveToPosition(i)) {
public boolean isInChoiceMode() { if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) {
return mChoiceMode; setCheckedItem(i, checked);
} }
}
// 设置选择模式的方法 }
public void setChoiceMode(boolean mode) { }
// 清空选中项的哈希表
mSelectedIndex.clear(); public HashSet<Long> getSelectedItemIds() {
mChoiceMode = mode; HashSet<Long> itemSet = new HashSet<Long>();
} for (Integer position : mSelectedIndex.keySet()) {
if (mSelectedIndex.get(position) == true) {
// 全选或全不选的方法 Long id = getItemId(position);
public void selectAll(boolean checked) { if (id == Notes.ID_ROOT_FOLDER) {
Cursor cursor = getCursor(); Log.d(TAG, "Wrong item id, should not happen");
for (int i = 0; i < getCount(); i++) { } else {
if (cursor.moveToPosition(i)) { itemSet.add(id);
// 如果是笔记类型,则设置选中状态 }
if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { }
setCheckedItem(i, checked); }
}
} return itemSet;
} }
}
public HashSet<AppWidgetAttribute> getSelectedWidget() {
// 获取选中项的 ID 集合的方法 HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
public HashSet<Long> getSelectedItemIds() { for (Integer position : mSelectedIndex.keySet()) {
HashSet<Long> itemSet = new HashSet<Long>(); if (mSelectedIndex.get(position) == true) {
for (Integer position : mSelectedIndex.keySet()) { Cursor c = (Cursor) getItem(position);
if (mSelectedIndex.get(position) == true) { if (c != null) {
Long id = getItemId(position); AppWidgetAttribute widget = new AppWidgetAttribute();
if (id == Notes.ID_ROOT_FOLDER) { NoteItemData item = new NoteItemData(mContext, c);
Log.d(TAG, "Wrong item id, should not happen"); widget.widgetId = item.getWidgetId();
} else { widget.widgetType = item.getWidgetType();
itemSet.add(id); itemSet.add(widget);
} /**
} * Don't close cursor here, only the adapter could close it
} */
} else {
return itemSet; Log.e(TAG, "Invalid cursor");
} return null;
}
// 获取选中的小部件属性集合的方法 }
public HashSet<AppWidgetAttribute> getSelectedWidget() { }
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>(); return itemSet;
for (Integer position : mSelectedIndex.keySet()) { }
if (mSelectedIndex.get(position) == true) {
Cursor c = (Cursor) getItem(position); public int getSelectedCount() {
if (c!= null) { Collection<Boolean> values = mSelectedIndex.values();
AppWidgetAttribute widget = new AppWidgetAttribute(); if (null == values) {
NoteItemData item = new NoteItemData(mContext, c); return 0;
widget.widgetId = item.getWidgetId(); }
widget.widgetType = item.getWidgetType(); Iterator<Boolean> iter = values.iterator();
itemSet.add(widget); int count = 0;
/** while (iter.hasNext()) {
* Don't close cursor here, only the adapter could close it if (true == iter.next()) {
*/ count++;
} else { }
Log.e(TAG, "Invalid cursor"); }
return null; return count;
} }
}
} public boolean isAllSelected() {
return itemSet; int checkedCount = getSelectedCount();
} return (checkedCount != 0 && checkedCount == mNotesCount);
}
// 获取选中项的数量的方法
public int getSelectedCount() { public boolean isSelectedItem(final int position) {
Collection<Boolean> values = mSelectedIndex.values(); if (null == mSelectedIndex.get(position)) {
if (null == values) { return false;
return 0; }
} return mSelectedIndex.get(position);
Iterator<Boolean> iter = values.iterator(); }
int count = 0;
while (iter.hasNext()) { @Override
if (true == iter.next()) { protected void onContentChanged() {
count++; super.onContentChanged();
} calcNotesCount();
} }
return count;
} @Override
public void changeCursor(Cursor cursor) {
// 判断是否全选的方法 super.changeCursor(cursor);
public boolean isAllSelected() { calcNotesCount();
int checkedCount = getSelectedCount(); }
return (checkedCount!= 0 && checkedCount == mNotesCount);
} private void calcNotesCount() {
mNotesCount = 0;
// 判断指定位置的项是否被选中的方法 for (int i = 0; i < getCount(); i++) {
public boolean isSelectedItem(final int position) { Cursor c = (Cursor) getItem(i);
if (null == mSelectedIndex.get(position)) { if (c != null) {
return false; if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
} mNotesCount++;
return mSelectedIndex.get(position); }
} } else {
Log.e(TAG, "Invalid cursor");
// 当数据内容改变时调用的方法 return;
@Override }
protected void onContentChanged() { }
super.onContentChanged(); }
// 计算笔记数量 }
calcNotesCount();
}
// 更换游标时调用的方法
@Override
public void changeCursor(Cursor cursor) {
super.changeCursor(cursor);
// 计算笔记数量
calcNotesCount();
}
// 计算笔记数量的私有方法
private void calcNotesCount() {
mNotesCount = 0;
for (int i = 0; i < getCount(); i++) {
Cursor c = (Cursor) getItem(i);
if (c!= null) {
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
mNotesCount++;
}
} else {
Log.e(TAG, "Invalid cursor");
return;
}
}
}
}

@ -14,144 +14,109 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.content.Context; import android.content.Context;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.view.View; import android.view.View;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import net.micode.notes.R; import net.micode.notes.R;
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import net.micode.notes.tool.DataUtils; import net.micode.notes.tool.DataUtils;
import net.micode.notes.tool.ResourceParser.NoteItemBgResources; import net.micode.notes.tool.ResourceParser.NoteItemBgResources;
// 定义一个名为 NotesListItem 的类,继承自 LinearLayout
public class NotesListItem extends LinearLayout { public class NotesListItem extends LinearLayout {
// 提醒图标 private ImageView mAlert;
private ImageView mAlert; private TextView mTitle;
// 标题文本视图 private TextView mTime;
private TextView mTitle; private TextView mCallName;
// 时间文本视图 private NoteItemData mItemData;
private TextView mTime; private CheckBox mCheckBox;
// 通话记录名称文本视图
private TextView mCallName; public NotesListItem(Context context) {
// 复选框 super(context);
private NoteItemData mItemData; inflate(context, R.layout.note_item, this);
private CheckBox mCheckBox; mAlert = (ImageView) findViewById(R.id.iv_alert_icon);
mTitle = (TextView) findViewById(R.id.tv_title);
// 构造函数,接受上下文 mTime = (TextView) findViewById(R.id.tv_time);
public NotesListItem(Context context) { mCallName = (TextView) findViewById(R.id.tv_name);
super(context); mCheckBox = (CheckBox) findViewById(android.R.id.checkbox);
// 从布局文件中填充视图 }
inflate(context, R.layout.note_item, this);
mAlert = (ImageView) findViewById(R.id.iv_alert_icon); public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
mTitle = (TextView) findViewById(R.id.tv_title); if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
mTime = (TextView) findViewById(R.id.tv_time); mCheckBox.setVisibility(View.VISIBLE);
mCallName = (TextView) findViewById(R.id.tv_name); mCheckBox.setChecked(checked);
mCheckBox = (CheckBox) findViewById(android.R.id.checkbox); } else {
} mCheckBox.setVisibility(View.GONE);
}
// 绑定数据到视图的方法
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) { mItemData = data;
// 如果处于选择模式且数据类型为笔记类型 if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
if (choiceMode && data.getType() == Notes.TYPE_NOTE) { mCallName.setVisibility(View.GONE);
// 显示复选框并设置选中状态 mAlert.setVisibility(View.VISIBLE);
mCheckBox.setVisibility(View.VISIBLE); mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
mCheckBox.setChecked(checked); mTitle.setText(context.getString(R.string.call_record_folder_name)
} else { + context.getString(R.string.format_folder_files_count, data.getNotesCount()));
// 隐藏复选框 mAlert.setImageResource(R.drawable.call_record);
mCheckBox.setVisibility(View.GONE); } else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) {
} mCallName.setVisibility(View.VISIBLE);
mCallName.setText(data.getCallName());
mItemData = data; mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem);
// 如果是通话记录文件夹 mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) { if (data.hasAlert()) {
// 隐藏通话记录名称视图 mAlert.setImageResource(R.drawable.clock);
mCallName.setVisibility(View.GONE); mAlert.setVisibility(View.VISIBLE);
// 显示提醒图标 } else {
mAlert.setVisibility(View.VISIBLE); mAlert.setVisibility(View.GONE);
// 设置标题文本外观和内容 }
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); } else {
mTitle.setText(context.getString(R.string.call_record_folder_name) mCallName.setVisibility(View.GONE);
+ context.getString(R.string.format_folder_files_count, data.getNotesCount())); mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
// 设置提醒图标资源为通话记录图标
mAlert.setImageResource(R.drawable.call_record); if (data.getType() == Notes.TYPE_FOLDER) {
} else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) { mTitle.setText(data.getSnippet()
// 显示通话记录名称视图 + context.getString(R.string.format_folder_files_count,
mCallName.setVisibility(View.VISIBLE); data.getNotesCount()));
// 设置通话记录名称文本 mAlert.setVisibility(View.GONE);
mCallName.setText(data.getCallName()); } else {
// 设置标题文本外观 mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
mTitle.setTextAppearance(context, R.style.TextAppearanceSecondaryItem); if (data.hasAlert()) {
// 设置标题文本为格式化后的片段内容 mAlert.setImageResource(R.drawable.clock);
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); mAlert.setVisibility(View.VISIBLE);
// 如果有提醒,则显示提醒图标 } else {
if (data.hasAlert()) { mAlert.setVisibility(View.GONE);
mAlert.setImageResource(R.drawable.clock); }
mAlert.setVisibility(View.VISIBLE); }
} else { }
// 没有提醒则隐藏提醒图标 mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
mAlert.setVisibility(View.GONE);
} setBackground(data);
} else { }
// 隐藏通话记录名称视图
mCallName.setVisibility(View.GONE); private void setBackground(NoteItemData data) {
// 设置标题文本外观 int id = data.getBgColorId();
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); if (data.getType() == Notes.TYPE_NOTE) {
if (data.isSingle() || data.isOneFollowingFolder()) {
if (data.getType() == Notes.TYPE_FOLDER) { setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id));
// 如果是文件夹类型,设置标题文本为片段内容加上文件数量 } else if (data.isLast()) {
mTitle.setText(data.getSnippet() setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id));
+ context.getString(R.string.format_folder_files_count, } else if (data.isFirst() || data.isMultiFollowingFolder()) {
data.getNotesCount())); setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id));
// 隐藏提醒图标 } else {
mAlert.setVisibility(View.GONE); setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id));
} else { }
// 如果是笔记类型,设置标题文本为格式化后的片段内容 } else {
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); setBackgroundResource(NoteItemBgResources.getFolderBgRes());
// 如果有提醒,则显示提醒图标 }
if (data.hasAlert()) { }
mAlert.setImageResource(R.drawable.clock);
mAlert.setVisibility(View.VISIBLE); public NoteItemData getItemData() {
} else { return mItemData;
// 没有提醒则隐藏提醒图标 }
mAlert.setVisibility(View.GONE); }
}
}
}
// 设置时间文本为相对时间
mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
// 设置背景
setBackground(data);
}
// 设置背景的私有方法
private void setBackground(NoteItemData data) {
int id = data.getBgColorId();
if (data.getType() == Notes.TYPE_NOTE) {
// 根据不同的位置状态设置不同的笔记背景资源
if (data.isSingle() || data.isOneFollowingFolder()) {
setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id));
} else if (data.isLast()) {
setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id));
} else if (data.isFirst() || data.isMultiFollowingFolder()) {
setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id));
} else {
setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id));
}
} else {
// 设置文件夹的背景资源
setBackgroundResource(NoteItemBgResources.getFolderBgRes());
}
}
// 获取当前项的数据对象
public NoteItemData getItemData() {
return mItemData;
}
}

@ -14,388 +14,367 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.app.ActionBar; import android.app.ActionBar;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener; import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory; import android.preference.PreferenceCategory;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import net.micode.notes.R; import net.micode.notes.R;
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.gtask.remote.GTaskSyncService; import net.micode.notes.gtask.remote.GTaskSyncService;
// 定义一个名为 NotesPreferenceActivity 的类,继承自 PreferenceActivity
public class NotesPreferenceActivity extends PreferenceActivity { public class NotesPreferenceActivity extends PreferenceActivity {
public static final String PREFERENCE_NAME = "notes_preferences"; public static final String PREFERENCE_NAME = "notes_preferences";
// 同步账户名称的偏好键名
public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name"; public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name";
// 最后同步时间的偏好键名
public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time"; public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time";
// 设置背景颜色随机出现的偏好键名
public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear"; public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear";
// 同步账户偏好类别键名
private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key"; private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key";
// 权限过滤器键名
private static final String AUTHORITIES_FILTER_KEY = "authorities"; private static final String AUTHORITIES_FILTER_KEY = "authorities";
// 账户偏好类别对象 private PreferenceCategory mAccountCategory;
private PreferenceCategory mAccountCategory;
// 广播接收器对象 private GTaskReceiver mReceiver;
private GTaskReceiver mReceiver;
// 原始账户数组 private Account[] mOriAccounts;
private Account[] mOriAccounts;
// 是否添加了账户的标志 private boolean mHasAddedAccount;
private boolean mHasAddedAccount;
@Override
// 活动创建时调用的方法 protected void onCreate(Bundle icicle) {
@Override super.onCreate(icicle);
protected void onCreate(Bundle icicle) {
super.onCreate(icicle); /* using the app icon for navigation */
getActionBar().setDisplayHomeAsUpEnabled(true);
/* using the app icon for navigation */
getActionBar().setDisplayHomeAsUpEnabled(true); addPreferencesFromResource(R.xml.preferences);
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY);
// 从资源文件中加载偏好设置 mReceiver = new GTaskReceiver();
addPreferencesFromResource(R.xml.preferences); IntentFilter filter = new IntentFilter();
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY); filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME);
mReceiver = new GTaskReceiver(); registerReceiver(mReceiver, filter);
IntentFilter filter = new IntentFilter();
filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME); mOriAccounts = null;
// 注册广播接收器 View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null);
registerReceiver(mReceiver, filter); getListView().addHeaderView(header, null, true);
}
mOriAccounts = null;
// 加载头部视图 @Override
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null); protected void onResume() {
getListView().addHeaderView(header, null, true); super.onResume();
}
// need to set sync account automatically if user has added a new
// 活动恢复时调用的方法 // account
@Override if (mHasAddedAccount) {
protected void onResume() { Account[] accounts = getGoogleAccounts();
super.onResume(); if (mOriAccounts != null && accounts.length > mOriAccounts.length) {
for (Account accountNew : accounts) {
// 如果添加了账户,则需要自动设置同步账户 boolean found = false;
if (mHasAddedAccount) { for (Account accountOld : mOriAccounts) {
Account[] accounts = getGoogleAccounts(); if (TextUtils.equals(accountOld.name, accountNew.name)) {
if (mOriAccounts!= null && accounts.length > mOriAccounts.length) { found = true;
for (Account accountNew : accounts) { break;
boolean found = false; }
for (Account accountOld : mOriAccounts) { }
if (TextUtils.equals(accountOld.name, accountNew.name)) { if (!found) {
found = true; setSyncAccount(accountNew.name);
break; break;
} }
} }
if (!found) { }
setSyncAccount(accountNew.name); }
break;
} refreshUI();
} }
}
} @Override
protected void onDestroy() {
// 刷新用户界面 if (mReceiver != null) {
refreshUI(); unregisterReceiver(mReceiver);
} }
super.onDestroy();
// 活动销毁时调用的方法 }
@Override
protected void onDestroy() { private void loadAccountPreference() {
if (mReceiver!= null) { mAccountCategory.removeAll();
// 注销广播接收器
unregisterReceiver(mReceiver); Preference accountPref = new Preference(this);
} final String defaultAccount = getSyncAccountName(this);
super.onDestroy(); accountPref.setTitle(getString(R.string.preferences_account_title));
} accountPref.setSummary(getString(R.string.preferences_account_summary));
accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
// 加载账户偏好设置的方法 public boolean onPreferenceClick(Preference preference) {
private void loadAccountPreference() { if (!GTaskSyncService.isSyncing()) {
mAccountCategory.removeAll(); if (TextUtils.isEmpty(defaultAccount)) {
// the first time to set account
Preference accountPref = new Preference(this); showSelectAccountAlertDialog();
final String defaultAccount = getSyncAccountName(this); } else {
accountPref.setTitle(getString(R.string.preferences_account_title)); // if the account has already been set, we need to promp
accountPref.setSummary(getString(R.string.preferences_account_summary)); // user about the risk
accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { showChangeAccountConfirmAlertDialog();
public boolean onPreferenceClick(Preference preference) { }
if (!GTaskSyncService.isSyncing()) { } else {
if (TextUtils.isEmpty(defaultAccount)) { Toast.makeText(NotesPreferenceActivity.this,
// 如果是第一次设置账户,则显示选择账户的警告对话框 R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
showSelectAccountAlertDialog();
} else {
// 如果账户已经设置,则显示更改账户确认的警告对话框
showChangeAccountConfirmAlertDialog();
}
} else {
// 如果正在同步,则显示不能更改账户的吐司消息
Toast.makeText(NotesPreferenceActivity.this,
R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
.show(); .show();
} }
return true; return true;
} }
}); });
mAccountCategory.addPreference(accountPref); mAccountCategory.addPreference(accountPref);
} }
// 加载同步按钮的方法 private void loadSyncButton() {
private void loadSyncButton() { Button syncButton = (Button) findViewById(R.id.preference_sync_button);
Button syncButton = (Button) findViewById(R.id.preference_sync_button); TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
// set button state
// 设置按钮状态 if (GTaskSyncService.isSyncing()) {
if (GTaskSyncService.isSyncing()) { syncButton.setText(getString(R.string.preferences_button_sync_cancel));
syncButton.setText(getString(R.string.preferences_button_sync_cancel)); syncButton.setOnClickListener(new View.OnClickListener() {
syncButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
public void onClick(View v) { GTaskSyncService.cancelSync(NotesPreferenceActivity.this);
GTaskSyncService.cancelSync(NotesPreferenceActivity.this); }
} });
}); } else {
} else { syncButton.setText(getString(R.string.preferences_button_sync_immediately));
syncButton.setText(getString(R.string.preferences_button_sync_immediately)); syncButton.setOnClickListener(new View.OnClickListener() {
syncButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
public void onClick(View v) { GTaskSyncService.startSync(NotesPreferenceActivity.this);
GTaskSyncService.startSync(NotesPreferenceActivity.this); }
} });
}); }
} syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this)));
syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this)));
// set last sync time
// 设置最后同步时间文本 if (GTaskSyncService.isSyncing()) {
if (GTaskSyncService.isSyncing()) { lastSyncTimeView.setText(GTaskSyncService.getProgressString());
lastSyncTimeView.setText(GTaskSyncService.getProgressString()); lastSyncTimeView.setVisibility(View.VISIBLE);
lastSyncTimeView.setVisibility(View.VISIBLE); } else {
} else { long lastSyncTime = getLastSyncTime(this);
long lastSyncTime = getLastSyncTime(this); if (lastSyncTime != 0) {
if (lastSyncTime!= 0) { lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time,
lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time, DateFormat.format(getString(R.string.preferences_last_sync_time_format),
DateFormat.format(getString(R.string.preferences_last_sync_time_format), lastSyncTime)));
lastSyncTime))); lastSyncTimeView.setVisibility(View.VISIBLE);
lastSyncTimeView.setVisibility(View.VISIBLE); } else {
} else { lastSyncTimeView.setVisibility(View.GONE);
lastSyncTimeView.setVisibility(View.GONE); }
} }
} }
}
private void refreshUI() {
// 刷新用户界面的方法 loadAccountPreference();
private void refreshUI() { loadSyncButton();
loadAccountPreference(); }
loadSyncButton();
} private void showSelectAccountAlertDialog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// 显示选择账户警告对话框的方法
private void showSelectAccountAlertDialog() { View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null); TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title); subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips));
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle); dialogBuilder.setCustomTitle(titleView);
subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips)); dialogBuilder.setPositiveButton(null, null);
dialogBuilder.setCustomTitle(titleView); Account[] accounts = getGoogleAccounts();
dialogBuilder.setPositiveButton(null, null); String defAccount = getSyncAccountName(this);
Account[] accounts = getGoogleAccounts(); mOriAccounts = accounts;
String defAccount = getSyncAccountName(this); mHasAddedAccount = false;
mOriAccounts = accounts; if (accounts.length > 0) {
mHasAddedAccount = false; CharSequence[] items = new CharSequence[accounts.length];
final CharSequence[] itemMapping = items;
if (accounts.length > 0) { int checkedItem = -1;
CharSequence[] items = new CharSequence[accounts.length]; int index = 0;
final CharSequence[] itemMapping = items; for (Account account : accounts) {
int checkedItem = -1; if (TextUtils.equals(account.name, defAccount)) {
int index = 0; checkedItem = index;
for (Account account : accounts) { }
if (TextUtils.equals(account.name, defAccount)) { items[index++] = account.name;
checkedItem = index; }
} dialogBuilder.setSingleChoiceItems(items, checkedItem,
items[index++] = account.name; new DialogInterface.OnClickListener() {
} public void onClick(DialogInterface dialog, int which) {
dialogBuilder.setSingleChoiceItems(items, checkedItem, setSyncAccount(itemMapping[which].toString());
new DialogInterface.OnClickListener() { dialog.dismiss();
public void onClick(DialogInterface dialog, int which) { refreshUI();
setSyncAccount(itemMapping[which].toString()); }
dialog.dismiss(); });
refreshUI(); }
}
}); View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null);
} dialogBuilder.setView(addAccountView);
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null); final AlertDialog dialog = dialogBuilder.show();
dialogBuilder.setView(addAccountView); addAccountView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final AlertDialog dialog = dialogBuilder.show(); mHasAddedAccount = true;
addAccountView.setOnClickListener(new View.OnClickListener() { Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
public void onClick(View v) { intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] {
mHasAddedAccount = true; "gmail-ls"
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS"); });
intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] { startActivityForResult(intent, -1);
"gmail-ls" dialog.dismiss();
}); }
startActivityForResult(intent, -1); });
dialog.dismiss(); }
}
}); private void showChangeAccountConfirmAlertDialog() {
} AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// 显示更改账户确认警告对话框的方法 View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
private void showChangeAccountConfirmAlertDialog() { TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
getSyncAccountName(this)));
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null); TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title); subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg));
titleTextView.setText(getString(R.string.preferences_dialog_change_account_title, dialogBuilder.setCustomTitle(titleView);
getSyncAccountName(this)));
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle); CharSequence[] menuItemArray = new CharSequence[] {
subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg)); getString(R.string.preferences_menu_change_account),
dialogBuilder.setCustomTitle(titleView); getString(R.string.preferences_menu_remove_account),
getString(R.string.preferences_menu_cancel)
CharSequence[] menuItemArray = new CharSequence[] { };
getString(R.string.preferences_menu_change_account), dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() {
getString(R.string.preferences_menu_remove_account), public void onClick(DialogInterface dialog, int which) {
getString(R.string.preferences_menu_cancel) if (which == 0) {
}; showSelectAccountAlertDialog();
dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() { } else if (which == 1) {
public void onClick(DialogInterface dialog, int which) { removeSyncAccount();
if (which == 0) { refreshUI();
showSelectAccountAlertDialog(); }
} else if (which == 1) { }
removeSyncAccount(); });
refreshUI(); dialogBuilder.show();
} }
}
}); private Account[] getGoogleAccounts() {
dialogBuilder.show(); AccountManager accountManager = AccountManager.get(this);
} return accountManager.getAccountsByType("com.google");
}
// 获取谷歌账户数组的方法
private Account[] getGoogleAccounts() { private void setSyncAccount(String account) {
AccountManager accountManager = AccountManager.get(this); if (!getSyncAccountName(this).equals(account)) {
return accountManager.getAccountsByType("com.google"); SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
} SharedPreferences.Editor editor = settings.edit();
if (account != null) {
// 设置同步账户的方法 editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account);
private void setSyncAccount(String account) { } else {
if (!getSyncAccountName(this).equals(account)) { editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); }
SharedPreferences.Editor editor = settings.edit(); editor.commit();
if (account!= null) {
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account); // clean up last sync time
} else { setLastSyncTime(this, 0);
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
} // clean up local gtask related info
editor.commit(); new Thread(new Runnable() {
public void run() {
// 清理最后同步时间 ContentValues values = new ContentValues();
setLastSyncTime(this, 0); values.put(NoteColumns.GTASK_ID, "");
values.put(NoteColumns.SYNC_ID, 0);
// 清理本地与谷歌任务相关的信息 getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
new Thread(new Runnable() { }
public void run() { }).start();
ContentValues values = new ContentValues();
values.put(NoteColumns.GTASK_ID, ""); Toast.makeText(NotesPreferenceActivity.this,
values.put(NoteColumns.SYNC_ID, 0); getString(R.string.preferences_toast_success_set_accout, account),
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null); Toast.LENGTH_SHORT).show();
} }
}).start(); }
Toast.makeText(NotesPreferenceActivity.this, private void removeSyncAccount() {
getString(R.string.preferences_toast_success_set_accout, account), SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
Toast.LENGTH_SHORT).show(); SharedPreferences.Editor editor = settings.edit();
} if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) {
} editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME);
}
// 移除同步账户的方法 if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) {
private void removeSyncAccount() { editor.remove(PREFERENCE_LAST_SYNC_TIME);
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); }
SharedPreferences.Editor editor = settings.edit(); editor.commit();
if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) {
editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME); // clean up local gtask related info
} new Thread(new Runnable() {
if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) { public void run() {
editor.remove(PREFERENCE_LAST_SYNC_TIME); ContentValues values = new ContentValues();
} values.put(NoteColumns.GTASK_ID, "");
editor.commit(); values.put(NoteColumns.SYNC_ID, 0);
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
// 清理本地与谷歌任务相关的信息 }
new Thread(new Runnable() { }).start();
public void run() { }
ContentValues values = new ContentValues();
values.put(NoteColumns.GTASK_ID, ""); public static String getSyncAccountName(Context context) {
values.put(NoteColumns.SYNC_ID, 0); SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null); Context.MODE_PRIVATE);
} return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
}).start(); }
}
public static void setLastSyncTime(Context context, long time) {
// 获取同步账户名称的静态方法 SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
public static String getSyncAccountName(Context context) { Context.MODE_PRIVATE);
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, SharedPreferences.Editor editor = settings.edit();
Context.MODE_PRIVATE); editor.putLong(PREFERENCE_LAST_SYNC_TIME, time);
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); editor.commit();
} }
// 设置最后同步时间的静态方法 public static long getLastSyncTime(Context context) {
public static void setLastSyncTime(Context context, long time) { SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
Context.MODE_PRIVATE); return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0);
SharedPreferences.Editor editor = settings.edit(); }
editor.putLong(PREFERENCE_LAST_SYNC_TIME, time);
editor.commit(); private class GTaskReceiver extends BroadcastReceiver {
}
@Override
// 获取最后同步时间的静态方法 public void onReceive(Context context, Intent intent) {
public static long getLastSyncTime(Context context) { refreshUI();
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) {
Context.MODE_PRIVATE); TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0); syncStatus.setText(intent
}
// 广播接收器内部类
private class GTaskReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
refreshUI();
if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) {
TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
syncStatus.setText(intent
.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG)); .getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG));
} }
} }
} }
// 选项菜单项被选择时调用的方法 public boolean onOptionsItemSelected(MenuItem item) {
public boolean onOptionsItemSelected(
switch (item.getItemId()) { switch (item.getItemId()) {
case android.R.id.home: case android.R.id.home:
Intent intent = new Intent(this, NotesListActivity.class); Intent intent = new Intent(this, NotesListActivity.class);

@ -2,7 +2,6 @@
# as it contains information specific to your local configuration. # as it contains information specific to your local configuration.
# #
# Location of the SDK. This is only used by Gradle. # Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the #
# header note. #Sun Sep 08 13:35:35 CST 2024
#Sun Sep 22 17:32:55 CST 2024 sdk.dir=D\:\\Android\\Sdk
sdk.dir=D\:\\sdk

Loading…
Cancel
Save