新增文件夹功能,密码登入;新增便签修改时间。

pull/4/head
frh 1 month ago
parent 9c4868f2ea
commit dece766fbf

@ -64,10 +64,6 @@ public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
}
private void showNotification(int tickerId, String content) {
Notification notification = new Notification(R.drawable.notification, mContext
.getString(tickerId), System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.flags = Notification.FLAG_AUTO_CANCEL;
PendingIntent pendingIntent;
if (tickerId != R.string.ticker_success) {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
@ -77,11 +73,20 @@ public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
NotesListActivity.class), 0);
}
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
pendingIntent);
Notification.Builder builder = new Notification.Builder(mContext)
.setAutoCancel(true)
.setContentTitle(mContext.getString(R.string.app_name))
.setContentText(content)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setOngoing(true);
Notification notification=builder.getNotification();
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
}
@Override
protected Integer doInBackground(Void... unused) {
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity

@ -0,0 +1,59 @@
package net.micode.notes.ui;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import net.micode.notes.R;
import net.micode.notes.ui.bean.Note;
import net.micode.notes.util.ToastUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AddActivity extends Activity {
private EditText etContent;
private NoteDbOpenHelper mNoteDbOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
etContent = findViewById(R.id.sf_et_1);
mNoteDbOpenHelper = new NoteDbOpenHelper(this);
}
public void add(View view) {
String content = etContent.getText().toString();
Note note = new Note();
note.setContent(content);
note.setCreatedTime(getCurrentTimeFormat());
long row = mNoteDbOpenHelper.insertData(note);
if(row != -1){
ToastUtil.toastShort(this,"Add Success");
this.finish();
}else{
ToastUtil.toastShort(this,"Add Fail");
}
}
private String getCurrentTimeFormat() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd HH:mm:ss");
Date date = new Date();
return simpleDateFormat.format(date);
}
}

@ -16,456 +16,143 @@
package net.micode.notes.ui;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.os.PowerManager;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.gtask.remote.GTaskSyncService;
import net.micode.notes.tool.DataUtils;
/**
* NotesPreferenceActivity 便
*
*/
public class NotesPreferenceActivity extends PreferenceActivity {
// 偏好设置的名称
public static final String PREFERENCE_NAME = "notes_preferences";
// 同步账户名称的偏好设置键
public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name";
// 上次同步时间的偏好设置键
public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time";
// 设置背景颜色随机显示的偏好设置键
public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear";
// 同步账户的偏好设置类别键
private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key";
// 权限过滤器键
private static final String AUTHORITIES_FILTER_KEY = "authorities";
// 同步账户的偏好设置类别
private PreferenceCategory mAccountCategory;
// 用于接收 GTask 同步服务广播的接收器
private GTaskReceiver mReceiver;
// 原始的 Google 账户数组
private Account[] mOriAccounts;
// 标记是否添加了新账户
private boolean mHasAddedAccount;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
// 设置 ActionBar 可返回
getActionBar().setDisplayHomeAsUpEnabled(true);
// 加载偏好设置资源
addPreferencesFromResource(R.xml.preferences);
// 找到同步账户的偏好设置类别
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY);
// 创建 GTask 广播接收器
mReceiver = new GTaskReceiver();
// 创建意图过滤器,用于接收 GTask 同步服务的广播
IntentFilter filter = new IntentFilter();
filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME);
// 注册广播接收器
registerReceiver(mReceiver, filter);
// 初始化原始账户数组
mOriAccounts = null;
// 加载设置界面的头部视图
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null);
// 将头部视图添加到列表视图中
getListView().addHeaderView(header, null, true);
}
@Override
protected void onResume() {
super.onResume();
import java.io.IOException;
// 如果添加了新账户,自动设置同步账户
if (mHasAddedAccount) {
Account[] accounts = getGoogleAccounts();
if (mOriAccounts != null && accounts.length > mOriAccounts.length) {
for (Account accountNew : accounts) {
boolean found = false;
for (Account accountOld : mOriAccounts) {
if (TextUtils.equals(accountOld.name, accountNew.name)) {
found = true;
break;
}
}
if (!found) {
setSyncAccount(accountNew.name);
break;
}
}
}
}
// 刷新界面
refreshUI();
}
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
private long mNoteId;
private String mSnippet;
private static final int SNIPPET_PREW_MAX_LEN = 60;
MediaPlayer mPlayer;
@Override
protected void onDestroy() {
// 注销广播接收器
if (mReceiver != null) {
unregisterReceiver(mReceiver);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
if (!isScreenOn()) {
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_LAYOUT_INSET_DECOR);
}
super.onDestroy();
}
/**
*
*/
private void loadAccountPreference() {
// 清空账户偏好设置类别中的所有项
mAccountCategory.removeAll();
// 创建一个新的偏好设置项
Preference accountPref = new Preference(this);
// 获取默认的同步账户名称
final String defaultAccount = getSyncAccountName(this);
// 设置偏好设置项的标题
accountPref.setTitle(getString(R.string.preferences_account_title));
// 设置偏好设置项的摘要
accountPref.setSummary(getString(R.string.preferences_account_summary));
// 设置偏好设置项的点击监听器
accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
// 如果当前没有正在同步
if (!GTaskSyncService.isSyncing()) {
// 如果默认账户为空,显示选择账户的对话框
if (TextUtils.isEmpty(defaultAccount)) {
showSelectAccountAlertDialog();
} else {
// 如果已经设置了账户,显示更改账户的确认对话框
showChangeAccountConfirmAlertDialog();
}
} else {
// 如果正在同步,显示提示信息
Toast.makeText(NotesPreferenceActivity.this,
R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
.show();
}
return true;
}
});
// 将账户偏好设置项添加到账户偏好设置类别中
mAccountCategory.addPreference(accountPref);
}
/**
*
*/
private void loadSyncButton() {
// 找到同步按钮和上次同步时间的文本视图
Button syncButton = (Button) findViewById(R.id.preference_sync_button);
TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
// 根据同步状态设置按钮的文本和点击监听器
if (GTaskSyncService.isSyncing()) {
syncButton.setText(getString(R.string.preferences_button_sync_cancel));
syncButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 取消同步
GTaskSyncService.cancelSync(NotesPreferenceActivity.this);
}
});
} else {
syncButton.setText(getString(R.string.preferences_button_sync_immediately));
syncButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 立即开始同步
GTaskSyncService.startSync(NotesPreferenceActivity.this);
}
});
Intent intent = getIntent();
try {
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0,
SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info)
: mSnippet;
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
}
// 如果同步账户不为空,启用同步按钮
syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this)));
// 根据同步状态设置上次同步时间的显示
if (GTaskSyncService.isSyncing()) {
lastSyncTimeView.setText(GTaskSyncService.getProgressString());
lastSyncTimeView.setVisibility(View.VISIBLE);
mPlayer = new MediaPlayer();
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
showActionDialog();
playAlarmSound();
} else {
long lastSyncTime = getLastSyncTime(this);
if (lastSyncTime != 0) {
lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time,
DateFormat.format(getString(R.string.preferences_last_sync_time_format),
lastSyncTime)));
lastSyncTimeView.setVisibility(View.VISIBLE);
} else {
lastSyncTimeView.setVisibility(View.GONE);
}
finish();
}
}
/**
*
*/
private void refreshUI() {
loadAccountPreference();
loadSyncButton();
private boolean isScreenOn() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
}
/**
*
*/
private void showSelectAccountAlertDialog() {
// 创建一个 AlertDialog 构建器
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// 加载账户选择对话框的标题视图
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips));
private void playAlarmSound() {
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
// 设置对话框的自定义标题
dialogBuilder.setCustomTitle(titleView);
dialogBuilder.setPositiveButton(null, null);
int silentModeStreams = Settings.System.getInt(getContentResolver(),
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
// 获取所有的 Google 账户
Account[] accounts = getGoogleAccounts();
// 获取默认的同步账户名称
String defAccount = getSyncAccountName(this);
// 记录原始账户数组
mOriAccounts = accounts;
// 标记未添加新账户
mHasAddedAccount = false;
if (accounts.length > 0) {
// 创建一个字符序列数组,用于显示账户名称
CharSequence[] items = new CharSequence[accounts.length];
final CharSequence[] itemMapping = items;
int checkedItem = -1;
int index = 0;
for (Account account : accounts) {
if (TextUtils.equals(account.name, defAccount)) {
checkedItem = index;
}
items[index++] = account.name;
}
// 设置单选列表项
dialogBuilder.setSingleChoiceItems(items, checkedItem,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 设置选择的账户为同步账户
setSyncAccount(itemMapping[which].toString());
// 关闭对话框
dialog.dismiss();
// 刷新界面
refreshUI();
}
});
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
mPlayer.setAudioStreamType(silentModeStreams);
} else {
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
}
try {
mPlayer.setDataSource(this, url);
mPlayer.prepare();
mPlayer.setLooping(true);
mPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 加载添加账户的视图
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null);
dialogBuilder.setView(addAccountView);
// 显示对话框
final AlertDialog dialog = dialogBuilder.show();
addAccountView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 标记添加了新账户
mHasAddedAccount = true;
// 创建一个添加账户的意图
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] {
"gmail-ls"
});
// 启动添加账户的活动
startActivityForResult(intent, -1);
// 关闭对话框
dialog.dismiss();
}
});
}
/**
*
*/
private void showChangeAccountConfirmAlertDialog() {
// 创建一个 AlertDialog 构建器
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// 加载账户更改对话框的标题视图
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
getSyncAccountName(this)));
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg));
// 设置对话框的自定义标题
dialogBuilder.setCustomTitle(titleView);
// 创建一个菜单项数组
CharSequence[] menuItemArray = new CharSequence[] {
getString(R.string.preferences_menu_change_account),
getString(R.string.preferences_menu_remove_account),
getString(R.string.preferences_menu_cancel)
};
// 设置菜单项的点击监听器
dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
// 显示选择账户的对话框
showSelectAccountAlertDialog();
} else if (which == 1) {
// 移除同步账户
removeSyncAccount();
// 刷新界面
refreshUI();
}
}
});
// 显示对话框
dialogBuilder.show();
}
/**
* Google
* @return Google
*/
private Account[] getGoogleAccounts() {
// 获取账户管理器
AccountManager accountManager = AccountManager.get(this);
// 获取所有的 Google 账户
return accountManager.getAccountsByType("com.google");
}
/**
*
* @param account
*/
private void setSyncAccount(String account) {
// 如果当前同步账户与要设置的账户不同
if (!getSyncAccountName(this).equals(account)) {
// 获取共享偏好设置
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
if (account != null) {
// 保存同步账户名称
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account);
} else {
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
}
// 提交更改
editor.commit();
// 清空上次同步时间
setLastSyncTime(this, 0);
// 清空本地的 GTask 相关信息
new Thread(new Runnable() {
public void run() {
ContentValues values = new ContentValues();
values.put(NoteColumns.GTASK_ID, "");
values.put(NoteColumns.SYNC_ID, 0);
// 更新所有便签的 GTask 相关信息
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
}
}).start();
// 显示设置成功的提示信息
Toast.makeText(NotesPreferenceActivity.this,
getString(R.string.preferences_toast_success_set_accout, account),
Toast.LENGTH_SHORT).show();
private void showActionDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.app_name);
dialog.setMessage(mSnippet);
dialog.setPositiveButton(R.string.notealert_ok, this);
if (isScreenOn()) {
dialog.setNegativeButton(R.string.notealert_enter, this);
}
dialog.show().setOnDismissListener(this);
}
/**
*
*/
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);
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_NEGATIVE:
Intent intent = new Intent(this, NoteEditActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_UID, mNoteId);
startActivity(intent);
break;
default:
break;
}
if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) {
// 移除上次同步时间
editor.remove(PREFERENCE_LAST_SYNC_TIME);
}
// 提交更改
editor.commit();
// 清空本地的 GTask 相关信息
new Thread(new Runnable() {
public void run() {
ContentValues values = new ContentValues();
values.put(NoteColumns.GTASK_ID, "");
values.put(NoteColumns.SYNC_ID, 0);
// 更新所有便签的 GTask 相关信息
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
}
}).start();
}
/**
*
* @param context
* @return
*/
public static String getSyncAccountName(Context context) {
// 获取共享偏好设置
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
// 获取同步账户名称
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
public void onDismiss(DialogInterface dialog) {
stopAlarmSound();
finish();
}
/**
*
* @param context
* @param time
*/
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();
private void stopAlarmSound() {
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
/**
*
* @param context
* @return 0
*/
public static long getLastSyncTime(Context context) {
// 获取共享偏好设置
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
}

@ -27,66 +27,39 @@ import android.database.Cursor;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
/**
* AlarmInitReceiver 广广
*
*/
public class AlarmInitReceiver extends BroadcastReceiver {
// 定义查询笔记时需要的列,包括笔记的 ID 和提醒日期
private static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.ALERTED_DATE
};
// 定义查询结果中笔记 ID 所在的列索引
private static final int COLUMN_ID = 0;
// 定义查询结果中提醒日期所在的列索引
private static final int COLUMN_ALERTED_DATE = 1;
/**
* 广
*
*
*
* @param context 访
* @param intent 广
*/
@Override
public void onReceive(Context context, Intent intent) {
// 获取当前时间的毫秒数
long currentDate = System.currentTimeMillis();
// 查询所有设置了提醒且提醒日期在当前时间之后的笔记
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION,
// 查询条件:提醒日期大于当前时间,并且笔记类型为普通笔记
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
new String[] { String.valueOf(currentDate) },
null);
// 如果查询结果不为空
if (c != null) {
// 将游标移动到第一行
if (c.moveToFirst()) {
// 循环遍历查询结果的每一行
do {
// 获取当前笔记的提醒日期
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
// 创建一个新的意图,用于触发 AlarmReceiver 广播接收器
Intent sender = new Intent(context, AlarmReceiver.class);
// 设置意图的数据,包含笔记的 URI 和 ID
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 创建一个 PendingIntent用于在闹钟触发时发送广播
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 获取系统的闹钟服务
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
// 设置一个 RTC_WAKEUP 类型的闹钟,在提醒日期触发时唤醒设备
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext());
}
// 关闭游标,释放资源
c.close();
}
}
}
}

@ -21,100 +21,64 @@ import java.util.Calendar;
import net.micode.notes.R;
import android.content.Context;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.NumberPicker;
/**
* DateTimePicker
* /AM/PM
*/
public class DateTimePicker extends FrameLayout {
// 默认的启用状态
private static final boolean DEFAULT_ENABLE_STATE = true;
// 半天的小时数
private static final int HOURS_IN_HALF_DAY = 12;
// 一整天的小时数
private static final int HOURS_IN_ALL_DAY = 24;
// 一周的天数
private static final int DAYS_IN_ALL_WEEK = 7;
// 日期选择器的最小值
private static final int DATE_SPINNER_MIN_VAL = 0;
// 日期选择器的最大值
private static final int DATE_SPINNER_MAX_VAL = DAYS_IN_ALL_WEEK - 1;
// 24小时制小时选择器的最小值
private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0;
// 24小时制小时选择器的最大值
private static final int HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW = 23;
// 12小时制小时选择器的最小值
private static final int HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW = 1;
// 12小时制小时选择器的最大值
private static final int HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW = 12;
// 分钟选择器的最小值
private static final int MINUT_SPINNER_MIN_VAL = 0;
// 分钟选择器的最大值
private static final int MINUT_SPINNER_MAX_VAL = 59;
// 上午/下午AM/PM选择器的最小值
private static final int AMPM_SPINNER_MIN_VAL = 0;
// 上午/下午AM/PM选择器的最大值
private static final int AMPM_SPINNER_MAX_VAL = 1;
// 日期选择器
private final NumberPicker mDateSpinner;
// 小时选择器
private final NumberPicker mHourSpinner;
// 分钟选择器
private final NumberPicker mMinuteSpinner;
// 上午/下午AM/PM选择器
private final NumberPicker mAmPmSpinner;
// 用于存储当前日期和时间的 Calendar 对象
private Calendar mDate;
// 用于显示日期的字符串数组
private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK];
// 是否为上午的标志
private boolean mIsAm;
// 是否为24小时制的标志
private boolean mIs24HourView;
// 选择器的启用状态
private boolean mIsEnabled = DEFAULT_ENABLE_STATE;
// 初始化标志,用于避免在初始化过程中触发不必要的事件
private boolean mInitialising;
// 日期和时间改变监听器
private OnDateTimeChangedListener mOnDateTimeChangedListener;
// 日期选择器值改变监听器
private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// 根据新值和旧值的差值,更新日期
mDate.add(Calendar.DAY_OF_YEAR, newVal - oldVal);
// 更新日期选择器的显示
updateDateControl();
// 触发日期和时间改变事件
onDateTimeChanged();
}
};
// 小时选择器值改变监听器
private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// 日期是否改变的标志
boolean isDateChanged = false;
// 创建一个新的 Calendar 对象
Calendar cal = Calendar.getInstance();
if (!mIs24HourView) {
// 在12小时制下处理跨天的情况
if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) {
cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, 1);
@ -124,15 +88,12 @@ public class DateTimePicker extends FrameLayout {
cal.add(Calendar.DAY_OF_YEAR, -1);
isDateChanged = true;
}
// 处理上午/下午AM/PM的切换
if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY ||
oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
mIsAm = !mIsAm;
// 更新上午/下午AM/PM选择器的显示
updateAmPmControl();
}
} else {
// 在24小时制下处理跨天的情况
if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) {
cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, 1);
@ -143,14 +104,10 @@ public class DateTimePicker extends FrameLayout {
isDateChanged = true;
}
}
// 计算新的小时数
int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY);
// 设置新的小时数
mDate.set(Calendar.HOUR_OF_DAY, newHour);
// 触发日期和时间改变事件
onDateTimeChanged();
if (isDateChanged) {
// 如果日期改变,更新年、月、日
setCurrentYear(cal.get(Calendar.YEAR));
setCurrentMonth(cal.get(Calendar.MONTH));
setCurrentDay(cal.get(Calendar.DAY_OF_MONTH));
@ -158,269 +115,182 @@ public class DateTimePicker extends FrameLayout {
}
};
// 分钟选择器值改变监听器
private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// 分钟选择器的最小值
int minValue = mMinuteSpinner.getMinValue();
// 分钟选择器的最大值
int maxValue = mMinuteSpinner.getMaxValue();
// 小时偏移量
int offset = 0;
// 处理分钟跨小时的情况
if (oldVal == maxValue && newVal == minValue) {
offset += 1;
} else if (oldVal == minValue && newVal == maxValue) {
offset -= 1;
}
if (offset != 0) {
// 根据偏移量更新小时数
mDate.add(Calendar.HOUR_OF_DAY, offset);
// 更新小时选择器的显示
mHourSpinner.setValue(getCurrentHour());
// 更新日期选择器的显示
updateDateControl();
// 获取当前小时数
int newHour = getCurrentHourOfDay();
if (newHour >= HOURS_IN_HALF_DAY) {
mIsAm = false;
// 更新上午/下午AM/PM选择器的显示
updateAmPmControl();
} else {
mIsAm = true;
// 更新上午/下午AM/PM选择器的显示
updateAmPmControl();
}
}
// 设置新的分钟数
mDate.set(Calendar.MINUTE, newVal);
// 触发日期和时间改变事件
onDateTimeChanged();
}
};
// 上午/下午AM/PM选择器值改变监听器
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// 切换上午/下午AM/PM状态
mIsAm = !mIsAm;
if (mIsAm) {
// 如果切换到上午减去12小时
mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY);
} else {
// 如果切换到下午加上12小时
mDate.add(Calendar.HOUR_OF_DAY, HOURS_IN_HALF_DAY);
}
// 更新上午/下午AM/PM选择器的显示
updateAmPmControl();
// 触发日期和时间改变事件
onDateTimeChanged();
}
};
/**
*
*/
public interface OnDateTimeChangedListener {
/**
*
*
* @param view DateTimePicker
* @param year
* @param month
* @param dayOfMonth
* @param hourOfDay 24
* @param minute
*/
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) {
this(context, System.currentTimeMillis());
}
/**
* 使
*
* @param context
* @param date
*/
public DateTimePicker(Context context, long date) {
this(context, date, DateFormat.is24HourFormat(context));
}
/**
* 使24
*
* @param context
* @param date
* @param is24HourView 24
*/
public DateTimePicker(Context context, long date, boolean is24HourView) {
super(context);
// 初始化 Calendar 对象
mDate = Calendar.getInstance();
// 设置初始化标志
mInitialising = true;
// 判断当前是否为上午
mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY;
// 加载布局文件
inflate(context, R.layout.datetime_picker, this);
// 初始化日期选择器
mDateSpinner = (NumberPicker) findViewById(R.id.date);
mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL);
mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL);
mDateSpinner.setOnValueChangedListener(mOnDateChangedListener);
// 初始化小时选择器
mHourSpinner = (NumberPicker) findViewById(R.id.hour);
mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
// 初始化分钟选择器
mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL);
mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL);
mMinuteSpinner.setOnLongPressUpdateInterval(100);
mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
// 获取上午/下午AM/PM的字符串数组
String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings();
// 初始化上午/下午AM/PM选择器
mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm);
mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL);
mAmPmSpinner.setMaxValue(AMPM_SPINNER_MAX_VAL);
mAmPmSpinner.setDisplayedValues(stringsForAmPm);
mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener);
// 更新日期选择器的显示
// update controls to initial state
updateDateControl();
// 更新小时选择器的显示
updateHourControl();
// 更新上午/下午AM/PM选择器的显示
updateAmPmControl();
// 设置是否为24小时制
set24HourView(is24HourView);
// 设置当前日期和时间
// set to current time
setCurrentDate(date);
// 设置选择器的启用状态
setEnabled(isEnabled());
// 设置内容描述
// set the content descriptions
mInitialising = false;
}
/**
*
*
* @param enabled
*/
@Override
public void setEnabled(boolean enabled) {
if (mIsEnabled == enabled) {
return;
}
super.setEnabled(enabled);
// 设置日期选择器的启用状态
mDateSpinner.setEnabled(enabled);
// 设置分钟选择器的启用状态
mMinuteSpinner.setEnabled(enabled);
// 设置小时选择器的启用状态
mHourSpinner.setEnabled(enabled);
// 设置上午/下午AM/PM选择器的启用状态
mAmPmSpinner.setEnabled(enabled);
// 更新启用状态标志
mIsEnabled = enabled;
}
/**
*
*
* @return
*/
@Override
public boolean isEnabled() {
return mIsEnabled;
}
/**
*
* Get the current date in millis
*
* @return
* @return the current date in millis
*/
public long getCurrentDateInTimeMillis() {
return mDate.getTimeInMillis();
}
/**
*
* Set the current date
*
* @param date
* @param date The current date in millis
*/
public void setCurrentDate(long date) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(date);
// 设置年、月、日、小时、分钟
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));
}
/**
*
* Set the current date
*
* @param year
* @param month
* @param dayOfMonth
* @param hourOfDay 24
* @param minute
* @param year The current year
* @param month The current month
* @param dayOfMonth The current dayOfMonth
* @param hourOfDay The current hourOfDay
* @param minute The current minute
*/
public void setCurrentDate(int year, int month,
int dayOfMonth, int hourOfDay, int minute) {
// 设置年份
int dayOfMonth, int hourOfDay, int minute) {
setCurrentYear(year);
// 设置月份
setCurrentMonth(month);
// 设置日期
setCurrentDay(dayOfMonth);
// 设置小时
setCurrentHour(hourOfDay);
// 设置分钟
setCurrentMinute(minute);
}
/**
*
* Get current year
*
* @return
* @return The current year
*/
public int getCurrentYear() {
return mDate.get(Calendar.YEAR);
}
/**
*
* Set current year
*
* @param year
* @param year The current year
*/
public void setCurrentYear(int year) {
if (!mInitialising && year == getCurrentYear()) {
return;
}
// 设置年份
mDate.set(Calendar.YEAR, year);
// 更新日期选择器的显示
updateDateControl();
// 触发日期和时间改变事件
onDateTimeChanged();
}

@ -29,126 +29,59 @@ import android.content.DialogInterface.OnClickListener;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
/**
* DateTimePickerDialog AlertDialog
*
*/
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
// 用于存储用户选择的日期和时间的 Calendar 对象
private Calendar mDate = Calendar.getInstance();
// 标记是否使用 24 小时制显示时间
private boolean mIs24HourView;
// 用于处理日期和时间设置完成后的回调接口
private OnDateTimeSetListener mOnDateTimeSetListener;
// 自定义的日期和时间选择器
private DateTimePicker mDateTimePicker;
/**
*
*/
public interface OnDateTimeSetListener {
/**
*
* @param dialog
* @param date
*/
void OnDateTimeSet(AlertDialog dialog, long date);
}
/**
* DateTimePickerDialog
* @param context
* @param date
*/
public DateTimePickerDialog(Context context, long date) {
super(context);
// 创建一个 DateTimePicker 实例
mDateTimePicker = new DateTimePicker(context);
// 将 DateTimePicker 设置为对话框的视图
setView(mDateTimePicker);
// 为 DateTimePicker 设置日期和时间改变监听器
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
/**
*
* @param view
* @param year
* @param month
* @param dayOfMonth
* @param hourOfDay
* @param minute
*/
public void onDateTimeChanged(DateTimePicker view, int year, int month,
int dayOfMonth, int hourOfDay, int minute) {
// 更新 Calendar 对象中的日期和时间
mDate.set(Calendar.YEAR, year);
mDate.set(Calendar.MONTH, month);
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
mDate.set(Calendar.MINUTE, minute);
// 更新对话框的标题,显示当前选择的日期和时间
updateTitle(mDate.getTimeInMillis());
}
});
// 设置 Calendar 对象的时间为传入的初始时间
mDate.setTimeInMillis(date);
// 将秒数设置为 0
mDate.set(Calendar.SECOND, 0);
// 设置 DateTimePicker 的当前日期和时间
mDateTimePicker.setCurrentDate(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());
}
/**
* 使 24
* @param is24HourView 使 24
*/
public void set24HourView(boolean is24HourView) {
mIs24HourView = is24HourView;
}
/**
*
* @param callBack OnDateTimeSetListener
*/
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
mOnDateTimeSetListener = callBack;
}
/**
*
* @param date
*/
private void updateTitle(long date) {
// 设置日期和时间格式化的标志
int flag =
DateUtils.FORMAT_SHOW_YEAR |
DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_TIME;
// 根据是否使用 24 小时制添加相应的标志
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;
// 使用 DateUtils 格式化日期和时间,并设置为对话框的标题
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
}
/**
*
* @param arg0
* @param arg1
*/
public void onClick(DialogInterface arg0, int arg1) {
// 如果设置了回调监听器,则调用其 OnDateTimeSet 方法
if (mOnDateTimeSetListener != null) {
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
}

@ -0,0 +1,72 @@
package net.micode.notes.ui;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import net.micode.notes.R;
import net.micode.notes.ui.bean.Note;
import net.micode.notes.util.ToastUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class EditActivity extends Activity {
private Note note;
private EditText etContent;
private NoteDbOpenHelper mNoteDbOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
etContent = findViewById(R.id.sf_et_2);
initData();
}
private void initData() {
Intent intent = getIntent();
note = (Note) intent.getSerializableExtra("note");
if(note!=null){
etContent.setText(note.getContent());
}
mNoteDbOpenHelper = new NoteDbOpenHelper(this);
}
public void save(View view) {
String content = etContent.getText().toString();
note.setContent(content);
note.setCreatedTime(getCurrentTimeFormat());
long row = mNoteDbOpenHelper.updateData(note);
if(row != -1 && row != 0){
ToastUtil.toastShort(this,"Edit Success");
this.finish();
}else{
ToastUtil.toastShort(this,"Edit Fail");
}
}
private String getCurrentTimeFormat() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd HH:mm:ss");
TimeZone time = TimeZone.getTimeZone("Etc/GMT-8"); //转换为中国时区
TimeZone.setDefault(time);
Date date = new Date();
return simpleDateFormat.format(date);
}
}

@ -0,0 +1,128 @@
package net.micode.notes.ui;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import net.micode.notes.ui.bean.Note;
import java.util.ArrayList;
import java.util.List;
public class NoteDbOpenHelper extends SQLiteOpenHelper {
private static final String DB_NAME="noteSQLite.db";
private static final String TABLE_NAME_NOTE = "note";
private static final String CREATE_TABLE_SQL = "create table " + TABLE_NAME_NOTE + " (id integer primary key autoincrement, content text, create_time text)";
public NoteDbOpenHelper(Context context){
super(context,DB_NAME,null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long insertData(Note note){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("content",note.getContent());
values.put("create_time",note.getCreatedTime());
return db.insert(TABLE_NAME_NOTE,null,values);
}
public int deleteFromDbById(String id){
SQLiteDatabase db = getWritableDatabase();
return db.delete(TABLE_NAME_NOTE,"id like ?", new String[]{id});
}
public int updateData(Note note) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("content", note.getContent());
values.put("create_time", note.getCreatedTime());
return db.update(TABLE_NAME_NOTE, values, "id like ?", new String[]{note.getId()});
}
public List<Note> queryAllFromDb(){
SQLiteDatabase db = getWritableDatabase();
List<Note> notelist = new ArrayList<>();
Cursor cursor = db.query(TABLE_NAME_NOTE,null,null,null,null,null,null);
if (cursor != null){
while (cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex("id"));
String content = cursor.getString(cursor.getColumnIndex("content"));
String createTime = cursor.getString(cursor.getColumnIndex("create_time"));
Note note = new Note();
note.setId(id);
note.setContent(content);
note.setCreatedTime(createTime);
notelist.add(note);
}
cursor.close();
}
return notelist;
}
public List<Note> queryFromDbByContent(String content){
if (TextUtils.isEmpty(content)){
return queryAllFromDb();
}
SQLiteDatabase db = getWritableDatabase();
List<Note> noteList = new ArrayList<>();
Cursor cursor = db.query(TABLE_NAME_NOTE,null,"content like ?",new String[]{"%"+content+"%"},null,null,null);
if (cursor != null){
while (cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex("id"));
String content1 = cursor.getString(cursor.getColumnIndex("content"));
String createTime = cursor.getString(cursor.getColumnIndex("create_time"));
Note note = new Note();
note.setId(id);
note.setContent(content1);
note.setCreatedTime(createTime);
noteList.add(note);
}
cursor.close();
}
return noteList;
}
}

@ -71,148 +71,103 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* NoteEditActivity
*/
public class NoteEditActivity extends Activity implements OnClickListener,
NoteSettingChangedListener, OnTextViewChangeListener {
/**
* ViewHolder
*/
private class HeadViewHolder {
// 显示笔记修改日期的TextView
public TextView tvModified;
// 提醒图标ImageView
public ImageView ivAlertIcon;
// 显示提醒日期的TextView
public TextView tvAlertDate;
// 设置背景颜色的按钮ImageView
public ImageView ibSetBgColor;
}
// 背景选择按钮和对应背景资源ID的映射
private static final Map<Integer, Integer> sBgSelectorBtnsMap = new HashMap<Integer, Integer>();
static {
// 黄色背景按钮和对应资源ID
sBgSelectorBtnsMap.put(R.id.iv_bg_yellow, ResourceParser.YELLOW);
// 红色背景按钮和对应资源ID
sBgSelectorBtnsMap.put(R.id.iv_bg_red, ResourceParser.RED);
// 蓝色背景按钮和对应资源ID
sBgSelectorBtnsMap.put(R.id.iv_bg_blue, ResourceParser.BLUE);
// 绿色背景按钮和对应资源ID
sBgSelectorBtnsMap.put(R.id.iv_bg_green, ResourceParser.GREEN);
// 白色背景按钮和对应资源ID
sBgSelectorBtnsMap.put(R.id.iv_bg_white, ResourceParser.WHITE);
}
// 背景选择和对应选中状态视图ID的映射
private static final Map<Integer, Integer> sBgSelectorSelectionMap = new HashMap<Integer, Integer>();
static {
// 黄色背景选中状态视图ID
sBgSelectorSelectionMap.put(ResourceParser.YELLOW, R.id.iv_bg_yellow_select);
// 红色背景选中状态视图ID
sBgSelectorSelectionMap.put(ResourceParser.RED, R.id.iv_bg_red_select);
// 蓝色背景选中状态视图ID
sBgSelectorSelectionMap.put(ResourceParser.BLUE, R.id.iv_bg_blue_select);
// 绿色背景选中状态视图ID
sBgSelectorSelectionMap.put(ResourceParser.GREEN, R.id.iv_bg_green_select);
// 白色背景选中状态视图ID
sBgSelectorSelectionMap.put(ResourceParser.WHITE, R.id.iv_bg_white_select);
}
// 字体大小按钮和对应字体大小资源ID的映射
private static final Map<Integer, Integer> sFontSizeBtnsMap = new HashMap<Integer, Integer>();
static {
// 大字体按钮和对应资源ID
sFontSizeBtnsMap.put(R.id.ll_font_large, ResourceParser.TEXT_LARGE);
// 小字体按钮和对应资源ID
sFontSizeBtnsMap.put(R.id.ll_font_small, ResourceParser.TEXT_SMALL);
// 正常字体按钮和对应资源ID
sFontSizeBtnsMap.put(R.id.ll_font_normal, ResourceParser.TEXT_MEDIUM);
// 超大字体按钮和对应资源ID
sFontSizeBtnsMap.put(R.id.ll_font_super, ResourceParser.TEXT_SUPER);
}
// 字体选择和对应选中状态视图ID的映射
private static final Map<Integer, Integer> sFontSelectorSelectionMap = new HashMap<Integer, Integer>();
static {
// 大字体选中状态视图ID
sFontSelectorSelectionMap.put(ResourceParser.TEXT_LARGE, R.id.iv_large_select);
// 小字体选中状态视图ID
sFontSelectorSelectionMap.put(ResourceParser.TEXT_SMALL, R.id.iv_small_select);
// 正常字体选中状态视图ID
sFontSelectorSelectionMap.put(ResourceParser.TEXT_MEDIUM, R.id.iv_medium_select);
// 超大字体选中状态视图ID
sFontSelectorSelectionMap.put(ResourceParser.TEXT_SUPER, R.id.iv_super_select);
}
// 日志标签
private static final String TAG = "NoteEditActivity";
// 头部视图的ViewHolder实例
private HeadViewHolder mNoteHeaderHolder;
// 头部视图面板
private View mHeadViewPanel;
// 背景颜色选择器视图
private View mNoteBgColorSelector;
// 字体大小选择器视图
private View mFontSizeSelector;
// 笔记编辑器EditText
private EditText mNoteEditor;
// 笔记编辑器面板
private View mNoteEditorPanel;
// 工作笔记实例
private WorkingNote mWorkingNote;
// 共享偏好设置
private SharedPreferences mSharedPrefs;
// 字体大小ID
private int mFontSizeId;
// 共享偏好设置中字体大小的键
private static final String PREFERENCE_FONT_SIZE = "pref_font_size";
// 快捷图标标题的最大长度
private static final int SHORTCUT_ICON_TITLE_MAX_LEN = 10;
// 已选中标记
public static final String TAG_CHECKED = String.valueOf('\u221A');
// 未选中标记
public static final String TAG_UNCHECKED = String.valueOf('\u25A1');
// 编辑文本列表
private LinearLayout mEditTextList;
// 用户查询关键词
private String mUserQuery;
// 正则表达式模式
private Pattern mPattern;
/**
*
*
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置布局
this.setContentView(R.layout.note_edit);
// 如果是首次创建且初始化活动状态失败,则关闭活动
if (savedInstanceState == null && !initActivityState(getIntent())) {
finish();
return;
}
// 初始化资源
initResources();
}
/**
*
*
* @param savedInstanceState
* Current activity may be killed when the memory is low. Once it is killed, for another time
* user load this activity, we should restore the former state
*/
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// 如果保存的实例状态不为空且包含笔记ID则恢复活动状态
if (savedInstanceState != null && savedInstanceState.containsKey(Intent.EXTRA_UID)) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_UID, savedInstanceState.getLong(Intent.EXTRA_UID));
@ -224,38 +179,31 @@ public class NoteEditActivity extends Activity implements OnClickListener,
}
}
/**
*
*
* @param intent
* @return truefalse
*/
private boolean initActivityState(Intent intent) {
// 清空工作笔记
/**
* If the user specified the {@link Intent#ACTION_VIEW} but not provided with id,
* then jump to the NotesListActivity
*/
mWorkingNote = null;
// 如果是查看笔记的意图
if (TextUtils.equals(Intent.ACTION_VIEW, intent.getAction())) {
// 获取笔记ID
long noteId = intent.getLongExtra(Intent.EXTRA_UID, 0);
// 初始化用户查询为空
mUserQuery = "";
// 如果是从搜索结果启动的
/**
* Starting from the searched result
*/
if (intent.hasExtra(SearchManager.EXTRA_DATA_KEY)) {
noteId = Long.parseLong(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
mUserQuery = intent.getStringExtra(SearchManager.USER_QUERY);
}
// 检查笔记是否在数据库中可见
if (!DataUtils.visibleInNoteDatabase(getContentResolver(), noteId, Notes.TYPE_NOTE)) {
// 不可见则跳转到笔记列表活动并提示错误
Intent jump = new Intent(this, NotesListActivity.class);
startActivity(jump);
showToast(R.string.error_note_not_exist);
finish();
return false;
} else {
// 加载笔记
mWorkingNote = WorkingNote.load(this, noteId);
if (mWorkingNote == null) {
Log.e(TAG, "load note failed with note id" + noteId);
@ -263,25 +211,20 @@ public class NoteEditActivity extends Activity implements OnClickListener,
return false;
}
}
// 设置软键盘模式
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
} else if(TextUtils.equals(Intent.ACTION_INSERT_OR_EDIT, intent.getAction())) {
// 插入或编辑笔记的意图
// 获取文件夹ID
// New note
long folderId = intent.getLongExtra(Notes.INTENT_EXTRA_FOLDER_ID, 0);
// 获取小部件ID
int widgetId = intent.getIntExtra(Notes.INTENT_EXTRA_WIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
// 获取小部件类型
int widgetType = intent.getIntExtra(Notes.INTENT_EXTRA_WIDGET_TYPE,
Notes.TYPE_WIDGET_INVALIDE);
// 获取背景资源ID
int bgResId = intent.getIntExtra(Notes.INTENT_EXTRA_BACKGROUND_ID,
ResourceParser.getDefaultBgId(this));
// 解析通话记录笔记
// Parse call-record note
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
long callDate = intent.getLongExtra(Notes.INTENT_EXTRA_CALL_DATE, 0);
if (callDate != 0 && phoneNumber != null) {
@ -289,7 +232,6 @@ public class NoteEditActivity extends Activity implements OnClickListener,
Log.w(TAG, "The call record number is null");
}
long noteId = 0;
// 检查是否已有该通话记录的笔记
if ((noteId = DataUtils.getNoteIdByPhoneNumberAndCallDate(getContentResolver(),
phoneNumber, callDate)) > 0) {
mWorkingNote = WorkingNote.load(this, noteId);
@ -299,18 +241,15 @@ public class NoteEditActivity extends Activity implements OnClickListener,
return false;
}
} else {
// 创建新的通话记录笔记
mWorkingNote = WorkingNote.createEmptyNote(this, folderId, widgetId,
widgetType, bgResId);
mWorkingNote.convertToCallNote(phoneNumber, callDate);
}
} else {
// 创建新的普通笔记
mWorkingNote = WorkingNote.createEmptyNote(this, folderId, widgetId, widgetType,
bgResId);
}
// 设置软键盘模式
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
| WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
@ -319,47 +258,31 @@ public class NoteEditActivity extends Activity implements OnClickListener,
finish();
return false;
}
// 设置笔记设置状态改变监听器
mWorkingNote.setOnSettingStatusChangedListener(this);
return true;
}
/**
*
*/
@Override
protected void onResume() {
super.onResume();
initNoteScreen();
}
/**
*
*/
private void initNoteScreen() {
// 设置笔记编辑器的文本样式
mNoteEditor.setTextAppearance(this, TextAppearanceResources
.getTexAppearanceResource(mFontSizeId));
// 如果是复选列表模式
if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) {
// 切换到列表模式
switchToListMode(mWorkingNote.getContent());
} else {
// 设置笔记编辑器的文本并高亮显示查询结果
mNoteEditor.setText(getHighlightQueryResult(mWorkingNote.getContent(), mUserQuery));
// 将光标移动到文本末尾
mNoteEditor.setSelection(mNoteEditor.getText().length());
}
// 隐藏所有背景选择的选中状态视图
for (Integer id : sBgSelectorSelectionMap.keySet()) {
findViewById(sBgSelectorSelectionMap.get(id)).setVisibility(View.GONE);
}
// 设置头部视图面板的背景资源
mHeadViewPanel.setBackgroundResource(mWorkingNote.getTitleBgResId());
// 设置笔记编辑器面板的背景资源
mNoteEditorPanel.setBackgroundResource(mWorkingNote.getBgColorResId());
// 设置笔记修改日期的显示
mNoteHeaderHolder.tvModified.setText(DateUtils.formatDateTime(this,
mWorkingNote.getModifiedDate(), DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_SHOW_TIME
@ -369,55 +292,35 @@ public class NoteEditActivity extends Activity implements OnClickListener,
* TODO: Add the menu for setting alert. Currently disable it because the DateTimePicker
* is not ready
*/
// 显示提醒头部信息
showAlertHeader();
}
/**
*
*/
private void showAlertHeader() {
// 如果笔记有提醒设置
if (mWorkingNote.hasClockAlert()) {
long time = System.currentTimeMillis();
// 如果提醒时间已过
if (time > mWorkingNote.getAlertDate()) {
mNoteHeaderHolder.tvAlertDate.setText(R.string.note_alert_expired);
} else {
// 显示相对提醒时间
mNoteHeaderHolder.tvAlertDate.setText(DateUtils.getRelativeTimeSpanString(
mWorkingNote.getAlertDate(), time, DateUtils.MINUTE_IN_MILLIS));
}
// 显示提醒日期和图标
mNoteHeaderHolder.tvAlertDate.setVisibility(View.VISIBLE);
mNoteHeaderHolder.ivAlertIcon.setVisibility(View.VISIBLE);
} else {
// 隐藏提醒日期和图标
mNoteHeaderHolder.tvAlertDate.setVisibility(View.GONE);
mNoteHeaderHolder.ivAlertIcon.setVisibility(View.GONE);
};
}
/**
*
*
* @param intent
*/
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
initActivityState(intent);
}
/**
* ID
*
* @param outState
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// 如果笔记还未保存到数据库中,则先
/**
* For new note without note id, we should firstly save it to
* generate a id. If the editing note is not worth saving, there

@ -37,134 +37,83 @@ import net.micode.notes.R;
import java.util.HashMap;
import java.util.Map;
/**
* NoteEditText EditText
*
* 便
*/
public class NoteEditText extends EditText {
// 日志标签,用于调试输出
public class NoteEditText extends androidx.appcompat.widget.AppCompatEditText {
private static final String TAG = "NoteEditText";
// 当前编辑文本框的索引,用于标识其在一组编辑文本框中的位置
private int mIndex;
// 删除操作前的选择起始位置
private int mSelectionStartBeforeDelete;
// 定义支持的 URL 协议
private static final String SCHEME_TEL = "tel:" ;
private static final String SCHEME_HTTP = "http:" ;
private static final String SCHEME_EMAIL = "mailto:" ;
// 存储协议与对应菜单项资源 ID 的映射
private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>();
static {
// 初始化映射,将不同协议与对应的菜单项资源 ID 关联
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);
}
/**
* OnTextViewChangeListener
* /
* Call by the {@link NoteEditActivity} to delete or add edit text
*/
public interface OnTextViewChangeListener {
/**
* {@link KeyEvent#KEYCODE_DEL}
* @param index
* @param text
* Delete current edit text when {@link KeyEvent#KEYCODE_DEL} happens
* and the text is null
*/
void onEditTextDelete(int index, String text);
/**
* {@link KeyEvent#KEYCODE_ENTER}
* @param index
* @param text
* Add edit text after current edit text when {@link KeyEvent#KEYCODE_ENTER}
* happen
*/
void onEditTextEnter(int index, String text);
/**
*
* @param index
* @param hasText
* Hide or show item option when text change
*/
void onTextChange(int index, boolean hasText);
}
// 外部活动实现的 OnTextViewChangeListener 接口实例
private OnTextViewChangeListener mOnTextViewChangeListener;
/**
* 使 NoteEditText
* @param context
*/
public NoteEditText(Context context) {
super(context, null);
// 初始化索引为 0
mIndex = 0;
}
/**
*
* @param index
*/
public void setIndex(int index) {
mIndex = index;
}
/**
* OnTextViewChangeListener
* @param listener OnTextViewChangeListener
*/
public void setOnTextViewChangeListener(OnTextViewChangeListener listener) {
mOnTextViewChangeListener = listener;
}
/**
* 使 NoteEditText
* @param context
* @param attrs
*/
public NoteEditText(Context context, AttributeSet attrs) {
super(context, attrs, android.R.attr.editTextStyle);
}
/**
* 使 NoteEditText
* @param context
* @param attrs
* @param defStyle
*/
public NoteEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
/**
*
* @param event
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 获取触摸点的坐标
int x = (int) event.getX();
int y = (int) event.getY();
// 调整坐标,去除内边距并考虑滚动
x -= getTotalPaddingLeft();
y -= getTotalPaddingTop();
x += getScrollX();
y += getScrollY();
// 获取文本布局
Layout layout = getLayout();
// 根据垂直坐标获取所在行
int line = layout.getLineForVertical(y);
// 根据行和水平坐标获取字符偏移量
int off = layout.getOffsetForHorizontal(line, x);
// 设置文本选择位置
Selection.setSelection(getText(), off);
break;
}
@ -172,23 +121,15 @@ public class NoteEditText extends EditText {
return super.onTouchEvent(event);
}
/**
*
* @param keyCode
* @param event
* @return
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
// 如果设置了 OnTextViewChangeListener 接口实例,返回 false
if (mOnTextViewChangeListener != null) {
return false;
}
break;
case KeyEvent.KEYCODE_DEL:
// 记录删除操作前的选择起始位置
mSelectionStartBeforeDelete = getSelectionStart();
break;
default:
@ -197,41 +138,26 @@ public class NoteEditText extends EditText {
return super.onKeyDown(keyCode, event);
}
/**
*
* @param keyCode
* @param event
* @return
*/
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_DEL:
// 如果设置了 OnTextViewChangeListener 接口实例
if (mOnTextViewChangeListener != null) {
// 当选择起始位置为 0 且索引不为 0 时,调用删除回调方法
if (0 == mSelectionStartBeforeDelete && mIndex != 0) {
mOnTextViewChangeListener.onEditTextDelete(mIndex, getText().toString());
return true;
}
} else {
// 未设置 OnTextViewChangeListener 接口实例,输出日志
Log.d(TAG, "OnTextViewChangeListener was not seted");
}
break;
case KeyEvent.KEYCODE_ENTER:
// 如果设置了 OnTextViewChangeListener 接口实例
if (mOnTextViewChangeListener != null) {
// 获取当前选择起始位置
int selectionStart = getSelectionStart();
// 获取从当前光标位置到文本末尾的文本
String text = getText().subSequence(selectionStart, length()).toString();
// 更新文本,保留当前光标之前的部分
setText(getText().subSequence(0, selectionStart));
// 调用添加编辑文本框的回调方法
mOnTextViewChangeListener.onEditTextEnter(mIndex + 1, text);
} else {
// 未设置 OnTextViewChangeListener 接口实例,输出日志
Log.d(TAG, "OnTextViewChangeListener was not seted");
}
break;
@ -241,68 +167,45 @@ public class NoteEditText extends EditText {
return super.onKeyUp(keyCode, event);
}
/**
*
* @param focused
* @param direction
* @param previouslyFocusedRect
*/
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
// 如果设置了 OnTextViewChangeListener 接口实例
if (mOnTextViewChangeListener != null) {
// 当失去焦点且文本为空时,调用文本变化回调方法,传递 false
if (!focused && TextUtils.isEmpty(getText())) {
mOnTextViewChangeListener.onTextChange(mIndex, false);
} else {
// 否则,调用文本变化回调方法,传递 true
mOnTextViewChangeListener.onTextChange(mIndex, true);
}
}
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
/**
* URL
* @param menu
*/
@Override
protected void onCreateContextMenu(ContextMenu menu) {
// 检查文本是否为 Spanned 类型
if (getText() instanceof Spanned) {
// 获取选择的起始和结束位置
int selStart = getSelectionStart();
int selEnd = getSelectionEnd();
// 确定选择的最小和最大位置
int min = Math.min(selStart, selEnd);
int max = Math.max(selStart, selEnd);
// 获取选择范围内的 URLSpan 数组
final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class);
// 如果只选择了一个 URL
if (urls.length == 1) {
int defaultResId = 0;
// 遍历支持的协议
for(String schema: sSchemaActionResMap.keySet()) {
// 检查 URL 是否包含当前协议
if(urls[0].getURL().indexOf(schema) >= 0) {
// 获取对应的菜单项资源 ID
defaultResId = sSchemaActionResMap.get(schema);
break;
}
}
// 如果未找到匹配的协议,使用默认的菜单项资源 ID
if (defaultResId == 0) {
defaultResId = R.string.note_link_other;
}
// 添加菜单项,并设置点击监听器
menu.add(0, 0, 0, defaultResId).setOnMenuItemClickListener(
new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// 调用 URLSpan 的点击方法,打开链接
// goto a new intent
urls[0].onClick(NoteEditText.this);
return true;
}
@ -311,4 +214,4 @@ public class NoteEditText extends EditText {
}
super.onCreateContextMenu(menu);
}
}
}

@ -25,28 +25,23 @@ import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.tool.DataUtils;
/**
* NoteItemData
* Cursor 访 getter
*/
public class NoteItemData {
// 定义从数据库查询笔记项数据时使用的投影列
static final String [] PROJECTION = new String [] {
NoteColumns.ID, // 笔记的 ID
NoteColumns.ALERTED_DATE, // 提醒日期
NoteColumns.BG_COLOR_ID, // 背景颜色 ID
NoteColumns.CREATED_DATE, // 创建日期
NoteColumns.HAS_ATTACHMENT, // 是否有附件
NoteColumns.MODIFIED_DATE, // 修改日期
NoteColumns.NOTES_COUNT, // 笔记数量
NoteColumns.PARENT_ID, // 父文件夹 ID
NoteColumns.SNIPPET, // 笔记摘要
NoteColumns.TYPE, // 笔记类型
NoteColumns.WIDGET_ID, // 小部件 ID
NoteColumns.WIDGET_TYPE, // 小部件类型
NoteColumns.ID,
NoteColumns.ALERTED_DATE,
NoteColumns.BG_COLOR_ID,
NoteColumns.CREATED_DATE,
NoteColumns.HAS_ATTACHMENT,
NoteColumns.MODIFIED_DATE,
NoteColumns.NOTES_COUNT,
NoteColumns.PARENT_ID,
NoteColumns.SNIPPET,
NoteColumns.TYPE,
NoteColumns.WIDGET_ID,
NoteColumns.WIDGET_TYPE,
};
// 定义投影列在 Cursor 中的索引
private static final int ID_COLUMN = 0;
private static final int ALERTED_DATE_COLUMN = 1;
private static final int BG_COLOR_ID_COLUMN = 2;
@ -60,37 +55,28 @@ public class NoteItemData {
private static final int WIDGET_ID_COLUMN = 10;
private static final int WIDGET_TYPE_COLUMN = 11;
// 笔记项的各种属性
private long mId; // 笔记的 ID
private long mAlertDate; // 提醒日期
private int mBgColorId; // 背景颜色 ID
private long mCreatedDate; // 创建日期
private boolean mHasAttachment; // 是否有附件
private long mModifiedDate; // 修改日期
private int mNotesCount; // 笔记数量
private long mParentId; // 父文件夹 ID
private String mSnippet; // 笔记摘要
private int mType; // 笔记类型
private int mWidgetId; // 小部件 ID
private int mWidgetType; // 小部件类型
private String mName; // 联系人姓名
private String mPhoneNumber; // 联系人电话号码
// 笔记项在列表中的位置状态
private boolean mIsLastItem; // 是否是列表中的最后一项
private boolean mIsFirstItem; // 是否是列表中的第一项
private boolean mIsOnlyOneItem; // 是否是列表中唯一的一项
private boolean mIsOneNoteFollowingFolder; // 是否是文件夹后面的唯一一条笔记
private boolean mIsMultiNotesFollowingFolder; // 是否是文件夹后面有多条笔记
/**
* Cursor
*
* @param context
* @param cursor Cursor
*/
private long mId;
private long mAlertDate;
private int mBgColorId;
private long mCreatedDate;
private boolean mHasAttachment;
private long mModifiedDate;
private int mNotesCount;
private long mParentId;
private String mSnippet;
private int mType;
private int mWidgetId;
private int mWidgetType;
private String mName;
private String mPhoneNumber;
private boolean mIsLastItem;
private boolean mIsFirstItem;
private boolean mIsOnlyOneItem;
private boolean mIsOneNoteFollowingFolder;
private boolean mIsMultiNotesFollowingFolder;
public NoteItemData(Context context, Cursor cursor) {
// 从 Cursor 中提取数据并赋值给相应的成员变量
mId = cursor.getLong(ID_COLUMN);
mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN);
mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN);
@ -100,7 +86,6 @@ public class NoteItemData {
mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN);
mParentId = cursor.getLong(PARENT_ID_COLUMN);
mSnippet = cursor.getString(SNIPPET_COLUMN);
// 移除笔记摘要中的特定标签
mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace(
NoteEditActivity.TAG_UNCHECKED, "");
mType = cursor.getInt(TYPE_COLUMN);
@ -108,15 +93,11 @@ public class NoteItemData {
mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN);
mPhoneNumber = "";
// 如果父文件夹 ID 是通话记录文件夹的 ID
if (mParentId == Notes.ID_CALL_RECORD_FOLDER) {
// 获取通话记录的电话号码
mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId);
if (!TextUtils.isEmpty(mPhoneNumber)) {
// 根据电话号码获取联系人姓名
mName = Contact.getContact(context, mPhoneNumber);
if (mName == null) {
// 如果没有联系人姓名,则使用电话号码作为姓名
mName = mPhoneNumber;
}
}
@ -125,37 +106,24 @@ public class NoteItemData {
if (mName == null) {
mName = "";
}
// 检查笔记项在列表中的位置状态
checkPostion(cursor);
}
/**
*
*
* @param cursor Cursor
*/
private void checkPostion(Cursor cursor) {
// 判断是否是列表中的最后一项
mIsLastItem = cursor.isLast() ? true : false;
// 判断是否是列表中的第一项
mIsFirstItem = cursor.isFirst() ? true : false;
// 判断是否是列表中唯一的一项
mIsOnlyOneItem = (cursor.getCount() == 1);
mIsMultiNotesFollowingFolder = false;
mIsOneNoteFollowingFolder = false;
// 如果是笔记类型且不是第一项
if (mType == Notes.TYPE_NOTE && !mIsFirstItem) {
int position = cursor.getPosition();
if (cursor.moveToPrevious()) {
// 如果前一项是文件夹或系统类型
if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER
|| cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) {
if (cursor.getCount() > (position + 1)) {
// 如果后面还有多项笔记,则标记为文件夹后面有多条笔记
mIsMultiNotesFollowingFolder = true;
} else {
// 如果后面只有一项笔记,则标记为文件夹后面的唯一一条笔记
mIsOneNoteFollowingFolder = true;
}
}
@ -166,202 +134,91 @@ public class NoteItemData {
}
}
/**
*
*
* @return true false
*/
public boolean isOneFollowingFolder() {
return mIsOneNoteFollowingFolder;
}
/**
*
*
* @return true false
*/
public boolean isMultiFollowingFolder() {
return mIsMultiNotesFollowingFolder;
}
/**
*
*
* @return true false
*/
public boolean isLast() {
return mIsLastItem;
}
/**
*
*
* @return
*/
public String getCallName() {
return mName;
}
/**
*
*
* @return true false
*/
public boolean isFirst() {
return mIsFirstItem;
}
/**
*
*
* @return true false
*/
public boolean isSingle() {
return mIsOnlyOneItem;
}
/**
* ID
*
* @return ID
*/
public long getId() {
return mId;
}
/**
*
*
* @return
*/
public long getAlertDate() {
return mAlertDate;
}
/**
*
*
* @return
*/
public long getCreatedDate() {
return mCreatedDate;
}
/**
*
*
* @return true false
*/
public boolean hasAttachment() {
return mHasAttachment;
}
/**
*
*
* @return
*/
public long getModifiedDate() {
return mModifiedDate;
}
/**
* ID
*
* @return ID
*/
public int getBgColorId() {
return mBgColorId;
}
/**
* ID
*
* @return ID
*/
public long getParentId() {
return mParentId;
}
/**
*
*
* @return
*/
public int getNotesCount() {
return mNotesCount;
}
/**
* ID
*
* @return ID
*/
public long getFolderId () {
return mParentId;
}
/**
*
*
* @return
*/
public int getType() {
return mType;
}
/**
*
*
* @return
*/
public int getWidgetType() {
return mWidgetType;
}
/**
* ID
*
* @return ID
*/
public int getWidgetId() {
return mWidgetId;
}
/**
*
*
* @return
*/
public String getSnippet() {
return mSnippet;
}
/**
*
*
* @return true false
*/
public boolean hasAlert() {
return (mAlertDate > 0);
}
/**
*
*
* @return true false
*/
public boolean isCallRecord() {
return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));
}
/**
* Cursor
*
* @param cursor Cursor
* @return
*/
public static int getNoteType(Cursor cursor) {
return cursor.getInt(TYPE_COLUMN);
}
}
}

@ -135,12 +135,26 @@ public class NotesListActivity extends Activity implements OnClickListener, OnIt
private final static int REQUEST_CODE_OPEN_NODE = 102;
private final static int REQUEST_CODE_NEW_NODE = 103;
public Button mBtnsf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note_list);
initResources();
mBtnsf = findViewById(R.id.btn_sf);
mBtnsf.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=null;
intent = new Intent(NotesListActivity.this,pwdActivity.class);
startActivity(intent);
}
});
/**
* Insert an introduction when user firstly use this application
*/

@ -30,35 +30,19 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
/**
* 便
* CursorAdapter便
*/
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 static class AppWidgetAttribute {
// 小部件的 ID
public int widgetId;
// 小部件的类型
public int widgetType;
};
/**
*
* @param context
*/
public NotesListAdapter(Context context) {
super(context, null);
mSelectedIndex = new HashMap<Integer, Boolean>();
@ -66,73 +50,38 @@ public class NotesListAdapter extends CursorAdapter {
mNotesCount = 0;
}
/**
* 便
* @param context
* @param cursor 便
* @param parent
* @return 便
*/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new NotesListItem(context);
}
/**
* 便
* @param view 便
* @param context
* @param cursor 便
*/
@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,
isSelectedItem(cursor.getPosition()));
}
}
/**
* 便
* @param position 便
* @param checked
*/
public void setCheckedItem(final int position, final boolean checked) {
mSelectedIndex.put(position, checked);
// 通知适配器数据已更改,刷新视图
notifyDataSetChanged();
}
/**
*
* @return true false
*/
public boolean isInChoiceMode() {
return mChoiceMode;
}
/**
*
* @param mode
*/
public void setChoiceMode(boolean mode) {
// 清除所有选择状态
mSelectedIndex.clear();
mChoiceMode = mode;
}
/**
* 便
* @param 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);
}
@ -140,10 +89,6 @@ public class NotesListAdapter extends CursorAdapter {
}
}
/**
* 便 ID
* @return 便 ID
*/
public HashSet<Long> getSelectedItemIds() {
HashSet<Long> itemSet = new HashSet<Long>();
for (Integer position : mSelectedIndex.keySet()) {
@ -160,10 +105,6 @@ public class NotesListAdapter extends CursorAdapter {
return itemSet;
}
/**
* 便
* @return 便
*/
public HashSet<AppWidgetAttribute> getSelectedWidget() {
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
for (Integer position : mSelectedIndex.keySet()) {
@ -187,10 +128,6 @@ public class NotesListAdapter extends CursorAdapter {
return itemSet;
}
/**
* 便
* @return 便
*/
public int getSelectedCount() {
Collection<Boolean> values = mSelectedIndex.values();
if (null == values) {
@ -206,20 +143,11 @@ public class NotesListAdapter extends CursorAdapter {
return count;
}
/**
* 便
* @return 便 true false
*/
public boolean isAllSelected() {
int checkedCount = getSelectedCount();
return (checkedCount != 0 && checkedCount == mNotesCount);
}
/**
* 便
* @param position 便
* @return true false
*/
public boolean isSelectedItem(final int position) {
if (null == mSelectedIndex.get(position)) {
return false;
@ -227,36 +155,23 @@ public class NotesListAdapter extends CursorAdapter {
return mSelectedIndex.get(position);
}
/**
*
* 便
*/
@Override
protected void onContentChanged() {
super.onContentChanged();
calcNotesCount();
}
/**
*
* 便
* @param cursor
*/
@Override
public void changeCursor(Cursor cursor) {
super.changeCursor(cursor);
calcNotesCount();
}
/**
* 便
*/
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++;
}
@ -266,4 +181,4 @@ public class NotesListAdapter extends CursorAdapter {
}
}
}
}
}

@ -94,7 +94,8 @@ public class NotesListItem extends LinearLayout {
}
}
}
mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
//mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
mTime.setText(DateUtils.formatDateTime(getContext(),data.getModifiedDate(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL));
setBackground(data);
}

@ -47,57 +47,44 @@ import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.gtask.remote.GTaskSyncService;
/**
* NotesPreferenceActivity 便
*
*/
public class NotesPreferenceActivity extends PreferenceActivity {
// 偏好设置的名称
public static final String PREFERENCE_NAME = "notes_preferences";
// 同步账户名称的偏好设置键
public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name";
// 上次同步时间的偏好设置键
public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time";
// 设置背景颜色随机显示的偏好设置键
public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear";
// 同步账户的偏好设置类别键
private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key";
// 权限过滤器键
private static final String AUTHORITIES_FILTER_KEY = "authorities";
// 同步账户的偏好设置类别
private PreferenceCategory mAccountCategory;
// 用于接收 GTask 同步服务广播的接收器
private GTaskReceiver mReceiver;
// 原始的 Google 账户数组
private Account[] mOriAccounts;
// 标记是否添加了新账户
private boolean mHasAddedAccount;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
// 设置 ActionBar 可返回
/* using the app icon for navigation */
getActionBar().setDisplayHomeAsUpEnabled(true);
// 加载偏好设置资源
addPreferencesFromResource(R.xml.preferences);
// 找到同步账户的偏好设置类别
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY);
// 创建 GTask 广播接收器
mReceiver = new GTaskReceiver();
// 创建意图过滤器,用于接收 GTask 同步服务的广播
IntentFilter filter = new IntentFilter();
filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME);
// 注册广播接收器
registerReceiver(mReceiver, filter);
// 初始化原始账户数组
mOriAccounts = null;
// 加载设置界面的头部视图
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null);
// 将头部视图添加到列表视图中
getListView().addHeaderView(header, null, true);
}
@ -105,7 +92,8 @@ public class NotesPreferenceActivity extends PreferenceActivity {
protected void onResume() {
super.onResume();
// 如果添加了新账户,自动设置同步账户
// need to set sync account automatically if user has added a new
// account
if (mHasAddedAccount) {
Account[] accounts = getGoogleAccounts();
if (mOriAccounts != null && accounts.length > mOriAccounts.length) {
@ -125,48 +113,36 @@ public class NotesPreferenceActivity extends PreferenceActivity {
}
}
// 刷新界面
refreshUI();
}
@Override
protected void onDestroy() {
// 注销广播接收器
if (mReceiver != null) {
unregisterReceiver(mReceiver);
}
super.onDestroy();
}
/**
*
*/
private void loadAccountPreference() {
// 清空账户偏好设置类别中的所有项
mAccountCategory.removeAll();
// 创建一个新的偏好设置项
Preference accountPref = new Preference(this);
// 获取默认的同步账户名称
final String defaultAccount = getSyncAccountName(this);
// 设置偏好设置项的标题
accountPref.setTitle(getString(R.string.preferences_account_title));
// 设置偏好设置项的摘要
accountPref.setSummary(getString(R.string.preferences_account_summary));
// 设置偏好设置项的点击监听器
accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
// 如果当前没有正在同步
if (!GTaskSyncService.isSyncing()) {
// 如果默认账户为空,显示选择账户的对话框
if (TextUtils.isEmpty(defaultAccount)) {
// the first time to set account
showSelectAccountAlertDialog();
} else {
// 如果已经设置了账户,显示更改账户的确认对话框
// if the account has already been set, we need to promp
// user about the risk
showChangeAccountConfirmAlertDialog();
}
} else {
// 如果正在同步,显示提示信息
Toast.makeText(NotesPreferenceActivity.this,
R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
.show();
@ -175,24 +151,18 @@ public class NotesPreferenceActivity extends PreferenceActivity {
}
});
// 将账户偏好设置项添加到账户偏好设置类别中
mAccountCategory.addPreference(accountPref);
}
/**
*
*/
private void loadSyncButton() {
// 找到同步按钮和上次同步时间的文本视图
Button syncButton = (Button) findViewById(R.id.preference_sync_button);
TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
// 根据同步状态设置按钮的文本和点击监听器
// set button state
if (GTaskSyncService.isSyncing()) {
syncButton.setText(getString(R.string.preferences_button_sync_cancel));
syncButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 取消同步
GTaskSyncService.cancelSync(NotesPreferenceActivity.this);
}
});
@ -200,15 +170,13 @@ public class NotesPreferenceActivity extends PreferenceActivity {
syncButton.setText(getString(R.string.preferences_button_sync_immediately));
syncButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 立即开始同步
GTaskSyncService.startSync(NotesPreferenceActivity.this);
}
});
}
// 如果同步账户不为空,启用同步按钮
syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this)));
// 根据同步状态设置上次同步时间的显示
// set last sync time
if (GTaskSyncService.isSyncing()) {
lastSyncTimeView.setText(GTaskSyncService.getProgressString());
lastSyncTimeView.setVisibility(View.VISIBLE);
@ -225,44 +193,30 @@ public class NotesPreferenceActivity extends PreferenceActivity {
}
}
/**
*
*/
private void refreshUI() {
loadAccountPreference();
loadSyncButton();
}
/**
*
*/
private void showSelectAccountAlertDialog() {
// 创建一个 AlertDialog 构建器
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// 加载账户选择对话框的标题视图
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips));
// 设置对话框的自定义标题
dialogBuilder.setCustomTitle(titleView);
dialogBuilder.setPositiveButton(null, null);
// 获取所有的 Google 账户
Account[] accounts = getGoogleAccounts();
// 获取默认的同步账户名称
String defAccount = getSyncAccountName(this);
// 记录原始账户数组
mOriAccounts = accounts;
// 标记未添加新账户
mHasAddedAccount = false;
if (accounts.length > 0) {
// 创建一个字符序列数组,用于显示账户名称
CharSequence[] items = new CharSequence[accounts.length];
final CharSequence[] itemMapping = items;
int checkedItem = -1;
@ -273,201 +227,134 @@ public class NotesPreferenceActivity extends PreferenceActivity {
}
items[index++] = account.name;
}
// 设置单选列表项
dialogBuilder.setSingleChoiceItems(items, checkedItem,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 设置选择的账户为同步账户
setSyncAccount(itemMapping[which].toString());
// 关闭对话框
dialog.dismiss();
// 刷新界面
refreshUI();
}
});
}
// 加载添加账户的视图
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null);
dialogBuilder.setView(addAccountView);
// 显示对话框
final AlertDialog dialog = dialogBuilder.show();
addAccountView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 标记添加了新账户
mHasAddedAccount = true;
// 创建一个添加账户的意图
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] {
"gmail-ls"
});
// 启动添加账户的活动
startActivityForResult(intent, -1);
// 关闭对话框
dialog.dismiss();
}
});
}
/**
*
*/
private void showChangeAccountConfirmAlertDialog() {
// 创建一个 AlertDialog 构建器
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// 加载账户更改对话框的标题视图
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
getSyncAccountName(this)));
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg));
// 设置对话框的自定义标题
dialogBuilder.setCustomTitle(titleView);
// 创建一个菜单项数组
CharSequence[] menuItemArray = new CharSequence[] {
getString(R.string.preferences_menu_change_account),
getString(R.string.preferences_menu_remove_account),
getString(R.string.preferences_menu_cancel)
};
// 设置菜单项的点击监听器
dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
// 显示选择账户的对话框
showSelectAccountAlertDialog();
} else if (which == 1) {
// 移除同步账户
removeSyncAccount();
// 刷新界面
refreshUI();
}
}
});
// 显示对话框
dialogBuilder.show();
}
/**
* Google
* @return Google
*/
private Account[] getGoogleAccounts() {
// 获取账户管理器
AccountManager accountManager = AccountManager.get(this);
// 获取所有的 Google 账户
return accountManager.getAccountsByType("com.google");
}
/**
*
* @param account
*/
private void setSyncAccount(String account) {
// 如果当前同步账户与要设置的账户不同
if (!getSyncAccountName(this).equals(account)) {
// 获取共享偏好设置
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
if (account != null) {
// 保存同步账户名称
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account);
} else {
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
}
// 提交更改
editor.commit();
// 清空上次同步时间
// clean up last sync time
setLastSyncTime(this, 0);
// 清空本地的 GTask 相关信息
// 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);
// 更新所有便签的 GTask 相关信息
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
}
}).start();
// 显示设置成功的提示信息
Toast.makeText(NotesPreferenceActivity.this,
getString(R.string.preferences_toast_success_set_accout, account),
Toast.LENGTH_SHORT).show();
}
}
/**
*
*/
private void removeSyncAccount() {
// 获取共享偏好设置
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) {
// 移除同步账户名称
editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME);
}
if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) {
// 移除上次同步时间
editor.remove(PREFERENCE_LAST_SYNC_TIME);
}
// 提交更改
editor.commit();
// 清空本地的 GTask 相关信息
// 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);
// 更新所有便签的 GTask 相关信息
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
}
}).start();
}
/**
*
* @param context
* @return
*/
public static String getSyncAccountName(Context context) {
// 获取共享偏好设置
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
// 获取同步账户名称
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
}
/**
*
* @param context
* @param time
*/
public static void setLastSyncTime(Context context, long time) {
// 获取共享偏好设置
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
// 保存上次同步时间
editor.putLong(PREFERENCE_LAST_SYNC_TIME, time);
// 提交更改
editor.commit();
}
/**
*
* @param context
* @return 0
*/
public static long getLastSyncTime(Context context) {
// 获取共享偏好设置
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0);

@ -0,0 +1,168 @@
package net.micode.notes.ui;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import net.micode.notes.R;
import net.micode.notes.ui.adapter.MyAdapter;
import net.micode.notes.ui.bean.Note;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class SafeFolderActivity extends Activity {
private RecyclerView mRecyclerView;
private Button mBtnNew;
private List<Note> mNotes;
private MyAdapter mMyAdapter;
private NoteDbOpenHelper mNoteDbOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_safe_folder);
initView();
initData();
initEvent();
}
@Override
protected void onResume() {
super.onResume();
refreshDataFromDb();
}
private void refreshDataFromDb() {
mNotes = getDataFromDB();
mMyAdapter.refreshData(mNotes);
}
private void initEvent() {
mMyAdapter = new MyAdapter(this,mNotes);
mRecyclerView.setAdapter(mMyAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
}
private void initData() {
mNotes = new ArrayList<>();
mNoteDbOpenHelper = new NoteDbOpenHelper(this);
}
private List<Note> getDataFromDB() {
return mNoteDbOpenHelper.queryAllFromDb();
}
private String getCurrentTimeFormat(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd HH:mm:ss");
Date date = new Date();
return simpleDateFormat.format(date);
}
private void initView(){
mRecyclerView = findViewById(R.id.sf_list);
}
public void NewNotes(View view) {
Intent intent = new Intent(SafeFolderActivity.this,AddActivity.class);
startActivity(intent);
}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
//
// getMenuInflater().inflate(R.menu.menu_main, menu);
// SearchView searchView = (SearchView) menu.findItem(R.id.sf_menu_search).getActionView();
//
//
// searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
// @Override
// public boolean onQueryTextSubmit(String query) {
// return false;
// }
//
// @Override
// public boolean onQueryTextChange(String newText) {
// mNotes = mNoteDbOpenHelper.queryFromDbByContent(newText);
// mMyAdapter.refreshData(mNotes);
// return true;
// }
// });
// return super.onCreateOptionsMenu(menu);
// }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.safe, menu);
MenuItem x = menu.findItem(R.id.sf_menu_search);
if (x.getActionView() != null) {
Log.i("-------------","okk");
}else{
Log.i("-------------","false");
}
SearchView searchView = (SearchView) menu.findItem(R.id.sf_menu_search).getActionView();
if (searchView != null) {
Log.i("=====================","okk");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
mNotes = mNoteDbOpenHelper.queryFromDbByContent(s);
mMyAdapter.refreshData(mNotes);
return true;
}
});
}else{
Log.i("=====================","false");
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
return super.onOptionsItemSelected(item);
}
}

@ -0,0 +1,134 @@
package net.micode.notes.ui.adapter;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import net.micode.notes.R;
import net.micode.notes.ui.EditActivity;
import net.micode.notes.ui.NoteDbOpenHelper;
import net.micode.notes.ui.NoteEditActivity;
import net.micode.notes.ui.bean.Note;
import net.micode.notes.util.ToastUtil;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Note> mBeanList;
private LayoutInflater mLayoutInflater;
private Context mContext;
private NoteDbOpenHelper mNoteDbOpenHelper;
public MyAdapter(Context context,List<Note> mBeanList){
this.mBeanList = mBeanList;
this.mContext = context;
mLayoutInflater = LayoutInflater.from(mContext);
mNoteDbOpenHelper = new NoteDbOpenHelper(mContext);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mLayoutInflater.inflate(R.layout.list_item_layout,parent,false);
MyViewHolder myViewHolder= new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
Note note = mBeanList.get(position);
holder.mTvContent.setText(note.getContent());
holder.mTvTime.setText(note.getCreatedTime());
holder.rlContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, EditActivity.class);
intent.putExtra("note",note);
mContext.startActivity(intent);
}
});
holder.rlContainer.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//长按
Dialog dialog = new Dialog(mContext,android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar_MinWidth);
View view = mLayoutInflater.inflate(R.layout.list_item_dialog_layout, null);
TextView tvDelete = view.findViewById(R.id.tv_delete);
tvDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int row = mNoteDbOpenHelper.deleteFromDbById(note.getId());
if (row > 0){
removeData(position);
ToastUtil.toastShort(mContext,"Delete Success");
}else{
ToastUtil.toastShort(mContext,"Delete Fail");
}
dialog.dismiss();
}
});
dialog.setContentView(view);
dialog.show();
return false;
}
});
}
@Override
public int getItemCount() {
return mBeanList.size();
}
public void refreshData(List<Note> notes){
this.mBeanList = notes;
notifyDataSetChanged();
}
public void removeData(int pos){
mBeanList.remove(pos);
notifyItemRemoved(pos);
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView mTvContent;
TextView mTvTime;
ViewGroup rlContainer;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
this.mTvContent = itemView.findViewById(R.id.tv_content);
this.mTvTime = itemView.findViewById(R.id.tv_time);
this.rlContainer = itemView.findViewById(R.id.rl_item_container);
}
}
}

@ -0,0 +1,43 @@
package net.micode.notes.ui.bean;
import java.io.Serializable;
public class Note implements Serializable {
private String content;
private String createdTime;
private String id;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getCreatedTime() {
return createdTime;
}
public void setCreatedTime(String createdTime) {
this.createdTime = createdTime;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "Note{" +
"content='" + content + '\'' +
", createdTime='" + createdTime + '\'' +
", id='" + id + '\'' +
'}';
}
}

@ -0,0 +1,47 @@
package net.micode.notes.ui;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import net.micode.notes.R;
import net.micode.notes.util.ToastUtil;
public class pwdActivity extends NotesListActivity {
public Button mBtnpwd;
public EditText mEtpwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pwd);
mBtnpwd = findViewById(R.id.btn_pwd);
mEtpwd = findViewById(R.id.et_1);
mBtnpwd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String pwd = mEtpwd.getText().toString();
Intent intent=null;
if(pwd.equals("123")){
intent = new Intent(pwdActivity.this,SafeFolderActivity.class);
startActivity(intent);
}else{
ToastUtil.toastShort(pwdActivity.this,"ERROR");
}
}
});
}
}

@ -0,0 +1,38 @@
package net.micode.notes.util;
import android.content.Context;
import android.content.SharedPreferences;
public class SpfUtil {
private static String SPF_NAME = "noteSpf";
public static void saveString(Context context, String key, String value) {
SharedPreferences spf = context.getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = spf.edit();
edit.putString(key, value);
edit.apply();
}
public static String getString(Context context, String key) {
SharedPreferences spf = context.getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
return spf.getString(key, "");
}
public static void saveInt(Context context, String key, int value) {
SharedPreferences spf = context.getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = spf.edit();
edit.putInt(key, value);
edit.apply();
}
public static int getInt(Context context, String key) {
SharedPreferences spf = context.getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
return spf.getInt(key, -1);
}
public static int getIntWithDefault(Context context, String key, int defValue) {
SharedPreferences spf = context.getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
return spf.getInt(key, defValue);
}
}

@ -0,0 +1,15 @@
package net.micode.notes.util;
import android.content.Context;
import android.widget.Toast;
public class ToastUtil {
public static void toastShort(Context context, String msg) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
public static void toastLong(Context context, String msg) {
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
}
Loading…
Cancel
Save