You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
577 lines
22 KiB
577 lines
22 KiB
2 months ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
|
// 显示一个确认对话框,提示用户将更改同步账户
|
||
|
private void showChangeAccountConfirmAlertDialog() {
|
||
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
|
||
|
|
||
|
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
|
||
|
Te// 显示一个确认对话框,提示用户将更改同步账户
|
||
|
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)
|
||
|
};
|
||
|
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();
|
||
|
}
|
||
|
|
||
|
// 获取所有谷歌账户
|
||
|
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) {
|
||
|
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, "");
|
||
|
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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 移除同步账户
|
||
|
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();
|
||
|
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 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;
|
||
|
}
|
||
|
}
|
||
|
// 显示一个确认对话框,提示用户将更改同步账户
|
||
|
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)
|
||
|
};
|
||
|
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();
|
||
|
}
|
||
|
|
||
|
// 获取所有谷歌账户
|
||
|
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) {
|
||
|
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, "");
|
||
|
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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 移除同步账户
|
||
|
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();
|
||
|
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 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;
|
||
|
}
|
||
|
}
|
||
|
// 显示一个确认对话框,提示用户将更改同步账户
|
||
|
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)
|
||
|
};
|
||
|
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();
|
||
|
}
|
||
|
|
||
|
// 获取所有谷歌账户
|
||
|
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) {
|
||
|
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, "");
|
||
|
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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 移除同步账户
|
||
|
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();
|
||
|
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 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;
|
||
|
}
|
||
|
}
|
||
|
xtView 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)
|
||
|
};
|
||
|
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();
|
||
|
}
|
||
|
|
||
|
// 获取所有谷歌账户
|
||
|
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) {
|
||
|
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, "");
|
||
|
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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 移除同步账户
|
||
|
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();
|
||
|
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 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;
|
||
|
}
|
||
|
}
|