Search in sources :

Example 1 with DatabaseHandler

use of com.instructure.parentapp.database.DatabaseHandler in project instructure-android by instructure.

the class AssignmentFragment method cancelAlarm.

private void cancelAlarm() {
    // cancel the alarm
    AlarmReceiver alarmReceiver = new AlarmReceiver();
    String subTitle = "";
    if (mAssignment.getDueAt() != null) {
        subTitle = getContext().getResources().getString(R.string.due) + " " + DateHelper.getDateTimeString(getContext(), mAssignment.getDueAt());
    }
    alarmReceiver.cancelAlarm(getContext(), mAssignment.getId(), mAssignment.getName(), subTitle);
    // remove it from the database
    if (mDatabaseHandler == null) {
        mDatabaseHandler = new DatabaseHandler(getActivity());
    }
    try {
        mDatabaseHandler.open();
        int id = mDatabaseHandler.getRowIdByAssignmentId(mAssignment.getId());
        int result = mDatabaseHandler.deleteAlarm(id);
        mDatabaseHandler.close();
    } catch (SQLException e) {
    // couldn't delete the alarm, so it will remain in the database. But the actual
    // alarm should have been canceled above.
    }
}
Also used : SQLException(java.sql.SQLException) DatabaseHandler(com.instructure.parentapp.database.DatabaseHandler) AlarmReceiver(com.instructure.parentapp.receivers.AlarmReceiver)

Example 2 with DatabaseHandler

use of com.instructure.parentapp.database.DatabaseHandler in project instructure-android by instructure.

the class EventFragment method setupAlarmInfo.

private void setupAlarmInfo() {
    mDatabaseHandler = new DatabaseHandler(getActivity());
    try {
        mDatabaseHandler.open();
        Calendar alarm = mDatabaseHandler.getAlarmByAssignmentId(mScheduleItem.getId());
        if (alarm != null) {
            mAlarmId = mDatabaseHandler.getRowIdByAssignmentId(mScheduleItem.getId());
            mAlarmDetails.setVisibility(View.VISIBLE);
            mAlarmDetails.setText(DateHelper.getShortDateTimeStringUniversal(getContext(), alarm.getTime()));
            // set the listener to null so we don't trigger the onCheckChangedListener when we set the value
            mAlarmSwitch.setOnCheckedChangeListener(null);
            mAlarmSwitch.setChecked(true);
            mAlarmSwitch.setOnCheckedChangeListener(mCheckedChangeListener);
        } else {
            mAlarmSwitch.setChecked(false);
            mAlarmSwitch.setOnCheckedChangeListener(mCheckedChangeListener);
        }
        mDatabaseHandler.close();
    } catch (SQLException e) {
        // couldn't find the alarm in the database, so don't show that there is an alarm
        mAlarmSwitch.setChecked(false);
        mAlarmSwitch.setOnCheckedChangeListener(mCheckedChangeListener);
    }
}
Also used : SQLException(java.sql.SQLException) DatabaseHandler(com.instructure.parentapp.database.DatabaseHandler) Calendar(java.util.Calendar)

Example 3 with DatabaseHandler

use of com.instructure.parentapp.database.DatabaseHandler in project instructure-android by instructure.

the class AlarmReceiver method setAlarm.

public void setAlarm(Context context, Calendar calendar, long assignmentId, String title, String subTitle) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, AlarmReceiver.class);
    i.putExtra(Const.TITLE_TEXT, title);
    i.putExtra(Const.SUBTITLE_TEXT, subTitle);
    // set alarms here
    // get the row id and set it as the request code so it will be unique
    int alarmId = -1;
    DatabaseHandler mDatabaseHandler = new DatabaseHandler(context);
    try {
        mDatabaseHandler.open();
        alarmId = mDatabaseHandler.getRowIdByAssignmentId(assignmentId);
        mDatabaseHandler.close();
    } catch (SQLException e) {
    // can't find the alarmId
    }
    // verify that we have a valid alarm id
    if (alarmId != -1) {
        PendingIntent pi = PendingIntent.getBroadcast(context, alarmId, i, PendingIntent.FLAG_UPDATE_CURRENT);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
    }
}
Also used : SQLException(java.sql.SQLException) DatabaseHandler(com.instructure.parentapp.database.DatabaseHandler) AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Example 4 with DatabaseHandler

use of com.instructure.parentapp.database.DatabaseHandler in project instructure-android by instructure.

the class BootReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        // set alarms here
        DatabaseHandler mDatabaseHandler = new DatabaseHandler(context);
        try {
            mDatabaseHandler.open();
            ArrayList<CalendarWrapper> calendars = mDatabaseHandler.getAllAlarms();
            if (calendars != null) {
                for (CalendarWrapper wrapper : calendars) {
                    alarm.setAlarm(context, wrapper.getCalendar(), wrapper.getAssignmentId(), wrapper.getTitle(), wrapper.getSubTitle());
                }
            }
            mDatabaseHandler.close();
        } catch (SQLException e) {
        // do nothing
        }
    }
}
Also used : SQLException(java.sql.SQLException) DatabaseHandler(com.instructure.parentapp.database.DatabaseHandler) CalendarWrapper(com.instructure.parentapp.models.CalendarWrapper)

Example 5 with DatabaseHandler

use of com.instructure.parentapp.database.DatabaseHandler in project instructure-android by instructure.

the class AssignmentFragment method setupAlarmInfo.

private void setupAlarmInfo() {
    mDatabaseHandler = new DatabaseHandler(getActivity());
    try {
        mDatabaseHandler.open();
        Calendar alarm = mDatabaseHandler.getAlarmByAssignmentId(mAssignment.getId());
        if (alarm != null) {
            mAlarmId = mDatabaseHandler.getRowIdByAssignmentId(mAssignment.getId());
            mAlarmDetails.setVisibility(View.VISIBLE);
            mAlarmDetails.setText(DateHelper.getShortDateTimeStringUniversal(getContext(), alarm.getTime()));
            // set the listener to null so we don't trigger the onCheckChangedListener when we set the value
            mAlarmSwitch.setOnCheckedChangeListener(null);
            mAlarmSwitch.setChecked(true);
            mAlarmSwitch.setOnCheckedChangeListener(mCheckedChangeListener);
        } else {
            mAlarmSwitch.setChecked(false);
            mAlarmSwitch.setOnCheckedChangeListener(mCheckedChangeListener);
        }
        mDatabaseHandler.close();
    } catch (SQLException e) {
        // couldn't find the alarm in the database, so don't show that there is an alarm
        mAlarmSwitch.setChecked(false);
        mAlarmSwitch.setOnCheckedChangeListener(mCheckedChangeListener);
    }
}
Also used : SQLException(java.sql.SQLException) DatabaseHandler(com.instructure.parentapp.database.DatabaseHandler) Calendar(java.util.Calendar)

Aggregations

DatabaseHandler (com.instructure.parentapp.database.DatabaseHandler)7 SQLException (java.sql.SQLException)7 AlarmManager (android.app.AlarmManager)2 PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 AlarmReceiver (com.instructure.parentapp.receivers.AlarmReceiver)2 Calendar (java.util.Calendar)2 CalendarWrapper (com.instructure.parentapp.models.CalendarWrapper)1