yangmingrui

main
yangmingrui 7 months ago
parent 5c2c3cf910
commit b72f772fd9

@ -14,331 +14,316 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.tool; package net.micode.notes.tool;
import android.content.Context; public class BackupUtils {
import android.database.Cursor; private static final String TAG = "BackupUtils";
import android.os.Environment; // Singleton stuff
import android.text.TextUtils; private static BackupUtils sInstance; //类里面为什么可以定义自身类的对象?
import android.text.format.DateFormat;
import android.util.Log; public static synchronized BackupUtils getInstance(Context context) {
//ynchronized 关键字,代表这个方法加锁,相当于不管哪一个线程例如线程A
import net.micode.notes.R; //运行到这个方法时,都要检查有没有其它线程B或者C、 D等正在用这个方法(或者该类的其他同步方法)有的话要等正在使用synchronized方法的线程B或者C 、D运行完这个方法后再运行此线程A,没有的话,锁定调用者,然后直接运行。
import net.micode.notes.data.Notes; //它包括两种用法synchronized 方法和 synchronized 块。
import net.micode.notes.data.Notes.DataColumns; if (sInstance == null) {
import net.micode.notes.data.Notes.DataConstants; //如果当前备份不存在,则新声明一个
import net.micode.notes.data.Notes.NoteColumns; sInstance = new BackupUtils(context);
}
import java.io.File; return sInstance;
import java.io.FileNotFoundException; }
import java.io.FileOutputStream;
import java.io.IOException; /**
import java.io.PrintStream; * Following states are signs to represents backup or restore
* status
*/
public class BackupUtils { // Currently, the sdcard is not mounted SD卡没有被装入手机
private static final String TAG = "BackupUtils"; public static final int STATE_SD_CARD_UNMOUONTED = 0;
// Singleton stuff // The backup file not exist 备份文件夹不存在
private static BackupUtils sInstance; public static final int STATE_BACKUP_FILE_NOT_EXIST = 1;
// The data is not well formated, may be changed by other programs 数据已被破坏,可能被修改
public static synchronized BackupUtils getInstance(Context context) { public static final int STATE_DATA_DESTROIED = 2;
if (sInstance == null) { // Some run-time exception which causes restore or backup fails 超时异常
sInstance = new BackupUtils(context); public static final int STATE_SYSTEM_ERROR = 3;
} // Backup or restore success 成功存储
return sInstance; public static final int STATE_SUCCESS = 4;
}
private TextExport mTextExport;
/**
* Following states are signs to represents backup or restore private BackupUtils(Context context) { //初始化函数
* status mTextExport = new TextExport(context);
*/ }
// Currently, the sdcard is not mounted
public static final int STATE_SD_CARD_UNMOUONTED = 0; private static boolean externalStorageAvailable() { //外部存储功能是否可用
// The backup file not exist return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; }
// The data is not well formated, may be changed by other programs
public static final int STATE_DATA_DESTROIED = 2; public int exportToText() {
// Some run-time exception which causes restore or backup fails return mTextExport.exportToText();
public static final int STATE_SYSTEM_ERROR = 3; }
// Backup or restore success
public static final int STATE_SUCCESS = 4; public String getExportedTextFileName() {
return mTextExport.mFileName;
private TextExport mTextExport; }
private BackupUtils(Context context) { public String getExportedTextFileDir() {
mTextExport = new TextExport(context); return mTextExport.mFileDirectory;
} }
private static boolean externalStorageAvailable() { private static class TextExport {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); private static final String[] NOTE_PROJECTION = {
} NoteColumns.ID,
NoteColumns.MODIFIED_DATE,
public int exportToText() { NoteColumns.SNIPPET,
return mTextExport.exportToText(); NoteColumns.TYPE
} };
public String getExportedTextFileName() { private static final int NOTE_COLUMN_ID = 0;
return mTextExport.mFileName;
} private static final int NOTE_COLUMN_MODIFIED_DATE = 1;
public String getExportedTextFileDir() { private static final int NOTE_COLUMN_SNIPPET = 2;
return mTextExport.mFileDirectory;
} private static final String[] DATA_PROJECTION = {
DataColumns.CONTENT,
private static class TextExport { DataColumns.MIME_TYPE,
private static final String[] NOTE_PROJECTION = { DataColumns.DATA1,
NoteColumns.ID, DataColumns.DATA2,
NoteColumns.MODIFIED_DATE, DataColumns.DATA3,
NoteColumns.SNIPPET, DataColumns.DATA4,
NoteColumns.TYPE };
};
private static final int DATA_COLUMN_CONTENT = 0;
private static final int NOTE_COLUMN_ID = 0;
private static final int DATA_COLUMN_MIME_TYPE = 1;
private static final int NOTE_COLUMN_MODIFIED_DATE = 1;
private static final int DATA_COLUMN_CALL_DATE = 2;
private static final int NOTE_COLUMN_SNIPPET = 2;
private static final int DATA_COLUMN_PHONE_NUMBER = 4;
private static final String[] DATA_PROJECTION = {
DataColumns.CONTENT, private final String [] TEXT_FORMAT;
DataColumns.MIME_TYPE, private static final int FORMAT_FOLDER_NAME = 0;
DataColumns.DATA1, private static final int FORMAT_NOTE_DATE = 1;
DataColumns.DATA2, private static final int FORMAT_NOTE_CONTENT = 2;
DataColumns.DATA3,
DataColumns.DATA4, private Context mContext;
}; private String mFileName;
private String mFileDirectory;
private static final int DATA_COLUMN_CONTENT = 0;
public TextExport(Context context) {
private static final int DATA_COLUMN_MIME_TYPE = 1; TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note);
mContext = context;
private static final int DATA_COLUMN_CALL_DATE = 2; mFileName = ""; //为什么为空?
mFileDirectory = "";
private static final int DATA_COLUMN_PHONE_NUMBER = 4; }
private final String [] TEXT_FORMAT; private String getFormat(int id) { //获取文本的组成部分
private static final int FORMAT_FOLDER_NAME = 0; return TEXT_FORMAT[id];
private static final int FORMAT_NOTE_DATE = 1; }
private static final int FORMAT_NOTE_CONTENT = 2;
/**
private Context mContext; * Export the folder identified by folder id to text
private String mFileName; */
private String mFileDirectory; private void exportFolderToText(String folderId, PrintStream ps) {
// Query notes belong to this folder 通过查询parent id是文件夹id的note来选出制定ID文件夹下的Note
public TextExport(Context context) { Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI,
TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] {
mContext = context; folderId
mFileName = ""; }, null);
mFileDirectory = "";
} if (notesCursor != null) {
if (notesCursor.moveToFirst()) {
private String getFormat(int id) { do {
return TEXT_FORMAT[id]; // Print note's last modified date ps里面保存有这份note的日期
} ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
mContext.getString(R.string.format_datetime_mdhm),
/** notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
* Export the folder identified by folder id to text // Query data belong to this note
*/ String noteId = notesCursor.getString(NOTE_COLUMN_ID);
private void exportFolderToText(String folderId, PrintStream ps) { exportNoteToText(noteId, ps); //将文件导出到text
// Query notes belong to this folder } while (notesCursor.moveToNext());
Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, }
NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { notesCursor.close();
folderId }
}, null); }
if (notesCursor != null) { /**
if (notesCursor.moveToFirst()) { * Export note identified by id to a print stream
do { */
// Print note's last modified date private void exportNoteToText(String noteId, PrintStream ps) {
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI,
mContext.getString(R.string.format_datetime_mdhm), DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {
notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); noteId
// Query data belong to this note }, null);
String noteId = notesCursor.getString(NOTE_COLUMN_ID);
exportNoteToText(noteId, ps); if (dataCursor != null) { //利用光标来扫描内容区别为callnote和note两种靠ps.printline输出
} while (notesCursor.moveToNext()); if (dataCursor.moveToFirst()) {
} do {
notesCursor.close(); String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
} if (DataConstants.CALL_NOTE.equals(mimeType)) {
} // Print phone number
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
/** long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE);
* Export note identified by id to a print stream String location = dataCursor.getString(DATA_COLUMN_CONTENT);
*/
private void exportNoteToText(String noteId, PrintStream ps) { if (!TextUtils.isEmpty(phoneNumber)) { //判断是否为空字符
Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { phoneNumber));
noteId }
}, null); // Print call date
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat
if (dataCursor != null) { .format(mContext.getString(R.string.format_datetime_mdhm),
if (dataCursor.moveToFirst()) { callDate)));
do { // Print call attachment location
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); if (!TextUtils.isEmpty(location)) {
if (DataConstants.CALL_NOTE.equals(mimeType)) { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
// Print phone number location));
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); }
long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); } else if (DataConstants.NOTE.equals(mimeType)) {
String location = dataCursor.getString(DATA_COLUMN_CONTENT); String content = dataCursor.getString(DATA_COLUMN_CONTENT);
if (!TextUtils.isEmpty(content)) {
if (!TextUtils.isEmpty(phoneNumber)) { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), content));
phoneNumber)); }
} }
// Print call date } while (dataCursor.moveToNext());
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat }
.format(mContext.getString(R.string.format_datetime_mdhm), dataCursor.close();
callDate))); }
// Print call attachment location // print a line separator between note
if (!TextUtils.isEmpty(location)) { try {
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), ps.write(new byte[] {
location)); Character.LINE_SEPARATOR, Character.LETTER_NUMBER
} });
} else if (DataConstants.NOTE.equals(mimeType)) { } catch (IOException e) {
String content = dataCursor.getString(DATA_COLUMN_CONTENT); Log.e(TAG, e.toString());
if (!TextUtils.isEmpty(content)) { }
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), }
content));
} /**
} * Note will be exported as text which is user readable
} while (dataCursor.moveToNext()); */
} public int exportToText() { //总函数调用上面的exportFolder和exportNote
dataCursor.close(); if (!externalStorageAvailable()) {
} Log.d(TAG, "Media was not mounted");
// print a line separator between note return STATE_SD_CARD_UNMOUONTED;
try { }
ps.write(new byte[] {
Character.LINE_SEPARATOR, Character.LETTER_NUMBER PrintStream ps = getExportToTextPrintStream();
}); if (ps == null) {
} catch (IOException e) { Log.e(TAG, "get print stream error");
Log.e(TAG, e.toString()); return STATE_SYSTEM_ERROR;
} }
} // First export folder and its notes 导出文件夹,就是导出里面包含的便签
Cursor folderCursor = mContext.getContentResolver().query(
/** Notes.CONTENT_NOTE_URI,
* Note will be exported as text which is user readable NOTE_PROJECTION,
*/ "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND "
public int exportToText() { + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR "
if (!externalStorageAvailable()) { + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null);
Log.d(TAG, "Media was not mounted");
return STATE_SD_CARD_UNMOUONTED; if (folderCursor != null) {
} if (folderCursor.moveToFirst()) {
do {
PrintStream ps = getExportToTextPrintStream(); // Print folder's name
if (ps == null) { String folderName = "";
Log.e(TAG, "get print stream error"); if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) {
return STATE_SYSTEM_ERROR; folderName = mContext.getString(R.string.call_record_folder_name);
} } else {
// First export folder and its notes folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET);
Cursor folderCursor = mContext.getContentResolver().query( }
Notes.CONTENT_NOTE_URI, if (!TextUtils.isEmpty(folderName)) {
NOTE_PROJECTION, ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName));
"(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " }
+ NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " String folderId = folderCursor.getString(NOTE_COLUMN_ID);
+ NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); exportFolderToText(folderId, ps);
} while (folderCursor.moveToNext());
if (folderCursor != null) { }
if (folderCursor.moveToFirst()) { folderCursor.close();
do { }
// Print folder's name
String folderName = ""; // Export notes in root's folder 将根目录里的便签导出(由于不属于任何文件夹,因此无法通过文件夹导出来实现这一部分便签的导出)
if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { Cursor noteCursor = mContext.getContentResolver().query(
folderName = mContext.getString(R.string.call_record_folder_name); Notes.CONTENT_NOTE_URI,
} else { NOTE_PROJECTION,
folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID
} + "=0", null, null);
if (!TextUtils.isEmpty(folderName)) {
ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); if (noteCursor != null) {
} if (noteCursor.moveToFirst()) {
String folderId = folderCursor.getString(NOTE_COLUMN_ID); do {
exportFolderToText(folderId, ps); ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
} while (folderCursor.moveToNext()); mContext.getString(R.string.format_datetime_mdhm),
} noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
folderCursor.close(); // Query data belong to this note
} String noteId = noteCursor.getString(NOTE_COLUMN_ID);
exportNoteToText(noteId, ps);
// Export notes in root's folder } while (noteCursor.moveToNext());
Cursor noteCursor = mContext.getContentResolver().query( }
Notes.CONTENT_NOTE_URI, noteCursor.close();
NOTE_PROJECTION, }
NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID ps.close();
+ "=0", null, null);
return STATE_SUCCESS;
if (noteCursor != null) { }
if (noteCursor.moveToFirst()) {
do { /**
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( * Get a print stream pointed to the file {@generateExportedTextFile}
mContext.getString(R.string.format_datetime_mdhm), */
noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); private PrintStream getExportToTextPrintStream() {
// Query data belong to this note File file = generateFileMountedOnSDcard(mContext, R.string.file_path,
String noteId = noteCursor.getString(NOTE_COLUMN_ID); R.string.file_name_txt_format);
exportNoteToText(noteId, ps); if (file == null) {
} while (noteCursor.moveToNext()); Log.e(TAG, "create file to exported failed");
} return null;
noteCursor.close(); }
} mFileName = file.getName();
ps.close(); mFileDirectory = mContext.getString(R.string.file_path);
PrintStream ps = null;
return STATE_SUCCESS; try {
} FileOutputStream fos = new FileOutputStream(file);
ps = new PrintStream(fos); //将ps输出流输出到特定的文件目的就是导出到文件而不是直接输出
/** } catch (FileNotFoundException e) {
* Get a print stream pointed to the file {@generateExportedTextFile} e.printStackTrace();
*/ return null;
private PrintStream getExportToTextPrintStream() { } catch (NullPointerException e) {
File file = generateFileMountedOnSDcard(mContext, R.string.file_path, e.printStackTrace();
R.string.file_name_txt_format); return null;
if (file == null) { }
Log.e(TAG, "create file to exported failed"); return ps;
return null; }
} }
mFileName = file.getName();
mFileDirectory = mContext.getString(R.string.file_path); /**
PrintStream ps = null; * Generate the text file to store imported data
try { */
FileOutputStream fos = new FileOutputStream(file); private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {
ps = new PrintStream(fos); StringBuilder sb = new StringBuilder();
} catch (FileNotFoundException e) { sb.append(Environment.getExternalStorageDirectory()); //外部SD卡的存储路径
e.printStackTrace(); sb.append(context.getString(filePathResId)); //文件的存储路径
return null; File filedir = new File(sb.toString()); //filedir应该就是用来存储路径信息
} catch (NullPointerException e) { sb.append(context.getString(
e.printStackTrace(); fileNameFormatResId,
return null; DateFormat.format(context.getString(R.string.format_date_ymd),
} System.currentTimeMillis())));
return ps; File file = new File(sb.toString());
}
} try { //如果这些文件不存在,则新建
if (!filedir.exists()) {
/** filedir.mkdir();
* Generate the text file to store imported data }
*/ if (!file.exists()) {
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { file.createNewFile();
StringBuilder sb = new StringBuilder(); }
sb.append(Environment.getExternalStorageDirectory()); return file;
sb.append(context.getString(filePathResId)); } catch (SecurityException e) {
File filedir = new File(sb.toString()); e.printStackTrace();
sb.append(context.getString( } catch (IOException e) {
fileNameFormatResId, e.printStackTrace();
DateFormat.format(context.getString(R.string.format_date_ymd), }
System.currentTimeMillis()))); // try catch 异常处理
File file = new File(sb.toString()); return null;
}
try { }
if (!filedir.exists()) {
filedir.mkdir();
}
if (!file.exists()) {
file.createNewFile();
}
return file;
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

@ -13,9 +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.
*/ */
/*该类定义在 net.micode.notes.tool 包下并导入了Android和Java的一些核心类。这些导入的类提供了对内容提供者操作、日志记录、游标以及存储值的支持。 */
package net.micode.notes.tool; package net.micode.notes.tool;
/*定义了一个公共类 DataUtils并创建了一个常量 TAG 用于日志记录。 */
import android.content.ContentProviderOperation; import android.content.ContentProviderOperation;
import android.content.ContentProviderResult; import android.content.ContentProviderResult;
import android.content.ContentResolver; import android.content.ContentResolver;
@ -34,7 +34,9 @@ import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
/*ID
ID
ContentResolver true false */
public class DataUtils { public class DataUtils {
public static final String TAG = "DataUtils"; public static final String TAG = "DataUtils";
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) { public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
@ -71,7 +73,9 @@ public class DataUtils {
} }
return false; return false;
} }
/*
ID */
/*查询非系统文件夹的数量。异常处理用于确保在查询失败时释放游标资源。 */
public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(NoteColumns.PARENT_ID, desFolderId); values.put(NoteColumns.PARENT_ID, desFolderId);
@ -88,238 +92,294 @@ public class DataUtils {
* @param: [resolver, ids, folderId] * @param: [resolver, ids, folderId]
* @return: boolean * @return: boolean
*/ */
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids, long folderId) { /*检查数据库中是否存在特定类型的可见笔记。 */
if (ids == null) { /**
Log.d(TAG, "the ids is null"); * 便
return true; * @param resolver ContentResolver
} * @param ids 便ID
* @param folderId ID
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); * @return
for (long id : ids) { */
ContentProviderOperation.Builder builder = ContentProviderOperation public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids, long folderId) {
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); if (ids == null) {
builder.withValue(NoteColumns.PARENT_ID, folderId); Log.d(TAG, "the ids is null");
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); return true;
operationList.add(builder.build());
}
try {
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
if (results == null || results.length == 0 || results[0] == null) {
Log.d(TAG, "delete notes failed, ids:" + ids.toString());
return false;
}
return true;
} catch (RemoteException e) {
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
} catch (OperationApplicationException e) {
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
}
return false;
} }
/** ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
* Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} for (long id : ids) {
*/ ContentProviderOperation.Builder builder = ContentProviderOperation
public static int getUserFolderCount(ContentResolver resolver) { .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, builder.withValue(NoteColumns.PARENT_ID, folderId);
new String[] { "COUNT(*)" }, builder.withValue(NoteColumns.LOCAL_MODIFIED, 1);
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", operationList.add(builder.build());
new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)},
null);
int count = 0;
if(cursor != null) {
if(cursor.moveToFirst()) {
try {
count = cursor.getInt(0);
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "get folder count failed:" + e.toString());
} finally {
cursor.close();
}
}
}
return count;
} }
/**
* @Method visibleInNoteDatabase
* @Date 2024/12/13 9:08
* @param resolver
* @param noteId
* @param type
* @Author lenovo
* @Return boolean
* @Description 访 noteId 便
*/
public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) {
/**
* withAppendedId URI ID URI
* query Uri selection
* lenovo 2024/12/13 9:06
*/
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
null,
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER,
new String [] {String.valueOf(type)},
null);
boolean exist = false; try {
if (cursor != null) { ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
if (cursor.getCount() > 0) { if (results == null || results.length == 0 || results[0] == null) {
exist = true; Log.d(TAG, "delete notes failed, ids:" + ids.toString());
} return false;
cursor.close();
} }
return exist; return true;
} catch (RemoteException e) {
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
} catch (OperationApplicationException e) {
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
} }
return false;
}
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { /**
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), *
null, null, null, null); * @param resolver ContentResolver
* @return
*/
public static int getUserFolderCount(ContentResolver resolver) {
Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI,
new String[] { "COUNT(*)" },
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?",
new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)},
null);
boolean exist = false; int count = 0;
if (cursor != null) { if(cursor != null) {
if (cursor.getCount() > 0) { if(cursor.moveToFirst()) {
exist = true; try {
count = cursor.getInt(0);
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "get folder count failed:" + e.toString());
} finally {
cursor.close();
} }
cursor.close();
} }
return exist;
} }
return count;
}
public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { /**
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), * ID便
null, null, null, null); * @param resolver ContentResolver
* @param noteId 便ID
* @param type 便
* @return
*/
public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) {
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
null,
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER,
new String [] {String.valueOf(type)},
null);
boolean exist = false; boolean exist = false;
if (cursor != null) { if (cursor != null) {
if (cursor.getCount() > 0) { if (cursor.getCount() > 0) {
exist = true; exist = true;
}
cursor.close();
} }
return exist; cursor.close();
} }
return exist;
}
public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { /**
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, * ID便
NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + * @param resolver ContentResolver
" AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + * @param noteId 便ID
" AND " + NoteColumns.SNIPPET + "=?", * @return
new String[] { name }, null); */
boolean exist = false; public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {
if(cursor != null) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
if(cursor.getCount() > 0) { null, null, null, null);
exist = true;
} boolean exist = false;
cursor.close(); if (cursor != null) {
if (cursor.getCount() > 0) {
exist = true;
} }
return exist; cursor.close();
} }
return exist;
}
public static HashSet<AppWidgetAttribute> getFolderNoteWidget(ContentResolver resolver, long folderId) { /**
Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, * ID
new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, * @param resolver ContentResolver
NoteColumns.PARENT_ID + "=?", * @param dataId ID
new String[] { String.valueOf(folderId) }, * @return
null); */
public static boolean existInDataDatabase(ContentResolver resolver, long dataId) {
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId),
null, null, null, null);
HashSet<AppWidgetAttribute> set = null; boolean exist = false;
if (c != null) { if (cursor != null) {
if (c.moveToFirst()) { if (cursor.getCount() > 0) {
set = new HashSet<AppWidgetAttribute>(); exist = true;
do {
try {
AppWidgetAttribute widget = new AppWidgetAttribute();
widget.widgetId = c.getInt(0);
widget.widgetType = c.getInt(1);
set.add(widget);
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, e.toString());
}
} while (c.moveToNext());
}
c.close();
} }
return set; cursor.close();
} }
return exist;
}
public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { /**
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, *
new String [] { CallNote.PHONE_NUMBER }, * @param resolver ContentResolver
CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", * @param name
new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, * @return
null); */
public static boolean checkVisibleFolderName(ContentResolver resolver, String name) {
if (cursor != null && cursor.moveToFirst()) { Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null,
try { NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER +
return cursor.getString(0); " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER +
} catch (IndexOutOfBoundsException e) { " AND " + NoteColumns.SNIPPET + "=?",
Log.e(TAG, "Get call number fails " + e.toString()); new String[] { name }, null);
} finally { boolean exist = false;
cursor.close(); if(cursor != null) {
} if(cursor.getCount() > 0) {
exist = true;
} }
return ""; cursor.close();
} }
return exist;
}
public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { /**
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, *
new String [] { CallNote.NOTE_ID }, * @param resolver ContentResolver
CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" * @param folderId ID
+ CallNote.PHONE_NUMBER + ",?)", * @return
new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, */
null); public static HashSet<AppWidgetAttribute> getFolderNoteWidget(ContentResolver resolver, long folderId) {
Cursor c = resolver.query(Notes.CONTENT_NOTE_URI,
new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE },
NoteColumns.PARENT_ID + "=?",
new String[] { String.valueOf(folderId) },
null);
if (cursor != null) { HashSet<AppWidgetAttribute> set = null;
if (cursor.moveToFirst()) { if (c != null) {
if (c.moveToFirst()) {
set = new HashSet<AppWidgetAttribute>();
do {
try { try {
return cursor.getLong(0); AppWidgetAttribute widget = new AppWidgetAttribute();
widget.widgetId = c.getInt(0);
widget.widgetType = c.getInt(1);
set.add(widget);
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
Log.e(TAG, "Get call note id fails " + e.toString()); Log.e(TAG, e.toString());
} }
} } while (c.moveToNext());
cursor.close();
} }
return 0; c.close();
} }
return set;
}
public static String getSnippetById(ContentResolver resolver, long noteId) { /**
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, * 便ID
new String [] { NoteColumns.SNIPPET }, * @param resolver ContentResolver
NoteColumns.ID + "=?", * @param noteId 便ID
new String [] { String.valueOf(noteId)}, * @return
null); */
public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) {
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
new String [] { CallNote.PHONE_NUMBER },
CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?",
new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE },
null);
if (cursor != null) { if (cursor != null && cursor.moveToFirst()) {
String snippet = ""; try {
if (cursor.moveToFirst()) { return cursor.getString(0);
snippet = cursor.getString(0); } catch (IndexOutOfBoundsException e) {
} Log.e(TAG, "Get call number fails " + e.toString());
} finally {
cursor.close(); cursor.close();
return snippet;
} }
throw new IllegalArgumentException("Note is not found with id: " + noteId);
} }
return "";
}
public static String getFormattedSnippet(String snippet) { /**
if (snippet != null) { * 便ID
snippet = snippet.trim(); * @param resolver ContentResolver
int index = snippet.indexOf('\n'); * @param phoneNumber
if (index != -1) { * @param callDate
snippet = snippet.substring(0, index); * @return 便ID
*/
public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) {
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
new String [] { CallNote.NOTE_ID },
CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL("
+ CallNote.PHONE_NUMBER + ",?)",
new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber },
null);
if (cursor != null) {
if (cursor.moveToFirst()) {
try {
return cursor.getLong(0);
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "Get call note id fails " + e.toString());
} }
} }
cursor.close();
}
return 0;
}
/**
* 便ID便
* @param resolver ContentResolver
* @param noteId 便ID
* @return 便
*/
public static String getSnippetById(ContentResolver resolver, long noteId) {
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
new String [] { NoteColumns.SNIPPET },
NoteColumns.ID + "=?",
new String [] { String.valueOf(noteId)},
null);
if (cursor != null) {
String snippet = "";
if (cursor.moveToFirst()) {
snippet = cursor.getString(0);
}
cursor.close();
return snippet; return snippet;
} }
throw new IllegalArgumentException("Note is not found with id: " + noteId);
}
public static Cursor searchInNoteDatabase(ContentResolver resolver, String query) { /**
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, * 便
new String[]{NoteColumns.ID}, * @param snippet 便
Notes.DataColumns.CONTENT + " LIKE'%" + query +"%'", * @return 便
null, */
null); public static String getFormattedSnippet(String snippet) {
return cursor; if (snippet != null) {
snippet = snippet.trim();
int index = snippet.indexOf('\n');
if (index != -1) {
snippet = snippet.substring(0, index);
}
} }
return snippet;
}
/**
* 便
* @param resolver ContentResolver
* @param query
* @return Cursor
*/
public static Cursor searchInNoteDatabase(ContentResolver resolver, String query) {
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
new String[]{NoteColumns.ID},
Notes.DataColumns.CONTENT + " LIKE'%" + query +"%'",
null,
null);
return cursor;
} }

@ -16,6 +16,7 @@
package net.micode.notes.tool; package net.micode.notes.tool;
// 该类用于定义 Google 任务相关的 JSON 字段常量
public class GTaskStringUtils { public class GTaskStringUtils {
public final static String GTASK_JSON_ACTION_ID = "action_id"; public final static String GTASK_JSON_ACTION_ID = "action_id";
@ -110,4 +111,4 @@ public class GTaskStringUtils {
public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE";
} }

@ -22,6 +22,7 @@ import android.preference.PreferenceManager;
import net.micode.notes.R; import net.micode.notes.R;
import net.micode.notes.ui.NotesPreferenceActivity; import net.micode.notes.ui.NotesPreferenceActivity;
// 资源解析器类
public class ResourceParser { public class ResourceParser {
public static final int YELLOW = 0; public static final int YELLOW = 0;
@ -40,6 +41,7 @@ public class ResourceParser {
public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM;
// 备忘录背景资源类
public static class NoteBgResources { public static class NoteBgResources {
private final static int [] BG_EDIT_RESOURCES = new int [] { private final static int [] BG_EDIT_RESOURCES = new int [] {
R.drawable.edit_yellow, R.drawable.edit_yellow,
@ -57,15 +59,18 @@ public class ResourceParser {
R.drawable.edit_title_red R.drawable.edit_title_red
}; };
// 获取备忘录背景资源
public static int getNoteBgResource(int id) { public static int getNoteBgResource(int id) {
return BG_EDIT_RESOURCES[id]; return BG_EDIT_RESOURCES[id];
} }
// 获取备忘录标题背景资源
public static int getNoteTitleBgResource(int id) { public static int getNoteTitleBgResource(int id) {
return BG_EDIT_TITLE_RESOURCES[id]; return BG_EDIT_TITLE_RESOURCES[id];
} }
} }
// 获取默认背景ID
public static int getDefaultBgId(Context context) { public static int getDefaultBgId(Context context) {
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) {
@ -75,6 +80,7 @@ public class ResourceParser {
} }
} }
// 备忘录项目背景资源类
public static class NoteItemBgResources { public static class NoteItemBgResources {
private final static int [] BG_FIRST_RESOURCES = new int [] { private final static int [] BG_FIRST_RESOURCES = new int [] {
R.drawable.list_yellow_up, R.drawable.list_yellow_up,
@ -108,27 +114,33 @@ public class ResourceParser {
R.drawable.list_red_single R.drawable.list_red_single
}; };
// 获取备忘录第一个背景资源
public static int getNoteBgFirstRes(int id) { public static int getNoteBgFirstRes(int id) {
return BG_FIRST_RESOURCES[id]; return BG_FIRST_RESOURCES[id];
} }
// 获取备忘录最后一个背景资源
public static int getNoteBgLastRes(int id) { public static int getNoteBgLastRes(int id) {
return BG_LAST_RESOURCES[id]; return BG_LAST_RESOURCES[id];
} }
// 获取备忘录单独背景资源
public static int getNoteBgSingleRes(int id) { public static int getNoteBgSingleRes(int id) {
return BG_SINGLE_RESOURCES[id]; return BG_SINGLE_RESOURCES[id];
} }
// 获取备忘录正常背景资源
public static int getNoteBgNormalRes(int id) { public static int getNoteBgNormalRes(int id) {
return BG_NORMAL_RESOURCES[id]; return BG_NORMAL_RESOURCES[id];
} }
// 获取文件夹背景资源
public static int getFolderBgRes() { public static int getFolderBgRes() {
return R.drawable.list_folder; return R.drawable.list_folder;
} }
} }
// 小部件背景资源类
public static class WidgetBgResources { public static class WidgetBgResources {
private final static int [] BG_2X_RESOURCES = new int [] { private final static int [] BG_2X_RESOURCES = new int [] {
R.drawable.widget_2x_yellow, R.drawable.widget_2x_yellow,
@ -138,6 +150,7 @@ public class ResourceParser {
R.drawable.widget_2x_red, R.drawable.widget_2x_red,
}; };
// 获取2x小部件背景资源
public static int getWidget2xBgResource(int id) { public static int getWidget2xBgResource(int id) {
return BG_2X_RESOURCES[id]; return BG_2X_RESOURCES[id];
} }
@ -150,11 +163,13 @@ public class ResourceParser {
R.drawable.widget_4x_red R.drawable.widget_4x_red
}; };
// 获取4x小部件背景资源
public static int getWidget4xBgResource(int id) { public static int getWidget4xBgResource(int id) {
return BG_4X_RESOURCES[id]; return BG_4X_RESOURCES[id];
} }
} }
// 文本外观资源类
public static class TextAppearanceResources { public static class TextAppearanceResources {
private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] {
R.style.TextAppearanceNormal, R.style.TextAppearanceNormal,
@ -163,6 +178,7 @@ public class ResourceParser {
R.style.TextAppearanceSuper R.style.TextAppearanceSuper
}; };
// 获取文本外观资源
public static int getTexAppearanceResource(int id) { public static int getTexAppearanceResource(int id) {
/** /**
* HACKME: Fix bug of store the resource id in shared preference. * HACKME: Fix bug of store the resource id in shared preference.
@ -175,8 +191,9 @@ public class ResourceParser {
return TEXTAPPEARANCE_RESOURCES[id]; return TEXTAPPEARANCE_RESOURCES[id];
} }
// 获取资源大小
public static int getResourcesSize() { public static int getResourcesSize() {
return TEXTAPPEARANCE_RESOURCES.length; return TEXTAPPEARANCE_RESOURCES.length;
} }
} }
} }

@ -14,6 +14,7 @@
* 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;
@ -39,78 +40,85 @@ 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; private long mNoteId; //文本在数据库存储中的ID号
private String mSnippet; private String mSnippet; //闹钟提示时出现的文本片段
private static final int SNIPPET_PREW_MAX_LEN = 60; private static final int SNIPPET_PREW_MAX_LEN = 60;
MediaPlayer mPlayer; MediaPlayer mPlayer;
@Override @Override
/** protected void onCreate(Bundle savedInstanceState) {
* @Method onCreate
* @Date 2024/12/25 8:15
* @param savedInstanceState
* @Author lenovo
* @Return void
* @Description
*/
protected void onCreate(Bundle savedInstanceState) {
/**
* Bundel mapkey-value
* super , onCreate
* lenovo 2024/12/25 8:39
*/
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
//Bundle类型的数据与Map类型的数据相似都是以key-value的形式存储数据的
//onsaveInstanceState方法是用来保存Activity的状态的
//能从onCreate的参数savedInsanceState中获得状态数据
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);
//界面显示——无标题
final Window win = getWindow(); final Window win = getWindow();
// 在屏幕锁定时显示
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
// 锁屏时到闹钟提示时间后,点亮屏幕
if (!isScreenOn()) { if (!isScreenOn()) {
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
//保持窗体点亮
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
//将窗体点亮
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
//允许窗体点亮时锁屏
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR); | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
} }//在手机锁屏后如果到了闹钟提示时间,点亮屏幕
Intent intent = getIntent(); Intent intent = getIntent();
try { try {
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
// 超出长度则变为 substr + "..." //根据ID从数据库中获取标签的内容
//getContentResolver是实现数据共享实例存储。
mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0, mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0,
SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info) SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info)
: mSnippet; : mSnippet;
//判断标签片段是否达到符合长度
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
e.printStackTrace(); e.printStackTrace();
return; return;
} }
/*
try
{
// 代码区
}
catch(Exception e)
{
// 异常处理
}
*/
mPlayer = new MediaPlayer(); mPlayer = new MediaPlayer();
// 查找数据库中有没有 mNoteId 的便签, 如果有则激发对话框 + 闹钟提示音
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) { if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
showActionDialog(); showActionDialog();
//弹出对话框
playAlarmSound(); playAlarmSound();
//闹钟提示音激发
} else { } else {
finish(); finish();
//完成闹钟动作
} }
} }
private boolean isScreenOn() { private boolean isScreenOn() {
//判断屏幕是否锁屏,调用系统函数判断,最后返回值是布尔类型
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn(); return pm.isScreenOn();
} }
private void playAlarmSound() { private void playAlarmSound() {
//闹钟提示音激发
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM); Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
//调用系统的铃声管理URI得到闹钟提示音
int silentModeStreams = Settings.System.getInt(getContentResolver(), int silentModeStreams = Settings.System.getInt(getContentResolver(),
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) { if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
mPlayer.setAudioStreamType(silentModeStreams); mPlayer.setAudioStreamType(silentModeStreams);
} else { } else {
@ -118,12 +126,19 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
} }
try { try {
mPlayer.setDataSource(this, url); mPlayer.setDataSource(this, url);
//方法setDataSource(Context context, Uri uri)
//解释:无返回值,设置多媒体数据来源【根据 Uri】
mPlayer.prepare(); mPlayer.prepare();
//准备同步
mPlayer.setLooping(true); mPlayer.setLooping(true);
//设置是否循环播放
mPlayer.start(); mPlayer.start();
//开始播放
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
//e.printStackTrace()函数功能是抛出异常, 还将显示出更深的调用信息
//System.out.println(e),这个方法打印出异常,并且输出在哪里出现的异常
} catch (SecurityException e) { } catch (SecurityException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -135,41 +150,61 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
e.printStackTrace(); e.printStackTrace();
} }
} }
private void showActionDialog() {// TODO: 2024/1/2 可以模仿这个写一个xxx条件下弹出来的Dialog private void showActionDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this); AlertDialog.Builder dialog = new AlertDialog.Builder(this);
//AlertDialog的构造方法全部是Protected的
//所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。
//要创建一个AlertDialog就要用到AlertDialog.Builder中的create()方法
//如这里的dialog就是新建了一个AlertDialog
dialog.setTitle(R.string.app_name); dialog.setTitle(R.string.app_name);
//为对话框设置标题
dialog.setMessage(mSnippet); dialog.setMessage(mSnippet);
//为对话框设置内容
dialog.setPositiveButton(R.string.notealert_ok, this); dialog.setPositiveButton(R.string.notealert_ok, this);
//给对话框添加"Yes"按钮
if (isScreenOn()) { if (isScreenOn()) {
dialog.setNegativeButton(R.string.notealert_enter, this); dialog.setNegativeButton(R.string.notealert_enter, this);
} }//对话框添加"No"按钮
dialog.show().setOnDismissListener(this); dialog.show().setOnDismissListener(this);
} }
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
switch (which) { switch (which) {
//用which来选择click后下一步的操作
case DialogInterface.BUTTON_NEGATIVE: case DialogInterface.BUTTON_NEGATIVE:
//这是取消操作
Intent intent = new Intent(this, NoteEditActivity.class); Intent intent = new Intent(this, NoteEditActivity.class);
//实现两个类间的数据传输
intent.setAction(Intent.ACTION_VIEW); intent.setAction(Intent.ACTION_VIEW);
//设置动作属性
intent.putExtra(Intent.EXTRA_UID, mNoteId); intent.putExtra(Intent.EXTRA_UID, mNoteId);
//实现key-value对
//EXTRA_UID为keymNoteId为键
startActivity(intent); startActivity(intent);
//开始动作
break; break;
default: default:
//这是确定操作
break; break;
} }
} }
public void onDismiss(DialogInterface dialog) { public void onDismiss(DialogInterface dialog) {
//忽略
stopAlarmSound(); stopAlarmSound();
//停止闹钟声音
finish(); finish();
//完成该动作
} }
private void stopAlarmSound() { private void stopAlarmSound() {
if (mPlayer != null) { if (mPlayer != null) {
mPlayer.stop(); mPlayer.stop();
//停止播放
mPlayer.release(); mPlayer.release();
//释放MediaPlayer对象
mPlayer = null; mPlayer = null;
} }
} }
} }

@ -30,36 +30,60 @@ import net.micode.notes.data.Notes.NoteColumns;
public class AlarmInitReceiver extends BroadcastReceiver { public class AlarmInitReceiver extends BroadcastReceiver {
// 定义查询数据库时要获取的列
private static final String [] PROJECTION = new String [] { private static final String [] PROJECTION = new String [] {
NoteColumns.ID, NoteColumns.ID, // 笔记的ID
NoteColumns.ALERTED_DATE NoteColumns.ALERTED_DATE // 笔记的提醒日期
}; };
// 定义列的索引以便在Cursor中快速访问
private static final int COLUMN_ID = 0; private static final int COLUMN_ID = 0;
private static final int COLUMN_ALERTED_DATE = 1; private static final int COLUMN_ALERTED_DATE = 1;
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
long currentDate = System.currentTimeMillis(); long currentDate = System.currentTimeMillis(); // 获取当前的系统时间(毫秒)
// 从内容提供者中查询需要提醒的笔记
// Notes.CONTENT_NOTE_URI 是内容提供者的URI
// PROJECTION 指定了要查询的列
// 查询条件是提醒日期大于当前日期且笔记类型为普通笔记Notes.TYPE_NOTE
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI, Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION, PROJECTION,
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE, NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
new String[] { String.valueOf(currentDate) }, new String[] { String.valueOf(currentDate) }, // 查询参数,这里只有当前日期
null); null); // 不需要排序
// 检查Cursor是否为null
if (c != null) { if (c != null) {
// 如果Cursor不为空移动到第一条记录
if (c.moveToFirst()) { if (c.moveToFirst()) {
// 开始遍历Cursor中的所有记录
do { do {
// 获取当前笔记的提醒日期
long alertDate = c.getLong(COLUMN_ALERTED_DATE); long alertDate = c.getLong(COLUMN_ALERTED_DATE);
// 创建一个Intent目标组件是AlarmReceiver
Intent sender = new Intent(context, AlarmReceiver.class); Intent sender = new Intent(context, AlarmReceiver.class);
// 为Intent设置数据URI这里使用ContentUris.withAppendedId方法将笔记ID附加到Notes的内容URI上
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID))); sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 创建一个PendingIntent这里使用getBroadcast方法因为AlarmReceiver是一个BroadcastReceiver
// 注意这里使用的请求码第二个参数为0这可能导致如果有多个相同的Intent被发送时PendingIntent会被覆盖
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE); // 获取AlarmManager服务
AlarmManager alermManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// 设置一个闹钟当到达提醒日期时触发PendingIntent即发送广播给AlarmReceiver
// AlarmManager.RTC_WAKEUP表示在指定时间唤醒设备并发送广播
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent); alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext());
} while (c.moveToNext()); // 移动到下一条记录,继续循环
} }
// 关闭Cursor
c.close(); c.close();
} }
} }
} }

@ -14,17 +14,30 @@
* 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.Context; import android.content.BroadcastReceiver; // 广播接收器基类
import android.content.Intent; import android.content.Context; // 提供应用环境信息的类
import android.content.Intent; // 用于组件间通信的消息传递对象
// 定义一个公开类AlarmReceiver它继承自BroadcastReceiver
public class AlarmReceiver extends BroadcastReceiver { public class AlarmReceiver extends BroadcastReceiver {
// 重写onReceive方法当接收到广播时调用此方法
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
// 设置intent的目标组件为AlarmAlertActivity类
// 这意味着当这个广播接收器接收到广播时它希望启动AlarmAlertActivity活动
intent.setClass(context, AlarmAlertActivity.class); intent.setClass(context, AlarmAlertActivity.class);
// 为intent添加FLAG_ACTIVITY_NEW_TASK标志
// 这通常用于从非活动上下文中启动活动时,确保活动作为新的任务启动
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 使用context对象调用startActivity方法来启动由intent指定的活动
// 由于intent已经被设置为启动AlarmAlertActivity这行代码的作用就是启动AlarmAlertActivity活动
context.startActivity(intent); context.startActivity(intent);
} }
} }

@ -14,8 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
// 指定类所在的包名
package net.micode.notes.ui; package net.micode.notes.ui;
// 导入所需的Java和Android类
import java.text.DateFormatSymbols; import java.text.DateFormatSymbols;
import java.util.Calendar; import java.util.Calendar;
@ -27,11 +29,13 @@ import android.text.format.DateFormat;
import android.view.View; import android.view.View;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.NumberPicker; import android.widget.NumberPicker;
// 定义一个名为DateTimePicker的类它继承自FrameLayout
public class DateTimePicker extends FrameLayout { public class DateTimePicker extends FrameLayout {
//FrameLayout是布局模板之一
//所有的子元素全部在屏幕的右上方
private static final boolean DEFAULT_ENABLE_STATE = true; private static final boolean DEFAULT_ENABLE_STATE = true;
private static final int HOURS_IN_HALF_DAY = 12; private static final int HOURS_IN_HALF_DAY = 12;
private static final int HOURS_IN_ALL_DAY = 24; private static final int HOURS_IN_ALL_DAY = 24;
private static final int DAYS_IN_ALL_WEEK = 7; private static final int DAYS_IN_ALL_WEEK = 7;
@ -45,25 +49,27 @@ public class DateTimePicker extends FrameLayout {
private static final int MINUT_SPINNER_MAX_VAL = 59; private static final int MINUT_SPINNER_MAX_VAL = 59;
private static final int AMPM_SPINNER_MIN_VAL = 0; private static final int AMPM_SPINNER_MIN_VAL = 0;
private static final int AMPM_SPINNER_MAX_VAL = 1; private static final int AMPM_SPINNER_MAX_VAL = 1;
//初始化控件
private final NumberPicker mDateSpinner; private final NumberPicker mDateSpinner;
private final NumberPicker mHourSpinner; private final NumberPicker mHourSpinner;
private final NumberPicker mMinuteSpinner; private final NumberPicker mMinuteSpinner;
private final NumberPicker mAmPmSpinner; private final NumberPicker mAmPmSpinner;
//NumberPicker是数字选择器
//这里定义的四个变量全部是在设置闹钟时需要选择的变量(如日期、时、分、上午或者下午)
private Calendar mDate; private Calendar mDate;
//定义了Calendar类型的变量mDate用于操作时间
private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK]; private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK];
private boolean mIsAm; private boolean mIsAm;
private boolean mIs24HourView; private boolean mIs24HourView;
private boolean mIsEnabled = DEFAULT_ENABLE_STATE; private boolean mIsEnabled = DEFAULT_ENABLE_STATE;
private boolean mInitialising; private boolean mInitialising;
private OnDateTimeChangedListener mOnDateTimeChangedListener; private OnDateTimeChangedListener mOnDateTimeChangedListener;
private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() { private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() {
@Override @Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
@ -71,41 +77,49 @@ public class DateTimePicker extends FrameLayout {
updateDateControl(); updateDateControl();
onDateTimeChanged(); onDateTimeChanged();
} }
}; };//OnValueChangeListener这是时间改变监听器这里主要是对日期的监听
//将现在日期的值传递给mDateupdateDateControl是同步操作
private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() { private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
@Override //这里是对 小时Hour 的监听
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
boolean isDateChanged = false; boolean isDateChanged = false;
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
//声明一个Calendar的变量cal便于后续的操作
if (!mIs24HourView) { if (!mIs24HourView) {
if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) { if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) {
cal.setTimeInMillis(mDate.getTimeInMillis()); cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, 1); cal.add(Calendar.DAY_OF_YEAR, 1);
isDateChanged = true; isDateChanged = true;
//这里是对于12小时制时晚上11点和12点交替时对日期的更改
} else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) { } else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
cal.setTimeInMillis(mDate.getTimeInMillis()); cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, -1); cal.add(Calendar.DAY_OF_YEAR, -1);
isDateChanged = true; isDateChanged = true;
} }
//这里是对于12小时制时凌晨11点和12点交替时对日期的更改
if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY || if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY ||
oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) { oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
mIsAm = !mIsAm; mIsAm = !mIsAm;
updateAmPmControl(); updateAmPmControl();
} }//这里是对于12小时制时中午11点和12点交替时对AM和PM的更改
} else { } else {
if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) { if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) {
cal.setTimeInMillis(mDate.getTimeInMillis()); cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, 1); cal.add(Calendar.DAY_OF_YEAR, 1);
isDateChanged = true; isDateChanged = true;
//这里是对于24小时制时晚上11点和12点交替时对日期的更改
} else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) { } else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) {
cal.setTimeInMillis(mDate.getTimeInMillis()); cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, -1); cal.add(Calendar.DAY_OF_YEAR, -1);
isDateChanged = true; isDateChanged = true;
} }
} } //这里是对于12小时制时凌晨11点和12点交替时对日期的更改
int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY); int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY);
//通过数字选择器对newHour的赋值
mDate.set(Calendar.HOUR_OF_DAY, newHour); mDate.set(Calendar.HOUR_OF_DAY, newHour);
//通过set函数将新的Hour值传给mDate
onDateTimeChanged(); onDateTimeChanged();
if (isDateChanged) { if (isDateChanged) {
setCurrentYear(cal.get(Calendar.YEAR)); setCurrentYear(cal.get(Calendar.YEAR));
@ -114,18 +128,22 @@ public class DateTimePicker extends FrameLayout {
} }
} }
}; };
private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() { private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
@Override @Override
//这里是对 分钟Minute改变的监听
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
int minValue = mMinuteSpinner.getMinValue(); int minValue = mMinuteSpinner.getMinValue();
int maxValue = mMinuteSpinner.getMaxValue(); int maxValue = mMinuteSpinner.getMaxValue();
int offset = 0; int offset = 0;
//设置offset作为小时改变的一个记录数据
if (oldVal == maxValue && newVal == minValue) { if (oldVal == maxValue && newVal == minValue) {
offset += 1; offset += 1;
} else if (oldVal == minValue && newVal == maxValue) { } else if (oldVal == minValue && newVal == maxValue) {
offset -= 1; offset -= 1;
} }
//如果原值为59新值为0则offset加1
//如果原值为0新值为59则offset减1
if (offset != 0) { if (offset != 0) {
mDate.add(Calendar.HOUR_OF_DAY, offset); mDate.add(Calendar.HOUR_OF_DAY, offset);
mHourSpinner.setValue(getCurrentHour()); mHourSpinner.setValue(getCurrentHour());
@ -143,9 +161,10 @@ public class DateTimePicker extends FrameLayout {
onDateTimeChanged(); onDateTimeChanged();
} }
}; };
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() { private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
@Override //对AM和PM的监听
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
mIsAm = !mIsAm; mIsAm = !mIsAm;
if (mIsAm) { if (mIsAm) {
@ -157,63 +176,66 @@ public class DateTimePicker extends FrameLayout {
onDateTimeChanged(); onDateTimeChanged();
} }
}; };
public interface OnDateTimeChangedListener { public interface OnDateTimeChangedListener {
void onDateTimeChanged(DateTimePicker view, int year, int month, void onDateTimeChanged(DateTimePicker view, int year, int month,
int dayOfMonth, int hourOfDay, int minute); int dayOfMonth, int hourOfDay, int minute);
} }
public DateTimePicker(Context context) { public DateTimePicker(Context context) {
this(context, System.currentTimeMillis()); this(context, System.currentTimeMillis());
} }//通过对数据库的访问,获取当前的系统时间
public DateTimePicker(Context context, long date) { public DateTimePicker(Context context, long date) {
this(context, date, DateFormat.is24HourFormat(context)); this(context, date, DateFormat.is24HourFormat(context));
} }//上面函数的得到的是一个天文数字1970至今的秒数需要DateFormat将其变得有意义
public DateTimePicker(Context context, long date, boolean is24HourView) { public DateTimePicker(Context context, long date, boolean is24HourView) {
super(context); super(context);
//获取系统时间
mDate = Calendar.getInstance(); mDate = Calendar.getInstance();
mInitialising = true; mInitialising = true;
mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY; mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY;
inflate(context, R.layout.datetime_picker, this); inflate(context, R.layout.datetime_picker, this);
//如果当前Activity里用到别的layout比如对话框layout
//还要设置这个layout上的其他组件的内容就必须用inflate()方法先将对话框的layout找出来
//然后再用findViewById()找到它上面的其它组件
mDateSpinner = (NumberPicker) findViewById(R.id.date); mDateSpinner = (NumberPicker) findViewById(R.id.date);
mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL); mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL);
mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL); mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL);
mDateSpinner.setOnValueChangedListener(mOnDateChangedListener); mDateSpinner.setOnValueChangedListener(mOnDateChangedListener);
mHourSpinner = (NumberPicker) findViewById(R.id.hour); mHourSpinner = (NumberPicker) findViewById(R.id.hour);
mHourSpinner.setOnValueChangedListener(mOnHourChangedListener); mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
mMinuteSpinner = (NumberPicker) findViewById(R.id.minute); mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL); mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL);
mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL); mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL);
mMinuteSpinner.setOnLongPressUpdateInterval(100); mMinuteSpinner.setOnLongPressUpdateInterval(100);
mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener); mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings(); String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings();
mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm); mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm);
mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL); mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL);
mAmPmSpinner.setMaxValue(AMPM_SPINNER_MAX_VAL); mAmPmSpinner.setMaxValue(AMPM_SPINNER_MAX_VAL);
mAmPmSpinner.setDisplayedValues(stringsForAmPm); mAmPmSpinner.setDisplayedValues(stringsForAmPm);
mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener); mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener);
// update controls to initial state // update controls to initial state
updateDateControl(); updateDateControl();
updateHourControl(); updateHourControl();
updateAmPmControl(); updateAmPmControl();
set24HourView(is24HourView); set24HourView(is24HourView);
// set to current time // set to current time
setCurrentDate(date); setCurrentDate(date);
setEnabled(isEnabled()); setEnabled(isEnabled());
// set the content descriptions // set the content descriptions
mInitialising = false; mInitialising = false;
} }
@Override @Override
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
if (mIsEnabled == enabled) { if (mIsEnabled == enabled) {
@ -226,12 +248,14 @@ public class DateTimePicker extends FrameLayout {
mAmPmSpinner.setEnabled(enabled); mAmPmSpinner.setEnabled(enabled);
mIsEnabled = enabled; mIsEnabled = enabled;
} }
//存在疑问setEnabled的作用
//下面的代码通过原程序的注释已经比较清晰,另外可以通过函数名来判断
//下面的各函数主要是对上面代码引用到的各函数功能的实现
@Override @Override
public boolean isEnabled() { public boolean isEnabled() {
return mIsEnabled; return mIsEnabled;
} }
/** /**
* Get the current date in millis * Get the current date in millis
* *
@ -239,8 +263,8 @@ public class DateTimePicker extends FrameLayout {
*/ */
public long getCurrentDateInTimeMillis() { public long getCurrentDateInTimeMillis() {
return mDate.getTimeInMillis(); return mDate.getTimeInMillis();
} }//实现函数——得到当前的秒数
/** /**
* Set the current date * Set the current date
* *
@ -251,8 +275,8 @@ public class DateTimePicker extends FrameLayout {
cal.setTimeInMillis(date); cal.setTimeInMillis(date);
setCurrentDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), setCurrentDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE)); cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
} }//实现函数功能——设置当前的时间参数是date
/** /**
* Set the current date * Set the current date
* *
@ -269,17 +293,18 @@ public class DateTimePicker extends FrameLayout {
setCurrentDay(dayOfMonth); setCurrentDay(dayOfMonth);
setCurrentHour(hourOfDay); setCurrentHour(hourOfDay);
setCurrentMinute(minute); setCurrentMinute(minute);
} }//实现函数功能——设置当前的时间,参数是各详细的变量
/** /**
* Get current year * Get current year
* *
* @return The current year * @return The current year
*/ */
//下面是得到year、month、day等值
public int getCurrentYear() { public int getCurrentYear() {
return mDate.get(Calendar.YEAR); return mDate.get(Calendar.YEAR);
} }
/** /**
* Set current year * Set current year
* *
@ -293,7 +318,7 @@ public class DateTimePicker extends FrameLayout {
updateDateControl(); updateDateControl();
onDateTimeChanged(); onDateTimeChanged();
} }
/** /**
* Get current month in the year * Get current month in the year
* *
@ -302,7 +327,7 @@ public class DateTimePicker extends FrameLayout {
public int getCurrentMonth() { public int getCurrentMonth() {
return mDate.get(Calendar.MONTH); return mDate.get(Calendar.MONTH);
} }
/** /**
* Set current month in the year * Set current month in the year
* *
@ -316,7 +341,7 @@ public class DateTimePicker extends FrameLayout {
updateDateControl(); updateDateControl();
onDateTimeChanged(); onDateTimeChanged();
} }
/** /**
* Get current day of the month * Get current day of the month
* *
@ -325,7 +350,7 @@ public class DateTimePicker extends FrameLayout {
public int getCurrentDay() { public int getCurrentDay() {
return mDate.get(Calendar.DAY_OF_MONTH); return mDate.get(Calendar.DAY_OF_MONTH);
} }
/** /**
* Set current day of the month * Set current day of the month
* *
@ -339,7 +364,7 @@ public class DateTimePicker extends FrameLayout {
updateDateControl(); updateDateControl();
onDateTimeChanged(); onDateTimeChanged();
} }
/** /**
* Get current hour in 24 hour mode, in the range (0~23) * Get current hour in 24 hour mode, in the range (0~23)
* @return The current hour in 24 hour mode * @return The current hour in 24 hour mode
@ -347,7 +372,7 @@ public class DateTimePicker extends FrameLayout {
public int getCurrentHourOfDay() { public int getCurrentHourOfDay() {
return mDate.get(Calendar.HOUR_OF_DAY); return mDate.get(Calendar.HOUR_OF_DAY);
} }
private int getCurrentHour() { private int getCurrentHour() {
if (mIs24HourView){ if (mIs24HourView){
return getCurrentHourOfDay(); return getCurrentHourOfDay();
@ -360,7 +385,7 @@ public class DateTimePicker extends FrameLayout {
} }
} }
} }
/** /**
* Set current hour in 24 hour mode, in the range (0~23) * Set current hour in 24 hour mode, in the range (0~23)
* *
@ -388,7 +413,7 @@ public class DateTimePicker extends FrameLayout {
mHourSpinner.setValue(hourOfDay); mHourSpinner.setValue(hourOfDay);
onDateTimeChanged(); onDateTimeChanged();
} }
/** /**
* Get currentMinute * Get currentMinute
* *
@ -397,7 +422,7 @@ public class DateTimePicker extends FrameLayout {
public int getCurrentMinute() { public int getCurrentMinute() {
return mDate.get(Calendar.MINUTE); return mDate.get(Calendar.MINUTE);
} }
/** /**
* Set current minute * Set current minute
*/ */
@ -409,14 +434,14 @@ public class DateTimePicker extends FrameLayout {
mDate.set(Calendar.MINUTE, minute); mDate.set(Calendar.MINUTE, minute);
onDateTimeChanged(); onDateTimeChanged();
} }
/** /**
* @return true if this is in 24 hour view else false. * @return true if this is in 24 hour view else false.
*/ */
public boolean is24HourView () { public boolean is24HourView () {
return mIs24HourView; return mIs24HourView;
} }
/** /**
* Set whether in 24 hour or AM/PM mode. * Set whether in 24 hour or AM/PM mode.
* *
@ -433,7 +458,7 @@ public class DateTimePicker extends FrameLayout {
setCurrentHour(hour); setCurrentHour(hour);
updateAmPmControl(); updateAmPmControl();
} }
private void updateDateControl() { private void updateDateControl() {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(mDate.getTimeInMillis()); cal.setTimeInMillis(mDate.getTimeInMillis());
@ -446,8 +471,8 @@ public class DateTimePicker extends FrameLayout {
mDateSpinner.setDisplayedValues(mDateDisplayValues); mDateSpinner.setDisplayedValues(mDateDisplayValues);
mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2); mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2);
mDateSpinner.invalidate(); mDateSpinner.invalidate();
} }// 对于星期几的算法
private void updateAmPmControl() { private void updateAmPmControl() {
if (mIs24HourView) { if (mIs24HourView) {
mAmPmSpinner.setVisibility(View.GONE); mAmPmSpinner.setVisibility(View.GONE);
@ -455,9 +480,9 @@ public class DateTimePicker extends FrameLayout {
int index = mIsAm ? Calendar.AM : Calendar.PM; int index = mIsAm ? Calendar.AM : Calendar.PM;
mAmPmSpinner.setValue(index); mAmPmSpinner.setValue(index);
mAmPmSpinner.setVisibility(View.VISIBLE); mAmPmSpinner.setVisibility(View.VISIBLE);
} }// 对于上下午操作的算法
} }
private void updateHourControl() { private void updateHourControl() {
if (mIs24HourView) { if (mIs24HourView) {
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW); mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW);
@ -465,9 +490,9 @@ public class DateTimePicker extends FrameLayout {
} else { } else {
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW); mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW);
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW); mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW);
} }// 对与小时的算法
} }
/** /**
* Set the callback that indicates the 'Set' button has been pressed. * Set the callback that indicates the 'Set' button has been pressed.
* @param callback the callback, if null will do nothing * @param callback the callback, if null will do nothing
@ -475,11 +500,11 @@ public class DateTimePicker extends FrameLayout {
public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) { public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) {
mOnDateTimeChangedListener = callback; mOnDateTimeChangedListener = callback;
} }
private void onDateTimeChanged() { private void onDateTimeChanged() {
if (mOnDateTimeChangedListener != null) { if (mOnDateTimeChangedListener != null) {
mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(), mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(),
getCurrentMonth(), getCurrentDay(), getCurrentHourOfDay(), getCurrentMinute()); getCurrentMonth(), getCurrentDay(), getCurrentHourOfDay(), getCurrentMinute());
} }
} }
} }

@ -14,77 +14,89 @@
* 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;
public class DateTimePickerDialog extends AlertDialog implements OnClickListener { public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
private Calendar mDate = Calendar.getInstance(); private Calendar mDate = Calendar.getInstance();
private boolean mIs24HourView; //创建一个Calendar类型的变量 mDate方便时间的操作
private OnDateTimeSetListener mOnDateTimeSetListener; private boolean mIs24HourView;
private DateTimePicker mDateTimePicker; private OnDateTimeSetListener mOnDateTimeSetListener;
//声明一个时间日期滚动选择控件 mOnDateTimeSetListener
public interface OnDateTimeSetListener { private DateTimePicker mDateTimePicker;
void OnDateTimeSet(AlertDialog dialog, long date); //DateTimePicker控件控件一般用于让用户可以从日期列表中选择单个值。
} //运行时,单击控件边上的下拉箭头,会显示为两个部分:一个下拉列表,一个用于选择日期的
public DateTimePickerDialog(Context context, long date) { public interface OnDateTimeSetListener {
super(context); void OnDateTimeSet(AlertDialog dialog, long date);
mDateTimePicker = new DateTimePicker(context); }
setView(mDateTimePicker);
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() { public DateTimePickerDialog(Context context, long date) {
public void onDateTimeChanged(DateTimePicker view, int year, int month, //对该界面对话框的实例化
int dayOfMonth, int hourOfDay, int minute) { super(context);
mDate.set(Calendar.YEAR, year); //对数据库的操作
mDate.set(Calendar.MONTH, month); mDateTimePicker = new DateTimePicker(context);
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); setView(mDateTimePicker);
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); //添加一个子视图
mDate.set(Calendar.MINUTE, minute); mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
updateTitle(mDate.getTimeInMillis()); public void onDateTimeChanged(DateTimePicker view, int year, int month,
} int dayOfMonth, int hourOfDay, int minute) {
}); mDate.set(Calendar.YEAR, year);
mDate.setTimeInMillis(date); mDate.set(Calendar.MONTH, month);
mDate.set(Calendar.SECOND, 0); mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
setButton(context.getString(R.string.datetime_dialog_ok), this); mDate.set(Calendar.MINUTE, minute);
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null); //将视图中的各选项设置为系统当前时间
set24HourView(DateFormat.is24HourFormat(this.getContext())); updateTitle(mDate.getTimeInMillis());
updateTitle(mDate.getTimeInMillis()); }
} });
mDate.setTimeInMillis(date);
public void set24HourView(boolean is24HourView) { //得到系统时间
mIs24HourView = is24HourView; mDate.set(Calendar.SECOND, 0);
} //将秒数设置为0
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) { setButton(context.getString(R.string.datetime_dialog_ok), this);
mOnDateTimeSetListener = callBack; setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
} //设置按钮
set24HourView(DateFormat.is24HourFormat(this.getContext()));
private void updateTitle(long date) { //时间标准化打印
int flag = updateTitle(mDate.getTimeInMillis());
DateUtils.FORMAT_SHOW_YEAR | }
DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_TIME; public void set24HourView(boolean is24HourView) {
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR; mIs24HourView = is24HourView;
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag)); }
}
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
public void onClick(DialogInterface arg0, int arg1) { mOnDateTimeSetListener = callBack;
if (mOnDateTimeSetListener != null) { }//将时间日期滚动选择控件实例化
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
} 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));
}//android开发中常见日期管理工具类API——DateUtils按照上下午显示时间
public void onClick(DialogInterface arg0, int arg1) {
if (mOnDateTimeSetListener != null) {
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
}
}//第一个参数arg0是接收到点击事件的对话框
//第二个参数arg1是该对话框上的按钮
} }

@ -15,7 +15,7 @@
*/ */
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;
@ -24,38 +24,42 @@ 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;
public class DropdownMenu { public class DropdownMenu {
private Button mButton; private Button mButton;
private PopupMenu mPopupMenu; private PopupMenu mPopupMenu;
//声明一个下拉菜单
private Menu mMenu; private Menu mMenu;
public DropdownMenu(Context context, Button button, int menuId) { public DropdownMenu(Context context, Button button, int menuId) {
mButton = button; mButton = button;
mButton.setBackgroundResource(R.drawable.dropdown_icon); mButton.setBackgroundResource(R.drawable.dropdown_icon);
//设置这个view的背景
mPopupMenu = new PopupMenu(context, mButton); mPopupMenu = new PopupMenu(context, mButton);
mMenu = mPopupMenu.getMenu(); mMenu = mPopupMenu.getMenu();
mPopupMenu.getMenuInflater().inflate(menuId, mMenu); mPopupMenu.getMenuInflater().inflate(menuId, mMenu);
//MenuInflater是用来实例化Menu目录下的Menu布局文件
//根据ID来确认menu的内容选项
mButton.setOnClickListener(new OnClickListener() { mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
mPopupMenu.show(); mPopupMenu.show();
} }
}); });
} }
public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) { public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {
if (mPopupMenu != null) { if (mPopupMenu != null) {
mPopupMenu.setOnMenuItemClickListener(listener); mPopupMenu.setOnMenuItemClickListener(listener);
} }//设置菜单的监听
} }
public MenuItem findItem(int id) { public MenuItem findItem(int id) {
return mMenu.findItem(id); return mMenu.findItem(id);
} }//对于菜单选项的初始化,根据索引搜索菜单需要的选项
public void setTitle(CharSequence title) { public void setTitle(CharSequence title) {
mButton.setText(title); mButton.setText(title);
} }//布局文件,设置标题
} }

@ -14,67 +14,74 @@
* 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;
public class FoldersListAdapter extends CursorAdapter { public class FoldersListAdapter extends CursorAdapter {
public static final String [] PROJECTION = { //CursorAdapter是Cursor和ListView的接口
NoteColumns.ID, //FoldersListAdapter继承了CursorAdapter的类
NoteColumns.SNIPPET //主要作用是便签数据库和用户的交互
}; //这里就是用folder文件夹的形式展现给用户
public static final String [] PROJECTION = {
public static final int ID_COLUMN = 0; NoteColumns.ID,
public static final int NAME_COLUMN = 1; NoteColumns.SNIPPET
};//调用数据库中便签的ID和片段
public FoldersListAdapter(Context context, Cursor c) {
super(context, c); public static final int ID_COLUMN = 0;
// TODO Auto-generated constructor stub public static final int NAME_COLUMN = 1;
}
public FoldersListAdapter(Context context, Cursor c) {
@Override super(context, c);
public View newView(Context context, Cursor cursor, ViewGroup parent) { // TODO Auto-generated constructor stub
return new FolderListItem(context); }//数据库操作
}
@Override
@Override public View newView(Context context, Cursor cursor, ViewGroup parent) {
public void bindView(View view, Context context, Cursor cursor) { //ViewGroup是容器
if (view instanceof FolderListItem) { return new FolderListItem(context);
String folderName = (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context }//创建一个文件夹,对于各文件夹中子标签的初始化
.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
((FolderListItem) view).bind(folderName); @Override
} public void bindView(View view, Context context, Cursor cursor) {
} if (view instanceof FolderListItem) {
String folderName = (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
public String getFolderName(Context context, int position) { .getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
Cursor cursor = (Cursor) getItem(position); ((FolderListItem) view).bind(folderName);
return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context }
.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN); }//将各个布局文件绑定起来
}
public String getFolderName(Context context, int position) {
private class FolderListItem extends LinearLayout { Cursor cursor = (Cursor) getItem(position);
private TextView mName; return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
public FolderListItem(Context context) { }//根据数据库中标签的ID得到标签的各项内容
super(context);
inflate(context, R.layout.folder_list_item, this); private class FolderListItem extends LinearLayout {
mName = (TextView) findViewById(R.id.tv_folder_name); private TextView mName;
}
public FolderListItem(Context context) {
public void bind(String name) { super(context);
mName.setText(name); //操作数据库
} inflate(context, R.layout.folder_list_item, this);
} //根据布局文件的名字等信息将其找出来
mName = (TextView) findViewById(R.id.tv_folder_name);
} }
public void bind(String name) {
mName.setText(name);
}
}
}

@ -14,212 +14,273 @@
* 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;
public class NoteEditText extends EditText { //继承edittext设置便签设置文本框
private static final String TAG = "NoteEditText"; public class NoteEditText extends EditText {
private int mIndex; private static final String TAG = "NoteEditText";
private int mSelectionStartBeforeDelete; private int mIndex;
private int mSelectionStartBeforeDelete;
private static final String SCHEME_TEL = "tel:" ;
private static final String SCHEME_HTTP = "http:" ; private static final String SCHEME_TEL = "tel:" ;
private static final String SCHEME_EMAIL = "mailto:" ; private static final String SCHEME_HTTP = "http:" ;
private static final String SCHEME_EMAIL = "mailto:" ;
private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>();
static { ///建立一个字符和整数的hash表用于链接电话网站还有邮箱
sSchemaActionResMap.put(SCHEME_TEL, R.string.note_link_tel); private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>();
sSchemaActionResMap.put(SCHEME_HTTP, R.string.note_link_web); static {
sSchemaActionResMap.put(SCHEME_EMAIL, R.string.note_link_email); sSchemaActionResMap.put(SCHEME_TEL, R.string.note_link_tel);
} sSchemaActionResMap.put(SCHEME_HTTP, R.string.note_link_web);
sSchemaActionResMap.put(SCHEME_EMAIL, R.string.note_link_email);
/** }
* Call by the {@link NoteEditActivity} to delete or add edit text
*/ /**
public interface OnTextViewChangeListener { * Call by the {@link NoteEditActivity} to delete or add edit text
/** */
* Delete current edit text when {@link KeyEvent#KEYCODE_DEL} happens //在NoteEditActivity中删除或添加文本的操作可以看做是一个文本是否被变的标记英文注释已说明的很清楚
* and the text is null public interface OnTextViewChangeListener {
*/ /**
void onEditTextDelete(int index, String text); * Delete current edit text when {@link KeyEvent#KEYCODE_DEL} happens
* and the text is null
/** */
* Add edit text after current edit text when {@link KeyEvent#KEYCODE_ENTER} //处理删除按键时的操作
* happen void onEditTextDelete(int index, String text);
*/
void onEditTextEnter(int index, String text); /**
* Add edit text after current edit text when {@link KeyEvent#KEYCODE_ENTER}
/** * happen
* Hide or show item option when text change */
*/ //处理进入按键时的操作
void onTextChange(int index, boolean hasText); void onEditTextEnter(int index, String text);
}
/**
private OnTextViewChangeListener mOnTextViewChangeListener; * Hide or show item option when text change
*/
public NoteEditText(Context context) { void onTextChange(int index, boolean hasText);
super(context, null); }
mIndex = 0;
} private OnTextViewChangeListener mOnTextViewChangeListener;
public void setIndex(int index) { //根据context设置文本
mIndex = index; public NoteEditText(Context context) {
} super(context, null);//用super引用父类变量
mIndex = 0;
public void setOnTextViewChangeListener(OnTextViewChangeListener listener) { }
mOnTextViewChangeListener = listener;
} //设置当前光标
public void setIndex(int index) {
public NoteEditText(Context context, AttributeSet attrs) { mIndex = index;
super(context, attrs, android.R.attr.editTextStyle); }
}
//初始化文本修改标记
public NoteEditText(Context context, AttributeSet attrs, int defStyle) { public void setOnTextViewChangeListener(OnTextViewChangeListener listener) {
super(context, attrs, defStyle); mOnTextViewChangeListener = listener;
// TODO Auto-generated constructor stub }
}
//AttributeSet 百度了一下是自定义空控件属性,用于维护便签动态变化的属性
@Override //初始化便签
public boolean onTouchEvent(MotionEvent event) { public NoteEditText(Context context, AttributeSet attrs) {
switch (event.getAction()) { super(context, attrs, android.R.attr.editTextStyle);
case MotionEvent.ACTION_DOWN: }
int x = (int) event.getX(); // 根据defstyle自动初始化
int y = (int) event.getY(); public NoteEditText(Context context, AttributeSet attrs, int defStyle) {
x -= getTotalPaddingLeft(); super(context, attrs, defStyle);
y -= getTotalPaddingTop(); // TODO Auto-generated construct or stub
x += getScrollX(); }
y += getScrollY();
@Override
Layout layout = getLayout(); //view里的函数处理手机屏幕的所有事件
int line = layout.getLineForVertical(y); /*event
int off = layout.getOffsetForHorizontal(line, x); */
Selection.setSelection(getText(), off); public boolean onTouchEvent(MotionEvent event) {
break; switch (event.getAction()) {
} //重写了需要处理屏幕被按下的事件
case MotionEvent.ACTION_DOWN:
return super.onTouchEvent(event); //跟新当前坐标值
} int x = (int) event.getX();
int y = (int) event.getY();
@Override x -= getTotalPaddingLeft();
public boolean onKeyDown(int keyCode, KeyEvent event) { y -= getTotalPaddingTop();
switch (keyCode) { x += getScrollX();
case KeyEvent.KEYCODE_ENTER: y += getScrollY();
if (mOnTextViewChangeListener != null) {
return false; //用布局控件layout根据x,y的新值设置新的位置
} Layout layout = getLayout();
break; int line = layout.getLineForVertical(y);
case KeyEvent.KEYCODE_DEL: int off = layout.getOffsetForHorizontal(line, x);
mSelectionStartBeforeDelete = getSelectionStart();
break; //更新光标新的位置
default: Selection.setSelection(getText(), off);
break; break;
} }
return super.onKeyDown(keyCode, event);
} return super.onTouchEvent(event);
/** }
* @method: onKeyUp
* @description: deleteenter @Override
* 退 /*
* @date: 2023/12/21 0:28 *
* @author: zhoukexing *
* @param: [keyCode, event] */
* @return: boolean public boolean onKeyDown(int keyCode, KeyEvent event) {
*/ switch (keyCode) {
@Override //根据按键的 Unicode 编码值来处理
public boolean onKeyUp(int keyCode, KeyEvent event) { case KeyEvent.KEYCODE_ENTER:
switch(keyCode) { //“进入”按键
case KeyEvent.KEYCODE_DEL: // delete键的号为67 @zhoukexing 2023/12/21 0:31 if (mOnTextViewChangeListener != null) {
if (mOnTextViewChangeListener != null) { return false;
if (0 == mSelectionStartBeforeDelete && mIndex != 0) { }
mOnTextViewChangeListener.onEditTextDelete(mIndex, getText().toString()); break;
return true; case KeyEvent.KEYCODE_DEL:
} //“删除”按键
} else { mSelectionStartBeforeDelete = getSelectionStart();
Log.d(TAG, "OnTextViewChangeListener was not seted"); break;
} default:
break; break;
case KeyEvent.KEYCODE_ENTER: // enter键的号为66 @zhoukexing 2023/12/21 0:31 }
if (mOnTextViewChangeListener != null) { //继续执行父类的其他点击事件
int selectionStart = getSelectionStart(); return super.onKeyDown(keyCode, event);
String text = getText().subSequence(selectionStart, length()).toString(); }
setText(getText().subSequence(0, selectionStart));
mOnTextViewChangeListener.onEditTextEnter(mIndex + 1, text); @Override
} else { /*
Log.d(TAG, "OnTextViewChangeListener was not seted"); *
} *
break; */
default: public boolean onKeyUp(int keyCode, KeyEvent event) {
break; switch(keyCode) {
} //根据按键的 Unicode 编码值来处理有删除和进入2种操作
return super.onKeyUp(keyCode, event); case KeyEvent.KEYCODE_DEL:
} if (mOnTextViewChangeListener != null) {
//若是被修改过
@Override if (0 == mSelectionStartBeforeDelete && mIndex != 0) {
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { //若之前有被修改并且文档不为空
if (mOnTextViewChangeListener != null) { mOnTextViewChangeListener.onEditTextDelete(mIndex, getText().toString());
if (!focused && TextUtils.isEmpty(getText())) { //利用上文OnTextViewChangeListener对KEYCODE_DEL按键情况的删除函数进行删除
mOnTextViewChangeListener.onTextChange(mIndex, false); return true;
} else { }
mOnTextViewChangeListener.onTextChange(mIndex, true); } else {
} Log.d(TAG, "OnTextViewChangeListener was not seted");
} //其他情况报错,文档的改动监听器并没有建立
super.onFocusChanged(focused, direction, previouslyFocusedRect); }
} break;
case KeyEvent.KEYCODE_ENTER:
@Override //同上也是分为监听器是否建立2种情况
protected void onCreateContextMenu(ContextMenu menu) { if (mOnTextViewChangeListener != null) {
if (getText() instanceof Spanned) { int selectionStart = getSelectionStart();
int selStart = getSelectionStart(); //获取当前位置
int selEnd = getSelectionEnd(); String text = getText().subSequence(selectionStart, length()).toString();
//获取当前文本
int min = Math.min(selStart, selEnd); setText(getText().subSequence(0, selectionStart));
int max = Math.max(selStart, selEnd); //根据获取的文本设置当前文本
mOnTextViewChangeListener.onEditTextEnter(mIndex + 1, text);
final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class); //当{@link KeyEvent#KEYCODE_ENTER}添加新文本
if (urls.length == 1) { } else {
int defaultResId = 0; Log.d(TAG, "OnTextViewChangeListener was not seted");
for(String schema: sSchemaActionResMap.keySet()) { //其他情况报错,文档的改动监听器并没有建立
if(urls[0].getURL().indexOf(schema) >= 0) { }
defaultResId = sSchemaActionResMap.get(schema); break;
break; default:
} break;
} }
//继续执行父类的其他按键弹起的事件
if (defaultResId == 0) { return super.onKeyUp(keyCode, event);
defaultResId = R.string.note_link_other; }
}
@Override
menu.add(0, 0, 0, defaultResId).setOnMenuItemClickListener( /*
new OnMenuItemClickListener() { *
public boolean onMenuItemClick(MenuItem item) { *
// goto a new intent * focusedViewFocusedtruefalse
urls[0].onClick(NoteEditText.this); direction
return true; RectViewnull
} */
}); protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
} if (mOnTextViewChangeListener != null) {
} //若监听器已经建立
super.onCreateContextMenu(menu); if (!focused && TextUtils.isEmpty(getText())) {
} //获取到焦点并且文本不为空
} mOnTextViewChangeListener.onTextChange(mIndex, false);
//mOnTextViewChangeListener子函数置false隐藏事件选项
} else {
mOnTextViewChangeListener.onTextChange(mIndex, true);
//mOnTextViewChangeListener子函数置true显示事件选项
}
}
//继续执行父类的其他焦点变化的事件
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
/*
*
*
*/
protected void onCreateContextMenu(ContextMenu menu) {
if (getText() instanceof Spanned) {
//有文本存在
int selStart = getSelectionStart();
int selEnd = getSelectionEnd();
//获取文本开始和结尾位置
int min = Math.min(selStart, selEnd);
int max = Math.max(selStart, selEnd);
//获取开始到结尾的最大值和最小值
final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class);
//设置url的信息的范围值
if (urls.length == 1) {
int defaultResId = 0;
for(String schema: sSchemaActionResMap.keySet()) {
//获取计划表中所有的key值
if(urls[0].getURL().indexOf(schema) >= 0) {
//若url可以添加则在添加后将defaultResId置为key所映射的值
defaultResId = sSchemaActionResMap.get(schema);
break;
}
}
if (defaultResId == 0) {
//defaultResId == 0则说明url并没有添加任何东西所以置为连接其他SchemaActionResMap的值
defaultResId = R.string.note_link_other;
}
//建立菜单
menu.add(0, 0, 0, defaultResId).setOnMenuItemClickListener(
new OnMenuItemClickListener() {
//新建按键监听器
public boolean onMenuItemClick(MenuItem item) {
// goto a new intent
urls[0].onClick(NoteEditText.this);
//根据相应的文本设置菜单的按键
return true;
}
});
}
}
//继续执行父类的其他菜单创建的事件
super.onCreateContextMenu(menu);
}
}

@ -14,251 +14,260 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.text.TextUtils; 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 android.widget.LinearLayout;
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;
/*
public class NotesListAdapter extends CursorAdapter { * 便CursorAdaptercursorListView
private static final String TAG = "NotesListAdapter"; * NotesListAdapter便
private Context mContext; */
/** 键-值对值为true时表示被选中 @zhoukexing 2024/1/2 20:50 */ public class NotesListAdapter extends CursorAdapter {
private HashMap<Integer, Boolean> mSelectedIndex; private static final String TAG = "NotesListAdapter";
// private HashMap<Integer, String> mSnippetPosition; private Context mContext;
private int mNotesCount; private HashMap<Integer, Boolean> mSelectedIndex;
private boolean mChoiceMode; private int mNotesCount; //便签数
// private String mQuery; // 搜索串 private boolean mChoiceMode; //选择模式标记
public static class AppWidgetAttribute { /*
public int widgetId; * widget
public int widgetType; */
} public static class AppWidgetAttribute {
public int widgetId;
public NotesListAdapter(Context context, Cursor cursor) { public int widgetType;
super(context, cursor); };
mSelectedIndex = new HashMap<Integer, Boolean>();
// mSnippetPosition = new HashMap<Integer, String>(); /*
mContext = context; * 便
mNotesCount = 0; *
// mQuery=""; */
} public NotesListAdapter(Context context) {
super(context, null); //父类对象置空
// @Override mSelectedIndex = new HashMap<Integer, Boolean>(); //新建选项下标的hash表
// public View getView(int position, View convertView, ViewGroup parent) { mContext = context;
// View view = super.getView(position, convertView, parent); mNotesCount = 0;
// // 仅显示便签项中包含搜索串的便签 }
// String snippet = mSnippetPosition.get(position);
// Log.e(TAG, snippet); @Override
// if(!TextUtils.isEmpty(mQuery) && !mSnippetPosition.get(position).contains(mQuery)) { /*
// view.setVisibility(View.GONE); *
// LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0,1); * 使NotesListItem
// view.setLayoutParams(param); */
// } public View newView(Context context, Cursor cursor, ViewGroup parent) {
// return view; return new NotesListItem(context);
// } }
@Override /*
public View newView(Context context, Cursor cursor, ViewGroup parent) { *
return new NotesListItem(context); *
} */
@Override
@Override public void bindView(View view, Context context, Cursor cursor) {
public void bindView(View view, Context context, Cursor cursor) { if (view instanceof NotesListItem) {
if (view instanceof NotesListItem) { //若view是NotesListItem的一个实例
NoteItemData itemData = new NoteItemData(context, cursor); NoteItemData itemData = new NoteItemData(context, cursor);
((NotesListItem) view).bind(context, itemData, mChoiceMode, ((NotesListItem) view).bind(context, itemData, mChoiceMode,
isSelectedItem(cursor.getPosition())); isSelectedItem(cursor.getPosition()));
} //则新建一个项目选项并且用bind跟将view和鼠标内容便签数据捆绑在一起
} }
}
public void setCheckedItem(final int position, final boolean checked) {
mSelectedIndex.put(position, checked); /*
notifyDataSetChanged(); *
} *
*/
public boolean isInChoiceMode() { public void setCheckedItem(final int position, final boolean checked) {
return mChoiceMode; mSelectedIndex.put(position, checked);
} //根据定位和是否勾选设置下标
notifyDataSetChanged();
public void setChoiceMode(boolean mode) { //在修改后刷新activity
mSelectedIndex.clear(); }
mChoiceMode = mode;
} /*
*
/** */
* @method: selectAll public boolean isInChoiceMode() {
* @description: boolean return mChoiceMode;
* setCheckedItemmSelectedIndex }
*
* or /*
* @date: 2024/1/3 22:36 *
* @author: zhoukexing * mode
* @param: [checked] */
* @return: void public void setChoiceMode(boolean mode) {
*/ mSelectedIndex.clear();
public void selectAll(boolean checked) { mChoiceMode = mode;
Cursor cursor = getCursor(); }
for (int i = 0; i < getCount(); i++) {
if (cursor.moveToPosition(i)) { /*
if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { *
setCheckedItem(i, checked); *
} */
} public void selectAll(boolean checked) {
} Cursor cursor = getCursor();
} //获取光标位置
for (int i = 0; i < getCount(); i++) {
public HashSet<Long> getSelectedItemIds() { if (cursor.moveToPosition(i)) {
HashSet<Long> itemSet = new HashSet<Long>(); if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) {
for (Integer position : mSelectedIndex.keySet()) { setCheckedItem(i, checked);
if (mSelectedIndex.get(position) == true) { }
Long id = getItemId(position); }
if (id == Notes.ID_ROOT_FOLDER) { }
Log.d(TAG, "Wrong item id, should not happen"); //遍历所有光标可用的位置在判断为便签类型之后勾选单项框
} else { }
itemSet.add(id);
} /*
} *
} *
*/
return itemSet; public HashSet<Long> getSelectedItemIds() {
} HashSet<Long> itemSet = new HashSet<Long>();
//建立hash表
public HashSet<AppWidgetAttribute> getSelectedWidget() { for (Integer position : mSelectedIndex.keySet()) {
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>(); //遍历所有的关键
for (Integer position : mSelectedIndex.keySet()) { if (mSelectedIndex.get(position) == true) {
if (mSelectedIndex.get(position) == true) { //若光标位置可用
Cursor c = (Cursor) getItem(position); Long id = getItemId(position);
if (c != null) { if (id == Notes.ID_ROOT_FOLDER) {
AppWidgetAttribute widget = new AppWidgetAttribute(); //原文件不需要添加
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); }
/** //则将id该下标假如选项集合中
* Don't close cursor here, only the adapter could close it
*/ }
} else { }
Log.e(TAG, "Invalid cursor");
return null; return itemSet;
} }
}
} /*
return itemSet; * Widget
} *
*/
public int getSelectedCount() { public HashSet<AppWidgetAttribute> getSelectedWidget() {
Collection<Boolean> values = mSelectedIndex.values(); HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
if (null == values) { for (Integer position : mSelectedIndex.keySet()) {
return 0; if (mSelectedIndex.get(position) == true) {
} Cursor c = (Cursor) getItem(position);
Iterator<Boolean> iter = values.iterator(); //以上4句和getSelectedItemIds一样不再重复
int count = 0; if (c != null) {
while (iter.hasNext()) { //光标位置可用的话就建立新的Widget属性并编辑下标和类型最后添加到选项集中
if (true == iter.next()) { AppWidgetAttribute widget = new AppWidgetAttribute();
count++; NoteItemData item = new NoteItemData(mContext, c);
} widget.widgetId = item.getWidgetId();
} widget.widgetType = item.getWidgetType();
return count; itemSet.add(widget);
} /**
* Don't close cursor here, only the adapter could close it
public int getMNotesCount() { */
return mNotesCount; } else {
} Log.e(TAG, "Invalid cursor");
return null;
/** }
* @method: isAllSelected }
* @description: truefalse }
* @date: 2024/1/2 20:47 return itemSet;
* @author: zhoukexing }
* @param: []
* @return: boolean /*
*/ *
public boolean isAllSelected() { *
int checkedCount = getSelectedCount(); */
return (checkedCount != 0 && checkedCount == mNotesCount); public int getSelectedCount() {
} Collection<Boolean> values = mSelectedIndex.values();
//首先获取选项下标的值
// public void setmQuery(String Query) { if (null == values) {
// mQuery = Query; return 0;
// } }
Iterator<Boolean> iter = values.iterator();
public boolean isSelectedItem(final int position) { //初始化叠加器
if (null == mSelectedIndex.get(position)) { int count = 0;
return false; while (iter.hasNext()) {
} if (true == iter.next()) {
return mSelectedIndex.get(position); //若value值为真计数+1
} count++;
}
@Override }
protected void onContentChanged() { return count;
super.onContentChanged(); }
calcNotesCount();
// updateSnippetMap(); // 在数据发生改变时更新 Snippet 到 Position 的映射 /*
} *
*
/** */
* @Method changeCursor public boolean isAllSelected() {
* @Date 2024/1/18 15:36 int checkedCount = getSelectedCount();
* @param cursor return (checkedCount != 0 && checkedCount == mNotesCount);
* @Author lenovo //获取选项数看是否等于便签的个数
* @Return void }
* @Description cursor
*/ /*
@Override *
public void changeCursor(Cursor cursor) { *
super.changeCursor(cursor); */
calcNotesCount(); public boolean isSelectedItem(final int position) {
// updateSnippetMap(); // 在数据发生改变时更新 Snippet 到 Position 的映射 if (null == mSelectedIndex.get(position)) {
} return false;
}
// private void updateSnippetMap() { return mSelectedIndex.get(position);
// mSnippetPosition = new HashMap<Integer, String>(); }
// for (int i = 0; i < getCount(); i++) {
// Cursor c = (Cursor) getItem(i); @Override
// if (c != null) { /*
// int position = c.getPosition(); * activity便
// NoteItemData item = new NoteItemData(mContext, c); *
// mSnippetPosition.put(position, item.getSnippet()); */
// } else { protected void onContentChanged() {
// Log.e(TAG, "Invalid cursor"); super.onContentChanged();
// break; //执行基类函数
// } calcNotesCount();
// } }
// }
@Override
/** /*
* @Method calcNotesCount * activity便
* @Date 2024/1/18 15:36 */
* @Author lenovo public void changeCursor(Cursor cursor) {
* @Return void super.changeCursor(cursor);
* @Description 便 //执行基类函数
*/ 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++; private void calcNotesCount() {
} mNotesCount = 0;
} else { for (int i = 0; i < getCount(); i++) {
Log.e(TAG, "Invalid cursor"); //获取总数同时遍历
return; Cursor c = (Cursor) getItem(i);
} if (c != null) {
} if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
} mNotesCount++;
} //若该位置不为空并且文本类型为便签就+1
}
} else {
Log.e(TAG, "Invalid cursor");
return;
}
//否则报错
}
}
}

@ -14,126 +14,119 @@
* 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;
/** //创建便签列表项目选项
* @Package: public class NotesListItem extends LinearLayout {
* @Class: NotesListItem private ImageView mAlert;//闹钟图片
* @Author lenovo private TextView mTitle; //标题
* @Date 2024/1/18 15:43 private TextView mTime; //时间
* @Description: 便 private TextView mCallName; //
*/ private NoteItemData mItemData; //标签数据
public class NotesListItem extends LinearLayout { private CheckBox mCheckBox; //打钩框
private ImageView mAlert;
private ImageView mTop; /*初始化基本信息*/
private TextView mTitle; public NotesListItem(Context context) {
private TextView mTime; super(context); //super()它的主要作用是调整调用父类构造函数的顺序
private TextView mCallName; inflate(context, R.layout.note_item, this);//Inflate可用于将一个xml中定义的布局控件找出来,这里的xml是r。layout
private NoteItemData mItemData; //findViewById用于从contentView中查找指定ID的View转换出来的形式根据需要而定;
private CheckBox mCheckBox; mAlert = (ImageView) findViewById(R.id.iv_alert_icon);
mTitle = (TextView) findViewById(R.id.tv_title);
public NotesListItem(Context context) { mTime = (TextView) findViewById(R.id.tv_time);
super(context); mCallName = (TextView) findViewById(R.id.tv_name);
inflate(context, R.layout.note_item, this); mCheckBox = (CheckBox) findViewById(android.R.id.checkbox);
mAlert = (ImageView) findViewById(R.id.iv_alert_icon); }
mTop = (ImageView) findViewById(R.id.iv_top_icon); ///根据data的属性对各个控件的属性的控制主要是可见性Visibility内容setText格式setTextAppearance
mTitle = (TextView) findViewById(R.id.tv_title); public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
mTime = (TextView) findViewById(R.id.tv_time); if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
mCallName = (TextView) findViewById(R.id.tv_name); mCheckBox.setVisibility(View.VISIBLE); ///设置可见行为可见
mCheckBox = (CheckBox) findViewById(android.R.id.checkbox); mCheckBox.setChecked(checked); ///格子打钩
} } else {
mCheckBox.setVisibility(View.GONE);
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) { }
if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
mCheckBox.setVisibility(View.VISIBLE); mItemData = data;
mCheckBox.setChecked(checked); ///设置控件属性一共三种情况由data的id和父id是否与保存到文件夹的id一致来决定
} else { if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
mCheckBox.setVisibility(View.GONE); mCallName.setVisibility(View.GONE);
} mAlert.setVisibility(View.VISIBLE);
//设置该textview的style
mItemData = data; mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) { //settext为设置内容
mCallName.setVisibility(View.GONE); mTitle.setText(context.getString(R.string.call_record_folder_name)
mAlert.setVisibility(View.VISIBLE); + context.getString(R.string.format_folder_files_count, data.getNotesCount()));
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); mAlert.setImageResource(R.drawable.call_record);
mTitle.setText(context.getString(R.string.call_record_folder_name) } else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) {
+ context.getString(R.string.format_folder_files_count, data.getNotesCount())); mCallName.setVisibility(View.VISIBLE);
mAlert.setImageResource(R.drawable.call_record); mCallName.setText(data.getCallName());
} else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) { mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem);
mCallName.setVisibility(View.VISIBLE); mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
mCallName.setText(data.getCallName()); ///关于闹钟的设置
mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem); if (data.hasAlert()) {
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); mAlert.setImageResource(R.drawable.clock);//图片来源的设置
if (data.hasAlert()) { mAlert.setVisibility(View.VISIBLE);
mAlert.setImageResource(R.drawable.clock); } else {
mAlert.setVisibility(View.VISIBLE); mAlert.setVisibility(View.GONE);
} else { }
mAlert.setVisibility(View.GONE); } else {
} mCallName.setVisibility(View.GONE);
} else { mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
mCallName.setVisibility(View.GONE); ///设置title格式
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); if (data.getType() == Notes.TYPE_FOLDER) {
mTitle.setText(data.getSnippet()
if (data.getType() == Notes.TYPE_FOLDER) { + context.getString(R.string.format_folder_files_count,
mTitle.setText(data.getSnippet() data.getNotesCount()));
+ context.getString(R.string.format_folder_files_count, mAlert.setVisibility(View.GONE);
data.getNotesCount())); } else {
mAlert.setVisibility(View.GONE); mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
} else { if (data.hasAlert()) {
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); mAlert.setImageResource(R.drawable.clock);///设置图片来源
if (data.hasAlert()) { mAlert.setVisibility(View.VISIBLE);
mAlert.setImageResource(R.drawable.clock); } else {
mAlert.setVisibility(View.VISIBLE); mAlert.setVisibility(View.GONE);
} else { }
mAlert.setVisibility(View.GONE); }
} }
} ///设置内容获取相关时间从data里编辑的日期中获取
} mTime. setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
// 设置置顶图标 setBackground(data);
if(data.getTop() == 1){ }
mTop.setImageResource(R.drawable.set_top); //根据data的文件属性来设置背景
mTop.setVisibility(View.VISIBLE); private void setBackground(NoteItemData data) {
} int id = data.getBgColorId();
else{ //若是note型文件则4种情况对于4种不同情况的背景来源
mTop.setVisibility(View.GONE); if (data.getType() == Notes.TYPE_NOTE) {
} //单个数据并且只有一个子文件夹
if (data.isSingle() || data.isOneFollowingFolder()) {
setBackground(data); setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id));
} } else if (data.isLast()) {//是最后一个数据
setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id));
private void setBackground(NoteItemData data) { } else if (data.isFirst() || data.isMultiFollowingFolder()) {//是一个数据并有多个子文件夹
int id = data.getBgColorId(); setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id));
if (data.getType() == Notes.TYPE_NOTE) { } else {
if (data.isSingle() || data.isOneFollowingFolder()) { setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id));
setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id)); }
} else if (data.isLast()) { } else {
setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id)); //若不是note直接调用文件夹的背景来源
} else if (data.isFirst() || data.isMultiFollowingFolder()) { setBackgroundResource(NoteItemBgResources.getFolderBgRes());
setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id)); }
} else { }
setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id)); public NoteItemData getItemData() {
} return mItemData;
} else { }
setBackgroundResource(NoteItemBgResources.getFolderBgRes()); }
}
}
public NoteItemData getItemData() {
return mItemData;
}
}

@ -14,375 +14,517 @@
* 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;
/*
public class NotesPreferenceActivity extends PreferenceActivity { *NotesPreferenceActivity便
public static final String PREFERENCE_NAME = "notes_preferences"; * PreferenceActivityActivity
*/
public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name"; public class NotesPreferenceActivity extends PreferenceActivity {
public static final String PREFERENCE_NAME = "notes_preferences";
public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time"; //优先名
public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name";
public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear"; //同步账号
public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time";
private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key"; //同步时间
public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear";
private static final String AUTHORITIES_FILTER_KEY = "authorities";
private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key";
private PreferenceCategory mAccountCategory; //同步密码
private static final String AUTHORITIES_FILTER_KEY = "authorities";
private GTaskReceiver mReceiver; //本地密码
private PreferenceCategory mAccountCategory;
private Account[] mOriAccounts; //账户分组
private GTaskReceiver mReceiver;
private boolean mHasAddedAccount; //同步任务接收器
private Account[] mOriAccounts;
@Override //账户
protected void onCreate(Bundle icicle) { private boolean mHasAddedAccount;
super.onCreate(icicle); //账户的hash标记
/* using the app icon for navigation */ @Override
getActionBar().setDisplayHomeAsUpEnabled(true); /*
*activity
addPreferencesFromResource(R.xml.preferences); *Bundle icicle activity
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY); *
mReceiver = new GTaskReceiver(); */
IntentFilter filter = new IntentFilter(); protected void onCreate(Bundle icicle) {
filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME); //先执行父类的创建函数
registerReceiver(mReceiver, filter); super.onCreate(icicle);
mOriAccounts = null; /* using the app icon for navigation */
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null); getActionBar().setDisplayHomeAsUpEnabled(true);
getListView().addHeaderView(header, null, true); //给左上角图标的左边加上一个返回的图标
}
addPreferencesFromResource(R.xml.preferences);
@Override //添加xml来源并显示 xml
protected void onResume() { mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY);
super.onResume(); //根据同步账户关键码来初始化分组
mReceiver = new GTaskReceiver();
// need to set sync account automatically if user has added a new IntentFilter filter = new IntentFilter();
// account filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME);
if (mHasAddedAccount) { registerReceiver(mReceiver, filter);
Account[] accounts = getGoogleAccounts(); //初始化同步组件
if (mOriAccounts != null && accounts.length > mOriAccounts.length) {
for (Account accountNew : accounts) { mOriAccounts = null;
boolean found = false; View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null);
for (Account accountOld : mOriAccounts) { //获取listvivewListView的作用:用于列出所有选择
if (TextUtils.equals(accountOld.name, accountNew.name)) { getListView().addHeaderView(header, null, true);
found = true; //在listview组件上方添加其他组件
break; }
}
} @Override
if (!found) { /*
setSyncAccount(accountNew.name); * activity
break; *
} */
} protected void onResume() {
} //先执行父类 的交互实现
} super.onResume();
refreshUI(); // need to set sync account automatically if user has added a new
} // account
if (mHasAddedAccount) {
@Override //若用户新加了账户则自动设置同步账户
protected void onDestroy() { Account[] accounts = getGoogleAccounts();
if (mReceiver != null) { //获取google同步账户
unregisterReceiver(mReceiver); if (mOriAccounts != null && accounts.length > mOriAccounts.length) {
} //若原账户不为空且当前账户有增加
super.onDestroy(); for (Account accountNew : accounts) {
} boolean found = false;
for (Account accountOld : mOriAccounts) {
private void loadAccountPreference() { if (TextUtils.equals(accountOld.name, accountNew.name)) {
mAccountCategory.removeAll(); //更新账户
found = true;
Preference accountPref = new Preference(this); break;
final String defaultAccount = getSyncAccountName(this); }
accountPref.setTitle(getString(R.string.preferences_account_title)); }
accountPref.setSummary(getString(R.string.preferences_account_summary)); if (!found) {
accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { setSyncAccount(accountNew.name);
public boolean onPreferenceClick(Preference preference) { //若是没有找到旧的账户,那么同步账号中就只添加新账户
if (!GTaskSyncService.isSyncing()) { break;
if (TextUtils.isEmpty(defaultAccount)) { }
// the first time to set account }
showSelectAccountAlertDialog(); }
} else { }
// if the account has already been set, we need to promp
// user about the risk refreshUI();
showChangeAccountConfirmAlertDialog(); //刷新标签界面
} }
} else {
Toast.makeText(NotesPreferenceActivity.this, @Override
R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT) /*
.show(); * activity
} *
return true; */
} protected void onDestroy() {
}); if (mReceiver != null) {
unregisterReceiver(mReceiver);
mAccountCategory.addPreference(accountPref); //注销接收器
} }
super.onDestroy();
private void loadSyncButton() { //执行父类的销毁动作
Button syncButton = (Button) findViewById(R.id.preference_sync_button); }
TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
/*
// set button state *
if (GTaskSyncService.isSyncing()) { *
syncButton.setText(getString(R.string.preferences_button_sync_cancel)); */
syncButton.setOnClickListener(new View.OnClickListener() { private void loadAccountPreference() {
public void onClick(View v) { mAccountCategory.removeAll();
GTaskSyncService.cancelSync(NotesPreferenceActivity.this); //销毁所有的分组
} Preference accountPref = new Preference(this);
}); //建立首选项
} else { final String defaultAccount = getSyncAccountName(this);
syncButton.setText(getString(R.string.preferences_button_sync_immediately)); accountPref.setTitle(getString(R.string.preferences_account_title));
syncButton.setOnClickListener(new View.OnClickListener() { accountPref.setSummary(getString(R.string.preferences_account_summary));
public void onClick(View v) { //设置首选项的大标题和小标题
GTaskSyncService.startSync(NotesPreferenceActivity.this); accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
} public boolean onPreferenceClick(Preference preference) {
}); //建立监听器
} if (!GTaskSyncService.isSyncing()) {
syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this))); if (TextUtils.isEmpty(defaultAccount)) {
// the first time to set account
// set last sync time //若是第一次建立账户显示选择账户提示对话框
if (GTaskSyncService.isSyncing()) { showSelectAccountAlertDialog();
lastSyncTimeView.setText(GTaskSyncService.getProgressString()); } else {
lastSyncTimeView.setVisibility(View.VISIBLE); // if the account has already been set, we need to promp
} else { // user about the risk
long lastSyncTime = getLastSyncTime(this); //若是已经建立则显示修改对话框并进行修改操作
if (lastSyncTime != 0) { showChangeAccountConfirmAlertDialog();
lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time, }
DateFormat.format(getString(R.string.preferences_last_sync_time_format), } else {
lastSyncTime))); //若在没有同步的情况下则在toast中显示不能修改
lastSyncTimeView.setVisibility(View.VISIBLE); Toast.makeText(NotesPreferenceActivity.this,
} else { R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
lastSyncTimeView.setVisibility(View.GONE); .show();
} }
} return true;
} }
});
private void refreshUI() {
loadAccountPreference(); //根据新建首选项编辑新的账户分组
loadSyncButton(); mAccountCategory.addPreference(accountPref);
} }
private void showSelectAccountAlertDialog() { /*
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); *
*
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null); */
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title); private void loadSyncButton() {
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title)); Button syncButton = (Button) findViewById(R.id.preference_sync_button);
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle); TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips)); //获取同步按钮控件和最终同步时间的的窗口
// set button state
dialogBuilder.setCustomTitle(titleView); //设置按钮的状态
dialogBuilder.setPositiveButton(null, null); if (GTaskSyncService.isSyncing()) {
//若是在同步状态下
Account[] accounts = getGoogleAccounts(); syncButton.setText(getString(R.string.preferences_button_sync_cancel));
String defAccount = getSyncAccountName(this); syncButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mOriAccounts = accounts; GTaskSyncService.cancelSync(NotesPreferenceActivity.this);
mHasAddedAccount = false; }
});
if (accounts.length > 0) { //设置按钮显示的文本为“取消同步”以及监听器
CharSequence[] items = new CharSequence[accounts.length]; } else {
final CharSequence[] itemMapping = items; syncButton.setText(getString(R.string.preferences_button_sync_immediately));
int checkedItem = -1; syncButton.setOnClickListener(new View.OnClickListener() {
int index = 0; public void onClick(View v) {
for (Account account : accounts) { GTaskSyncService.startSync(NotesPreferenceActivity.this);
if (TextUtils.equals(account.name, defAccount)) { }
checkedItem = index; });
} //若是不同步则设置按钮显示的文本为“立即同步”以及对应监听器
items[index++] = account.name; }
} syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this)));
dialogBuilder.setSingleChoiceItems(items, checkedItem, //设置按键可用还是不可用
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { // set last sync time
setSyncAccount(itemMapping[which].toString()); // 设置最终同步时间
dialog.dismiss(); if (GTaskSyncService.isSyncing()) {
refreshUI(); //若是在同步的情况下
} lastSyncTimeView.setText(GTaskSyncService.getProgressString());
}); lastSyncTimeView.setVisibility(View.VISIBLE);
} // 根据当前同步服务器设置时间显示框的文本以及可见性
} else {
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null); //若是非同步情况
dialogBuilder.setView(addAccountView); long lastSyncTime = getLastSyncTime(this);
if (lastSyncTime != 0) {
final AlertDialog dialog = dialogBuilder.show(); lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time,
addAccountView.setOnClickListener(new View.OnClickListener() { DateFormat.format(getString(R.string.preferences_last_sync_time_format),
public void onClick(View v) { lastSyncTime)));
mHasAddedAccount = true; lastSyncTimeView.setVisibility(View.VISIBLE);
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS"); //则根据最后同步时间的信息来编辑时间显示框的文本内容和可见性
intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] { } else {
"gmail-ls" //若时间为空直接设置为不可见状态
}); lastSyncTimeView.setVisibility(View.GONE);
startActivityForResult(intent, -1); }
dialog.dismiss(); }
} }
}); /*
} *
*
private void showChangeAccountConfirmAlertDialog() { */
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); private void refreshUI() {
loadAccountPreference();
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null); loadSyncButton();
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title); }
titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
getSyncAccountName(this))); /*
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle); *
subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg)); *
dialogBuilder.setCustomTitle(titleView); */
private void showSelectAccountAlertDialog() {
CharSequence[] menuItemArray = new CharSequence[] { AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
getString(R.string.preferences_menu_change_account), //创建一个新的对话框
getString(R.string.preferences_menu_remove_account),
getString(R.string.preferences_menu_cancel) View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
}; TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() { titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
public void onClick(DialogInterface dialog, int which) { TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
if (which == 0) { subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips));
showSelectAccountAlertDialog(); //设置标题以及子标题的内容
} else if (which == 1) { dialogBuilder.setCustomTitle(titleView);
removeSyncAccount(); dialogBuilder.setPositiveButton(null, null);
refreshUI(); //设置对话框的自定义标题建立一个YES的按钮
} Account[] accounts = getGoogleAccounts();
} String defAccount = getSyncAccountName(this);
}); //获取同步账户信息
dialogBuilder.show(); mOriAccounts = accounts;
} mHasAddedAccount = false;
private Account[] getGoogleAccounts() { if (accounts.length > 0) {
AccountManager accountManager = AccountManager.get(this); //若账户不为空
return accountManager.getAccountsByType("com.google"); CharSequence[] items = new CharSequence[accounts.length];
} final CharSequence[] itemMapping = items;
int checkedItem = -1;
private void setSyncAccount(String account) { int index = 0;
if (!getSyncAccountName(this).equals(account)) { for (Account account : accounts) {
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); if (TextUtils.equals(account.name, defAccount)) {
SharedPreferences.Editor editor = settings.edit(); checkedItem = index;
if (account != null) { //在账户列表中查询到所需账户
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account); }
} else { items[index++] = account.name;
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); }
} dialogBuilder.setSingleChoiceItems(items, checkedItem,
editor.commit(); //在对话框建立一个单选的复选框
new DialogInterface.OnClickListener() {
// clean up last sync time public void onClick(DialogInterface dialog, int which) {
setLastSyncTime(this, 0); setSyncAccount(itemMapping[which].toString());
dialog.dismiss();
// clean up local gtask related info //取消对话框
new Thread(new Runnable() { refreshUI();
public void run() { }
ContentValues values = new ContentValues(); //设置点击后执行的事件,包括检录新同步账户和刷新标签界面
values.put(NoteColumns.GTASK_ID, ""); });
values.put(NoteColumns.SYNC_ID, 0); //建立对话框网络版的监听器
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null); }
}
}).start(); View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null);
dialogBuilder.setView(addAccountView);
Toast.makeText(NotesPreferenceActivity.this, //给新加账户对话框设置自定义样式
getString(R.string.preferences_toast_success_set_accout, account),
Toast.LENGTH_SHORT).show(); final AlertDialog dialog = dialogBuilder.show();
} //显示对话框
} addAccountView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
private void removeSyncAccount() { mHasAddedAccount = true;
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); //将新加账户的hash置true
SharedPreferences.Editor editor = settings.edit(); Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) { //建立网络建立组件
editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME); intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] {
} "gmail-ls"
if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) { });
editor.remove(PREFERENCE_LAST_SYNC_TIME); startActivityForResult(intent, -1);
} //跳回上一个选项
editor.commit(); dialog.dismiss();
}
// clean up local gtask related info });
new Thread(new Runnable() { //建立新加账户对话框的监听器
public void run() { }
ContentValues values = new ContentValues();
values.put(NoteColumns.GTASK_ID, ""); /*
values.put(NoteColumns.SYNC_ID, 0); *
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null); *
} */
}).start(); private void showChangeAccountConfirmAlertDialog() {
} AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
//创建一个新的对话框
public static String getSyncAccountName(Context context) { View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
Context.MODE_PRIVATE); titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); getSyncAccountName(this)));
} TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg));
public static void setLastSyncTime(Context context, long time) { //根据同步修改的账户信息设置标题以及子标题的内容
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, dialogBuilder.setCustomTitle(titleView);
Context.MODE_PRIVATE); //设置对话框的自定义标题
SharedPreferences.Editor editor = settings.edit(); CharSequence[] menuItemArray = new CharSequence[] {
editor.putLong(PREFERENCE_LAST_SYNC_TIME, time); getString(R.string.preferences_menu_change_account),
editor.commit(); getString(R.string.preferences_menu_remove_account),
} getString(R.string.preferences_menu_cancel)
};
public static long getLastSyncTime(Context context) { //定义一些标记字符串
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() {
Context.MODE_PRIVATE); //设置对话框要显示的一个list用于显示几个命令时,即changeremovecancel
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0); public void onClick(DialogInterface dialog, int which) {
} //按键功能由which来决定
if (which == 0) {
private class GTaskReceiver extends BroadcastReceiver { //进入账户选择对话框
showSelectAccountAlertDialog();
@Override } else if (which == 1) {
public void onReceive(Context context, Intent intent) { //删除账户并且跟新便签界面
refreshUI(); removeSyncAccount();
if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) { refreshUI();
TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview); }
syncStatus.setText(intent }
.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG)); });
} dialogBuilder.show();
//显示对话框
} }
}
/*
public boolean onOptionsItemSelected(MenuItem item) { *
switch (item.getItemId()) { *
case android.R.id.home: */
Intent intent = new Intent(this, NotesListActivity.class); private Account[] getGoogleAccounts() {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AccountManager accountManager = AccountManager.get(this);
startActivity(intent); return accountManager.getAccountsByType("com.google");
return true; }
default:
return false; /*
} *
} *
} */
private void setSyncAccount(String account) {
if (!getSyncAccountName(this).equals(account)) {
//假如该账号不在同步账号列表中
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
//编辑共享的首选项
if (account != null) {
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account);
} else {
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
}
//将该账号加入到首选项中
editor.commit();
//提交修改的数据
setLastSyncTime(this, 0);
//将最后同步时间清零
// clean up local gtask related info
new Thread(new Runnable() {
public void run() {
ContentValues values = new ContentValues();
values.put(NoteColumns.GTASK_ID, "");
values.put(NoteColumns.SYNC_ID, 0);
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
}
}).start();
//重置当地同步任务的信息
Toast.makeText(NotesPreferenceActivity.this,
getString(R.string.preferences_toast_success_set_accout, account),
Toast.LENGTH_SHORT).show();
//将toast的文本信息置为“设置账户成功”并显示出来
}
}
/*
*
*
*/
private void removeSyncAccount() {
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
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)) {
editor.remove(PREFERENCE_LAST_SYNC_TIME);
//删除当前首选项中有账户时间
}
editor.commit();
//提交更新后的数据
// clean up local gtask related info
new Thread(new Runnable() {
public void run() {
ContentValues values = new ContentValues();
values.put(NoteColumns.GTASK_ID, "");
values.put(NoteColumns.SYNC_ID, 0);
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
}
}).start();
//重置当地同步任务的信息
}
/*
*
*
*/
public static String getSyncAccountName(Context context) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
}
/*
*
*
*/
public static void setLastSyncTime(Context context, long time) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
// 从共享首选项中找到相关账户并获取其编辑器
editor.putLong(PREFERENCE_LAST_SYNC_TIME, time);
editor.commit();
//编辑最终同步时间并提交更新
}
/*
*
*
*/
public static long getLastSyncTime(Context context) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0);
}
/*
*
* BroadcastReceiver
*/
private class GTaskReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
refreshUI();
if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) {
//获取随广播而来的Intent中的同步服务的数据
TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
syncStatus.setText(intent
.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG));
//通过获取的数据在设置系统的状态
}
}
}
/*
*
*
* :MenuItem
*/
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
//根据选项的id选择这里只有一个主页
case android.R.id.home:
Intent intent = new Intent(this, NotesListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
//在主页情况下在创建连接组件intent发出清空的信号并开始一个相应的activity
default:
return false;
}
}
}

Loading…
Cancel
Save