main
chenjianan 7 months ago
parent e5f863580e
commit c8d177a5b6

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
// 导入必要的类和接口
package net.micode.notes.ui; package net.micode.notes.ui;
import android.app.Activity; import android.app.Activity;
@ -39,147 +40,171 @@ import net.micode.notes.tool.DataUtils;
import java.io.IOException; import java.io.IOException;
/** // AlarmAlertActivity类继承自Activity实现OnClickListener和OnDismissListener接口
* AlarmAlertActivity
*/
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener { public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
private long mNoteId; // 便签的ID private long mNoteId; //文本在数据库存储中的ID号
private String mSnippet; // 便签的摘要信息 private String mSnippet; //闹钟提示时出现的文本片段
private static final int SNIPPET_PREW_MAX_LEN = 60; // 摘要信息的最大长度 private static final int SNIPPET_PREW_MAX_LEN = 60;
MediaPlayer mPlayer; // 用于播放提醒音的MediaPlayer MediaPlayer mPlayer;
@Override @Override
/**
* Activity
* @param savedInstanceState
*/
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题栏 //Bundle类型的数据与Map类型的数据相似都是以key-value的形式存储数据的
//onsaveInstanceState方法是用来保存Activity的状态的
//能从onCreate的参数savedInsanceState中获得状态数据
requestWindowFeature(Window.FEATURE_NO_TITLE);
//界面显示——无标题
final Window win = getWindow(); final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); // 锁屏时显示 win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
// 如果屏幕未点亮,则设置相关标志以确保屏幕点亮并显示对话框
if (!isScreenOn()) { if (!isScreenOn()) {
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | //保持窗体点亮
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR); //将窗体点亮
} | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
//允许窗体点亮时锁屏
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
}//在手机锁屏后如果到了闹钟提示时间,点亮屏幕
Intent intent = getIntent(); Intent intent = getIntent();
try { try {
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); // 获取便签ID mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); // 获取便签摘要 mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
// 如果摘要过长,则截取并添加省略号 //根据ID从数据库中获取标签的内容
//getContentResolver是实现数据共享实例存储。
mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0, mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0,
SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info) SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info)
: mSnippet; : mSnippet;
//判断标签片段是否达到符合长度
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
e.printStackTrace(); e.printStackTrace();
return; return;
} }
/*
try
{
// 代码区
}
catch(Exception e)
{
// 异常处理
}
*/
mPlayer = new MediaPlayer(); mPlayer = new MediaPlayer();
// 如果便签存在于数据库中,则显示对话框并播放提醒音
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) { if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
showActionDialog(); showActionDialog();
//弹出对话框
playAlarmSound(); playAlarmSound();
//闹钟提示音激发
} else { } else {
finish(); // 如果不存在则结束Activity finish();
//完成闹钟动作
} }
} }
/**
*
* @return
*/
private boolean isScreenOn() { private boolean isScreenOn() {
//判断屏幕是否锁屏,调用系统函数判断,最后返回值是布尔类型
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn(); return pm.isScreenOn();
} }
/**
*
*/
private void playAlarmSound() { private void playAlarmSound() {
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM); // 获取默认闹钟铃声 //闹钟提示音激发
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
//调用系统的铃声管理URI得到闹钟提示音
int silentModeStreams = Settings.System.getInt(getContentResolver(), int silentModeStreams = Settings.System.getInt(getContentResolver(),
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); // 获取静音模式影响的流类型 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) { if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
mPlayer.setAudioStreamType(silentModeStreams); // 设置音频流类型 mPlayer.setAudioStreamType(silentModeStreams);
} else { } else {
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
} }
try { try {
mPlayer.setDataSource(this, url); // 设置播放数据源 mPlayer.setDataSource(this, url);
mPlayer.prepare(); // 准备播放 //方法setDataSource(Context context, Uri uri)
mPlayer.setLooping(true); // 设置循环播放 //解释:无返回值,设置多媒体数据来源【根据 Uri】
mPlayer.start(); // 开始播放 mPlayer.prepare();
//准备同步
mPlayer.setLooping(true);
//设置是否循环播放
mPlayer.start();
//开始播放
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
//e.printStackTrace()函数功能是抛出异常, 还将显示出更深的调用信息
//System.out.println(e),这个方法打印出异常,并且输出在哪里出现的异常
} catch (SecurityException e) { } catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
/**
*
*/
private void showActionDialog() { private void showActionDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this); AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.app_name); // 设置对话框标题 //AlertDialog的构造方法全部是Protected的
dialog.setMessage(mSnippet); // 设置对话框消息 //所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。
dialog.setPositiveButton(R.string.notealert_ok, this); // 设置确定按钮 //要创建一个AlertDialog就要用到AlertDialog.Builder中的create()方法
//如这里的dialog就是新建了一个AlertDialog
dialog.setTitle(R.string.app_name);
//为对话框设置标题
dialog.setMessage(mSnippet);
//为对话框设置内容
dialog.setPositiveButton(R.string.notealert_ok, this);
//给对话框添加"Yes"按钮
if (isScreenOn()) { if (isScreenOn()) {
dialog.setNegativeButton(R.string.notealert_enter, this); // 设置取消按钮 dialog.setNegativeButton(R.string.notealert_enter, this);
} }//对话框添加"No"按钮
dialog.show().setOnDismissListener(this); // 显示对话框并设置消失监听器 dialog.show().setOnDismissListener(this);
} }
/**
*
* @param dialog
* @param which
*/
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
switch (which) { switch (which) {
//用which来选择click后下一步的操作
case DialogInterface.BUTTON_NEGATIVE: case DialogInterface.BUTTON_NEGATIVE:
//这是取消操作
Intent intent = new Intent(this, NoteEditActivity.class); Intent intent = new Intent(this, NoteEditActivity.class);
//实现两个类间的数据传输
intent.setAction(Intent.ACTION_VIEW); intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_UID, mNoteId); // 传递便签ID //设置动作属性
startActivity(intent); // 启动便签编辑Activity intent.putExtra(Intent.EXTRA_UID, mNoteId);
//实现key-value对
//EXTRA_UID为keymNoteId为键
startActivity(intent);
//开始动作
break; break;
default: default:
//这是确定操作
break; break;
} }
} }
/**
*
* @param dialog
*/
public void onDismiss(DialogInterface dialog) { public void onDismiss(DialogInterface dialog) {
stopAlarmSound(); // 停止播放提醒音 //忽略
finish(); // 结束Activity stopAlarmSound();
//停止闹钟声音
finish();
//完成该动作
} }
/**
*
*/
private void stopAlarmSound() { private void stopAlarmSound() {
if (mPlayer != null) { if (mPlayer != null) {
mPlayer.stop(); // 停止播放 mPlayer.stop();
mPlayer.release(); // 释放资源 //停止播放
mPlayer = null; // 清空MediaPlayer对象 mPlayer.release();
//释放MediaPlayer对象
mPlayer = null;
} }
} }
} }

@ -27,58 +27,63 @@ import android.database.Cursor;
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;
/**
* AlarmInitReceiver
*/
public class AlarmInitReceiver extends BroadcastReceiver { public class AlarmInitReceiver extends BroadcastReceiver {
// 查询数据库时要的列 // 定义查询数据库时要获取的列
private static final String [] PROJECTION = new String [] { private static final String [] PROJECTION = new String [] {
NoteColumns.ID, // 便签ID NoteColumns.ID, // 笔记的ID
NoteColumns.ALERTED_DATE // 闹钟提醒时间 NoteColumns.ALERTED_DATE // 笔记的提醒日期
}; };
// 列索引,方便获取数据 // 定义列的索引以便在Cursor中快速访问
private static final int COLUMN_ID = 0; private static final int COLUMN_ID = 0;
private static final int COLUMN_ALERTED_DATE = 1; private static final int COLUMN_ALERTED_DATE = 1;
/**
* 广
* @param context
* @param intent
*/
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
// 获取当前时间 long currentDate = System.currentTimeMillis(); // 获取当前的系统时间(毫秒)
long currentDate = System.currentTimeMillis();
// 查询所有未触发的闹钟 // 从内容提供者中查询需要提醒的笔记
// Notes.CONTENT_NOTE_URI 是内容提供者的URI
// PROJECTION 指定了要查询的列
// 查询条件是提醒日期大于当前日期且笔记类型为普通笔记Notes.TYPE_NOTE
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI, Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION, PROJECTION,
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE, NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
new String[] { String.valueOf(currentDate) }, new String[] { String.valueOf(currentDate) }, // 查询参数,这里只有当前日期
null); null); // 不需要排序
// 检查Cursor是否为null
if (c != null) { if (c != null) {
// 如果有未触发的闹钟 // 如果Cursor不为空移动到第一条记录
if (c.moveToFirst()) { if (c.moveToFirst()) {
// 开始遍历Cursor中的所有记录
do { do {
// 获取闹钟提醒时间和便签ID // 获取当前笔记的提醒日期
long alertDate = c.getLong(COLUMN_ALERTED_DATE); long alertDate = c.getLong(COLUMN_ALERTED_DATE);
// 创建一个Intent目标组件是AlarmReceiver
Intent sender = new Intent(context, AlarmReceiver.class); Intent sender = new Intent(context, AlarmReceiver.class);
// 为Intent设置数据URI这里使用ContentUris.withAppendedId方法将笔记ID附加到Notes的内容URI上
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID))); sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 创建PendingIntent // 创建一个PendingIntent这里使用getBroadcast方法因为AlarmReceiver是一个BroadcastReceiver
// 注意这里使用的请求码第二个参数为0这可能导致如果有多个相同的Intent被发送时PendingIntent会被覆盖
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 获取AlarmManager服务 // 获取AlarmManager服务
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); AlarmManager alermManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// 设置一个闹钟当到达提醒日期时触发PendingIntent即发送广播给AlarmReceiver
// AlarmManager.RTC_WAKEUP表示在指定时间唤醒设备并发送广播
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
// 设置闹钟 } while (c.moveToNext()); // 移动到下一条记录,继续循环
alarmManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext()); // 继续查询下一个闹钟
} }
c.close(); // 关闭游标 // 关闭Cursor
c.close();
} }
} }
} }

@ -14,28 +14,30 @@
* limitations under the License. * limitations under the License.
*/ */
// 指定类所在的包名
package net.micode.notes.ui; package net.micode.notes.ui;
import android.content.BroadcastReceiver; // 导入所需的类
import android.content.Context; import android.content.BroadcastReceiver; // 广播接收器基类
import android.content.Intent; import android.content.Context; // 提供应用环境信息的类
import android.content.Intent; // 用于组件间通信的消息传递对象
/** // 定义一个公开类AlarmReceiver它继承自BroadcastReceiver
* AlarmReceiver
*/
public class AlarmReceiver extends BroadcastReceiver { public class AlarmReceiver extends BroadcastReceiver {
/**
* 广 // 重写onReceive方法当接收到广播时调用此方法
* @param context
* @param intent
*/
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
// 设置意图的目标类为闹钟提醒界面 // 设置intent的目标组件为AlarmAlertActivity类
// 这意味着当这个广播接收器接收到广播时它希望启动AlarmAlertActivity活动
intent.setClass(context, AlarmAlertActivity.class); intent.setClass(context, AlarmAlertActivity.class);
// 添加标志,表示启动一个新的任务
// 为intent添加FLAG_ACTIVITY_NEW_TASK标志
// 这通常用于从非活动上下文中启动活动时,确保活动作为新的任务启动
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 启动闹钟提醒界面
// 使用context对象调用startActivity方法来启动由intent指定的活动
// 由于intent已经被设置为启动AlarmAlertActivity这行代码的作用就是启动AlarmAlertActivity活动
context.startActivity(intent); context.startActivity(intent);
} }
} }

@ -14,35 +14,31 @@
* limitations under the License. * limitations under the License.
*/ */
// 指定类所在的包名
package net.micode.notes.ui; package net.micode.notes.ui;
// 导入所需的Java和Android类
import java.text.DateFormatSymbols; import java.text.DateFormatSymbols;
import java.util.Calendar; import java.util.Calendar;
import net.micode.notes.R; import net.micode.notes.R;
import android.content.Context; import android.content.Context;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.view.View; import android.view.View;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.NumberPicker; import android.widget.NumberPicker;
// 定义一个名为DateTimePicker的类它继承自FrameLayout
/**
* DateTimePicker
*/
public class DateTimePicker extends FrameLayout { public class DateTimePicker extends FrameLayout {
//FrameLayout是布局模板之一
// 默认使能状态 //所有的子元素全部在屏幕的右上方
private static final boolean DEFAULT_ENABLE_STATE = true; private static final boolean DEFAULT_ENABLE_STATE = true;
// 12小时制和24小时制的小时数
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;
private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0; private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0;
@ -53,82 +49,77 @@ public class DateTimePicker extends FrameLayout {
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;
// AM/PM选择器
private final NumberPicker mAmPmSpinner; private final NumberPicker mAmPmSpinner;
// 当前日期 //NumberPicker是数字选择器
//这里定义的四个变量全部是在设置闹钟时需要选择的变量(如日期、时、分、上午或者下午)
private Calendar mDate; private Calendar mDate;
//定义了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();
} }
}; };//OnValueChangeListener这是时间改变监听器这里主要是对日期的监听
//将现在日期的值传递给mDateupdateDateControl是同步操作
// 小时改变监听器
private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() { private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
@Override //这里是对 小时Hour 的监听
@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();
//声明一个Calendar的变量cal便于后续的操作
if (!mIs24HourView) { if (!mIs24HourView) {
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;
//这里是对于12小时制时晚上11点和12点交替时对日期的更改
} else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) { } 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;
} }
//这里是对于12小时制时凌晨11点和12点交替时对日期的更改
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();
} }//这里是对于12小时制时中午11点和12点交替时对AM和PM的更改
} else { } else {
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;
//这里是对于24小时制时晚上11点和12点交替时对日期的更改
} else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) { } 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;
} }
} } //这里是对于12小时制时凌晨11点和12点交替时对日期的更改
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);
//通过数字选择器对newHour的赋值
mDate.set(Calendar.HOUR_OF_DAY, newHour); mDate.set(Calendar.HOUR_OF_DAY, newHour);
//通过set函数将新的Hour值传给mDate
onDateTimeChanged(); onDateTimeChanged();
if (isDateChanged) { if (isDateChanged) {
setCurrentYear(cal.get(Calendar.YEAR)); setCurrentYear(cal.get(Calendar.YEAR));
@ -138,19 +129,21 @@ public class DateTimePicker extends FrameLayout {
} }
}; };
// 分钟改变监听器
private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() { private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
@Override @Override
//这里是对 分钟Minute改变的监听
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;
//设置offset作为小时改变的一个记录数据
if (oldVal == maxValue && newVal == minValue) { if (oldVal == maxValue && newVal == minValue) {
offset += 1; offset += 1;
} else if (oldVal == minValue && newVal == maxValue) { } else if (oldVal == minValue && newVal == maxValue) {
offset -= 1; offset -= 1;
} }
//如果原值为59新值为0则offset加1
//如果原值为0新值为59则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());
@ -169,11 +162,10 @@ public class DateTimePicker extends FrameLayout {
} }
}; };
// AM/PM改变监听器
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() { private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
@Override //对AM和PM的监听
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// 更新AM/PM
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);
@ -185,44 +177,29 @@ public class DateTimePicker extends FrameLayout {
} }
}; };
/**
*
*/
public interface OnDateTimeChangedListener { public interface OnDateTimeChangedListener {
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) {
this(context, date, DateFormat.is24HourFormat(context)); this(context, date, DateFormat.is24HourFormat(context));
} }//上面函数的得到的是一个天文数字1970至今的秒数需要DateFormat将其变得有意义
/**
*
* @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);
//如果当前Activity里用到别的layout比如对话框layout
//还要设置这个layout上的其他组件的内容就必须用inflate()方法先将对话框的layout找出来
//然后再用findViewById()找到它上面的其它组件
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);
@ -230,13 +207,12 @@ public class DateTimePicker extends FrameLayout {
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);
// AM/PM选择器的字符串数组
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);
@ -244,28 +220,22 @@ public class DateTimePicker extends FrameLayout {
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) {
@ -278,42 +248,43 @@ public class DateTimePicker extends FrameLayout {
mAmPmSpinner.setEnabled(enabled); mAmPmSpinner.setEnabled(enabled);
mIsEnabled = enabled; mIsEnabled = enabled;
} }
//存在疑问setEnabled的作用
/** //下面的代码通过原程序的注释已经比较清晰,另外可以通过函数名来判断
* 使 //下面的各函数主要是对上面代码引用到的各函数功能的实现
* @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));
} }//实现函数功能——设置当前的时间参数是date
/** /**
* * 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) {
@ -322,19 +293,22 @@ public class DateTimePicker extends FrameLayout {
setCurrentDay(dayOfMonth); setCurrentDay(dayOfMonth);
setCurrentHour(hourOfDay); setCurrentHour(hourOfDay);
setCurrentMinute(minute); setCurrentMinute(minute);
} }//实现函数功能——设置当前的时间,参数是各详细的变量
/** /**
* * Get current year
* @return *
* @return The current year
*/ */
//下面是得到year、month、day等值
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()) {
@ -346,16 +320,18 @@ public class DateTimePicker extends FrameLayout {
} }
/** /**
* * 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()) {
@ -367,16 +343,18 @@ public class DateTimePicker extends FrameLayout {
} }
/** /**
* * 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()) {
@ -388,19 +366,15 @@ public class DateTimePicker extends FrameLayout {
} }
/** /**
* 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);
} }
/**
* 1224
* @return
*/
private int getCurrentHour() { private int getCurrentHour() {
if (mIs24HourView) { if (mIs24HourView){
return getCurrentHourOfDay(); return getCurrentHourOfDay();
} else { } else {
int hour = getCurrentHourOfDay(); int hour = getCurrentHourOfDay();
@ -413,8 +387,9 @@ 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()) {
@ -440,16 +415,16 @@ public class DateTimePicker extends FrameLayout {
} }
/** /**
* * 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()) {
@ -461,16 +436,16 @@ public class DateTimePicker extends FrameLayout {
} }
/** /**
* 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) {
@ -484,9 +459,6 @@ public class DateTimePicker extends FrameLayout {
updateAmPmControl(); updateAmPmControl();
} }
/**
*
*/
private void updateDateControl() { private void updateDateControl() {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(mDate.getTimeInMillis()); cal.setTimeInMillis(mDate.getTimeInMillis());
@ -499,11 +471,8 @@ public class DateTimePicker extends FrameLayout {
mDateSpinner.setDisplayedValues(mDateDisplayValues); mDateSpinner.setDisplayedValues(mDateDisplayValues);
mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2); mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2);
mDateSpinner.invalidate(); mDateSpinner.invalidate();
} }// 对于星期几的算法
/**
* AM/PM
*/
private void updateAmPmControl() { private void updateAmPmControl() {
if (mIs24HourView) { if (mIs24HourView) {
mAmPmSpinner.setVisibility(View.GONE); mAmPmSpinner.setVisibility(View.GONE);
@ -511,12 +480,9 @@ public class DateTimePicker extends FrameLayout {
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() {
if (mIs24HourView) { if (mIs24HourView) {
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW); mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW);
@ -524,20 +490,17 @@ public class DateTimePicker extends FrameLayout {
} else { } else {
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(),

@ -14,122 +14,89 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import java.util.Calendar; import java.util.Calendar;
import net.micode.notes.R; import net.micode.notes.R;
import net.micode.notes.ui.DateTimePicker; import net.micode.notes.ui.DateTimePicker;
import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener; import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.text.format.DateUtils; import android.text.format.DateUtils;
/** public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
* DateTimePickerDialog
*/
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
// 当前日期 private Calendar mDate = Calendar.getInstance();
private Calendar mDate = Calendar.getInstance(); //创建一个Calendar类型的变量 mDate方便时间的操作
// 是否为24小时制 private boolean mIs24HourView;
private boolean mIs24HourView; private OnDateTimeSetListener mOnDateTimeSetListener;
// 日期时间设置监听器 //声明一个时间日期滚动选择控件 mOnDateTimeSetListener
private OnDateTimeSetListener mOnDateTimeSetListener; private DateTimePicker mDateTimePicker;
// 日期时间选择器 //DateTimePicker控件控件一般用于让用户可以从日期列表中选择单个值。
private DateTimePicker mDateTimePicker; //运行时,单击控件边上的下拉箭头,会显示为两个部分:一个下拉列表,一个用于选择日期的
/** public interface OnDateTimeSetListener {
* void OnDateTimeSet(AlertDialog dialog, long date);
*/ }
public interface OnDateTimeSetListener {
void OnDateTimeSet(AlertDialog dialog, long date);
}
/** public DateTimePickerDialog(Context context, long date) {
* //对该界面对话框的实例化
* @param context super(context);
* @param date //对数据库的操作
*/ mDateTimePicker = new DateTimePicker(context);
public DateTimePickerDialog(Context context, long date) { setView(mDateTimePicker);
super(context); //添加一个子视图
// 创建日期时间选择器 mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
mDateTimePicker = new DateTimePicker(context); public void onDateTimeChanged(DateTimePicker view, int year, int month,
// 设置对话框视图 int dayOfMonth, int hourOfDay, int minute) {
setView(mDateTimePicker); mDate.set(Calendar.YEAR, year);
// 设置日期时间改变监听器 mDate.set(Calendar.MONTH, month);
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() { mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
public void onDateTimeChanged(DateTimePicker view, int year, int month, mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
int dayOfMonth, int hourOfDay, int minute) { mDate.set(Calendar.MINUTE, minute);
// 更新日期 //将视图中的各选项设置为系统当前时间
mDate.set(Calendar.YEAR, year); updateTitle(mDate.getTimeInMillis());
mDate.set(Calendar.MONTH, month); }
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); });
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); mDate.setTimeInMillis(date);
mDate.set(Calendar.MINUTE, minute); //得到系统时间
// 更新对话框标题 mDate.set(Calendar.SECOND, 0);
updateTitle(mDate.getTimeInMillis()); //将秒数设置为0
} mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
}); setButton(context.getString(R.string.datetime_dialog_ok), this);
// 设置当前日期 setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
mDate.setTimeInMillis(date); //设置按钮
mDate.set(Calendar.SECOND, 0); set24HourView(DateFormat.is24HourFormat(this.getContext()));
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); //时间标准化打印
// 设置确定按钮 updateTitle(mDate.getTimeInMillis());
setButton(context.getString(R.string.datetime_dialog_ok), this); }
// 设置取消按钮
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
// 设置24小时制
set24HourView(DateFormat.is24HourFormat(this.getContext()));
// 更新对话框标题
updateTitle(mDate.getTimeInMillis());
}
/** public void set24HourView(boolean is24HourView) {
* 24 mIs24HourView = is24HourView;
* @param is24HourView 24 }
*/
public void set24HourView(boolean is24HourView) {
mIs24HourView = is24HourView;
}
/** public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
* mOnDateTimeSetListener = callBack;
* @param callBack }//将时间日期滚动选择控件实例化
*/
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
mOnDateTimeSetListener = callBack;
}
/** private void updateTitle(long date) {
* int flag =
* @param date DateUtils.FORMAT_SHOW_YEAR |
*/ DateUtils.FORMAT_SHOW_DATE |
private void updateTitle(long date) { DateUtils.FORMAT_SHOW_TIME;
// 设置日期格式 flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;
int flag = setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
DateUtils.FORMAT_SHOW_YEAR | }//android开发中常见日期管理工具类API——DateUtils按照上下午显示时间
DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_TIME;
// 根据是否为24小时制设置时间格式
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_12HOUR;
// 设置对话框标题
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
}
/** public void onClick(DialogInterface arg0, int arg1) {
* if (mOnDateTimeSetListener != null) {
* @param arg0 mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
* @param arg1 ID }
*/ }//第一个参数arg0是接收到点击事件的对话框
public void onClick(DialogInterface arg0, int arg1) { //第二个参数arg1是该对话框上的按钮
// 如果设置了日期时间设置监听器,则通知日期时间已设置
if (mOnDateTimeSetListener != null) {
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
}
}
} }

@ -27,67 +27,39 @@ import android.widget.PopupMenu.OnMenuItemClickListener;
import net.micode.notes.R; import net.micode.notes.R;
/**
* DropdownMenu
*/
public class DropdownMenu { public class DropdownMenu {
// 下拉菜单按钮
private Button mButton; private Button mButton;
// 弹出菜单
private PopupMenu mPopupMenu; private PopupMenu mPopupMenu;
// 菜单项 //声明一个下拉菜单
private Menu mMenu; private Menu mMenu;
/**
*
* @param context
* @param button
* @param menuId ID
*/
public DropdownMenu(Context context, Button button, int menuId) { public DropdownMenu(Context context, Button button, int menuId) {
mButton = button; mButton = button;
// 设置按钮背景为下拉图标
mButton.setBackgroundResource(R.drawable.dropdown_icon); mButton.setBackgroundResource(R.drawable.dropdown_icon);
// 创建弹出菜单 //设置这个view的背景
mPopupMenu = new PopupMenu(context, mButton); mPopupMenu = new PopupMenu(context, mButton);
// 获取菜单项
mMenu = mPopupMenu.getMenu(); mMenu = mPopupMenu.getMenu();
// 加载菜单资源
mPopupMenu.getMenuInflater().inflate(menuId, mMenu); mPopupMenu.getMenuInflater().inflate(menuId, mMenu);
// 设置按钮点击事件 //MenuInflater是用来实例化Menu目录下的Menu布局文件
//根据ID来确认menu的内容选项
mButton.setOnClickListener(new OnClickListener() { mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
// 显示弹出菜单
mPopupMenu.show(); mPopupMenu.show();
} }
}); });
} }
/**
*
* @param listener
*/
public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) { public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {
if (mPopupMenu != null) { if (mPopupMenu != null) {
// 设置弹出菜单项点击事件监听器
mPopupMenu.setOnMenuItemClickListener(listener); mPopupMenu.setOnMenuItemClickListener(listener);
} }//设置菜单的监听
} }
/**
*
* @param id ID
* @return
*/
public MenuItem findItem(int id) { public MenuItem findItem(int id) {
return mMenu.findItem(id); return mMenu.findItem(id);
} }//对于菜单选项的初始化,根据索引搜索菜单需要的选项
/**
*
* @param title
*/
public void setTitle(CharSequence title) { public void setTitle(CharSequence title) {
mButton.setText(title); mButton.setText(title);
} }//布局文件,设置标题
} }

@ -14,108 +14,74 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.CursorAdapter; import android.widget.CursorAdapter;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
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;
/**
* FoldersListAdapter
*/
public class FoldersListAdapter extends CursorAdapter {
// 查询数据库时需要的列名
public static final String [] PROJECTION = {
NoteColumns.ID, // 文件夹ID
NoteColumns.SNIPPET // 文件夹名称
};
// 列索引,方便获取数据 public class FoldersListAdapter extends CursorAdapter {
public static final int ID_COLUMN = 0; //CursorAdapter是Cursor和ListView的接口
public static final int NAME_COLUMN = 1; //FoldersListAdapter继承了CursorAdapter的类
//主要作用是便签数据库和用户的交互
//这里就是用folder文件夹的形式展现给用户
public static final String [] PROJECTION = {
NoteColumns.ID,
NoteColumns.SNIPPET
};//调用数据库中便签的ID和片段
public static final int ID_COLUMN = 0;
public static final int NAME_COLUMN = 1;
public FoldersListAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}//数据库操作
/** @Override
* public View newView(Context context, Cursor cursor, ViewGroup parent) {
* @param context //ViewGroup是容器
* @param c return new FolderListItem(context);
*/ }//创建一个文件夹,对于各文件夹中子标签的初始化
public FoldersListAdapter(Context context, Cursor c) {
super(context, c);
}
/** @Override
* public void bindView(View view, Context context, Cursor cursor) {
* @param context if (view instanceof FolderListItem) {
* @param cursor String folderName = (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
* @param parent .getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
* @return ((FolderListItem) view).bind(folderName);
*/ }
@Override }//将各个布局文件绑定起来
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new FolderListItem(context);
}
/** public String getFolderName(Context context, int position) {
* Cursor cursor = (Cursor) getItem(position);
* @param view return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
* @param context .getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
* @param cursor }//根据数据库中标签的ID得到标签的各项内容
*/
@Override
public void bindView(View view, Context context, Cursor cursor) {
if (view instanceof FolderListItem) {
// 获取文件夹名称
String folderName = (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
// 绑定文件夹名称
((FolderListItem) view).bind(folderName);
}
}
/** private class FolderListItem extends LinearLayout {
* private TextView mName;
* @param context
* @param position
* @return
*/
public String getFolderName(Context context, int position) {
Cursor cursor = (Cursor) getItem(position);
return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
}
/** public FolderListItem(Context context) {
* FolderListItem super(context);
*/ //操作数据库
private class FolderListItem extends LinearLayout { inflate(context, R.layout.folder_list_item, this);
private TextView mName; // 文件夹名称文本视图 //根据布局文件的名字等信息将其找出来
mName = (TextView) findViewById(R.id.tv_folder_name);
}
/** public void bind(String name) {
* mName.setText(name);
* @param context }
*/ }
public FolderListItem(Context context) {
super(context);
// 加载文件夹列表项布局
inflate(context, R.layout.folder_list_item, this);
// 获取文件夹名称文本视图
mName = (TextView) findViewById(R.id.tv_folder_name);
}
/** }
*
* @param name
*/
public void bind(String name) {
mName.setText(name); // 设置文件夹名称
}
}
}

@ -57,6 +57,7 @@ import android.os.Environment;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Typeface; // 自带四种字体 import android.graphics.Typeface; // 自带四种字体
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.TextNote; import net.micode.notes.data.Notes.TextNote;
@ -81,9 +82,9 @@ import java.util.Vector;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
/**
* NoteEditActivity 便
*/
public class NoteEditActivity extends Activity //NOTE: extends--单继承,但可多重继承 @zhoukexing 2023/12/17 23:29 public class NoteEditActivity extends Activity //NOTE: extends--单继承,但可多重继承 @zhoukexing 2023/12/17 23:29
implements OnClickListener, NoteSettingChangedListener, OnTextViewChangeListener { implements OnClickListener, NoteSettingChangedListener, OnTextViewChangeListener {
private Intent intent; //NOTE: implements--实现接口 @zhoukexing 2023/12/17 23:24 private Intent intent; //NOTE: implements--实现接口 @zhoukexing 2023/12/17 23:24
@ -92,11 +93,11 @@ public class NoteEditActivity extends Activity //NOTE: extends--单继承,但
* @zhoukexing 2023/12/17 23:39 * @zhoukexing 2023/12/17 23:39
*/ */
private class HeadViewHolder { private class HeadViewHolder {
public TextView tvModified; // 显示修改时间的文本视图 public TextView tvModified;
public ImageView ivAlertIcon; // 提醒图标的图像视图 public ImageView ivAlertIcon;
public TextView tvAlertDate; // 提醒日期的文本视图 public TextView tvAlertDate;
// 顶部置顶文本 // 顶部置顶文本
public TextView tvTopText; public TextView tvTopText;
@ -104,10 +105,9 @@ public class NoteEditActivity extends Activity //NOTE: extends--单继承,但
// 顶部长度统计文本 // 顶部长度统计文本
public TextView tvTextNum; public TextView tvTextNum;
public ImageView ibSetBgColor; // 设置背景颜色的按钮 public ImageView ibSetBgColor;
} }
// 背景颜色选择器按钮映射
private static final Map<Integer, Integer> sBgSelectorBtnsMap = new HashMap<Integer, Integer>(); private static final Map<Integer, Integer> sBgSelectorBtnsMap = new HashMap<Integer, Integer>();
static { static {
sBgSelectorBtnsMap.put(R.id.iv_bg_yellow, ResourceParser.YELLOW); sBgSelectorBtnsMap.put(R.id.iv_bg_yellow, ResourceParser.YELLOW);
@ -117,7 +117,6 @@ public class NoteEditActivity extends Activity //NOTE: extends--单继承,但
sBgSelectorBtnsMap.put(R.id.iv_bg_white, ResourceParser.WHITE); sBgSelectorBtnsMap.put(R.id.iv_bg_white, ResourceParser.WHITE);
} }
// 背景颜色选择器选中状态映射
private static final Map<Integer, Integer> sBgSelectorSelectionMap = new HashMap<Integer, Integer>(); private static final Map<Integer, Integer> sBgSelectorSelectionMap = new HashMap<Integer, Integer>();
static { static {
sBgSelectorSelectionMap.put(ResourceParser.YELLOW, R.id.iv_bg_yellow_select); sBgSelectorSelectionMap.put(ResourceParser.YELLOW, R.id.iv_bg_yellow_select);
@ -127,7 +126,6 @@ public class NoteEditActivity extends Activity //NOTE: extends--单继承,但
sBgSelectorSelectionMap.put(ResourceParser.WHITE, R.id.iv_bg_white_select); sBgSelectorSelectionMap.put(ResourceParser.WHITE, R.id.iv_bg_white_select);
} }
// 字体大小选择器按钮映射
private static final Map<Integer, Integer> sFontSizeBtnsMap = new HashMap<Integer, Integer>(); private static final Map<Integer, Integer> sFontSizeBtnsMap = new HashMap<Integer, Integer>();
static { static {
sFontSizeBtnsMap.put(R.id.ll_font_large, ResourceParser.TEXT_LARGE); sFontSizeBtnsMap.put(R.id.ll_font_large, ResourceParser.TEXT_LARGE);
@ -136,7 +134,6 @@ public class NoteEditActivity extends Activity //NOTE: extends--单继承,但
sFontSizeBtnsMap.put(R.id.ll_font_super, ResourceParser.TEXT_SUPER); sFontSizeBtnsMap.put(R.id.ll_font_super, ResourceParser.TEXT_SUPER);
} }
// 字体大小选择器选中状态映射
private static final Map<Integer, Integer> sFontSelectorSelectionMap = new HashMap<Integer, Integer>(); private static final Map<Integer, Integer> sFontSelectorSelectionMap = new HashMap<Integer, Integer>();
static { static {
sFontSelectorSelectionMap.put(ResourceParser.TEXT_LARGE, R.id.iv_large_select); sFontSelectorSelectionMap.put(ResourceParser.TEXT_LARGE, R.id.iv_large_select);
@ -145,44 +142,47 @@ public class NoteEditActivity extends Activity //NOTE: extends--单继承,但
sFontSelectorSelectionMap.put(ResourceParser.TEXT_SUPER, R.id.iv_super_select); sFontSelectorSelectionMap.put(ResourceParser.TEXT_SUPER, R.id.iv_super_select);
} }
private static final String TAG = "NoteEditActivity"; private static final String TAG = "NoteEditActivity";
private static int mMaxRevokeTimes = 10; // 最大撤销次数 private static int mMaxRevokeTimes = 10;
private HeadViewHolder mNoteHeaderHolder; // 便签头部视图的持有者 private HeadViewHolder mNoteHeaderHolder;
private View mHeadViewPanel; // 头部视图面板 private View mHeadViewPanel;
private View mNoteBgColorSelector; // 便签背景颜色选择器 private View mNoteBgColorSelector;
private View mFontSizeSelector; // 字体大小选择器 private View mFontSizeSelector;
private EditText mNoteEditor; // 便签编辑器 private EditText mNoteEditor;
private View mNoteEditorPanel; // 便签编辑器面板 private View mNoteEditorPanel;
private WorkingNote mWorkingNote; // 正在编辑的便签 private WorkingNote mWorkingNote;
private SharedPreferences mSharedPrefs; // 共享偏好设置 private SharedPreferences mSharedPrefs;
private int mFontSizeId; // 字体大小ID private int mFontSizeId;
private int mFontStyleId; // 字体样式ID private int mFontStyleId;
private static final String PREFERENCE_FONT_SIZE = "pref_font_size"; // 字体大小偏好设置键 private static final String PREFERENCE_FONT_SIZE = "pref_font_size";
private static final String PREFERENCE_FONT_STYLE = "pref_font_style"; // 字体样式偏好设置键 private static final String PREFERENCE_FONT_STYLE = "pref_font_style";
private static final int SHORTCUT_ICON_TITLE_MAX_LEN = 10; // 快捷方式图标标题最大长度 private static final int SHORTCUT_ICON_TITLE_MAX_LEN = 10;
public static final String TAG_CHECKED = String.valueOf('\u221A'); // 已选中标签 public static final String TAG_CHECKED = String.valueOf('\u221A');
public static final String TAG_UNCHECKED = String.valueOf('\u25A1'); // 未选中标签 public static final String TAG_UNCHECKED = String.valueOf('\u25A1');
private LinearLayout mEditTextList; // 编辑文本列表 private LinearLayout mEditTextList;
private String mUserQuery; // 用户查询 private String mUserQuery;
private Pattern mPattern; // 正则表达式模式 private Pattern mPattern;
// 存储改变的数据 // 存储改变的数据
private Vector<SpannableString> mHistory = new Vector<SpannableString>(mMaxRevokeTimes); private Vector<SpannableString> mHistory = new Vector<SpannableString>(mMaxRevokeTimes);
private boolean mIsRvoke; // 是否撤销 private boolean mIsRvoke;
/*--- 以上是此类中的数据区,以下是方法区 ---*/ /*--- 以上是此类中的数据区,以下是方法区 ---*/

@ -14,222 +14,273 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.content.Context; import android.content.Context;
import android.graphics.Rect; import android.graphics.Rect;
import android.text.Layout; import android.text.Layout;
import android.text.Selection; import android.text.Selection;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.URLSpan; import android.text.style.URLSpan;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener; import android.view.MenuItem.OnMenuItemClickListener;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.widget.EditText; import android.widget.EditText;
import net.micode.notes.R; import net.micode.notes.R;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** //继承edittext设置便签设置文本框
* 便EditText public class NoteEditText extends EditText {
* private static final String TAG = "NoteEditText";
*/ private int mIndex;
public class NoteEditText extends EditText { private int mSelectionStartBeforeDelete;
private static final String TAG = "NoteEditText"; // 日志标签
private int mIndex; // 当前文本框的索引 private static final String SCHEME_TEL = "tel:" ;
private int mSelectionStartBeforeDelete; // 删除前的选择起始位置 private static final String SCHEME_HTTP = "http:" ;
private static final String SCHEME_EMAIL = "mailto:" ;
// 定义几种URL协议的前缀
private static final String SCHEME_TEL = "tel:"; // 电话 ///建立一个字符和整数的hash表用于链接电话网站还有邮箱
private static final String SCHEME_HTTP = "http:"; // 网络 private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>();
private static final String SCHEME_EMAIL = "mailto:"; // 邮箱 static {
sSchemaActionResMap.put(SCHEME_TEL, R.string.note_link_tel);
// URL协议到资源ID的映射 sSchemaActionResMap.put(SCHEME_HTTP, R.string.note_link_web);
private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>(); sSchemaActionResMap.put(SCHEME_EMAIL, R.string.note_link_email);
static { }
sSchemaActionResMap.put(SCHEME_TEL, R.string.note_link_tel); // 电话链接文本
sSchemaActionResMap.put(SCHEME_HTTP, R.string.note_link_web); // 网络链接文本 /**
sSchemaActionResMap.put(SCHEME_EMAIL, R.string.note_link_email); // 邮箱链接文本 * Call by the {@link NoteEditActivity} to delete or add edit text
} */
//在NoteEditActivity中删除或添加文本的操作可以看做是一个文本是否被变的标记英文注释已说明的很清楚
/** public interface OnTextViewChangeListener {
* /**
* * Delete current edit text when {@link KeyEvent#KEYCODE_DEL} happens
*/ * and the text is null
public interface OnTextViewChangeListener { */
/** //处理删除按键时的操作
* void onEditTextDelete(int index, String text);
* @param index
* @param text /**
*/ * Add edit text after current edit text when {@link KeyEvent#KEYCODE_ENTER}
void onEditTextDelete(int index, String text); * happen
*/
/** //处理进入按键时的操作
* void onEditTextEnter(int index, String text);
* @param index
* @param text /**
*/ * Hide or show item option when text change
void onEditTextEnter(int index, String text); */
void onTextChange(int index, boolean hasText);
/** }
*
* @param index private OnTextViewChangeListener mOnTextViewChangeListener;
* @param hasText
*/ //根据context设置文本
void onTextChange(int index, boolean hasText); public NoteEditText(Context context) {
} super(context, null);//用super引用父类变量
mIndex = 0;
private OnTextViewChangeListener mOnTextViewChangeListener; // 文本框变化监听器 }
/** //设置当前光标
* public void setIndex(int index) {
* @param context mIndex = index;
*/ }
public NoteEditText(Context context) {
super(context, null); //初始化文本修改标记
mIndex = 0; // 默认索引为0 public void setOnTextViewChangeListener(OnTextViewChangeListener listener) {
} mOnTextViewChangeListener = listener;
}
/**
* //AttributeSet 百度了一下是自定义空控件属性,用于维护便签动态变化的属性
* @param index //初始化便签
*/ public NoteEditText(Context context, AttributeSet attrs) {
public void setIndex(int index) { super(context, attrs, android.R.attr.editTextStyle);
mIndex = index; }
}
// 根据defstyle自动初始化
/** public NoteEditText(Context context, AttributeSet attrs, int defStyle) {
* super(context, attrs, defStyle);
* @param listener // TODO Auto-generated construct or stub
*/ }
public void setOnTextViewChangeListener(OnTextViewChangeListener listener) {
mOnTextViewChangeListener = listener; @Override
} //view里的函数处理手机屏幕的所有事件
/*event
/** */
* public boolean onTouchEvent(MotionEvent event) {
* @param context switch (event.getAction()) {
* @param attrs //重写了需要处理屏幕被按下的事件
*/ case MotionEvent.ACTION_DOWN:
public NoteEditText(Context context, AttributeSet attrs) { //跟新当前坐标值
super(context, attrs, android.R.attr.editTextStyle); int x = (int) event.getX();
} int y = (int) event.getY();
x -= getTotalPaddingLeft();
/** y -= getTotalPaddingTop();
* x += getScrollX();
* @param context y += getScrollY();
* @param attrs
* @param defStyle //用布局控件layout根据x,y的新值设置新的位置
*/ Layout layout = getLayout();
public NoteEditText(Context context, AttributeSet attrs, int defStyle) { int line = layout.getLineForVertical(y);
super(context, attrs, defStyle); int off = layout.getOffsetForHorizontal(line, x);
}
//更新光标新的位置
/** Selection.setSelection(getText(), off);
* break;
* }
* @param event
* @return return super.onTouchEvent(event);
*/ }
@Override
public boolean onTouchEvent(MotionEvent event) { @Override
if (event.getAction() == MotionEvent.ACTION_DOWN) { /*
int x = (int) event.getX(); *
int y = (int) event.getY(); *
x -= getTotalPaddingLeft(); */
y -= getTotalPaddingTop(); public boolean onKeyDown(int keyCode, KeyEvent event) {
x += getScrollX(); switch (keyCode) {
y += getScrollY(); //根据按键的 Unicode 编码值来处理
case KeyEvent.KEYCODE_ENTER:
Layout layout = getLayout(); //“进入”按键
int line = layout.getLineForVertical(y); if (mOnTextViewChangeListener != null) {
int off = layout.getOffsetForHorizontal(line, x); return false;
Selection.setSelection(getText(), off); // 设置文本选择位置 }
} break;
return super.onTouchEvent(event); case KeyEvent.KEYCODE_DEL:
} //“删除”按键
mSelectionStartBeforeDelete = getSelectionStart();
/** break;
* default:
* break;
* @param keyCode }
* @param event //继续执行父类的其他点击事件
* @return return super.onKeyDown(keyCode, event);
*/ }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { @Override
switch (keyCode) { /*
case KeyEvent.KEYCODE_ENTER: *
if (mOnTextViewChangeListener != null) { *
return false; // 不处理回车键,交由监听器处理 */
} public boolean onKeyUp(int keyCode, KeyEvent event) {
break; switch(keyCode) {
case KeyEvent.KEYCODE_DEL: //根据按键的 Unicode 编码值来处理有删除和进入2种操作
mSelectionStartBeforeDelete = getSelectionStart(); // 记录删除前的选择起始位置 case KeyEvent.KEYCODE_DEL:
break; if (mOnTextViewChangeListener != null) {
} //若是被修改过
return super.onKeyDown(keyCode, event); if (0 == mSelectionStartBeforeDelete && mIndex != 0) {
} //若之前有被修改并且文档不为空
mOnTextViewChangeListener.onEditTextDelete(mIndex, getText().toString());
/** //利用上文OnTextViewChangeListener对KEYCODE_DEL按键情况的删除函数进行删除
* return true;
* }
* @param keyCode } else {
* @param event Log.d(TAG, "OnTextViewChangeListener was not seted");
* @return //其他情况报错,文档的改动监听器并没有建立
*/ }
@Override break;
public boolean onKeyUp(int keyCode, KeyEvent event) { case KeyEvent.KEYCODE_ENTER:
switch (keyCode) { //同上也是分为监听器是否建立2种情况
case KeyEvent.KEYCODE_DEL: // 删除键 if (mOnTextViewChangeListener != null) {
if (mOnTextViewChangeListener != null) { int selectionStart = getSelectionStart();
if (mSelectionStartBeforeDelete == 0 && mIndex != 0) { //获取当前位置
// 如果删除前的选择起始位置为0且不是第一个文本框则删除当前文本框 String text = getText().subSequence(selectionStart, length()).toString();
mOnTextViewChangeListener.onEditTextDelete(mIndex, getText().toString()); //获取当前文本
return true; setText(getText().subSequence(0, selectionStart));
} //根据获取的文本设置当前文本
} else { mOnTextViewChangeListener.onEditTextEnter(mIndex + 1, text);
Log.d(TAG, "OnTextViewChangeListener was not seted"); //当{@link KeyEvent#KEYCODE_ENTER}添加新文本
} } else {
break; Log.d(TAG, "OnTextViewChangeListener was not seted");
case KeyEvent.KEYCODE_ENTER: // 回车键 //其他情况报错,文档的改动监听器并没有建立
if (mOnTextViewChangeListener != null) { }
int selectionStart = getSelectionStart(); break;
String text = getText().subSequence(selectionStart, length()).toString(); default:
setText(getText().subSequence(0, selectionStart)); // 删除回车键后的内容 break;
mOnTextViewChangeListener.onEditTextEnter(mIndex + 1, text); // 添加新的文本框 }
} else { //继续执行父类的其他按键弹起的事件
Log.d(TAG, "OnTextViewChangeListener was not seted"); return super.onKeyUp(keyCode, event);
} }
break;
} @Override
return super.onKeyUp(keyCode, event); /*
} *
*
/** * focusedViewFocusedtruefalse
* direction
* RectViewnull
* @param focused */
* @param direction protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
* @param previouslyFocusedRect if (mOnTextViewChangeListener != null) {
*/ //若监听器已经建立
@Override if (!focused && TextUtils.isEmpty(getText())) {
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { //获取到焦点并且文本不为空
if (mOnTextViewChangeListener != null) { mOnTextViewChangeListener.onTextChange(mIndex, false);
if (!focused && TextUtils.isEmpty(getText())) { //mOnTextViewChangeListener子函数置false隐藏事件选项
mOnTextViewChangeListener.onTextChange(mIndex, false); } else {
} else { mOnTextViewChangeListener.onTextChange(mIndex, true);
mOnTextViewChangeListener.onTextChange(mIndex, true); //mOnTextViewChangeListener子函数置true显示事件选项
} }
} }
super.onFocusChanged(focused, direction, previouslyFocusedRect); //继续执行父类的其他焦点变化的事件
} super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
/*
*
*
*/
protected void onCreateContextMenu(ContextMenu menu) {
if (getText() instanceof Spanned) {
//有文本存在
int selStart = getSelectionStart();
int selEnd = getSelectionEnd();
//获取文本开始和结尾位置
int min = Math.min(selStart, selEnd);
int max = Math.max(selStart, selEnd);
//获取开始到结尾的最大值和最小值
final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class);
//设置url的信息的范围值
if (urls.length == 1) {
int defaultResId = 0;
for(String schema: sSchemaActionResMap.keySet()) {
//获取计划表中所有的key值
if(urls[0].getURL().indexOf(schema) >= 0) {
//若url可以添加则在添加后将defaultResId置为key所映射的值
defaultResId = sSchemaActionResMap.get(schema);
break;
}
}
if (defaultResId == 0) {
//defaultResId == 0则说明url并没有添加任何东西所以置为连接其他SchemaActionResMap的值
defaultResId = R.string.note_link_other;
}
//建立菜单
menu.add(0, 0, 0, defaultResId).setOnMenuItemClickListener(
new OnMenuItemClickListener() {
//新建按键监听器
public boolean onMenuItemClick(MenuItem item) {
// goto a new intent
urls[0].onClick(NoteEditText.this);
//根据相应的文本设置菜单的按键
return true;
}
});
}
}
//继续执行父类的其他菜单创建的事件
super.onCreateContextMenu(menu);
}
}

@ -25,12 +25,8 @@ import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.tool.DataUtils; import net.micode.notes.tool.DataUtils;
/**
* NoteItemData 便
* 便便
*/
public class NoteItemData { public class NoteItemData {
// 定义查询便签数据库时需要的列
static final String [] PROJECTION = new String [] { static final String [] PROJECTION = new String [] {
NoteColumns.ID, NoteColumns.ID,
NoteColumns.ALERTED_DATE, NoteColumns.ALERTED_DATE,
@ -47,7 +43,6 @@ public class NoteItemData {
NoteColumns.TOP, NoteColumns.TOP,
}; };
// 定义列的索引
private static final int ID_COLUMN = 0; private static final int ID_COLUMN = 0;
private static final int ALERTED_DATE_COLUMN = 1; private static final int ALERTED_DATE_COLUMN = 1;
private static final int BG_COLOR_ID_COLUMN = 2; private static final int BG_COLOR_ID_COLUMN = 2;
@ -60,9 +55,11 @@ public class NoteItemData {
private static final int TYPE_COLUMN = 9; private static final int TYPE_COLUMN = 9;
private static final int WIDGET_ID_COLUMN = 10; private static final int WIDGET_ID_COLUMN = 10;
private static final int WIDGET_TYPE_COLUMN = 11; private static final int WIDGET_TYPE_COLUMN = 11;
private static final int TOP_STATE_COLUMN = 12; private static final int TOP_STATE_COLUMN = 12;
// 便签项的数据字段 /** NotesDatabaseHelper.java
*
* @zhoukexing 2023/12/25 20:22 */
private long mId; private long mId;
private long mAlertDate; private long mAlertDate;
private int mBgColorId; private int mBgColorId;
@ -79,7 +76,6 @@ public class NoteItemData {
private String mName; private String mName;
private String mPhoneNumber; private String mPhoneNumber;
// 便签项的位置状态
private boolean mIsLastItem; private boolean mIsLastItem;
private boolean mIsFirstItem; private boolean mIsFirstItem;
private boolean mIsOnlyOneItem; private boolean mIsOnlyOneItem;
@ -87,33 +83,37 @@ public class NoteItemData {
private boolean mIsMultiNotesFollowingFolder; private boolean mIsMultiNotesFollowingFolder;
/** /**
* 便 * @method: NoteItemData
* @param context * @description:
* @param cursor * @date: 2023/12/25 19:58
* @author: zhoukexing
* @param: [context, cursor]
* @return:
*/ */
public NoteItemData(Context context, Cursor cursor) { public NoteItemData(Context context, Cursor cursor) { // 把cursor理解为这样一个指针指向一个表格 @zhoukexing 2023/12/25 20:12
mId = cursor.getLong(ID_COLUMN); // 获取便签ID mId = cursor.getLong(ID_COLUMN); // 可以根据传入的列号获取到表格里对应列的值 @zhoukexing 2023/12/25 20:12
mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN); // 获取提醒日期 mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN);
mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN); // 获取背景颜色ID mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN);
mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN); // 获取创建日期 mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN);
mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0); // 是否有附件 mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0) ? true : false;
mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN); // 获取修改日期 mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN);
mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN); // 获取便签数量 mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN);
mParentId = cursor.getLong(PARENT_ID_COLUMN); // 获取父便签ID mParentId = cursor.getLong(PARENT_ID_COLUMN);
mSnippet = cursor.getString(SNIPPET_COLUMN); // 获取便签摘要 mSnippet = cursor.getString(SNIPPET_COLUMN);
mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace( mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace(
NoteEditActivity.TAG_UNCHECKED, ""); // 去除摘要中的标签 NoteEditActivity.TAG_UNCHECKED, "");
mType = cursor.getInt(TYPE_COLUMN); // 获取便签类型 mType = cursor.getInt(TYPE_COLUMN);
mWidgetId = cursor.getInt(WIDGET_ID_COLUMN); // 获取小部件ID mWidgetId = cursor.getInt(WIDGET_ID_COLUMN);
mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN); // 获取小部件类型 mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN);
mTop = cursor.getInt(TOP_STATE_COLUMN); // 获取置顶状态 mTop = cursor.getInt(TOP_STATE_COLUMN);
mPhoneNumber = ""; mPhoneNumber = "";
if (mParentId == Notes.ID_CALL_RECORD_FOLDER) { // 如果便签属于通话记录文件夹 if (mParentId == Notes.ID_CALL_RECORD_FOLDER) { //Q: 文件夹为什么有电话记录之说?怎么是通过一个便签的父文件夹来判断便签内有无电话号码?@zkx 2023/12/25
mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId); // 获取电话号码 mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId);
if (!TextUtils.isEmpty(mPhoneNumber)) { // 如果电话号码不为空 // 根据电话号码锁定联系人名称,若不在联系人里,直接使用电话号码 @zhoukexing 2023/12/25 20:17
mName = Contact.getContact(context, mPhoneNumber); // 获取联系人名称 if (!TextUtils.isEmpty(mPhoneNumber)) {
if (mName == null) { // 如果联系人名称为空,则使用电话号码 mName = Contact.getContact(context, mPhoneNumber);
if (mName == null) {
mName = mPhoneNumber; mName = mPhoneNumber;
} }
} }
@ -122,114 +122,70 @@ public class NoteItemData {
if (mName == null) { if (mName == null) {
mName = ""; mName = "";
} }
checkPostion(cursor); // 检查便签项的位置状态 checkPostion(cursor);
} }
/**
* 便
* @param cursor
*/
private void checkPostion(Cursor cursor) { private void checkPostion(Cursor cursor) {
mIsLastItem = cursor.isLast(); // 是否是最后一个便签项 mIsLastItem = cursor.isLast() ? true : false;
mIsFirstItem = cursor.isFirst(); // 是否是第一个便签项 mIsFirstItem = cursor.isFirst() ? true : false;
mIsOnlyOneItem = (cursor.getCount() == 1); // 是否只有一个便签项 mIsOnlyOneItem = (cursor.getCount() == 1);
mIsMultiNotesFollowingFolder = false; mIsMultiNotesFollowingFolder = false;
mIsOneNoteFollowingFolder = false; mIsOneNoteFollowingFolder = false;
if (mType == Notes.TYPE_NOTE && !mIsFirstItem) { // 如果是便签且不是第一个便签项 if (mType == Notes.TYPE_NOTE && !mIsFirstItem) {
int position = cursor.getPosition(); int position = cursor.getPosition();
if (cursor.moveToPrevious()) { // 移动到前一个便签项 if (cursor.moveToPrevious()) {
if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER
|| cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) { // 如果前一个便签项是文件夹或系统便签 || cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) {
if (cursor.getCount() > (position + 1)) { // 如果便签项数量大于当前便签项的位置+1 if (cursor.getCount() > (position + 1)) {
mIsMultiNotesFollowingFolder = true; // 多个便签项跟随文件夹 mIsMultiNotesFollowingFolder = true;
} else { } else {
mIsOneNoteFollowingFolder = true; // 一个便签项跟随文件夹 mIsOneNoteFollowingFolder = true;
} }
} }
if (!cursor.moveToNext()) { // 移动回当前便签项 if (!cursor.moveToNext()) {
throw new IllegalStateException("cursor move to previous but can't move back"); throw new IllegalStateException("cursor move to previous but can't move back");
} }
} }
} }
} }
/**
* 便
* @return 便
*/
public boolean isOneFollowingFolder() { public boolean isOneFollowingFolder() {
return mIsOneNoteFollowingFolder; return mIsOneNoteFollowingFolder;
} }
/**
* 便
* @return 便
*/
public boolean isMultiFollowingFolder() { public boolean isMultiFollowingFolder() {
return mIsMultiNotesFollowingFolder; return mIsMultiNotesFollowingFolder;
} }
/**
* 便
* @return 便
*/
public boolean isLast() { public boolean isLast() {
return mIsLastItem; return mIsLastItem;
} }
/**
*
* @return
*/
public String getCallName() { public String getCallName() {
return mName; return mName;
} }
/**
* 便
* @return 便
*/
public boolean isFirst() { public boolean isFirst() {
return mIsFirstItem; return mIsFirstItem;
} }
/**
* 便
* @return 便
*/
public boolean isSingle() { public boolean isSingle() {
return mIsOnlyOneItem; return mIsOnlyOneItem;
} }
/**
* 便
* @return
*/
public int getTop(){ public int getTop(){
return mTop; return mTop;
} }
/**
* 便ID
* @return 便ID
*/
public long getId() { public long getId() {
return mId; return mId;
} }
/**
* 便
* @return
*/
public long getAlertDate() { public long getAlertDate() {
return mAlertDate; return mAlertDate;
} }
/**
* 便
* @return
*/
public long getCreatedDate() { public long getCreatedDate() {
return mCreatedDate; return mCreatedDate;
} }

@ -14,251 +14,260 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.text.TextUtils; import android.content.Context;
import android.content.Context; import android.database.Cursor;
import android.database.Cursor; import android.util.Log;
import android.util.Log; import android.view.View;
import android.view.View; import android.view.ViewGroup;
import android.view.ViewGroup; import android.widget.CursorAdapter;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import net.micode.notes.data.Notes;
import java.util.Collection; import net.micode.notes.data.Notes;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
public class NotesListAdapter extends CursorAdapter {
private static final String TAG = "NotesListAdapter";
private Context mContext;
/** 键-值对值为true时表示被选中 @zhoukexing 2024/1/2 20:50 */
private HashMap<Integer, Boolean> mSelectedIndex;
// private HashMap<Integer, String> mSnippetPosition;
private int mNotesCount;
private boolean mChoiceMode;
// private String mQuery; // 搜索串
public static class AppWidgetAttribute { /*
public int widgetId; * 便CursorAdaptercursorListView
public int widgetType; * NotesListAdapter便
} */
public class NotesListAdapter extends CursorAdapter {
private static final String TAG = "NotesListAdapter";
private Context mContext;
private HashMap<Integer, Boolean> mSelectedIndex;
private int mNotesCount; //便签数
private boolean mChoiceMode; //选择模式标记
public NotesListAdapter(Context context, Cursor cursor) { /*
super(context, cursor); * widget
mSelectedIndex = new HashMap<Integer, Boolean>(); */
// mSnippetPosition = new HashMap<Integer, String>(); public static class AppWidgetAttribute {
mContext = context; public int widgetId;
mNotesCount = 0; public int widgetType;
// mQuery=""; };
}
// @Override /*
// public View getView(int position, View convertView, ViewGroup parent) { * 便
// View view = super.getView(position, convertView, parent); *
// // 仅显示便签项中包含搜索串的便签 */
// String snippet = mSnippetPosition.get(position); public NotesListAdapter(Context context) {
// Log.e(TAG, snippet); super(context, null); //父类对象置空
// if(!TextUtils.isEmpty(mQuery) && !mSnippetPosition.get(position).contains(mQuery)) { mSelectedIndex = new HashMap<Integer, Boolean>(); //新建选项下标的hash表
// view.setVisibility(View.GONE); mContext = context;
// LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0,1); mNotesCount = 0;
// view.setLayoutParams(param); }
// }
// return view;
// }
@Override @Override
public View newView(Context context, Cursor cursor, ViewGroup parent) { /*
return new NotesListItem(context); *
} * 使NotesListItem
*/
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new NotesListItem(context);
}
@Override /*
public void bindView(View view, Context context, Cursor cursor) { *
if (view instanceof NotesListItem) { *
NoteItemData itemData = new NoteItemData(context, cursor); */
((NotesListItem) view).bind(context, itemData, mChoiceMode, @Override
isSelectedItem(cursor.getPosition())); public void bindView(View view, Context context, Cursor cursor) {
} if (view instanceof NotesListItem) {
} //若view是NotesListItem的一个实例
NoteItemData itemData = new NoteItemData(context, cursor);
((NotesListItem) view).bind(context, itemData, mChoiceMode,
isSelectedItem(cursor.getPosition()));
//则新建一个项目选项并且用bind跟将view和鼠标内容便签数据捆绑在一起
}
}
public void setCheckedItem(final int position, final boolean checked) { /*
mSelectedIndex.put(position, checked); *
notifyDataSetChanged(); *
} */
public void setCheckedItem(final int position, final boolean checked) {
mSelectedIndex.put(position, checked);
//根据定位和是否勾选设置下标
notifyDataSetChanged();
//在修改后刷新activity
}
public boolean isInChoiceMode() { /*
return mChoiceMode; *
} */
public boolean isInChoiceMode() {
return mChoiceMode;
}
public void setChoiceMode(boolean mode) { /*
mSelectedIndex.clear(); *
mChoiceMode = mode; * mode
} */
public void setChoiceMode(boolean mode) {
mSelectedIndex.clear();
mChoiceMode = mode;
}
/** /*
* @method: selectAll *
* @description: boolean *
* setCheckedItemmSelectedIndex */
* public void selectAll(boolean checked) {
* or Cursor cursor = getCursor();
* @date: 2024/1/3 22:36 //获取光标位置
* @author: zhoukexing for (int i = 0; i < getCount(); i++) {
* @param: [checked] if (cursor.moveToPosition(i)) {
* @return: void if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) {
*/ setCheckedItem(i, checked);
public void selectAll(boolean checked) { }
Cursor cursor = getCursor(); }
for (int i = 0; i < getCount(); i++) { }
if (cursor.moveToPosition(i)) { //遍历所有光标可用的位置在判断为便签类型之后勾选单项框
if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { }
setCheckedItem(i, checked);
}
}
}
}
public HashSet<Long> getSelectedItemIds() { /*
HashSet<Long> itemSet = new HashSet<Long>(); *
for (Integer position : mSelectedIndex.keySet()) { *
if (mSelectedIndex.get(position) == true) { */
Long id = getItemId(position); public HashSet<Long> getSelectedItemIds() {
if (id == Notes.ID_ROOT_FOLDER) { HashSet<Long> itemSet = new HashSet<Long>();
Log.d(TAG, "Wrong item id, should not happen"); //建立hash表
} else { for (Integer position : mSelectedIndex.keySet()) {
itemSet.add(id); //遍历所有的关键
} if (mSelectedIndex.get(position) == true) {
} //若光标位置可用
} Long id = getItemId(position);
if (id == Notes.ID_ROOT_FOLDER) {
//原文件不需要添加
Log.d(TAG, "Wrong item id, should not happen");
} else {
itemSet.add(id);
}
//则将id该下标假如选项集合中
return itemSet; }
} }
public HashSet<AppWidgetAttribute> getSelectedWidget() { return itemSet;
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>(); }
for (Integer position : mSelectedIndex.keySet()) {
if (mSelectedIndex.get(position) == true) {
Cursor c = (Cursor) getItem(position);
if (c != null) {
AppWidgetAttribute widget = new AppWidgetAttribute();
NoteItemData item = new NoteItemData(mContext, c);
widget.widgetId = item.getWidgetId();
widget.widgetType = item.getWidgetType();
itemSet.add(widget);
/**
* Don't close cursor here, only the adapter could close it
*/
} else {
Log.e(TAG, "Invalid cursor");
return null;
}
}
}
return itemSet;
}
public int getSelectedCount() { /*
Collection<Boolean> values = mSelectedIndex.values(); * Widget
if (null == values) { *
return 0; */
} public HashSet<AppWidgetAttribute> getSelectedWidget() {
Iterator<Boolean> iter = values.iterator(); HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
int count = 0; for (Integer position : mSelectedIndex.keySet()) {
while (iter.hasNext()) { if (mSelectedIndex.get(position) == true) {
if (true == iter.next()) { Cursor c = (Cursor) getItem(position);
count++; //以上4句和getSelectedItemIds一样不再重复
} if (c != null) {
} //光标位置可用的话就建立新的Widget属性并编辑下标和类型最后添加到选项集中
return count; AppWidgetAttribute widget = new AppWidgetAttribute();
} NoteItemData item = new NoteItemData(mContext, c);
widget.widgetId = item.getWidgetId();
widget.widgetType = item.getWidgetType();
itemSet.add(widget);
/**
* Don't close cursor here, only the adapter could close it
*/
} else {
Log.e(TAG, "Invalid cursor");
return null;
}
}
}
return itemSet;
}
public int getMNotesCount() { /*
return mNotesCount; *
} *
*/
public int getSelectedCount() {
Collection<Boolean> values = mSelectedIndex.values();
//首先获取选项下标的值
if (null == values) {
return 0;
}
Iterator<Boolean> iter = values.iterator();
//初始化叠加器
int count = 0;
while (iter.hasNext()) {
if (true == iter.next()) {
//若value值为真计数+1
count++;
}
}
return count;
}
/** /*
* @method: isAllSelected *
* @description: truefalse *
* @date: 2024/1/2 20:47 */
* @author: zhoukexing public boolean isAllSelected() {
* @param: [] int checkedCount = getSelectedCount();
* @return: boolean return (checkedCount != 0 && checkedCount == mNotesCount);
*/ //获取选项数看是否等于便签的个数
public boolean isAllSelected() { }
int checkedCount = getSelectedCount();
return (checkedCount != 0 && checkedCount == mNotesCount);
}
// public void setmQuery(String Query) { /*
// mQuery = Query; *
// } *
*/
public boolean isSelectedItem(final int position) {
if (null == mSelectedIndex.get(position)) {
return false;
}
return mSelectedIndex.get(position);
}
public boolean isSelectedItem(final int position) { @Override
if (null == mSelectedIndex.get(position)) { /*
return false; * activity便
} *
return mSelectedIndex.get(position); */
} protected void onContentChanged() {
super.onContentChanged();
//执行基类函数
calcNotesCount();
}
@Override @Override
protected void onContentChanged() { /*
super.onContentChanged(); * activity便
calcNotesCount(); */
// updateSnippetMap(); // 在数据发生改变时更新 Snippet 到 Position 的映射 public void changeCursor(Cursor cursor) {
} super.changeCursor(cursor);
//执行基类函数
calcNotesCount();
}
/** /*
* @Method changeCursor * 便
* @Date 2024/1/18 15:36 *
* @param cursor */
* @Author lenovo private void calcNotesCount() {
* @Return void mNotesCount = 0;
* @Description cursor for (int i = 0; i < getCount(); i++) {
*/ //获取总数同时遍历
@Override Cursor c = (Cursor) getItem(i);
public void changeCursor(Cursor cursor) { if (c != null) {
super.changeCursor(cursor); if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
calcNotesCount(); mNotesCount++;
// updateSnippetMap(); // 在数据发生改变时更新 Snippet 到 Position 的映射 //若该位置不为空并且文本类型为便签就+1
} }
} else {
// private void updateSnippetMap() { Log.e(TAG, "Invalid cursor");
// mSnippetPosition = new HashMap<Integer, String>(); return;
// for (int i = 0; i < getCount(); i++) { }
// Cursor c = (Cursor) getItem(i); //否则报错
// if (c != null) { }
// int position = c.getPosition(); }
// NoteItemData item = new NoteItemData(mContext, c); }
// mSnippetPosition.put(position, item.getSnippet());
// } else {
// Log.e(TAG, "Invalid cursor");
// break;
// }
// }
// }
/**
* @Method calcNotesCount
* @Date 2024/1/18 15:36
* @Author lenovo
* @Return void
* @Description 便
*/
private void calcNotesCount() {
mNotesCount = 0;
for (int i = 0; i < getCount(); i++) {
Cursor c = (Cursor) getItem(i);
if (c != null) {
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
mNotesCount++;
}
} else {
Log.e(TAG, "Invalid cursor");
return;
}
}
}
}

@ -14,126 +14,119 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.content.Context; import android.content.Context;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.view.View; import android.view.View;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
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.tool.DataUtils; import net.micode.notes.tool.DataUtils;
import net.micode.notes.tool.ResourceParser.NoteItemBgResources; import net.micode.notes.tool.ResourceParser.NoteItemBgResources;
/** //创建便签列表项目选项
* @Package: public class NotesListItem extends LinearLayout {
* @Class: NotesListItem private ImageView mAlert;//闹钟图片
* @Author lenovo private TextView mTitle; //标题
* @Date 2024/1/18 15:43 private TextView mTime; //时间
* @Description: 便 private TextView mCallName; //
*/ private NoteItemData mItemData; //标签数据
public class NotesListItem extends LinearLayout { private CheckBox mCheckBox; //打钩框
private ImageView mAlert;
private ImageView mTop;
private TextView mTitle;
private TextView mTime;
private TextView mCallName;
private NoteItemData mItemData;
private CheckBox mCheckBox;
public NotesListItem(Context context) {
super(context);
inflate(context, R.layout.note_item, this);
mAlert = (ImageView) findViewById(R.id.iv_alert_icon);
mTop = (ImageView) findViewById(R.id.iv_top_icon);
mTitle = (TextView) findViewById(R.id.tv_title);
mTime = (TextView) findViewById(R.id.tv_time);
mCallName = (TextView) findViewById(R.id.tv_name);
mCheckBox = (CheckBox) findViewById(android.R.id.checkbox);
}
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
mCheckBox.setVisibility(View.VISIBLE);
mCheckBox.setChecked(checked);
} else {
mCheckBox.setVisibility(View.GONE);
}
mItemData = data;
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
mCallName.setVisibility(View.GONE);
mAlert.setVisibility(View.VISIBLE);
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
mTitle.setText(context.getString(R.string.call_record_folder_name)
+ context.getString(R.string.format_folder_files_count, data.getNotesCount()));
mAlert.setImageResource(R.drawable.call_record);
} else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) {
mCallName.setVisibility(View.VISIBLE);
mCallName.setText(data.getCallName());
mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem);
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
if (data.hasAlert()) {
mAlert.setImageResource(R.drawable.clock);
mAlert.setVisibility(View.VISIBLE);
} else {
mAlert.setVisibility(View.GONE);
}
} else {
mCallName.setVisibility(View.GONE);
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
if (data.getType() == Notes.TYPE_FOLDER) {
mTitle.setText(data.getSnippet()
+ context.getString(R.string.format_folder_files_count,
data.getNotesCount()));
mAlert.setVisibility(View.GONE);
} else {
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
if (data.hasAlert()) {
mAlert.setImageResource(R.drawable.clock);
mAlert.setVisibility(View.VISIBLE);
} else {
mAlert.setVisibility(View.GONE);
}
}
}
mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
// 设置置顶图标
if(data.getTop() == 1){
mTop.setImageResource(R.drawable.set_top);
mTop.setVisibility(View.VISIBLE);
}
else{
mTop.setVisibility(View.GONE);
}
setBackground(data); /*初始化基本信息*/
} public NotesListItem(Context context) {
super(context); //super()它的主要作用是调整调用父类构造函数的顺序
inflate(context, R.layout.note_item, this);//Inflate可用于将一个xml中定义的布局控件找出来,这里的xml是r。layout
//findViewById用于从contentView中查找指定ID的View转换出来的形式根据需要而定;
mAlert = (ImageView) findViewById(R.id.iv_alert_icon);
mTitle = (TextView) findViewById(R.id.tv_title);
mTime = (TextView) findViewById(R.id.tv_time);
mCallName = (TextView) findViewById(R.id.tv_name);
mCheckBox = (CheckBox) findViewById(android.R.id.checkbox);
}
///根据data的属性对各个控件的属性的控制主要是可见性Visibility内容setText格式setTextAppearance
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
mCheckBox.setVisibility(View.VISIBLE); ///设置可见行为可见
mCheckBox.setChecked(checked); ///格子打钩
} else {
mCheckBox.setVisibility(View.GONE);
}
private void setBackground(NoteItemData data) { mItemData = data;
int id = data.getBgColorId(); ///设置控件属性一共三种情况由data的id和父id是否与保存到文件夹的id一致来决定
if (data.getType() == Notes.TYPE_NOTE) { if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
if (data.isSingle() || data.isOneFollowingFolder()) { mCallName.setVisibility(View.GONE);
setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id)); mAlert.setVisibility(View.VISIBLE);
} else if (data.isLast()) { //设置该textview的style
setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id)); mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
} else if (data.isFirst() || data.isMultiFollowingFolder()) { //settext为设置内容
setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id)); mTitle.setText(context.getString(R.string.call_record_folder_name)
} else { + context.getString(R.string.format_folder_files_count, data.getNotesCount()));
setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id)); mAlert.setImageResource(R.drawable.call_record);
} } else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) {
} else { mCallName.setVisibility(View.VISIBLE);
setBackgroundResource(NoteItemBgResources.getFolderBgRes()); mCallName.setText(data.getCallName());
} mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem);
} mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
///关于闹钟的设置
if (data.hasAlert()) {
mAlert.setImageResource(R.drawable.clock);//图片来源的设置
mAlert.setVisibility(View.VISIBLE);
} else {
mAlert.setVisibility(View.GONE);
}
} else {
mCallName.setVisibility(View.GONE);
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
///设置title格式
if (data.getType() == Notes.TYPE_FOLDER) {
mTitle.setText(data.getSnippet()
+ context.getString(R.string.format_folder_files_count,
data.getNotesCount()));
mAlert.setVisibility(View.GONE);
} else {
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
if (data.hasAlert()) {
mAlert.setImageResource(R.drawable.clock);///设置图片来源
mAlert.setVisibility(View.VISIBLE);
} else {
mAlert.setVisibility(View.GONE);
}
}
}
///设置内容获取相关时间从data里编辑的日期中获取
mTime. setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
public NoteItemData getItemData() { setBackground(data);
return mItemData; }
} //根据data的文件属性来设置背景
} private void setBackground(NoteItemData data) {
int id = data.getBgColorId();
//若是note型文件则4种情况对于4种不同情况的背景来源
if (data.getType() == Notes.TYPE_NOTE) {
//单个数据并且只有一个子文件夹
if (data.isSingle() || data.isOneFollowingFolder()) {
setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id));
} else if (data.isLast()) {//是最后一个数据
setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id));
} else if (data.isFirst() || data.isMultiFollowingFolder()) {//是一个数据并有多个子文件夹
setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id));
} else {
setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id));
}
} else {
//若不是note直接调用文件夹的背景来源
setBackgroundResource(NoteItemBgResources.getFolderBgRes());
}
}
public NoteItemData getItemData() {
return mItemData;
}
}

@ -14,375 +14,517 @@
* limitations under the License. * limitations under the License.
*/ */
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;
import android.app.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;
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;
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.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;
import net.micode.notes.gtask.remote.GTaskSyncService; import net.micode.notes.gtask.remote.GTaskSyncService;
/*
public class NotesPreferenceActivity extends PreferenceActivity { *NotesPreferenceActivity便
public static final String PREFERENCE_NAME = "notes_preferences"; * PreferenceActivityActivity
*/
public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name"; public class NotesPreferenceActivity extends PreferenceActivity {
public static final String PREFERENCE_NAME = "notes_preferences";
public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time"; //优先名
public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name";
public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear"; //同步账号
public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time";
private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key"; //同步时间
public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear";
private static final String AUTHORITIES_FILTER_KEY = "authorities";
private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key";
private PreferenceCategory mAccountCategory; //同步密码
private static final String AUTHORITIES_FILTER_KEY = "authorities";
private GTaskReceiver mReceiver; //本地密码
private PreferenceCategory mAccountCategory;
private Account[] mOriAccounts; //账户分组
private GTaskReceiver mReceiver;
private boolean mHasAddedAccount; //同步任务接收器
private Account[] mOriAccounts;
@Override //账户
protected void onCreate(Bundle icicle) { private boolean mHasAddedAccount;
super.onCreate(icicle); //账户的hash标记
/* using the app icon for navigation */ @Override
getActionBar().setDisplayHomeAsUpEnabled(true); /*
*activity
addPreferencesFromResource(R.xml.preferences); *Bundle icicle activity
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY); *
mReceiver = new GTaskReceiver(); */
IntentFilter filter = new IntentFilter(); protected void onCreate(Bundle icicle) {
filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME); //先执行父类的创建函数
registerReceiver(mReceiver, filter); super.onCreate(icicle);
mOriAccounts = null; /* using the app icon for navigation */
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null); getActionBar().setDisplayHomeAsUpEnabled(true);
getListView().addHeaderView(header, null, true); //给左上角图标的左边加上一个返回的图标
}
addPreferencesFromResource(R.xml.preferences);
@Override //添加xml来源并显示 xml
protected void onResume() { mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY);
super.onResume(); //根据同步账户关键码来初始化分组
mReceiver = new GTaskReceiver();
// need to set sync account automatically if user has added a new IntentFilter filter = new IntentFilter();
// account filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME);
if (mHasAddedAccount) { registerReceiver(mReceiver, filter);
Account[] accounts = getGoogleAccounts(); //初始化同步组件
if (mOriAccounts != null && accounts.length > mOriAccounts.length) {
for (Account accountNew : accounts) { mOriAccounts = null;
boolean found = false; View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null);
for (Account accountOld : mOriAccounts) { //获取listvivewListView的作用:用于列出所有选择
if (TextUtils.equals(accountOld.name, accountNew.name)) { getListView().addHeaderView(header, null, true);
found = true; //在listview组件上方添加其他组件
break; }
}
} @Override
if (!found) { /*
setSyncAccount(accountNew.name); * activity
break; *
} */
} protected void onResume() {
} //先执行父类 的交互实现
} super.onResume();
refreshUI(); // need to set sync account automatically if user has added a new
} // account
if (mHasAddedAccount) {
@Override //若用户新加了账户则自动设置同步账户
protected void onDestroy() { Account[] accounts = getGoogleAccounts();
if (mReceiver != null) { //获取google同步账户
unregisterReceiver(mReceiver); if (mOriAccounts != null && accounts.length > mOriAccounts.length) {
} //若原账户不为空且当前账户有增加
super.onDestroy(); for (Account accountNew : accounts) {
} boolean found = false;
for (Account accountOld : mOriAccounts) {
private void loadAccountPreference() { if (TextUtils.equals(accountOld.name, accountNew.name)) {
mAccountCategory.removeAll(); //更新账户
found = true;
Preference accountPref = new Preference(this); break;
final String defaultAccount = getSyncAccountName(this); }
accountPref.setTitle(getString(R.string.preferences_account_title)); }
accountPref.setSummary(getString(R.string.preferences_account_summary)); if (!found) {
accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { setSyncAccount(accountNew.name);
public boolean onPreferenceClick(Preference preference) { //若是没有找到旧的账户,那么同步账号中就只添加新账户
if (!GTaskSyncService.isSyncing()) { break;
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 refreshUI();
showChangeAccountConfirmAlertDialog(); //刷新标签界面
} }
} else {
Toast.makeText(NotesPreferenceActivity.this, @Override
R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT) /*
.show(); * activity
} *
return true; */
} protected void onDestroy() {
}); if (mReceiver != null) {
unregisterReceiver(mReceiver);
mAccountCategory.addPreference(accountPref); //注销接收器
} }
super.onDestroy();
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() { private void loadAccountPreference() {
public void onClick(View v) { mAccountCategory.removeAll();
GTaskSyncService.cancelSync(NotesPreferenceActivity.this); //销毁所有的分组
} Preference accountPref = new Preference(this);
}); //建立首选项
} else { final String defaultAccount = getSyncAccountName(this);
syncButton.setText(getString(R.string.preferences_button_sync_immediately)); accountPref.setTitle(getString(R.string.preferences_account_title));
syncButton.setOnClickListener(new View.OnClickListener() { accountPref.setSummary(getString(R.string.preferences_account_summary));
public void onClick(View v) { //设置首选项的大标题和小标题
GTaskSyncService.startSync(NotesPreferenceActivity.this); accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
} public boolean onPreferenceClick(Preference preference) {
}); //建立监听器
} if (!GTaskSyncService.isSyncing()) {
syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this))); if (TextUtils.isEmpty(defaultAccount)) {
// the first time to set account
// set last sync time //若是第一次建立账户显示选择账户提示对话框
if (GTaskSyncService.isSyncing()) { showSelectAccountAlertDialog();
lastSyncTimeView.setText(GTaskSyncService.getProgressString()); } else {
lastSyncTimeView.setVisibility(View.VISIBLE); // if the account has already been set, we need to promp
} else { // user about the risk
long lastSyncTime = getLastSyncTime(this); //若是已经建立则显示修改对话框并进行修改操作
if (lastSyncTime != 0) { showChangeAccountConfirmAlertDialog();
lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time, }
DateFormat.format(getString(R.string.preferences_last_sync_time_format), } else {
lastSyncTime))); //若在没有同步的情况下则在toast中显示不能修改
lastSyncTimeView.setVisibility(View.VISIBLE); Toast.makeText(NotesPreferenceActivity.this,
} else { R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
lastSyncTimeView.setVisibility(View.GONE); .show();
} }
} return true;
} }
});
private void refreshUI() {
loadAccountPreference(); //根据新建首选项编辑新的账户分组
loadSyncButton(); mAccountCategory.addPreference(accountPref);
} }
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); private void loadSyncButton() {
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title)); Button syncButton = (Button) findViewById(R.id.preference_sync_button);
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle); TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips)); //获取同步按钮控件和最终同步时间的的窗口
// set button state
dialogBuilder.setCustomTitle(titleView); //设置按钮的状态
dialogBuilder.setPositiveButton(null, null); if (GTaskSyncService.isSyncing()) {
//若是在同步状态下
Account[] accounts = getGoogleAccounts(); syncButton.setText(getString(R.string.preferences_button_sync_cancel));
String defAccount = getSyncAccountName(this); syncButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mOriAccounts = accounts; GTaskSyncService.cancelSync(NotesPreferenceActivity.this);
mHasAddedAccount = false; }
});
if (accounts.length > 0) { //设置按钮显示的文本为“取消同步”以及监听器
CharSequence[] items = new CharSequence[accounts.length]; } else {
final CharSequence[] itemMapping = items; syncButton.setText(getString(R.string.preferences_button_sync_immediately));
int checkedItem = -1; syncButton.setOnClickListener(new View.OnClickListener() {
int index = 0; public void onClick(View v) {
for (Account account : accounts) { GTaskSyncService.startSync(NotesPreferenceActivity.this);
if (TextUtils.equals(account.name, defAccount)) { }
checkedItem = index; });
} //若是不同步则设置按钮显示的文本为“立即同步”以及对应监听器
items[index++] = account.name; }
} syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this)));
dialogBuilder.setSingleChoiceItems(items, checkedItem, //设置按键可用还是不可用
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { // set last sync time
setSyncAccount(itemMapping[which].toString()); // 设置最终同步时间
dialog.dismiss(); if (GTaskSyncService.isSyncing()) {
refreshUI(); //若是在同步的情况下
} lastSyncTimeView.setText(GTaskSyncService.getProgressString());
}); lastSyncTimeView.setVisibility(View.VISIBLE);
} // 根据当前同步服务器设置时间显示框的文本以及可见性
} else {
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null); //若是非同步情况
dialogBuilder.setView(addAccountView); long lastSyncTime = getLastSyncTime(this);
if (lastSyncTime != 0) {
final AlertDialog dialog = dialogBuilder.show(); lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time,
addAccountView.setOnClickListener(new View.OnClickListener() { DateFormat.format(getString(R.string.preferences_last_sync_time_format),
public void onClick(View v) { lastSyncTime)));
mHasAddedAccount = true; lastSyncTimeView.setVisibility(View.VISIBLE);
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS"); //则根据最后同步时间的信息来编辑时间显示框的文本内容和可见性
intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] { } else {
"gmail-ls" //若时间为空直接设置为不可见状态
}); lastSyncTimeView.setVisibility(View.GONE);
startActivityForResult(intent, -1); }
dialog.dismiss(); }
} }
}); /*
} *
*
private void showChangeAccountConfirmAlertDialog() { */
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); private void refreshUI() {
loadAccountPreference();
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null); loadSyncButton();
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); */
private void showSelectAccountAlertDialog() {
CharSequence[] menuItemArray = new CharSequence[] { AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
getString(R.string.preferences_menu_change_account), //创建一个新的对话框
getString(R.string.preferences_menu_remove_account),
getString(R.string.preferences_menu_cancel) View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
}; TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() { titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
public void onClick(DialogInterface dialog, int which) { TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
if (which == 0) { subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips));
showSelectAccountAlertDialog(); //设置标题以及子标题的内容
} else if (which == 1) { dialogBuilder.setCustomTitle(titleView);
removeSyncAccount(); dialogBuilder.setPositiveButton(null, null);
refreshUI(); //设置对话框的自定义标题建立一个YES的按钮
} Account[] accounts = getGoogleAccounts();
} String defAccount = getSyncAccountName(this);
}); //获取同步账户信息
dialogBuilder.show(); mOriAccounts = accounts;
} mHasAddedAccount = false;
private Account[] getGoogleAccounts() { if (accounts.length > 0) {
AccountManager accountManager = AccountManager.get(this); //若账户不为空
return accountManager.getAccountsByType("com.google"); CharSequence[] items = new CharSequence[accounts.length];
} final CharSequence[] itemMapping = items;
int checkedItem = -1;
private void setSyncAccount(String account) { int index = 0;
if (!getSyncAccountName(this).equals(account)) { for (Account account : accounts) {
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); if (TextUtils.equals(account.name, defAccount)) {
SharedPreferences.Editor editor = settings.edit(); checkedItem = index;
if (account != null) { //在账户列表中查询到所需账户
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account); }
} else { items[index++] = account.name;
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); }
} dialogBuilder.setSingleChoiceItems(items, checkedItem,
editor.commit(); //在对话框建立一个单选的复选框
new DialogInterface.OnClickListener() {
// clean up last sync time public void onClick(DialogInterface dialog, int which) {
setLastSyncTime(this, 0); setSyncAccount(itemMapping[which].toString());
dialog.dismiss();
// clean up local gtask related info //取消对话框
new Thread(new Runnable() { refreshUI();
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(); View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null);
dialogBuilder.setView(addAccountView);
Toast.makeText(NotesPreferenceActivity.this, //给新加账户对话框设置自定义样式
getString(R.string.preferences_toast_success_set_accout, account),
Toast.LENGTH_SHORT).show(); final AlertDialog dialog = dialogBuilder.show();
} //显示对话框
} addAccountView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
private void removeSyncAccount() { mHasAddedAccount = true;
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); //将新加账户的hash置true
SharedPreferences.Editor editor = settings.edit(); Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) { //建立网络建立组件
editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME); intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] {
} "gmail-ls"
if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) { });
editor.remove(PREFERENCE_LAST_SYNC_TIME); startActivityForResult(intent, -1);
} //跳回上一个选项
editor.commit(); dialog.dismiss();
}
// clean up local gtask related info });
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(); private void showChangeAccountConfirmAlertDialog() {
} AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
//创建一个新的对话框
public static String getSyncAccountName(Context context) { View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
Context.MODE_PRIVATE); titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); getSyncAccountName(this)));
} TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg));
public static void setLastSyncTime(Context context, long time) { //根据同步修改的账户信息设置标题以及子标题的内容
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, dialogBuilder.setCustomTitle(titleView);
Context.MODE_PRIVATE); //设置对话框的自定义标题
SharedPreferences.Editor editor = settings.edit(); CharSequence[] menuItemArray = new CharSequence[] {
editor.putLong(PREFERENCE_LAST_SYNC_TIME, time); getString(R.string.preferences_menu_change_account),
editor.commit(); getString(R.string.preferences_menu_remove_account),
} getString(R.string.preferences_menu_cancel)
};
public static long getLastSyncTime(Context context) { //定义一些标记字符串
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() {
Context.MODE_PRIVATE); //设置对话框要显示的一个list用于显示几个命令时,即changeremovecancel
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0); public void onClick(DialogInterface dialog, int which) {
} //按键功能由which来决定
if (which == 0) {
private class GTaskReceiver extends BroadcastReceiver { //进入账户选择对话框
showSelectAccountAlertDialog();
@Override } else if (which == 1) {
public void onReceive(Context context, Intent intent) { //删除账户并且跟新便签界面
refreshUI(); removeSyncAccount();
if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) { refreshUI();
TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview); }
syncStatus.setText(intent }
.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG)); });
} dialogBuilder.show();
//显示对话框
} }
}
/*
public boolean onOptionsItemSelected(MenuItem item) { *
switch (item.getItemId()) { *
case android.R.id.home: */
Intent intent = new Intent(this, NotesListActivity.class); private Account[] getGoogleAccounts() {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AccountManager accountManager = AccountManager.get(this);
startActivity(intent); return accountManager.getAccountsByType("com.google");
return true; }
default:
return false; /*
} *
} *
} */
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);
//将最后同步时间清零
// clean up local gtask related info
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();
//将toast的文本信息置为“设置账户成功”并显示出来
}
}
/*
*
*
*/
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();
//提交更新后的数据
// clean up local gtask related info
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);
}
/*
*
* BroadcastReceiver
*/
private class GTaskReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
refreshUI();
if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) {
//获取随广播而来的Intent中的同步服务的数据
TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
syncStatus.setText(intent
.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG));
//通过获取的数据在设置系统的状态
}
}
}
/*
*
*
* :MenuItem
*/
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
//根据选项的id选择这里只有一个主页
case android.R.id.home:
Intent intent = new Intent(this, NotesListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
//在主页情况下在创建连接组件intent发出清空的信号并开始一个相应的activity
default:
return false;
}
}
}

Loading…
Cancel
Save