yang commot

master
yangmingrui 7 months ago
parent cdb9c556ff
commit a21ccf1b66

@ -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 "";
}
/**
* 便ID
* @param resolver ContentResolver
* @param phoneNumber
* @param callDate
* @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);
public static String getFormattedSnippet(String snippet) { if (cursor != null) {
if (snippet != null) { if (cursor.moveToFirst()) {
snippet = snippet.trim(); try {
int index = snippet.indexOf('\n'); return cursor.getLong(0);
if (index != -1) { } catch (IndexOutOfBoundsException e) {
snippet = snippet.substring(0, index); 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";

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

Loading…
Cancel
Save