|
|
|
@ -5,7 +5,7 @@
|
|
|
|
|
* 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
|
|
|
|
|
* 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,
|
|
|
|
@ -14,6 +14,7 @@
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 导入所需的包,这些包提供了各种 Android 开发中常用的类和接口
|
|
|
|
|
package net.micode.notes.ui;
|
|
|
|
|
|
|
|
|
|
import android.accounts.Account;
|
|
|
|
@ -47,74 +48,90 @@ import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
import net.micode.notes.gtask.remote.GTaskSyncService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置和偏好活动,用于配置应用程序的偏好设置。
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个名为 NotesPreferenceActivity 的类,继承自 PreferenceActivity,用于处理应用的偏好设置相关操作
|
|
|
|
|
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";
|
|
|
|
|
// 账户类别
|
|
|
|
|
|
|
|
|
|
// 声明一个 PreferenceCategory 类型的成员变量,用于存储账户相关的偏好设置分类
|
|
|
|
|
private PreferenceCategory mAccountCategory;
|
|
|
|
|
// GTask接收器
|
|
|
|
|
|
|
|
|
|
// 声明一个 GTaskReceiver 类型的成员变量,用于接收与 GTask 相关的广播
|
|
|
|
|
private GTaskReceiver mReceiver;
|
|
|
|
|
// 原始账户数组
|
|
|
|
|
|
|
|
|
|
// 声明一个 Account 数组类型的成员变量,用于存储原始账户信息
|
|
|
|
|
private Account[] mOriAccounts;
|
|
|
|
|
// 是否添加了账户
|
|
|
|
|
|
|
|
|
|
// 声明一个布尔类型的成员变量,用于标记是否添加了新账户
|
|
|
|
|
private boolean mHasAddedAccount;
|
|
|
|
|
|
|
|
|
|
// 重写 onCreate 方法,用于初始化活动
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle icicle) {
|
|
|
|
|
super.onCreate(icicle);
|
|
|
|
|
|
|
|
|
|
/* 使用应用程序图标进行导航 */
|
|
|
|
|
// 使用应用图标作为导航图标
|
|
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
|
|
|
|
|
// 从资源文件中添加偏好设置
|
|
|
|
|
addPreferencesFromResource(R.xml.preferences);
|
|
|
|
|
// 获取账户类别
|
|
|
|
|
// 获取同步账户的偏好设置分类
|
|
|
|
|
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY);
|
|
|
|
|
// 创建GTask接收器
|
|
|
|
|
// 初始化 GTaskReceiver 实例
|
|
|
|
|
mReceiver = new GTaskReceiver();
|
|
|
|
|
// 创建意图过滤器
|
|
|
|
|
// 创建 IntentFilter 实例,用于过滤与 GTask 服务相关的广播
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
|
filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME);
|
|
|
|
|
// 注册接收器
|
|
|
|
|
// 注册广播接收器
|
|
|
|
|
registerReceiver(mReceiver, filter);
|
|
|
|
|
|
|
|
|
|
// 初始化原始账户数组
|
|
|
|
|
// 初始化原始账户信息为 null
|
|
|
|
|
mOriAccounts = null;
|
|
|
|
|
// 从XML布局文件中加载设置头部
|
|
|
|
|
// 使用布局填充器从布局文件中加载设置头部视图
|
|
|
|
|
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null);
|
|
|
|
|
// 将头部添加到偏好列表中
|
|
|
|
|
// 将头部视图添加到列表视图中
|
|
|
|
|
getListView().addHeaderView(header, null, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重写 onResume 方法,用于在活动恢复时执行相关操作
|
|
|
|
|
@Override
|
|
|
|
|
protected void onResume() {
|
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
|
|
// 自动设置同步账户,如果用户添加了新账户
|
|
|
|
|
// 如果添加了新账户,则需要自动设置同步账户
|
|
|
|
|
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;
|
|
|
|
@ -123,172 +140,173 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 刷新UI
|
|
|
|
|
// 刷新 UI 界面
|
|
|
|
|
refreshUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
// 如果接收器不为空,注销接收器
|
|
|
|
|
if (mReceiver != null) {
|
|
|
|
|
unregisterReceiver(mReceiver);
|
|
|
|
|
}
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
// 重写 onDestroy 方法,用于在活动销毁时执行相关操作
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
// 如果广播接收器不为空,则注销广播接收器
|
|
|
|
|
if (mReceiver != null) {
|
|
|
|
|
unregisterReceiver(mReceiver);
|
|
|
|
|
}
|
|
|
|
|
// 调用父类的 onDestroy 方法
|
|
|
|
|
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) {
|
|
|
|
|
// 如果GTask服务没有正在同步
|
|
|
|
|
if (!GTaskSyncService.isSyncing()) {
|
|
|
|
|
// 如果默认账户为空
|
|
|
|
|
if (TextUtils.isEmpty(defaultAccount)) {
|
|
|
|
|
// 第一次设置账户
|
|
|
|
|
showSelectAccountAlertDialog();
|
|
|
|
|
} else {
|
|
|
|
|
// 如果账户已经设置,提示用户风险
|
|
|
|
|
showChangeAccountConfirmAlertDialog();
|
|
|
|
|
}
|
|
|
|
|
// 定义一个私有方法 loadAccountPreference,用于加载账户相关的偏好设置
|
|
|
|
|
private void loadAccountPreference() {
|
|
|
|
|
// 清除账户分类中的所有偏好设置
|
|
|
|
|
mAccountCategory.removeAll();
|
|
|
|
|
|
|
|
|
|
// 创建一个新的 Preference 实例
|
|
|
|
|
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)) {
|
|
|
|
|
// 显示选择账户的对话框
|
|
|
|
|
showSelectAccountAlertDialog();
|
|
|
|
|
} else {
|
|
|
|
|
// 提示用户不能更改账户
|
|
|
|
|
Toast.makeText(NotesPreferenceActivity.this,
|
|
|
|
|
R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
|
|
|
|
|
.show();
|
|
|
|
|
// 如果账户已经设置,提示用户更改账户的风险
|
|
|
|
|
showChangeAccountConfirmAlertDialog();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
// 显示提示信息,表示不能更改账户
|
|
|
|
|
Toast.makeText(NotesPreferenceActivity.this,
|
|
|
|
|
R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
|
|
|
|
|
.show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 将账户偏好设置添加到账户类别中
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 设置按钮状态
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this)));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 将账户偏好设置添加到账户分类中
|
|
|
|
|
mAccountCategory.addPreference(accountPref);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置最后同步时间
|
|
|
|
|
if (GTaskSyncService.isSyncing()) {
|
|
|
|
|
lastSyncTimeView.setText(GTaskSyncService.getProgressString());
|
|
|
|
|
// 定义一个私有方法 loadSyncButton,用于加载同步按钮和显示最后一次同步时间
|
|
|
|
|
private void loadSyncButton() {
|
|
|
|
|
// 获取同步按钮和显示最后一次同步时间的文本视图
|
|
|
|
|
Button syncButton = (Button) findViewById(R.id.preference_sync_button);
|
|
|
|
|
TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
|
|
|
|
|
|
|
|
|
|
// 设置按钮状态
|
|
|
|
|
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)));
|
|
|
|
|
|
|
|
|
|
// 设置最后一次同步时间
|
|
|
|
|
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,
|
|
|
|
|
DateFormat.format(getString(R.string.preferences_last_sync_time_format),
|
|
|
|
|
lastSyncTime)));
|
|
|
|
|
lastSyncTimeView.setVisibility(View.VISIBLE);
|
|
|
|
|
} else {
|
|
|
|
|
long lastSyncTime = getLastSyncTime(this);
|
|
|
|
|
if (lastSyncTime != 0) {
|
|
|
|
|
lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time,
|
|
|
|
|
DateFormat.format(getString(R.string.preferences_last_sync_time_format),
|
|
|
|
|
lastSyncTime)));
|
|
|
|
|
lastSyncTimeView.setVisibility(View.VISIBLE);
|
|
|
|
|
} else {
|
|
|
|
|
lastSyncTimeView.setVisibility(View.GONE);
|
|
|
|
|
}
|
|
|
|
|
lastSyncTimeView.setVisibility(View.GONE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 刷新UI。
|
|
|
|
|
*/
|
|
|
|
|
private void refreshUI() {
|
|
|
|
|
// 加载账户偏好设置和同步按钮
|
|
|
|
|
loadAccountPreference();
|
|
|
|
|
loadSyncButton();
|
|
|
|
|
}
|
|
|
|
|
// 定义一个私有方法 refreshUI,用于刷新 UI 界面
|
|
|
|
|
private void refreshUI() {
|
|
|
|
|
// 加载账户偏好设置和同步按钮
|
|
|
|
|
loadAccountPreference();
|
|
|
|
|
loadSyncButton();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 显示选择账户的警告对话框。
|
|
|
|
|
*/
|
|
|
|
|
private void showSelectAccountAlertDialog() {
|
|
|
|
|
// 创建警告对话框的构建器
|
|
|
|
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
|
|
|
|
|
|
|
|
|
|
// 从XML布局文件中加载标题视图
|
|
|
|
|
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
|
|
|
|
|
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
|
|
|
|
|
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
|
|
|
|
|
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
|
|
|
|
|
subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips));
|
|
|
|
|
|
|
|
|
|
// 设置自定义标题
|
|
|
|
|
dialogBuilder.setCustomTitle(titleView);
|
|
|
|
|
dialogBuilder.setPositiveButton(null, null);
|
|
|
|
|
|
|
|
|
|
// 获取Google账户
|
|
|
|
|
Account[] accounts = getGoogleAccounts());
|
|
|
|
|
String defAccount = getSyncAccountName(this);
|
|
|
|
|
|
|
|
|
|
// 初始化原始账户数组和添加账户标志
|
|
|
|
|
mOriAccounts = accounts;
|
|
|
|
|
mHasAddedAccount = false;
|
|
|
|
|
|
|
|
|
|
// 如果账户数量大于0
|
|
|
|
|
if (accounts.length > 0) {
|
|
|
|
|
// 创建账户项
|
|
|
|
|
CharSequence[] items = new CharSequence[accounts.length];
|
|
|
|
|
final CharSequence[] itemMapping = items;
|
|
|
|
|
int checkedItem = -1;
|
|
|
|
|
int index = 0;
|
|
|
|
|
for (Account account : accounts) {
|
|
|
|
|
if (TextUtils.equals(account.name, defAccount)) {
|
|
|
|
|
checkedItem = index;
|
|
|
|
|
}
|
|
|
|
|
items[index++] = account.name;
|
|
|
|
|
// 定义一个私有方法 showSelectAccountAlertDialog,用于显示选择账户的对话框
|
|
|
|
|
private void showSelectAccountAlertDialog() {
|
|
|
|
|
// 创建一个对话框构建器
|
|
|
|
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
|
|
|
|
|
|
|
|
|
|
// 从布局文件中加载自定义标题视图
|
|
|
|
|
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
|
|
|
|
|
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
|
|
|
|
|
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
|
|
|
|
|
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
|
|
|
|
|
subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips));
|
|
|
|
|
|
|
|
|
|
// 设置自定义标题
|
|
|
|
|
dialogBuilder.setCustomTitle(titleView);
|
|
|
|
|
dialogBuilder.setPositiveButton(null, null);
|
|
|
|
|
|
|
|
|
|
// 获取谷歌账户信息
|
|
|
|
|
Account[] accounts = getGoogleAccounts();
|
|
|
|
|
String defAccount = getSyncAccountName(this);
|
|
|
|
|
|
|
|
|
|
// 保存原始账户信息
|
|
|
|
|
mOriAccounts = accounts;
|
|
|
|
|
mHasAddedAccount = false;
|
|
|
|
|
|
|
|
|
|
// 如果有账户信息
|
|
|
|
|
if (accounts.length > 0) {
|
|
|
|
|
// 创建账户名称数组
|
|
|
|
|
CharSequence[] items = new CharSequence[accounts.length];
|
|
|
|
|
final CharSequence[] itemMapping = items;
|
|
|
|
|
int checkedItem = -1;
|
|
|
|
|
int index = 0;
|
|
|
|
|
for (Account account : accounts) {
|
|
|
|
|
if (TextUtils.equals(account.name, defAccount)) {
|
|
|
|
|
checkedItem = index;
|
|
|
|
|
}
|
|
|
|
|
// 设置单选项目
|
|
|
|
|
dialogBuilder.setSingleChoiceItems(items, checkedItem,
|
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
//设置同步账户
|
|
|
|
|
setSyncAccount(itemMapping[which].toString());
|
|
|
|
|
dialog.dismiss();
|
|
|
|
|
refreshUI();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// 从XML布局文件中加载添加账户视图
|
|
|
|
|
items[index++] = account.name;
|
|
|
|
|
}
|
|
|
|
|
// 设置单选列表项
|
|
|
|
|
dialogBuilder.setSingleChoiceItems(items, checkedItem,
|
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
// 设置同步账户
|
|
|
|
|
setSyncAccount(itemMapping[which].toString());
|
|
|
|
|
dialog.dismiss();
|
|
|
|
|
refreshUI();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从布局文件中加载添加账户视图
|
|
|
|
|
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null);
|
|
|
|
|
dialogBuilder.setView(addAccountView);
|
|
|
|
|
|
|
|
|
|
// 显示警告对话框
|
|
|
|
|
// 显示对话框
|
|
|
|
|
final AlertDialog dialog = dialogBuilder.show();
|
|
|
|
|
addAccountView.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
// 添加账户
|
|
|
|
|
// 标记添加了新账户
|
|
|
|
|
mHasAddedAccount = true;
|
|
|
|
|
// 启动添加账户的设置活动
|
|
|
|
|
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
|
|
|
|
|
intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] {
|
|
|
|
|
"gmail-ls"
|
|
|
|
@ -299,14 +317,12 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 显示更改账户的确认警告对话框。
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个私有方法 showChangeAccountConfirmAlertDialog,用于显示更改账户确认对话框
|
|
|
|
|
private void showChangeAccountConfirmAlertDialog() {
|
|
|
|
|
// 创建警告对话框的构建器
|
|
|
|
|
// 创建一个对话框构建器
|
|
|
|
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
|
|
|
|
|
|
|
|
|
|
// 从XML布局文件中加载标题视图
|
|
|
|
|
// 从布局文件中加载自定义标题视图
|
|
|
|
|
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
|
|
|
|
|
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
|
|
|
|
|
titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
|
|
|
|
@ -315,56 +331,57 @@ private void showChangeAccountConfirmAlertDialog() {
|
|
|
|
|
subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg));
|
|
|
|
|
dialogBuilder.setCustomTitle(titleView);
|
|
|
|
|
|
|
|
|
|
// 创建菜单项
|
|
|
|
|
// 创建菜单项数组
|
|
|
|
|
CharSequence[] menuItemArray = new CharSequence[] {
|
|
|
|
|
getString(R.string.preferences_menu_change_account),
|
|
|
|
|
getString(R.string.preferences_menu_remove_account),
|
|
|
|
|
getString(R.string.preferences_menu_cancel)
|
|
|
|
|
};
|
|
|
|
|
// 设置菜单项点击监听器
|
|
|
|
|
dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() {
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
// 处理菜单项点击事件
|
|
|
|
|
if (which == 0) {
|
|
|
|
|
// 显示选择账户的对话框
|
|
|
|
|
showSelectAccountAlertDialog();
|
|
|
|
|
} else if (which == 1) {
|
|
|
|
|
// 移除同步账户
|
|
|
|
|
removeSyncAccount();
|
|
|
|
|
refreshUI();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// 显示对话框
|
|
|
|
|
dialogBuilder.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取Google账户。
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个私有方法 getGoogleAccounts,用于获取谷歌账户信息
|
|
|
|
|
private Account[] getGoogleAccounts() {
|
|
|
|
|
// 获取账户管理器
|
|
|
|
|
// 获取 AccountManager 实例
|
|
|
|
|
AccountManager accountManager = AccountManager.get(this);
|
|
|
|
|
// 获取Google账户
|
|
|
|
|
// 返回类型为 "com.google" 的账户数组
|
|
|
|
|
return accountManager.getAccountsByType("com.google");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置同步账户。
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个私有方法 setSyncAccount,用于设置同步账户
|
|
|
|
|
private void setSyncAccount(String account) {
|
|
|
|
|
// 如果同步账户不为空且与当前账户不同
|
|
|
|
|
// 如果新账户与当前设置的同步账户不同
|
|
|
|
|
if (!getSyncAccountName(this).equals(account)) {
|
|
|
|
|
// 获取共享首选项
|
|
|
|
|
// 获取共享偏好设置
|
|
|
|
|
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
|
|
|
|
// 获取编辑器
|
|
|
|
|
SharedPreferences.Editor editor = settings.edit();
|
|
|
|
|
// 如果账户不为空,则设置同步账户名称,否则设置为空字符串
|
|
|
|
|
if (account != null) {
|
|
|
|
|
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account);
|
|
|
|
|
} else {
|
|
|
|
|
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
|
|
|
|
|
}
|
|
|
|
|
// 提交编辑
|
|
|
|
|
editor.commit();
|
|
|
|
|
|
|
|
|
|
// 清除最后同步时间
|
|
|
|
|
// 清除最后一次同步时间
|
|
|
|
|
setLastSyncTime(this, 0);
|
|
|
|
|
|
|
|
|
|
// 清除本地gtask相关信息
|
|
|
|
|
// 清除本地与 GTask 相关的信息
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
@ -374,29 +391,31 @@ private void setSyncAccount(String account) {
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
|
|
|
|
|
// 提示用户设置账户成功
|
|
|
|
|
// 显示设置账户成功的提示信息
|
|
|
|
|
Toast.makeText(NotesPreferenceActivity.this,
|
|
|
|
|
getString(R.string.preferences_toast_success_set_accout, account),
|
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移除同步账户。
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个私有方法 removeSyncAccount,用于移除同步账户
|
|
|
|
|
private void removeSyncAccount() {
|
|
|
|
|
// 获取共享首选项
|
|
|
|
|
// 获取共享偏好设置
|
|
|
|
|
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
|
|
|
|
// 获取编辑器
|
|
|
|
|
SharedPreferences.Editor editor = settings.edit();
|
|
|
|
|
// 如果存在同步账户名称,则移除
|
|
|
|
|
if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) {
|
|
|
|
|
editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME);
|
|
|
|
|
}
|
|
|
|
|
// 如果存在最后一次同步时间,则移除
|
|
|
|
|
if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) {
|
|
|
|
|
editor.remove(PREFERENCE_LAST_SYNC_TIME);
|
|
|
|
|
}
|
|
|
|
|
// 提交编辑
|
|
|
|
|
editor.commit();
|
|
|
|
|
|
|
|
|
|
// 清除本地gtask相关信息
|
|
|
|
|
// 清除本地与 GTask 相关的信息
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
@ -407,65 +426,69 @@ private void removeSyncAccount() {
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取同步账户名称。
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个公共静态方法 getSyncAccountName,用于获取同步账户名称
|
|
|
|
|
public static String getSyncAccountName(Context context) {
|
|
|
|
|
// 获取共享偏好设置
|
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
|
// 返回同步账户名称,如果不存在则返回空字符串
|
|
|
|
|
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置最后同步时间。
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个公共静态方法 setLastSyncTime,用于设置最后一次同步时间
|
|
|
|
|
public static void setLastSyncTime(Context context, long time) {
|
|
|
|
|
// 获取共享偏好设置
|
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
|
// 获取编辑器
|
|
|
|
|
SharedPreferences.Editor editor = settings.edit();
|
|
|
|
|
// 设置最后一次同步时间
|
|
|
|
|
editor.putLong(PREFERENCE_LAST_SYNC_TIME, time);
|
|
|
|
|
// 提交编辑
|
|
|
|
|
editor.commit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取最后同步时间。
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个公共静态方法 getLastSyncTime,用于获取最后一次同步时间
|
|
|
|
|
public static long getLastSyncTime(Context context) {
|
|
|
|
|
// 获取共享偏好设置
|
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
|
// 返回最后一次同步时间,如果不存在则返回 0
|
|
|
|
|
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GTask接收器,用于接收GTask服务的广播。
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个内部类 GTaskReceiver,继承自 BroadcastReceiver,用于接收 GTask 相关的广播
|
|
|
|
|
private class GTaskReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
// 刷新UI
|
|
|
|
|
// 刷新 UI 界面
|
|
|
|
|
refreshUI();
|
|
|
|
|
// 如果正在同步
|
|
|
|
|
if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) {
|
|
|
|
|
// 获取同步状态视图
|
|
|
|
|
// 获取显示同步状态的文本视图
|
|
|
|
|
TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
|
|
|
|
|
// 设置同步状态文本
|
|
|
|
|
syncStatus.setText(intent
|
|
|
|
|
.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理选项菜单项点击事件。
|
|
|
|
|
*/
|
|
|
|
|
// 重写 onOptionsItemSelected 方法,用于处理菜单项点击事件
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
// 根据菜单项的 ID 进行处理
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
|
case android.R.id.home:
|
|
|
|
|
// 创建一个 Intent,用于启动 NotesListActivity
|
|
|
|
|
Intent intent = new Intent(this, NotesListActivity.class);
|
|
|
|
|
// 添加标志,清除任务栈顶部的活动
|
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
|
// 启动活动
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|