|
|
|
@ -14,375 +14,452 @@
|
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.ui; // 定义类的包名
|
|
|
|
|
|
|
|
|
|
import android.accounts.Account; // 导入Account类
|
|
|
|
|
import android.accounts.AccountManager; // 导入AccountManager类
|
|
|
|
|
import android.app.ActionBar; // 导入ActionBar类
|
|
|
|
|
import android.app.AlertDialog; // 导入AlertDialog类
|
|
|
|
|
import android.content.BroadcastReceiver; // 导入BroadcastReceiver类
|
|
|
|
|
import android.content.ContentValues; // 导入ContentValues类
|
|
|
|
|
import android.content.Context; // 导入Context类
|
|
|
|
|
import android.content.DialogInterface; // 导入DialogInterface类
|
|
|
|
|
import android.content.Intent; // 导入Intent类
|
|
|
|
|
import android.content.IntentFilter; // 导入IntentFilter类
|
|
|
|
|
import android.content.SharedPreferences; // 导入SharedPreferences类
|
|
|
|
|
import android.os.Bundle; // 导入Bundle类
|
|
|
|
|
import android.preference.Preference; // 导入Preference类
|
|
|
|
|
import android.preference.Preference.OnPreferenceClickListener; // 导入PreferenceClickListener接口
|
|
|
|
|
import android.preference.PreferenceActivity; // 导入PreferenceActivity类
|
|
|
|
|
import android.preference.PreferenceCategory; // 导入PreferenceCategory类
|
|
|
|
|
import android.text.TextUtils; // 导入TextUtils类
|
|
|
|
|
import android.text.format.DateFormat; // 导入DateFormat类
|
|
|
|
|
import android.view.LayoutInflater; // 导入LayoutInflater类
|
|
|
|
|
import android.view.Menu; // 导入Menu类
|
|
|
|
|
import android.view.MenuItem; // 导入MenuItem类
|
|
|
|
|
import android.view.View; // 导入View类
|
|
|
|
|
import android.widget.Button; // 导入Button类
|
|
|
|
|
import android.widget.TextView; // 导入TextView类
|
|
|
|
|
import android.widget.Toast; // 导入Toast类
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R; // 导入项目的资源文件
|
|
|
|
|
import net.micode.notes.data.Notes; // 导入Notes类
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns; // 导入NoteColumns类
|
|
|
|
|
import net.micode.notes.gtask.remote.GTaskSyncService; // 导入GTaskSyncService类
|
|
|
|
|
|
|
|
|
|
// 继承自PreferenceActivity的设置活动类
|
|
|
|
|
public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
public static final String PREFERENCE_NAME = "notes_preferences";
|
|
|
|
|
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_SYNC_ACCOUNT_NAME = "pref_key_account_name"; // 同步账户名称的键
|
|
|
|
|
|
|
|
|
|
public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time";
|
|
|
|
|
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";
|
|
|
|
|
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 PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key"; // 同步账户的键
|
|
|
|
|
|
|
|
|
|
private static final String AUTHORITIES_FILTER_KEY = "authorities";
|
|
|
|
|
private static final String AUTHORITIES_FILTER_KEY = "authorities"; // 权限过滤的键
|
|
|
|
|
|
|
|
|
|
private PreferenceCategory mAccountCategory;
|
|
|
|
|
private PreferenceCategory mAccountCategory; // 账户类别的PreferenceCategory
|
|
|
|
|
|
|
|
|
|
private GTaskReceiver mReceiver;
|
|
|
|
|
private GTaskReceiver mReceiver; // 用于接收GTask同步状态的BroadcastReceiver
|
|
|
|
|
|
|
|
|
|
private Account[] mOriAccounts;
|
|
|
|
|
private Account[] mOriAccounts; // 原始账户数组
|
|
|
|
|
|
|
|
|
|
private boolean mHasAddedAccount;
|
|
|
|
|
private boolean mHasAddedAccount; // 是否添加了账户
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle icicle) {
|
|
|
|
|
super.onCreate(icicle);
|
|
|
|
|
super.onCreate(icicle); // 调用父类的onCreate方法
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(true); // 设置ActionBar的返回键可用
|
|
|
|
|
|
|
|
|
|
addPreferencesFromResource(R.xml.preferences); // 从资源文件中添加偏好设置
|
|
|
|
|
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY); // 获取同步账户的PreferenceCategory
|
|
|
|
|
mReceiver = new GTaskReceiver(); // 创建GTaskReceiver实例
|
|
|
|
|
IntentFilter filter = new IntentFilter(); // 创建IntentFilter
|
|
|
|
|
filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME); // 添加GTask服务广播的Action
|
|
|
|
|
registerReceiver(mReceiver, filter); // 注册BroadcastReceiver
|
|
|
|
|
|
|
|
|
|
mOriAccounts = null; // 初始化原始账户数组为null
|
|
|
|
|
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null); // 从布局文件中填充设置头部视图
|
|
|
|
|
getListView().addHeaderView(header, null, true); // 将头部视图添加到ListView
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onResume() {
|
|
|
|
|
super.onResume();
|
|
|
|
|
super.onResume(); // 调用父类的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;
|
|
|
|
|
Account[] accounts = getGoogleAccounts(); // 获取Google账户数组
|
|
|
|
|
if (mOriAccounts != null && accounts.length > mOriAccounts.length) { // 如果新账户数组长度大于原始账户数组长度
|
|
|
|
|
for (Account accountNew : accounts) { // 遍历新账户数组
|
|
|
|
|
boolean found = false; // 初始化找到标志为false
|
|
|
|
|
for (Account accountOld : mOriAccounts) { // 遍历原始账户数组
|
|
|
|
|
if (TextUtils.equals(accountOld.name, accountNew.name)) { // 如果找到相同的账户
|
|
|
|
|
found = true; // 设置找到标志为true
|
|
|
|
|
break; // 跳出循环
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!found) {
|
|
|
|
|
setSyncAccount(accountNew.name);
|
|
|
|
|
break;
|
|
|
|
|
if (!found) { // 如果没有找到相同的账户
|
|
|
|
|
setSyncAccount(accountNew.name); // 设置同步账户
|
|
|
|
|
break; // 跳出循环
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
refreshUI();
|
|
|
|
|
refreshUI(); // 刷新用户界面
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
if (mReceiver != null) {
|
|
|
|
|
unregisterReceiver(mReceiver);
|
|
|
|
|
}
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
// 检查mReceiver是否已经被实例化,如果是,则注销广播接收器
|
|
|
|
|
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) {
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
// 加载账户偏好设置
|
|
|
|
|
private void loadAccountPreference() {
|
|
|
|
|
// 清除mAccountCategory中的所有Preference
|
|
|
|
|
mAccountCategory.removeAll();
|
|
|
|
|
|
|
|
|
|
// 创建一个新的Preference对象
|
|
|
|
|
Preference accountPref = new Preference(this);
|
|
|
|
|
// 获取默认同步账户的名称
|
|
|
|
|
final String defaultAccount = getSyncAccountName(this);
|
|
|
|
|
// 设置Preference的标题
|
|
|
|
|
accountPref.setTitle(getString(R.string.preferences_account_title));
|
|
|
|
|
// 设置Preference的摘要信息
|
|
|
|
|
accountPref.setSummary(getString(R.string.preferences_account_summary));
|
|
|
|
|
// 设置Preference点击事件的监听器
|
|
|
|
|
accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
|
// 如果没有正在进行的同步操作
|
|
|
|
|
if (!GTaskSyncService.isSyncing()) {
|
|
|
|
|
// 如果defaultAccount为空,表示是第一次设置账户
|
|
|
|
|
if (TextUtils.isEmpty(defaultAccount)) {
|
|
|
|
|
showSelectAccountAlertDialog(); // 显示选择账户的AlertDialog
|
|
|
|
|
} 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);
|
|
|
|
|
// 如果账户已经设置,提示用户更换账户的风险
|
|
|
|
|
showChangeAccountConfirmAlertDialog();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} 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,
|
|
|
|
|
DateFormat.format(getString(R.string.preferences_last_sync_time_format),
|
|
|
|
|
lastSyncTime)));
|
|
|
|
|
lastSyncTimeView.setVisibility(View.VISIBLE);
|
|
|
|
|
} else {
|
|
|
|
|
lastSyncTimeView.setVisibility(View.GONE);
|
|
|
|
|
// 如果正在进行同步操作,显示一个Toast提示用户不能更改账户
|
|
|
|
|
Toast.makeText(NotesPreferenceActivity.this,
|
|
|
|
|
R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
|
|
|
|
|
.show();
|
|
|
|
|
}
|
|
|
|
|
// 返回true表示已经处理了点击事件
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void refreshUI() {
|
|
|
|
|
loadAccountPreference();
|
|
|
|
|
loadSyncButton();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
// 将创建的Preference添加到mAccountCategory中
|
|
|
|
|
mAccountCategory.addPreference(accountPref);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
// 加载同步按钮
|
|
|
|
|
private void loadSyncButton() {
|
|
|
|
|
// 通过findViewById获取同步按钮和最后同步时间的TextView
|
|
|
|
|
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); // 取消同步
|
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// 如果没有同步操作,设置按钮文本为立即同步,并设置点击事件为开始同步操作
|
|
|
|
|
syncButton.setText(getString(R.string.preferences_button_sync_immediately));
|
|
|
|
|
syncButton.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"
|
|
|
|
|
});
|
|
|
|
|
startActivityForResult(intent, -1);
|
|
|
|
|
dialog.dismiss();
|
|
|
|
|
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 {
|
|
|
|
|
lastSyncTimeView.setVisibility(View.GONE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showChangeAccountConfirmAlertDialog() {
|
|
|
|
|
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_change_account_title,
|
|
|
|
|
getSyncAccountName(this)));
|
|
|
|
|
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
// 刷新UI,加载账户偏好和同步按钮
|
|
|
|
|
private void refreshUI() {
|
|
|
|
|
loadAccountPreference(); // 加载账户偏好
|
|
|
|
|
loadSyncButton(); // 加载同步按钮
|
|
|
|
|
}
|
|
|
|
|
// 显示选择账户的对话框
|
|
|
|
|
private void showSelectAccountAlertDialog() {
|
|
|
|
|
// 创建AlertDialog.Builder对象,用于构建对话框
|
|
|
|
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
|
|
|
|
|
|
|
|
|
|
// 从布局文件中加载自定义标题视图
|
|
|
|
|
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
|
|
|
|
|
// 获取标题TextView并设置标题文本
|
|
|
|
|
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
|
|
|
|
|
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
|
|
|
|
|
// 获取副标题TextView并设置副标题文本
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 如果有账户可供选择
|
|
|
|
|
if (accounts.length > 0) {
|
|
|
|
|
// 创建CharSequence数组用于存储账户名称
|
|
|
|
|
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.show();
|
|
|
|
|
// 将账户名称添加到items数组中
|
|
|
|
|
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(); // 刷新UI
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Account[] getGoogleAccounts() {
|
|
|
|
|
AccountManager accountManager = AccountManager.get(this);
|
|
|
|
|
return accountManager.getAccountsByType("com.google");
|
|
|
|
|
}
|
|
|
|
|
// 从布局文件中加载添加账户的视图
|
|
|
|
|
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" // 过滤器,指定要添加的账户类型
|
|
|
|
|
});
|
|
|
|
|
startActivityForResult(intent, -1); // 启动添加账户设置
|
|
|
|
|
dialog.dismiss(); // 关闭对话框
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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, "");
|
|
|
|
|
// 显示更改账户确认对话框
|
|
|
|
|
private void showChangeAccountConfirmAlertDialog() {
|
|
|
|
|
// 创建AlertDialog.Builder对象
|
|
|
|
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
|
|
|
|
|
|
|
|
|
|
// 从布局文件中加载自定义标题视图
|
|
|
|
|
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
|
|
|
|
|
// 获取标题TextView并设置标题文本
|
|
|
|
|
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
|
|
|
|
|
titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
|
|
|
|
|
getSyncAccountName(this))); // 显示当前同步账户名称
|
|
|
|
|
// 获取副标题TextView并设置副标题文本
|
|
|
|
|
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
|
|
|
|
|
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(); // 刷新UI
|
|
|
|
|
}
|
|
|
|
|
editor.commit();
|
|
|
|
|
|
|
|
|
|
// clean up last sync time
|
|
|
|
|
setLastSyncTime(this, 0);
|
|
|
|
|
|
|
|
|
|
// clean up local gtask related info
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.GTASK_ID, "");
|
|
|
|
|
values.put(NoteColumns.SYNC_ID, 0);
|
|
|
|
|
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
|
|
|
|
|
Toast.makeText(NotesPreferenceActivity.this,
|
|
|
|
|
getString(R.string.preferences_toast_success_set_accout, account),
|
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// 显示对话框
|
|
|
|
|
dialogBuilder.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取Google账户列表
|
|
|
|
|
private Account[] getGoogleAccounts() {
|
|
|
|
|
// 获取AccountManager实例
|
|
|
|
|
AccountManager accountManager = AccountManager.get(this);
|
|
|
|
|
// 返回所有Google账户
|
|
|
|
|
return accountManager.getAccountsByType("com.google");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void removeSyncAccount() {
|
|
|
|
|
// 设置同步账户
|
|
|
|
|
private void setSyncAccount(String account) {
|
|
|
|
|
// 检查当前同步账户是否与传入的账户不同
|
|
|
|
|
if (!getSyncAccountName(this).equals(account)) {
|
|
|
|
|
// 获取SharedPreferences实例,用于存储偏好设置
|
|
|
|
|
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);
|
|
|
|
|
// 如果传入的账户不为空,则保存账户名称;否则保存空字符串
|
|
|
|
|
if (account != null) {
|
|
|
|
|
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account);
|
|
|
|
|
} else {
|
|
|
|
|
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
|
|
|
|
|
}
|
|
|
|
|
editor.commit();
|
|
|
|
|
editor.commit(); // 提交更改
|
|
|
|
|
|
|
|
|
|
// clean up local gtask related info
|
|
|
|
|
// 清除最后同步时间
|
|
|
|
|
setLastSyncTime(this, 0);
|
|
|
|
|
|
|
|
|
|
// 清除本地与gtask相关的信息
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.GTASK_ID, "");
|
|
|
|
|
values.put(NoteColumns.SYNC_ID, 0);
|
|
|
|
|
values.put(NoteColumns.GTASK_ID, ""); // 清空GTASK_ID
|
|
|
|
|
values.put(NoteColumns.SYNC_ID, 0); // 清空SYNC_ID
|
|
|
|
|
// 更新Notes.CONTENT_NOTE_URI中的数据
|
|
|
|
|
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
}).start(); // 启动新线程执行清理操作
|
|
|
|
|
|
|
|
|
|
public static String getSyncAccountName(Context context) {
|
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
|
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
|
|
|
|
|
// 显示成功设置账户的Toast提示
|
|
|
|
|
Toast.makeText(NotesPreferenceActivity.this,
|
|
|
|
|
getString(R.string.preferences_toast_success_set_accout, account),
|
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
// 移除同步账户
|
|
|
|
|
private void removeSyncAccount() {
|
|
|
|
|
// 获取SharedPreferences实例
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static long getLastSyncTime(Context context) {
|
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
|
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0);
|
|
|
|
|
// 如果存在最后同步时间,则移除
|
|
|
|
|
if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) {
|
|
|
|
|
editor.remove(PREFERENCE_LAST_SYNC_TIME);
|
|
|
|
|
}
|
|
|
|
|
editor.commit(); // 提交更改
|
|
|
|
|
|
|
|
|
|
// 清除本地与gtask相关的信息
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.GTASK_ID, ""); // 清空GTASK_ID
|
|
|
|
|
values.put(NoteColumns.SYNC_ID, 0); // 清空SYNC_ID
|
|
|
|
|
// 更新Notes.CONTENT_NOTE_URI中的数据
|
|
|
|
|
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
|
|
|
|
|
}
|
|
|
|
|
}).start(); // 启动新线程执行清理操作
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class GTaskReceiver extends BroadcastReceiver {
|
|
|
|
|
// 获取当前同步账户名称
|
|
|
|
|
public static String getSyncAccountName(Context context) {
|
|
|
|
|
// 获取SharedPreferences实例
|
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
|
// 返回存储的同步账户名称,默认为空字符串
|
|
|
|
|
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
// 设置最后同步时间
|
|
|
|
|
public static void setLastSyncTime(Context context, long time) {
|
|
|
|
|
// 获取SharedPreferences实例
|
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
|
SharedPreferences.Editor editor = settings.edit();
|
|
|
|
|
// 保存最后同步时间
|
|
|
|
|
editor.putLong(PREFERENCE_LAST_SYNC_TIME, time);
|
|
|
|
|
editor.commit(); // 提交更改
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 获取最后同步时间
|
|
|
|
|
public static long getLastSyncTime(Context context) {
|
|
|
|
|
// 获取SharedPreferences实例
|
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
|
// 返回存储的最后同步时间,默认为0
|
|
|
|
|
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 定义GTaskReceiver类,继承自BroadcastReceiver
|
|
|
|
|
private class GTaskReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
|
case android.R.id.home:
|
|
|
|
|
Intent intent = new Intent(this, NotesListActivity.class);
|
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
// 当接收到广播时调用
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
refreshUI(); // 刷新UI
|
|
|
|
|
// 检查广播中是否包含同步状态信息
|
|
|
|
|
if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) {
|
|
|
|
|
// 获取同步状态的TextView并更新其文本
|
|
|
|
|
TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
|
|
|
|
|
syncStatus.setText(intent
|
|
|
|
|
.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理选项菜单项的选择事件
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
|
case android.R.id.home: // 如果点击的是返回按钮
|
|
|
|
|
// 创建返回到NotesListActivity的意图
|
|
|
|
|
Intent intent = new Intent(this, NotesListActivity.class);
|
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 清除上层活动
|
|
|
|
|
startActivity(intent); // 启动活动
|
|
|
|
|
return true; // 返回true表示事件已处理
|
|
|
|
|
default:
|
|
|
|
|
return false; // 返回false表示未处理该事件
|
|
|
|
|
}
|
|
|
|
|
}
|