/* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * 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; /* * @description * @param * @return * @author 太子 * @time 2023/12/20 23:31 */ 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; // 声明一个 GTaskReceiver 对象变量,用于接收 GTask 的广播 private GTaskReceiver mReceiver; // 声明一个原始账户信息的数组变量 private Account[] mOriAccounts; // 声明一个布尔类型变量,用于标记是否已添加账户 private boolean mHasAddedAccount; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); /* 使用应用程序图标进行导航 */ getActionBar().setDisplayHomeAsUpEnabled(true); // 加载偏好设置资源文件 addPreferencesFromResource(R.xml.preferences); // 获取账户类别的 PreferenceCategory 对象 mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY); // 创建一个 GTaskReceiver 对象,用于接收 GTask 的广播 mReceiver = new GTaskReceiver(); // 创建一个 IntentFilter 对象,用于过滤指定的广播 IntentFilter filter = new IntentFilter(); filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME); // 注册广播接收器,将接收到的广播交给 mReceiver 处理 registerReceiver(mReceiver, filter); // 初始化 mOriAccounts 为空 mOriAccounts = null; // 创建一个头部视图,并将其添加到列表中 View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null); getListView().addHeaderView(header, null, true); } @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; } } } } // 刷新界面显示 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 { // 如果最后同步时间为0,将最后同步时间文本视图设置为不可见 lastSyncTimeView.setVisibility(View.GONE); } } } private void refreshUI() { // 加载账户偏好设置 loadAccountPreference(); // 加载同步按钮 loadSyncButton(); } private void showSelectAccountAlertDialog() { // 创建一个 AlertDialog.Builder 对象 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); // 从布局文件 account_dialog_title 中获取标题视图 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 subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle); subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips)); // 将自定义标题视图设置给 dialogBuilder dialogBuilder.setCustomTitle(titleView); // 设置对话框的确定按钮为空,不显示 dialogBuilder.setPositiveButton(null, null); // 获取 Google 账户数组,以及当前默认的同步账户名 Account[] accounts = getGoogleAccounts(); String defAccount = getSyncAccountName(this); // 初始化一些变量 mOriAccounts = accounts; mHasAddedAccount = false; // 如果存在 Google 账户 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() 方法设置单选列表项 dialogBuilder.setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // 当用户选择一个账户时,设置新的同步账户并关闭对话框,然后刷新界面 setSyncAccount(itemMapping[which].toString()); dialog.dismiss(); refreshUI(); } }); } // 从布局文件 add_account_text 中获取一个视图添加到对话框中 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,打开添加账户的系统设置页面,并关闭对话框 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 对象 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); // 从布局文件 account_dialog_title 中获取标题视图 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 subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle); subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg)); // 将自定义标题视图设置给 dialogBuilder 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(); } /** * 获取所有的 Google 账户 * @return 包含所有 Google 账户的数组 */ private Account[] getGoogleAccounts() { AccountManager accountManager = AccountManager.get(this); return accountManager.getAccountsByType("com.google"); } /** * 设置同步账户 * @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); // 清除本地 GTasks 相关信息 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 提示用户成功设置了新的账户 Toast.makeText(NotesPreferenceActivity.this, getString(R.string.preferences_toast_success_set_accout, account), Toast.LENGTH_SHORT).show(); } } /* private void removeSyncAccount() { 这是一个方法,用于移除同步账户及相关信息,并清理本地 GTasks 相关信息。 1. 获取偏好设置; 2. 创建 SharedPreferences.Editor 对象进行修改; 3. 如果存在同步账户名,则移除; 4. 如果存在上次同步时间,则移除; 5. 提交偏好设置的修改; 6. 在新线程中进行以下操作: 1. 创建 ContentValues 对象存储更新的数值; 2. 将 GTasks ID 设置为空字符串; 3. 将同步 ID 设置为 0; 4. 通过内容解析器更新 Notes 表中的数据。 */ 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(); // 清理本地 GTasks 相关信息 new Thread(new Runnable() { public void run() { // 创建 ContentValues 对象存储更新的数值 ContentValues values = new ContentValues(); // 将 GTasks ID 设置为空字符串 values.put(NoteColumns.GTASK_ID, ""); // 将同步 ID 设置为 0 values.put(NoteColumns.SYNC_ID, 0); // 通过内容解析器更新 Notes 表中的数据 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 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); // 启动 NotesListActivity startActivity(intent); // 返回 true 表示已经处理了此事件 return true; default: // 如果选择了未知的菜单项,则返回 false return false; } } }