From c5c75ac2ae50b3de4a516c4b2fcd86aab9ee8d8e Mon Sep 17 00:00:00 2001 From: Sunique_L <2758798112@qq.com> Date: Thu, 21 Dec 2023 16:36:01 +0800 Subject: [PATCH] Update NotesPreferenceActivity.java --- .../notes/ui/NotesPreferenceActivity.java | 788 +++++++++++------- 1 file changed, 477 insertions(+), 311 deletions(-) diff --git a/src/net/micode/notes/ui/NotesPreferenceActivity.java b/src/net/micode/notes/ui/NotesPreferenceActivity.java index 07c5f7e..ca97984 100644 --- a/src/net/micode/notes/ui/NotesPreferenceActivity.java +++ b/src/net/micode/notes/ui/NotesPreferenceActivity.java @@ -48,228 +48,334 @@ import net.micode.notes.data.Notes.NoteColumns; 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"; - - private PreferenceCategory mAccountCategory; - - private GTaskReceiver mReceiver; - - private Account[] mOriAccounts; - - 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); - } - - @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); - break; - } - } - } - } - - 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(); - } - } 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); - } - }); - } 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); - } - } - } - - private void refreshUI() { - loadAccountPreference(); - loadSyncButton(); - } +//声明一个名为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"; + + // 声明一个PreferenceCategory类型的成员变量mAccountCategory,用于保存账户偏好设置的类别 + private PreferenceCategory mAccountCategory; + + // 声明一个GTaskReceiver类型的成员变量mReceiver,可能用于接收和处理任务相关的广播 + private GTaskReceiver mReceiver; + + // 声明一个Account类型的数组mOriAccounts,用于保存原始账户信息 + private Account[] mOriAccounts; + + // 声明一个布尔类型的成员变量mHasAddedAccount,用于跟踪是否已添加账户 + private boolean mHasAddedAccount; + + // 重写PreferenceActivity的onCreate方法,该方法在活动创建时被调用 + @Override + protected void onCreate(Bundle icicle) { + // 调用父类的onCreate方法并设置导航图标为应用图标,允许用户通过图标进行导航 + super.onCreate(icicle); + getActionBar().setDisplayHomeAsUpEnabled(true); + + // 从资源文件中加载偏好设置 + addPreferencesFromResource(R.xml.preferences); + // 获取账户偏好设置的类别 + mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY); + // 创建一个GTaskReceiver对象 + mReceiver = new GTaskReceiver(); + // 创建一个IntentFilter对象,并添加一个动作过滤器,该动作过滤器用于过滤来自GTaskSyncService的广播 + IntentFilter filter = new IntentFilter(); + filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME); + // 注册广播接收器以接收与GTaskSyncService相关的广播 + registerReceiver(mReceiver, filter); + + // 将mOriAccounts设置为null,表示尚未初始化该变量(在后续代码中可能会进行初始化) + mOriAccounts = null; + // 使用LayoutInflater从当前context创建一个视图,并从布局文件R.layout.settings_header中获取头部视图(可能包含一些标题或标头信息) + View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null); + // 将头部视图添加到列表视图中,允许在偏好设置列表中显示一些额外的信息或标题等。参数为null是因为我们不需要在此处创建任何新的视图层次结构。第三个参数为true表示在列表中显示头部视图。如果为false,则头部视图不会显示在列表中。 + getListView().addHeaderView(header, null, true); + } // 结束onCreate方法定义 +} // 结束NotesPreferenceActivity类定义 + + +@Override +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) { + // 检查新账户是否在原始列表中 + if (TextUtils.equals(accountOld.name, accountNew.name)) { + found = true; + break; + } + } + // 如果新账户不在原始列表中,则设置该账户为同步账户 + if (!found) { + setSyncAccount(accountNew.name); + break; + } + } + } + } + + // 刷新UI,可能是为了显示同步账户的更改或其他相关UI更新 + refreshUI(); +} + +@Override +protected void onDestroy() { + if (mReceiver != null) { + // 取消注册广播接收器,以避免内存泄漏 + unregisterReceiver(mReceiver); + } + super.onDestroy(); +} - private void showSelectAccountAlertDialog() { - AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); +//加载账户偏好 +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(); + } + // 返回true表示监听器已经处理了点击事件 + return true; + } + }); + + // 将账户偏好添加到账户分类中 + mAccountCategory.addPreference(accountPref); +} - 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); +//加载同步按钮 +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); + // 如果最后一次同步时间不为0 + 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); + } + } +} - Account[] accounts = getGoogleAccounts(); - String defAccount = getSyncAccountName(this); - mOriAccounts = accounts; - mHasAddedAccount = false; +//刷新UI,可能包括加载账户偏好设置和同步按钮的状态等 +private void refreshUI() { + // 加载账户偏好设置 + loadAccountPreference(); + // 加载同步按钮 + loadSyncButton(); +} + +//显示选择账户的对话框提示 +private void showSelectAccountAlertDialog() { + // 创建对话框构建器 + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); + + // 从布局中加载 account_dialog_title 视图,并获取其中的 TextView 实例 + 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; + // 标记是否已添加账户,默认为 false + mHasAddedAccount = false; + + // 如果存在谷歌账户 + if (accounts.length > 0) { + // 创建字符序列数组,用于存放账户名称 + CharSequence[] items = new CharSequence[accounts.length]; + // 用于映射原始账户数组和字符序列数组的对应关系 + final CharSequence[] itemMapping = items; + int checkedItem = -1; // 默认未选中任何账户,初始值为 -1 + int index = 0; + // 遍历所有账户 + for (Account account : accounts) { + // 如果账户名称与默认同步账户名称相同 + if (TextUtils.equals(account.name, defAccount)) { + checkedItem = index; // 将该账户设置为默认选中状态,checkedItem 值为索引值 + } + items[index++] = account.name; // 将账户名称存入字符序列数组中 + } + // 设置单选列表项,并指定默认选中项和点击监听器 + dialogBuilder.setSingleChoiceItems(items, checkedItem, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + // 设置同步账户,参数为选中的账户名称 + setSyncAccount(itemMapping[which].toString()); + // 关闭对话框 + dialog.dismiss(); + // 刷新UI + refreshUI(); + } + }); + } +} - 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(); - } - }); - } - - 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) - }; +//加载添加账户的视图,并将其设置为对话框的内容 +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"); + // 添加过滤条件,仅显示gmail-ls账户 + intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] { + "gmail-ls" + }); + // 启动意图,并等待结果(-1) + startActivityForResult(intent, -1); + // 关闭对话框 + dialog.dismiss(); + } +}); +} + +//显示更改账户的确认对话框 +private void showChangeAccountConfirmAlertDialog() { +AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); + +//加载账户对话框的标题视图,并获取其中的 TextView 实例 +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) { @@ -288,101 +394,161 @@ public class NotesPreferenceActivity extends PreferenceActivity { 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) { - editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account); - } else { - editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); - } - 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(); - } + /** + * 设置同步账户 + * + * @param account 要设置的同步账户名称 + */ + 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相关的信息 + new Thread(new Runnable() { + public void run() { + ContentValues values = new ContentValues(); + values.put(NoteColumns.GTASK_ID, ""); // 将GTask ID设置为空字符串 + values.put(NoteColumns.SYNC_ID, 0); // 将同步ID设置为0 + getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null); // 更新内容URI,清理与同步相关的信息 + } + }).start(); // 启动新线程来执行清理操作 + + // 显示一个短暂的Toast消息,告知用户成功设置了账户 + Toast.makeText(NotesPreferenceActivity.this, + getString(R.string.preferences_toast_success_set_accout, account), // 获取并格式化Toast消息文本 + Toast.LENGTH_SHORT).show(); // 显示Toast消息,持续时间短暂 + } } - - 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(); - - // 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(); - } - - 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); - SharedPreferences.Editor editor = settings.edit(); - editor.putLong(PREFERENCE_LAST_SYNC_TIME, time); - editor.commit(); - } - - public static long getLastSyncTime(Context context) { - SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, - Context.MODE_PRIVATE); - return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0); + + /** + * 移除同步账户 + */ + 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相关的信息 + new Thread(new Runnable() { + public void run() { + ContentValues values = new ContentValues(); + // 将GTask ID设置为空字符串 + values.put(NoteColumns.GTASK_ID, ""); + // 将同步ID设置为0 + values.put(NoteColumns.SYNC_ID, 0); + // 更新内容URI,清理与同步相关的信息 + getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null); + } + }).start(); // 启动新线程来执行清理操作 + } + + /** + * 获取同步账户名称 + * @param context 上下文对象 + * @return 同步账户名称,如果不存在则返回空字符串 + */ + public static String getSyncAccountName(Context context) { + // 获取共享偏好设置 + SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, + Context.MODE_PRIVATE); + // 返回同步账户名称,如果不存在则返回空字符串 + return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); + } + + /** + * 设置上次同步的时间 + * @param context 上下文对象 + * @param time 上次同步的时间(以毫秒为单位) + */ + 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 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); - syncStatus.setText(intent - .getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG)); - } - - } - } - - 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; - } - } -} + + + // 定义一个公共静态方法,名为getLastSyncTime,接收一个Context对象作为参数 + // 该方法用于从SharedPreferences中获取上次同步的时间(以毫秒为单位),如果不存在则返回0 + public static long getLastSyncTime(Context context) { + // 从给定的Context对象中获取名为PREFERENCE_NAME的SharedPreferences对象,同时设置其访问模式为Context.MODE_PRIVATE + SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, + Context.MODE_PRIVATE); + // 从SharedPreferences对象中获取名为PREFERENCE_Last_SYNC_TIME的值,如果不存在则返回0 + return settings.getLong(PREFERENCE_Last_Sync_Time, 0); + } + + // 定义一个私有的内部类GTaskReceiver,它继承了BroadcastReceiver类 + private class GTaskReceiver extends BroadcastReceiver { + + // 覆盖BroadcastReceiver的onReceive方法,接收两个参数:Context对象和Intent对象 + @Override + public void onReceive(Context context, Intent intent) { + // 调用refreshUI方法,可能是为了更新用户界面 + refreshUI(); + // 从Intent对象中获取一个Boolean类型的额外数据,键为GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING,如果不存在则返回false + // 如果该额外数据存在并且为true,说明正在进行同步操作 + if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) { + // 从布局中获取id为R.id.prefenerece_sync_status_textview的TextView对象 + TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview); + // 将从Intent中获取的额外数据(键为GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG)设置为TextView的内容,显示同步进度信息 + syncStatus.setText(intent.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG)); + } + } + } + + // 定义一个公共的onOptionsItemSelected方法,接收一个MenuItem对象作为参数,返回一个boolean值 + // 该方法用于处理菜单项的选择事件 + public boolean onOptionsItemSelected(MenuItem item) { + // 根据选择的菜单项ID进行不同的处理 + switch (item.getItemId()) { + // 如果选择的菜单项ID为android.R.id.home,则执行以下操作: + case android.R.id.home: + // 创建一个新的Intent对象,目标Activity为NotesListActivity.class + Intent intent = new Intent(this, NotesListActivity.class); + // 向Intent对象添加一个标志位,表示在启动新Activity之前先清除当前Activity的栈记录,达到关闭当前Activity的效果 + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + // 启动目标Activity + startActivity(intent); + // 返回true表示该菜单项已被处理,不再向下执行 + return true; + // 如果选择的菜单项ID不属于android.R.id.home,则返回false,表示该菜单项未被处理,继续向下执行其他菜单项的处理逻辑 + default: + return false; + } + } \ No newline at end of file