You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xiaomi_notes_reading/src/notes/gtask/data/MetaData.java

118 lines
4.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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");
}
}