1
+ package com .github .meafs .recover .activites ;
2
+
3
+
4
+ import androidx .appcompat .app .AppCompatActivity ;
5
+
6
+ import android .app .AlarmManager ;
7
+ import android .app .DatePickerDialog ;
8
+ import android .app .PendingIntent ;
9
+ import android .app .TimePickerDialog ;
10
+ import android .content .Context ;
11
+ import android .content .Intent ;
12
+ import android .os .Bundle ;
13
+ import android .view .View ;
14
+
15
+ import android .widget .ArrayAdapter ;
16
+ import android .widget .AutoCompleteTextView ;
17
+ import android .widget .Button ;
18
+ import android .widget .DatePicker ;
19
+ import android .widget .EditText ;
20
+ import android .widget .TimePicker ;
21
+ import android .widget .Toast ;
22
+
23
+ import com .github .meafs .recover .R ;
24
+ import com .github .meafs .recover .utils .AlarmBroadcast ;
25
+ import com .github .meafs .recover .utils .dbManager ;
26
+
27
+ import java .text .DateFormat ;
28
+ import java .text .ParseException ;
29
+ import java .text .SimpleDateFormat ;
30
+ import java .util .ArrayList ;
31
+ import java .util .Calendar ;
32
+ import java .util .Date ;
33
+
34
+ public class ReminderActivity extends AppCompatActivity {
35
+ Button mSubmitbtn , mDatebtn , mTimebtn , mPname ;
36
+ EditText mTitledit ;
37
+ String timeTonotify ;
38
+ private ArrayList <String > names = new ArrayList <>();
39
+ private String patientName ;
40
+
41
+ @ Override
42
+ protected void onCreate (Bundle savedInstanceState ) {
43
+ super .onCreate (savedInstanceState );
44
+ setContentView (R .layout .activity_reminder );
45
+ mTitledit = findViewById (R .id .editTitle );
46
+ mDatebtn = findViewById (R .id .btnDate ); //assigned all the material reference to get and set data
47
+ mTimebtn = findViewById (R .id .btnTime );
48
+ mSubmitbtn = findViewById (R .id .btnSubmit );
49
+
50
+ Bundle mBundle = getIntent ().getExtras ();
51
+ if (mBundle != null ) {
52
+ names = mBundle .getStringArrayList ("namesList" );
53
+ }
54
+
55
+ ArrayAdapter <String > adapter = new ArrayAdapter <>(ReminderActivity .this
56
+ , R .layout .dropdown_menu_item , names );
57
+
58
+ AutoCompleteTextView autoCompleteTextView = findViewById (R .id .patient_dropdown );
59
+ autoCompleteTextView .setAdapter (adapter );
60
+
61
+ autoCompleteTextView .setOnItemClickListener ((adapterView , view , i , l ) -> {
62
+ patientName = (String ) adapterView .getItemAtPosition (i );
63
+ });
64
+
65
+ mTimebtn .setOnClickListener (new View .OnClickListener () {
66
+ @ Override
67
+ public void onClick (View view ) {
68
+ selectTime (); //when we click on the choose time button it calls the select time method
69
+ }
70
+ });
71
+
72
+ mDatebtn .setOnClickListener (new View .OnClickListener () {
73
+ @ Override
74
+ public void onClick (View view ) {
75
+ selectDate ();
76
+ } //when we click on the choose date button it calls the select date method
77
+ });
78
+
79
+ mSubmitbtn .setOnClickListener (new View .OnClickListener () {
80
+ @ Override
81
+ public void onClick (View view ) {
82
+ String title = mTitledit .getText ().toString ().trim (); //access the data from the input field
83
+ String date = mDatebtn .getText ().toString ().trim (); //access the date from the choose date button
84
+ String time = mTimebtn .getText ().toString ().trim (); //access the time from the choose time button
85
+ String pname = patientName ;
86
+ if (pname != null ) {
87
+
88
+ if (title .isEmpty ()) {
89
+ Toast .makeText (getApplicationContext (), "Please Enter text" , Toast .LENGTH_SHORT ).show (); //shows the toast if input field is empty
90
+ } else {
91
+ if (time .equals ("time" ) || date .equals ("date" )) { //shows toast if date and time are not selected
92
+ Toast .makeText (getApplicationContext (), "Please select date and time" , Toast .LENGTH_SHORT ).show ();
93
+ } else {
94
+ processinsert (title , date , time , pname );
95
+ }
96
+ }
97
+ } else {
98
+ Toast .makeText (getApplicationContext (), "Please select Patient" , Toast .LENGTH_SHORT ).show ();
99
+ }
100
+ }
101
+ });
102
+ }
103
+
104
+ private void processinsert (String title , String date , String time , String pname ) {
105
+ String result = new dbManager (this ).addreminder (title , date , time , pname ); //inserts the title,date,time into sql lite database
106
+ setAlarm (title , date , time ); //calls the set alarm method to set alarm
107
+ mTitledit .setText ("" );
108
+ Toast .makeText (getApplicationContext (), result , Toast .LENGTH_SHORT ).show ();
109
+ }
110
+
111
+ private void selectTime () { //this method performs the time picker task
112
+ Calendar calendar = Calendar .getInstance ();
113
+ int hour = calendar .get (Calendar .HOUR_OF_DAY );
114
+ int minute = calendar .get (Calendar .MINUTE );
115
+ TimePickerDialog timePickerDialog = new TimePickerDialog (this , new TimePickerDialog .OnTimeSetListener () {
116
+ @ Override
117
+ public void onTimeSet (TimePicker timePicker , int i , int i1 ) {
118
+ timeTonotify = i + ":" + i1 ; //temp variable to store the time to set alarm
119
+ mTimebtn .setText (FormatTime (i , i1 )); //sets the button text as selected time
120
+ }
121
+ }, hour , minute , false );
122
+ timePickerDialog .show ();
123
+ }
124
+
125
+ private void selectDate () { //this method performs the date picker task
126
+ Calendar calendar = Calendar .getInstance ();
127
+ int year = calendar .get (Calendar .YEAR );
128
+ int month = calendar .get (Calendar .MONTH );
129
+ int day = calendar .get (Calendar .DAY_OF_MONTH );
130
+ DatePickerDialog datePickerDialog = new DatePickerDialog (this , new DatePickerDialog .OnDateSetListener () {
131
+ @ Override
132
+ public void onDateSet (DatePicker datePicker , int year , int month , int day ) {
133
+ mDatebtn .setText (day + "-" + (month + 1 ) + "-" + year ); //sets the selected date as test for button
134
+ }
135
+ }, year , month , day );
136
+ datePickerDialog .show ();
137
+ }
138
+
139
+ public String FormatTime (int hour , int minute ) { //this method converts the time into 12hr format and assigns am or pm
140
+ String time ;
141
+ time = "" ;
142
+ String formattedMinute ;
143
+ if (minute / 10 == 0 ) {
144
+ formattedMinute = "0" + minute ;
145
+ } else {
146
+ formattedMinute = "" + minute ;
147
+ }
148
+ if (hour == 0 ) {
149
+ time = "12" + ":" + formattedMinute + " AM" ;
150
+ } else if (hour < 12 ) {
151
+ time = hour + ":" + formattedMinute + " AM" ;
152
+ } else if (hour == 12 ) {
153
+ time = "12" + ":" + formattedMinute + " PM" ;
154
+ } else {
155
+ int temp = hour - 12 ;
156
+ time = temp + ":" + formattedMinute + " PM" ;
157
+ }
158
+ return time ;
159
+ }
160
+
161
+ private void setAlarm (String text , String date , String time ) {
162
+ AlarmManager am = (AlarmManager ) getSystemService (Context .ALARM_SERVICE ); //assigning alarm manager object to set alarm
163
+ Intent intent = new Intent (getApplicationContext (), AlarmBroadcast .class );
164
+ intent .putExtra ("event" , text ); //sending data to alarm class to create channel and notification
165
+ intent .putExtra ("time" , date );
166
+ intent .putExtra ("date" , time );
167
+ PendingIntent pendingIntent = PendingIntent .getBroadcast (getApplicationContext (), 0 , intent , PendingIntent .FLAG_ONE_SHOT );
168
+ String dateandtime = date + " " + timeTonotify ;
169
+ DateFormat formatter = new SimpleDateFormat ("d-M-yyyy hh:mm" );
170
+ try {
171
+ Date date1 = formatter .parse (dateandtime );
172
+ am .set (AlarmManager .RTC_WAKEUP , date1 .getTime (), pendingIntent );
173
+ Toast .makeText (getApplicationContext (), "Alarm" , Toast .LENGTH_SHORT ).show ();
174
+ } catch (ParseException e ) {
175
+ e .printStackTrace ();
176
+ }
177
+ Intent intentBack = new Intent (getApplicationContext (), MainActivity .class ); //this intent will be called once the setting alarm is complete
178
+ intentBack .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_CLEAR_TASK );
179
+ startActivity (intentBack ); //navigates from adding reminder activity to mainactivity
180
+ }
181
+ }
0 commit comments