第一次合并

master
mlrpikq4f 3 months ago
commit 33c7299d29

@ -0,0 +1,427 @@
/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.micode.notes.tool;
import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
/**
*
* SD
*/
public class BackupUtils {
private static final String TAG = "BackupUtils";
// 单例实例
private static BackupUtils sInstance;
/**
*
* @param context
* @return BackupUtils
*/
public static synchronized BackupUtils getInstance(Context context) {
if (sInstance == null) {
sInstance = new BackupUtils(context);
}
return sInstance;
}
/**
* /
* STATE_SD_CARD_UNMOUONTED: SD
* STATE_BACKUP_FILE_NOT_EXIST:
* STATE_DATA_DESTROIED:
* STATE_SYSTEM_ERROR:
* STATE_SUCCESS:
*/
public static final int STATE_SD_CARD_UNMOUONTED = 0;
public static final int STATE_BACKUP_FILE_NOT_EXIST = 1;
public static final int STATE_DATA_DESTROIED = 2;
public static final int STATE_SYSTEM_ERROR = 3;
public static final int STATE_SUCCESS = 4;
private final TextExport mTextExport; // 文本导出模块
/**
*
* @param context
*/
private BackupUtils(Context context) {
mTextExport = new TextExport(context);
}
/**
*
* @return true if SD
*/
private static boolean externalStorageAvailable() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}
/**
*
* @return STATE_*
*/
public int exportToText() {
return mTextExport.exportToText();
}
/**
*
* @return "notes_20231001.txt"
*/
public String getExportedTextFileName() {
return mTextExport.mFileName;
}
/**
*
* @return "/sdcard/notes_backup/"
*/
public String getExportedTextFileDir() {
return mTextExport.mFileDirectory;
}
/**
*
*/
private static class TextExport {
// 笔记查询投影(仅获取必要字段)
private static final String[] NOTE_PROJECTION = {
NoteColumns.ID, // 笔记ID
NoteColumns.MODIFIED_DATE, // 最后修改时间
NoteColumns.SNIPPET, // 摘要(文件夹名称/笔记标题)
NoteColumns.TYPE // 类型(文件夹/笔记)
};
// 投影字段索引
private static final int NOTE_COLUMN_ID = 0;
private static final int NOTE_COLUMN_MODIFIED_DATE = 1;
private static final int NOTE_COLUMN_SNIPPET = 2;
// 笔记内容数据查询投影
private static final String[] DATA_PROJECTION = {
DataColumns.CONTENT, // 内容(文本/位置信息)
DataColumns.MIME_TYPE, // MIME类型区分普通笔记/通话记录)
DataColumns.DATA1, // 预留字段1通话记录中为日期
DataColumns.DATA2, // 预留字段2
DataColumns.DATA3, // 预留字段3
DataColumns.DATA4, // 电话号码(通话记录专用)
};
// 数据投影字段索引
private static final int DATA_COLUMN_CONTENT = 0;
private static final int DATA_COLUMN_MIME_TYPE = 1;
private static final int DATA_COLUMN_CALL_DATE = 2; // DATA1的别名通话时间
private static final int DATA_COLUMN_PHONE_NUMBER = 4; // DATA4的别名电话号码
// 导出格式模板对应res/array/format_for_exported_note.xml
private final String[] TEXT_FORMAT;
// 格式模板索引
private static final int FORMAT_FOLDER_NAME = 0; // 文件夹名称格式
private static final int FORMAT_NOTE_DATE = 1; // 笔记日期格式
private static final int FORMAT_NOTE_CONTENT = 2; // 笔记内容格式
private final Context mContext;
private String mFileName; // 导出文件名
private String mFileDirectory; // 导出文件目录
/**
*
* @param context
*/
public TextExport(Context context) {
// 从资源文件获取导出格式模板
TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note);
mContext = context;
}
/**
*
* @param id FORMAT_*
* @return
*/
private String getFormat(int id) {
return TEXT_FORMAT[id];
}
/**
*
* @param folderId ID
* @param ps
*/
private void exportFolderToText(String folderId, PrintStream ps) {
// 查询该文件夹下的所有笔记
Cursor notesCursor = mContext.getContentResolver().query(
Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION,
NoteColumns.PARENT_ID + "=?", // 条件父级ID为当前文件夹ID
new String[]{folderId},
null
);
if (notesCursor != null) {
try {
if (notesCursor.moveToFirst()) {
do {
// 打印笔记最后修改时间
String dateStr = DateFormat.format(
mContext.getString(R.string.format_datetime_mdhm), // 格式MM月dd日 HH:mm
notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)
).toString();
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), dateStr));
// 导出笔记内容
String noteId = notesCursor.getString(NOTE_COLUMN_ID);
exportNoteToText(noteId, ps);
} while (notesCursor.moveToNext());
}
} finally {
notesCursor.close(); // 释放游标资源
}
}
}
/**
*
* @param noteId ID
* @param ps
*/
private void exportNoteToText(String noteId, PrintStream ps) {
// 查询笔记对应的内容数据(如文本内容、通话记录)
Cursor dataCursor = mContext.getContentResolver().query(
Notes.CONTENT_DATA_URI,
DATA_PROJECTION,
DataColumns.NOTE_ID + "=?", // 条件属于该笔记ID
new String[]{noteId},
null
);
if (dataCursor != null) {
try {
if (dataCursor.moveToFirst()) {
do {
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
if (DataConstants.CALL_NOTE.equals(mimeType)) {
// 处理通话记录笔记
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE);
String location = dataCursor.getString(DATA_COLUMN_CONTENT); // 通话位置或备注
// 打印电话号码
if (!TextUtils.isEmpty(phoneNumber)) {
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), phoneNumber));
}
// 打印通话时间
String callDateStr = DateFormat.format(
mContext.getString(R.string.format_datetime_mdhm),
callDate
).toString();
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), callDateStr));
// 打印位置信息
if (!TextUtils.isEmpty(location)) {
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), location));
}
} else if (DataConstants.NOTE.equals(mimeType)) {
// 处理普通文本笔记
String content = dataCursor.getString(DATA_COLUMN_CONTENT);
if (!TextUtils.isEmpty(content)) {
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), content));
}
}
} while (dataCursor.moveToNext());
}
} finally {
dataCursor.close(); // 释放游标资源
}
}
// 打印分隔符(换行符+特殊字符,可能用于区分笔记)
try {
ps.write(new byte[]{Character.LINE_SEPARATOR, Character.LETTER_NUMBER});
} catch (IOException e) {
Log.e(TAG, "写入分隔符失败:" + e.getMessage());
}
}
/**
*
* @return
*/
public int exportToText() {
// 检查SD卡是否可用
if (!externalStorageAvailable()) {
Log.d(TAG, "SD卡未挂载无法导出");
return STATE_SD_CARD_UNMOUONTED;
}
// 获取输出流
PrintStream ps = getExportToTextPrintStream();
if (ps == null) {
Log.e(TAG, "创建输出流失败");
return STATE_SYSTEM_ERROR;
}
// 导出文件夹及其笔记
String whereClause = "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " +
NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " +
NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER; // 排除回收站,包含通话记录文件夹
Cursor folderCursor = mContext.getContentResolver().query(
Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION,
whereClause,
null,
null
);
if (folderCursor != null) {
try {
if (folderCursor.moveToFirst()) {
do {
String folderName;
// 处理特殊文件夹:通话记录文件夹使用固定名称
if (folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) {
folderName = mContext.getString(R.string.call_record_folder_name);
} else {
folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); // 使用文件夹摘要作为名称
}
// 打印文件夹名称
if (!TextUtils.isEmpty(folderName)) {
ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName));
}
String folderId = folderCursor.getString(NOTE_COLUMN_ID);
exportFolderToText(folderId, ps); // 导出文件夹下的笔记
} while (folderCursor.moveToNext());
}
} finally {
folderCursor.close();
}
}
// 导出根目录下的普通笔记父级ID=0
Cursor noteCursor = mContext.getContentResolver().query(
Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION,
NoteColumns.TYPE + "=" + Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID + "=0",
null,
null
);
if (noteCursor != null) {
try {
if (noteCursor.moveToFirst()) {
do {
String dateStr = DateFormat.format(
mContext.getString(R.string.format_datetime_mdhm),
noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)
).toString();
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), dateStr));
String noteId = noteCursor.getString(NOTE_COLUMN_ID);
exportNoteToText(noteId, ps); // 导出笔记内容
} while (noteCursor.moveToNext());
}
} finally {
noteCursor.close();
}
}
ps.close(); // 关闭输出流
return STATE_SUCCESS;
}
/**
*
* @return PrintStreamnull
*/
private PrintStream getExportToTextPrintStream() {
// 生成文件路径(如/sdcard/notes_backup/notes_20231001.txt
File file = generateFileMountedOnSDcard(
mContext,
R.string.file_path, // 目录路径(如"/notes_backup/"
R.string.file_name_txt_format // 文件名格式(如"notes_%s.txt"
);
if (file == null) {
return null;
}
mFileName = file.getName();
mFileDirectory = mContext.getString(R.string.file_path); // 存储目录路径
// 创建文件输出流
try {
FileOutputStream fos = new FileOutputStream(file);
return new PrintStream(fos);
} catch (FileNotFoundException e) {
Log.e(TAG, "文件未找到:" + e.getMessage());
} catch (NullPointerException e) {
Log.e(TAG, "空指针异常:" + e.getMessage());
}
return null;
}
}
/**
* SD
* @param context
* @param filePathResId IDR.string.file_path
* @param fileNameFormatResId IDR.string.file_name_txt_format
* @return null
*/
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {
// 构建文件路径SD卡根目录 + 自定义目录 + 文件名
StringBuilder sb = new StringBuilder();
sb.append(Environment.getExternalStorageDirectory()); // SD卡根目录
sb.append(context.getString(filePathResId)); // 自定义目录(如"/notes_backup/"
File filedir = new File(sb.toString()); // 目录对象
// 构建文件名:格式字符串 + 当前日期(如"notes_20231001.txt"
sb.append(context.getString(
fileNameFormatResId,
DateFormat.format(context.getString(R.string.format_date_ymd), System.currentTimeMillis()) // 日期格式yyyyMMdd
));
File file = new File(sb.toString()); // 文件对象
// 创建目录和文件(若不存在)
try {
if (!filedir.exists() && !filedir.mkdirs()) {
Log.e(TAG, "创建目录失败:" + filedir.getPath());
return null;
}
if (!file.exists() && !file.createNewFile()) {
Log.e(TAG, "创建文件失败:" + file.getPath());
return null;
}
return file;
} catch (SecurityException | IOException e) {
Log.e(TAG, "文件操作失败:" + e.getMessage());
return null;
}
}
}

@ -0,0 +1,392 @@
/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.micode.notes.tool;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.os.RemoteException;
import android.util.Log;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.CallNote;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute;
import java.util.ArrayList;
import java.util.HashSet;
/**
*
* ContentProvider
*/
public class DataUtils {
public static final String TAG = "DataUtils";
/**
*
* @param resolver
* @param ids ID
* @return
*/
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
if (ids == null) {
Log.d(TAG, "待删除的ID集合为空");
return true;
}
if (ids.size() == 0) {
Log.d(TAG, "ID集合中无有效数据");
return true;
}
ArrayList<ContentProviderOperation> operationList = new ArrayList<>();
for (long id : ids) {
// 跳过系统根文件夹(禁止删除)
if (id == Notes.ID_ROOT_FOLDER) {
Log.e(TAG, "尝试删除系统根文件夹,已忽略");
continue;
}
// 创建删除操作根据ID构建删除URI
ContentProviderOperation.Builder builder = ContentProviderOperation
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
operationList.add(builder.build());
}
try {
// 执行批量操作
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
// 检查操作结果
if (results == null || results.length == 0 || results[0] == null) {
Log.d(TAG, "删除失败ID集合" + ids.toString());
return false;
}
return true;
} catch (RemoteException | OperationApplicationException e) {
Log.e(TAG, "批量删除异常:" + e.getMessage());
e.printStackTrace();
}
return false;
}
/**
*
* @param resolver
* @param id ID
* @param srcFolderId ID
* @param desFolderId ID
*/
public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) {
ContentValues values = new ContentValues();
// 更新父级ID为目标文件夹ID
values.put(NoteColumns.PARENT_ID, desFolderId);
// 记录原始父级ID用于同步或回滚
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);
// 标记本地修改(可能用于同步逻辑)
values.put(NoteColumns.LOCAL_MODIFIED, 1);
// 执行更新操作
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
}
/**
*
* @param resolver
* @param ids ID
* @param folderId ID
* @return
*/
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids, long folderId) {
if (ids == null) {
Log.d(TAG, "待移动的ID集合为空");
return true;
}
ArrayList<ContentProviderOperation> operationList = new ArrayList<>();
for (long id : ids) {
// 创建更新操作设置父级ID并标记本地修改
ContentProviderOperation.Builder builder = ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
builder.withValue(NoteColumns.PARENT_ID, folderId);
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1);
operationList.add(builder.build());
}
try {
// 执行批量操作
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
// 检查操作结果
if (results == null || results.length == 0 || results[0] == null) {
Log.d(TAG, "移动失败ID集合" + ids.toString());
return false;
}
return true;
} catch (RemoteException | OperationApplicationException e) {
Log.e(TAG, "批量移动异常:" + e.getMessage());
e.printStackTrace();
}
return false;
}
/**
*
* @param resolver
* @return
*/
public static int getUserFolderCount(ContentResolver resolver) {
// 查询条件类型为文件夹且父级ID不是回收站ID
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);
int count = 0;
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
count = cursor.getInt(0); // 获取计数结果
}
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "查询文件夹数量失败:" + e.getMessage());
} finally {
cursor.close(); // 关闭游标释放资源
}
}
return count;
}
/**
*
* @param resolver
* @param noteId ID
* @param type Notes.TYPE_NOTENotes.TYPE_FOLDER
* @return
*/
public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) {
// 查询条件类型匹配且父级ID不是回收站ID
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;
if (cursor != null) {
exist = cursor.getCount() > 0; // 存在有效记录则可见
cursor.close();
}
return exist;
}
/**
*
* @param resolver
* @param noteId ID
* @return
*/
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {
// 直接查询笔记ID是否存在
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
null, null, null, null);
boolean exist = false;
if (cursor != null) {
exist = cursor.getCount() > 0;
cursor.close();
}
return exist;
}
/**
*
* @param resolver
* @param dataId ID
* @return
*/
public static boolean existInDataDatabase(ContentResolver resolver, long dataId) {
// 查询数据项URI对应的记录
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId),
null, null, null, null);
boolean exist = false;
if (cursor != null) {
exist = cursor.getCount() > 0;
cursor.close();
}
return exist;
}
/**
*
* @param resolver
* @param name
* @return
*/
public static boolean checkVisibleFolderName(ContentResolver resolver, String name) {
// 查询条件:类型为文件夹,不在回收站,且名称匹配
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null,
NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER +
" AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER +
" AND " + NoteColumns.SNIPPET + "=?",
new String[]{name}, null);
boolean exist = false;
if (cursor != null) {
exist = cursor.getCount() > 0;
cursor.close();
}
return exist;
}
/**
*
* @param resolver
* @param folderId ID
* @return AppWidgetAttribute
*/
public static HashSet<AppWidgetAttribute> getFolderNoteWidget(ContentResolver resolver, long folderId) {
// 查询该文件夹下所有笔记的小部件ID和类型
Cursor c = resolver.query(Notes.CONTENT_NOTE_URI,
new String[]{NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE},
NoteColumns.PARENT_ID + "=?",
new String[]{String.valueOf(folderId)},
null);
HashSet<AppWidgetAttribute> set = null;
if (c != null) {
set = new HashSet<>();
try {
if (c.moveToFirst()) {
do {
AppWidgetAttribute widget = new AppWidgetAttribute();
widget.widgetId = c.getInt(0); // 小部件ID
widget.widgetType = c.getInt(1); // 小部件类型
set.add(widget);
} while (c.moveToNext());
}
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "解析小部件数据失败:" + e.getMessage());
} finally {
c.close();
}
}
return set;
}
/**
* ID
* @param resolver
* @param noteId ID
* @return
*/
public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) {
// 查询通话笔记对应的电话号码通过MIME类型过滤
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 && cursor.moveToFirst()) {
try {
return cursor.getString(0); // 获取电话号码
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "获取电话号码失败:" + e.getMessage());
} finally {
cursor.close();
}
}
return "";
}
/**
* ID
* @param resolver
* @param phoneNumber
* @param callDate
* @return ID0
*/
public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) {
// 使用PHONE_NUMBERS_EQUAL函数进行模糊匹配处理格式差异
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);
long noteId = 0;
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
noteId = cursor.getLong(0); // 获取笔记ID
}
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "获取通话笔记ID失败" + e.getMessage());
} finally {
cursor.close();
}
}
return noteId;
}
/**
* ID
* @param resolver
* @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 = "";
try {
if (cursor.moveToFirst()) {
snippet = cursor.getString(0); // 获取摘要
}
} finally {
cursor.close();
}
return snippet;
}
throw new IllegalArgumentException("未找到ID为 " + noteId + " 的笔记");
}
/**
*
* @param snippet
* @return
*/
public static String getFormattedSnippet(String snippet) {
if (snippet != null) {
snippet = snippet.trim(); // 去除首尾空格
int index = snippet.indexOf('\n');
if (index != -1) {
snippet = snippet.substring(0, index); // 截断到第一个换行符
}
}
return snippet;
}
}

@ -0,0 +1,87 @@
/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.micode.notes.tool;
/**
* Google Tasks
* Google Tasks API使JSON
*/
public class GTaskStringUtils {
// --------------- JSON请求/响应字段名常量 ---------------
// 操作相关字段
public final static String GTASK_JSON_ACTION_ID = "action_id"; // 操作ID
public final static String GTASK_JSON_ACTION_LIST = "action_list"; // 操作列表
public final static String GTASK_JSON_ACTION_TYPE = "action_type"; // 操作类型
public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; // 创建操作
public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; // 获取所有操作
public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; // 移动操作
public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; // 更新操作
// 实体相关字段
public final static String GTASK_JSON_CREATOR_ID = "creator_id"; // 创建者ID
public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; // 子实体
public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; // 实体变更
public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; // 实体类型
// 任务/列表属性字段
public final static String GTASK_JSON_CLIENT_VERSION = "client_version";// 客户端版本
public final static String GTASK_JSON_COMPLETED = "completed"; // 完成状态
public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id";// 当前列表ID
public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id";// 默认列表ID
public final static String GTASK_JSON_DELETED = "deleted"; // 删除状态
public final static String GTASK_JSON_DEST_LIST = "dest_list"; // 目标列表
public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; // 目标父级
public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type";// 目标父级类型
public final static String GTASK_JSON_ID = "id"; // ID
public final static String GTASK_JSON_INDEX = "index"; // 索引位置
public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; // 最后修改时间
public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point";// 最新同步点
public final static String GTASK_JSON_LIST_ID = "list_id"; // 列表ID
public final static String GTASK_JSON_LISTS = "lists"; // 列表集合
public final static String GTASK_JSON_NAME = "name"; // 名称
public final static String GTASK_JSON_NEW_ID = "new_id"; // 新ID
public final static String GTASK_JSON_NOTES = "notes"; // 备注
public final static String GTASK_JSON_PARENT_ID = "parent_id"; // 父级ID
public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id";// 前一个兄弟节点ID
public final static String GTASK_JSON_RESULTS = "results"; // 结果集
public final static String GTASK_JSON_SOURCE_LIST = "source_list"; // 源列表
public final static String GTASK_JSON_TASKS = "tasks"; // 任务集合
public final static String GTASK_JSON_TYPE = "type"; // 类型
public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; // 组类型
public final static String GTASK_JSON_TYPE_TASK = "TASK"; // 任务类型
public final static String GTASK_JSON_USER = "user"; // 用户
// --------------- 特殊文件夹和元数据相关常量 ---------------
// MIUI笔记文件夹前缀用于标识由MIUI笔记创建的Google Tasks文件夹
public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]";
// 默认文件夹名称
public final static String FOLDER_DEFAULT = "Default"; // 默认任务列表
public final static String FOLDER_CALL_NOTE = "Call_Note"; // 通话记录笔记
public final static String FOLDER_META = "METADATA"; // 元数据文件夹
// 元数据头部标识
public final static String META_HEAD_GTASK_ID = "meta_gid"; // Google Tasks ID元数据
public final static String META_HEAD_NOTE = "meta_note"; // 笔记元数据
public final static String META_HEAD_DATA = "meta_data"; // 数据元数据
// 元数据笔记名称,提示用户不要更新或删除
public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE";
}

@ -0,0 +1,273 @@
/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.micode.notes.tool;
import android.content.Context;
import android.preference.PreferenceManager;
import net.micode.notes.R;
import net.micode.notes.ui.NotesPreferenceActivity;
/**
*
*
*/
public class ResourceParser {
// 笔记背景颜色常量定义
public static final int YELLOW = 0; // 黄色背景
public static final int BLUE = 1; // 蓝色背景
public static final int WHITE = 2; // 白色背景
public static final int GREEN = 3; // 绿色背景
public static final int RED = 4; // 红色背景
public static final int BG_DEFAULT_COLOR = YELLOW; // 默认背景颜色为黄色
// 字体大小常量定义
public static final int TEXT_SMALL = 0; // 小号字体
public static final int TEXT_MEDIUM = 1; // 中号字体(默认)
public static final int TEXT_LARGE = 2; // 大号字体
public static final int TEXT_SUPER = 3; // 超大号字体
public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; // 默认字体大小为中号
/**
*
*
*/
public static class NoteBgResources {
// 笔记编辑界面背景资源数组,按颜色索引
private final static int [] BG_EDIT_RESOURCES = new int [] {
R.drawable.edit_yellow, // 黄色背景
R.drawable.edit_blue, // 蓝色背景
R.drawable.edit_white, // 白色背景
R.drawable.edit_green, // 绿色背景
R.drawable.edit_red // 红色背景
};
// 笔记编辑界面标题栏背景资源数组,按颜色索引
private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] {
R.drawable.edit_title_yellow, // 黄色标题栏
R.drawable.edit_title_blue, // 蓝色标题栏
R.drawable.edit_title_white, // 白色标题栏
R.drawable.edit_title_green, // 绿色标题栏
R.drawable.edit_title_red // 红色标题栏
};
/**
* ID
* @param id ID (0-4)
* @return ID
*/
public static int getNoteBgResource(int id) {
return BG_EDIT_RESOURCES[id];
}
/**
* ID
* @param id ID (0-4)
* @return ID
*/
public static int getNoteTitleBgResource(int id) {
return BG_EDIT_TITLE_RESOURCES[id];
}
}
/**
* ID
*
* @param context
* @return ID
*/
public static int getDefaultBgId(Context context) {
// 检查用户是否开启了"随机背景色"设置
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) {
// 随机生成一个颜色ID
return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length);
} else {
// 返回默认颜色ID
return BG_DEFAULT_COLOR;
}
}
/**
*
* ()
*/
public static class NoteItemBgResources {
// 列表首项背景资源数组,按颜色索引
private final static int [] BG_FIRST_RESOURCES = new int [] {
R.drawable.list_yellow_up, // 黄色首项背景
R.drawable.list_blue_up, // 蓝色首项背景
R.drawable.list_white_up, // 白色首项背景
R.drawable.list_green_up, // 绿色首项背景
R.drawable.list_red_up // 红色首项背景
};
// 列表中间项背景资源数组,按颜色索引
private final static int [] BG_NORMAL_RESOURCES = new int [] {
R.drawable.list_yellow_middle, // 黄色中间项背景
R.drawable.list_blue_middle, // 蓝色中间项背景
R.drawable.list_white_middle, // 白色中间项背景
R.drawable.list_green_middle, // 绿色中间项背景
R.drawable.list_red_middle // 红色中间项背景
};
// 列表末项背景资源数组,按颜色索引
private final static int [] BG_LAST_RESOURCES = new int [] {
R.drawable.list_yellow_down, // 黄色末项背景
R.drawable.list_blue_down, // 蓝色末项背景
R.drawable.list_white_down, // 白色末项背景
R.drawable.list_green_down, // 绿色末项背景
R.drawable.list_red_down, // 红色末项背景
};
// 列表单项(只有一项)背景资源数组,按颜色索引
private final static int [] BG_SINGLE_RESOURCES = new int [] {
R.drawable.list_yellow_single, // 黄色单项背景
R.drawable.list_blue_single, // 蓝色单项背景
R.drawable.list_white_single, // 白色单项背景
R.drawable.list_green_single, // 绿色单项背景
R.drawable.list_red_single // 红色单项背景
};
/**
* ID
* @param id ID (0-4)
* @return ID
*/
public static int getNoteBgFirstRes(int id) {
return BG_FIRST_RESOURCES[id];
}
/**
* ID
* @param id ID (0-4)
* @return ID
*/
public static int getNoteBgLastRes(int id) {
return BG_LAST_RESOURCES[id];
}
/**
* ID
* @param id ID (0-4)
* @return ID
*/
public static int getNoteBgSingleRes(int id) {
return BG_SINGLE_RESOURCES[id];
}
/**
* ID
* @param id ID (0-4)
* @return ID
*/
public static int getNoteBgNormalRes(int id) {
return BG_NORMAL_RESOURCES[id];
}
/**
*
* @return ID
*/
public static int getFolderBgRes() {
return R.drawable.list_folder;
}
}
/**
*
* (2x4x)
*/
public static class WidgetBgResources {
// 2x尺寸小部件背景资源数组按颜色索引
private final static int [] BG_2X_RESOURCES = new int [] {
R.drawable.widget_2x_yellow, // 黄色2x小部件背景
R.drawable.widget_2x_blue, // 蓝色2x小部件背景
R.drawable.widget_2x_white, // 白色2x小部件背景
R.drawable.widget_2x_green, // 绿色2x小部件背景
R.drawable.widget_2x_red, // 红色2x小部件背景
};
/**
* ID2x
* @param id ID (0-4)
* @return 2xID
*/
public static int getWidget2xBgResource(int id) {
return BG_2X_RESOURCES[id];
}
// 4x尺寸小部件背景资源数组按颜色索引
private final static int [] BG_4X_RESOURCES = new int [] {
R.drawable.widget_4x_yellow, // 黄色4x小部件背景
R.drawable.widget_4x_blue, // 蓝色4x小部件背景
R.drawable.widget_4x_white, // 白色4x小部件背景
R.drawable.widget_4x_green, // 绿色4x小部件背景
R.drawable.widget_4x_red // 红色4x小部件背景
};
/**
* ID4x
* @param id ID (0-4)
* @return 4xID
*/
public static int getWidget4xBgResource(int id) {
return BG_4X_RESOURCES[id];
}
}
/**
*
*
*/
public static class TextAppearanceResources {
// 文本样式资源数组,按字体大小索引
private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] {
R.style.TextAppearanceNormal, // 小号字体样式
R.style.TextAppearanceMedium, // 中号字体样式
R.style.TextAppearanceLarge, // 大号字体样式
R.style.TextAppearanceSuper // 超大号字体样式
};
/**
* ID
* @param id ID (0-3)
* @return ID
*/
public static int getTexAppearanceResource(int id) {
/**
* HACKME: ID
* IDID
*/
if (id >= TEXTAPPEARANCE_RESOURCES.length) {
return BG_DEFAULT_FONT_SIZE;
}
return TEXTAPPEARANCE_RESOURCES[id];
}
/**
*
* @return
*/
public static int getResourcesSize() {
return TEXTAPPEARANCE_RESOURCES.length;
}
}
}
Loading…
Cancel
Save