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.
rass/gtask/data/MetaData.java

98 lines
4.4 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类从命名来看可能是用于处理任务相关的元数据信息
public class MetaData extends Task {
// 用于日志输出的标签,取类的简单名称,方便在日志中识别该类相关的日志信息
private final static String TAG = MetaData.class.getSimpleName();
// 用于存储相关的Gid可能是某种任务的唯一标识符之类的具体要看业务逻辑初始化为null
private String mRelatedGid = null;
// 设置元数据的方法接收一个任务的gid和包含元数据信息的JSONObject对象
public void setMeta(String gid, JSONObject metaInfo) {
try {
// 尝试将给定的gid放入到metaInfo这个JSONObject中键由GTaskStringUtils.META_HEAD_GTASK_ID指定
metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);
} catch (JSONException e) {
// 如果在放入gid到JSONObject时出现JSON解析异常则记录错误日志
Log.e(TAG, "failed to put related gid");
}
// 将更新后的metaInfo转换为字符串后设置为该对象的笔记内容可能在Task类中有对应的处理逻辑
setNotes(metaInfo.toString());
// 设置该对象的名称名称由GTaskStringUtils.META_NOTE_NAME指定具体值需看对应的工具类
setName(GTaskStringUtils.META_NOTE_NAME);
}
// 获取相关的Gid的方法
public String getRelatedGid() {
return mRelatedGid;
}
// 重写父类Task类的方法用于判断该元数据对象是否值得保存判断依据是笔记内容是否为null
@Override
public boolean isWorthSaving() {
return getNotes()!= null;
}
// 重写父类Task类的方法根据远程的JSON对象来设置该对象的内容同时尝试从设置后的笔记内容中提取相关的Gid
@Override
public void setContentByRemoteJSON(JSONObject js) {
super.setContentByRemoteJSON(js);
if (getNotes()!= null) {
try {
// 将笔记内容字符串转换为JSONObject对象以便提取其中的信息
JSONObject metaInfo = new JSONObject(getNotes().trim());
// 从metaInfo中获取相关的Gid并赋值给成员变量mRelatedGid
mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);
} catch (JSONException e) {
// 如果在获取Gid时出现JSON解析异常则记录警告日志并将mRelatedGid设置为null
Log.w(TAG, "failed to get related gid");
mRelatedGid = null;
}
}
}
// 重写父类Task类的方法这里明确表示该方法不应该被调用如果调用会抛出非法访问错误
@Override
public void setContentByLocalJSON(JSONObject js) {
// 抛出非法访问错误,并给出相应提示说明该方法不应被调用
throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
}
// 重写父类Task类的方法同样明确表示该方法不应该被调用如果调用会抛出非法访问错误
@Override
public JSONObject getLocalJSONFromContent() {
throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
}
// 重写父类Task类的方法也明确表示该方法不应该被调用如果调用会抛出非法访问错误
@Override
public int getSyncAction(Cursor c) {
throw new IllegalAccessError("MetaData:getSyncAction should not be called");
}
}