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.

105 lines
3.5 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;
public class MetaData extends Task {
// 类标识常量,用于日志记录
private final static String TAG = MetaData.class.getSimpleName();
// 存储关联的GTask ID
private String mRelatedGid = null;
// 设置元数据将gid存入JSON对象并作为Task的notes内容
public void setMeta(String gid, JSONObject metaInfo) {
try {
// 在JSON元信息中添加GTASK_ID字段
metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);
} catch (JSONException e) {
// JSON操作异常处理
Log.e(TAG, "failed to put related gid");
}
// 将JSON对象转为字符串保存到Task的notes属性
setNotes(metaInfo.toString());
// 设置任务名称为预定义的元数据标识
setName(GTaskStringUtils.META_NOTE_NAME);
}
// 获取关联的GTask ID
public String getRelatedGid() {
return mRelatedGid;
}
// 判断是否需要保存仅当notes非空时需保存
@Override
public boolean isWorthSaving() {
return getNotes() != null;
}
// 从远程JSON解析数据服务端→本地
@Override
public void setContentByRemoteJSON(JSONObject js) {
// 先调用父类解析基础字段
super.setContentByRemoteJSON(js);
// 解析notes中的关联ID
if (getNotes() != null) {
try {
// 将notes内容转为JSON对象
JSONObject metaInfo = new JSONObject(getNotes().trim());
// 提取META_HEAD_GTASK_ID字段值
mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);
} catch (JSONException e) {
// 解析失败时清空关联ID
Log.w(TAG, "failed to get related gid");
mRelatedGid = null;
}
}
}
// 以下方法禁止调用 //
// 本地数据解析方法(本不应被调用)
@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");
}
}