/* * 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;//包名声明:路径"net.micode.notes.gtask.data",文件中定义的类和接口都归属于这个包 import org.json.JSONException; //引入"org.json.JSONException"类,用于处理JSON格式数据的异常 import org.json.JSONObject; //表示和操作JSON对象 import android.database.Cursor;//导入Android平台的数据库Cursor类用于从数据库中检索数据记录并对结果进行管理和遍历。 import android.util.Log;//引入工具类Log来存放一些通用的、与业务逻辑无关的工具方法 import net.micode.notes.tool.GTaskStringUtils;//导入 GTaskStringUtils工具类 public class MetaData extends Task { private final static String TAG = MetaData.class.getSimpleName(); // 用于在日志中标识当前类的名称 private String mRelatedGid = null; public void setMeta(String gid, JSONObject metaInfo) { try { metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid); } catch (JSONException e) { // 如果出现 JSONException 异常,则会捕获并记录日志 Log.e(TAG, "failed to put related gid"); } // try-catch 块中,将键值对 (GTaskStringUtils.META_HEAD_GTASK_ID, gid) 添加到传入的 metaInfo // JSON 对象中。 setNotes(metaInfo.toString()); setName(GTaskStringUtils.META_NOTE_NAME); } public String getRelatedGid() { return mRelatedGid; } @Override // 是一个Java注解,用于告诉编译器该方法是重写父类或接口中的方法。 public boolean isWorthSaving() { return getNotes() != null; } @Override public void setContentByRemoteJSON(JSONObject js) { super.setContentByRemoteJSON(js); // 调用父类的方法,传入参数js if (getNotes() != null) { String notes = getNotes().trim(); // 获取notes并去除首尾空格 try { JSONObject metaInfo = new JSONObject(notes); // 转化为JSONObject对象 mRelatedGid = metaInfo.optString(GTaskStringUtils.META_HEAD_GTASK_ID, null); // 提取特征字段 } catch (JSONException e) { Log.w(TAG, "failed to get related gid", e); mRelatedGid = null; } } } // 以下三段表示不应该被调用,如果调用了,抛出了一个IllegalAccessError异常,并提供了错误消息 @Override public void setContentByLocalJSON(JSONObject js) { // this function should not be called 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"); } }