/* * 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. */ java // 导入所需的包和类 package net.micode.notes.gtask.data; import android.database.Cursor; // 用于数据库查询结果的游标 import android.util.Log; // 用于记录日志 import net.micode.notes.tool.GTaskStringUtils; // 自定义的工具类,可能包含一些字符串处理的静态方法 import org.json.JSONException; // JSON处理时可能抛出的异常 import org.json.JSONObject; // JSON对象类 // MetaData类,继承自Task类 public class MetaData extends Task { // 定义日志标签,用于记录日志时区分来源 private final static String TAG = MetaData.class.getSimpleName(); // 定义一个私有成员变量,用于存储与元数据相关的GID private String mRelatedGid = null; // 设置元数据的方法,接收GID和包含元数据的JSONObject public void setMeta(String gid, JSONObject metaInfo) { try { // 将GID添加到metaInfo中,作为元数据的一部分 metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid); } catch (JSONException e) { // 如果添加GID时发生JSON异常,则记录错误日志 Log.e(TAG, "failed to put related gid"); } // 将metaInfo转换为字符串,并设置为任务的笔记内容 setNotes(metaInfo.toString()); // 设置任务的名称为一个特定的字符串,表示这是一个元数据任务 setName(GTaskStringUtils.META_NOTE_NAME); } // 获取与元数据相关的GID的方法 public String getRelatedGid() { return mRelatedGid; } // 重写isWorthSaving方法,判断该元数据是否值得保存 // 在这里,如果笔记内容不为空,则认为值得保存 @Override public boolean isWorthSaving() { return getNotes() != null; } // 重写setContentByRemoteJSON方法,用于从远程JSON对象设置内容 // 在这里,首先从远程JSON中恢复元数据,并尝试从中提取GID @Override public void setContentByRemoteJSON(JSONObject js) { super.setContentByRemoteJSON(js); // 调用父类的方法处理其他内容 if (getNotes() != null) { try { // 将笔记内容(JSON字符串)转换为JSONObject JSONObject metaInfo = new JSONObject(getNotes().trim()); // 从metaInfo中提取GID mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); } catch (JSONException e) { // 如果提取GID时发生JSON异常,则记录警告日志,并将GID设置为null Log.w(TAG, "failed to get related gid"); mRelatedGid = null; } } } // 重写setContentByLocalJSON方法,但在这里抛出异常,表示该方法不应被调用 // 因为元数据通常是从远程获取的,而不是从本地JSON设置的 @Override public void setContentByLocalJSON(JSONObject js) { throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called"); } // 重写getLocalJSONFromContent方法,但在这里抛出异常,表示该方法不应被调用 // 因为元数据通常不需要转换为本地JSON表示 @Override public JSONObject getLocalJSONFromContent() { throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called"); } // 重写getSyncAction方法,但在这里抛出异常,表示该方法不应被调用 // 因为元数据的同步逻辑可能与其他任务类型不同,或者根本不需要同步 @Override public int getSyncAction(Cursor c) { throw new IllegalAccessError("MetaData:getSyncAction should not be called"); } }