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.
minote_wd/MetaData.java

86 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.

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;
// MetaData类继承自Task类用于处理任务相关的元数据信息
public class MetaData extends Task {
// 定义一个私有静态常量TAG用于在日志输出时标识当前类其值为类的简单名称
private final static String TAG = MetaData.class.getSimpleName();
// 用于存储相关任务的标识符初始值设为null
private String mRelatedGid = null;
// 设置元数据的方法接收任务标识符gid和一个JSONObject类型的metaInfo对象
public void setMeta(String gid, JSONObject metaInfo) {
try {
// 尝试将任务标识符gid添加到metaInfo这个JSON对象中键由GTaskStringUtils.META_HEAD_GTASK_ID指定
metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);
} catch (JSONException e) {
// 如果添加过程中出现JSONException异常通过Log.e记录错误日志提示添加相关标识符失败
Log.e(TAG, "failed to put related gid");
}
// 将更新后的metaInfo对象转换为字符串并通过调用继承自Task类的setNotes方法进行设置
setNotes(metaInfo.toString());
// 设置名称为GTaskStringUtils.META_NOTE_NAME所代表的值该值应该是一个预定义的用于标识名称的字符串常量
setName(GTaskStringUtils.META_NOTE_NAME);
}
// 获取相关任务标识符的方法返回mRelatedGid的值
public String getRelatedGid() {
return mRelatedGid;
}
// 重写isWorthSaving方法判断当前MetaData对象是否值得保存
// 逻辑是只要通过getNotes方法推测来自Task类用于获取备注信息相关内容获取到的值不为null就返回true
@Override
public boolean isWorthSaving() {
return getNotes()!= null;
}
// 重写用于根据远程获取的JSON对象来设置内容的方法
@Override
public void setContentByRemoteJSON(JSONObject js) {
// 首先调用父类Task类的setContentByRemoteJSON方法进行一些通用的或者继承体系中已有的设置操作
super.setContentByRemoteJSON(js);
if (getNotes()!= null) {
try {
// 对通过getNotes获取到的内容先进行trim操作去除首尾空白字符然后解析为JSONObject
JSONObject metaInfo = new JSONObject(getNotes().trim());
// 从解析出的JSONObject中获取以GTaskStringUtils.META_HEAD_GTASK_ID为键对应的字符串值并赋值给mRelatedGid
mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);
} catch (JSONException e) {
// 如果在获取过程中出现JSONException异常通过Log.w记录警告日志并将mRelatedGid设为null
Log.w(TAG, "failed to get related gid");
mRelatedGid = null;
}
}
}
// 重写用于根据本地JSON对象设置内容的方法此方法在当前类的设计中不应该被调用
// 直接抛出IllegalAccessError异常并给出相应提示信息
@Override
public void setContentByLocalJSON(JSONObject js) {
// this function should not be called
throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
}
// 重写获取本地JSON对象的方法此方法在当前类的设计中不应该被调用
// 直接抛出IllegalAccessError异常并给出相应提示信息
@Override
public JSONObject getLocalJSONFromContent() {
throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
}
// 重写获取同步操作相关的方法,此方法在当前类的设计中不应该被调用
// 直接抛出IllegalAccessError异常并给出相应提示信息
@Override
public int getSyncAction(Cursor c) {
throw new IllegalAccessError("MetaData:getSyncAction should not be called");
}
}