代码逻辑优化

master
liuyx 2 years ago
parent fec99da20f
commit 4a6219c302

@ -6,30 +6,27 @@
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" /> <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<application <application
android:theme="@style/DayTheme"
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_app" android:icon="@mipmap/ic_app"
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_app_round" android:roundIcon="@mipmap/ic_app_round"
android:supportsRtl="true" android:supportsRtl="true"
> android:theme="@style/DayTheme">
<activity android:name="cc.liuyx.note.activity.MainActivity" <activity
android:name="cc.liuyx.note.activity.MainActivity"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity>
<activity android:name="cc.liuyx.note.activity.EditActivity">
</activity> </activity>
<activity android:name="cc.liuyx.note.activity.EditActivity" />
<activity android:name="cc.liuyx.note.activity.FabColorActivity" /> <activity android:name="cc.liuyx.note.activity.FabColorActivity" />
<activity android:name="cc.liuyx.note.activity.UserSettingsActivity" /> <activity android:name="cc.liuyx.note.activity.UserSettingsActivity" />
<activity android:name="cc.liuyx.note.alarm.EditAlarmActivity" /> <activity android:name="cc.liuyx.note.activity.UserSettingsActivity$EditAlarmActivity" />
<activity android:name="cc.liuyx.note.activity.AboutActivity" />
<receiver android:name="cc.liuyx.note.alarm.AlarmReceiver" /> <receiver android:name="cc.liuyx.note.alarm.AlarmReceiver" />
</application> </application>

@ -0,0 +1,45 @@
package cc.liuyx.note.activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.LinearLayout;
import android.widget.Switch;
import com.example.atry.R;
import java.util.Objects;
public class AboutActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Intent intent = getIntent();
boolean night_change;
if (intent.getExtras() != null)
night_change = intent.getBooleanExtra("night_change", false);
else night_change = false;
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
Objects.requireNonNull(getSupportActionBar()).setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (isNightMode())
myToolbar.setNavigationIcon(getDrawable(R.drawable.ic_more));
else myToolbar.setNavigationIcon(getDrawable(R.drawable.ic_more));
}
@Override
protected void needRefresh() {
}
}

@ -47,13 +47,13 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import cc.liuyx.note.alarm.AlarmReceiver; import cc.liuyx.note.alarm.AlarmReceiver;
import cc.liuyx.note.alarm.EditAlarmActivity; import cc.liuyx.note.db.PlanDatabase;
import cc.liuyx.note.alarm.Plan; import cc.liuyx.note.entity.Plan;
import cc.liuyx.note.alarm.PlanAdapter; import cc.liuyx.note.adapter.PlanAdapter;
import cc.liuyx.note.alarm.PlanDatabase; import cc.liuyx.note.db.PlanCRUD;
import cc.liuyx.note.adapter.NoteAdapter; import cc.liuyx.note.adapter.NoteAdapter;
import cc.liuyx.note.adapter.TagAdapter; import cc.liuyx.note.adapter.TagAdapter;
import cc.liuyx.note.db.CRUD; import cc.liuyx.note.db.NoteCRUD;
import cc.liuyx.note.db.NoteDatabase; import cc.liuyx.note.db.NoteDatabase;
import cc.liuyx.note.entity.Note; import cc.liuyx.note.entity.Note;
@ -105,6 +105,8 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
private TextView setting_text; private TextView setting_text;
private ImageView setting_image; private ImageView setting_image;
private TextView about_text;
private ImageView about_image;
private ListView lv_tag; private ListView lv_tag;
private TextView add_tag; private TextView add_tag;
@ -161,6 +163,10 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
setting_text = customView.findViewById(R.id.setting_settings_text); setting_text = customView.findViewById(R.id.setting_settings_text);
setting_image = customView.findViewById(R.id.setting_settings_image); setting_image = customView.findViewById(R.id.setting_settings_image);
about_image = customView.findViewById(R.id.setting_about_image);
about_text = customView.findViewById(R.id.setting_about_text);
lv_tag = customView.findViewById(R.id.lv_tag); lv_tag = customView.findViewById(R.id.lv_tag);
add_tag = customView.findViewById(R.id.add_tag); add_tag = customView.findViewById(R.id.add_tag);
@ -170,7 +176,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
if (sharedPreferences.getString("tagListString", "").split("_").length < 8) { if (sharedPreferences.getString("tagListString", "").split("_").length < 8) {
final EditText et = new EditText(context); final EditText et = new EditText(context);
new AlertDialog.Builder(MainActivity.this) new AlertDialog.Builder(MainActivity.this)
.setMessage("Enter the name of tag") .setMessage("输入标签名")
.setView(et) .setView(et)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override @Override
@ -184,7 +190,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
String newTagListString = oldTagListString + "_" + name; String newTagListString = oldTagListString + "_" + name;
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("tagListString", newTagListString); editor.putString("tagListString", newTagListString);
editor.commit(); editor.apply();
refreshTagList(); refreshTagList();
} else } else
Toast.makeText(context, "Repeated tag!", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Repeated tag!", Toast.LENGTH_SHORT).show();
@ -254,7 +260,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
Note temp = noteList.get(i); Note temp = noteList.get(i);
if (temp.getTag() == tag) { if (temp.getTag() == tag) {
temp.setTag(1); temp.setTag(1);
CRUD op = new CRUD(context); NoteCRUD op = new NoteCRUD(context);
op.open(); op.open();
op.updateNote(temp); op.updateNote(temp);
op.close(); op.close();
@ -268,7 +274,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
Note temp = noteList.get(i); Note temp = noteList.get(i);
if (temp.getTag() == j) { if (temp.getTag() == j) {
temp.setTag(j - 1); temp.setTag(j - 1);
CRUD op = new CRUD(context); NoteCRUD op = new NoteCRUD(context);
op.open(); op.open();
op.updateNote(temp); op.updateNote(temp);
op.close(); op.close();
@ -278,14 +284,13 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
} }
//edit the preference //edit the preference
List<String> newTagList = new ArrayList<>(); List<String> newTagList = new ArrayList<>(tagList);
newTagList.addAll(tagList);
newTagList.remove(position); newTagList.remove(position);
String newTagListString = TextUtils.join("_", newTagList); String newTagListString = TextUtils.join("_", newTagList);
Log.d(TAG, "onClick: " + newTagListString); Log.d(TAG, "onClick: " + newTagListString);
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("tagListString", newTagListString); editor.putString("tagListString", newTagListString);
editor.commit(); editor.apply();
refreshTagList(); refreshTagList();
} }
@ -304,7 +309,6 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
} }
}); });
setting_text.setOnClickListener(new OnClickListener() { setting_text.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -322,8 +326,24 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
} }
}); });
about_text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, AboutActivity.class));
overridePendingTransition(R.anim.in_lefttoright, R.anim.no);
}
});
about_image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, AboutActivity.class));
overridePendingTransition(R.anim.in_lefttoright, R.anim.no);
}
});
coverView.setOnTouchListener(new View.OnTouchListener() { coverView.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override @Override
public boolean onTouch(View v, MotionEvent event) { public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss(); popupWindow.dismiss();
@ -408,7 +428,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("content_switch", isChecked); editor.putBoolean("content_switch", isChecked);
editor.commit(); editor.apply();
refreshLvVisibility(); refreshLvVisibility();
} }
}); });
@ -426,7 +446,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
fab_alarm.setOnClickListener(new OnClickListener() { fab_alarm.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, EditAlarmActivity.class); Intent intent = new Intent(MainActivity.this, UserSettingsActivity.EditAlarmActivity.class);
intent.putExtra("mode", 2); // MODE of 'new plan' intent.putExtra("mode", 2); // MODE of 'new plan'
startActivityForResult(intent, 1); startActivityForResult(intent, 1);
overridePendingTransition(R.anim.in_righttoleft, R.anim.no); overridePendingTransition(R.anim.in_righttoleft, R.anim.no);
@ -441,7 +461,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
setSupportActionBar(myToolbar); setSupportActionBar(myToolbar);
getSupportActionBar().setHomeButtonEnabled(true); Objects.requireNonNull(getSupportActionBar()).setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //设置toolbar取代actionbar getSupportActionBar().setDisplayHomeAsUpEnabled(true); //设置toolbar取代actionbar
initPopupView(); initPopupView();
} }
@ -482,32 +502,32 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
if (!sharedPreferences.contains("nightMode")) { if (!sharedPreferences.contains("nightMode")) {
editor.putBoolean("nightMode", false); editor.putBoolean("nightMode", false);
editor.commit(); editor.apply();
} }
if (!sharedPreferences.contains("reverseSort")) { if (!sharedPreferences.contains("reverseSort")) {
editor.putBoolean("reverseSort", false); editor.putBoolean("reverseSort", false);
editor.commit(); editor.apply();
} }
if (!sharedPreferences.contains("fabColor")) { if (!sharedPreferences.contains("fabColor")) {
editor.putInt("fabColor", -500041); editor.putInt("fabColor", -500041);
editor.commit(); editor.apply();
} }
if (!sharedPreferences.contains("tagListString")) { if (!sharedPreferences.contains("tagListString")) {
String s = "no tag_life_study_work_play"; String s = "no tag_life_study_work_play";
editor.putString("tagListString", s); editor.putString("tagListString", s);
editor.commit(); editor.apply();
} }
if (!sharedPreferences.contains("content_switch")) { if (!sharedPreferences.contains("content_switch")) {
editor.putBoolean("content_switch", false); editor.putBoolean("content_switch", false);
editor.commit(); editor.apply();
} }
if (!sharedPreferences.contains("fabPlanColor")) { if (!sharedPreferences.contains("fabPlanColor")) {
editor.putInt("fabPlanColor", -500041); editor.putInt("fabPlanColor", -500041);
editor.commit(); editor.apply();
} }
if (!sharedPreferences.contains("noteTitle")) { if (!sharedPreferences.contains("noteTitle")) {
editor.putBoolean("noteTitle", true); editor.putBoolean("noteTitle", true);
editor.commit(); editor.apply();
} }
@ -549,9 +569,9 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Delete all " + itemName); builder.setTitle("Delete all " + itemName);
builder.setIcon(R.drawable.ic_error_outline_black_24dp); builder.setIcon(R.drawable.ic_error_outline_black_24dp);
builder.setItems(list_String, new DialogInterface.OnClickListener() {//列表对话框; builder.setItems(list_String, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, final int which) {//根据这里which值即可以指定是点击哪一个Item public void onClick(DialogInterface dialog, final int which) {
new AlertDialog.Builder(MainActivity.this) new AlertDialog.Builder(MainActivity.this)
.setMessage("Do you want to delete all " + itemName + " " + list_String[which] + "? ") .setMessage("Do you want to delete all " + itemName + " " + list_String[which] + "? ")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@ -563,6 +583,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
} }
//根据模式与时长删除对顶的计划s/笔记s //根据模式与时长删除对顶的计划s/笔记s
@SuppressLint("Range")
private void removeSelectItems(int which, int mode) { private void removeSelectItems(int which, int mode) {
int monthNum = 0; int monthNum = 0;
switch (which) { switch (which) {
@ -582,14 +603,14 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
Calendar rightNow = Calendar.getInstance(); Calendar rightNow = Calendar.getInstance();
rightNow.add(Calendar.MONTH, -monthNum);//日期加3个月 rightNow.add(Calendar.MONTH, -monthNum);//日期加3个月
Date selectDate = rightNow.getTime(); Date selectDate = rightNow.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String selectDateStr = simpleDateFormat.format(selectDate); String selectDateStr = simpleDateFormat.format(selectDate);
Log.d(TAG, "removeSelectItems: " + selectDateStr); Log.d(TAG, "removeSelectItems: " + selectDateStr);
switch (mode) { switch (mode) {
case 1: //notes case 1: //notes
dbHelper = new NoteDatabase(context); dbHelper = new NoteDatabase(context);
SQLiteDatabase db = dbHelper.getWritableDatabase(); SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from notes", null); @SuppressLint("Recycle") Cursor cursor = db.rawQuery("select * from notes", null);
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
if (cursor.getString(cursor.getColumnIndex(NoteDatabase.TIME)).compareTo(selectDateStr) < 0) { if (cursor.getString(cursor.getColumnIndex(NoteDatabase.TIME)).compareTo(selectDateStr) < 0) {
db.delete("notes", NoteDatabase.ID + "=?", new String[]{Long.toString(cursor.getLong(cursor.getColumnIndex(NoteDatabase.ID)))}); db.delete("notes", NoteDatabase.ID + "=?", new String[]{Long.toString(cursor.getLong(cursor.getColumnIndex(NoteDatabase.ID)))});
@ -601,7 +622,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
case 2: //plans case 2: //plans
planDbHelper = new PlanDatabase(context); planDbHelper = new PlanDatabase(context);
SQLiteDatabase pdb = planDbHelper.getWritableDatabase(); SQLiteDatabase pdb = planDbHelper.getWritableDatabase();
Cursor pcursor = pdb.rawQuery("select * from plans", null); @SuppressLint("Recycle") Cursor pcursor = pdb.rawQuery("select * from plans", null);
while (pcursor.moveToNext()) { while (pcursor.moveToNext()) {
if (pcursor.getString(pcursor.getColumnIndex(PlanDatabase.TIME)).compareTo(selectDateStr) < 0) { if (pcursor.getString(pcursor.getColumnIndex(PlanDatabase.TIME)).compareTo(selectDateStr) < 0) {
pdb.delete("plans", PlanDatabase.ID + "=?", new String[]{Long.toString(pcursor.getLong(pcursor.getColumnIndex(PlanDatabase.ID)))}); pdb.delete("plans", PlanDatabase.ID + "=?", new String[]{Long.toString(pcursor.getLong(pcursor.getColumnIndex(PlanDatabase.ID)))});
@ -635,6 +656,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
} }
@SuppressLint("NonConstantResourceId")
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
@ -647,8 +669,8 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
dbHelper = new NoteDatabase(context); dbHelper = new NoteDatabase(context);
SQLiteDatabase db = dbHelper.getWritableDatabase(); SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("notes", null, null);//delete data in table NOTES db.delete("notes", null, null);
db.execSQL("update sqlite_sequence set seq=0 where name='notes'"); //reset id to 1 db.execSQL("update sqlite_sequence set seq=0 where name='notes'");
refreshListView(); refreshListView();
} }
}).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@ -665,8 +687,8 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
planDbHelper = new PlanDatabase(context); planDbHelper = new PlanDatabase(context);
SQLiteDatabase db = planDbHelper.getWritableDatabase(); SQLiteDatabase db = planDbHelper.getWritableDatabase();
db.delete("plans", null, null);//delete data in table NOTES db.delete("plans", null, null);
db.execSQL("update sqlite_sequence set seq=0 where name='plans'"); //reset id to 1 db.execSQL("update sqlite_sequence set seq=0 where name='plans'");
refreshListView(); refreshListView();
} }
}).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@ -679,7 +701,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
break; break;
case R.id.refresh: case R.id.refresh:
myToolbar.setTitle("All Notes"); myToolbar.setTitle("所有笔记");
lv.setAdapter(adapter); lv.setAdapter(adapter);
break; break;
} }
@ -694,7 +716,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
int fabPlanColor = sharedPreferences.getInt("fabPlanColor", -500041); int fabPlanColor = sharedPreferences.getInt("fabPlanColor", -500041);
chooseFabPlanColor(fabPlanColor); chooseFabPlanColor(fabPlanColor);
//initialize CRUD //initialize CRUD
CRUD op = new CRUD(context); NoteCRUD op = new NoteCRUD(context);
op.open(); op.open();
// set adapter // set adapter
@ -705,7 +727,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
op.close(); op.close();
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
cc.liuyx.note.alarm.CRUD op1 = new cc.liuyx.note.alarm.CRUD(context); PlanCRUD op1 = new PlanCRUD(context);
op1.open(); op1.open();
if (planList.size() > 0) { if (planList.size() > 0) {
cancelAlarms(planList);//删除所有闹钟 cancelAlarms(planList);//删除所有闹钟
@ -801,6 +823,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
} }
//click item in listView //click item in listView
@SuppressLint("NonConstantResourceId")
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) { switch (parent.getId()) {
@ -817,7 +840,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
break; break;
case R.id.lv_plan: case R.id.lv_plan:
Plan curPlan = (Plan) parent.getItemAtPosition(position); Plan curPlan = (Plan) parent.getItemAtPosition(position);
Intent intent1 = new Intent(MainActivity.this, EditAlarmActivity.class); Intent intent1 = new Intent(MainActivity.this, UserSettingsActivity.EditAlarmActivity.class);
intent1.putExtra("title", curPlan.getTitle()); intent1.putExtra("title", curPlan.getTitle());
intent1.putExtra("content", curPlan.getContent()); intent1.putExtra("content", curPlan.getContent());
intent1.putExtra("time", curPlan.getTime()); intent1.putExtra("time", curPlan.getTime());
@ -834,6 +857,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
int returnMode; int returnMode;
long note_Id; long note_Id;
assert data != null;
returnMode = data.getExtras().getInt("mode", -1); returnMode = data.getExtras().getInt("mode", -1);
note_Id = data.getExtras().getLong("id", 0); note_Id = data.getExtras().getLong("id", 0);
if (returnMode == 1) { //update current note if (returnMode == 1) { //update current note
@ -843,7 +867,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
int tag = data.getExtras().getInt("tag", 1); int tag = data.getExtras().getInt("tag", 1);
Note newNote = new Note(content, time, tag); Note newNote = new Note(content, time, tag);
newNote.setId(note_Id); newNote.setId(note_Id);
CRUD op = new CRUD(context); NoteCRUD op = new NoteCRUD(context);
op.open(); op.open();
op.updateNote(newNote); op.updateNote(newNote);
achievement.editNote(op.getNote(note_Id).getContent(), content); achievement.editNote(op.getNote(note_Id).getContent(), content);
@ -852,7 +876,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
} else if (returnMode == 2) { //delete current note } else if (returnMode == 2) { //delete current note
Note curNote = new Note(); Note curNote = new Note();
curNote.setId(note_Id); curNote.setId(note_Id);
CRUD op = new CRUD(context); NoteCRUD op = new NoteCRUD(context);
op.open(); op.open();
op.removeNote(curNote); op.removeNote(curNote);
op.close(); op.close();
@ -862,7 +886,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
String time = data.getExtras().getString("time"); String time = data.getExtras().getString("time");
int tag = data.getExtras().getInt("tag", 1); int tag = data.getExtras().getInt("tag", 1);
Note newNote = new Note(content, time, tag); Note newNote = new Note(content, time, tag);
CRUD op = new CRUD(context); NoteCRUD op = new NoteCRUD(context);
op.open(); op.open();
op.addNote(newNote); op.addNote(newNote);
op.close(); op.close();
@ -874,14 +898,14 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
Log.d(TAG, time); Log.d(TAG, time);
Plan plan = new Plan(title, content, time); Plan plan = new Plan(title, content, time);
plan.setId(note_Id); plan.setId(note_Id);
cc.liuyx.note.alarm.CRUD op = new cc.liuyx.note.alarm.CRUD(context); PlanCRUD op = new PlanCRUD(context);
op.open(); op.open();
op.updatePlan(plan); op.updatePlan(plan);
op.close(); op.close();
} else if (returnMode == 12) {//delete existing plan } else if (returnMode == 12) {//delete existing plan
Plan plan = new Plan(); Plan plan = new Plan();
plan.setId(note_Id); plan.setId(note_Id);
cc.liuyx.note.alarm.CRUD op = new cc.liuyx.note.alarm.CRUD(context); PlanCRUD op = new PlanCRUD(context);
op.open(); op.open();
op.removePlan(plan); op.removePlan(plan);
op.close(); op.close();
@ -890,28 +914,28 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
String content = data.getExtras().getString("content", null); String content = data.getExtras().getString("content", null);
String time = data.getExtras().getString("time", null); String time = data.getExtras().getString("time", null);
Plan newPlan = new Plan(title, content, time); Plan newPlan = new Plan(title, content, time);
cc.liuyx.note.alarm.CRUD op = new cc.liuyx.note.alarm.CRUD(context); PlanCRUD op = new PlanCRUD(context);
op.open(); op.open();
op.addPlan(newPlan); op.addPlan(newPlan);
Log.d(TAG, "onActivityResult: " + time); Log.d(TAG, "onActivityResult: " + time);
op.close(); op.close();
} else {
} }
refreshListView(); refreshListView();
} }
//longclick item in listView //longclick item in listView
@SuppressLint("NonConstantResourceId")
@Override @Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) { switch (parent.getId()) {
case R.id.lv: case R.id.lv:
final Note note = noteList.get(position); final Note note = noteList.get(position);
new AlertDialog.Builder(MainActivity.this) new AlertDialog.Builder(MainActivity.this)
.setMessage("Do you want to delete this note ?") .setMessage("你想要删除这条笔记吗?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
CRUD op = new CRUD(context); NoteCRUD op = new NoteCRUD(context);
op.open(); op.open();
op.removeNote(note); op.removeNote(note);
op.close(); op.close();
@ -927,12 +951,12 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
case R.id.lv_plan: case R.id.lv_plan:
final Plan plan = planList.get(position); final Plan plan = planList.get(position);
new AlertDialog.Builder(MainActivity.this) new AlertDialog.Builder(MainActivity.this)
.setMessage("Do you want to delete this plan ?") .setMessage("你想要删除这条计划吗?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
cc.liuyx.note.alarm.CRUD op = new cc.liuyx.note.alarm.CRUD(context); PlanCRUD op = new PlanCRUD(context);
op.open(); op.open();
op.removePlan(plan); op.removePlan(plan);
op.close(); op.close();
@ -1021,9 +1045,12 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
Calendar c = p.getPlanTime(); Calendar c = p.getPlanTime();
if (!c.before(Calendar.getInstance())) { if (!c.before(Calendar.getInstance())) {
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
intent.putExtra("title", p.getTitle()); Bundle data = new Bundle();
intent.putExtra("content", p.getContent()); data.putString("title", p.getTitle());
intent.putExtra("id", (int) p.getId()); data.putString("title", p.getTitle());
data.putString("content", p.getContent());
data.putInt("id", (int) p.getId());
intent.putExtras(data);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, (int) p.getId(), intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, (int) p.getId(), intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent); alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
@ -1059,7 +1086,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
//achievement system //achievement system
public class Achievement { public class Achievement {
private SharedPreferences sharedPreferences; private final SharedPreferences sharedPreferences;
private int noteNumber; private int noteNumber;
private int wordNumber; private int wordNumber;
@ -1084,27 +1111,27 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
if (!sharedPreferences.contains("noteLevel")) { if (!sharedPreferences.contains("noteLevel")) {
editor.putInt("noteLevel", 0); editor.putInt("noteLevel", 0);
editor.commit(); editor.apply();
if (!sharedPreferences.contains("wordLevel")) { if (!sharedPreferences.contains("wordLevel")) {
editor.putInt("wordLevel", 0); editor.putInt("wordLevel", 0);
editor.commit(); editor.apply();
addCurrent(noteList); addCurrent(noteList);
if (sharedPreferences.contains("maxRemainNumber")) { if (sharedPreferences.contains("maxRemainNumber")) {
editor.remove("maxRemainNumber"); editor.remove("maxRemainNumber");
editor.commit(); editor.apply();
} }
if (sharedPreferences.contains("remainNumber")) { if (sharedPreferences.contains("remainNumber")) {
editor.remove("remainNumber"); editor.remove("remainNumber");
editor.commit(); editor.apply();
} }
if (!sharedPreferences.contains("noteNumber")) { if (!sharedPreferences.contains("noteNumber")) {
editor.putInt("noteNumber", 0); editor.putInt("noteNumber", 0);
editor.commit(); editor.apply();
addCurrent(noteList); addCurrent(noteList);
if (!sharedPreferences.contains("wordNumber")) { if (!sharedPreferences.contains("wordNumber")) {
editor.putInt("wordNumber", 0); editor.putInt("wordNumber", 0);
editor.commit(); editor.apply();
} }
} }
@ -1132,7 +1159,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
else if (wordCount >= 1000) editor.putInt("noteLevel", 3); else if (wordCount >= 1000) editor.putInt("noteLevel", 3);
else if (wordCount >= 500) editor.putInt("noteLevel", 2); else if (wordCount >= 500) editor.putInt("noteLevel", 2);
else if (wordCount >= 100) editor.putInt("noteLevel", 1); else if (wordCount >= 100) editor.putInt("noteLevel", 1);
editor.commit(); editor.apply();
} }
//添加笔记 //添加笔记
@ -1144,7 +1171,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
wordNumber += content.length(); wordNumber += content.length();
editor.putInt("wordNumber", wordNumber); editor.putInt("wordNumber", wordNumber);
editor.commit(); editor.apply();
} }
//删除笔记 //删除笔记
@ -1158,7 +1185,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
wordNumber += (newContent.length() - oldContent.length()); wordNumber += (newContent.length() - oldContent.length());
editor.putInt("wordNumber", wordNumber); editor.putInt("wordNumber", wordNumber);
editor.commit(); editor.apply();
} }
} }
@ -1231,12 +1258,12 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
case 1: case 1:
noteLevel++; noteLevel++;
editor.putInt("noteLevel", noteLevel); editor.putInt("noteLevel", noteLevel);
editor.commit(); editor.apply();
break; break;
case 2: case 2:
wordLevel++; wordLevel++;
editor.putInt("wordLevel", wordLevel); editor.putInt("wordLevel", wordLevel);
editor.commit(); editor.apply();
break; break;
} }
} }
@ -1257,7 +1284,7 @@ public class MainActivity extends BaseActivity implements OnItemClickListener, O
editor.putInt("wordNumber", wordNumber); editor.putInt("wordNumber", wordNumber);
editor.putInt("noteLevel", 0); editor.putInt("noteLevel", 0);
editor.putInt("wordLevel", 0); editor.putInt("wordLevel", 0);
editor.commit(); editor.apply();
} }
} }

@ -1,20 +1,37 @@
package cc.liuyx.note.activity; package cc.liuyx.note.activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Switch; import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import com.example.atry.R; import com.example.atry.R;
import java.util.Calendar;
import java.util.Objects; import java.util.Objects;
import cc.liuyx.note.entity.Plan;
public class UserSettingsActivity extends BaseActivity { public class UserSettingsActivity extends BaseActivity {
private Switch nightMode; private Switch nightMode;
@ -171,4 +188,305 @@ public class UserSettingsActivity extends BaseActivity {
} }
public static class EditAlarmActivity extends BaseActivity implements View.OnClickListener {
private DatePickerDialog.OnDateSetListener dateSetListener;
private TimePickerDialog.OnTimeSetListener timeSetListener;
private EditText et_title;
private EditText et;
private Button set_date;
private Button set_time;
private TextView date;
private TextView time;
private Plan plan;
private int[] dateArray = new int[3];
private int[] timeArray = new int[2];
private int openMode = 0;
private String old_title = "";
private String old_content = "";
private String old_time = "";
private long id = 0;
private boolean timeChange = false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_alarm_layout);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
init();
final Intent intent = getIntent();
openMode = intent.getExtras().getInt("mode", 0);
if(openMode == 1){
id = intent.getLongExtra("id", 0);
old_title = intent.getStringExtra("title");
old_content = intent.getStringExtra("content");
old_time = intent.getStringExtra("time");
et_title.setText(old_title);
et_title.setSelection(old_title.length());
et.setText(old_content);
et.setSelection(old_content.length());
String[] wholeTime = old_time.split(" ");
String[] temp = wholeTime[0].split("-");
String[] temp1 = wholeTime[1].split(":");
setDateTV(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]), Integer.parseInt(temp[2]));
setTimeTV(Integer.parseInt(temp1[0]), Integer.parseInt(temp1[1]));
}
if(isNightMode()) myToolbar.setNavigationIcon(getDrawable(R.drawable.ic_keyboard_arrow_left_white_24dp));
else myToolbar.setNavigationIcon(getDrawable(R.drawable.ic_keyboard_arrow_left_black_24dp));
myToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!canBeSet()) {
Toast.makeText(EditAlarmActivity.this, "Invalid Time", Toast.LENGTH_SHORT).show();
}else if(et.getText().toString().length() + et_title.getText().toString().length() == 0 && openMode == 2){
Intent intent1 = new Intent();
intent1.putExtra("mode", -1);//nothing new happens.
setResult(RESULT_OK, intent1);
finish();//返回
overridePendingTransition(R.anim.in_lefttoright, R.anim.out_lefttoright);
}
else if (et_title.getText().toString().length() == 0) {
Toast.makeText(EditAlarmActivity.this, "Title cannot be empty", Toast.LENGTH_SHORT).show();
}
else {
isTimeChange();
Intent intent = new Intent();
if (openMode == 2) {
intent.putExtra("mode", 10); // new one plan;
intent.putExtra("title", et_title.getText().toString());
intent.putExtra("content", et.getText().toString());
intent.putExtra("time", date.getText().toString() + " " + time.getText().toString());
Log.d(TAG, date.getText().toString() + time.getText().toString());
} else {
if (et.getText().toString().equals(old_content) && et_title.getText().toString().equals(old_title) && !timeChange) {
intent.putExtra("mode", -1); // edit nothing
}
else {
intent.putExtra("mode", 11); //edit the content
intent.putExtra("title", et_title.getText().toString());
intent.putExtra("content", et.getText().toString());
intent.putExtra("time", date.getText().toString() + " " + time.getText().toString());
intent.putExtra("id", id);
}
}
setResult(RESULT_OK, intent);
finish();//返回
overridePendingTransition(R.anim.in_lefttoright, R.anim.out_lefttoright);
}
}
});
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if( keyCode== KeyEvent.KEYCODE_HOME){
return true;
} else if( keyCode== KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
if(!canBeSet()) {
Toast.makeText(EditAlarmActivity.this, "Invalid Time", Toast.LENGTH_SHORT).show();
}else if(et.getText().toString().length() + et_title.getText().toString().length() == 0 && openMode == 2){
Intent intent1 = new Intent();
intent1.putExtra("mode", -1);//nothing new happens.
setResult(RESULT_OK, intent1);
finish();//返回
overridePendingTransition(R.anim.in_lefttoright, R.anim.out_lefttoright);
}
else if (et_title.getText().toString().length() == 0) {
Toast.makeText(EditAlarmActivity.this, "Title cannot be empty", Toast.LENGTH_SHORT).show();
}
else {
isTimeChange();
Intent intent = new Intent();
if (openMode == 2) {
intent.putExtra("mode", 10); // new one plan;
intent.putExtra("title", et_title.getText().toString());
intent.putExtra("content", et.getText().toString());
intent.putExtra("time", date.getText().toString() + " " + time.getText().toString());
Log.d(TAG, date.getText().toString() + time.getText().toString());
} else {
if (et.getText().toString().equals(old_content) && et_title.getText().toString().equals(old_title) && !timeChange) {
intent.putExtra("mode", -1); // edit nothing
}
else {
intent.putExtra("mode", 11); //edit the content
intent.putExtra("title", et_title.getText().toString());
intent.putExtra("content", et.getText().toString());
intent.putExtra("time", date.getText().toString() + " " + time.getText().toString());
intent.putExtra("id", id);
}
}
setResult(RESULT_OK, intent);
finish();//返回
overridePendingTransition(R.anim.in_lefttoright, R.anim.out_lefttoright);
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.edit_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final Intent intent = new Intent();
switch (item.getItemId()){
case R.id.delete:
new AlertDialog.Builder(EditAlarmActivity.this)
.setMessage("Delete this plan ?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(openMode == 2){
intent.putExtra("mode", -1); // delete the plan
setResult(RESULT_OK, intent);
}
else {
intent.putExtra("mode", 12); // delete the plan
intent.putExtra("id", id);
setResult(RESULT_OK, intent);
}
finish();
}
}).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void needRefresh() {
setNightMode();
startActivity(new Intent(this, EditAlarmActivity.class));
overridePendingTransition(R.anim.night_switch, R.anim.night_switch_over);
finish();
}
private void init(){
plan = new Plan();
dateArray[0] = plan.getYear();
dateArray[1] = plan.getMonth() + 1;
dateArray[2] = plan.getDay();
timeArray[0] = plan.getHour();
timeArray[1] = plan.getMinute();
et_title = findViewById(R.id.et_title);
et = findViewById(R.id.et);
set_date = findViewById(R.id.set_date);
set_time = findViewById(R.id.set_time);
date = findViewById(R.id.date);
time = findViewById(R.id.time);
//initialize two textviews
setDateTV(dateArray[0], dateArray[1], dateArray[2]);
setTimeTV((timeArray[1]>54? timeArray[0]+1 : timeArray[0]), (timeArray[1]+5)%60);
Log.d(TAG, "init: "+dateArray[1]);
set_date.setOnClickListener(this);
set_time.setOnClickListener(this);
dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
setDateTV(year, month+1, dayOfMonth);
}
};
timeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
setTimeTV(hourOfDay, minute);
}
};
}
private void setDateTV(int y, int m, int d){
//update tv and dateArray
String temp = y + "-";
if(m<10) temp += "0";
temp += (m + "-");
if(d<10) temp +="0";
temp += d;
date.setText(temp);
dateArray[0] = y;
dateArray[1] = m;
dateArray[2] = d;
}
private void setTimeTV(int h, int m){
//update tv and timeArra
String temp = "";
if(h<10) temp += "0";
temp += (h + ":");
if(m<10) temp += "0";
temp += m;
time.setText(temp);
timeArray[0] = h;
timeArray[1] = m;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.set_date: //choose day
DatePickerDialog dialog = new DatePickerDialog(EditAlarmActivity.this,
(isNightMode()?R.style.NightDialogTheme :R.style.DayDialogTheme), dateSetListener,
dateArray[0], dateArray[1] - 1, dateArray[2]);
//dialog.getWindow().setBackgroundDrawable(new ColorDrawable((isNightMode()?Color.BLACK : Color.WHITE)));
dialog.show();
break;
case R.id.set_time://choose hour and minute
TimePickerDialog dialog1 = new TimePickerDialog(EditAlarmActivity.this,
(isNightMode()?R.style.NightDialogTheme :R.style.DayDialogTheme), timeSetListener,
timeArray[0], timeArray[1], true);
//dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog1.show();
break;
}
}
private void isTimeChange(){
String newTime = date.getText().toString() + " " + time.getText().toString();
if(!newTime.equals(old_time)) timeChange = true;
}
private boolean canBeSet(){
Calendar calendar = Calendar.getInstance();
calendar.set(dateArray[0], dateArray[1] - 1, dateArray[2], timeArray[0], timeArray[1]);
Calendar cur = Calendar.getInstance();
Log.d(TAG, "canBeSet: " + cur.getTime().toString() + calendar.getTime().toString());
if(cur.before(calendar)) return true;
else {
Toast.makeText(this, "Invalid Time", Toast.LENGTH_SHORT).show();
return false;
}
}
}
} }

@ -1,4 +1,4 @@
package cc.liuyx.note.alarm; package cc.liuyx.note.adapter;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -16,6 +16,8 @@ import com.example.atry.R;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import cc.liuyx.note.entity.Plan;
public class PlanAdapter extends BaseAdapter implements Filterable { public class PlanAdapter extends BaseAdapter implements Filterable {
private Context mContext; private Context mContext;

@ -1,63 +0,0 @@
@startuml
title __ADAPTER's Class Diagram__\n
namespace cc.liuyx.note {
namespace adapter {
class cc.liuyx.note.adapter.NoteAdapter {
- backList : List<Note>
- mContext : Context
- noteList : List<Note>
+ NoteAdapter()
+ getCount()
+ getFilter()
+ getItem()
+ getItemId()
+ getView()
}
}
}
namespace cc.liuyx.note {
namespace adapter {
class cc.liuyx.note.adapter.NoteAdapter.MyFilter {
# performFiltering()
# publishResults()
}
}
}
namespace cc.liuyx.note {
namespace adapter {
class cc.liuyx.note.adapter.TagAdapter {
- context : Context
- numList : List<Integer>
- tagList : List<String>
+ TagAdapter()
+ getCount()
+ getItem()
+ getItemId()
+ getView()
}
}
}
cc.liuyx.note.adapter.NoteAdapter .up.|> android.widget.Filterable
cc.liuyx.note.adapter.NoteAdapter -up-|> android.widget.BaseAdapter
cc.liuyx.note.adapter.NoteAdapter o-- cc.liuyx.note.adapter.NoteAdapter.MyFilter : mFilter
cc.liuyx.note.adapter.NoteAdapter +-down- cc.liuyx.note.adapter.NoteAdapter.MyFilter
cc.liuyx.note.adapter.NoteAdapter.MyFilter -up-|> android.widget.Filter
cc.liuyx.note.adapter.TagAdapter -up-|> android.widget.BaseAdapter
right footer
PlantUML diagram generated by SketchIt! (https://bitbucket.org/pmesmeur/sketch.it)
For more information about this tool, please contact philippe.mesmeur@gmail.com
endfooter
@enduml

@ -8,6 +8,7 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import cc.liuyx.note.activity.MainActivity; import cc.liuyx.note.activity.MainActivity;
@ -15,13 +16,14 @@ import com.example.atry.R;
public class AlarmReceiver extends BroadcastReceiver { public class AlarmReceiver extends BroadcastReceiver {
private String channelId = "Piggy Notes"; private String channelId = "JNote";
private String name = "ChannelName"; private String name = "Liuyx";
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String title = intent.getExtras().getString("title"); Bundle data = intent.getExtras();
String content = intent.getExtras().getString("content"); String title = data.getString("title");
int id = intent.getExtras().getInt("id"); String content = data.getString("content");
int id = data.getInt("id");
Intent intent1 = new Intent(context, MainActivity.class); Intent intent1 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intent1, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE); PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intent1, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);

@ -1,329 +0,0 @@
package cc.liuyx.note.alarm;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import cc.liuyx.note.activity.BaseActivity;
import com.example.atry.R;
import java.util.Calendar;
public class EditAlarmActivity extends BaseActivity implements View.OnClickListener {
private DatePickerDialog.OnDateSetListener dateSetListener;
private TimePickerDialog.OnTimeSetListener timeSetListener;
private EditText et_title;
private EditText et;
private Button set_date;
private Button set_time;
private TextView date;
private TextView time;
private Plan plan;
private int[] dateArray = new int[3];
private int[] timeArray = new int[2];
private int openMode = 0;
private String old_title = "";
private String old_content = "";
private String old_time = "";
private long id = 0;
private boolean timeChange = false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_alarm_layout);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
init();
final Intent intent = getIntent();
openMode = intent.getExtras().getInt("mode", 0);
if(openMode == 1){
id = intent.getLongExtra("id", 0);
old_title = intent.getStringExtra("title");
old_content = intent.getStringExtra("content");
old_time = intent.getStringExtra("time");
et_title.setText(old_title);
et_title.setSelection(old_title.length());
et.setText(old_content);
et.setSelection(old_content.length());
String[] wholeTime = old_time.split(" ");
String[] temp = wholeTime[0].split("-");
String[] temp1 = wholeTime[1].split(":");
setDateTV(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]), Integer.parseInt(temp[2]));
setTimeTV(Integer.parseInt(temp1[0]), Integer.parseInt(temp1[1]));
}
if(isNightMode()) myToolbar.setNavigationIcon(getDrawable(R.drawable.ic_keyboard_arrow_left_white_24dp));
else myToolbar.setNavigationIcon(getDrawable(R.drawable.ic_keyboard_arrow_left_black_24dp));
myToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!canBeSet()) {
Toast.makeText(EditAlarmActivity.this, "Invalid Time", Toast.LENGTH_SHORT).show();
}else if(et.getText().toString().length() + et_title.getText().toString().length() == 0 && openMode == 2){
Intent intent1 = new Intent();
intent1.putExtra("mode", -1);//nothing new happens.
setResult(RESULT_OK, intent1);
finish();//返回
overridePendingTransition(R.anim.in_lefttoright, R.anim.out_lefttoright);
}
else if (et_title.getText().toString().length() == 0) {
Toast.makeText(EditAlarmActivity.this, "Title cannot be empty", Toast.LENGTH_SHORT).show();
}
else {
isTimeChange();
Intent intent = new Intent();
if (openMode == 2) {
intent.putExtra("mode", 10); // new one plan;
intent.putExtra("title", et_title.getText().toString());
intent.putExtra("content", et.getText().toString());
intent.putExtra("time", date.getText().toString() + " " + time.getText().toString());
Log.d(TAG, date.getText().toString() + time.getText().toString());
} else {
if (et.getText().toString().equals(old_content) && et_title.getText().toString().equals(old_title) && !timeChange) {
intent.putExtra("mode", -1); // edit nothing
}
else {
intent.putExtra("mode", 11); //edit the content
intent.putExtra("title", et_title.getText().toString());
intent.putExtra("content", et.getText().toString());
intent.putExtra("time", date.getText().toString() + " " + time.getText().toString());
intent.putExtra("id", id);
}
}
setResult(RESULT_OK, intent);
finish();//返回
overridePendingTransition(R.anim.in_lefttoright, R.anim.out_lefttoright);
}
}
});
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if( keyCode== KeyEvent.KEYCODE_HOME){
return true;
} else if( keyCode== KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
if(!canBeSet()) {
Toast.makeText(EditAlarmActivity.this, "Invalid Time", Toast.LENGTH_SHORT).show();
}else if(et.getText().toString().length() + et_title.getText().toString().length() == 0 && openMode == 2){
Intent intent1 = new Intent();
intent1.putExtra("mode", -1);//nothing new happens.
setResult(RESULT_OK, intent1);
finish();//返回
overridePendingTransition(R.anim.in_lefttoright, R.anim.out_lefttoright);
}
else if (et_title.getText().toString().length() == 0) {
Toast.makeText(EditAlarmActivity.this, "Title cannot be empty", Toast.LENGTH_SHORT).show();
}
else {
isTimeChange();
Intent intent = new Intent();
if (openMode == 2) {
intent.putExtra("mode", 10); // new one plan;
intent.putExtra("title", et_title.getText().toString());
intent.putExtra("content", et.getText().toString());
intent.putExtra("time", date.getText().toString() + " " + time.getText().toString());
Log.d(TAG, date.getText().toString() + time.getText().toString());
} else {
if (et.getText().toString().equals(old_content) && et_title.getText().toString().equals(old_title) && !timeChange) {
intent.putExtra("mode", -1); // edit nothing
}
else {
intent.putExtra("mode", 11); //edit the content
intent.putExtra("title", et_title.getText().toString());
intent.putExtra("content", et.getText().toString());
intent.putExtra("time", date.getText().toString() + " " + time.getText().toString());
intent.putExtra("id", id);
}
}
setResult(RESULT_OK, intent);
finish();//返回
overridePendingTransition(R.anim.in_lefttoright, R.anim.out_lefttoright);
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.edit_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final Intent intent = new Intent();
switch (item.getItemId()){
case R.id.delete:
new AlertDialog.Builder(EditAlarmActivity.this)
.setMessage("Delete this plan ?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(openMode == 2){
intent.putExtra("mode", -1); // delete the plan
setResult(RESULT_OK, intent);
}
else {
intent.putExtra("mode", 12); // delete the plan
intent.putExtra("id", id);
setResult(RESULT_OK, intent);
}
finish();
}
}).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void needRefresh() {
setNightMode();
startActivity(new Intent(this, EditAlarmActivity.class));
overridePendingTransition(R.anim.night_switch, R.anim.night_switch_over);
finish();
}
private void init(){
plan = new Plan();
dateArray[0] = plan.getYear();
dateArray[1] = plan.getMonth() + 1;
dateArray[2] = plan.getDay();
timeArray[0] = plan.getHour();
timeArray[1] = plan.getMinute();
et_title = findViewById(R.id.et_title);
et = findViewById(R.id.et);
set_date = findViewById(R.id.set_date);
set_time = findViewById(R.id.set_time);
date = findViewById(R.id.date);
time = findViewById(R.id.time);
//initialize two textviews
setDateTV(dateArray[0], dateArray[1], dateArray[2]);
setTimeTV((timeArray[1]>54? timeArray[0]+1 : timeArray[0]), (timeArray[1]+5)%60);
Log.d(TAG, "init: "+dateArray[1]);
set_date.setOnClickListener(this);
set_time.setOnClickListener(this);
dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
setDateTV(year, month+1, dayOfMonth);
}
};
timeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
setTimeTV(hourOfDay, minute);
}
};
}
private void setDateTV(int y, int m, int d){
//update tv and dateArray
String temp = y + "-";
if(m<10) temp += "0";
temp += (m + "-");
if(d<10) temp +="0";
temp += d;
date.setText(temp);
dateArray[0] = y;
dateArray[1] = m;
dateArray[2] = d;
}
private void setTimeTV(int h, int m){
//update tv and timeArra
String temp = "";
if(h<10) temp += "0";
temp += (h + ":");
if(m<10) temp += "0";
temp += m;
time.setText(temp);
timeArray[0] = h;
timeArray[1] = m;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.set_date: //choose day
DatePickerDialog dialog = new DatePickerDialog(EditAlarmActivity.this,
(isNightMode()?R.style.NightDialogTheme :R.style.DayDialogTheme), dateSetListener,
dateArray[0], dateArray[1] - 1, dateArray[2]);
//dialog.getWindow().setBackgroundDrawable(new ColorDrawable((isNightMode()?Color.BLACK : Color.WHITE)));
dialog.show();
break;
case R.id.set_time://choose hour and minute
TimePickerDialog dialog1 = new TimePickerDialog(EditAlarmActivity.this,
(isNightMode()?R.style.NightDialogTheme :R.style.DayDialogTheme), timeSetListener,
timeArray[0], timeArray[1], true);
//dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog1.show();
break;
}
}
private void isTimeChange(){
String newTime = date.getText().toString() + " " + time.getText().toString();
if(!newTime.equals(old_time)) timeChange = true;
}
private boolean canBeSet(){
Calendar calendar = Calendar.getInstance();
calendar.set(dateArray[0], dateArray[1] - 1, dateArray[2], timeArray[0], timeArray[1]);
Calendar cur = Calendar.getInstance();
Log.d(TAG, "canBeSet: " + cur.getTime().toString() + calendar.getTime().toString());
if(cur.before(calendar)) return true;
else {
Toast.makeText(this, "Invalid Time", Toast.LENGTH_SHORT).show();
return false;
}
}
}

@ -12,7 +12,7 @@ import java.util.List;
import cc.liuyx.note.entity.Note; import cc.liuyx.note.entity.Note;
public class CRUD { public class NoteCRUD {
SQLiteOpenHelper dbHandler; SQLiteOpenHelper dbHandler;
SQLiteDatabase db; SQLiteDatabase db;
@ -23,7 +23,7 @@ public class CRUD {
NoteDatabase.MODE NoteDatabase.MODE
}; };
public CRUD(Context context){ public NoteCRUD(Context context){
dbHandler = new NoteDatabase(context); dbHandler = new NoteDatabase(context);
} }
@ -32,7 +32,7 @@ public class CRUD {
} }
public void close(){ public void close(){
dbHandler.close(); // dbHandler.close();
} }
public void addNote(Note note){ public void addNote(Note note){

@ -1,5 +1,6 @@
package cc.liuyx.note.alarm; package cc.liuyx.note.db;
import android.annotation.SuppressLint;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
@ -8,7 +9,9 @@ import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class CRUD { import cc.liuyx.note.entity.Plan;
public class PlanCRUD {
SQLiteOpenHelper dbHandler; SQLiteOpenHelper dbHandler;
SQLiteDatabase db; SQLiteDatabase db;
@ -19,7 +22,7 @@ public class CRUD {
PlanDatabase.TIME, PlanDatabase.TIME,
}; };
public CRUD(Context context){ public PlanCRUD(Context context){
dbHandler = new PlanDatabase(context); dbHandler = new PlanDatabase(context);
} }
@ -28,10 +31,10 @@ public class CRUD {
} }
public void close(){ public void close(){
dbHandler.close(); // dbHandler.close();
} }
public Plan addPlan(Plan plan){ public void addPlan(Plan plan){
//add a plan object to database //add a plan object to database
ContentValues contentValues = new ContentValues(); ContentValues contentValues = new ContentValues();
contentValues.put(PlanDatabase.TITLE, plan.getTitle()); contentValues.put(PlanDatabase.TITLE, plan.getTitle());
@ -39,18 +42,18 @@ public class CRUD {
contentValues.put(PlanDatabase.TIME, plan.getTime()); contentValues.put(PlanDatabase.TIME, plan.getTime());
long insertId = db.insert(PlanDatabase.TABLE_NAME, null, contentValues); long insertId = db.insert(PlanDatabase.TABLE_NAME, null, contentValues);
plan.setId(insertId); plan.setId(insertId);
return plan;
} }
public Plan getPlan(long id){ public Plan getPlan(long id){
//get a plan from database using cursor index //get a plan from database using cursor index
Cursor cursor = db.query(PlanDatabase.TABLE_NAME,columns,PlanDatabase.ID + "=?", @SuppressLint("Recycle") Cursor cursor = db.query(PlanDatabase.TABLE_NAME,columns, PlanDatabase.ID + "=?",
new String[]{String.valueOf(id)},null,null, null, null); new String[]{String.valueOf(id)},null,null, null, null);
if (cursor != null) cursor.moveToFirst(); if (cursor != null) cursor.moveToFirst();
Plan e = new Plan(cursor.getString(1),cursor.getString(2), cursor.getString(3)); assert cursor != null;
return e; return new Plan(cursor.getString(1),cursor.getString(2), cursor.getString(3));
} }
@SuppressLint("Range")
public List<Plan> getAllPlans(){ public List<Plan> getAllPlans(){
Cursor cursor = db.query(PlanDatabase.TABLE_NAME,columns,null,null,null, null, null); Cursor cursor = db.query(PlanDatabase.TABLE_NAME,columns,null,null,null, null, null);

@ -1,8 +1,13 @@
package cc.liuyx.note.alarm; package cc.liuyx.note.db;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class PlanDatabase extends SQLiteOpenHelper { public class PlanDatabase extends SQLiteOpenHelper {
@ -14,13 +19,13 @@ public class PlanDatabase extends SQLiteOpenHelper {
public static final String MODE = "mode"; public static final String MODE = "mode";
public PlanDatabase(Context context){ public PlanDatabase(Context context) {
super(context, "plans", null, 1); super(context, "plans", null, 1);
} }
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+ TABLE_NAME db.execSQL("CREATE TABLE " + TABLE_NAME
+ "(" + "("
+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ TITLE + " TEXT NOT NULL," + TITLE + " TEXT NOT NULL,"

@ -1,4 +1,4 @@
package cc.liuyx.note.alarm; package cc.liuyx.note.entity;
import android.util.Log; import android.util.Log;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

@ -8,7 +8,7 @@
<corners android:radius="6dp"/> <corners android:radius="6dp"/>
</shape> </shape>
</item> </item>
<item android:bottom="3dp"> <item android:bottom="5dp">
<shape android:shape="rectangle"> <shape android:shape="rectangle">
<solid android:color="?attr/tvBackground" /> <solid android:color="?attr/tvBackground" />
<corners android:radius="6dp"/> <corners android:radius="6dp"/>
Loading…
Cancel
Save