@ -0,0 +1,185 @@
|
|||||||
|
/*
|
||||||
|
* 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.cloud;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import net.micode.notes.account.AccountManager;
|
||||||
|
import net.micode.notes.tool.NoteSyncUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SyncManager类 - 同步协调器
|
||||||
|
*
|
||||||
|
* 负责协调上传下载逻辑
|
||||||
|
* 管理同步状态
|
||||||
|
* 处理同步冲突
|
||||||
|
* 提供同步触发方法
|
||||||
|
*/
|
||||||
|
public class SyncManager {
|
||||||
|
private static final String TAG = "SyncManager";
|
||||||
|
private static SyncManager sInstance;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
private OSSManager mOssManager;
|
||||||
|
private boolean mIsSyncing;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取SyncManager单例
|
||||||
|
* @param context 上下文对象
|
||||||
|
* @return SyncManager实例
|
||||||
|
*/
|
||||||
|
public static synchronized SyncManager getInstance(Context context) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new SyncManager(context.getApplicationContext());
|
||||||
|
}
|
||||||
|
return sInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造方法
|
||||||
|
* @param context 上下文对象
|
||||||
|
*/
|
||||||
|
private SyncManager(Context context) {
|
||||||
|
mContext = context;
|
||||||
|
mOssManager = new OSSManager(context);
|
||||||
|
mIsSyncing = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否正在同步
|
||||||
|
* @return 是否正在同步
|
||||||
|
*/
|
||||||
|
public boolean isSyncing() {
|
||||||
|
return mIsSyncing;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置同步状态
|
||||||
|
* @param syncing 是否正在同步
|
||||||
|
*/
|
||||||
|
public void setSyncing(boolean syncing) {
|
||||||
|
mIsSyncing = syncing;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 触发同步操作
|
||||||
|
* @param callback 同步回调
|
||||||
|
*/
|
||||||
|
public void sync(SyncCallback callback) {
|
||||||
|
if (mIsSyncing) {
|
||||||
|
Log.d(TAG, "Sync already in progress");
|
||||||
|
if (callback != null) {
|
||||||
|
callback.onSyncFailed("同步已在进行中");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查用户登录状态
|
||||||
|
if (!AccountManager.isUserLoggedIn(mContext)) {
|
||||||
|
Log.w(TAG, "User not logged in");
|
||||||
|
if (callback != null) {
|
||||||
|
callback.onSyncFailed("请先登录后再同步");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String username = AccountManager.getCurrentUser(mContext);
|
||||||
|
if (username.isEmpty()) {
|
||||||
|
Log.w(TAG, "Empty username");
|
||||||
|
if (callback != null) {
|
||||||
|
callback.onSyncFailed("用户名为空");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 执行同步任务
|
||||||
|
new SyncTask(mContext, this, username, callback).execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传笔记到云端
|
||||||
|
* @param username 用户名
|
||||||
|
* @return 是否上传成功
|
||||||
|
*/
|
||||||
|
public boolean uploadNotes(String username) {
|
||||||
|
try {
|
||||||
|
// 获取本地所有笔记数据
|
||||||
|
String jsonContent = NoteSyncUtils.localNotesToJson(mContext);
|
||||||
|
if (jsonContent == null) {
|
||||||
|
Log.e(TAG, "Failed to convert local notes to JSON");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 上传到OSS
|
||||||
|
String filePath = mOssManager.getFilePath(username);
|
||||||
|
boolean success = mOssManager.uploadFile(filePath, jsonContent);
|
||||||
|
Log.d(TAG, "Upload notes result: " + success);
|
||||||
|
return success;
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Failed to upload notes", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从云端下载笔记
|
||||||
|
* @param username 用户名
|
||||||
|
* @return 是否下载成功
|
||||||
|
*/
|
||||||
|
public boolean downloadNotes(String username) {
|
||||||
|
try {
|
||||||
|
// 从OSS下载
|
||||||
|
String filePath = mOssManager.getFilePath(username);
|
||||||
|
String jsonContent = mOssManager.downloadFile(filePath);
|
||||||
|
|
||||||
|
if (jsonContent == null) {
|
||||||
|
Log.e(TAG, "Failed to download notes from OSS");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析并合并数据
|
||||||
|
boolean success = NoteSyncUtils.jsonToLocalNotes(mContext, jsonContent);
|
||||||
|
Log.d(TAG, "Download notes result: " + success);
|
||||||
|
return success;
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Failed to download notes", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步回调接口
|
||||||
|
*/
|
||||||
|
public interface SyncCallback {
|
||||||
|
/**
|
||||||
|
* 同步开始
|
||||||
|
*/
|
||||||
|
void onSyncStart();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步成功
|
||||||
|
*/
|
||||||
|
void onSyncSuccess();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步失败
|
||||||
|
* @param errorMessage 错误信息
|
||||||
|
*/
|
||||||
|
void onSyncFailed(String errorMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:height="24dp"
|
||||||
|
android:tint="#309760"
|
||||||
|
android:viewportHeight="24"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:width="24dp">
|
||||||
|
<path
|
||||||
|
android:fillColor="@android:color/white"
|
||||||
|
android:pathData="M16,12V4H17V2H7V4H8V12L6,14V16H8V20H16V16H18V14L16,12Z"/>
|
||||||
|
</vector>
|
||||||
Loading…
Reference in new issue