/* * 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; /** * gtask同步任务的元数据管理类 * 存储和管理笔记与gtask同步时的元数据信息 * MetaData是什么?描述便签属性的信息,比如创建时间、修改时间、便签ID等,而不是便签的正文内容。 * gid是什么?google为每个同步任务分配的唯一标识,分给远端,保证同步的一致性 * JSON是什么?一种数据封装格式,用于封装MetaData,可读性强,可被用户或其他应用读取/编辑 * * 继承Task类,专门用于存储同步元数据 */ public class MetaData extends Task { /** * MetaData.class拿到MetaData的class对象 * getSimpleName返回类名不含包名的简短形式,这里就是’MetaData‘ */ private final static String TAG = MetaData.class.getSimpleName(); //存储当前元数据关联的gid private String mRelatedGid = null; /** *设置元数据的核心方法 * 将关联的gtask ID封装到JSON对象中,并设置为Task的笔记内容和名称 * @param gid 相当于远端的身份证号 * @param metaInfo JSONObject对象,用来存储MetaData的JSON格式数据结构。 */ public void setMeta(String gid, JSONObject metaInfo) { try { 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); } /** * 获取当前元数据关联的gtask ID * @return 关联的远端gtask ID(返回null时,未关联) */ public String getRelatedGid() { return mRelatedGid; } //笔记内容不为空就保存 @Override public boolean isWorthSaving() { return getNotes() != null; } /** * 从远端gtask返回的JSON数据设置当前元数据内容 * @param js 远端gtask返回的JSON对象 */ @Override public void setContentByRemoteJSON(JSONObject js) { //调用父类方法,初始化task基础属性 super.setContentByRemoteJSON(js); if (getNotes() != null) { try { JSONObject metaInfo = new JSONObject(getNotes().trim()); mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); } catch (JSONException e) { Log.w(TAG, "failed to get related gid"); mRelatedGid = null; } } } /**不会将元数据单独保存为JSON文件并保存到本地, * 而是将MetaData和正文一起存储在本地的SQLite数据库, * 为了防止用户篡改信息,比如便签的创建时间,导致APP读取数据时出现异常 */ @Override public void setContentByLocalJSON(JSONObject js) { // this function should not be called throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called"); } //不支持生成本地JSON @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"); } }