|
|
@ -1,265 +1,388 @@
|
|
|
|
//// ====================== 同步时间显示控制 ======================
|
|
|
|
/*
|
|
|
|
设置最后一次同步时间的UI显示逻辑
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
if (GTaskSyncService.isSyncing()) { // 检测同步服务是否正在运行
|
|
|
|
*
|
|
|
|
lastSyncTimeView.setText(GTaskSyncService.getProgressString()); // 显示动态同步进度文本
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
lastSyncTimeView.setVisibility(View.VISIBLE); // 确保视图可见
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
} else { // 未处于同步状态时
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
long lastSyncTime = getLastSyncTime(this); // 从SharedPreferences获取存储的时间戳
|
|
|
|
*
|
|
|
|
if (lastSyncTime != 0) { // 存在有效同步记录
|
|
|
|
* 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.ui;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.accounts.Account;
|
|
|
|
|
|
|
|
import android.accounts.AccountManager;
|
|
|
|
|
|
|
|
import android.app.ActionBar;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
|
|
|
import android.preference.Preference;
|
|
|
|
|
|
|
|
import android.preference.Preference.OnPreferenceClickListener;
|
|
|
|
|
|
|
|
import android.preference.PreferenceActivity;
|
|
|
|
|
|
|
|
import android.preference.PreferenceCategory;
|
|
|
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
import android.text.format.DateFormat;
|
|
|
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
|
|
|
import android.view.Menu;
|
|
|
|
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
import android.widget.Button;
|
|
|
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
|
|
|
import net.micode.notes.gtask.remote.GTaskSyncService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
|
|
|
public static final String PREFERENCE_NAME = "notes_preferences";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String AUTHORITIES_FILTER_KEY = "authorities";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private PreferenceCategory mAccountCategory;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private GTaskReceiver mReceiver;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Account[] mOriAccounts;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mHasAddedAccount;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
protected void onCreate(Bundle icicle) {
|
|
|
|
|
|
|
|
super.onCreate(icicle);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* using the app icon for navigation */
|
|
|
|
|
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
addPreferencesFromResource(R.xml.preferences);
|
|
|
|
|
|
|
|
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY);
|
|
|
|
|
|
|
|
mReceiver = new GTaskReceiver();
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
|
|
|
|
filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME);
|
|
|
|
|
|
|
|
registerReceiver(mReceiver, filter);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mOriAccounts = null;
|
|
|
|
|
|
|
|
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null);
|
|
|
|
|
|
|
|
getListView().addHeaderView(header, null, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
protected void onResume() {
|
|
|
|
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// need to set sync account automatically if user has added a new
|
|
|
|
|
|
|
|
// account
|
|
|
|
|
|
|
|
if (mHasAddedAccount) {
|
|
|
|
|
|
|
|
Account[] accounts = getGoogleAccounts();
|
|
|
|
|
|
|
|
if (mOriAccounts != null && accounts.length > mOriAccounts.length) {
|
|
|
|
|
|
|
|
for (Account accountNew : accounts) {
|
|
|
|
|
|
|
|
boolean found = false;
|
|
|
|
|
|
|
|
for (Account accountOld : mOriAccounts) {
|
|
|
|
|
|
|
|
if (TextUtils.equals(accountOld.name, accountNew.name)) {
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
|
|
|
|
setSyncAccount(accountNew.name);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
refreshUI();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
|
|
|
if (mReceiver != null) {
|
|
|
|
|
|
|
|
unregisterReceiver(mReceiver);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void loadAccountPreference() {
|
|
|
|
|
|
|
|
mAccountCategory.removeAll();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Preference accountPref = new Preference(this);
|
|
|
|
|
|
|
|
final String defaultAccount = getSyncAccountName(this);
|
|
|
|
|
|
|
|
accountPref.setTitle(getString(R.string.preferences_account_title));
|
|
|
|
|
|
|
|
accountPref.setSummary(getString(R.string.preferences_account_summary));
|
|
|
|
|
|
|
|
accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
|
|
|
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
|
|
|
|
if (!GTaskSyncService.isSyncing()) {
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(defaultAccount)) {
|
|
|
|
|
|
|
|
// the first time to set account
|
|
|
|
|
|
|
|
showSelectAccountAlertDialog();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// if the account has already been set, we need to promp
|
|
|
|
|
|
|
|
// user about the risk
|
|
|
|
|
|
|
|
showChangeAccountConfirmAlertDialog();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Toast.makeText(NotesPreferenceActivity.this,
|
|
|
|
|
|
|
|
R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
|
|
|
|
|
|
|
|
.show();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mAccountCategory.addPreference(accountPref);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void loadSyncButton() {
|
|
|
|
|
|
|
|
Button syncButton = (Button) findViewById(R.id.preference_sync_button);
|
|
|
|
|
|
|
|
TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// set button state
|
|
|
|
|
|
|
|
if (GTaskSyncService.isSyncing()) {
|
|
|
|
|
|
|
|
syncButton.setText(getString(R.string.preferences_button_sync_cancel));
|
|
|
|
|
|
|
|
syncButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
|
|
|
GTaskSyncService.cancelSync(NotesPreferenceActivity.this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
syncButton.setText(getString(R.string.preferences_button_sync_immediately));
|
|
|
|
|
|
|
|
syncButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
|
|
|
GTaskSyncService.startSync(NotesPreferenceActivity.this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this)));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// set last sync time
|
|
|
|
|
|
|
|
if (GTaskSyncService.isSyncing()) {
|
|
|
|
|
|
|
|
lastSyncTimeView.setText(GTaskSyncService.getProgressString());
|
|
|
|
|
|
|
|
lastSyncTimeView.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
long lastSyncTime = getLastSyncTime(this);
|
|
|
|
|
|
|
|
if (lastSyncTime != 0) {
|
|
|
|
lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time,
|
|
|
|
lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time,
|
|
|
|
DateFormat.format(getString(R.string.preferences_last_sync_time_format),
|
|
|
|
DateFormat.format(getString(R.string.preferences_last_sync_time_format),
|
|
|
|
lastSyncTime))); // 格式化时间为"yyyy-MM-dd HH:mm"样式
|
|
|
|
lastSyncTime)));
|
|
|
|
lastSyncTimeView.setVisibility(View.VISIBLE); // 显示时间文本
|
|
|
|
lastSyncTimeView.setVisibility(View.VISIBLE);
|
|
|
|
} else { // 无同步记录
|
|
|
|
} else {
|
|
|
|
lastSyncTimeView.setVisibility(View.GONE); // 隐藏视图避免空白区域
|
|
|
|
lastSyncTimeView.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ====================== 界面刷新方法 ======================
|
|
|
|
|
|
|
|
private void refreshUI() {
|
|
|
|
private void refreshUI() {
|
|
|
|
loadAccountPreference(); // 更新顶部账户名称显示(调用内部方法)
|
|
|
|
loadAccountPreference();
|
|
|
|
loadSyncButton(); // 控制同步按钮的可用状态(如网络未连接时禁用)
|
|
|
|
loadSyncButton();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ====================== 账户选择对话框 ======================
|
|
|
|
|
|
|
|
private void showSelectAccountAlertDialog() {
|
|
|
|
private void showSelectAccountAlertDialog() {
|
|
|
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); // 创建对话框构建器
|
|
|
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
|
|
|
|
|
|
|
|
|
|
|
|
// 构建自定义标题布局
|
|
|
|
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
|
|
|
|
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null); // 加载布局文件
|
|
|
|
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
|
|
|
|
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title); // 获取标题TextView
|
|
|
|
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
|
|
|
|
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title)); // 设置主标题文本
|
|
|
|
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
|
|
|
|
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle); // 副标题视图
|
|
|
|
subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips));
|
|
|
|
subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips)); // 设置提示性文字
|
|
|
|
|
|
|
|
dialogBuilder.setCustomTitle(titleView); // 应用自定义标题布局
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dialogBuilder.setPositiveButton(null, null); // 移除默认确认按钮(因使用单选列表交互)
|
|
|
|
dialogBuilder.setCustomTitle(titleView);
|
|
|
|
|
|
|
|
dialogBuilder.setPositiveButton(null, null);
|
|
|
|
|
|
|
|
|
|
|
|
Account[] accounts = getGoogleAccounts(); // 通过AccountManager获取设备上所有Google账户
|
|
|
|
Account[] accounts = getGoogleAccounts();
|
|
|
|
String defAccount = getSyncAccountName(this); // 从SharedPreferences读取当前选中的账户名
|
|
|
|
String defAccount = getSyncAccountName(this);
|
|
|
|
|
|
|
|
|
|
|
|
mOriAccounts = accounts; // 类成员变量缓存账户列表(用于后续比较是否新增账户)
|
|
|
|
mOriAccounts = accounts;
|
|
|
|
mHasAddedAccount = false; // 标记位标识用户是否执行了添加账户操作
|
|
|
|
mHasAddedAccount = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (accounts.length > 0) { // 存在可用账户时构建单选列表
|
|
|
|
if (accounts.length > 0) {
|
|
|
|
CharSequence[] items = new CharSequence[accounts.length]; // 准备显示项数组
|
|
|
|
CharSequence[] items = new CharSequence[accounts.length];
|
|
|
|
final CharSequence[] itemMapping = items; // 用于内部类访问的final引用
|
|
|
|
final CharSequence[] itemMapping = items;
|
|
|
|
int checkedItem = -1; // 默认没有选中项(-1表示无选中)
|
|
|
|
int checkedItem = -1;
|
|
|
|
int index = 0; // 数组索引计数器
|
|
|
|
int index = 0;
|
|
|
|
for (Account account : accounts) { // 遍历所有Google账户
|
|
|
|
for (Account account : accounts) {
|
|
|
|
if (TextUtils.equals(account.name, defAccount)) { // 匹配当前选中账户
|
|
|
|
if (TextUtils.equals(account.name, defAccount)) {
|
|
|
|
checkedItem = index; // 记录选中项位置
|
|
|
|
checkedItem = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
items[index++] = account.name; // 将账户名存入显示项数组
|
|
|
|
items[index++] = account.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 设置单选列表项及选择监听
|
|
|
|
|
|
|
|
dialogBuilder.setSingleChoiceItems(items, checkedItem,
|
|
|
|
dialogBuilder.setSingleChoiceItems(items, checkedItem,
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
setSyncAccount(itemMapping[which].toString()); // 用户点击时更新选中账户
|
|
|
|
setSyncAccount(itemMapping[which].toString());
|
|
|
|
dialog.dismiss(); // 关闭对话框
|
|
|
|
dialog.dismiss();
|
|
|
|
refreshUI(); // 刷新界面显示新账户
|
|
|
|
refreshUI();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 添加"新建账户"入口
|
|
|
|
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null);
|
|
|
|
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null); // 加载布局
|
|
|
|
dialogBuilder.setView(addAccountView);
|
|
|
|
dialogBuilder.setView(addAccountView); // 将布局添加到对话框底部
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final AlertDialog dialog = dialogBuilder.show(); // 显示完整对话框
|
|
|
|
final AlertDialog dialog = dialogBuilder.show();
|
|
|
|
addAccountView.setOnClickListener(new View.OnClickListener() { // 处理添加账户点击事件
|
|
|
|
addAccountView.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
public void onClick(View v) {
|
|
|
|
mHasAddedAccount = true; // 标记已触发添加账户操作
|
|
|
|
mHasAddedAccount = true;
|
|
|
|
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS"); // 系统级添加账户Intent
|
|
|
|
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
|
|
|
|
intent.putExtra(AUTHORITIES_FILTER_KEY, new String[]{"gmail-ls"}); // 过滤只显示Gmail类账户
|
|
|
|
intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] {
|
|
|
|
startActivityForResult(intent, -1); // 启动系统界面(-1为临时请求码,建议改为常量)
|
|
|
|
"gmail-ls"
|
|
|
|
dialog.dismiss(); // 关闭当前对话框
|
|
|
|
});
|
|
|
|
|
|
|
|
startActivityForResult(intent, -1);
|
|
|
|
|
|
|
|
dialog.dismiss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ====================== 账户变更确认对话框 ======================
|
|
|
|
|
|
|
|
private void showChangeAccountConfirmAlertDialog() {
|
|
|
|
private void showChangeAccountConfirmAlertDialog() {
|
|
|
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); // 新建对话框构建器
|
|
|
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
|
|
|
|
|
|
|
|
|
|
|
|
// 复用标题布局,动态显示当前账户
|
|
|
|
|
|
|
|
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
|
|
|
|
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
|
|
|
|
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
|
|
|
|
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
|
|
|
|
titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
|
|
|
|
titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
|
|
|
|
getSyncAccountName(this))); // 动态插入当前账户名(例:"当前账户:user@gmail.com")
|
|
|
|
getSyncAccountName(this)));
|
|
|
|
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
|
|
|
|
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
|
|
|
|
subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg)); // 警告文本
|
|
|
|
subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg));
|
|
|
|
dialogBuilder.setCustomTitle(titleView); // 应用自定义标题
|
|
|
|
dialogBuilder.setCustomTitle(titleView);
|
|
|
|
|
|
|
|
|
|
|
|
// 操作选项列表(更换/移除/取消)
|
|
|
|
|
|
|
|
CharSequence[] menuItemArray = new CharSequence[] {
|
|
|
|
CharSequence[] menuItemArray = new CharSequence[] {
|
|
|
|
getString(R.string.preferences_menu_change_account), // 资源ID对应"更换账户"
|
|
|
|
getString(R.string.preferences_menu_change_account),
|
|
|
|
getString(R.string.preferences_menu_remove_account), // "移除账户"
|
|
|
|
getString(R.string.preferences_menu_remove_account),
|
|
|
|
getString(R.string.preferences_menu_cancel) // "取消"
|
|
|
|
getString(R.string.preferences_menu_cancel)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() {
|
|
|
|
dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
if (which == 0) { // 用户点击第一项(更换账户)
|
|
|
|
if (which == 0) {
|
|
|
|
showSelectAccountAlertDialog(); // 打开账户选择对话框
|
|
|
|
showSelectAccountAlertDialog();
|
|
|
|
} else if (which == 1) { // 点击第二项(移除账户)
|
|
|
|
} else if (which == 1) {
|
|
|
|
removeSyncAccount(); // 清空SharedPreferences中的账户存储
|
|
|
|
removeSyncAccount();
|
|
|
|
refreshUI(); // 更新界面为无账户状态
|
|
|
|
refreshUI();
|
|
|
|
} // 第三项"取消"无操作,自动关闭对话框
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
dialogBuilder.show(); // 显示最终对话框
|
|
|
|
dialogBuilder.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ====================== 账户管理工具方法 ======================
|
|
|
|
|
|
|
|
private Account[] getGoogleAccounts() {
|
|
|
|
private Account[] getGoogleAccounts() {
|
|
|
|
AccountManager accountManager = AccountManager.get(this); // 获取系统账户管理器实例
|
|
|
|
AccountManager accountManager = AccountManager.get(this);
|
|
|
|
return accountManager.getAccountsByType("com.google"); // 过滤出所有Google类型账户
|
|
|
|
return accountManager.getAccountsByType("com.google");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ====================== 核心账户设置方法 ======================
|
|
|
|
|
|
|
|
private void setSyncAccount(String account) {
|
|
|
|
private void setSyncAccount(String account) {
|
|
|
|
if (!getSyncAccountName(this).equals(account)) { // 仅在新旧账户不同时执行操作
|
|
|
|
if (!getSyncAccountName(this).equals(account)) {
|
|
|
|
// 持久化存储账户名到SharedPreferences
|
|
|
|
|
|
|
|
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = settings.edit(); // 获取编辑器
|
|
|
|
SharedPreferences.Editor editor = settings.edit();
|
|
|
|
if (account != null) {
|
|
|
|
if (account != null) {
|
|
|
|
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account); // 存储有效账户名
|
|
|
|
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); // 空值处理
|
|
|
|
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
editor.commit(); // 立即提交写入(注意:同步操作可能阻塞UI)
|
|
|
|
editor.commit();
|
|
|
|
|
|
|
|
|
|
|
|
setLastSyncTime(this, 0); // 重置最后同步时间戳为0(表示需要重新同步)
|
|
|
|
// clean up last sync time
|
|
|
|
|
|
|
|
setLastSyncTime(this, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// 启动后台线程清理旧账户关联数据
|
|
|
|
// clean up local gtask related info
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
public void run() {
|
|
|
|
ContentValues values = new ContentValues(); // 创建更新数据集
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(NoteColumns.GTASK_ID, ""); // 清空任务ID字段
|
|
|
|
values.put(NoteColumns.GTASK_ID, "");
|
|
|
|
values.put(NoteColumns.SYNC_ID, 0); // 重置同步ID为初始状态
|
|
|
|
values.put(NoteColumns.SYNC_ID, 0);
|
|
|
|
getContentResolver().update(
|
|
|
|
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
|
|
|
|
Notes.CONTENT_NOTE_URI, // 目标ContentProvider URI
|
|
|
|
}
|
|
|
|
values, // 更新内容
|
|
|
|
}).start();
|
|
|
|
null, // 无WHERE条件(更新所有记录)
|
|
|
|
|
|
|
|
null // 无参数
|
|
|
|
|
|
|
|
); // 执行批量更新操作
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}).start(); // 立即启动清理线程
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 显示操作成功的Toast提示
|
|
|
|
|
|
|
|
Toast.makeText(NotesPreferenceActivity.this,
|
|
|
|
Toast.makeText(NotesPreferenceActivity.this,
|
|
|
|
getString(R.string.preferences_toast_success_set_accout, account), // 动态插入账户名
|
|
|
|
getString(R.string.preferences_toast_success_set_accout, account),
|
|
|
|
Toast.LENGTH_SHORT).show(); // 短时显示提示
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void removeSyncAccount() {
|
|
|
|
private void removeSyncAccount() {
|
|
|
|
// 获取SharedPreferences实例(私有模式仅本应用可访问)
|
|
|
|
|
|
|
|
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = settings.edit(); // 获取编辑器对象
|
|
|
|
SharedPreferences.Editor editor = settings.edit();
|
|
|
|
|
|
|
|
|
|
|
|
// 条件移除账户名配置(避免删除不存在的键)
|
|
|
|
|
|
|
|
if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) {
|
|
|
|
if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) {
|
|
|
|
editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME); // 删除键值对
|
|
|
|
editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 条件移除最后同步时间戳
|
|
|
|
|
|
|
|
if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) {
|
|
|
|
if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) {
|
|
|
|
editor.remove(PREFERENCE_LAST_SYNC_TIME);
|
|
|
|
editor.remove(PREFERENCE_LAST_SYNC_TIME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
editor.commit(); // 同步提交修改(立即生效,适合关键配置操作)
|
|
|
|
editor.commit();
|
|
|
|
|
|
|
|
|
|
|
|
// 启动异步任务清理关联数据
|
|
|
|
// clean up local gtask related info
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
public void run() {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(NoteColumns.GTASK_ID, ""); // 重置任务ID为空字符串
|
|
|
|
values.put(NoteColumns.GTASK_ID, "");
|
|
|
|
values.put(NoteColumns.SYNC_ID, 0); // 重置同步标识为初始状态
|
|
|
|
values.put(NoteColumns.SYNC_ID, 0);
|
|
|
|
getContentResolver().update(
|
|
|
|
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
|
|
|
|
Notes.CONTENT_NOTE_URI, // 目标ContentProvider的URI
|
|
|
|
|
|
|
|
values, // 更新值集合
|
|
|
|
|
|
|
|
null, // 无WHERE条件(全表更新)
|
|
|
|
|
|
|
|
null // 无参数绑定
|
|
|
|
|
|
|
|
); // 执行批量数据清理
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).start(); // 立即启动后台线程
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ====================== 配置存取工具方法 ======================
|
|
|
|
|
|
|
|
// 获取当前绑定账户名(返回空字符串表示未设置)
|
|
|
|
|
|
|
|
public static String getSyncAccountName(Context context) {
|
|
|
|
public static String getSyncAccountName(Context context) {
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
PREFERENCE_NAME, // 配置文件名
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
Context.MODE_PRIVATE // 访问模式
|
|
|
|
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
|
|
|
|
);
|
|
|
|
|
|
|
|
return settings.getString(
|
|
|
|
|
|
|
|
PREFERENCE_SYNC_ACCOUNT_NAME, // 键名
|
|
|
|
|
|
|
|
"" // 默认值(空字符串)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新最后同步时间戳(单位:毫秒)
|
|
|
|
|
|
|
|
public static void setLastSyncTime(Context context, long time) {
|
|
|
|
public static void setLastSyncTime(Context context, long time) {
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
PREFERENCE_NAME,
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
Context.MODE_PRIVATE
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
SharedPreferences.Editor editor = settings.edit();
|
|
|
|
SharedPreferences.Editor editor = settings.edit();
|
|
|
|
editor.putLong(PREFERENCE_LAST_SYNC_TIME, time); // 写入长整型数值
|
|
|
|
editor.putLong(PREFERENCE_LAST_SYNC_TIME, time);
|
|
|
|
editor.commit(); // 同步提交保证时间戳立即更新
|
|
|
|
editor.commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取最后成功同步时间(返回0表示从未同步)
|
|
|
|
|
|
|
|
public static long getLastSyncTime(Context context) {
|
|
|
|
public static long getLastSyncTime(Context context) {
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
PREFERENCE_NAME,
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
Context.MODE_PRIVATE
|
|
|
|
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0);
|
|
|
|
);
|
|
|
|
|
|
|
|
return settings.getLong(
|
|
|
|
|
|
|
|
PREFERENCE_LAST_SYNC_TIME, // 键名
|
|
|
|
|
|
|
|
0L // 默认值(0)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ====================== 同步状态广播接收器 ======================
|
|
|
|
|
|
|
|
private class GTaskReceiver extends BroadcastReceiver {
|
|
|
|
private class GTaskReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
refreshUI(); // 强制刷新界面显示最新状态
|
|
|
|
refreshUI();
|
|
|
|
|
|
|
|
if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) {
|
|
|
|
// 检测广播是否携带同步状态
|
|
|
|
TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
|
|
|
|
if (intent.getBooleanExtra(
|
|
|
|
syncStatus.setText(intent
|
|
|
|
GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, // 附加参数键名
|
|
|
|
.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG));
|
|
|
|
false // 默认值
|
|
|
|
|
|
|
|
)) {
|
|
|
|
|
|
|
|
TextView syncStatus = (TextView) findViewById(
|
|
|
|
|
|
|
|
R.id.prefenerece_sync_status_textview // 状态显示文本框ID
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
// 设置动态进度信息(例如:"同步中(50%)")
|
|
|
|
|
|
|
|
syncStatus.setText(intent.getStringExtra(
|
|
|
|
|
|
|
|
GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG
|
|
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ====================== 选项菜单处理 ======================
|
|
|
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home: // 处理导航栏返回按钮
|
|
|
|
case android.R.id.home:
|
|
|
|
Intent intent = new Intent(this, NotesListActivity.class);
|
|
|
|
Intent intent = new Intent(this, NotesListActivity.class);
|
|
|
|
// 清理活动栈:如果目标Activity已在栈中,则弹出其上所有Activity
|
|
|
|
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
startActivity(intent); // 跳转到笔记列表
|
|
|
|
startActivity(intent);
|
|
|
|
return true; // 消费事件
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
return false; // 未处理的事件传递给超类
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|