From cca565fcdaf965e739f59966833869284fba66fe Mon Sep 17 00:00:00 2001 From: wukunwei <2132219892@qq.com> Date: Fri, 21 Apr 2023 14:49:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=B4=E5=8F=8B=E6=94=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 吴友政/tool/BackupUtils.java | 343 +++++++++++++++++++++++++++ 吴友政/tool/DataUtils.java | 307 ++++++++++++++++++++++++ 吴友政/tool/GTaskStringUtils.java | 113 +++++++++ 吴友政/tool/ResourceParser.java | 181 ++++++++++++++ 4 files changed, 944 insertions(+) create mode 100644 吴友政/tool/BackupUtils.java create mode 100644 吴友政/tool/DataUtils.java create mode 100644 吴友政/tool/GTaskStringUtils.java create mode 100644 吴友政/tool/ResourceParser.java diff --git a/吴友政/tool/BackupUtils.java b/吴友政/tool/BackupUtils.java new file mode 100644 index 0000000..b4fd702 --- /dev/null +++ b/吴友政/tool/BackupUtils.java @@ -0,0 +1,343 @@ +/* + * 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;//导入需要的包 + + +public class BackupUtils {//此处为备份工具包 + private static final String TAG = "BackupUtils";//实化一个BackupUtils的对象 + // Singleton stuff + private static BackupUtils sInstance;//初始化sInstance + + public static synchronized BackupUtils getInstance(Context context) {//如果当前备份不在,则再新声明一个 + if (sInstance == null) { + sInstance = new BackupUtils(context); + } + return sInstance;//返回当前的sInstance值 + } + + /** + * Following states are signs to represents backup or restore + * status + */ + // Currently, the sdcard is not mounted + public static final int STATE_SD_CARD_UNMOUONTED = 0;//备份文件夹不存在 + // The backup file not exist + 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;//超时异常 + // Some run-time exception which causes restore or backup fails + public static final int STATE_SYSTEM_ERROR = 3;//系统错误状态为3 + // Backup or restore success + public static final int STATE_SUCCESS = 4;//变量表示备份成功 + + private TextExport mTextExport;//实例化一个为mTextExport的对象 + + private BackupUtils(Context context) {//初始化构造函数 + mTextExport = new TextExport(context); + } + + private static boolean externalStorageAvailable() {//判断外部存储设备是否有效 + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + } + + public int exportToText() {//输出至文本 + return mTextExport.exportToText(); + } + + public String getExportedTextFileName() {//返回获取的文件名 + return mTextExport.mFileName; + } + + public String getExportedTextFileDir() {//返回文件的目录 + return mTextExport.mFileDirectory; + } + + private static class TextExport {//内部类:文本输出 + private static final String[] NOTE_PROJECTION = {//定义了一个数组存储便签的信息 + NoteColumns.ID,//ID + NoteColumns.MODIFIED_DATE,//日期 + NoteColumns.SNIPPET, + NoteColumns.TYPE + }; + + private static final int NOTE_COLUMN_ID = 0;//初始化便签ID + + 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, + DataColumns.DATA1, + DataColumns.DATA2, + DataColumns.DATA3, + DataColumns.DATA4, + }; + + private static final int DATA_COLUMN_CONTENT = 0;//表示设定:数据内容表示为0 + + private static final int DATA_COLUMN_MIME_TYPE = 1;//数据媒体类型标识为1 + + private static final int DATA_COLUMN_CALL_DATE = 2;//访问日期表示为2 + + private static final int DATA_COLUMN_PHONE_NUMBER = 4;//电话号码表示为4 + + private final String [] TEXT_FORMAT;//文档格式标识 + private static final int FORMAT_FOLDER_NAME = 0;//文件命名格式表示为0 + private static final int FORMAT_NOTE_DATE = 1;//便签日期格式表示为1 + private static final int FORMAT_NOTE_CONTENT = 2;//便签目录格式 + + private Context mContext;//定义上下文类 + private String mFileName;//定义文件名 + private String mFileDirectory;//文件路径 + + public TextExport(Context context) {//从context类实例中花去信息,给对应的属性赋初始值 + TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); + mContext = context; + mFileName = ""; + mFileDirectory = ""; + } + + private String getFormat(int id) { + return TEXT_FORMAT[id]; + }//获取文本组成部分 + + /** + * Export the folder identified by folder id to text + */ + private void exportFolderToText(String folderId, PrintStream ps) {//通过文件夹目录ID将目录导出后成为文件 + // Query notes belong to this folder + Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { + folderId + }, null); + + if (notesCursor != null) { + if (notesCursor.moveToFirst()) { + do { + // Print note's last modified date + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); + // Query data belong to this note + String noteId = notesCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); + } while (notesCursor.moveToNext()); + } + notesCursor.close();//关闭游标 + } + } + + /** + * Export note identified by id to a print stream + */ + private void exportNoteToText(String noteId, PrintStream ps) {//函数:将便签的内容以文本的形式显示在屏幕上 + Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI,//利用光标来扫描内容 + DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { + noteId + }, null); + + if (dataCursor != null) {//如果光标不为空 + if (dataCursor.moveToFirst()) {//游标已经在第一步就位 + do { + 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);//获取通话时间 + String location = dataCursor.getString(DATA_COLUMN_CONTENT);//获取地址 + + if (!TextUtils.isEmpty(phoneNumber)) {//判断是否为空字符 + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + phoneNumber)); + } + // Print call date + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat//输出电话号码 + .format(mContext.getString(R.string.format_datetime_mdhm), + callDate))); + // Print call attachment location + if (!TextUtils.isEmpty(location)) {//输出位置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());//光标下移 + } + dataCursor.close();//关闭光标 + } + // print a line separator between note + try {//正常情况再note下面输出一条线 + ps.write(new byte[] { + Character.LINE_SEPARATOR, Character.LETTER_NUMBER + }); + } catch (IOException e) {//获取错误信息 + Log.e(TAG, e.toString()); + } + } + + /** + * Note will be exported as text which is user readable + */ + public int exportToText() {//以TEXT形式输出到外部设备 + if (!externalStorageAvailable()) {//如果外部设备未安装好,返回对应状态 + Log.d(TAG, "Media was not mounted"); + return STATE_SD_CARD_UNMOUONTED; + } + + PrintStream ps = getExportToTextPrintStream();//获取存储信息 + if (ps == null) {//如果没有得到正常的数据流,则输出错误信息 + Log.e(TAG, "get print stream error"); + return STATE_SYSTEM_ERROR; + } + // First export folder and its notes + Cursor folderCursor = mContext.getContentResolver().query(//定位需要导出的文件夹 + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " + + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " + + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); + + if (folderCursor != null) {//如果文件夹不为空,即存在文件夹 + if (folderCursor.moveToFirst()) {//文件光标再第一行就位 + do { + // Print folder's name + 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)) {//判断folderName是否存在 + ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); + } + String folderId = folderCursor.getString(NOTE_COLUMN_ID);//通过便签ID获得folderID + exportFolderToText(folderId, ps); + } while (folderCursor.moveToNext());//文件夹的光标下移 + } + folderCursor.close();//关闭光标 + } + + // Export notes in root's folder + 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) {//如果笔记游标不为空 + if (noteCursor.moveToFirst()) {//便签光标下移 + do { + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(//输出修改日期 + mContext.getString(R.string.format_datetime_mdhm), + noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); + // Query data belong to this note + String noteId = noteCursor.getString(NOTE_COLUMN_ID);//找到这块数据的ID + exportNoteToText(noteId, ps); + } while (noteCursor.moveToNext());//便签光标下移 + } + noteCursor.close();//关闭游标 + } + ps.close(); + + return STATE_SUCCESS; //返回成功导出状态 + } + + /** + * Get a print stream pointed to the file {@generateExportedTextFile} + */ + private PrintStream getExportToTextPrintStream() {//获取指向文件的打印流 + File file = generateFileMountedOnSDcard(mContext, R.string.file_path,//创建文件 + R.string.file_name_txt_format); + if (file == null) {//如果文件夹为空,则创建失败 + Log.e(TAG, "create file to exported failed"); + return null; + } + mFileName = file.getName();//得到文件名 + mFileDirectory = mContext.getString(R.string.file_path);//获取文件路径 + PrintStream ps = null;//初始化ps + try {//将ps输出流输出到特定的文件,目的是导出文件 + FileOutputStream fos = new FileOutputStream(file); + ps = new PrintStream(fos); + } catch (FileNotFoundException e) {//给出错误信息 + e.printStackTrace(); + return null; + } catch (NullPointerException e) {//捕获空指针异常 + e.printStackTrace(); + return null; + } + return ps; + } + } + + /** + * Generate the text file to store imported data + */ + private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { + //产生用于存储输入信息的文件 + StringBuilder sb = new StringBuilder();//构建一个动态字符串将外部信息加入其中 + sb.append(Environment.getExternalStorageDirectory());//加入路径 + sb.append(context.getString(filePathResId));//文件的存储路径 + File filedir = new File(sb.toString());//存储路径信息 + sb.append(context.getString(//格式化输出当前系统时间 + fileNameFormatResId, + DateFormat.format(context.getString(R.string.format_date_ymd),//将当前的系统时间以预定的格式输出 + System.currentTimeMillis()))); + File file = new File(sb.toString());//将输出连接到一个文件里 + + 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; + } +} \ No newline at end of file diff --git a/吴友政/tool/DataUtils.java b/吴友政/tool/DataUtils.java new file mode 100644 index 0000000..8633d59 --- /dev/null +++ b/吴友政/tool/DataUtils.java @@ -0,0 +1,307 @@ +/* + * 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; + + +public class DataUtils {//数据的集成工具类 + public static final String TAG = "DataUtils"; + public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + //方法:实现了批量删除便签 + if (ids == null) {//判断笔记id是否为空 + Log.d(TAG, "the ids is null"); + return true; + } + if (ids.size() == 0) {//判断笔记大小是否为空 + Log.d(TAG, "no id is in the hashset"); + return true; + } + + ArrayList operationList = new ArrayList();//提供一个事件的列表 + for (long id : ids) {//遍历数据,如果此数据为根目录则跳过此数据不删除,如果不是根目录则将此数据删除 + if(id == Notes.ID_ROOT_FOLDER) { + Log.e(TAG, "Don't delete system folder root"); + continue; + } + ContentProviderOperation.Builder builder = ContentProviderOperation//使用newdelete进行删除 + .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, "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; + } + + public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { + //将标签移动到另一个目录下 + ContentValues values = new ContentValues();//实例化一个contentValues类 + values.put(NoteColumns.PARENT_ID, desFolderId);//将PARENT_ID更改为目标目录ID + values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);//设置origin也即原本的父节点为原本的文件夹的id + values.put(NoteColumns.LOCAL_MODIFIED, 1);//设置修改符号为1 + resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); + //对需要移动的便签进行数据更新,然后用update实现 + } + + public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, + long folderId) {//批量的将标签移动到另一个目录下 + if (ids == null) {//判断便签ID是否为空 + Log.d(TAG, "the ids is null"); + return true; + } + + ArrayList operationList = new ArrayList(); + //将ids里包含的每一列的数据逐次加入到operationList中,等待最后的批量处理 + 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());//将ids里的数据添加到operationlist中以便接下来批量处理 + } + + try {//利用Log输出信息来进行错误检测 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + //applyBatch一次性处理一个操作列表 + 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; + } + + /** + * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} + */ + public static int getUserFolderCount(ContentResolver resolver) {//获取用户文件夹数 + Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, + //resolver.query()方法第二个参数是要返回的列,第三个参数是Section,查询where字句;第四个是查询条件属性值,第五个是筛选规则 + new String[] { "COUNT(*)" }, + NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", + new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)}, + null);//String.valueof将形参转成字符串返回 + + 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; + } + + public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) {//是否在便签数据库中可见 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),//通过withappendedid的方法为uri加上id + null, + NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, + new String [] {String.valueOf(type)}, + null);//sql语句,表示筛选出列中type等于string数组中type,且每一项的PARENT_ID不等于Notes.ID.TRAXH_FOLDER + + boolean exist = false;//查询文件 + if (cursor != null) {//用getcount函数判断cursor是否为空 + if (cursor.getCount() > 0) {//如果有满足条件的条目,那么就是可见,exist为真,否则不可见,exist为假 + exist = true; + } + cursor.close();//关闭游标 + } + return exist;//在数据数据库中是否存在 + } + + public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {//判断该note是否在数据库中存在 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), + null, null, null, null); +//相比于上面,这个只是存在性判定,因此不需要过多筛选条件 + boolean exist = false;//初始化存在状态 + if (cursor != null) {//根据getcount此时的值可以判断dataID的存在性 + if (cursor.getCount() > 0) {//根据筛选出来的条数判断存在性 + exist = true; + } + cursor.close();//关闭游标 + } + return exist; + } + + public static boolean existInDataDatabase(ContentResolver resolver, long dataId) {//检查文件名字是否可见 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), + //通过URI与dataId在数据库中查找数据 + null, null, null, null); + + boolean exist = false;//根据数据的有无返回相应的布尔值 + if (cursor != null) { + if (cursor.getCount() > 0) {//调用对应的uri的数据值进行查询 + exist = true; + } + cursor.close();//关闭游标 + } + return exist;//通过名字查询文件是否存在 + } + + 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) {//判断找到的文件名返回文件是否存在的bool值 + if(cursor.getCount() > 0) { + exist = true; + } + cursor.close();//关闭游标 + } + return exist; + } + + public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { + //使用hashset来存储不同窗口的id和type并且建立对应关系 + Cursor c = resolver.query(Notes.CONTENT_NOTE_URI,//父id为传入的文件夹id + new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, + NoteColumns.PARENT_ID + "=?", + new String[] { String.valueOf(folderId) },//hash集合为空的情况 + null); + + HashSet set = null;//根据窗口的记录一一添加对应的属性值 + if (c != null) {//将app窗口的属性加入到HashSet中 + if (c.moveToFirst()) { + set = new HashSet(); + do { + try {//把每一个条目对应的窗口id和type记录下来,放到set里面。每一行的第0个int和第1个int分别对应widgetId和widgetType + AppWidgetAttribute widget = new AppWidgetAttribute();//新建一个区块 + widget.widgetId = c.getInt(0);//0对应的NoteColumns.WIDGET_ID + widget.widgetType = c.getInt(1);//1对应的NoteColumns.WIDGET_TYPE + set.add(widget); + } catch (IndexOutOfBoundsException e) {//当下标超过边界,那么返回错误 + Log.e(TAG, e.toString()); + } + } while (c.moveToNext());//访问下一条 + } + c.close();//关闭游标 + } + return set; + } + + public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) {//通过笔记ID获取号码 + 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, "Get call number fails " + e.toString()); + } finally { + cursor.close(); + } + } + return ""; + } + + public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { + //同样的,通过映射关系,通过号码和日期获取ID + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,//通过数据库操作,查询条件是(callDate和phoneNumber匹配传入参数的值) + 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);//通过数据库操作,查询条件(callDate和phoneNumber匹配传入参数的值),找到对应的note + + if (cursor != null) {//得到该note的系统属性,并以Long值的形式来保存 + if (cursor.moveToFirst()) { + try { + return cursor.getLong(0);//0对应的CallNote.NOTE_ID + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Get call note id fails " + e.toString()); + } + } + cursor.close();//关闭游标 + } + return 0; + } + + public static String getSnippetById(ContentResolver resolver, long noteId) {//按ID获取片段 + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,//通过ID查询 + new String [] { NoteColumns.SNIPPET }, + NoteColumns.ID + "=?", + new String [] { String.valueOf(noteId)}, + null);//通过数据库操作,查询条件是(callDate和phoneNumber匹配传入参数的值 + + if (cursor != null) {//以string的形式获取该对象当前行指定列的值。 + String snippet = "";//对字符串进行格式处理,将字符串两头的空格去掉同时将换行符去掉 + if (cursor.moveToFirst()) { + snippet = cursor.getString(0); + } + cursor.close(); + return snippet; + } + throw new IllegalArgumentException("Note is not found with id: " + noteId); + //IllegalArgumentException是非法传参异常,也就是参数传的类型冲突,属于RunTimeException运行时异常 + } + + public static String getFormattedSnippet(String snippet) {//对字符串进行格式处理,将字符串两头的空格去掉,同时将换行符去掉 + if (snippet != null) { + snippet = snippet.trim();// trim()函数: 移除字符串两侧的空白字符或其他预定义字符 + int index = snippet.indexOf('\n'); + if (index != -1) { + snippet = snippet.substring(0, index);//截取到第一个换行符 + } + } + return snippet; + } +} diff --git a/吴友政/tool/GTaskStringUtils.java b/吴友政/tool/GTaskStringUtils.java new file mode 100644 index 0000000..a33b4ab --- /dev/null +++ b/吴友政/tool/GTaskStringUtils.java @@ -0,0 +1,113 @@ +/* + * 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;//定义了很多的静态字符串,目的就是为了提供jsonObject中相应字符串的"key" + +public class GTaskStringUtils { + + 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_CLIENT_VERSION = "client_version";//客户端 + + public final static String GTASK_JSON_COMPLETED = "completed"; + + public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id";//当前列表位置 + + public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_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_ENTITY_DELTA = "entity_delta"; + + public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; + + public final static String GTASK_JSON_GET_DELETED = "get_deleted"; + + public final static String GTASK_JSON_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"; + + 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"; + + 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"; + + 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"; + + 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"; + + 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"; + +} diff --git a/吴友政/tool/ResourceParser.java b/吴友政/tool/ResourceParser.java new file mode 100644 index 0000000..4ec7fb2 --- /dev/null +++ b/吴友政/tool/ResourceParser.java @@ -0,0 +1,181 @@ +/* + * 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 {//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 {// Note的背景颜色 + private final static int [] BG_EDIT_RESOURCES = new int [] {//.调用drawable中的五种颜色的背景图片png文件 + R.drawable.edit_yellow,//调用drawable中的五种颜色的标题背景图片png文件 + 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 + }; + + public static int getNoteBgResource(int id) { + return BG_EDIT_RESOURCES[id]; + }//获取便签背景资源id + + public static int getNoteTitleBgResource(int id) { + return BG_EDIT_TITLE_RESOURCES[id]; + } + }//获取标题使用的资源的ID + + public static int getDefaultBgId(Context context) {//获取默认背景颜色对应Id + if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(//如果颜色设定为随机的颜色,那么就随机返回一个颜色 + NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { + return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); + } else { + return BG_DEFAULT_COLOR;//否则返回默认背景颜色 + } + } + + public static class NoteItemBgResources {//便签项目背景资源子类,包括方法:获得首尾项目等背景资源 + private final static int [] BG_FIRST_RESOURCES = new int [] {//不同drawable的变量声明 + 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 + }; + + public static int getNoteBgFirstRes(int id) { + return BG_FIRST_RESOURCES[id]; + }//通过ID获取所需要的资源 + + public static int getNoteBgLastRes(int id) { + return BG_LAST_RESOURCES[id]; + }//通过ID寻找last的颜色值 + + public static int getNoteBgSingleRes(int id) { + return BG_SINGLE_RESOURCES[id]; + }//通过ID获取单个便签背景颜色资源 + + public static int getNoteBgNormalRes(int id) { + return BG_NORMAL_RESOURCES[id]; + }//通过ID寻找normal的颜色值 + + public static int getFolderBgRes() { + return R.drawable.list_folder; + }//设置窗口的资源 + } + + public static class WidgetBgResources {//小窗口情况下的背景资源类 + private final static int [] BG_2X_RESOURCES = new int [] {//2x小窗口背景资源初始化 + R.drawable.widget_2x_yellow, + R.drawable.widget_2x_blue, + R.drawable.widget_2x_white, + R.drawable.widget_2x_green, + R.drawable.widget_2x_red, + }; + + public static int getWidget2xBgResource(int id) { + return BG_2X_RESOURCES[id]; + }//根据ID加载BG_2X_RESOURCES数组里的颜色资源序号。 + + private final static int [] BG_4X_RESOURCES = new int [] {//本条与下一条private定义与上两条相同,只不过由2倍扩大成了4倍 + R.drawable.widget_4x_yellow, + R.drawable.widget_4x_blue, + R.drawable.widget_4x_white, + R.drawable.widget_4x_green, + R.drawable.widget_4x_red + }; + + public static int getWidget4xBgResource(int id) { + return BG_4X_RESOURCES[id]; + }//根据ID加载BG_4X_RESOURCES数组里的颜色资源序号。 + } + + public static class TextAppearanceResources {//文本外观资源,包括默认字体,以及获取资源大小 + private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] {//定义外观资源 + R.style.TextAppearanceNormal,//通过ID找到格式,没有要求的话就设置为默认格式 + R.style.TextAppearanceMedium, + R.style.TextAppearanceLarge, + R.style.TextAppearanceSuper + }; + + public static int getTexAppearanceResource(int id) {//这是一个容错的函数,防止输入的id大于资源总量。如果大于资源总量,则自动返回默认的设置结果 + /** + * HACKME: Fix bug of store the resource id in shared preference. + * The id may larger than the length of resources, in this case, + * return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE} + */ + if (id >= TEXTAPPEARANCE_RESOURCES.length) {//若输入id大于字体编号最大值,则返回默认值 + return BG_DEFAULT_FONT_SIZE; + } + return TEXTAPPEARANCE_RESOURCES[id]; + } + + public static int getResourcesSize() { + return TEXTAPPEARANCE_RESOURCES.length; + }//直接返回为资源的长度 + } +}