/* * 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;//声名一个包,包名包含多个层次,每个层次用'.'分割,这是data包,用倒置的域名开头 import android.database.Cursor;//引用android.database.Cursor类,基于数据库服务的类 import android.util.Log;//日志工具类,常用的方法有:Log.v() Log.d() Log.i() Log.w() Log.e() import net.micode.notes.tool.GTaskStringUtils;//引用tool包中的GTaskStringUtils工具类。 import org.json.JSONException;//引用JSON使用失败异常处理类 import org.json.JSONObject;//引用JSON对象库类,JsonObject是一种数据格式。其格式为{"key1":value1,"key2",value2....};key必须是字符串。 public class MetaData extends Task {//创建一个继承于Task的类MetaData,主要用于记录数据的变化 private final static String TAG = MetaData.class.getSimpleName();//调用getSimpleName()函数,得到类的简写名称存入字符串TAG中 private String mRelatedGid = null;//创建私有变量mRelatedGid,并初始化为null。 public void setMeta(String gid, JSONObject metaInfo) {//调用JSONObject库函数put (),Task类中的setNotes ()和setName ()函数,实现设置数据,即生成元数据库 try {//执行 metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);//将这对键值放入metaInfo这个jsonobject对象中 } catch (JSONException e) {//捕捉异常并进行异常处理放入TAG Log.e(TAG, "failed to put related gid");//输出错误信息,TAG为类名,即该类无法放入相关联gid信息 } setNotes(metaInfo.toString());//设置便签,将json类的metaInfo转为string setName(GTaskStringUtils.META_NOTE_NAME);//设置GTask的名字 } public String getRelatedGid() { return mRelatedGid;//获取相关联的Gid } @Override// @Override 注解是用来指定方法重写的 public boolean isWorthSaving() { return getNotes() != null; }//如果getNotes不等于null就返回真,如果便签内容非空,getNotes就不为null @Override//表示重写 public void setContentByRemoteJSON(JSONObject js) {//调用父类Task中的setContentByRemoteJSON ()函数,使用远程json数据对象设置元数据内容 super.setContentByRemoteJSON(js);//调用函数 if (getNotes() != null) {//如果数据非空 try {//执行 JSONObject metaInfo = new JSONObject(getNotes().trim());//创建新json对象,调用trim方法去掉首尾空格 mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);//获取关联gid,且为字符串类型 } catch (JSONException e) {//进行异常处理 Log.w(TAG, "failed to get related gid");//输出警告信息 mRelatedGid = null; } } } @Override//表示重写 public void setContentByLocalJSON(JSONObject js) {//使用本地json数据对象设置元数据内容,一般不会用到,若用到,则抛出异常 // this function should not be called throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");//抛出非法参数异常 } @Override//表示重写 public JSONObject getLocalJSONFromContent() {//从元数据内容中获取本地json对象 throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");//抛出非法参数异常 } @Override//表示重写 public int getSyncAction(Cursor c) {//获取同步动作状态 throw new IllegalAccessError("MetaData:getSyncAction should not be called");//抛出非法参数异常 } }