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

85 lines
3.0 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类用于处理与Google Tasks相关的元数据
public class MetaData extends Task {
// 类的标签,用于日志记录
private final static String TAG = MetaData.class.getSimpleName();
// 用于存储与Google Tasks相关的全局ID
private String mRelatedGid = null;
// 设置元数据信息
public void setMeta(String gid, JSONObject metaInfo) {
try {
// 将全局ID添加到元数据信息中
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);
}
// 获取与Google Tasks相关的全局ID
public String getRelatedGid() {
return mRelatedGid;
}
// 重写isWorthSaving方法判断是否有值得保存的笔记内容
@Override
public boolean isWorthSaving() {
return getNotes() != null;
}
// 根据远程JSON对象设置内容
@Override
public void setContentByRemoteJSON(JSONObject js) {
// 首先调用父类的同名方法
super.setContentByRemoteJSON(js);
// 如果笔记内容不为空尝试解析JSON对象
if (getNotes() != null) {
try {
JSONObject metaInfo = new JSONObject(getNotes().trim());
// 从JSON对象中获取全局ID
mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);
} catch (JSONException e) {
// 如果解析失败记录警告日志并将全局ID设置为null
Log.w(TAG, "failed to get related gid");
mRelatedGid = null;
}
}
}
// 根据本地JSON对象设置内容这个方法不应该被调用
@Override
public void setContentByLocalJSON(JSONObject js) {
// 抛出异常,表示这个方法不应该被调用
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");
}
}