]> iEval git - unical.git/commitdiff
Add, show and remove alarms
authorMarius Gavrilescu <marius@ieval.ro>
Wed, 21 Aug 2013 12:19:49 +0000 (15:19 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Wed, 21 Aug 2013 12:19:49 +0000 (15:19 +0300)
res/layout/add_alarm.xml
res/layout/add_event.xml
res/layout/alarm_row_layout.xml
res/layout/event_view.xml
res/values/strings.xml
src/ro/ieval/unical/AddAlarmActivity.java
src/ro/ieval/unical/Application.java
src/ro/ieval/unical/DisplayEventActivity.java
src/ro/ieval/unical/Utils.java

index ea4959f4beed64c826879fa3a6f39dc196e6a067..6ccc266b9738e94f4a2be591e1dc188b11dea561 100644 (file)
@@ -6,11 +6,11 @@
               android:layout_height="match_parent">
 
     <TimePicker
-            android:layout_width="wrap_content"
+            android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:id="@+id/timePicker" android:layout_gravity="left|center_vertical"/>
+            android:id="@+id/timePicker" android:layout_gravity="center"/>
     <DatePicker
-            android:layout_width="wrap_content"
+            android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:id="@+id/datePicker" android:layout_gravity="left|center_vertical"/>
-</LinearLayout>
\ No newline at end of file
+            android:id="@+id/datePicker" android:layout_gravity="center"/>
+</LinearLayout>
index c14561c8094408161dab3f116148449f28730ddd..45c47b84142cb651ea640e735cb4b4cd9377a3f5 100644 (file)
@@ -86,7 +86,6 @@
                 android:onClick="showDateTimePickerDialog"
                 android:text="@string/pick_date"
                 android:layout_weight="1"
-                android:ems="10"
                 android:focusable="false"
                 />
 
         </LinearLayout>
     </LinearLayout>
 </LinearLayout>
-        </ScrollView>
\ No newline at end of file
+        </ScrollView>
index b65b8f1388d449885cd561231d87d5e6c7edb716..62aa6761d9c9c325c3a48f7630193581800c0508 100644 (file)
@@ -1,20 +1,17 @@
 <?xml version="1.0" encoding="utf-8"?>
 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-              android:orientation="vertical"
+              android:orientation="horizontal"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
-    <TextView
-            android:id="@+id/smallalarmdatestart"
+  <TextView android:id="@+id/alarm_datetime"
             android:layout_width="0dip"
             android:layout_height="wrap_content"
-            android:layout_weight="1"
-            android:textAppearance="?android:attr/textAppearanceSmall" />
-    <TextView
-            android:id="@+id/smallalarmtimestart"
-            android:layout_width="0dip"
-            android:layout_height="wrap_content"
-            android:layout_weight="1"
+                       android:layout_weight="1"
             android:textAppearance="?android:attr/textAppearanceSmall" />
 
-</LinearLayout>
\ No newline at end of file
+  <Button android:id="@+id/alarm_delete"
+                 android:layout_width="wrap_content"
+                 android:layout_height="wrap_content"
+                 android:text="@string/delete"/>
+</LinearLayout>
index bcc97aa2bb7c0468fb90ccf5d3e236a81ffbb773..6fd935fb8638653bebe465b4e3e671b65678a0f9 100644 (file)
@@ -80,7 +80,9 @@
     <TextView
         android:id="@+id/event_view_description"
         android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-    />
-
-</LinearLayout>
\ No newline at end of file
+        android:layout_height="wrap_content" />
+       <ListView
+               android:id="@+id/alarm_list"
+               android:layout_width="match_parent"
+               android:layout_height="wrap_content" />
+</LinearLayout>
index 66692fae9212bcc3db22aac967639564ab6546ac..a4c44a26093eab7421fd6996ffca4c3358154f4f 100644 (file)
@@ -49,6 +49,7 @@
     <string name="stop_alarm">Stop Alarm</string>
     <string name="edit_event">Edit Event</string>
        <string name="add_alarm">Add alarm</string>
+       <string name="delete">Delete</string>
 
     <string-array name="repeat_array_s">
         <item>Day</item>
index b4833a7b192fd5a8df4335740d53509ea44a6880..c823809c3831dffdc61263de641cbdb01b5da757 100644 (file)
@@ -4,8 +4,14 @@ import android.app.ListActivity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuInflater;
+import android.view.MenuItem;
+import android.widget.DatePicker;
+import android.widget.TimePicker;
+import java.util.GregorianCalendar;
 
 public class AddAlarmActivity  extends ListActivity {
+       public static final String EXTRA_EVENT = "event";
+
     @Override
     protected void onCreate(final Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -19,4 +25,19 @@ public class AddAlarmActivity  extends ListActivity {
         inflater.inflate(R.menu.add_alarm, menu);
         return true;
     }
+
+       @Override
+       public boolean onOptionsItemSelected(final MenuItem item){
+               switch(item.getItemId()){
+               case R.id.save:
+                       final DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker);
+                       final TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);
+                       final GregorianCalendar calendar = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
+                       Utils.addAlarm((Application) getApplication(), calendar.getTimeInMillis(), getIntent().getLongExtra(EXTRA_EVENT, 0));
+                       finish();
+                       return true;
+               default:
+                       return false;
+               }
+       }
 }
index 09f8707b466a5d2a8c3d8c89c91cc3dd02b0e0e0..ea6b1546948ec1a60bb2ad32f43c4d5a0f65e373 100644 (file)
@@ -11,7 +11,8 @@ public final class Application extends android.app.Application {
                final Cursor cur = alarmOpenHelper.getReadableDatabase().query(AlarmOpenHelper.TABLE, null, null, null, null, null, null);
                if(cur.moveToFirst()) {
                        do {
-                               Utils.setAlarm(this, cur.getLong(0), cur.getLong(1));
+                               if(cur.getLong(0) > System.currentTimeMillis())
+                                       Utils.setAlarm(this, cur.getLong(0), cur.getLong(1));
                                cur.moveToNext();
                        } while(!cur.isAfterLast());
                }
index 45da8dc740312b8c563a1a409d77d0e5d8828c61..0bff03217f7a9a15003b59b921548608e960063c 100644 (file)
@@ -8,16 +8,50 @@ import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
+import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.ListView;
 import android.widget.TextView;
 
 public class DisplayEventActivity extends Activity {
+       private final class DeleteListener implements View.OnClickListener {
+               private final long time;
+               DeleteListener(final long time){
+                       this.time=time;
+               }
+
+               @Override public void onClick(final View v){
+                       Utils.deleteAlarm((Application) getApplication(), time);
+                       setAdapter();
+               }
+       }
+
        public static final String EXTRA_EVENT = "event";
        private Event event;
     private final DeleteEventDialog d=new DeleteEventDialog();
 
+       private void setAdapter(){
+               final ListView alarms = (ListView) findViewById(R.id.alarm_list);
+               alarms.setAdapter(new ArrayAdapter<Long>(this, R.layout.alarm_row_layout, Utils.getAlarmsByEvent((Application) getApplication(), event._id)){
+                               @Override public View getView(final int position, final View convertView, final ViewGroup parent) {
+                                       final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+                                       final View rowView = inflater.inflate(R.layout.alarm_row_layout, parent, false);
+                                       final TextView date = (TextView) rowView.findViewById(R.id.alarm_datetime);
+                                       final Button delete = (Button) rowView.findViewById(R.id.alarm_delete);
+                                       delete.setOnClickListener(new DeleteListener(getItem(position)));
+                                       date.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK).format(getItem(position)));
+
+                                       return rowView;
+                               }
+                       });
+       }
+
        public static void displayEvent(final Context context, final long id){
                final Intent i=new Intent(context,DisplayEventActivity.class);
                i.putExtra(DisplayEventActivity.EXTRA_EVENT, id);
@@ -37,11 +71,11 @@ public class DisplayEventActivity extends Activity {
                final TextView endDate= (TextView) findViewById(R.id.enddate);
                final TextView startTime= (TextView) findViewById(R.id.starttime);
                final TextView endTime= (TextView) findViewById(R.id.endtime);
-               final TextView description= (TextView) findViewById(R.id.event_view_description);
+               final TextView description= (TextView) findViewById(R.id.event_view_description);;
 
                final Date start=new Date(event.dtstart);
                final Date end=new Date(event.dtend);
-               final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.UK);
+               final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.UK);
                final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss", Locale.UK);
                final SimpleDateFormat dayOfWeekFormat = new SimpleDateFormat("EEEEEEE", Locale.UK);
 
@@ -54,6 +88,7 @@ public class DisplayEventActivity extends Activity {
                startTime.setText(timeFormat.format(start));
                endTime.setText(timeFormat.format(end));
                description.setText(event.description);
+               setAdapter();
        }
 
        @Override
@@ -75,17 +110,19 @@ public class DisplayEventActivity extends Activity {
 
                        return true;
                case R.id.eventlist:
-                       final Intent it=new Intent(this,EventListActivity.class);
+                       final Intent it=new Intent(this, EventListActivity.class);
                        startActivity(it);
                        return true;
                case R.id.edit_event:
-                       AddEventActivity.editEvent(DisplayEventActivity.this,event);
+                       AddEventActivity.editEvent(this, event);
                        return true;
                case R.id.add_alarm:
-            startActivity(new Intent(this,AddAlarmActivity.class));
+                       final Intent intent = new Intent(this, AddAlarmActivity.class);
+                       intent.putExtra(AddAlarmActivity.EXTRA_EVENT, event._id);
+            startActivity(intent);
                        return true;
                default:
-                       return true;
+                       return false;
                }
        }
 }
index 895c9464d30069dc8150934c5436b30f7f2d1d3d..3b53b479a90cce7964136bcbaeb759055f1a6f5c 100644 (file)
@@ -45,18 +45,19 @@ final class Utils {
                unsetAlarm(application, time);
        }
 
-       public static long[] getAlarmsByEvent(final Application application, final long eventId) throws SQLException{
+       public static Long[] getAlarmsByEvent(final Application application, final long eventId) throws SQLException{
                final SQLiteDatabase db = application.alarmOpenHelper.getWritableDatabase();
+               db.delete(AlarmOpenHelper.TABLE, AlarmOpenHelper.TIME + " < " + System.currentTimeMillis(), new String[0]);
                final Cursor cursor = db.query(AlarmOpenHelper.TABLE, new String[]{AlarmOpenHelper.TIME}, AlarmOpenHelper.EVENT + " = " + eventId, new String[0], null, null, null);
                if(cursor.moveToFirst()){
-                       final long[] ret = new long[cursor.getCount()];
+                       final Long[] ret = new Long[cursor.getCount()];
                        for(int i=0;i<ret.length;i++){
-                               ret[i]=cursor.getLong(i);
+                               ret[i]=cursor.getLong(0);
                                cursor.moveToNext();
                        }
                        return ret;
                }
-               return new long[0];
+               return new Long[0];
        }
 
        public static void setAlarm(final Context context, final long time, final long eventId){
This page took 0.031868 seconds and 4 git commands to generate.