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.

144 lines
4.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.
*/
/**
* 文件: MetaData.java
* 描述: Google Tasks元数据处理类用于存储和管理便签与Google Tasks之间的映射关系
* 作用: 存储便签的元数据信息建立本地便签与Google Tasks任务的关联
*/
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 Task ID */
private String mRelatedGid = null;
/**
* 设置元数据信息
*
* @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字符串设置为任务的备注
setNotes(metaInfo.toString());
// 设置元数据任务的名称为固定值
setName(GTaskStringUtils.META_NOTE_NAME);
}
/**
* 获取关联的Google Task ID
*
* @return 关联的Google Task ID
*/
public String getRelatedGid() {
return mRelatedGid;
}
/**
* 判断元数据是否值得保存
*
* @return 如果备注不为空则返回true否则返回false
*/
@Override
public boolean isWorthSaving() {
return getNotes() != null;
}
/**
* 从远程JSON数据中设置内容
* 解析远程数据并提取关联的Google Task ID
*
* @param js 远程JSON数据
*/
@Override
public void setContentByRemoteJSON(JSONObject js) {
// 调用父类方法设置基本内容
super.setContentByRemoteJSON(js);
if (getNotes() != null) {
try {
// 解析备注中的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数据中设置内容
* 此方法不应被调用,因为元数据只从远程获取
*
* @param js 本地JSON数据
* @throws IllegalAccessError 如果调用此方法则抛出异常
*/
@Override
public void setContentByLocalJSON(JSONObject js) {
// 此方法不应被调用
throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
}
/**
* 从内容中获取本地JSON数据
* 此方法不应被调用,因为元数据只从远程获取
*
* @return JSONObject 本地JSON数据
* @throws IllegalAccessError 如果调用此方法则抛出异常
*/
@Override
public JSONObject getLocalJSONFromContent() {
throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
}
/**
* 获取同步操作类型
* 此方法不应被调用因为元数据的同步操作由GTaskManager管理
*
* @param c 数据库游标
* @return 同步操作类型
* @throws IllegalAccessError 如果调用此方法则抛出异常
*/
@Override
public int getSyncAction(Cursor c) {
throw new IllegalAccessError("MetaData:getSyncAction should not be called");
}
}