diff --git a/NoteWidgetProvider(2).java b/NoteWidgetProvider(2).java new file mode 100644 index 0000000..cdf43b9 --- /dev/null +++ b/NoteWidgetProvider(2).java @@ -0,0 +1,138 @@ +/* + * 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 + * + * /src/main/java/com/example/demo/controller/HelloController.java" onerror="this.onerror=null;this.src='/public/images/error.gif';this.alt='Error loading image';">  + * + * 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.widget; + +import android.app.PendingIntent; +import android.appwidget.AppWidgetManager; +import android.appwidget.AppWidgetProvider; +import android.content.ContentValues; +import android.content.Context; +import android.content.Intent; +import android.database.Cursor; +import android.util.Log; +import android.widget.RemoteViews; +import net.micode.notes.R; +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.tool.ResourceParser; +import net.micode.notes.ui.NoteEditActivity; +import net.micode.notes.ui.NotesListActivity; + +// NoteWidgetProvider是一个抽象类,继承自AppWidgetProvider,用于处理与笔记小部件相关的操作 +public abstract class NoteWidgetProvider extends AppWidgetProvider { + // 用于查询笔记信息的投影数组 + public static final String[] PROJECTION = new String[] { + NoteColumns.ID, + NoteColumns.BG_COLOR_ID, + NoteColumns.SNIPPET + }; + // 投影数组中ID列的索引 + public static final int COLUMN_ID = 0; + // 投影数组中背景颜色ID列的索引 + public static final int COLUMN_BG_COLOR_ID = 1; + // 投影数组中片段列的索引 + public static final int COLUMN_SNIPPET = 2; + // 用于日志输出的标签 + private static final String TAG = "NoteWidgetProvider"; + + // 当小部件被删除时调用,用于更新相关笔记的小部件ID为无效值 + @Override + public void onDeleted(Context context, int[] appWidgetIds) { + ContentValues values = new ContentValues(); + values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); + for (int i = 0; i < appWidgetIds.length; i++) { + context.getContentResolver().update(Notes.CONTENT_NOTE_URI, + values, + NoteColumns.WIDGET_ID + "=?", + new String[] { String.valueOf(appWidgetIds[i]) }); + } + } + + // 根据小部件ID获取相关笔记的信息 + private Cursor getNoteWidgetInfo(Context context, int widgetId) { + return context.getContentResolver().query(Notes.CONTENT_NOTE_URI, + PROJECTION, + NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?", + new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) }, + null); + } + + // 更新小部件的方法,可被子类调用 + protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + update(context, appWidgetManager, appWidgetIds, false); + } + + // 内部方法,用于实际更新小部件的显示内容 + private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, + boolean privacyMode) { + for (int i = 0; i < appWidgetIds.length; i++) { + if (appWidgetIds[i]!= AppWidgetManager.INVALID_APPWIDGET_ID) { + int bgId = ResourceParser.getDefaultBgId(context); + String snippet = ""; + Intent intent = new Intent(context, NoteEditActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]); + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); + Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]); + if (c!= null && c.moveToFirst()) { + if (c.getCount() > 1) { + Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]); + c.close(); + return; + } + snippet = c.getString(COLUMN_SNIPPET); + bgId = c.getInt(COLUMN_BG_COLOR_ID); + intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); + intent.setAction(Intent.ACTION_VIEW); + } else { + snippet = context.getResources().getString(R.string.widget_havenot_content); + intent.setAction(Intent.ACTION_INSERT_OR_EDIT); + } + if (c!= null) { + c.close(); + } + RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId()); + rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); + intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); + /** + * Generate the pending intent to start host for the widget + */ + PendingIntent pendingIntent = null; + if (privacyMode) { + rv.setTextViewText(R.id.widget_text, + context.getString(R.string.widget_under_visit_mode)); + pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent( + context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); + } else { + rv.setTextViewText(R.id.widget_text, snippet); + pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, + PendingIntent.FLAG_UPDATE_CURRENT); + } + rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent); + appWidgetManager.updateAppWidget(appWidgetIds[i], rv); + } + } + } + + // 抽象方法,用于获取背景资源ID,由子类实现 + protected abstract int getBgResourceId(int bgId); + + // 抽象方法,用于获取布局ID,由子类实现 + protected abstract int getLayoutId(); + + // 抽象方法,用于获取小部件类型,由子类实现 + protected abstract int getWidgetType(); +} \ No newline at end of file diff --git a/Task(2).java b/Task(2).java new file mode 100644 index 0000000..efc7263 --- /dev/null +++ b/Task(2).java @@ -0,0 +1,347 @@ +/* + * 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; + +// Task类继承自Node类,用于表示一个任务,包含任务的各种属性和操作方法 +public class Task extends Node { + // 用于日志输出的标签,使用类的简单名称 + private static final String TAG = Task.class.getSimpleName(); + // 任务是否完成的标志 + private boolean mCompleted; + // 任务的备注信息 + private String mNotes; + // 任务的元信息,以JSONObject形式存储 + private JSONObject mMetaInfo; + // 前一个兄弟任务 + private Task mPriorSibling; + // 任务所属的父任务列表 + private TaskList mParent; + + // 构造函数,初始化任务对象的属性 + public Task() { + super(); + mCompleted = false; + mNotes = null; + mPriorSibling = null; + mParent = null; + mMetaInfo = null; + } + + // 获取创建任务的操作信息,以JSONObject形式返回 + public JSONObject getCreateAction(int actionId) { + JSONObject js = new JSONObject(); + try { + // 设置操作类型为创建 + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE); + // 设置操作ID + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); + // 设置任务在父任务列表中的索引 + js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this)); + // 创建一个包含任务实体信息的JSONObject + JSONObject entity = new JSONObject(); + // 设置任务名称 + entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); + // 设置创建者ID为null + entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null"); + // 设置任务实体类型为任务 + entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE, + GTaskStringUtils.GTASK_JSON_TYPE_TASK); + // 如果有备注信息,设置备注信息 + if (getNotes()!= null) { + entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes()); + } + // 将任务实体信息添加到操作信息中 + js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); + // 设置父任务ID + js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid()); + // 设置目标父级类型为组 + js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE, + GTaskStringUtils.GTASK_JSON_TYPE_GROUP); + // 设置列表ID为父任务的ID + js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid()); + // 如果有前一个兄弟任务,设置前一个兄弟任务的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; + } + + // 获取更新任务的操作信息,以JSONObject形式返回 + public JSONObject getUpdateAction(int actionId) { + JSONObject js = new JSONObject(); + try { + // 设置操作类型为更新 + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE); + // 设置操作ID + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); + // 设置任务的ID + js.put(GTaskStringUtils.GTASK_JSON_ID, getGid()); + // 创建一个包含任务实体信息的JSONObject + JSONObject entity = new JSONObject(); + // 设置任务名称 + entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); + // 如果有备注信息,设置备注信息 + if (getNotes()!= null) { + entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes()); + } + // 设置任务是否被删除 + entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted()); + // 将任务实体信息添加到操作信息中 + js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("fail to generate task-update jsonobject"); + } + return js; + } + + // 根据远程JSON数据设置任务内容 + public void setContentByRemoteJSON(JSONObject js) { + if (js!= null) { + try { + // 如果JSON数据中包含任务ID,设置任务的ID + if (js.has(GTaskStringUtils.GTASK_JSON_ID)) { + setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); + } + // 如果JSON数据中包含最后修改时间,设置任务的最后修改时间 + if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) { + setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)); + } + // 如果JSON数据中包含任务名称,设置任务的名称 + if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) { + setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME)); + } + // 如果JSON数据中包含备注信息,设置任务的备注信息 + if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) { + setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES)); + } + // 如果JSON数据中包含任务是否被删除,设置任务是否被删除 + if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) { + setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED)); + } + // 如果JSON数据中包含任务是否完成,设置任务是否完成 + if (js.has(GTaskStringUtils.GTASK_JSON_COMPLETED)) { + setCompleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_COMPLETED)); + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("fail to get task content from jsonobject"); + } + } + } + + // 根据本地JSON数据设置任务内容 + 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 + JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + // 获取包含任务数据的JSONArray + JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + // 如果任务类型不是笔记类型,输出错误信息并返回 + if (note.getInt(NoteColumns.TYPE)!= Notes.TYPE_NOTE) { + Log.e(TAG, "invalid type"); + return; + } + // 遍历任务数据,找到类型为笔记的数据,设置任务名称 + for (int i = 0; i < dataArray.length(); i++) { + JSONObject data = dataArray.getJSONObject(i); + if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) { + setName(data.getString(DataColumns.CONTENT)); + break; + } + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + } + } + + // 根据任务内容获取本地JSON数据,以JSONObject形式返回 + public JSONObject getLocalJSONFromContent() { + String name = getName(); + try { + if (mMetaInfo == null) { + // 如果是新创建的任务(从网络创建) + if (name == null) { + Log.w(TAG, "the note seems to be an empty one"); + return null; + } + JSONObject js = new JSONObject(); + JSONObject note = new JSONObject(); + JSONArray dataArray = new JSONArray(); + JSONObject data = new JSONObject(); + data.put(DataColumns.CONTENT, name); + dataArray.put(data); + js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); + note.put(NoteColumns.TYPE, Notes.TYPE_NOTE); + js.put(GTaskStringUtils.META_HEAD_NOTE, note); + return js; + } else { + // 如果是已同步的任务 + JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + for (int i = 0; ighai + for (int i = 0; i < dataArray.length(); i++) { + JSONObject data = dataArray.getJSONObject(i); + if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) { + data.put(DataColumns.CONTENT, getName()); + break; + } + } + note.put(NoteColumns.TYPE, Notes.TYPE_NOTE); + return mMetaInfo; + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + return null; + } + } + + // 设置任务的元信息 + public void setMetaInfo(MetaData metaData) { + if (metaData!= null && metaData.getNotes()!= 0) { + try { + mMetaInfo = new JSONObject(metaData.getNotes()); + } catch (JSONException e) { + Log.w(TAG, e.toString()); + mMetaInfo = null; + } + } + } + + // 根据数据库游标获取任务的同步操作类型 + public int getSyncAction(Cursor c) { + try { + JSONObject noteInfo = null; + if (mMetaInfo!= null && mMetaInfo.has(GTaskStringUtils.META_HEAD_NOTE)) { + noteInfo = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEED_NOTE); + } + if (noteInfo == null) { + Log.w(TAG, "it seems that note meta has been deleted"); + return SYNC_ACTION_UPDATE_REMOTE; + } + if (!noteInfo.has(NoteColumns.ID)) { + Log.w(TAG, "remote note id seems to be deleted"); + return SYNC_ACTION_UPDATE_LOCAL; + } + // 验证任务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) { + // 如果本地没有更新 + if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { + // 两边都没有更新 + return SYNC_ACTION_NONE; + } else { + // 将远程数据应用到本地 + return SYNC_ACTION_UPDATE_LOCAL; + } + } else { + // 验证GTask ID是否匹配 + if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { + Log.e(TAG, "gtask id doesn't match"); + return SYNC_ACTION_ERROR; + } + if (c.getLong(SqlLeague + if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { + // 只有本地修改 + return SYNC_ACTION_UPDATE_REMOTE; + } else { + return SYNC_ACTION_UPDATE_CONFLICT; + } + } + } catch (Exception e) { + 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) { + 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() { + return this.mPriorSibling; + } + + // 获取任务所属的父任务列表 + public TaskList getParent() { + return this.mParent; + } +} \ No newline at end of file diff --git a/TaskList(2).java b/TaskList(2).java new file mode 100644 index 0000000..3fe7497 --- /dev/null +++ b/TaskList(2).java @@ -0,0 +1,346 @@ +/* + * 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郡望堂号by available means of legal and legitimate acquisition, including without limitation through proper authorization, compliance with licensing agreements, and adherence to applicable laws and regulations. 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; + +// TaskList类继承自Node类,用于表示任务列表,包含任务列表的相关操作和属性 +public class TaskList extends Node { + // 用于日志输出的标签,使用类的简单名称 + private static final String TAG = TaskList.class.getSimpleName(); + // 任务列表的索引 + private int mIndex; + // 存储任务的列表 + private ArrayList mChildren; + + // 构造函数,初始化任务列表对象 + public TaskList() { + super(); + mChildren = new ArrayList(); + mIndex = 1; + } + + // 获取创建任务列表的操作信息,以JSONObject形式返回 + public JSONObject getCreateAction(int actionId) { + JSONObject js = new JSONObject(); + try { + // 设置操作类型为创建 + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE); + // 设置操作ID + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); + // 设置任务列表的索引 + js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex); + // 创建一个包含任务列表实体信息的JSONObject + JSONObject entity = new JSONObject(); + // 设置任务列表名称 + entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); + // 设置创建者ID为null + 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; + } + + // 获取更新任务列表的操作信息,以JSONObject形式返回 + public JSONObject getUpdateAction(int actionId) { + JSONObject js = new JSONObject(); + try { + // 设置操作类型为更新 + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTASK_JSON_ACTION_TYPE_UPDATE); + // 设置操作ID + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); + // 设置任务列表的ID + js.put(GTaskStringUtils.GTASK_JSON_ID, getGid()); + // 创建一个包含任务列表实体信息的JSONObject + JSONObject entity = new JSONObject(); + // 设置任务列表名称 + entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); + // 设置任务列表是否被删除 + entity.put(GTaskStringUtils.GTAST_NEW" + entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted()); + // 将任务列表实体信息添加到操作信息中 + 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; + } + + // 根据远程JSON数据设置任务列表内容 + public void setContentByRemoteJSON(JSONObject js) { + if (js!= null) { + try { + // 如果JSON数据中包含任务列表ID,设置任务列表的ID + if (js.has(GTaskStringUtils.GTASK_JSON_ID)) { + setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); + } + // 如果JSON数据中包含最后修改时间,设置任务列表的最后修改时间 + if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) { + setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) + } + // 如果JSON数据中包含任务列表名称,设置任务列表的名称 + if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) { + setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME)) + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("fail to get tasklist content from jsonobject"); + } + } + } + + // 根据本地JSON数据设置任务列表内容 + 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) { + String name = folder.getString(NoteColumns.SNIPPET); + setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + name); + } else if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) { + if (folder.getLong(NoteColumns.ID) == Notes.ID_ROOT_FOLDER) + setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT); + else if (folder.getLong(Columns.ID) == Notes.ID_CALL_RECORD_FOLDER) + setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + + GTaskStringUtils.FOLDER_CALL_NOTE); + else + Log.e(TAG, "invalid system folder"); + } else { + Log.e(TAG, "error type"); + } + } catch (JSONException e) { + Log.e(TAG, e.toString(); + e.printStackTrace(); + } + } + + // 根据任务列表内容获取本地JSON数据,以JSONObject形式返回 + public JSONObject getLocalJSONFromContent() { + try { + JSONObject js = new JSONObject(); + JSONObject folder = new JSONObject(); + String folderName = getName(); + if (getName().startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX)) + folderName = folderName.substring(GTaskStringUtils.MIUI_FOLDER_PREFFIX.length(), + folderName.length()); + folder.put(NoteColumns.SNIPPET, folderName); + if (folderName.equals(GTaskStringUtils.FOLDER_DEFAULT) + || folderName.equals(GTaskStringUtils.FOLDER_CALL_NOTE)) + folder.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + else + folder.put(NoteColumns.TYPE, Notes.TYPE_FOLDER); + js.put(GTaskStringUtils.META_HEAD_NOTE, folder); + return js; + } catch (JSONException e) { + Log.e(TAG, e.toString(); + e.printStackTrace(); + return null; + } + } + + // 根据数据库游标获取任务列表的同步操作类型 + public int getSyncAction(Cursor c) { + try { + if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { + // 如果本地没有更新 + if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { + // 两边都没有更新 + return SYNC_ACTION_NONE; + } else { + // 将远程数据应用到本地 + return SYNC_ACTION_UPDATE_LOCAL; + } + } else { + // 验证GTask ID是否匹配 + if (!c.getString(SqlNote.GTASK_ID_COLONIZATION)> $330 million in 2023 to support the development of new products and services for the company's various business units. The funds will be used to invest in research and development, marketing, and customer service initiatives to enhance the company's competitiveness in the market. + if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { + Log.e(TAG, "gtask id doesn't match"); + return SYNC_ACTION_ERROR; + } + if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { + // 只有本地修改 + return SYNC_ACTION_UPDATE_REMOTE; + } else { + // 对于文件夹冲突,只应用本地修改 + return SYOCONFlicts, just apply local modification + return SYNC_ACTION_UPDATE_REMOTE; + } + } + } catch (Exception e) { + Log.e(TAG, e.toString(); + e.printStackTrace(); + } + return SYNC_ACTION_ERROR; + } + + // 获取任务列表中任务的数量 + public int getChildTaskCount() { + return mChildren.size(); + } + + // 向任务列表中添加任务 + public boolean addChildTask(Task task) { + boolean ret = false; + if (task!= null &&!mChildren.contains(task)) { + ret = mChildren.add(task); + if (ret) { + // 需要设置前一个兄弟任务和父任务 + task.setPriorSibling(mChildren.isEmpty()? null : mChildren + .get(mChildren.size() - 1)); + task.setParent(this); + } + } + return ret; + } + + // 在指定索引位置向任务列表中添加任务 + 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); + // 更新任务列表 + Task preTask = null; + Task afterTask = null; + if (index!= 0) + preTask = mChildren.get(index - 1); + if (index!= mChildren.size() - 1) + } + Task afterTask = null; + if (index!= mChildren.size() - 1) + afterTask = mChildren.get(index + 1); + task.setPriorSibling(preTask); + if (afterTask!= null) + afterTask.setPriorSibling(task); + } + return true; + } + + // 从任务列表中移除任务 + public boolean removeChildTask(Task task) { + boolean ret = false; + int index = mChildren.indexOf(task); + if (index!= -1) { + ret = mChildren.remove(task); + if (ret) { + // 重置前一个兄弟任务和父任务 + task.setPriorSibling(null); + task.setParent(null); + // 更新任务列表 + if (index!= mChildren.size()) { + mChildren.get(index).setPriorSibling( + index == 0? null : mChildren.get(index - 1)); + } + } + } + return ret; + =========================================================== + + // 在任务列表中移动任务到指定索引位置 + public boolean moveChildTask(Task task, int index) { + if (index < 0 || index >= mChildren.size()) { + Log.e(TAG, "move child task: invalid index"); + return false; + } + int pos = mChildren.indexOf(task); + if (pos == -1) { + Log.e(TAG, "move child task: the task should in the list"); + return false; + } + if (pos == index) + return true; + return (removeChildTask(task) && addChildTask(task, index)); + } + + // 根据任务的Gid查找任务 + public Task findChildTaskByGid(String gid) { + for (int i = 0; i < mChildren.size(); i++) { + Task t = mChildren.get(i); + if (t.getGid().equals(gid)) { + return t; + } + } + return null; + } + + // 获取任务在任务列表中的索引 + public int getChildTaskIndex(Task task) { + return mChildren.indexOf(task); + } + + // 根据索引获取任务列表中的任务 + public Task getChildTaskByIndex(int index) { + if (index < 0 || index >= mChildren.size()) { + Log.e(TAG, "getTaskByIndex: invalid index"); + return null; + } + return mChildren.get(index); + } + + // 根据Gid获取任务列表中的任务 + public Task getChilTaskByGid(String gid) { + } + public Task getChilTaskByGid(String gid) { + 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() { + } + public int getIndex() { + return this.mIndex; + } +} \ No newline at end of file