|
|
|
@ -70,225 +70,269 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
protected void onCreate(Bundle icicle) {
|
|
|
|
|
super.onCreate(icicle);
|
|
|
|
|
|
|
|
|
|
/* using the app icon for navigation */
|
|
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(true); // 设置ActionBar的导航按钮可见,通常用于返回上一个界面。
|
|
|
|
|
|
|
|
|
|
addPreferencesFromResource(R.xml.preferences); // 从XML资源文件中加载偏好设置。
|
|
|
|
|
|
|
|
|
|
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY); // 获取偏好设置中的PreferenceCategory对象。
|
|
|
|
|
|
|
|
|
|
mReceiver = new GTaskReceiver(); // 创建一个GTaskReceiver对象,用于接收广播。
|
|
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter(); // 创建一个IntentFilter对象,用于过滤广播。
|
|
|
|
|
|
|
|
|
|
filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME); // 添加一个广播动作到IntentFilter中。
|
|
|
|
|
|
|
|
|
|
registerReceiver(mReceiver, filter); // 注册广播接收器,使其能够接收指定动作的广播。
|
|
|
|
|
|
|
|
|
|
mOriAccounts = null; // 将mOriAccounts变量设置为null。
|
|
|
|
|
|
|
|
|
|
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null); // 通过布局文件`settings_header`创建一个视图对象。
|
|
|
|
|
|
|
|
|
|
getListView().addHeaderView(header, null, true); // 将创建的视图对象添加为ListView的头部视图。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
protected void onResume() {
|
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
|
|
// 如果用户已经添加了一个新的账户,那么就需要自动设置同步账户
|
|
|
|
|
if (mHasAddedAccount) {
|
|
|
|
|
// 获取所有的Google账户,并将其存储在一个数组中
|
|
|
|
|
Account[] accounts = getGoogleAccounts();
|
|
|
|
|
// 如果新账户的数量大于旧账户的数量,说明有新账户添加
|
|
|
|
|
if (mOriAccounts != null && accounts.length > mOriAccounts.length) {
|
|
|
|
|
// 遍历新账户数组,与旧账户数组进行比较
|
|
|
|
|
for (Account accountNew : accounts) {
|
|
|
|
|
boolean found = false;
|
|
|
|
|
for (Account accountOld : mOriAccounts) {
|
|
|
|
|
// 如果找到了相同的账户,将found标志设置为true,并跳出循环
|
|
|
|
|
if (TextUtils.equals(accountOld.name, accountNew.name)) {
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 如果没有找到相同的账户,就调用setSyncAccount()方法设置同步账户为新账户的名称,并跳出循环
|
|
|
|
|
if (!found) {
|
|
|
|
|
setSyncAccount(accountNew.name);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
refreshUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
if (mReceiver != null) {
|
|
|
|
|
unregisterReceiver(mReceiver);
|
|
|
|
|
}
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
// 刷新用户界面
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
private void loadAccountPreference() {
|
|
|
|
|
// 清空账户类别下的所有偏好设置
|
|
|
|
|
mAccountCategory.removeAll();
|
|
|
|
|
|
|
|
|
|
// 创建一个Preference对象
|
|
|
|
|
Preference accountPref = new Preference(this);
|
|
|
|
|
final String defaultAccount = getSyncAccountName(this);
|
|
|
|
|
// 设置Preference的标题和摘要
|
|
|
|
|
accountPref.setTitle(getString(R.string.preferences_account_title));
|
|
|
|
|
accountPref.setSummary(getString(R.string.preferences_account_summary));
|
|
|
|
|
// 设置Preference的点击事件监听器
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mAccountCategory.addPreference(accountPref);
|
|
|
|
|
}
|
|
|
|
|
// 将Preference添加到账户类别下
|
|
|
|
|
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)));
|
|
|
|
|
private void loadSyncButton() {
|
|
|
|
|
// 获取同步按钮和上次同步时间的视图
|
|
|
|
|
Button syncButton = (Button) findViewById(R.id.preference_sync_button);
|
|
|
|
|
TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
|
|
|
|
|
|
|
|
|
|
// set last sync time
|
|
|
|
|
if (GTaskSyncService.isSyncing()) {
|
|
|
|
|
lastSyncTimeView.setText(GTaskSyncService.getProgressString());
|
|
|
|
|
// 设置按钮状态
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
private void refreshUI() {
|
|
|
|
|
// 刷新用户界面
|
|
|
|
|
loadAccountPreference();
|
|
|
|
|
loadSyncButton();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
items[index++] = account.name;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 获取Google账户列表和当前同步账户
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
items[index++] = account.name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
dialogBuilder.setSingleChoiceItems(items, checkedItem,
|
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
// 设置同步账户并刷新界面
|
|
|
|
|
setSyncAccount(itemMapping[which].toString());
|
|
|
|
|
dialog.dismiss();
|
|
|
|
|
refreshUI();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
startActivityForResult(intent, -1);
|
|
|
|
|
dialog.dismiss();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
// 设置添加账户的视图
|
|
|
|
|
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 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();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
dialogBuilder.show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
dialogBuilder.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Account[] getGoogleAccounts() {
|
|
|
|
|
// 获取Google账户列表
|
|
|
|
|
AccountManager accountManager = AccountManager.get(this);
|
|
|
|
|
return accountManager.getAccountsByType("com.google");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Account[] getGoogleAccounts() {
|
|
|
|
|
AccountManager accountManager = AccountManager.get(this);
|
|
|
|
|
return accountManager.getAccountsByType("com.google");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setSyncAccount(String account) {
|
|
|
|
|
/*设置同步账户。它首先检查传入的账户名是否与当前保存的账户名不同,如果不同,则更新SharedPreferences中的账户名。然后,它清除上次同步时间,并在后台线程中清除本地与GTask相关的信息。最后,它显示一个Toast消息,通知用户账户设置成功。*/
|
|
|
|
|
if (!getSyncAccountName(this).equals(account)) {
|
|
|
|
|
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
|
|
|
|
SharedPreferences.Editor editor = settings.edit();
|
|
|
|
@ -339,7 +383,7 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//设置上次同步时间
|
|
|
|
|
public static String getSyncAccountName(Context context) {
|
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
@ -361,7 +405,7 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class GTaskReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
|
|
//广播接收器,用于接收GTask同步服务的广播消息。
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
refreshUI();
|
|
|
|
@ -370,7 +414,7 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
syncStatus.setText(intent
|
|
|
|
|
.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//它在接收到广播后刷新UI,并根据广播中的信息更新同步状态
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|