|
|
|
@ -47,43 +47,47 @@ import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
import net.micode.notes.gtask.remote.GTaskSyncService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 笔记应用的设置Activity,继承自PreferenceActivity
|
|
|
|
|
* 主要处理与Google Task同步相关的账户设置和同步操作
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
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 */
|
|
|
|
|
// 在ActionBar上显示返回按钮
|
|
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
|
|
|
|
|
// 从XML资源添加偏好设置
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
@ -92,11 +96,11 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
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) {
|
|
|
|
@ -113,19 +117,23 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
refreshUI();
|
|
|
|
|
refreshUI(); // 刷新UI
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
// 注销广播接收器
|
|
|
|
|
if (mReceiver != null) {
|
|
|
|
|
unregisterReceiver(mReceiver);
|
|
|
|
|
}
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加载账户偏好设置
|
|
|
|
|
*/
|
|
|
|
|
private void loadAccountPreference() {
|
|
|
|
|
mAccountCategory.removeAll();
|
|
|
|
|
mAccountCategory.removeAll(); // 清除所有现有偏好
|
|
|
|
|
|
|
|
|
|
Preference accountPref = new Preference(this);
|
|
|
|
|
final String defaultAccount = getSyncAccountName(this);
|
|
|
|
@ -135,48 +143,52 @@ 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;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mAccountCategory.addPreference(accountPref);
|
|
|
|
|
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);
|
|
|
|
|
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);
|
|
|
|
|
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);
|
|
|
|
@ -193,14 +205,21 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 刷新UI
|
|
|
|
|
*/
|
|
|
|
|
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));
|
|
|
|
@ -210,13 +229,14 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
dialogBuilder.setCustomTitle(titleView);
|
|
|
|
|
dialogBuilder.setPositiveButton(null, null);
|
|
|
|
|
|
|
|
|
|
Account[] accounts = getGoogleAccounts();
|
|
|
|
|
Account[] accounts = getGoogleAccounts(); // 获取所有Google账户
|
|
|
|
|
String defAccount = getSyncAccountName(this);
|
|
|
|
|
|
|
|
|
|
mOriAccounts = accounts;
|
|
|
|
|
mHasAddedAccount = false;
|
|
|
|
|
mOriAccounts = accounts; // 保存原始账户列表
|
|
|
|
|
mHasAddedAccount = false; // 重置添加账户标志
|
|
|
|
|
|
|
|
|
|
if (accounts.length > 0) {
|
|
|
|
|
// 创建账户选择列表
|
|
|
|
|
CharSequence[] items = new CharSequence[accounts.length];
|
|
|
|
|
final CharSequence[] itemMapping = items;
|
|
|
|
|
int checkedItem = -1;
|
|
|
|
@ -230,6 +250,7 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
dialogBuilder.setSingleChoiceItems(items, checkedItem,
|
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
// 设置选择的账户
|
|
|
|
|
setSyncAccount(itemMapping[which].toString());
|
|
|
|
|
dialog.dismiss();
|
|
|
|
|
refreshUI();
|
|
|
|
@ -237,6 +258,7 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加"添加账户"视图
|
|
|
|
|
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null);
|
|
|
|
|
dialogBuilder.setView(addAccountView);
|
|
|
|
|
|
|
|
|
@ -244,9 +266,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" // 只显示Gmail账户
|
|
|
|
|
});
|
|
|
|
|
startActivityForResult(intent, -1);
|
|
|
|
|
dialog.dismiss();
|
|
|
|
@ -254,9 +277,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 +292,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 +301,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,11 +313,17 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
dialogBuilder.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取所有Google账户
|
|
|
|
|
*/
|
|
|
|
|
private Account[] getGoogleAccounts() {
|
|
|
|
|
AccountManager accountManager = AccountManager.get(this);
|
|
|
|
|
return accountManager.getAccountsByType("com.google");
|
|
|
|
|
return accountManager.getAccountsByType("com.google"); // 只获取Google账户
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置同步账户
|
|
|
|
|
*/
|
|
|
|
|
private void setSyncAccount(String account) {
|
|
|
|
|
if (!getSyncAccountName(this).equals(account)) {
|
|
|
|
|
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
|
|
|
@ -299,10 +335,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 +354,9 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移除同步账户
|
|
|
|
|
*/
|
|
|
|
|
private void removeSyncAccount() {
|
|
|
|
|
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
|
|
|
|
SharedPreferences.Editor editor = settings.edit();
|
|
|
|
@ -329,7 +368,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 +379,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);
|
|
|
|
@ -354,29 +399,36 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GTask同步服务广播接收器
|
|
|
|
|
*/
|
|
|
|
|
private class GTaskReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
refreshUI();
|
|
|
|
|
refreshUI(); // 刷新UI
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
|
case android.R.id.home:
|
|
|
|
|
// 点击返回按钮,返回到笔记列表Activity
|
|
|
|
|
Intent intent = new Intent(this, NotesListActivity.class);
|
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
|
startActivity(intent);
|
|
|
|
@ -385,4 +437,4 @@ public class NotesPreferenceActivity extends PreferenceActivity {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|