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.
git-test/src/main/java/net/micode/notes/gtask/data/MetaData.java

158 lines
5.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;
/**
* MetaData类是Task的子类用于存储与Google Tasks同步相关的元数据信息。
* <p>
* 该类主要用于在GTask同步过程中存储任务的关联信息将元数据以JSON格式存储在
* Task的notes字段中并提供了相应的解析和访问方法。
* </p>
*/
public class MetaData extends Task {
private final static String TAG = MetaData.class.getSimpleName();
/**
* 与当前元数据相关联的Google Task ID
*/
private String mRelatedGid = null;
/**
* 设置元数据信息
* <p>
* 将Google Task ID添加到元数据JSON对象中并将其存储在Task的notes字段中。
* 同时设置任务名称为元数据标识名称。
* </p>
*
* @param gid 与元数据关联的Google Task ID
* @param metaInfo 包含元数据信息的JSON对象
*/
public void setMeta(String gid, JSONObject metaInfo) {
try {
// 将关联的Google Task ID添加到元数据中
metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);
} catch (JSONException e) {
Log.e(TAG, "failed to put related gid");
}
// 将元数据JSON对象转换为字符串并存储在notes字段中
setNotes(metaInfo.toString());
// 设置任务名称为元数据标识名称
setName(GTaskStringUtils.META_NOTE_NAME);
}
/**
* 获取与当前元数据相关联的Google Task ID
*
* @return 关联的Google Task ID如果没有则返回null
*/
public String getRelatedGid() {
return mRelatedGid;
}
/**
* 判断当前元数据是否值得保存
* <p>
* 只有当notes字段不为null时元数据才值得保存
* </p>
*
* @return 如果notes字段不为null则返回true否则返回false
*/
@Override
public boolean isWorthSaving() {
return getNotes() != null;
}
/**
* 从远程JSON数据设置内容
* <p>
* 从远程获取的JSON数据中解析元信息并提取关联的Google Task ID
* </p>
*
* @param js 包含远程数据的JSON对象
*/
@Override
public void setContentByRemoteJSON(JSONObject js) {
// 调用父类方法设置基本内容
super.setContentByRemoteJSON(js);
// 如果notes字段不为null则解析元数据
if (getNotes() != null) {
try {
// 解析notes字段中的JSON元数据
JSONObject metaInfo = new JSONObject(getNotes().trim());
// 提取关联的Google Task ID
mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);
} catch (JSONException e) {
Log.w(TAG, "failed to get related gid");
mRelatedGid = null;
}
}
}
/**
* 从本地JSON数据设置内容不支持
* <p>
* 该方法不应该被调用因为MetaData主要用于处理远程同步数据
* </p>
*
* @param js 本地JSON对象
* @throws IllegalAccessError 总是抛出此异常,表示该方法不应该被调用
*/
@Override
public void setContentByLocalJSON(JSONObject js) {
// 此方法不应该被调用
throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
}
/**
* 从内容获取本地JSON数据不支持
* <p>
* 该方法不应该被调用因为MetaData主要用于处理远程同步数据
* </p>
*
* @return 本地JSON对象
* @throws IllegalAccessError 总是抛出此异常,表示该方法不应该被调用
*/
@Override
public JSONObject getLocalJSONFromContent() {
throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
}
/**
* 获取同步操作(不支持)
* <p>
* 该方法不应该被调用因为MetaData主要用于处理远程同步数据
* </p>
*
* @param c 本地数据库游标
* @return 同步操作类型
* @throws IllegalAccessError 总是抛出此异常,表示该方法不应该被调用
*/
@Override
public int getSyncAction(Cursor c) {
throw new IllegalAccessError("MetaData:getSyncAction should not be called");
}
}