From fbf4c62865c34d43cbcc198ea72ff9f77c9b6d97 Mon Sep 17 00:00:00 2001 From: RohanBh Date: Sat, 19 Aug 2017 15:08:24 +0000 Subject: [PATCH 01/16] Modify build.gradle --- app/build.gradle | 9 +++++---- build.gradle | 3 +++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d36ed4e..e818118 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -24,10 +24,11 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:26.+' + compile 'com.android.support:appcompat-v7:26.0.1' + compile 'com.android.support:design:26.0.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' - compile 'com.google.android.gms:play-services-places:11.0.2' - compile 'com.google.android.gms:play-services-location:11.0.2' - compile 'com.android.support:recyclerview-v7:26.+' + compile 'com.google.android.gms:play-services-places:11.0.4' + compile 'com.google.android.gms:play-services-location:11.0.4' + compile 'com.android.support:recyclerview-v7:26.0.1' testCompile 'junit:junit:4.12' } diff --git a/build.gradle b/build.gradle index c2eea8e..428e343 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,9 @@ buildscript { allprojects { repositories { jcenter() + maven { + url "https://maven.google.com" + } } } From 9350e018a40d7f608bb02edbc07b80e07c2f7c1d Mon Sep 17 00:00:00 2001 From: RohanBh Date: Sat, 19 Aug 2017 15:10:59 +0000 Subject: [PATCH 02/16] Complete Time list adapter and create time picker dialog fragment --- .../shush/adapters/TimeListAdapter.java | 213 ++++++++++++++++++ .../fragments/TimePickerDialogFragment.java | 49 ++++ app/src/main/res/layout/time_picker_row.xml | 121 ++++++++++ 3 files changed, 383 insertions(+) create mode 100644 app/src/main/java/com/mdg/droiders/samagra/shush/adapters/TimeListAdapter.java create mode 100644 app/src/main/java/com/mdg/droiders/samagra/shush/fragments/TimePickerDialogFragment.java create mode 100644 app/src/main/res/layout/time_picker_row.xml diff --git a/app/src/main/java/com/mdg/droiders/samagra/shush/adapters/TimeListAdapter.java b/app/src/main/java/com/mdg/droiders/samagra/shush/adapters/TimeListAdapter.java new file mode 100644 index 0000000..47d8e3f --- /dev/null +++ b/app/src/main/java/com/mdg/droiders/samagra/shush/adapters/TimeListAdapter.java @@ -0,0 +1,213 @@ +package com.mdg.droiders.samagra.shush.adapters; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.Switch; +import android.widget.TextView; + +import com.mdg.droiders.samagra.shush.R; +import com.mdg.droiders.samagra.shush.data.PlacesContract; +import com.mdg.droiders.samagra.shush.fragments.TimePickerDialogFragment; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Locale; + +/** + * Created by rohan on 19/8/17. + * Adapter that binds data to the view for time entries in the Shush app. + */ +public class TimeListAdapter extends RecyclerView.Adapter { + + private static final String DIALOG_TIME_TAG = "time_picker_dialog_tag"; + private static final SimpleDateFormat DISPLAY_DATE_FORMAT = + new SimpleDateFormat("hh:mm a", Locale.ENGLISH); + + private Context mContext; + private Cursor timeDataCursor; + + public TimeListAdapter(Context mContext, Cursor timeDataCursor) { + this.mContext = mContext; + this.timeDataCursor = timeDataCursor; + /*=mContext.getContentResolver().query( + PlacesContract.TimeEntry.CONTENT_URI, + null, + null, + null, + null);*/ + } + + @Override + public TimeRowHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View row = LayoutInflater.from(mContext) + .inflate(R.layout.time_picker_row, parent, false); + return new TimeRowHolder(row); + } + + @Override + public void onBindViewHolder(TimeRowHolder holder, int position) { + if (timeDataCursor.getCount() == 0) { + return; + } + if (position == timeDataCursor.getCount()) { + holder.startTime.setVisibility(View.INVISIBLE); + holder.endTime.setVisibility(View.INVISIBLE); + holder.monday.setVisibility(View.INVISIBLE); + holder.tuesday.setVisibility(View.INVISIBLE); + holder.wednesday.setVisibility(View.INVISIBLE); + holder.thursday.setVisibility(View.INVISIBLE); + holder.friday.setVisibility(View.INVISIBLE); + holder.saturday.setVisibility(View.INVISIBLE); + holder.sunday.setVisibility(View.INVISIBLE); + return; + } + if (timeDataCursor.moveToPosition(position)) { + String startTime = timeDataCursor.getString( + timeDataCursor.getColumnIndexOrThrow(PlacesContract.TimeEntry.COLUMN_START_TIME) + ); + String endTime = timeDataCursor.getString( + timeDataCursor.getColumnIndexOrThrow(PlacesContract.TimeEntry.COLUMN_END_TIME) + ); + int monday = timeDataCursor.getInt( + timeDataCursor.getColumnIndexOrThrow(PlacesContract.TimeEntry.COLUMN_MONDAY) + ); + int tuesday = timeDataCursor.getInt( + timeDataCursor.getColumnIndexOrThrow(PlacesContract.TimeEntry.COLUMN_TUESDAY) + ); + int wednesday = timeDataCursor.getInt( + timeDataCursor.getColumnIndexOrThrow(PlacesContract.TimeEntry.COLUMN_WEDNESDAY) + ); + int thursday = timeDataCursor.getInt( + timeDataCursor.getColumnIndexOrThrow(PlacesContract.TimeEntry.COLUMN_THURSDAY) + ); + int friday = timeDataCursor.getInt( + timeDataCursor.getColumnIndexOrThrow(PlacesContract.TimeEntry.COLUMN_FRIDAY) + ); + int saturday = timeDataCursor.getInt( + timeDataCursor.getColumnIndexOrThrow(PlacesContract.TimeEntry.COLUMN_SATURDAY) + ); + int sunday = timeDataCursor.getInt( + timeDataCursor.getColumnIndexOrThrow(PlacesContract.TimeEntry.COLUMN_SUNDAY) + ); + holder.id = timeDataCursor.getColumnIndexOrThrow(PlacesContract.TimeEntry._ID); + holder.startTime.setText(startTime); + holder.endTime.setText(endTime); + holder.monday.setChecked(monday == 1); + holder.tuesday.setChecked(tuesday == 1); + holder.wednesday.setChecked(wednesday == 1); + holder.thursday.setChecked(thursday == 1); + holder.friday.setChecked(friday == 1); + holder.saturday.setChecked(saturday == 1); + holder.sunday.setChecked(sunday == 1); + } + + } + + @Override + public int getItemCount() { + return timeDataCursor.getCount() + 1; + } + + private boolean refreshData(TimeRowHolder holder) { + + ContentValues values = new ContentValues(); + values.put(PlacesContract.TimeEntry.COLUMN_START_TIME, + holder.startTime.getText().toString()); + values.put(PlacesContract.TimeEntry.COLUMN_END_TIME, + holder.endTime.getText().toString()); + values.put(PlacesContract.TimeEntry.COLUMN_MONDAY, + holder.monday.isChecked() ? 1 : 0); + values.put(PlacesContract.TimeEntry.COLUMN_TUESDAY, + holder.tuesday.isChecked() ? 1 : 0); + values.put(PlacesContract.TimeEntry.COLUMN_WEDNESDAY, + holder.wednesday.isChecked() ? 1 : 0); + values.put(PlacesContract.TimeEntry.COLUMN_THURSDAY, + holder.thursday.isChecked() ? 1 : 0); + values.put(PlacesContract.TimeEntry.COLUMN_FRIDAY, + holder.friday.isChecked() ? 1 : 0); + values.put(PlacesContract.TimeEntry.COLUMN_SATURDAY, + holder.saturday.isChecked() ? 1 : 0); + values.put(PlacesContract.TimeEntry.COLUMN_SUNDAY, + holder.sunday.isChecked() ? 1 : 0); + + String selection = PlacesContract.TimeEntry._ID + " = ?"; + String[] selectionArgs = {String.valueOf(holder.id)}; + int affectedRows = mContext.getContentResolver().update( + PlacesContract.TimeEntry.CONTENT_URI, values, selection, selectionArgs + ); + return affectedRows == 1; + } + + class TimeRowHolder extends RecyclerView.ViewHolder { + + public int id; + TextView startTime; + TextView endTime; + Switch enableSwitch; + CheckBox monday; + CheckBox tuesday; + CheckBox wednesday; + CheckBox thursday; + CheckBox friday; + CheckBox saturday; + CheckBox sunday; + + TimeRowHolder(View itemView) { + super(itemView); + id = -1; + startTime = itemView.findViewById(R.id.start_time); + endTime = itemView.findViewById(R.id.end_time); + enableSwitch = itemView.findViewById(R.id.enable_switch); + monday = itemView.findViewById(R.id.monday); + tuesday = itemView.findViewById(R.id.tuesday); + wednesday = itemView.findViewById(R.id.wednesday); + thursday = itemView.findViewById(R.id.thursday); + friday = itemView.findViewById(R.id.friday); + saturday = itemView.findViewById(R.id.saturday); + sunday = itemView.findViewById(R.id.sunday); + View.OnClickListener timeChangeListener = new View.OnClickListener() { + @Override + public void onClick(final View view) { + TimePickerDialogFragment timePickerDialog = new TimePickerDialogFragment(); + timePickerDialog.setTimeSetCallback(new TimePickerDialogFragment.TimeSetCallback() { + @Override + public void onTimeSet(int hour, int minute) { + Calendar calendar = Calendar.getInstance(); + calendar.set(Calendar.HOUR_OF_DAY, hour); + calendar.set(Calendar.MINUTE, minute); + ((TextView) view).setText(DISPLAY_DATE_FORMAT.format(calendar.getTime())); + refreshData(TimeRowHolder.this); + } + }); + timePickerDialog.show( + ((AppCompatActivity) mContext).getSupportFragmentManager(), + DIALOG_TIME_TAG); + } + }; + CompoundButton.OnCheckedChangeListener dayCheckChangeListener + = new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean b) { + refreshData(TimeRowHolder.this); + } + }; + startTime.setOnClickListener(timeChangeListener); + endTime.setOnClickListener(timeChangeListener); + monday.setOnCheckedChangeListener(dayCheckChangeListener); + tuesday.setOnCheckedChangeListener(dayCheckChangeListener); + wednesday.setOnCheckedChangeListener(dayCheckChangeListener); + thursday.setOnCheckedChangeListener(dayCheckChangeListener); + friday.setOnCheckedChangeListener(dayCheckChangeListener); + saturday.setOnCheckedChangeListener(dayCheckChangeListener); + sunday.setOnCheckedChangeListener(dayCheckChangeListener); + } + } +} diff --git a/app/src/main/java/com/mdg/droiders/samagra/shush/fragments/TimePickerDialogFragment.java b/app/src/main/java/com/mdg/droiders/samagra/shush/fragments/TimePickerDialogFragment.java new file mode 100644 index 0000000..b43cb3c --- /dev/null +++ b/app/src/main/java/com/mdg/droiders/samagra/shush/fragments/TimePickerDialogFragment.java @@ -0,0 +1,49 @@ +package com.mdg.droiders.samagra.shush.fragments; + +import android.app.Dialog; + +import android.app.TimePickerDialog; +import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.text.format.DateFormat; +import android.widget.TimePicker; + +import java.util.Calendar; + +/** + * Created by rohan on 19/8/17. + * A fragment that displays a time picker dialog window, + * floating on top of its activity's window. + */ +public class TimePickerDialogFragment extends DialogFragment + implements TimePickerDialog.OnTimeSetListener { + + public interface TimeSetCallback { + void onTimeSet(int hour, int minute); + } + + TimeSetCallback mCallback; + + @Override + + public Dialog onCreateDialog(Bundle savedInstanceState) { + + Calendar rightNow = Calendar.getInstance(); + + return new TimePickerDialog(getActivity(), this, + rightNow.get(Calendar.HOUR_OF_DAY), + rightNow.get(Calendar.MINUTE), + DateFormat.is24HourFormat(getActivity())); + } + + @Override + public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) { + if (mCallback != null) { + mCallback.onTimeSet(hourOfDay, minute); + } + } + + public void setTimeSetCallback(TimeSetCallback mCallback) { + this.mCallback = mCallback; + } +} diff --git a/app/src/main/res/layout/time_picker_row.xml b/app/src/main/res/layout/time_picker_row.xml new file mode 100644 index 0000000..d443500 --- /dev/null +++ b/app/src/main/res/layout/time_picker_row.xml @@ -0,0 +1,121 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From a9c7f0b179001a5f9c85ea3edc9cf2943d44526e Mon Sep 17 00:00:00 2001 From: RohanBh Date: Sun, 20 Aug 2017 01:18:25 +0000 Subject: [PATCH 03/16] Add new methods to AlarmScheduler --- .../samagra/shush/AlarmScheduler.java | 158 ++++++++++++++++-- 1 file changed, 141 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/mdg/droiders/samagra/shush/AlarmScheduler.java b/app/src/main/java/com/mdg/droiders/samagra/shush/AlarmScheduler.java index 134b771..5c8f8ec 100644 --- a/app/src/main/java/com/mdg/droiders/samagra/shush/AlarmScheduler.java +++ b/app/src/main/java/com/mdg/droiders/samagra/shush/AlarmScheduler.java @@ -4,8 +4,13 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.util.Log; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; import java.util.List; +import java.util.Locale; /** * Created by rohan on 12/8/17. @@ -14,12 +19,8 @@ */ public class AlarmScheduler { - - /** - * Request code for scheduling an alarm - * Will be taken from the database - */ - private static final int REQUEST_CODE = 12; + private static final int ONE_WEEK_IN_MILLIS = 7 * 24 * 60 * 60 * 1000; + private static final String LOG_TAG = "Samagra/AS/"; private AlarmManager alarmManager; private Context context; @@ -29,53 +30,176 @@ public AlarmScheduler(Context context) { this.context = context; } + /** + * Sets a weekly alarm to silence your phone. + *

+ * Uses : {@link #setAlarm(long, long, Integer)} to set each alarm one by one. + * + * @param startTime The time of day at which phone is to be shushed + * @param endTime The time of day at which phone is to be un-shushed + * @param days A boolean array that is true if the alarm is to be + * scheduled for the corresponding day. i.e. days[0] is set to be true + * then a weekly alarm will be set such that it will shush the phone every Monday. + * @param rowID Unique primary key of the shush alarm row. + */ + public void setWeeklyAlarm(Calendar startTime, Calendar endTime, boolean[] days, Integer rowID) { + if (rowID == null || days.length != 7) { + return; + } + Calendar rightNow = Calendar.getInstance(); + + startTime.set(Calendar.YEAR, rightNow.get(Calendar.YEAR)); + startTime.set(Calendar.MONTH, rightNow.get(Calendar.MONTH)); + startTime.set(Calendar.DAY_OF_MONTH, rightNow.get(Calendar.DAY_OF_MONTH)); + endTime.set(Calendar.YEAR, rightNow.get(Calendar.YEAR)); + endTime.set(Calendar.MONTH, rightNow.get(Calendar.MONTH)); + endTime.set(Calendar.DAY_OF_MONTH, rightNow.get(Calendar.DAY_OF_MONTH)); + SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a, EE ,dd MMM yyyy", Locale.ENGLISH); + Log.d(LOG_TAG + "now", sdf.format(rightNow.getTime())); + + if (startTime.get(Calendar.HOUR_OF_DAY) + > endTime.get(Calendar.HOUR_OF_DAY)) { + endTime.add(Calendar.DAY_OF_MONTH, 1); + } + int i = getDay(startTime); + for (int j = i; j < 7; j++) { + if (j != i) { + startTime.add(Calendar.DAY_OF_MONTH, 1); + endTime.add(Calendar.DAY_OF_MONTH, 1); + } + Log.d(LOG_TAG + "I-Val", String.valueOf(j)); + if (days[j]) { + int dayID = getDayID(j, rowID); + setAlarm(startTime.getTimeInMillis(), + endTime.getTimeInMillis(), + dayID); + } + } + for (int j = 0; j < i; j++) { + startTime.add(Calendar.DAY_OF_MONTH, 1); + endTime.add(Calendar.DAY_OF_MONTH, 1); + Log.d(LOG_TAG + "I-Val", String.valueOf(j)); + if (days[j]) { + int dayID = getDayID(j, rowID); + setAlarm(startTime.getTimeInMillis(), + endTime.getTimeInMillis(), + dayID); + } + } + } + + /** + * Sets a weekly alarm to silence your phone. + *

+ * Uses : {@link #setWeeklyAlarm(Calendar, Calendar, boolean[], Integer)} to set weekly alarms. + * + * @param startHour The hour at which phone is to be shushed in 24 hour format + * @param startMinutes The minute at which phone is to be shushed + * @param endHour The hour at which phone is to be un-shushed in 24 hour format + * @param endMinutes The minute at which phone is to be un-shushed + * @param days A boolean array that is true if the alarm is to be + * scheduled for the corresponding day. i.e. days[0] is set to be true + * then a weekly alarm will be set such that it will shush the phone every Monday. + * @param rowID Unique primary key of the shush alarm row. + */ + public void setWeeklyAlarm(int startHour, int startMinutes, int endHour, + int endMinutes, boolean[] days, Integer rowID) { + if (rowID == null || days.length != 7) { + return; + } + Calendar startTime = Calendar.getInstance(); + Calendar endTime = Calendar.getInstance(); + startTime.set(Calendar.HOUR_OF_DAY, startHour); + startTime.set(Calendar.MINUTE, startMinutes); + endTime.set(Calendar.HOUR_OF_DAY, endHour); + endTime.set(Calendar.MINUTE, endMinutes); + setWeeklyAlarm(startTime, endTime, days, rowID); + } + /** * Set an alarm to silence the phone. * * @param startTimeInMillis The time at which phone is to be shushed * @param endTimeInMillis The time at which phone is to be un-shushed */ - public void setAlarm(long startTimeInMillis, long endTimeInMillis) { + public void setAlarm(long startTimeInMillis, long endTimeInMillis, Integer alarmId) { if (startTimeInMillis >= endTimeInMillis) { return; } - alarmManager.set( + if (alarmId == null) { + return; + } + SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a, EE ,dd MMM yyyy", Locale.ENGLISH); + Log.d(LOG_TAG + "start", sdf.format(new Date(startTimeInMillis))); + Log.d(LOG_TAG + "end", sdf.format(new Date(endTimeInMillis))); + alarmManager.setRepeating( AlarmManager.RTC_WAKEUP, startTimeInMillis, - getDefaultPendingIntent(true) + ONE_WEEK_IN_MILLIS, + getDefaultPendingIntent(true, alarmId) ); - alarmManager.set( + alarmManager.setRepeating( AlarmManager.RTC_WAKEUP, endTimeInMillis, - getDefaultPendingIntent(false) + ONE_WEEK_IN_MILLIS, + getDefaultPendingIntent(false, alarmId) ); } /** * Sets multiple alarms at once. *

- * Uses : {@link #setAlarm(long, long)} to set each alarm one by one. + * Uses : {@link #setAlarm(long, long, Integer)} to set each alarm one by one. * * @param startTimesInMillis The list of start times at which phone is to be shushed * @param endTimesInMillis The list of end times at which phone is to be un-shushed */ - public void setAlarms(List startTimesInMillis, List endTimesInMillis) { + public void setAlarms(List startTimesInMillis, List endTimesInMillis, List alarmIds) { if (startTimesInMillis.size() != endTimesInMillis.size()) { return; } for (int i = 0; i < startTimesInMillis.size(); i++) { - setAlarm(startTimesInMillis.get(i), endTimesInMillis.get(i)); + setAlarm(startTimesInMillis.get(i), endTimesInMillis.get(i), alarmIds.get(i)); } } - private PendingIntent getDefaultPendingIntent(boolean shouldSilence) { + private PendingIntent getDefaultPendingIntent(boolean shouldShush, int alarmId) { PendingIntent pendingIntent; Intent intent = new Intent(context, AlarmBroadcastReceiver.class); - intent.putExtra(context.getString(R.string.alarm_intent_extra_key), shouldSilence); + intent.putExtra(context.getString(R.string.alarm_intent_extra_key), shouldShush); pendingIntent = PendingIntent.getBroadcast( - context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); + context, alarmId, intent, PendingIntent.FLAG_UPDATE_CURRENT); return pendingIntent; } + + /** + * Get day ID for alarms that are scheduled by alarm manager. + * The ID returned identifies a particular day of a particular shush alarm. + * + * @param day int type for week days starting from monday and zero indexed. + * For e.g. - Monday is 0, Tuesday is 1, ... and so on + * @param rowID Unique primary key of the shush alarm row. + * @return The ID used by {@link AlarmManager} to schedule weekly alarms + */ + private int getDayID(int day, int rowID) { + return rowID + (day + 1) * 10000; + } + + /** + * Get day field of calendar indexed at 0 such that start day of week is monday. + * + * @param calendar The calendar whose day is to be returned + * @return int type for week days starting from monday and zero indexed. + * For e.g. - Monday is 0, Tuesday is 1, ... and so on + */ + private int getDay(Calendar calendar) { + int i = calendar.get(Calendar.DAY_OF_WEEK); + i -= 2; + if (i == -1) { + i += 7; + } + return i; + } } From cfcfe6511db7efd15544091305098d83c8f16f7a Mon Sep 17 00:00:00 2001 From: RohanBh Date: Sun, 20 Aug 2017 01:20:22 +0000 Subject: [PATCH 04/16] Correct wrong column name of primary key in query and update methods --- .../samagra/shush/data/PlacesContentProvider.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/mdg/droiders/samagra/shush/data/PlacesContentProvider.java b/app/src/main/java/com/mdg/droiders/samagra/shush/data/PlacesContentProvider.java index 4a8cb37..54bb908 100644 --- a/app/src/main/java/com/mdg/droiders/samagra/shush/data/PlacesContentProvider.java +++ b/app/src/main/java/com/mdg/droiders/samagra/shush/data/PlacesContentProvider.java @@ -102,7 +102,7 @@ public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable S String id = uri.getPathSegments().get(1); retCursor = db.query(PlacesContract.TimeEntry.TABLE_NAME, projection, - "id =?", + "_id =?", new String[]{id}, null, null, @@ -203,14 +203,14 @@ public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strin // Get the place ID from the URI path String id = uri.getPathSegments().get(1); // Use selections/selectionArgs to filter for this ID - deletedRows = db.delete(PlacesContract.PlaceEntry.TABLE_NAME,"id=?",new String[]{id}); + deletedRows = db.delete(PlacesContract.PlaceEntry.TABLE_NAME,"_id=?",new String[]{id}); break; // Handle the single item case, recognized by the ID included in the URI path case SINGLE_TIME_WITH_ID: // Get the place ID from the URI path String timeId = uri.getPathSegments().get(1); // Use selections/selectionArgs to filter for this ID - deletedRows = db.delete(PlacesContract.TimeEntry.TABLE_NAME,"id=?", new String[]{timeId}); + deletedRows = db.delete(PlacesContract.TimeEntry.TABLE_NAME,"_id=?", new String[]{timeId}); break; default: throw new UnsupportedOperationException("Invalid uri: "+ uri); @@ -254,7 +254,7 @@ public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Null // Get the place ID from the URI path id = uri.getPathSegments().get(1); // Use selections/selectionArgs to filter for this ID - affectedRows = db.update(PlacesContract.TimeEntry.TABLE_NAME,contentValues,"id=?", + affectedRows = db.update(PlacesContract.TimeEntry.TABLE_NAME,contentValues,"_id=?", new String[]{id}); break; // Default exception From ff2d145b94e501f0e4f91a985ead980c364c3cd4 Mon Sep 17 00:00:00 2001 From: RohanBh Date: Sun, 20 Aug 2017 01:24:14 +0000 Subject: [PATCH 05/16] Add time list activity and update adapter code --- app/src/main/AndroidManifest.xml | 11 +- .../droiders/samagra/shush/MainActivity.java | 10 ++ .../samagra/shush/TimeListActivity.java | 51 +++++++++ .../shush/adapters/TimeListAdapter.java | 105 +++++++++++++----- app/src/main/res/drawable/ic_plus.xml | 9 ++ app/src/main/res/layout/activity_main.xml | 10 ++ .../main/res/layout/activity_time_list.xml | 30 +++++ app/src/main/res/layout/time_picker_row.xml | 21 ++-- app/src/main/res/values/strings.xml | 1 + 9 files changed, 204 insertions(+), 44 deletions(-) create mode 100644 app/src/main/java/com/mdg/droiders/samagra/shush/TimeListActivity.java create mode 100644 app/src/main/res/drawable/ic_plus.xml create mode 100644 app/src/main/res/layout/activity_time_list.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index df88dfc..acae1f3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,10 +13,9 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - + android:value="@string/your_google_places_api_key" /> @@ -34,9 +33,11 @@ + android:exported="true" /> + - + - + \ No newline at end of file diff --git a/app/src/main/java/com/mdg/droiders/samagra/shush/MainActivity.java b/app/src/main/java/com/mdg/droiders/samagra/shush/MainActivity.java index efebcd5..1ef0a4d 100644 --- a/app/src/main/java/com/mdg/droiders/samagra/shush/MainActivity.java +++ b/app/src/main/java/com/mdg/droiders/samagra/shush/MainActivity.java @@ -58,6 +58,7 @@ public class MainActivity extends AppCompatActivity implements private PlaceListAdapter mAdapter; private RecyclerView mRecyclerView; private Button addPlaceButton; + private Button addAlarmButton; private GoogleApiClient mClient; private Geofencing mGeofencing; private boolean mIsEnabled; @@ -110,6 +111,15 @@ public void onClick(View view) { } }); + addAlarmButton = findViewById(R.id.add_alarm_button); + + addAlarmButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + startActivity(new Intent(MainActivity.this, TimeListActivity.class)); + } + }); + mClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) diff --git a/app/src/main/java/com/mdg/droiders/samagra/shush/TimeListActivity.java b/app/src/main/java/com/mdg/droiders/samagra/shush/TimeListActivity.java new file mode 100644 index 0000000..5afb26f --- /dev/null +++ b/app/src/main/java/com/mdg/droiders/samagra/shush/TimeListActivity.java @@ -0,0 +1,51 @@ +package com.mdg.droiders.samagra.shush; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.View; + +import com.mdg.droiders.samagra.shush.adapters.TimeListAdapter; + +public class TimeListActivity extends AppCompatActivity { + + private RecyclerView timeRecycler; + private TimeListAdapter timeListAdapter; + private boolean isActivityCreated; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_time_list); + isActivityCreated = true; + timeRecycler = findViewById(R.id.time_list_recycler); + findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (timeListAdapter != null) { + timeListAdapter.addItem(); + } + } + }); + timeListAdapter = new TimeListAdapter(this); + timeRecycler.setLayoutManager(new LinearLayoutManager(this)); + timeRecycler.setAdapter(timeListAdapter); + } + + @Override + protected void onResume() { + super.onResume(); + if (!isActivityCreated) { + timeListAdapter.refreshCursor(); + } + isActivityCreated = false; + } + + @Override + protected void onDestroy() { + super.onDestroy(); + timeListAdapter.closeCursor(); + } + +} diff --git a/app/src/main/java/com/mdg/droiders/samagra/shush/adapters/TimeListAdapter.java b/app/src/main/java/com/mdg/droiders/samagra/shush/adapters/TimeListAdapter.java index 47d8e3f..2cba1fe 100644 --- a/app/src/main/java/com/mdg/droiders/samagra/shush/adapters/TimeListAdapter.java +++ b/app/src/main/java/com/mdg/droiders/samagra/shush/adapters/TimeListAdapter.java @@ -3,6 +3,7 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; +import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; @@ -13,10 +14,12 @@ import android.widget.Switch; import android.widget.TextView; +import com.mdg.droiders.samagra.shush.AlarmScheduler; import com.mdg.droiders.samagra.shush.R; import com.mdg.droiders.samagra.shush.data.PlacesContract; import com.mdg.droiders.samagra.shush.fragments.TimePickerDialogFragment; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; @@ -30,19 +33,16 @@ public class TimeListAdapter extends RecyclerView.Adapter + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 541773e..411752d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -133,6 +133,16 @@ android:textColor="@android:color/white" android:id="@+id/add_location_button"/> +