diff --git a/src/MetaData.java b/src/MetaData.java new file mode 100644 index 0000000..8a56662 --- /dev/null +++ b/src/MetaData.java @@ -0,0 +1,90 @@ +/* + * 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.tool.GTaskStringUtils; + +import org.json.JSONException; +import org.json.JSONObject; + +// MetaData类继承自Task类,负责处理任务的元数据 +public class MetaData extends Task { + private final static String TAG = MetaData.class.getSimpleName(); // 用于日志记录的TAG + + private String mRelatedGid = null; // 存储相关任务的ID + + // 设置元数据,包括任务ID和相关的元信息 + public void setMeta(String gid, JSONObject metaInfo) { + try { + // 将任务ID放入元信息JSON对象中 + metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid); + } catch (JSONException e) { + Log.e(TAG, "failed to put related gid"); // 记录错误 + } + setNotes(metaInfo.toString()); // 设置笔记内容为元信息的字符串表示 + setName(GTaskStringUtils.META_NOTE_NAME); // 设置名称 + } + + // 获取相关任务的ID + public String getRelatedGid() { + return mRelatedGid; + } + + // 检查当前对象是否值得保存 + @Override + public boolean isWorthSaving() { + return getNotes() != null; // 如果笔记内容不为空,则值得保存 + } + + // 从远程JSON对象设置内容 + @Override + public void setContentByRemoteJSON(JSONObject js) { + super.setContentByRemoteJSON(js); // 调用父类方法 + if (getNotes() != null) { + try { + // 将笔记内容转换为JSON对象 + JSONObject metaInfo = new JSONObject(getNotes().trim()); + // 获取相关任务的ID + mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); + } catch (JSONException e) { + Log.w(TAG, "failed to get related gid"); // 记录警告 + mRelatedGid = null; // 设置为null以表示失败 + } + } + } + + // 此方法不应被调用,抛出异常 + @Override + public void setContentByLocalJSON(JSONObject js) { + throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called"); + } + + // 此方法不应被调用,抛出异常 + @Override + public JSONObject getLocalJSONFromContent() { + throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called"); + } + + // 此方法不应被调用,抛出异常 + @Override + public int getSyncAction(Cursor c) { + throw new IllegalAccessError("MetaData:getSyncAction should not be called"); + } +}