From 4e8ac3796c8b96c219abdeb8f9c956839f1438c3 Mon Sep 17 00:00:00 2001 From: chenzuiyulianmian <2895954175@qq.com> Date: Tue, 26 Dec 2023 21:11:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=87=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 标注/Contact.java | 69 ---- 标注/GTaskASyncTask.java | 135 ++++--- 标注/MetaData.java | 104 ----- 标注/Node.java | 90 ----- 标注/SqlData.java | 752 +++++++++++-------------------------- 标注/SqlNote.java | 669 +++++++++++++++++++++++++++++++++ 标注/Task.java | 242 +++++++----- 标注/TaskList.java | 262 ++++++------- 8 files changed, 1262 insertions(+), 1061 deletions(-) delete mode 100644 标注/Contact.java delete mode 100644 标注/MetaData.java delete mode 100644 标注/Node.java create mode 100644 标注/SqlNote.java diff --git a/标注/Contact.java b/标注/Contact.java deleted file mode 100644 index 3c2a35f..0000000 --- a/标注/Contact.java +++ /dev/null @@ -1,69 +0,0 @@ - - -package net.micode.notes.data; - -import android.content.Context; -import android.database.Cursor; -import android.provider.ContactsContract.CommonDataKinds.Phone; -import android.provider.ContactsContract.Data; -import android.telephony.PhoneNumberUtils; -import android.util.Log; - -import java.util.HashMap; -// 定义一个Contact类 -public class Contact { - // 声明一个HashMap类型的静态变量sContactCache - private static HashMap sContactCache; - // 声明一个String类型的静态变量TAG - private static final String TAG = "Contact"; - - // 声明一个String类型的静态变量CALLER_ID_SELECTION - private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER - + ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" - + " AND " + Data.RAW_CONTACT_ID + " IN " - + "(SELECT raw_contact_id " - + " FROM phone_lookup" - + " WHERE min_match = '+')"; - - // 定义一个静态方法getContact,参数为context和phoneNumber - public static String getContact(Context context, String phoneNumber) { - // 如果sContactCache为空,则初始化sContactCache - if(sContactCache == null) { - sContactCache = new HashMap(); - } - - // 检查缓存中是否有该号码的联系人 - if(sContactCache.containsKey(phoneNumber)) { - return sContactCache.get(phoneNumber); - } - - // 查询数据库中是否有该号码的联系人 - String selection = CALLER_ID_SELECTION.replace("+", - PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); - // 根据电话号码查询联系人 - Cursor cursor = context.getContentResolver().query( - Data.CONTENT_URI, - new String [] { Phone.DISPLAY_NAME }, - selection, - new String[] { phoneNumber }, - null); - - if (cursor != null && cursor.moveToFirst()) { - try { - // 获取联系人名字 - String name = cursor.getString(0); - // 将联系人名字存入缓存 - sContactCache.put(phoneNumber, name); - return name; - } catch (IndexOutOfBoundsException e) { - Log.e(TAG, " Cursor get string error " + e.toString()); - return null; - } finally { - cursor.close(); - } - } else { - Log.d(TAG, "No contact matched with number:" + phoneNumber); - return null; - } - } -} \ No newline at end of file diff --git a/标注/GTaskASyncTask.java b/标注/GTaskASyncTask.java index a224858..4bc048c 100644 --- a/标注/GTaskASyncTask.java +++ b/标注/GTaskASyncTask.java @@ -1,29 +1,58 @@ -package net.micode.notes.gtask.remote; - -/*异步操作类,实现GTask的异步操作过程 - * 主要方法: - * private void showNotification(int tickerId, String content) 向用户提示当前同步的状态,是一个用于交互的方法 - * protected Integer doInBackground(Void... unused) 此方法在后台线程执行,完成任务的主要工作,通常需要较长的时间 - * protected void onProgressUpdate(String... progress) 可以使用进度条增加用户体验度。 此方法在主线程执行,用于显示任务执行的进度。 - * protected void onPostExecute(Integer result) 相当于Handler 处理UI的方式,在这里面可以使用在doInBackground 得到的结果处理操作UI + +/* + * 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.gtask.remote; + +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; + +import net.micode.notes.R; +import net.micode.notes.ui.NotesListActivity; +import net.micode.notes.ui.NotesPreferenceActivity; + + +// 添加中文注释后 public class GTaskASyncTask extends AsyncTask { - - + + // 通知ID private static int GTASK_SYNC_NOTIFICATION_ID = 5234235; - + + // 完成监听器 public interface OnCompleteListener { void onComplete(); } - + + // 上下文 private Context mContext; - + + // 通知管理器 private NotificationManager mNotifiManager; - + + // 任务管理器 private GTaskManager mTaskManager; - + + // 完成监听器 private OnCompleteListener mOnCompleteListener; - + + // 构造函数 public GTaskASyncTask(Context context, OnCompleteListener listener) { mContext = context; mOnCompleteListener = listener; @@ -31,72 +60,90 @@ public class GTaskASyncTask extends AsyncTask { .getSystemService(Context.NOTIFICATION_SERVICE); mTaskManager = GTaskManager.getInstance(); } - + + // 取消同步 public void cancelSync() { mTaskManager.cancelSync(); } - - public void publishProgess(String message) { // 发布进度单位,系统将会调用onProgressUpdate()方法更新这些值 + + // 发布进度 + public void publishProgess(String message) { publishProgress(new String[] { message }); } - - private void showNotification(int tickerId, String content) { + + private void showNotification(int tickerId, String content) { + // 创建一个新的通知 Notification notification = new Notification(R.drawable.notification, mContext .getString(tickerId), System.currentTimeMillis()); - notification.defaults = Notification.DEFAULT_LIGHTS; // 调用系统自带灯光 - notification.flags = Notification.FLAG_AUTO_CANCEL; // 点击清除按钮或点击通知后会自动消失 - PendingIntent pendingIntent; //一个描述了想要启动一个Activity、Broadcast或是Service的意图 + // 设置默认的提示音 + notification.defaults = Notification.DEFAULT_LIGHTS; + // 设置自动取消 + notification.flags = Notification.FLAG_AUTO_CANCEL; + PendingIntent pendingIntent; + // 如果不是成功提示,则设置跳转到NotesPreferenceActivity if (tickerId != R.string.ticker_success) { pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, - NotesPreferenceActivity.class), 0); //如果同步不成功,那么从系统取得一个用于启动一个NotesPreferenceActivity的PendingIntent对象 - + NotesPreferenceActivity.class), 0); + + // 如果是成功提示,则设置跳转到NotesListActivity } else { pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, - NotesListActivity.class), 0); //如果同步成功,那么从系统取得一个用于启动一个NotesListActivity的PendingIntent对象 + NotesListActivity.class), 0); } + // 设置通知的内容 notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, pendingIntent); - mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);//通过NotificationManager对象的notify()方法来执行一个notification的消息 + // 发送通知 + mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification); } - - @Override + @Override protected Integer doInBackground(Void... unused) { + // 发布进度,提示正在登录 publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity - .getSyncAccountName(mContext))); //利用getString,将把 NotesPreferenceActivity.getSyncAccountName(mContext))的字符串内容传进sync_progress_login中 - return mTaskManager.sync(mContext, this); //进行后台同步具体操作 + .getSyncAccountName(mContext))); + // 返回任务管理器的结果 + return mTaskManager.sync(mContext, this); } - + @Override protected void onProgressUpdate(String... progress) { + // 显示进度条 showNotification(R.string.ticker_syncing, progress[0]); - if (mContext instanceof GTaskSyncService) { //instanceof 判断mContext是否是GTaskSyncService的实例 + // 如果上下文是GTaskSyncService,则发送广播 + if (mContext instanceof GTaskSyncService) { ((GTaskSyncService) mContext).sendBroadcast(progress[0]); } } - - @Override - protected void onPostExecute(Integer result) { //用于在执行完后台任务后更新UI,显示结果 + @Override + protected void onPostExecute(Integer result) { + // 判断执行结果 if (result == GTaskManager.STATE_SUCCESS) { + // 执行成功 showNotification(R.string.ticker_success, mContext.getString( R.string.success_sync_account, mTaskManager.getSyncAccount())); - NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis()); //设置最新同步的时间 + // 设置上一次同步时间 + NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis()); } else if (result == GTaskManager.STATE_NETWORK_ERROR) { + // 网络错误 showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network)); } else if (result == GTaskManager.STATE_INTERNAL_ERROR) { + // 内部错误 showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal)); } else if (result == GTaskManager.STATE_SYNC_CANCELLED) { + // 取消同步 showNotification(R.string.ticker_cancel, mContext .getString(R.string.error_sync_cancelled)); - } //几种不同情况下的结果显示 + } + // 执行完成回调 if (mOnCompleteListener != null) { - new Thread(new Runnable() { //这里好像是方法内的一个线程,但是并不太懂什么意思 - - public void run() { //完成后的操作,使用onComplete()将所有值都重新初始化,相当于完成一次操作 - mOnCompleteListener.onComplete(); + new Thread(new Runnable() { + + public void run() { + mOnCompleteListener.onComplete(); } }).start(); } } -} \ No newline at end of file +} diff --git a/标注/MetaData.java b/标注/MetaData.java deleted file mode 100644 index fe7a778..0000000 --- a/标注/MetaData.java +++ /dev/null @@ -1,104 +0,0 @@ -package net.micode.notes.gtask.data; - -public class MetaData extends Task { - /* - * 功能描述:得到类的简写名称存入字符串TAG中 - * 实现过程:调用getSimpleName ()函数 - */ - private final static String TAG = MetaData.class.getSimpleName(); - private String mRelatedGid = null; - /* - * 功能描述:设置数据,即生成元数据库 - * 实现过程:调用JSONObject库函数put (),Task类中的setNotes ()和setName ()函数 - * 参数注解: - */ - public void setMeta(String gid, JSONObject metaInfo) - { - //对函数块进行注释 - try { - metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid); - /* - * 将这对键值放入metaInfo这个jsonobject对象中 - */ - } catch (JSONException e) { - Log.e(TAG, "failed to put related gid"); - /* - * 输出错误信息 - */ - } - setNotes(metaInfo.toString()); - setName(GTaskStringUtils.META_NOTE_NAME); - } - /* - * 功能描述:获取相关联的Gid - */ - public String getRelatedGid() { - return mRelatedGid; - } - /* - * 功能描述:判断当前数据是否为空,若为空则返回真即值得保存 - * Made By CuiCan - */ - @Override - public boolean isWorthSaving() { - return getNotes() != null; - } - /* - * 功能描述:使用远程json数据对象设置元数据内容 - * 实现过程:调用父类Task中的setContentByRemoteJSON ()函数,并 - * 参数注解: - */ - @Override - public void setContentByRemoteJSON(JSONObject js) { - super.setContentByRemoteJSON(js); - if (getNotes() != null) { - try { - JSONObject metaInfo = new JSONObject(getNotes().trim()); - mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); - } catch (JSONException e) { - Log.w(TAG, "failed to get related gid"); - /* - * 输出警告信息 - */ - mRelatedGid = null; - } - } - } - /* - * 功能描述:使用本地json数据对象设置元数据内容,一般不会用到,若用到,则抛出异常 - * Made By CuiCan - */ - @Override - public void setContentByLocalJSON(JSONObject js) { - // this function should not be called - throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called"); - /* - * 传递非法参数异常 - */ - } - /* - * 功能描述:从元数据内容中获取本地json对象,一般不会用到,若用到,则抛出异常 - * Made By CuiCan - */ - @Override - public JSONObject getLocalJSONFromContent() { - throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called"); - /* - * 传递非法参数异常 - * Made By Cui Can - */ - } - /* - * 功能描述:获取同步动作状态,一般不会用到,若用到,则抛出异常 - * Made By CuiCan - */ - @Override - public int getSyncAction(Cursor c) { - throw new IllegalAccessError("MetaData:getSyncAction should not be called"); - /* - * 传递非法参数异常 - * Made By Cui Can - */ - } - -} \ No newline at end of file diff --git a/标注/Node.java b/标注/Node.java deleted file mode 100644 index db7db78..0000000 --- a/标注/Node.java +++ /dev/null @@ -1,90 +0,0 @@ -package net.micode.notes.gtask.data; - -import android.database.Cursor; - -import org.json.JSONObject; - -/** - * 应该是同步操作的基础数据类型,定义了相关指示同步操作的常量 - * 关键字:abstract - */ -public abstract class Node { - //定义了各种用于表征同步状态的常量 - public static final int SYNC_ACTION_NONE = 0;// 本地和云端都无可更新内容(即本地和云端内容一致) - - public static final int SYNC_ACTION_ADD_REMOTE = 1;// 需要在远程云端增加内容 - - public static final int SYNC_ACTION_ADD_LOCAL = 2;// 需要在本地增加内容 - - public static final int SYNC_ACTION_DEL_REMOTE = 3;// 需要在远程云端删除内容 - - public static final int SYNC_ACTION_DEL_LOCAL = 4;// 需要在本地删除内容 - - public static final int SYNC_ACTION_UPDATE_REMOTE = 5;// 需要将本地内容更新到远程云端 - - public static final int SYNC_ACTION_UPDATE_LOCAL = 6;// 需要将远程云端内容更新到本地 - - public static final int SYNC_ACTION_UPDATE_CONFLICT = 7;// 同步出现冲突 - - public static final int SYNC_ACTION_ERROR = 8;// 同步出现错误 - - private String mGid; - - private String mName; - - private long mLastModified;//记录最后一次修改时间 - - private boolean mDeleted;//表征是否被删除 - - public Node() { - mGid = null; - mName = ""; - mLastModified = 0; - mDeleted = false; - } - - public abstract JSONObject getCreateAction(int actionId); - - public abstract JSONObject getUpdateAction(int actionId); - - public abstract void setContentByRemoteJSON(JSONObject js); - - public abstract void setContentByLocalJSON(JSONObject js); - - public abstract JSONObject getLocalJSONFromContent(); - - public abstract int getSyncAction(Cursor c); - - public void setGid(String gid) { - this.mGid = gid; - } - - public void setName(String name) { - this.mName = name; - } - - public void setLastModified(long lastModified) { - this.mLastModified = lastModified; - } - - public void setDeleted(boolean deleted) { - this.mDeleted = deleted; - } - - public String getGid() { - return this.mGid; - } - - public String getName() { - return this.mName; - } - - public long getLastModified() { - return this.mLastModified; - } - - public boolean getDeleted() { - return this.mDeleted; - } - -} \ No newline at end of file diff --git a/标注/SqlData.java b/标注/SqlData.java index 9d26bbb..9345cec 100644 --- a/标注/SqlData.java +++ b/标注/SqlData.java @@ -1,577 +1,245 @@ /* - * Description:用于支持小米便签最底层的数据库相关操作,和sqldata的关系上是父集关系,即note是data的子父集。 - * 和SqlData相比,SqlNote算是真正意义上的数据了。 + * 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.gtask.data; -/* - * 功能描述: - * 实现过程: - * 参数注解: - * Made By CuiCan - */ - -public class SqlNote { - /* - * 功能描述:得到类的简写名称存入字符串TAG中 - * 实现过程:调用getSimpleName ()函数 - * Made By CuiCan - */ - private static final String TAG = SqlNote.class.getSimpleName(); - + +import android.content.ContentResolver; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.util.Log; + +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 net.micode.notes.data.NotesDatabaseHelper.TABLE; +import net.micode.notes.gtask.exception.ActionFailureException; + +import org.json.JSONException; +import org.json.JSONObject; + + + +// 定义一个SqlData类 +public class SqlData { + private static final String TAG = SqlData.class.getSimpleName(); + private static final int INVALID_ID = -99999; - // 集合了interface NoteColumns中所有SF常量(17个) - public static final String[] PROJECTION_NOTE = new String[] { - NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID, - NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE, - NoteColumns.NOTES_COUNT, NoteColumns.PARENT_ID, NoteColumns.SNIPPET, NoteColumns.TYPE, - NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE, NoteColumns.SYNC_ID, - NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID, - NoteColumns.VERSION + + public static final String[] PROJECTION_DATA = new String[] { + DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1, + DataColumns.DATA3 }; - - //以下设置17个列的编号 - public static final int ID_COLUMN = 0; - - public static final int ALERTED_DATE_COLUMN = 1; - - public static final int BG_COLOR_ID_COLUMN = 2; - - public static final int CREATED_DATE_COLUMN = 3; - - public static final int HAS_ATTACHMENT_COLUMN = 4; - - public static final int MODIFIED_DATE_COLUMN = 5; - - public static final int NOTES_COUNT_COLUMN = 6; - - public static final int PARENT_ID_COLUMN = 7; - - public static final int SNIPPET_COLUMN = 8; - - public static final int TYPE_COLUMN = 9; - - public static final int WIDGET_ID_COLUMN = 10; - - public static final int WIDGET_TYPE_COLUMN = 11; - - public static final int SYNC_ID_COLUMN = 12; - - public static final int LOCAL_MODIFIED_COLUMN = 13; - - public static final int ORIGIN_PARENT_ID_COLUMN = 14; - - public static final int GTASK_ID_COLUMN = 15; - - public static final int VERSION_COLUMN = 16; - - //一下定义了17个内部的变量,其中12个可以由content中获得,5个需要初始化为0或者new - private Context mContext; - + + public static final int DATA_ID_COLUMN = 0; + + // 数据类型 + public static final int DATA_MIME_TYPE_COLUMN = 1; + + // 数据内容 + public static final int DATA_CONTENT_COLUMN = 2; + + // 数据内容1 + public static final int DATA_CONTENT_DATA_1_COLUMN = 3; + + // 数据内容3 + public static final int DATA_CONTENT_DATA_3_COLUMN = 4; + + // 内容解析器 private ContentResolver mContentResolver; - + + // 是否创建 private boolean mIsCreate; - - private long mId; - - private long mAlertDate; - - private int mBgColorId; - - private long mCreatedDate; - - private int mHasAttachment; - - private long mModifiedDate; - - private long mParentId; - - private String mSnippet; - - private int mType; - - private int mWidgetId; - - private int mWidgetType; - - private long mOriginParent; - - private long mVersion; - - private ContentValues mDiffNoteValues; - - private ArrayList mDataList; - - /* - * 功能描述:构造函数 - * 参数注解: mIsCreate用于标示构造方式 - * 参数注解: - * Made By CuiCan - */ - //构造函数只有context,对所有的变量进行初始化 - public SqlNote(Context context) { - mContext = context; + + // 数据ID + private long mDataId; + + // 数据类型 + private String mDataMimeType; + + // 数据内容 + private String mDataContent; + private long mDataContentData1; + + // 数据内容 + private String mDataContentData3; + + // 差值数据 + private ContentValues mDiffDataValues; + + public SqlData(Context context) { + //获取内容解析器 mContentResolver = context.getContentResolver(); + //标识是否创建 mIsCreate = true; - mId = INVALID_ID; - mAlertDate = 0; - mBgColorId = ResourceParser.getDefaultBgId(context); - mCreatedDate = System.currentTimeMillis();//调用系统函数获得创建时间 - mHasAttachment = 0; - mModifiedDate = System.currentTimeMillis();//最后一次修改时间初始化为创建时间 - mParentId = 0; - mSnippet = ""; - mType = Notes.TYPE_NOTE; - mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; - mWidgetType = Notes.TYPE_WIDGET_INVALIDE; - mOriginParent = 0; - mVersion = 0; - mDiffNoteValues = new ContentValues(); - mDataList = new ArrayList(); - } - - - /* - * 功能描述:构造函数 - * 参数注解: mIsCreate用于标示构造方式 - * 参数注解: - * Made By CuiCan - */ - //构造函数有context和一个数据库的cursor,多数变量通过cursor指向的一条记录直接进行初始化 - public SqlNote(Context context, Cursor c) { - mContext = context; + //数据ID + mDataId = INVALID_ID; + //数据类型 + mDataMimeType = DataConstants.NOTE; + //数据内容 + mDataContent = ""; + //数据内容数据1 + mDataContentData1 = 0; + //数据内容数据3 + mDataContentData3 = ""; + //差异数据值 + mDiffDataValues = new ContentValues(); + } + + public SqlData(Context context, Cursor c) { + //获取内容解析器 mContentResolver = context.getContentResolver(); + //标识是否创建 mIsCreate = false; + //从游标中加载数据 loadFromCursor(c); - mDataList = new ArrayList(); - // - if (mType == Notes.TYPE_NOTE) - loadDataContent(); - mDiffNoteValues = new ContentValues(); - } - - - /* - * 功能描述:构造函数 - * 参数注解: mIsCreate用于标示构造方式 - * 参数注解: - * Made By CuiCan - */ - public SqlNote(Context context, long id) { - mContext = context; - mContentResolver = context.getContentResolver(); - mIsCreate = false; - loadFromCursor(id); - mDataList = new ArrayList(); - if (mType == Notes.TYPE_NOTE) - loadDataContent(); - mDiffNoteValues = new ContentValues(); - - } - - /* - * 功能描述:通过id从光标处加载数据 - * Made By CuiCan - */ - private void loadFromCursor(long id) { - Cursor c = null; - try { - c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)", - new String[] { - String.valueOf(id) - }, null);//通过id获得对应的ContentResolver中的cursor - if (c != null) { - c.moveToNext(); - loadFromCursor(c);//然后加载数据进行初始化,这样函数 - //SqlNote(Context context, long id)与SqlNote(Context context, long id)的实现方式基本相同 - } else { - Log.w(TAG, "loadFromCursor: cursor = null"); - } - } finally { - if (c != null) - c.close(); + //差异数据值 + mDiffDataValues = new ContentValues(); + } + private void loadFromCursor(Cursor c) { + // 从游标中加载数据 + mDataId = c.getLong(DATA_ID_COLUMN); + mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN); + mDataContent = c.getString(DATA_CONTENT_COLUMN); + mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN); + mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN); + } + + public void setContent(JSONObject js) throws JSONException { + // 从JSONObject中设置数据 + long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID; + if (mIsCreate || mDataId != dataId) { + mDiffDataValues.put(DataColumns.ID, dataId); } - } - - /* - * 功能描述:通过游标从光标处加载数据 - * Made By CuiCan - */ - private void loadFromCursor(Cursor c) { - //直接从一条记录中的获得以下变量的初始值 - mId = c.getLong(ID_COLUMN); - mAlertDate = c.getLong(ALERTED_DATE_COLUMN); - mBgColorId = c.getInt(BG_COLOR_ID_COLUMN); - mCreatedDate = c.getLong(CREATED_DATE_COLUMN); - mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN); - mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN); - mParentId = c.getLong(PARENT_ID_COLUMN); - mSnippet = c.getString(SNIPPET_COLUMN); - mType = c.getInt(TYPE_COLUMN); - mWidgetId = c.getInt(WIDGET_ID_COLUMN); - mWidgetType = c.getInt(WIDGET_TYPE_COLUMN); - mVersion = c.getLong(VERSION_COLUMN); - } - - /* - * 功能描述:通过content机制获取共享数据并加载到数据库当前游标处 - * 参数注解: - * Made By CuiCan - */ - private void loadDataContent() { - Cursor c = null; - mDataList.clear(); - try { - c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA, - "(note_id=?)", new String[] { - String.valueOf(mId) - }, null); - if (c != null) { - if (c.getCount() == 0) { - Log.w(TAG, "it seems that the note has not data"); - return; - } - while (c.moveToNext()) { - SqlData data = new SqlData(mContext, c); - mDataList.add(data); - } - } else { - Log.w(TAG, "loadDataContent: cursor = null"); - } - } finally { - if (c != null) - c.close(); + mDataId = dataId; + +// private void initDataMimeType(JSONObject js) { +// // 从JSONObject中获取MIME_TYPE + String dataMimeType = js.has(DataColumns.MIME_TYPE) ? js.getString(DataColumns.MIME_TYPE) + : DataConstants.NOTE; +// // 如果mIsCreate为true或者mDataMimeType不等于dataMimeType + if (mIsCreate || !mDataMimeType.equals(dataMimeType)) { +// // 将dataMimeType放入mDiffDataValues中 + mDiffDataValues.put(DataColumns.MIME_TYPE, dataMimeType); } - } - - /* - * 功能描述:设置通过content机制用于共享的数据信息 - * 参数注解: - * Made By CuiCan - */ - public boolean setContent(JSONObject js) { - try { - JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); - if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) { - Log.w(TAG, "cannot set system folder"); - } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { - // for folder we can only update the snnipet and type - String snippet = note.has(NoteColumns.SNIPPET) ? note - .getString(NoteColumns.SNIPPET) : ""; - if (mIsCreate || !mSnippet.equals(snippet)) { - mDiffNoteValues.put(NoteColumns.SNIPPET, snippet); - } - mSnippet = snippet; - - int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE) - : Notes.TYPE_NOTE; - if (mIsCreate || mType != type) { - mDiffNoteValues.put(NoteColumns.TYPE, type); - } - mType = type; - } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) { - JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); - long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID; - if (mIsCreate || mId != id) { - mDiffNoteValues.put(NoteColumns.ID, id); - } - mId = id; - - long alertDate = note.has(NoteColumns.ALERTED_DATE) ? note - .getLong(NoteColumns.ALERTED_DATE) : 0; - if (mIsCreate || mAlertDate != alertDate) { - mDiffNoteValues.put(NoteColumns.ALERTED_DATE, alertDate); - } - mAlertDate = alertDate; - - int bgColorId = note.has(NoteColumns.BG_COLOR_ID) ? note - .getInt(NoteColumns.BG_COLOR_ID) : ResourceParser.getDefaultBgId(mContext); - if (mIsCreate || mBgColorId != bgColorId) { - mDiffNoteValues.put(NoteColumns.BG_COLOR_ID, bgColorId); - } - mBgColorId = bgColorId; - long createDate = note.has(NoteColumns.CREATED_DATE) ? note - .getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis(); - if (mIsCreate || mCreatedDate != createDate) { - mDiffNoteValues.put(NoteColumns.CREATED_DATE, createDate); - } - mCreatedDate = createDate; - - int hasAttachment = note.has(NoteColumns.HAS_ATTACHMENT) ? note - .getInt(NoteColumns.HAS_ATTACHMENT) : 0; - if (mIsCreate || mHasAttachment != hasAttachment) { - mDiffNoteValues.put(NoteColumns.HAS_ATTACHMENT, hasAttachment); - } - mHasAttachment = hasAttachment; - - long modifiedDate = note.has(NoteColumns.MODIFIED_DATE) ? note - .getLong(NoteColumns.MODIFIED_DATE) : System.currentTimeMillis(); - if (mIsCreate || mModifiedDate != modifiedDate) { - mDiffNoteValues.put(NoteColumns.MODIFIED_DATE, modifiedDate); - } - mModifiedDate = modifiedDate; - - long parentId = note.has(NoteColumns.PARENT_ID) ? note - .getLong(NoteColumns.PARENT_ID) : 0; - if (mIsCreate || mParentId != parentId) { - mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId); - } - mParentId = parentId; - - String snippet = note.has(NoteColumns.SNIPPET) ? note - .getString(NoteColumns.SNIPPET) : ""; - if (mIsCreate || !mSnippet.equals(snippet)) { - mDiffNoteValues.put(NoteColumns.SNIPPET, snippet); - } - mSnippet = snippet; - - int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE) - : Notes.TYPE_NOTE; - if (mIsCreate || mType != type) { - mDiffNoteValues.put(NoteColumns.TYPE, type); - } - mType = type; - - int widgetId = note.has(NoteColumns.WIDGET_ID) ? note.getInt(NoteColumns.WIDGET_ID) - : AppWidgetManager.INVALID_APPWIDGET_ID; - if (mIsCreate || mWidgetId != widgetId) { - mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId); - } - mWidgetId = widgetId; - - int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? note - .getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE; - if (mIsCreate || mWidgetType != widgetType) { - mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType); - } - mWidgetType = widgetType; - - long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note - .getLong(NoteColumns.ORIGIN_PARENT_ID) : 0; - if (mIsCreate || mOriginParent != originParent) { - mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent); - } - mOriginParent = originParent; - - for (int i = 0; i < dataArray.length(); i++) { - JSONObject data = dataArray.getJSONObject(i); - SqlData sqlData = null; - if (data.has(DataColumns.ID)) { - long dataId = data.getLong(DataColumns.ID); - for (SqlData temp : mDataList) { - if (dataId == temp.getId()) { - sqlData = temp; - } - } - } - - if (sqlData == null) { - sqlData = new SqlData(mContext); - mDataList.add(sqlData); - } - - sqlData.setContent(data); - } - } - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - return false; +// // 将dataMimeType赋值给mDataMimeType + mDataMimeType = dataMimeType; + // 获取js中的内容 + String dataContent = js.has(DataColumns.CONTENT) ? js.getString(DataColumns.CONTENT) : ""; + // 判断是否创建或者内容是否改变 + if (mIsCreate || !mDataContent.equals(dataContent)) { + // 将内容添加到不同的数据值中 + mDiffDataValues.put(DataColumns.CONTENT, dataContent); } - return true; - } - - /* - * 功能描述:获取content机制提供的数据并加载到note中 - * 参数注解: - * Made By CuiCan - */ - public JSONObject getContent() { - try { - JSONObject js = new JSONObject(); - - if (mIsCreate) { - Log.e(TAG, "it seems that we haven't created this in database yet"); - return null; - } - - JSONObject note = new JSONObject(); - if (mType == Notes.TYPE_NOTE) {//类型为note时 - note.put(NoteColumns.ID, mId); - note.put(NoteColumns.ALERTED_DATE, mAlertDate); - note.put(NoteColumns.BG_COLOR_ID, mBgColorId); - note.put(NoteColumns.CREATED_DATE, mCreatedDate); - note.put(NoteColumns.HAS_ATTACHMENT, mHasAttachment); - note.put(NoteColumns.MODIFIED_DATE, mModifiedDate); - note.put(NoteColumns.PARENT_ID, mParentId); - note.put(NoteColumns.SNIPPET, mSnippet); - note.put(NoteColumns.TYPE, mType); - note.put(NoteColumns.WIDGET_ID, mWidgetId); - note.put(NoteColumns.WIDGET_TYPE, mWidgetType); - note.put(NoteColumns.ORIGIN_PARENT_ID, mOriginParent); - js.put(GTaskStringUtils.META_HEAD_NOTE, note); - - JSONArray dataArray = new JSONArray(); - for (SqlData sqlData : mDataList) { - JSONObject data = sqlData.getContent(); - if (data != null) { - dataArray.put(data); - } - } - js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); - } else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) {//类型为文件夹或者 - note.put(NoteColumns.ID, mId); - note.put(NoteColumns.TYPE, mType); - note.put(NoteColumns.SNIPPET, mSnippet); - js.put(GTaskStringUtils.META_HEAD_NOTE, note); - } - - return js; - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); + // 更新数据内容 + mDataContent = dataContent; + // 获取dataContent的data1 + long dataContentData1 = js.has(DataColumns.DATA1) ? js.getLong(DataColumns.DATA1) : 0; + // 如果mIsCreate为true或者dataContentData1不等于mDataContentData1,则将dataContentData1放入mDiffDataValues中 + if (mIsCreate || mDataContentData1 != dataContentData1) { + mDiffDataValues.put(DataColumns.DATA1, dataContentData1); } - return null; - } - - /* - * 功能描述:给当前id设置父id - * 参数注解: - * Made By CuiCan - */ - public void setParentId(long id) { - mParentId = id; - mDiffNoteValues.put(NoteColumns.PARENT_ID, id); - } - - /* - * 功能描述:给当前id设置Gtaskid - * 参数注解: - * Made By CuiCan - */ - public void setGtaskId(String gid) { - mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); - } - - /* - * 功能描述:给当前id设置同步id - * 参数注解: - * Made By CuiCan - */ - public void setSyncId(long syncId) { - mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); - } - - /* - * 功能描述:初始化本地修改,即撤销所有当前修改 - * 参数注解: - * Made By CuiCan - */ - public void resetLocalModified() { - mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); - } - - /* - * 功能描述:获得当前id - * 参数注解: - * Made By CuiCan - */ - public long getId() { - return mId; - } - - /* - * 功能描述:获得当前id的父id - * 参数注解: - * Made By CuiCan - */ - public long getParentId() { - return mParentId; - } - - /* - * 功能描述:获取小片段即用于显示的部分便签内容 - * 参数注解: - * Made By CuiCan - */ - public String getSnippet() { - return mSnippet; - } - - /* - * 功能描述:判断是否为便签类型 - * 参数注解: - * Made By CuiCan - */ - public boolean isNoteType() { - return mType == Notes.TYPE_NOTE; - } - - /* - * 功能描述:commit函数用于把当前造作所做的修改保存到数据库 - * 参数注解: - * Made By CuiCan - */ - public void commit(boolean validateVersion) { + // 将dataContentData1赋值给mDataContentData1 + mDataContentData1 = dataContentData1; + + // 获取dataContent的data3 + String dataContentData3 = js.has(DataColumns.DATA3) ? js.getString(DataColumns.DATA3) : ""; + // 如果mIsCreate为true或者dataContentData3不等于mDataContentData3,则将dataContentData3放入mDiffDataValues中 + if (mIsCreate || !mDataContentData3.equals(dataContentData3)) { + mDiffDataValues.put(DataColumns.DATA3, dataContentData3); + } + // 将dataContentData3赋值给mDataContentData3 + mDataContentData3 = dataContentData3; + } + + //获取内容 +//获取内容 +public JSONObject getContent() throws JSONException { + //判断是否创建 if (mIsCreate) { - if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) { - mDiffNoteValues.remove(NoteColumns.ID); + Log.e(TAG, "it seems that we haven't created this in database yet"); + return null; + } + //创建JSONObject + JSONObject js = new JSONObject(); + //添加ID + js.put(DataColumns.ID, mDataId); + //添加MIME_TYPE + js.put(DataColumns.MIME_TYPE, mDataMimeType); + //添加CONTENT + js.put(DataColumns.CONTENT, mDataContent); + //添加DATA1 + js.put(DataColumns.DATA1, mDataContentData1); + //添加DATA3 + js.put(DataColumns.DATA3, mDataContentData3); + //返回JSONObject + return js; + } + + public void commit(long noteId, boolean validateVersion, long version) { + + // 判断是否是创建操作 + if (mIsCreate) { + // 如果数据ID为无效ID,并且数据值中包含ID + if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) { + // 移除ID + mDiffDataValues.remove(DataColumns.ID); } - - Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues); + + // 添加noteId + mDiffDataValues.put(DataColumns.NOTE_ID, noteId); + // 插入数据 + Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues); try { - mId = Long.valueOf(uri.getPathSegments().get(1)); + // 获取插入数据的ID + mDataId = Long.valueOf(uri.getPathSegments().get(1)); } catch (NumberFormatException e) { Log.e(TAG, "Get note id error :" + e.toString()); throw new ActionFailureException("create note failed"); } - if (mId == 0) { - throw new IllegalStateException("Create thread id failed"); - } - - if (mType == Notes.TYPE_NOTE) { - for (SqlData sqlData : mDataList) {//直接使用sqldata中的实现 - sqlData.commit(mId, false, -1); - } - } } else { - if (mId <= 0 && mId != Notes.ID_ROOT_FOLDER && mId != Notes.ID_CALL_RECORD_FOLDER) { - Log.e(TAG, "No such note"); - throw new IllegalStateException("Try to update note with invalid id"); - } - if (mDiffNoteValues.size() > 0) { - mVersion ++; + // 更新数据 + if (mDiffDataValues.size() > 0) { int result = 0; - if (!validateVersion) {//构造字符串 - result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" - + NoteColumns.ID + "=?)", new String[] { - String.valueOf(mId) - }); + if (!validateVersion) { + // 更新数据 + result = mContentResolver.update(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null); } else { - result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" - + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)", - new String[] { - String.valueOf(mId), String.valueOf(mVersion) + // 更新数据,并且检查版本号 + result = mContentResolver.update(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, + " ? in (SELECT " + NoteColumns.ID + " FROM " + TABLE.NOTE + + " WHERE " + NoteColumns.VERSION + "=?)", new String[] { + String.valueOf(noteId), String.valueOf(version) }); } if (result == 0) { Log.w(TAG, "there is no update. maybe user updates note when syncing"); } } - - if (mType == Notes.TYPE_NOTE) { - for (SqlData sqlData : mDataList) { - sqlData.commit(mId, validateVersion, mVersion); - } - } } - - // refresh local info - loadFromCursor(mId); - if (mType == Notes.TYPE_NOTE) - loadDataContent(); - - mDiffNoteValues.clear(); + mDiffDataValues.clear(); mIsCreate = false; } -} \ No newline at end of file + + public long getId() { + return mDataId; + } +} diff --git a/标注/SqlNote.java b/标注/SqlNote.java new file mode 100644 index 0000000..7cfccf0 --- /dev/null +++ b/标注/SqlNote.java @@ -0,0 +1,669 @@ +/* + * 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.gtask.data; + +import android.appwidget.AppWidgetManager; +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.util.Log; + +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.gtask.exception.ActionFailureException; +import net.micode.notes.tool.GTaskStringUtils; +import net.micode.notes.tool.ResourceParser; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + + +public class SqlNote { + private static final String TAG = SqlNote.class.getSimpleName(); + + private static final int INVALID_ID = -99999; + + public static final String[] PROJECTION_NOTE = new String[] { + NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID, + NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE, + NoteColumns.NOTES_COUNT, NoteColumns.PARENT_ID, NoteColumns.SNIPPET, NoteColumns.TYPE, + NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE, NoteColumns.SYNC_ID, + NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID, + NoteColumns.VERSION + }; + + public static final int ID_COLUMN = 0; + + // 警报日期列 + public static final int ALERTED_DATE_COLUMN = 1; + + // 背景颜色ID列 + public static final int BG_COLOR_ID_COLUMN = 2; + + // 创建日期列 + public static final int CREATED_DATE_COLUMN = 3; + + // 是否有附件列 + public static final int HAS_ATTACHMENT_COLUMN = 4; + + // 修改日期列 + public static final int MODIFIED_DATE_COLUMN = 5; + + // 注释计数列 + public static final int NOTES_COUNT_COLUMN = 6; + + // 父ID列 + public static final int PARENT_ID_COLUMN = 7; + + // 片段列 + public static final int SNIPPET_COLUMN = 8; + + // 类型列 + public static final int TYPE_COLUMN = 9; + + // 小部件ID列 + public static final int WIDGET_ID_COLUMN = 10; + + // 小部件类型列 + public static final int WIDGET_TYPE_COLUMN = 11; + + // 同步ID列 + public static final int SYNC_ID_COLUMN =12; + + // 本地修改时间列 +public static final int LOCAL_MODIFIED_COLUMN = 13; + +// 父ID列 + public static final int ORIGIN_PARENT_ID_COLUMN = 14; + +// 任务ID列 + public static final int GTASK_ID_COLUMN = 15; + +// 版本列 + public static final int VERSION_COLUMN = 16; + +// 上下文 + private Context mContext; + +// 内容解析器 + private ContentResolver mContentResolver; + +// 是否创建 + private boolean mIsCreate; + +// ID + private long mId; + +// 警报日期 + private long mAlertDate; + +// 背景颜色ID + private int mBgColorId; + private long mCreatedDate; + + // 0 = no, 1 = yes + private int mHasAttachment; + + // 0 = no, 1 = yes + private long mModifiedDate; + + // parent id + private long mParentId; + + // 160 characters max + private String mSnippet; + + // 0 = note, 1 = task, 2 = both + private int mType; + + // widget id + private int mWidgetId; + + // widget type + private int mWidgetType; + + // origin parent id + private long mOriginParent; + + // version + private long mVersion; + + private ContentValues mDiffNoteValues; + + // 用于存储SqlData对象的列表 + private ArrayList mDataList; + + // 构造函数,传入上下文 + // 创建SqlNote对象,并初始化 +public SqlNote(Context context) { + mContext = context; + //获取上下文 + mContentResolver = context.getContentResolver(); + //获取内容解析器 + mIsCreate = true; + //是否创建 + mId = INVALID_ID; + //ID + mAlertDate = 0; + //警报日期 + mBgColorId = ResourceParser.getDefaultBgId(context); + //背景颜色ID + mCreatedDate = System.currentTimeMillis(); + //创建日期 + mHasAttachment = 0; + //是否有附件 + mModifiedDate = System.currentTimeMillis(); + //修改日期 + mParentId = 0; + //父ID + mSnippet = ""; + //摘要 + mType = Notes.TYPE_NOTE; + //类型 + mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; + //小部件ID + mWidgetType = Notes.TYPE_WIDGET_INVALIDE; + //小部件类型 + mOriginParent = 0; + //来源父 + mVersion = 0; + //版本 + mDiffNoteValues = new ContentValues(); + mDataList = new ArrayList(); + } +// 创建SqlNote对象,从Cursor中加载数据 +public SqlNote(Context context, Cursor c) { + mContext = context; + mContentResolver = context.getContentResolver(); + mIsCreate = false; + loadFromCursor(c); + mDataList = new ArrayList(); + if (mType == Notes.TYPE_NOTE) + loadDataContent(); + mDiffNoteValues = new ContentValues(); + } + + // 创建SqlNote对象,传入上下文和id +public SqlNote(Context context, long id) { + mContext = context; + mContentResolver = context.getContentResolver(); + mIsCreate = false; + loadFromCursor(id); + mDataList = new ArrayList(); + if (mType == Notes.TYPE_NOTE) + loadDataContent(); + mDiffNoteValues = new ContentValues(); + + } + + private void loadFromCursor(long id) { + Cursor c = null; + try { + // 根据id查询Notes表中的数据 + c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)", + new String[] { + String.valueOf(id) + }, null); + if (c != null) { + // 将查询到的数据加载到当前对象中 + c.moveToNext(); + loadFromCursor(c); + } else { + Log.w(TAG, "loadFromCursor: cursor = null"); + } + } finally { + if (c != null) + c.close(); + } + } + private void loadFromCursor(Cursor c) { + // 从游标中获取ID + mId = c.getLong(ID_COLUMN); + // 从游标中获取提示日期 + mAlertDate = c.getLong(ALERTED_DATE_COLUMN); + // 从游标中获取背景颜色ID + mBgColorId = c.getInt(BG_COLOR_ID_COLUMN); + // 从游标中获取创建日期 + mCreatedDate = c.getLong(CREATED_DATE_COLUMN); + // 从游标中获取是否有附件 + mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN); + // 从游标中获取修改日期 + mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN); + // 从游标中获取父ID + mParentId = c.getLong(PARENT_ID_COLUMN); + // 从游标中获取摘要 + mSnippet = c.getString(SNIPPET_COLUMN); + // 从游标中获取类型 + mType = c.getInt(TYPE_COLUMN); + // 从游标中获取小部件ID + mWidgetId = c.getInt(WIDGET_ID_COLUMN); + // 从游标中获取小部件类型 + mWidgetType = c.getInt(WIDGET_TYPE_COLUMN); + // 从游标中获取版本 + mVersion = c.getLong(VERSION_COLUMN); + } + + private void loadDataContent() { + Cursor c = null; + mDataList.clear(); + try { + // 根据note_id查询数据 + c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA, + "(note_id=?)", new String[] { + String.valueOf(mId) + }, null); + if (c != null) { + // 判断是否有数据 + if (c.getCount() == 0) { + Log.w(TAG, "it seems that the note has not data"); + return; + } + // 遍历查询到的数据 + while (c.moveToNext()) { + SqlData data = new SqlData(mContext, c); + mDataList.add(data); + } + } else { + Log.w(TAG, "loadDataContent: cursor = null"); + } + } finally { + if (c != null) + c.close(); + } + } + + public boolean setContent(JSONObject js) { + try { + // 获取js中的meta_head_note对象 + JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + // 判断note的类型是否为系统文件夹 + if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) { + Log.w(TAG, "cannot set system folder"); + } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { + // 判断note的类型是否为文件夹 + // for folder we can only update the snnipet and type + // 获取note中的snippet + String snippet = note.has(NoteColumns.SNIPPET) ? note + .getString(NoteColumns.SNIPPET) : ""; + // 判断是否为创建,或者mSnippet是否与note中的snippet不同 + if (mIsCreate || !mSnippet.equals(snippet)) { + // 将mSnippet与note中的snippet进行比较,不同的值放入mDiffNoteValues中 + mDiffNoteValues.put(NoteColumns.SNIPPET, snippet); + } + // 将note中的snippet赋值给mSnippet + mSnippet = snippet; + + // 获取note中的type + int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE) + : Notes.TYPE_NOTE; + // 判断是否为创建,或者mType是否与note中的type不同 + if (mIsCreate || mType != type) { + // 将mType与note中的type进行比较,不同的值放入mDiffNoteValues中 + mDiffNoteValues.put(NoteColumns.TYPE, type); + } + // 将note中的type赋值给mType + mType = type; + } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) { + // 获取JSONArray类型的dataArray + JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + // 获取note中的id + long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID; + // 判断是否是创建操作,或者mId和id不相等 + if (mIsCreate || mId != id) { + mDiffNoteValues.put(NoteColumns.ID, id); + } + mId = id; + + // 获取note中的alertDate + long alertDate = note.has(NoteColumns.ALERTED_DATE) ? note + .getLong(NoteColumns.ALERTED_DATE) : 0; + // 判断是否是创建操作,或者mAlertDate和alertDate不相等 + if (mIsCreate || mAlertDate != alertDate) { + mDiffNoteValues.put(NoteColumns.ALERTED_DATE, alertDate); + } + mAlertDate = alertDate; + + // 获取note中的bgColorId + int bgColorId = note.has(NoteColumns.BG_COLOR_ID) ? note + .getInt(NoteColumns.BG_COLOR_ID) : ResourceParser.getDefaultBgId(mContext); + // 判断是否是创建操作,或者mBgColorId和bgColorId不相等 + if (mIsCreate || mBgColorId != bgColorId) { + mDiffNoteValues.put(NoteColumns.BG_COLOR_ID, bgColorId); + } + mBgColorId = bgColorId; + + // 获取note中的createDate + long createDate = note.has(NoteColumns.CREATED_DATE) ? note + .getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis(); + // 判断是否是创建操作,或者mCreatedDate和createDate不相等 + if (mIsCreate || mCreatedDate != createDate) { + mDiffNoteValues.put(NoteColumns.CREATED_DATE, createDate); + } + mCreatedDate = createDate; + + // 获取是否有附件 + int hasAttachment = note.has(NoteColumns.HAS_ATTACHMENT) ? note + .getInt(NoteColumns.HAS_ATTACHMENT) : 0; + // 如果创建或者有附件改变 + if (mIsCreate || mHasAttachment != hasAttachment) { + // 将附件改变添加到不同的值 + mDiffNoteValues.put(NoteColumns.HAS_ATTACHMENT, hasAttachment); + } + mHasAttachment = hasAttachment; + + // 获取修改日期 + long modifiedDate = note.has(NoteColumns.MODIFIED_DATE) ? note + .getLong(NoteColumns.MODIFIED_DATE) : System.currentTimeMillis(); + // 如果创建或者修改日期改变 + if (mIsCreate || mModifiedDate != modifiedDate) { + // 将修改日期改变添加到不同的值 + mDiffNoteValues.put(NoteColumns.MODIFIED_DATE, modifiedDate); + } + mModifiedDate = modifiedDate; + + // 获取父ID + long parentId = note.has(NoteColumns.PARENT_ID) ? note + .getLong(NoteColumns.PARENT_ID) : 0; + // 如果创建或者父ID改变 + if (mIsCreate || mParentId != parentId) { + // 将父ID改变添加到不同的值 + mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId); + } + mParentId = parentId; + + String snippet = note.has(NoteColumns.SNIPPET) ? note + .getString(NoteColumns.SNIPPET) : ""; + // 判断是否创建或者是否修改了摘要 + if (mIsCreate || !mSnippet.equals(snippet)) { + mDiffNoteValues.put(NoteColumns.SNIPPET, snippet); + } + mSnippet = snippet; + + // 获取类型 + int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE) + : Notes.TYPE_NOTE; + // 判断是否创建或者是否修改了类型 + if (mIsCreate || mType != type) { + mDiffNoteValues.put(NoteColumns.TYPE, type); + } + mType = type; + // 获取note中的widgetId + int widgetId = note.has(NoteColumns.WIDGET_ID) ? note.getInt(NoteColumns.WIDGET_ID) + : AppWidgetManager.INVALID_APPWIDGET_ID; + // 如果mIsCreate为true或者mWidgetId不等于widgetId,则将widgetId添加到mDiffNoteValues中 + if (mIsCreate || mWidgetId != widgetId) { + mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId); + } + // 将widgetId赋值给mWidgetId + mWidgetId = widgetId; + + // 获取note中的widgetType + int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? note + .getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE; + // 如果mIsCreate为true或者mWidgetType不等于widgetType,则将widgetType添加到mDiffNoteValues中 + if (mIsCreate || mWidgetType != widgetType) { + mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType); + } + // 将widgetType赋值给mWidgetType + mWidgetType = widgetType; + + // 获取note中的originParent + long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note + .getLong(NoteColumns.ORIGIN_PARENT_ID) : 0; + // 如果mIsCreate为true或者mOriginParent不等于originParent,则将originParent添加到mDiffNoteValues中 + if (mIsCreate || mOriginParent != originParent) { + mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent); + } + // 将originParent赋值给mOriginParent + mOriginParent = originParent; + + // 遍历dataArray中的每一个JSONObject + for (int i = 0; i < dataArray.length(); i++) { + JSONObject data = dataArray.getJSONObject(i); + SqlData sqlData = null; + // 判断data中是否有ID字段 + if (data.has(DataColumns.ID)) { + // 获取ID字段的值 + long dataId = data.getLong(DataColumns.ID); + // 遍历mDataList中的每一个SqlData + for (SqlData temp : mDataList) { + // 如果ID字段的值和SqlData的ID值相等 + if (dataId == temp.getId()) { + // 将temp赋值给sqlData + sqlData = temp; + } + } + } + + // 如果sqlData为null,则创建一个新的SqlData + if (sqlData == null) { + sqlData = new SqlData(mContext); + // 将sqlData添加到mDataList中 + mDataList.add(sqlData); + } + + // 设置SqlData的内容 + sqlData.setContent(data); + } + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + return false; + } + return true; + } + + public JSONObject getContent() { + try { + JSONObject js = new JSONObject(); + + // 检查是否已经创建 + if (mIsCreate) { + Log.e(TAG, "it seems that we haven't created this in database yet"); + return null; + } + // 创建一个JSONObject对象note + JSONObject note = new JSONObject(); + // 如果mType等于Notes.TYPE_NOTE + if (mType == Notes.TYPE_NOTE) { + // 将mId放入note中 + note.put(NoteColumns.ID, mId); + // 将mAlertDate放入note中 + note.put(NoteColumns.ALERTED_DATE, mAlertDate); + // 将mBgColorId放入note中 + note.put(NoteColumns.BG_COLOR_ID, mBgColorId); + // 将mCreatedDate放入note中 + note.put(NoteColumns.CREATED_DATE, mCreatedDate); + // 将mHasAttachment放入note中 + note.put(NoteColumns.HAS_ATTACHMENT, mHasAttachment); + // 将mModifiedDate放入note中 + note.put(NoteColumns.MODIFIED_DATE, mModifiedDate); + // 将mParentId放入note中 + note.put(NoteColumns.PARENT_ID, mParentId); + // 将mSnippet放入note中 + note.put(NoteColumns.SNIPPET, mSnippet); + // 将mType放入note中 + note.put(NoteColumns.TYPE, mType); + // 将mWidgetId放入note中 + note.put(NoteColumns.WIDGET_ID, mWidgetId); + // 将mWidgetType放入note中 + note.put(NoteColumns.WIDGET_TYPE, mWidgetType); + // 将mOriginParent放入note中 + note.put(NoteColumns.ORIGIN_PARENT_ID, mOriginParent); + // 将note放入js中 + js.put(GTaskStringUtils.META_HEAD_NOTE, note); + + // 创建一个JSONArray对象dataArray + JSONArray dataArray = new JSONArray(); + // 遍历mDataList + for (SqlData sqlData : mDataList) { + // 获取sqlData的content + JSONObject data = sqlData.getContent(); + // 如果data不为空 + if (data != null) { + // 将data放入dataArray中 + dataArray.put(data); + } + } + // 将dataArray放入js中 + js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); + // 如果mType等于Notes.TYPE_FOLDER或者mType等于Notes.TYPE_SYSTEM + } else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) { + // 将mId放入note中 + note.put(NoteColumns.ID, mId); + // 将mType放入note中 + note.put(NoteColumns.TYPE, mType); + // 将mSnippet放入note中 + note.put(NoteColumns.SNIPPET, mSnippet); + // 将note放入js中 + js.put(GTaskStringUtils.META_HEAD_NOTE, note); + } + + return js; + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + } + return null; + } + + // 设置父id + public void setParentId(long id) { + mParentId = id; + mDiffNoteValues.put(NoteColumns.PARENT_ID, id); + } + + // 设置GtaskId + public void setGtaskId(String gid) { + mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); + } + + // 设置同步id + public void setSyncId(long syncId) { + mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); + } + + // 重置本地修改时间 + public void resetLocalModified() { + mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); + } + + // 获取id + public long getId() { + return mId; + } + + //获取父ID +public long getParentId() { + return mParentId; + } + + //获取摘要 + public String getSnippet() { + return mSnippet; + } + + //判断是否为笔记类型 + public boolean isNoteType() { + return mType == Notes.TYPE_NOTE; + } + + //提交 + public void commit(boolean validateVersion) { + if (mIsCreate) { + if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) { + mDiffNoteValues.remove(NoteColumns.ID); + } + + Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues); + try { + mId = Long.valueOf(uri.getPathSegments().get(1)); + } catch (NumberFormatException e) { + Log.e(TAG, "Get note id error :" + e.toString()); + throw new ActionFailureException("create note failed"); + } + if (mId == 0) { + throw new IllegalStateException("Create thread id failed"); + } + + if (mType == Notes.TYPE_NOTE) { + for (SqlData sqlData : mDataList) { + sqlData.commit(mId, false, -1); + } + } + } else { + // 检查mId是否有效 + if (mId <= 0 && mId != Notes.ID_ROOT_FOLDER && mId != Notes.ID_CALL_RECORD_FOLDER) { + Log.e(TAG, "No such note"); + throw new IllegalStateException("Try to update note with invalid id"); + } + // 检查是否有更新 + if (mDiffNoteValues.size() > 0) { + // 更新版本号 + mVersion ++; + int result = 0; + // 检查是否是同步更新 + if (!validateVersion) { + // 更新 + result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" + + NoteColumns.ID + "=?)", new String[] { + String.valueOf(mId) + }); + } else { + // 更新 + result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" + + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)", + new String[] { + String.valueOf(mId), String.valueOf(mVersion) + }); + } + // 检查是否更新成功 + if (result == 0) { + Log.w(TAG, "there is no update. maybe user updates note when syncing"); + } + } + // 判断mType是否为Notes.TYPE_NOTE + if (mType == Notes.TYPE_NOTE) { + // 遍历mDataList + for (SqlData sqlData : mDataList) { + // 提交数据 + sqlData.commit(mId, validateVersion, mVersion); + } + } + } + + // refresh local info + // 从游标中加载数据 + loadFromCursor(mId); + // 判断mType是否为Notes.TYPE_NOTE + if (mType == Notes.TYPE_NOTE) + // 加载数据内容 + loadDataContent(); + + // 清除mDiffNoteValues + mDiffNoteValues.clear(); + // 设置mIsCreate为false + mIsCreate = false; + } +} diff --git a/标注/Task.java b/标注/Task.java index 79610b7..ca97b49 100644 --- a/标注/Task.java +++ b/标注/Task.java @@ -1,41 +1,74 @@ +/* + * 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.gtask.data; - + +import android.database.Cursor; +import android.text.TextUtils; +import android.util.Log; + +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 net.micode.notes.gtask.exception.ActionFailureException; +import net.micode.notes.tool.GTaskStringUtils; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + + public class Task extends Node { private static final String TAG = Task.class.getSimpleName(); - - private boolean mCompleted;//是否完成 - + + private boolean mCompleted; + private String mNotes; - - private JSONObject mMetaInfo;//将在实例中存储数据的类型 - - private Task mPriorSibling;//对应的优先兄弟Task的指针(待完善) - - private TaskList mParent;//所在的任务列表的指针 - + + private JSONObject mMetaInfo; + + private Task mPriorSibling; + + private TaskList mParent; + public Task() { super(); mCompleted = false; mNotes = null; - mPriorSibling = null;//TaskList中当前Task前面的Task的指针 - mParent = null;//当前Task所在的TaskList + mPriorSibling = null; + mParent = null; mMetaInfo = null; } - - public JSONObject getCreateAction(int actionId) { + + //获取创建动作 +public JSONObject getCreateAction(int actionId) { JSONObject js = new JSONObject(); - + try { // action_type js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE); - + // action_id js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); - + // index js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this)); - + // entity_delta JSONObject entity = new JSONObject(); entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); @@ -46,49 +79,44 @@ public class Task extends Node { entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes()); } js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); - - // parent_id - if (mParent!= null) { + // parent_id js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid()); - } - + // dest_parent_type js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE, GTaskStringUtils.GTASK_JSON_TYPE_GROUP); - + // list_id - if (mParent!= null) { js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid()); - } - + // prior_sibling_id if (mPriorSibling != null) { js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid()); } - + } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("fail to generate task-create jsonobject"); } - + return js; } - + public JSONObject getUpdateAction(int actionId) { JSONObject js = new JSONObject(); - + try { // action_type js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE); - + // action_id js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); - + // id js.put(GTaskStringUtils.GTASK_JSON_ID, getGid()); - + // entity_delta JSONObject entity = new JSONObject(); entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); @@ -97,16 +125,16 @@ public class Task extends Node { } entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted()); js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); - - } catch (JSONException e) { + + }catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("fail to generate task-update jsonobject"); } - + return js; } - + public void setContentByRemoteJSON(JSONObject js) { if (js != null) { try { @@ -114,69 +142,79 @@ public class Task extends Node { if (js.has(GTaskStringUtils.GTASK_JSON_ID)) { setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); } - + // last_modified if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) { setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)); } - + // name if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) { setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME)); } - + // notes if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) { setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES)); } - + // deleted - if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) { + // 判断js中是否有GTaskStringUtils.GTASK_JSON_DELETED属性 + if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) { + // 设置deleted属性 setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED)); } - + // completed + // 判断js中是否有GTaskStringUtils.GTASK_JSON_COMPLETED属性 if (js.has(GTaskStringUtils.GTASK_JSON_COMPLETED)) { + // 设置completed属性 setCompleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_COMPLETED)); } - } catch (JSONException e) { + }catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("fail to get task content from jsonobject"); } } } - - public void setContentByLocalJSON(JSONObject js) { //��metadata����ʵʩ + + public void setContentByLocalJSON(JSONObject js) { if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE) || !js.has(GTaskStringUtils.META_HEAD_DATA)) { Log.w(TAG, "setContentByLocalJSON: nothing is avaiable"); } - + try { + // 获取JSONObject中的note和dataArray JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); - + + // 判断note的类型是否为Notes.TYPE_NOTE if (note.getInt(NoteColumns.TYPE) != Notes.TYPE_NOTE) { - Log.e(TAG, "invalid type"); + Log.e(TAG, "invalid type"); return; } - - for (int i = 0; i < dataArray.length(); i++) { + // 遍历dataArray数组 + for (int i = 0; i < dataArray.length(); i++) { + // 获取dataArray数组中的每一个JSONObject JSONObject data = dataArray.getJSONObject(i); + // 判断data中MIME_TYPE字段的值是否为NOTE if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) { + // 设置name字段 setName(data.getString(DataColumns.CONTENT)); + // 跳出循环 break; } } - + } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); } } - - public JSONObject getLocalJSONFromContent() { + + public JSONObject getLocalJSONFromContent() { String name = getName(); try { if (mMetaInfo == null) { @@ -185,22 +223,32 @@ public class Task extends Node { Log.w(TAG, "the note seems to be an empty one"); return null; } - + JSONObject js = new JSONObject(); + // 创建一个JSONObject对象,用于存储JSON数据 JSONObject note = new JSONObject(); + // 创建一个JSONObject对象,用于存储JSON数据 JSONArray dataArray = new JSONArray(); + // 创建一个JSONArray对象,用于存储JSON数据 JSONObject data = new JSONObject(); + // 创建一个JSONObject对象,用于存储JSON数据 data.put(DataColumns.CONTENT, name); + // 将name的值放入data中 dataArray.put(data); + // 将data放入dataArray中 js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); + // 将dataArray放入js中 note.put(NoteColumns.TYPE, Notes.TYPE_NOTE); + // 将Notes.TYPE_NOTE放入note中 js.put(GTaskStringUtils.META_HEAD_NOTE, note); + // 将note放入js中 return js; } else { - // synced task + // synced task JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA); - + + // 遍历dataArray,找到mime_type为NOTE的data,并将其content设置为getName() for (int i = 0; i < dataArray.length(); i++) { JSONObject data = dataArray.getJSONObject(i); if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) { @@ -208,7 +256,8 @@ public class Task extends Node { break; } } - + + // 将note的type设置为NOTE note.put(NoteColumns.TYPE, Notes.TYPE_NOTE); return mMetaInfo; } @@ -218,8 +267,9 @@ public class Task extends Node { return null; } } - - public void setMetaInfo(MetaData metaData) { + + //设置元数据 +public void setMetaInfo(MetaData metaData) { if (metaData != null && metaData.getNotes() != null) { try { mMetaInfo = new JSONObject(metaData.getNotes()); @@ -229,32 +279,44 @@ public class Task extends Node { } } } - - public int getSyncAction(Cursor c) { + + public int getSyncAction(Cursor c) { try { + // 获取JSONObject JSONObject noteInfo = null; + // 如果mMetaInfo不为空,并且mMetaInfo中有META_HEAD_NOTE if (mMetaInfo != null && mMetaInfo.has(GTaskStringUtils.META_HEAD_NOTE)) { + // 获取JSONObject noteInfo = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); } - + + // 如果noteInfo为空 if (noteInfo == null) { + // 打印日志 Log.w(TAG, "it seems that note meta has been deleted"); + // 返回更新远程的同步动作 return SYNC_ACTION_UPDATE_REMOTE; } - + + // 如果noteInfo中没有ID if (!noteInfo.has(NoteColumns.ID)) { + // 打印日志 Log.w(TAG, "remote note id seems to be deleted"); + // 返回更新远程的同步动作 return SYNC_ACTION_UPDATE_LOCAL; } - + // validate the note id now - if (c.getLong(SqlNote.ID_COLUMN) != noteInfo.getLong(NoteColumns.ID)) { + // 检查note的ID是否与noteInfo中的ID匹配 + if (c.getLong(SqlNote.ID_COLUMN) != noteInfo.getLong(NoteColumns.ID)) { Log.w(TAG, "note id doesn't match"); return SYNC_ACTION_UPDATE_LOCAL; } - + + // 检查本地修改时间 if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { // there is no local update + // 检查同步ID是否与本地修改时间匹配 if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { // no update both side return SYNC_ACTION_NONE; @@ -263,15 +325,17 @@ public class Task extends Node { return SYNC_ACTION_UPDATE_LOCAL; } } else { - // validate gtask id + // validate gtaskid if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { - Log.e(TAG, "gtask id doesn't match"); + Log.e(TAG, "gtask id doesn't match"); return SYNC_ACTION_ERROR; } + // 比较本地和远程的修改时间 if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { // local modification only return SYNC_ACTION_UPDATE_REMOTE; } else { + // remote modification only return SYNC_ACTION_UPDATE_CONFLICT; } } @@ -279,45 +343,53 @@ public class Task extends Node { Log.e(TAG, e.toString()); e.printStackTrace(); } - + return SYNC_ACTION_ERROR; } - + + // 判断是否需要保存 public boolean isWorthSaving() { return mMetaInfo != null || (getName() != null && getName().trim().length() > 0) || (getNotes() != null && getNotes().trim().length() > 0); } - + + // 设置是否完成 public void setCompleted(boolean completed) { this.mCompleted = completed; } - + + // 设置备注 public void setNotes(String notes) { this.mNotes = notes; } - - public void setPriorSibling(Task priorSibling) { + //设置前一个兄弟节点 +public void setPriorSibling(Task priorSibling) { this.mPriorSibling = priorSibling; } - + +//设置父节点 public void setParent(TaskList parent) { this.mParent = parent; } - + +//获取是否完成 public boolean getCompleted() { return this.mCompleted; } - + +//获取备注 public String getNotes() { return this.mNotes; } - - public Task getPriorSibling() { + + public Task getPriorSibling() { + // 获取前一个兄弟节点 return this.mPriorSibling; } - + public TaskList getParent() { + // 获取父节点 return this.mParent; } - -} \ No newline at end of file + +} diff --git a/标注/TaskList.java b/标注/TaskList.java index 15a6bbc..71000cc 100644 --- a/标注/TaskList.java +++ b/标注/TaskList.java @@ -1,86 +1,108 @@ +/* + * 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.gtask.data; - + +import android.database.Cursor; +import android.util.Log; + +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.gtask.exception.ActionFailureException; +import net.micode.notes.tool.GTaskStringUtils; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + + public class TaskList extends Node { - private static final String TAG = TaskList.class.getSimpleName();//tag标记 - - private int mIndex;//当前TaskList的指针 - - private ArrayList mChildren;//类中主要的保存数据的单元,用来实现一个以Task为元素的ArrayList - + private static final String TAG = TaskList.class.getSimpleName(); + + private int mIndex; + + private ArrayList mChildren; + public TaskList() { super(); mChildren = new ArrayList(); mIndex = 1; } - - /* (non-Javadoc) - * @see net.micode.notes.gtask.data.Node#getCreateAction(int) - * 生成并返回一个包含了一定数据的JSONObject实体 - */ + public JSONObject getCreateAction(int actionId) { JSONObject js = new JSONObject(); - + try { // action_type js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE); - + // action_id js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); - + // index js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex); - + // entity_delta - JSONObject entity = new JSONObject();//entity实体 + JSONObject entity = new JSONObject(); entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null"); entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE, GTaskStringUtils.GTASK_JSON_TYPE_GROUP); js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); - + } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("fail to generate tasklist-create jsonobject"); } - + return js; } - - /* (non-Javadoc) - * @see net.micode.notes.gtask.data.Node#getUpdateAction(int) - * 生成并返回一个包含了一定数据的JSONObject实体 - */ - public JSONObject getUpdateAction(int actionId) { + + //获取更新操作的JSONObject +public JSONObject getUpdateAction(int actionId) { JSONObject js = new JSONObject(); - + try { // action_type js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE); - + // action_id js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); - + // id js.put(GTaskStringUtils.GTASK_JSON_ID, getGid()); - + // entity_delta JSONObject entity = new JSONObject(); entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted()); - js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); - + js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("fail to generate tasklist-update jsonobject"); } - + return js; } - + public void setContentByRemoteJSON(JSONObject js) { if (js != null) { try { @@ -88,17 +110,17 @@ public class TaskList extends Node { if (js.has(GTaskStringUtils.GTASK_JSON_ID)) { setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); } - + // last_modified if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) { setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)); } - + // name if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) { setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME)); } - + } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); @@ -106,22 +128,28 @@ public class TaskList extends Node { } } } - - public void setContentByLocalJSON(JSONObject js) { + + public void setContentByLocalJSON(JSONObject js) { if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) { Log.w(TAG, "setContentByLocalJSON: nothing is avaiable"); } - + try { JSONObject folder = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); - - if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { + + if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { + // 获取文件夹的摘要 String name = folder.getString(NoteColumns.SNIPPET); + // 设置文件夹的名称 setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + name); } else if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) { + // 判断文件夹的ID是否为根文件夹 if (folder.getLong(NoteColumns.ID) == Notes.ID_ROOT_FOLDER) + // 设置文件夹的名称 setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT); + // 判断文件夹的ID是否为通话记录文件夹 else if (folder.getLong(NoteColumns.ID) == Notes.ID_CALL_RECORD_FOLDER) + // 设置文件夹的名称 setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE); else @@ -130,29 +158,35 @@ public class TaskList extends Node { Log.e(TAG, "error type"); } } catch (JSONException e) { - Log.e(TAG, e.toString()); + // public void error(String e) { + Log.e(TAG, e.toString()); e.printStackTrace(); } } - + public JSONObject getLocalJSONFromContent() { try { JSONObject js = new JSONObject(); JSONObject folder = new JSONObject(); - + + // 获取文件夹名 String folderName = getName(); + // 如果文件夹名以MIUI_FOLDER_PREFFIX开头,则截取文件夹名 if (getName().startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX)) folderName = folderName.substring(GTaskStringUtils.MIUI_FOLDER_PREFFIX.length(), folderName.length()); + // 将文件夹名放入folder中 folder.put(NoteColumns.SNIPPET, folderName); + // 如果文件夹名等于FOLDER_DEFAULT或者FOLDER_CALL_NOTE,则将文件夹类型设置为Notes.TYPE_SYSTEM if (folderName.equals(GTaskStringUtils.FOLDER_DEFAULT) || folderName.equals(GTaskStringUtils.FOLDER_CALL_NOTE)) folder.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + // 否则将文件夹类型设置为Notes.TYPE_FOLDER else folder.put(NoteColumns.TYPE, Notes.TYPE_FOLDER); - + + // 将文件夹放入js中 js.put(GTaskStringUtils.META_HEAD_NOTE, folder); - return js; } catch (JSONException e) { Log.e(TAG, e.toString()); @@ -160,20 +194,24 @@ public class TaskList extends Node { return null; } } - + public int getSyncAction(Cursor c) { try { - if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { + // 判断本地修改列是否为0 + if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { // there is no local update + // 判断远程更新列是否等于最后修改时间 if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { // no update both side + // 没有本地更新和远程更新,返回无操作 return SYNC_ACTION_NONE; } else { // apply remote to local + // 将远程更新应用到本地 return SYNC_ACTION_UPDATE_LOCAL; } } else { - // validate gtask id + // validate gtask id if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { Log.e(TAG, "gtask id doesn't match"); return SYNC_ACTION_ERROR; @@ -188,89 +226,79 @@ public class TaskList extends Node { } } catch (Exception e) { Log.e(TAG, e.toString()); - e.printStackTrace(); + e.printStackTrace(); } - + + // 返回同步操作错误 return SYNC_ACTION_ERROR; } - - /** - * @return - * 功能:获得TaskList的大小,即mChildren的大小 - */ + + // 获取子任务的数量 public int getChildTaskCount() { return mChildren.size(); } - - /** - * @param task - * @return 返回值为是否成功添加任务。 - * 功能:在当前任务表末尾添加新的任务。 - */ + + // 添加子任务 public boolean addChildTask(Task task) { boolean ret = false; if (task != null && !mChildren.contains(task)) { ret = mChildren.add(task); if (ret) { // need to set prior sibling and parent + // 设置前一个兄弟和父任务 task.setPriorSibling(mChildren.isEmpty() ? null : mChildren .get(mChildren.size() - 1)); task.setParent(this); - //注意:每一次ArrayList的变化都要紧跟相关Task中PriorSibling的更改 - //,接下来几个函数都有相关操作 } } return ret; } - - /** - * @param task - * @param index - * @return - * 功能:在当前任务表的指定位置添加新的任务。 - */ - public boolean addChildTask(Task task, int index) { + + //添加中文注释后 +public boolean addChildTask(Task task, int index) { + //检查索引是否有效 if (index < 0 || index > mChildren.size()) { Log.e(TAG, "add child task: invalid index"); return false; } - + + //检查任务是否已经存在 int pos = mChildren.indexOf(task); if (task != null && pos == -1) { + //添加任务 mChildren.add(index, task); - + // update the task list - Task preTask = null; + Task preTask = null; Task afterTask = null; + // 判断当前节点是否是第一个节点 if (index != 0) preTask = mChildren.get(index - 1); + // 判断当前节点是否是最后一个节点 if (index != mChildren.size() - 1) afterTask = mChildren.get(index + 1); - + + // 设置当前节点的前一个节点 task.setPriorSibling(preTask); + // 设置当前节点的后一个节点 if (afterTask != null) afterTask.setPriorSibling(task); } - + return true; } - - /** - * @param task - * @return 返回删除是否成功 - * 功能:删除TaskList中的一个Task - */ - public boolean removeChildTask(Task task) { + + public boolean removeChildTask(Task task) { boolean ret = false; int index = mChildren.indexOf(task); if (index != -1) { ret = mChildren.remove(task); - + if (ret) { // reset prior sibling and parent task.setPriorSibling(null); task.setParent(null); - + // update the task list if (index != mChildren.size()) { mChildren.get(index).setPriorSibling( @@ -280,37 +308,27 @@ public class TaskList extends Node { } return ret; } - - /** - * @param task - * @param index - * @return - * 功能:将当前TaskList中含有的某个Task移到index位置 - */ public boolean moveChildTask(Task task, int index) { - + + //检查index是否越界 if (index < 0 || index >= mChildren.size()) { Log.e(TAG, "move child task: invalid index"); return false; } - + + //检查task是否在mChildren中 int pos = mChildren.indexOf(task); if (pos == -1) { Log.e(TAG, "move child task: the task should in the list"); return false; } - + + //如果task的索引和index相等,则不需要移动 if (pos == index) return true; + //从mChildren中移除task,并将其添加到指定位置 return (removeChildTask(task) && addChildTask(task, index)); - //利用已实现好的功能完成当下功能; } - - /** - * @param gid - * @return返回寻找结果 - * 功能:按gid寻找Task - */ public Task findChildTaskByGid(String gid) { for (int i = 0; i < mChildren.size(); i++) { Task t = mChildren.get(i); @@ -320,21 +338,13 @@ public class TaskList extends Node { } return null; } - - /** - * @param task - * @return - * 功能:返回指定Task的index - */ - public int getChildTaskIndex(Task task) { + + //获取子任务索引 +public int getChildTaskIndex(Task task) { return mChildren.indexOf(task); } - - /** - * @param index - * @return - * 功能:返回指定index的Task - */ + + //根据索引获取子任务 public Task getChildTaskByIndex(int index) { if (index < 0 || index >= mChildren.size()) { Log.e(TAG, "getTaskByIndex: invalid index"); @@ -342,28 +352,26 @@ public class TaskList extends Node { } return mChildren.get(index); } - - /** - * @param gid - * @return - * 功能:返回指定gid的Task - */ + + //根据GID获取子任务 public Task getChilTaskByGid(String gid) { - for (Task task : mChildren) {//一种常见的ArrayList的遍历方法(四种,见精读笔记) + for (Task task : mChildren) { if (task.getGid().equals(gid)) return task; } return null; } - + public ArrayList getChildTaskList() { return this.mChildren; - } - + } + +//设置索引 public void setIndex(int index) { this.mIndex = index; } - + +//获取索引 public int getIndex() { return this.mIndex; }