From 41d468a2bbf652a7ac9df2062f8644f8f80be57b Mon Sep 17 00:00:00 2001 From: pfmsx5cbu <1807616406@qq.com> Date: Mon, 16 Jun 2025 19:40:27 +0800 Subject: [PATCH] Update NotesPreferenceActivity.java --- .../notes/ui/NotesPreferenceActivity.java | 91 +++++++++++++++---- 1 file changed, 74 insertions(+), 17 deletions(-) diff --git a/java/net/micode/notes/ui/NotesPreferenceActivity.java b/java/net/micode/notes/ui/NotesPreferenceActivity.java index 07c5f7e..5c08a4f 100644 --- a/java/net/micode/notes/ui/NotesPreferenceActivity.java +++ b/java/net/micode/notes/ui/NotesPreferenceActivity.java @@ -49,24 +49,25 @@ 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"; - + // 账户分类Preference private PreferenceCategory mAccountCategory; - + // 同步服务广播接收器 private GTaskReceiver mReceiver; - + // 原始账户列表 private Account[] mOriAccounts; - + // 标记是否添加了新账户 private boolean mHasAddedAccount; @Override @@ -74,16 +75,21 @@ public class NotesPreferenceActivity extends PreferenceActivity { super.onCreate(icicle); /* using the app icon for navigation */ + // 启用ActionBar的返回按钮 getActionBar().setDisplayHomeAsUpEnabled(true); - + // 从XML资源加载偏好设置 addPreferencesFromResource(R.xml.preferences); + // 获取账户分类Preference mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY); + // 创建并注册同步服务广播接收器 mReceiver = new GTaskReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME); registerReceiver(mReceiver, filter); - + + // 初始化原始账户列表为null mOriAccounts = null; + // 添加设置界面的头部视图 View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null); getListView().addHeaderView(header, null, true); } @@ -96,6 +102,7 @@ public class NotesPreferenceActivity extends PreferenceActivity { // account if (mHasAddedAccount) { Account[] accounts = getGoogleAccounts(); + // 比较新旧账户列表,找出新添加的账户 if (mOriAccounts != null && accounts.length > mOriAccounts.length) { for (Account accountNew : accounts) { boolean found = false; @@ -106,18 +113,20 @@ public class NotesPreferenceActivity extends PreferenceActivity { } } if (!found) { + // 设置新账户为同步账户 setSyncAccount(accountNew.name); break; } } } } - + // 刷新UI refreshUI(); } @Override protected void onDestroy() { + // 注销广播接收器 if (mReceiver != null) { unregisterReceiver(mReceiver); } @@ -125,14 +134,18 @@ public class NotesPreferenceActivity extends PreferenceActivity { } 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 @@ -143,6 +156,7 @@ public class NotesPreferenceActivity extends PreferenceActivity { showChangeAccountConfirmAlertDialog(); } } else { + // 同步中不能修改账户 Toast.makeText(NotesPreferenceActivity.this, R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT) .show(); @@ -150,37 +164,47 @@ public class NotesPreferenceActivity extends PreferenceActivity { 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, @@ -188,6 +212,7 @@ public class NotesPreferenceActivity extends PreferenceActivity { lastSyncTime))); lastSyncTimeView.setVisibility(View.VISIBLE); } else { + // 没有同步记录时隐藏 lastSyncTimeView.setVisibility(View.GONE); } } @@ -200,7 +225,8 @@ public class NotesPreferenceActivity extends PreferenceActivity { 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)); @@ -210,26 +236,32 @@ public class NotesPreferenceActivity extends PreferenceActivity { 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; } 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(); @@ -237,13 +269,16 @@ public class NotesPreferenceActivity extends PreferenceActivity { }); } + // 添加"添加账户"视图 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" @@ -256,7 +291,8 @@ public class NotesPreferenceActivity extends PreferenceActivity { 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, @@ -265,6 +301,7 @@ public class NotesPreferenceActivity extends PreferenceActivity { 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), @@ -273,8 +310,10 @@ public class NotesPreferenceActivity extends PreferenceActivity { dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which == 0) { + // 更改账户 showSelectAccountAlertDialog(); } else if (which == 1) { + // 移除账户 removeSyncAccount(); refreshUI(); } @@ -283,13 +322,19 @@ public class NotesPreferenceActivity extends PreferenceActivity { dialogBuilder.show(); } + /** + * 获取所有Google账户 + */ private Account[] getGoogleAccounts() { AccountManager accountManager = AccountManager.get(this); return accountManager.getAccountsByType("com.google"); } - + /** + * 设置同步账户 + */ 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) { @@ -340,12 +385,18 @@ public class NotesPreferenceActivity extends PreferenceActivity { }).start(); } + /** + * 获取同步账户名称 + */ public static String getSyncAccountName(Context context) { SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); } + /** + * 设置上次同步时间 + */ public static void setLastSyncTime(Context context, long time) { SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); @@ -364,8 +415,10 @@ public class NotesPreferenceActivity extends PreferenceActivity { @Override public void onReceive(Context context, Intent intent) { + // 收到广播后刷新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)); @@ -374,9 +427,13 @@ public class NotesPreferenceActivity extends PreferenceActivity { } } + /** + * 处理选项菜单项选择 + */ 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);