Compare commits

..

No commits in common. 'master' and 'feature/add-note-encryption' have entirely different histories.

@ -14,158 +14,98 @@
* limitations under the License. * limitations under the License.
*/ */
// 包声明,表明该类所在的包路径
package net.micode.notes.ui; package net.micode.notes.ui;
// 导入用于获取日期格式符号的类
import java.text.DateFormatSymbols;
// 导入用于处理日期和时间的日历类
import java.util.Calendar;
// 导入项目资源文件类
import net.micode.notes.R;
// 导入Android上下文类用于获取应用程序的环境信息
import android.content.Context; import android.content.Context;
// 导入Android用于处理日期格式的类
import android.text.format.DateFormat; import android.text.format.DateFormat;
// 导入Android视图类是所有视图组件的基类
import android.view.View; import android.view.View;
// 导入Android帧布局类用于将子视图堆叠在左上角
import android.widget.FrameLayout; import android.widget.FrameLayout;
// 导入Android数字选择器类用于选择数字
import android.widget.NumberPicker; import android.widget.NumberPicker;
/** import net.micode.notes.R;
* FrameLayout
* / import java.text.DateFormatSymbols;
*/ import java.util.Calendar;
public class DateTimePicker extends FrameLayout { public class DateTimePicker extends FrameLayout {
// 默认的启用状态,初始为启用
private static final boolean DEFAULT_ENABLE_STATE = true; private static final boolean DEFAULT_ENABLE_STATE = true;
// 半天的小时数
private static final int HOURS_IN_HALF_DAY = 12; private static final int HOURS_IN_HALF_DAY = 12;
// 一整天的小时数
private static final int HOURS_IN_ALL_DAY = 24; private static final int HOURS_IN_ALL_DAY = 24;
// 一周的天数
private static final int DAYS_IN_ALL_WEEK = 7; private static final int DAYS_IN_ALL_WEEK = 7;
// 日期选择器的最小值
private static final int DATE_SPINNER_MIN_VAL = 0; private static final int DATE_SPINNER_MIN_VAL = 0;
// 日期选择器的最大值
private static final int DATE_SPINNER_MAX_VAL = DAYS_IN_ALL_WEEK - 1; private static final int DATE_SPINNER_MAX_VAL = DAYS_IN_ALL_WEEK - 1;
// 24小时制下小时选择器的最小值
private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0; private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0;
// 24小时制下小时选择器的最大值
private static final int HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW = 23; private static final int HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW = 23;
// 12小时制下小时选择器的最小值
private static final int HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW = 1; private static final int HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW = 1;
// 12小时制下小时选择器的最大值
private static final int HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW = 12; private static final int HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW = 12;
// 分钟选择器的最小值
private static final int MINUT_SPINNER_MIN_VAL = 0; private static final int MINUT_SPINNER_MIN_VAL = 0;
// 分钟选择器的最大值
private static final int MINUT_SPINNER_MAX_VAL = 59; private static final int MINUT_SPINNER_MAX_VAL = 59;
// 上午/下午选择器的最小值
private static final int AMPM_SPINNER_MIN_VAL = 0; private static final int AMPM_SPINNER_MIN_VAL = 0;
// 上午/下午选择器的最大值
private static final int AMPM_SPINNER_MAX_VAL = 1; private static final int AMPM_SPINNER_MAX_VAL = 1;
// 日期选择器组件
private final NumberPicker mDateSpinner; private final NumberPicker mDateSpinner;
// 小时选择器组件
private final NumberPicker mHourSpinner; private final NumberPicker mHourSpinner;
// 分钟选择器组件
private final NumberPicker mMinuteSpinner; private final NumberPicker mMinuteSpinner;
// 上午/下午选择器组件
private final NumberPicker mAmPmSpinner; private final NumberPicker mAmPmSpinner;
// 用于存储当前选择的日期和时间的日历对象
private Calendar mDate; private Calendar mDate;
// 用于存储日期选择器显示的日期值数组
private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK]; private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK];
// 标记是否为上午
private boolean mIsAm; private boolean mIsAm;
// 标记是否为24小时制显示
private boolean mIs24HourView; private boolean mIs24HourView;
// 标记选择器是否启用
private boolean mIsEnabled = DEFAULT_ENABLE_STATE; private boolean mIsEnabled = DEFAULT_ENABLE_STATE;
// 标记是否处于初始化状态
private boolean mInitialising; private boolean mInitialising;
// 日期时间改变监听器,用于在日期时间改变时触发回调
private OnDateTimeChangedListener mOnDateTimeChangedListener; private OnDateTimeChangedListener mOnDateTimeChangedListener;
// 日期选择器值改变监听器,当日期选择器的值改变时触发
private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() { private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() {
@Override @Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// 根据新值和旧值的差值更新日历的日期
mDate.add(Calendar.DAY_OF_YEAR, newVal - oldVal); mDate.add(Calendar.DAY_OF_YEAR, newVal - oldVal);
// 更新日期选择器的显示
updateDateControl(); updateDateControl();
// 触发日期时间改变事件
onDateTimeChanged(); onDateTimeChanged();
} }
}; };
// 小时选择器值改变监听器,当小时选择器的值改变时触发
private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() { private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
@Override @Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// 标记日期是否改变
boolean isDateChanged = false; boolean isDateChanged = false;
// 创建一个新的日历对象
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
// 如果不是24小时制
if (!mIs24HourView) { if (!mIs24HourView) {
// 处理从晚上11点到中午12点的情况
if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) { if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) {
cal.setTimeInMillis(mDate.getTimeInMillis()); cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, 1); cal.add(Calendar.DAY_OF_YEAR, 1);
isDateChanged = true; isDateChanged = true;
} } else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
// 处理从中午12点到晚上11点的情况
else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
cal.setTimeInMillis(mDate.getTimeInMillis()); cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, -1); cal.add(Calendar.DAY_OF_YEAR, -1);
isDateChanged = true; isDateChanged = true;
} }
// 处理从11点到12点或从12点到11点的情况切换上午/下午状态
if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY || if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY ||
oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) { oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
mIsAm = !mIsAm; mIsAm = !mIsAm;
// 更新上午/下午选择器的显示
updateAmPmControl(); updateAmPmControl();
} }
} } else {
// 如果是24小时制
else {
// 处理从晚上23点到凌晨0点的情况
if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) { if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) {
cal.setTimeInMillis(mDate.getTimeInMillis()); cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, 1); cal.add(Calendar.DAY_OF_YEAR, 1);
isDateChanged = true; isDateChanged = true;
} } else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) {
// 处理从凌晨0点到晚上23点的情况
else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) {
cal.setTimeInMillis(mDate.getTimeInMillis()); cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, -1); cal.add(Calendar.DAY_OF_YEAR, -1);
isDateChanged = true; isDateChanged = true;
} }
} }
// 计算新的小时值
int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY); int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY);
// 更新日历的小时值
mDate.set(Calendar.HOUR_OF_DAY, newHour); mDate.set(Calendar.HOUR_OF_DAY, newHour);
// 触发日期时间改变事件
onDateTimeChanged(); onDateTimeChanged();
// 如果日期发生改变,更新年、月、日
if (isDateChanged) { if (isDateChanged) {
setCurrentYear(cal.get(Calendar.YEAR)); setCurrentYear(cal.get(Calendar.YEAR));
setCurrentMonth(cal.get(Calendar.MONTH)); setCurrentMonth(cal.get(Calendar.MONTH));
@ -174,35 +114,22 @@ public class DateTimePicker extends FrameLayout {
} }
}; };
// 分钟选择器值改变监听器,当分钟选择器的值改变时触发
private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() { private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
@Override @Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// 获取分钟选择器的最小值
int minValue = mMinuteSpinner.getMinValue(); int minValue = mMinuteSpinner.getMinValue();
// 获取分钟选择器的最大值
int maxValue = mMinuteSpinner.getMaxValue(); int maxValue = mMinuteSpinner.getMaxValue();
// 偏移量,用于处理分钟进位或退位
int offset = 0; int offset = 0;
// 处理从59分钟到0分钟的情况
if (oldVal == maxValue && newVal == minValue) { if (oldVal == maxValue && newVal == minValue) {
offset += 1; offset += 1;
} } else if (oldVal == minValue && newVal == maxValue) {
// 处理从0分钟到59分钟的情况
else if (oldVal == minValue && newVal == maxValue) {
offset -= 1; offset -= 1;
} }
// 如果有偏移量
if (offset != 0) { if (offset != 0) {
// 更新日历的小时值
mDate.add(Calendar.HOUR_OF_DAY, offset); mDate.add(Calendar.HOUR_OF_DAY, offset);
// 更新小时选择器的值
mHourSpinner.setValue(getCurrentHour()); mHourSpinner.setValue(getCurrentHour());
// 更新日期选择器的显示
updateDateControl(); updateDateControl();
// 获取当前的小时值
int newHour = getCurrentHourOfDay(); int newHour = getCurrentHourOfDay();
// 根据小时值更新上午/下午状态
if (newHour >= HOURS_IN_HALF_DAY) { if (newHour >= HOURS_IN_HALF_DAY) {
mIsAm = false; mIsAm = false;
updateAmPmControl(); updateAmPmControl();
@ -211,302 +138,219 @@ public class DateTimePicker extends FrameLayout {
updateAmPmControl(); updateAmPmControl();
} }
} }
// 更新日历的分钟值
mDate.set(Calendar.MINUTE, newVal); mDate.set(Calendar.MINUTE, newVal);
// 触发日期时间改变事件
onDateTimeChanged(); onDateTimeChanged();
} }
}; };
// 上午/下午选择器值改变监听器,当上午/下午选择器的值改变时触发
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() { private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
@Override @Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// 切换上午/下午状态
mIsAm = !mIsAm; mIsAm = !mIsAm;
// 根据上午/下午状态更新日历的小时值
if (mIsAm) { if (mIsAm) {
mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY); mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY);
} else { } else {
mDate.add(Calendar.HOUR_OF_DAY, HOURS_IN_HALF_DAY); mDate.add(Calendar.HOUR_OF_DAY, HOURS_IN_HALF_DAY);
} }
// 更新上午/下午选择器的显示
updateAmPmControl(); updateAmPmControl();
// 触发日期时间改变事件
onDateTimeChanged(); onDateTimeChanged();
} }
}; };
/**
*
*/
public interface OnDateTimeChangedListener { public interface OnDateTimeChangedListener {
/**
*
* @param view
* @param year
* @param month
* @param dayOfMonth
* @param hourOfDay
* @param minute
*/
void onDateTimeChanged(DateTimePicker view, int year, int month, void onDateTimeChanged(DateTimePicker view, int year, int month,
int dayOfMonth, int hourOfDay, int minute); int dayOfMonth, int hourOfDay, int minute);
} }
/**
* 使
* @param context
*/
public DateTimePicker(Context context) { public DateTimePicker(Context context) {
// 调用另一个构造函数,使用当前时间戳
this(context, System.currentTimeMillis()); this(context, System.currentTimeMillis());
} }
/**
* 使
* @param context
* @param date
*/
public DateTimePicker(Context context, long date) { public DateTimePicker(Context context, long date) {
// 调用另一个构造函数同时判断是否为24小时制
this(context, date, DateFormat.is24HourFormat(context)); this(context, date, DateFormat.is24HourFormat(context));
} }
/**
* 使24
* @param context
* @param date
* @param is24HourView 24
*/
public DateTimePicker(Context context, long date, boolean is24HourView) { public DateTimePicker(Context context, long date, boolean is24HourView) {
// 调用父类的构造函数
super(context); super(context);
// 初始化日历对象
mDate = Calendar.getInstance(); mDate = Calendar.getInstance();
// 标记为初始化状态
mInitialising = true; mInitialising = true;
// 判断当前是否为上午
mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY; mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY;
// 加载布局文件
inflate(context, R.layout.datetime_picker, this); inflate(context, R.layout.datetime_picker, this);
// 初始化日期选择器
mDateSpinner = (NumberPicker) findViewById(R.id.date); mDateSpinner = (NumberPicker) findViewById(R.id.date);
mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL); mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL);
mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL); mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL);
mDateSpinner.setOnValueChangedListener(mOnDateChangedListener); mDateSpinner.setOnValueChangedListener(mOnDateChangedListener);
// 初始化小时选择器
mHourSpinner = (NumberPicker) findViewById(R.id.hour); mHourSpinner = (NumberPicker) findViewById(R.id.hour);
mHourSpinner.setOnValueChangedListener(mOnHourChangedListener); mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
// 初始化分钟选择器
mMinuteSpinner = (NumberPicker) findViewById(R.id.minute); mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL); mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL);
mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL); mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL);
mMinuteSpinner.setOnLongPressUpdateInterval(100); mMinuteSpinner.setOnLongPressUpdateInterval(100);
mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener); mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
// 获取上午/下午的显示字符串数组
String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings(); String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings();
// 初始化上午/下午选择器
mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm); mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm);
mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL); mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL);
mAmPmSpinner.setMaxValue(AMPM_SPINNER_MAX_VAL); mAmPmSpinner.setMaxValue(AMPM_SPINNER_MAX_VAL);
mAmPmSpinner.setDisplayedValues(stringsForAmPm); mAmPmSpinner.setDisplayedValues(stringsForAmPm);
mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener); mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener);
// 更新日期选择器的显示 // update controls to initial state
updateDateControl(); updateDateControl();
// 更新小时选择器的显示
updateHourControl(); updateHourControl();
// 更新上午/下午选择器的显示
updateAmPmControl(); updateAmPmControl();
// 设置是否为24小时制
set24HourView(is24HourView); set24HourView(is24HourView);
// 设置当前日期和时间 // set to current time
setCurrentDate(date); setCurrentDate(date);
// 设置选择器的启用状态
setEnabled(isEnabled()); setEnabled(isEnabled());
// 设置内容描述 // set the content descriptions
mInitialising = false; mInitialising = false;
} }
/**
*
* @param enabled
*/
@Override @Override
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
// 如果当前状态和要设置的状态相同,直接返回
if (mIsEnabled == enabled) { if (mIsEnabled == enabled) {
return; return;
} }
// 调用父类的方法设置启用状态
super.setEnabled(enabled); super.setEnabled(enabled);
// 设置日期选择器的启用状态
mDateSpinner.setEnabled(enabled); mDateSpinner.setEnabled(enabled);
// 设置分钟选择器的启用状态
mMinuteSpinner.setEnabled(enabled); mMinuteSpinner.setEnabled(enabled);
// 设置小时选择器的启用状态
mHourSpinner.setEnabled(enabled); mHourSpinner.setEnabled(enabled);
// 设置上午/下午选择器的启用状态
mAmPmSpinner.setEnabled(enabled); mAmPmSpinner.setEnabled(enabled);
// 更新当前的启用状态
mIsEnabled = enabled; mIsEnabled = enabled;
} }
/**
*
* @return
*/
@Override @Override
public boolean isEnabled() { public boolean isEnabled() {
return mIsEnabled; return mIsEnabled;
} }
/** /**
* * Get the current date in millis
* @return *
* @return the current date in millis
*/ */
public long getCurrentDateInTimeMillis() { public long getCurrentDateInTimeMillis() {
return mDate.getTimeInMillis(); return mDate.getTimeInMillis();
} }
/** /**
* * Set the current date
* @param date *
* @param date The current date in millis
*/ */
public void setCurrentDate(long date) { public void setCurrentDate(long date) {
// 创建一个新的日历对象
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
// 设置日历对象的时间
cal.setTimeInMillis(date); cal.setTimeInMillis(date);
// 调用另一个方法设置年、月、日、小时、分钟
setCurrentDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), setCurrentDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE)); cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
} }
/** /**
* * Set the current date
* @param year *
* @param month * @param year The current year
* @param dayOfMonth * @param month The current month
* @param hourOfDay * @param dayOfMonth The current dayOfMonth
* @param minute * @param hourOfDay The current hourOfDay
* @param minute The current minute
*/ */
public void setCurrentDate(int year, int month, public void setCurrentDate(int year, int month,
int dayOfMonth, int hourOfDay, int minute) { int dayOfMonth, int hourOfDay, int minute) {
// 设置年份
setCurrentYear(year); setCurrentYear(year);
// 设置月份
setCurrentMonth(month); setCurrentMonth(month);
// 设置日期
setCurrentDay(dayOfMonth); setCurrentDay(dayOfMonth);
// 设置小时
setCurrentHour(hourOfDay); setCurrentHour(hourOfDay);
// 设置分钟
setCurrentMinute(minute); setCurrentMinute(minute);
} }
/** /**
* * Get current year
* @return *
* @return The current year
*/ */
public int getCurrentYear() { public int getCurrentYear() {
return mDate.get(Calendar.YEAR); return mDate.get(Calendar.YEAR);
} }
/** /**
* * Set current year
* @param year *
* @param year The current year
*/ */
public void setCurrentYear(int year) { public void setCurrentYear(int year) {
// 如果不是初始化状态且年份没有改变,直接返回
if (!mInitialising && year == getCurrentYear()) { if (!mInitialising && year == getCurrentYear()) {
return; return;
} }
// 设置日历对象的年份
mDate.set(Calendar.YEAR, year); mDate.set(Calendar.YEAR, year);
// 更新日期选择器的显示
updateDateControl(); updateDateControl();
// 触发日期时间改变事件
onDateTimeChanged(); onDateTimeChanged();
} }
/** /**
* * Get current month in the year
* @return *
* @return The current month in the year
*/ */
public int getCurrentMonth() { public int getCurrentMonth() {
return mDate.get(Calendar.MONTH); return mDate.get(Calendar.MONTH);
} }
/** /**
* * Set current month in the year
* @param month *
* @param month The month in the year
*/ */
public void setCurrentMonth(int month) { public void setCurrentMonth(int month) {
// 如果不是初始化状态且月份没有改变,直接返回
if (!mInitialising && month == getCurrentMonth()) { if (!mInitialising && month == getCurrentMonth()) {
return; return;
} }
// 设置日历对象的月份
mDate.set(Calendar.MONTH, month); mDate.set(Calendar.MONTH, month);
// 更新日期选择器的显示
updateDateControl(); updateDateControl();
// 触发日期时间改变事件
onDateTimeChanged(); onDateTimeChanged();
} }
/** /**
* * Get current day of the month
* @return *
* @return The day of the month
*/ */
public int getCurrentDay() { public int getCurrentDay() {
return mDate.get(Calendar.DAY_OF_MONTH); return mDate.get(Calendar.DAY_OF_MONTH);
} }
/** /**
* * Set current day of the month
* @param dayOfMonth *
* @param dayOfMonth The day of the month
*/ */
public void setCurrentDay(int dayOfMonth) { public void setCurrentDay(int dayOfMonth) {
// 如果不是初始化状态且日期没有改变,直接返回
if (!mInitialising && dayOfMonth == getCurrentDay()) { if (!mInitialising && dayOfMonth == getCurrentDay()) {
return; return;
} }
// 设置日历对象的日期
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
// 更新日期选择器的显示
updateDateControl(); updateDateControl();
// 触发日期时间改变事件
onDateTimeChanged(); onDateTimeChanged();
} }
/** /**
* 24 * Get current hour in 24 hour mode, in the range (0~23)
* @return 24 * @return The current hour in 24 hour mode
*/ */
public int getCurrentHourOfDay() { public int getCurrentHourOfDay() {
return mDate.get(Calendar.HOUR_OF_DAY); return mDate.get(Calendar.HOUR_OF_DAY);
} }
/**
* 24
* @return
*/
private int getCurrentHour() { private int getCurrentHour() {
// 如果是24小时制
if (mIs24HourView){ if (mIs24HourView){
return getCurrentHourOfDay(); return getCurrentHourOfDay();
} } else {
// 如果是12小时制
else {
int hour = getCurrentHourOfDay(); int hour = getCurrentHourOfDay();
if (hour > HOURS_IN_HALF_DAY) { if (hour > HOURS_IN_HALF_DAY) {
return hour - HOURS_IN_HALF_DAY; return hour - HOURS_IN_HALF_DAY;
@ -517,19 +361,16 @@ public class DateTimePicker extends FrameLayout {
} }
/** /**
* 24 * Set current hour in 24 hour mode, in the range (0~23)
* @param hourOfDay 24 *
* @param hourOfDay
*/ */
public void setCurrentHour(int hourOfDay) { public void setCurrentHour(int hourOfDay) {
// 如果不是初始化状态且小时值没有改变,直接返回
if (!mInitialising && hourOfDay == getCurrentHourOfDay()) { if (!mInitialising && hourOfDay == getCurrentHourOfDay()) {
return; return;
} }
// 设置日历对象的小时值
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
// 如果不是24小时制
if (!mIs24HourView) { if (!mIs24HourView) {
// 根据小时值判断是否为上午
if (hourOfDay >= HOURS_IN_HALF_DAY) { if (hourOfDay >= HOURS_IN_HALF_DAY) {
mIsAm = false; mIsAm = false;
if (hourOfDay > HOURS_IN_HALF_DAY) { if (hourOfDay > HOURS_IN_HALF_DAY) {
@ -541,151 +382,103 @@ public class DateTimePicker extends FrameLayout {
hourOfDay = HOURS_IN_HALF_DAY; hourOfDay = HOURS_IN_HALF_DAY;
} }
} }
// 更新上午/下午选择器的显示
updateAmPmControl(); updateAmPmControl();
} }
// 设置小时选择器的值
mHourSpinner.setValue(hourOfDay); mHourSpinner.setValue(hourOfDay);
// 触发日期时间改变事件
onDateTimeChanged(); onDateTimeChanged();
} }
/** /**
* * Get currentMinute
* @return *
* @return The Current Minute
*/ */
public int getCurrentMinute() { public int getCurrentMinute() {
return mDate.get(Calendar.MINUTE); return mDate.get(Calendar.MINUTE);
} }
/** /**
* * Set current minute
* @param minute
*/ */
public void setCurrentMinute(int minute) { public void setCurrentMinute(int minute) {
// 如果不是初始化状态且分钟值没有改变,直接返回
if (!mInitialising && minute == getCurrentMinute()) { if (!mInitialising && minute == getCurrentMinute()) {
return; return;
} }
// 设置分钟选择器的值
mMinuteSpinner.setValue(minute); mMinuteSpinner.setValue(minute);
// 设置日历对象的分钟值
mDate.set(Calendar.MINUTE, minute); mDate.set(Calendar.MINUTE, minute);
// 触发日期时间改变事件
onDateTimeChanged(); onDateTimeChanged();
} }
/** /**
* 24 * @return true if this is in 24 hour view else false.
* @return 24
*/ */
public boolean is24HourView () { public boolean is24HourView () {
return mIs24HourView; return mIs24HourView;
} }
/** /**
* 24 * Set whether in 24 hour or AM/PM mode.
* @param is24HourView 24 *
* @param is24HourView True for 24 hour mode. False for AM/PM mode.
*/ */
public void set24HourView(boolean is24HourView) { public void set24HourView(boolean is24HourView) {
// 如果当前状态和要设置的状态相同,直接返回
if (mIs24HourView == is24HourView) { if (mIs24HourView == is24HourView) {
return; return;
} }
// 更新是否为24小时制的状态
mIs24HourView = is24HourView; mIs24HourView = is24HourView;
// 根据是否为24小时制设置上午/下午选择器的可见性
mAmPmSpinner.setVisibility(is24HourView ? View.GONE : View.VISIBLE); mAmPmSpinner.setVisibility(is24HourView ? View.GONE : View.VISIBLE);
// 获取当前的小时值
int hour = getCurrentHourOfDay(); int hour = getCurrentHourOfDay();
// 更新小时选择器的显示
updateHourControl(); updateHourControl();
// 设置当前的小时值
setCurrentHour(hour); setCurrentHour(hour);
// 更新上午/下午选择器的显示
updateAmPmControl(); updateAmPmControl();
} }
/**
*
*/
private void updateDateControl() { private void updateDateControl() {
// 创建一个新的日历对象
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
// 设置日历对象的时间
cal.setTimeInMillis(mDate.getTimeInMillis()); cal.setTimeInMillis(mDate.getTimeInMillis());
// 将日历对象的日期向前移动一周的一半加一天
cal.add(Calendar.DAY_OF_YEAR, -DAYS_IN_ALL_WEEK / 2 - 1); cal.add(Calendar.DAY_OF_YEAR, -DAYS_IN_ALL_WEEK / 2 - 1);
// 清空日期选择器的显示值
mDateSpinner.setDisplayedValues(null); mDateSpinner.setDisplayedValues(null);
// 循环一周的天数
for (int i = 0; i < DAYS_IN_ALL_WEEK; ++i) { for (int i = 0; i < DAYS_IN_ALL_WEEK; ++i) {
// 将日历对象的日期向后移动一天
cal.add(Calendar.DAY_OF_YEAR, 1); cal.add(Calendar.DAY_OF_YEAR, 1);
// 格式化日期并存储到显示值数组中
mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal); mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal);
} }
// 设置日期选择器的显示值
mDateSpinner.setDisplayedValues(mDateDisplayValues); mDateSpinner.setDisplayedValues(mDateDisplayValues);
// 设置日期选择器的当前值
mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2); mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2);
// 使日期选择器重绘
mDateSpinner.invalidate(); mDateSpinner.invalidate();
} }
/**
* /
*/
private void updateAmPmControl() { private void updateAmPmControl() {
// 如果是24小时制隐藏上午/下午选择器
if (mIs24HourView) { if (mIs24HourView) {
mAmPmSpinner.setVisibility(View.GONE); mAmPmSpinner.setVisibility(View.GONE);
} } else {
// 如果不是24小时制
else {
// 根据是否为上午设置选择器的值
int index = mIsAm ? Calendar.AM : Calendar.PM; int index = mIsAm ? Calendar.AM : Calendar.PM;
mAmPmSpinner.setValue(index); mAmPmSpinner.setValue(index);
// 显示上午/下午选择器
mAmPmSpinner.setVisibility(View.VISIBLE); mAmPmSpinner.setVisibility(View.VISIBLE);
} }
} }
/**
*
*/
private void updateHourControl() { private void updateHourControl() {
// 如果是24小时制
if (mIs24HourView) { if (mIs24HourView) {
// 设置小时选择器的最小值和最大值为24小时制的范围
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW); mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW);
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW); mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW);
} } else {
// 如果不是24小时制
else {
// 设置小时选择器的最小值和最大值为12小时制的范围
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW); mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW);
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW); mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW);
} }
} }
/** /**
* * Set the callback that indicates the 'Set' button has been pressed.
* @param callback * @param callback the callback, if null will do nothing
*/ */
public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) { public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) {
mOnDateTimeChangedListener = callback; mOnDateTimeChangedListener = callback;
} }
/**
*
*/
private void onDateTimeChanged() { private void onDateTimeChanged() {
// 如果监听器不为空
if (mOnDateTimeChangedListener != null) { if (mOnDateTimeChangedListener != null) {
// 调用监听器的回调方法
mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(), mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(),
getCurrentMonth(), getCurrentDay(), getCurrentHourOfDay(), getCurrentMinute()); getCurrentMonth(), getCurrentDay(), getCurrentHourOfDay(), getCurrentMinute());
} }
} }
} }

File diff suppressed because it is too large Load Diff

@ -14,204 +14,133 @@
* limitations under the License. * limitations under the License.
*/ */
// 包声明,表明该类属于 net.micode.notes.ui 包
package net.micode.notes.ui; package net.micode.notes.ui;
// 导入用于处理账户相关操作的类
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
// 导入用于操作 ActionBar 的类
import android.app.ActionBar;
// 导入用于显示对话框的类
import android.app.AlertDialog; import android.app.AlertDialog;
// 导入用于接收广播的类
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
// 导入用于存储键值对的类
import android.content.ContentValues; import android.content.ContentValues;
// 导入上下文类
import android.content.Context; import android.content.Context;
// 导入对话框点击事件接口
import android.content.DialogInterface; import android.content.DialogInterface;
// 导入用于启动其他组件的类
import android.content.Intent; import android.content.Intent;
// 导入用于过滤广播的类
import android.content.IntentFilter; import android.content.IntentFilter;
// 导入用于存储应用偏好设置的类
import android.content.SharedPreferences; import android.content.SharedPreferences;
// 导入用于保存和恢复 Activity 状态的类
import android.os.Bundle; import android.os.Bundle;
// 导入偏好设置项类
import android.preference.Preference; import android.preference.Preference;
// 导入偏好设置项点击事件监听器接口
import android.preference.Preference.OnPreferenceClickListener; import android.preference.Preference.OnPreferenceClickListener;
// 导入偏好设置 Activity 类
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
// 导入偏好设置分类类
import android.preference.PreferenceCategory; import android.preference.PreferenceCategory;
// 导入用于处理文本的工具类
import android.text.TextUtils; import android.text.TextUtils;
// 导入用于日期格式化的类
import android.text.format.DateFormat; import android.text.format.DateFormat;
// 导入用于加载布局的类
import android.view.LayoutInflater; import android.view.LayoutInflater;
// 导入菜单类
import android.view.Menu;
// 导入菜单项类
import android.view.MenuItem; import android.view.MenuItem;
// 导入视图类
import android.view.View; import android.view.View;
// 导入按钮类
import android.widget.Button; import android.widget.Button;
// 导入文本视图类
import android.widget.TextView; import android.widget.TextView;
// 导入用于显示提示信息的类
import android.widget.Toast; import android.widget.Toast;
// 导入资源类
import net.micode.notes.R; import net.micode.notes.R;
// 导入笔记数据相关类
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
// 导入 Google 任务同步服务类
import net.micode.notes.gtask.remote.GTaskSyncService; import net.micode.notes.gtask.remote.GTaskSyncService;
// 定义 NotesPreferenceActivity 类,继承自 PreferenceActivity
public class NotesPreferenceActivity extends PreferenceActivity { public class NotesPreferenceActivity extends PreferenceActivity {
// 定义偏好设置文件的名称
public static final String PREFERENCE_NAME = "notes_preferences"; 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_SYNC_ACCOUNT_NAME = "pref_key_account_name";
// 定义存储最后同步时间的偏好设置键
public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time"; 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"; 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 PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key";
// 定义账户权限过滤键
private static final String AUTHORITIES_FILTER_KEY = "authorities"; private static final String AUTHORITIES_FILTER_KEY = "authorities";
// 定义同步账户偏好设置分类对象
private PreferenceCategory mAccountCategory; private PreferenceCategory mAccountCategory;
// 定义 Google 任务广播接收器对象
private GTaskReceiver mReceiver; private GTaskReceiver mReceiver;
// 定义原始账户数组
private Account[] mOriAccounts; private Account[] mOriAccounts;
// 定义是否添加了新账户的标志
private boolean mHasAddedAccount; private boolean mHasAddedAccount;
// Activity 创建时调用的方法
@Override @Override
protected void onCreate(Bundle icicle) { protected void onCreate(Bundle icicle) {
// 调用父类的 onCreate 方法
super.onCreate(icicle); super.onCreate(icicle);
// 设置 ActionBar 可通过应用图标导航回上级页面 /* using the app icon for navigation */
getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true);
// 从 XML 文件中加载偏好设置
addPreferencesFromResource(R.xml.preferences); addPreferencesFromResource(R.xml.preferences);
// 通过键查找同步账户偏好设置分类对象
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY); mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY);
// 创建 Google 任务广播接收器对象
mReceiver = new GTaskReceiver(); mReceiver = new GTaskReceiver();
// 创建意图过滤器对象
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
// 添加要接收的广播动作
filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME); filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME);
// 注册广播接收器
registerReceiver(mReceiver, filter); registerReceiver(mReceiver, filter);
// 初始化原始账户数组为 null
mOriAccounts = null; mOriAccounts = null;
// 从布局文件中加载头部视图
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null); View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null);
// 将头部视图添加到列表视图中
getListView().addHeaderView(header, null, true); getListView().addHeaderView(header, null, true);
} }
// Activity 恢复时调用的方法
@Override @Override
protected void onResume() { protected void onResume() {
// 调用父类的 onResume 方法
super.onResume(); super.onResume();
// 如果添加了新账户 // need to set sync account automatically if user has added a new
// account
if (mHasAddedAccount) { if (mHasAddedAccount) {
// 获取当前的 Google 账户数组
Account[] accounts = getGoogleAccounts(); Account[] accounts = getGoogleAccounts();
// 如果原始账户数组不为空且当前账户数量大于原始账户数量
if (mOriAccounts != null && accounts.length > mOriAccounts.length) { if (mOriAccounts != null && accounts.length > mOriAccounts.length) {
// 遍历当前账户数组
for (Account accountNew : accounts) { for (Account accountNew : accounts) {
// 标记是否找到该账户
boolean found = false; boolean found = false;
// 遍历原始账户数组
for (Account accountOld : mOriAccounts) { for (Account accountOld : mOriAccounts) {
// 如果当前账户名称与原始账户名称相同
if (TextUtils.equals(accountOld.name, accountNew.name)) { if (TextUtils.equals(accountOld.name, accountNew.name)) {
// 标记已找到
found = true; found = true;
// 跳出内层循环
break; break;
} }
} }
// 如果未找到该账户
if (!found) { if (!found) {
// 设置该账户为同步账户
setSyncAccount(accountNew.name); setSyncAccount(accountNew.name);
// 跳出外层循环
break; break;
} }
} }
} }
} }
// 刷新界面
refreshUI(); refreshUI();
} }
// Activity 销毁时调用的方法
@Override @Override
protected void onDestroy() { protected void onDestroy() {
// 如果广播接收器不为空
if (mReceiver != null) { if (mReceiver != null) {
// 注销广播接收器
unregisterReceiver(mReceiver); unregisterReceiver(mReceiver);
} }
// 调用父类的 onDestroy 方法
super.onDestroy(); super.onDestroy();
} }
// 加载同步账户偏好设置项的方法
private void loadAccountPreference() { private void loadAccountPreference() {
// 移除同步账户偏好设置分类中的所有偏好设置项
mAccountCategory.removeAll(); mAccountCategory.removeAll();
// 创建一个新的偏好设置项
Preference accountPref = new Preference(this); Preference accountPref = new Preference(this);
// 获取默认同步账户名称
final String defaultAccount = getSyncAccountName(this); final String defaultAccount = getSyncAccountName(this);
// 设置偏好设置项的标题
accountPref.setTitle(getString(R.string.preferences_account_title)); accountPref.setTitle(getString(R.string.preferences_account_title));
// 设置偏好设置项的摘要
accountPref.setSummary(getString(R.string.preferences_account_summary)); accountPref.setSummary(getString(R.string.preferences_account_summary));
// 设置偏好设置项的点击事件监听器
accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
// 偏好设置项点击时调用的方法
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
// 如果当前没有正在同步
if (!GTaskSyncService.isSyncing()) { if (!GTaskSyncService.isSyncing()) {
// 如果默认同步账户名称为空
if (TextUtils.isEmpty(defaultAccount)) { if (TextUtils.isEmpty(defaultAccount)) {
// 显示选择账户的对话框 // the first time to set account
showSelectAccountAlertDialog(); showSelectAccountAlertDialog();
} else { } else {
// 如果账户已经设置,显示更改账户的确认对话框 // if the account has already been set, we need to promp
// user about the risk
showChangeAccountConfirmAlertDialog(); showChangeAccountConfirmAlertDialog();
} }
} else { } else {
// 显示不能更改账户的提示信息
Toast.makeText(NotesPreferenceActivity.this, 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(); .show();
@ -220,338 +149,222 @@ public class NotesPreferenceActivity extends PreferenceActivity {
} }
}); });
// 将偏好设置项添加到同步账户偏好设置分类中
mAccountCategory.addPreference(accountPref); mAccountCategory.addPreference(accountPref);
} }
// 加载同步按钮和最后同步时间显示的方法
private void loadSyncButton() { private void loadSyncButton() {
// 通过 ID 查找同步按钮
Button syncButton = (Button) findViewById(R.id.preference_sync_button); Button syncButton = (Button) findViewById(R.id.preference_sync_button);
// 通过 ID 查找最后同步时间显示的文本视图
TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview); TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
// 如果当前正在同步 // set button state
if (GTaskSyncService.isSyncing()) { if (GTaskSyncService.isSyncing()) {
// 设置同步按钮的文本为取消同步
syncButton.setText(getString(R.string.preferences_button_sync_cancel)); syncButton.setText(getString(R.string.preferences_button_sync_cancel));
// 设置同步按钮的点击事件监听器为取消同步
syncButton.setOnClickListener(new View.OnClickListener() { syncButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
GTaskSyncService.cancelSync(NotesPreferenceActivity.this); GTaskSyncService.cancelSync(NotesPreferenceActivity.this);
} }
}); });
} else { } else {
// 设置同步按钮的文本为立即同步
syncButton.setText(getString(R.string.preferences_button_sync_immediately)); syncButton.setText(getString(R.string.preferences_button_sync_immediately));
// 设置同步按钮的点击事件监听器为开始同步
syncButton.setOnClickListener(new View.OnClickListener() { syncButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
GTaskSyncService.startSync(NotesPreferenceActivity.this); GTaskSyncService.startSync(NotesPreferenceActivity.this);
} }
}); });
} }
// 如果同步账户名称不为空,启用同步按钮
syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this))); syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this)));
// 如果当前正在同步 // set last sync time
if (GTaskSyncService.isSyncing()) { if (GTaskSyncService.isSyncing()) {
// 设置最后同步时间显示的文本为同步进度信息
lastSyncTimeView.setText(GTaskSyncService.getProgressString()); lastSyncTimeView.setText(GTaskSyncService.getProgressString());
// 显示最后同步时间文本视图
lastSyncTimeView.setVisibility(View.VISIBLE); lastSyncTimeView.setVisibility(View.VISIBLE);
} else { } else {
// 获取最后同步时间
long lastSyncTime = getLastSyncTime(this); long lastSyncTime = getLastSyncTime(this);
// 如果最后同步时间不为 0
if (lastSyncTime != 0) { if (lastSyncTime != 0) {
// 设置最后同步时间显示的文本为格式化后的最后同步时间
lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time, lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time,
DateFormat.format(getString(R.string.preferences_last_sync_time_format), DateFormat.format(getString(R.string.preferences_last_sync_time_format),
lastSyncTime))); lastSyncTime)));
// 显示最后同步时间文本视图
lastSyncTimeView.setVisibility(View.VISIBLE); lastSyncTimeView.setVisibility(View.VISIBLE);
} else { } else {
// 隐藏最后同步时间文本视图
lastSyncTimeView.setVisibility(View.GONE); lastSyncTimeView.setVisibility(View.GONE);
} }
} }
} }
// 刷新界面的方法
private void refreshUI() { private void refreshUI() {
// 加载同步账户偏好设置项
loadAccountPreference(); loadAccountPreference();
// 加载同步按钮和最后同步时间显示
loadSyncButton(); loadSyncButton();
} }
// 显示选择账户对话框的方法
private void showSelectAccountAlertDialog() { private void showSelectAccountAlertDialog() {
// 创建对话框构建器对象
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// 从布局文件中加载对话框标题视图
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null); View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
// 通过 ID 查找对话框标题文本视图
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title); TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
// 设置对话框标题文本
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title)); titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
// 通过 ID 查找对话框副标题文本视图
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle); TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
// 设置对话框副标题文本
subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips)); subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips));
// 设置对话框的自定义标题视图
dialogBuilder.setCustomTitle(titleView); dialogBuilder.setCustomTitle(titleView);
// 设置对话框的确定按钮为空
dialogBuilder.setPositiveButton(null, null); dialogBuilder.setPositiveButton(null, null);
// 获取当前的 Google 账户数组
Account[] accounts = getGoogleAccounts(); Account[] accounts = getGoogleAccounts();
// 获取默认同步账户名称
String defAccount = getSyncAccountName(this); String defAccount = getSyncAccountName(this);
// 保存原始账户数组
mOriAccounts = accounts; mOriAccounts = accounts;
// 标记未添加新账户
mHasAddedAccount = false; mHasAddedAccount = false;
// 如果账户数组不为空
if (accounts.length > 0) { if (accounts.length > 0) {
// 创建字符序列数组用于存储账户名称
CharSequence[] items = new CharSequence[accounts.length]; CharSequence[] items = new CharSequence[accounts.length];
// 用于映射账户名称的字符序列数组
final CharSequence[] itemMapping = items; final CharSequence[] itemMapping = items;
// 标记默认选中的账户索引
int checkedItem = -1; int checkedItem = -1;
// 账户索引
int index = 0; int index = 0;
// 遍历账户数组
for (Account account : accounts) { for (Account account : accounts) {
// 如果当前账户名称与默认同步账户名称相同
if (TextUtils.equals(account.name, defAccount)) { if (TextUtils.equals(account.name, defAccount)) {
// 标记该账户为默认选中
checkedItem = index; checkedItem = index;
} }
// 将账户名称存储到字符序列数组中
items[index++] = account.name; items[index++] = account.name;
} }
// 设置对话框的单选列表项
dialogBuilder.setSingleChoiceItems(items, checkedItem, dialogBuilder.setSingleChoiceItems(items, checkedItem,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
// 单选列表项点击时调用的方法
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
// 设置选中的账户为同步账户
setSyncAccount(itemMapping[which].toString()); setSyncAccount(itemMapping[which].toString());
// 关闭对话框
dialog.dismiss(); dialog.dismiss();
// 刷新界面
refreshUI(); refreshUI();
} }
}); });
} }
// 从布局文件中加载添加账户视图
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null); View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null);
// 设置对话框的视图为添加账户视图
dialogBuilder.setView(addAccountView); dialogBuilder.setView(addAccountView);
// 显示对话框
final AlertDialog dialog = dialogBuilder.show(); final AlertDialog dialog = dialogBuilder.show();
// 设置添加账户视图的点击事件监听器
addAccountView.setOnClickListener(new View.OnClickListener() { addAccountView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
// 标记已添加新账户
mHasAddedAccount = true; mHasAddedAccount = true;
// 创建启动添加账户设置界面的意图
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS"); Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
// 添加账户权限过滤信息
intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] { intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] {
"gmail-ls" "gmail-ls"
}); });
// 启动添加账户设置界面并等待结果
startActivityForResult(intent, -1); startActivityForResult(intent, -1);
// 关闭对话框
dialog.dismiss(); dialog.dismiss();
} }
}); });
} }
// 显示更改账户确认对话框的方法
private void showChangeAccountConfirmAlertDialog() { private void showChangeAccountConfirmAlertDialog() {
// 创建对话框构建器对象
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// 从布局文件中加载对话框标题视图
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null); View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
// 通过 ID 查找对话框标题文本视图
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title); TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
// 设置对话框标题文本,包含当前同步账户名称
titleTextView.setText(getString(R.string.preferences_dialog_change_account_title, titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
getSyncAccountName(this))); getSyncAccountName(this)));
// 通过 ID 查找对话框副标题文本视图
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle); TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
// 设置对话框副标题文本为更改账户的警告信息
subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg)); subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg));
// 设置对话框的自定义标题视图
dialogBuilder.setCustomTitle(titleView); dialogBuilder.setCustomTitle(titleView);
// 创建菜单项字符序列数组
CharSequence[] menuItemArray = new CharSequence[] { CharSequence[] menuItemArray = new CharSequence[] {
getString(R.string.preferences_menu_change_account), getString(R.string.preferences_menu_change_account),
getString(R.string.preferences_menu_remove_account), getString(R.string.preferences_menu_remove_account),
getString(R.string.preferences_menu_cancel) getString(R.string.preferences_menu_cancel)
}; };
// 设置对话框的菜单项
dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() { dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() {
// 菜单项点击时调用的方法
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
// 如果点击的是更改账户菜单项
if (which == 0) { if (which == 0) {
// 显示选择账户对话框
showSelectAccountAlertDialog(); showSelectAccountAlertDialog();
} else if (which == 1) { } else if (which == 1) {
// 如果点击的是移除账户菜单项,移除同步账户
removeSyncAccount(); removeSyncAccount();
// 刷新界面
refreshUI(); refreshUI();
} }
} }
}); });
// 显示对话框
dialogBuilder.show(); dialogBuilder.show();
} }
// 获取 Google 账户数组的方法
private Account[] getGoogleAccounts() { private Account[] getGoogleAccounts() {
// 获取账户管理器对象
AccountManager accountManager = AccountManager.get(this); AccountManager accountManager = AccountManager.get(this);
// 通过账户类型获取 Google 账户数组
return accountManager.getAccountsByType("com.google"); return accountManager.getAccountsByType("com.google");
} }
// 设置同步账户的方法
private void setSyncAccount(String account) { private void setSyncAccount(String account) {
// 如果当前同步账户名称与要设置的账户名称不同
if (!getSyncAccountName(this).equals(account)) { if (!getSyncAccountName(this).equals(account)) {
// 获取偏好设置对象
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
// 获取偏好设置编辑器对象
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
// 如果要设置的账户名称不为空
if (account != null) { if (account != null) {
// 将账户名称存储到偏好设置中
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account); editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account);
} else { } else {
// 将账户名称设置为空字符串
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
} }
// 提交偏好设置更改
editor.commit(); editor.commit();
// 清除最后同步时间 // clean up last sync time
setLastSyncTime(this, 0); setLastSyncTime(this, 0);
// 开启一个新线程清理本地 Google 任务相关信息 // clean up local gtask related info
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
// 创建内容值对象
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
// 将 Google 任务 ID 设置为空字符串
values.put(NoteColumns.GTASK_ID, ""); values.put(NoteColumns.GTASK_ID, "");
// 将同步 ID 设置为 0
values.put(NoteColumns.SYNC_ID, 0); values.put(NoteColumns.SYNC_ID, 0);
// 更新笔记内容 URI 对应的所有记录
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null); getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
} }
}).start(); }).start();
// 显示设置账户成功的提示信息
Toast.makeText(NotesPreferenceActivity.this, Toast.makeText(NotesPreferenceActivity.this,
getString(R.string.preferences_toast_success_set_accout, account), getString(R.string.preferences_toast_success_set_accout, account),
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
} }
} }
// 移除同步账户的方法
private void removeSyncAccount() { private void removeSyncAccount() {
// 获取偏好设置对象
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
// 获取偏好设置编辑器对象
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
// 如果偏好设置中包含同步账户名称
if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) { if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) {
// 移除同步账户名称
editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME); editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME);
} }
// 如果偏好设置中包含最后同步时间
if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) { if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) {
// 移除最后同步时间
editor.remove(PREFERENCE_LAST_SYNC_TIME); editor.remove(PREFERENCE_LAST_SYNC_TIME);
} }
// 提交偏好设置更改
editor.commit(); editor.commit();
// 开启一个新线程清理本地 Google 任务相关信息 // clean up local gtask related info
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
// 创建内容值对象
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
// 将 Google 任务 ID 设置为空字符串
values.put(NoteColumns.GTASK_ID, ""); values.put(NoteColumns.GTASK_ID, "");
// 将同步 ID 设置为 0
values.put(NoteColumns.SYNC_ID, 0); values.put(NoteColumns.SYNC_ID, 0);
// 更新笔记内容 URI 对应的所有记录
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null); getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
} }
}).start(); }).start();
} }
// 获取同步账户名称的静态方法
public static String getSyncAccountName(Context context) { public static String getSyncAccountName(Context context) {
// 获取偏好设置对象
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE); Context.MODE_PRIVATE);
// 从偏好设置中获取同步账户名称,默认为空字符串
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
} }
// 设置最后同步时间的静态方法
public static void setLastSyncTime(Context context, long time) { public static void setLastSyncTime(Context context, long time) {
// 获取偏好设置对象
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE); Context.MODE_PRIVATE);
// 获取偏好设置编辑器对象
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
// 将最后同步时间存储到偏好设置中
editor.putLong(PREFERENCE_LAST_SYNC_TIME, time); editor.putLong(PREFERENCE_LAST_SYNC_TIME, time);
// 提交偏好设置更改
editor.commit(); editor.commit();
} }
// 获取最后同步时间的静态方法
public static long getLastSyncTime(Context context) { public static long getLastSyncTime(Context context) {
// 获取偏好设置对象
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE); Context.MODE_PRIVATE);
// 从偏好设置中获取最后同步时间,默认为 0
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0); return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0);
} }
// 定义 Google 任务广播接收器内部类
private class GTaskReceiver extends BroadcastReceiver { private class GTaskReceiver extends BroadcastReceiver {
// 接收到广播时调用的方法
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
// 刷新界面
refreshUI(); refreshUI();
// 如果广播中包含正在同步的标志
if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) { if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) {
// 通过 ID 查找同步状态文本视图
TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview); TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
// 设置同步状态文本视图的文本为广播中的进度信息
syncStatus.setText(intent syncStatus.setText(intent
.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG)); .getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG));
} }
@ -559,22 +372,15 @@ public class NotesPreferenceActivity extends PreferenceActivity {
} }
} }
// 菜单项选择事件处理方法
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
// 根据菜单项的 ID 进行不同处理
switch (item.getItemId()) { switch (item.getItemId()) {
// 如果点击的是返回上级页面的菜单项
case android.R.id.home: case android.R.id.home:
// 创建启动笔记列表 Activity 的意图
Intent intent = new Intent(this, NotesListActivity.class); Intent intent = new Intent(this, NotesListActivity.class);
// 添加清除顶部 Activity 的标志
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// 启动笔记列表 Activity
startActivity(intent); startActivity(intent);
return true; return true;
// 默认情况
default: default:
return false; return false;
} }
} }
} }

Loading…
Cancel
Save