You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
software/NotesPreferenceActivity.txt

238 lines
8.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 便签应用的偏好设置活动,用于管理应用的设置。
*/
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; // 是否添加了账户
/**
* 活动创建时调用。
* @param icicle 保存的实例状态
*/
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
// 设置返回键
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);
// 初始化视图
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null);
getListView().addHeaderView(header, null, true);
}
/**
* 活动恢复时调用。
*/
@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;
}
}
}
}
// 刷新UI
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)) {
showSelectAccountAlertDialog();
} else {
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);
// 设置按钮状态
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 {
lastSyncTimeView.setVisibility(View.GONE);
}
}
}
/**
* 刷新UI。
*/
private void refreshUI() {
loadAccountPreference();
loadSyncButton();
}
/**
* 显示选择账户的对话框。
*/
private void showSelectAccountAlertDialog() {
// 对话框代码...
}
/**
* 显示更改账户的确认对话框。
*/
private void showChangeAccountConfirmAlertDialog() {
// 对话框代码...
}
/**
* 获取Google账户。
* @return 账户数组
*/
private Account[] getGoogleAccounts() {
AccountManager accountManager = AccountManager.get(this);
return accountManager.getAccountsByType("com.google");
}
/**
* 设置同步账户。
* @param account 账户名
*/
private void setSyncAccount(String account) {
// 设置同步账户代码...
}
/**
* 移除同步账户。
*/
private void removeSyncAccount() {
// 移除同步账户代码...
}
/**
* 获取同步账户名。
* @param context 上下文对象
* @return 同步账户名
*/
public static String getSyncAccountName(Context context) {
// 获取同步账户名代码...
}
/**
* 设置最后同步时间。
* @param context 上下文对象
* @param time 最后同步时间
*/
public static void setLastSyncTime(Context context, long time) {
// 设置最后同步时间代码...
}
/**
* 获取最后同步时间。
* @param context 上下文对象
* @return 最后同步时间
*/
public static long getLastSyncTime(Context context) {
// 获取最后同步时间代码...
}
/**
* GTask接收器用于接收同步服务的广播。
*/
private class GTaskReceiver extends BroadcastReceiver {
@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);
syncS