|
|
|
@ -47,53 +47,60 @@ import net.micode.notes.data.Notes;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 偏好设置相关常量
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
/* using the app icon for navigation */
|
|
|
|
|
// 设置ActionBar导航图标
|
|
|
|
|
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) {
|
|
|
|
@ -113,17 +120,24 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 刷新界面
|
|
|
|
|
refreshUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 活动销毁回调
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
if (mReceiver != null) {
|
|
|
|
|
unregisterReceiver(mReceiver);
|
|
|
|
|
unregisterReceiver(mReceiver); // 取消注册广播接收器
|
|
|
|
|
}
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加载账户偏好设置
|
|
|
|
|
*/
|
|
|
|
|
private void loadAccountPreference() {
|
|
|
|
|
mAccountCategory.removeAll();
|
|
|
|
|
|
|
|
|
@ -135,16 +149,15 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
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)
|
|
|
|
|
R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
|
|
|
|
|
.show();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
@ -154,11 +167,14 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
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() {
|
|
|
|
@ -176,7 +192,7 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}
|
|
|
|
|
syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this)));
|
|
|
|
|
|
|
|
|
|
// set last sync time
|
|
|
|
|
// 设置上次同步时间
|
|
|
|
|
if (GTaskSyncService.isSyncing()) {
|
|
|
|
|
lastSyncTimeView.setText(GTaskSyncService.getProgressString());
|
|
|
|
|
lastSyncTimeView.setVisibility(View.VISIBLE);
|
|
|
|
@ -193,14 +209,21 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 刷新用户界面
|
|
|
|
|
*/
|
|
|
|
|
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));
|
|
|
|
@ -217,6 +240,7 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
mHasAddedAccount = false;
|
|
|
|
|
|
|
|
|
|
if (accounts.length > 0) {
|
|
|
|
|
// 设置账户列表选项
|
|
|
|
|
CharSequence[] items = new CharSequence[accounts.length];
|
|
|
|
|
final CharSequence[] itemMapping = items;
|
|
|
|
|
int checkedItem = -1;
|
|
|
|
@ -237,6 +261,7 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加"添加账户"视图
|
|
|
|
|
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null);
|
|
|
|
|
dialogBuilder.setView(addAccountView);
|
|
|
|
|
|
|
|
|
@ -244,9 +269,10 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
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"
|
|
|
|
|
"gmail-ls"
|
|
|
|
|
});
|
|
|
|
|
startActivityForResult(intent, -1);
|
|
|
|
|
dialog.dismiss();
|
|
|
|
@ -254,9 +280,13 @@ 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 +295,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),
|
|
|
|
@ -283,11 +314,19 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
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);
|
|
|
|
@ -299,10 +338,10 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}
|
|
|
|
|
editor.commit();
|
|
|
|
|
|
|
|
|
|
// clean up last sync time
|
|
|
|
|
// 清除上次同步时间
|
|
|
|
|
setLastSyncTime(this, 0);
|
|
|
|
|
|
|
|
|
|
// clean up local gtask related info
|
|
|
|
|
// 清理本地GTask相关信息(在后台线程执行)
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
@ -318,6 +357,9 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移除同步账户
|
|
|
|
|
*/
|
|
|
|
|
private void removeSyncAccount() {
|
|
|
|
|
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
|
|
|
|
SharedPreferences.Editor editor = settings.edit();
|
|
|
|
@ -329,7 +371,7 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}
|
|
|
|
|
editor.commit();
|
|
|
|
|
|
|
|
|
|
// clean up local gtask related info
|
|
|
|
|
// 清理本地GTask相关信息(在后台线程执行)
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
@ -340,12 +382,22 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}).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);
|
|
|
|
@ -354,29 +406,41 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
editor.commit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取上次同步时间
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
* @return 上次同步时间戳(毫秒),若无则返回0
|
|
|
|
|
*/
|
|
|
|
|
public static long getLastSyncTime(Context context) {
|
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
|
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GTask同步服务广播接收器
|
|
|
|
|
*/
|
|
|
|
|
private class GTaskReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
refreshUI();
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 选项菜单点击处理
|
|
|
|
|
* @param item 点击的菜单项
|
|
|
|
|
* @return 是否处理该点击
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
@ -385,4 +449,4 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|