mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Initial version of reminder alarms
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="14"
|
||||
android:targetSdkVersion="20" />
|
||||
android:targetSdkVersion="22" />
|
||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||
|
||||
<application
|
||||
@@ -33,7 +33,7 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<receiver android:name="AlarmReceiver">
|
||||
<receiver android:name="ReminderAlarmReceiver">
|
||||
</receiver>
|
||||
</application>
|
||||
|
||||
|
||||
Binary file not shown.
@@ -11,4 +11,4 @@
|
||||
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
|
||||
|
||||
# Project target.
|
||||
target=android-18
|
||||
target=android-22
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
package org.isoron.uhabits;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import org.isoron.helpers.Command;
|
||||
import org.isoron.uhabits.dialogs.ShowHabitsFragment;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlarmManager;
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.media.RingtoneManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.SystemClock;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.Toast;
|
||||
@@ -39,8 +39,7 @@ public class MainActivity extends Activity
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
// Habit.rebuildOrder();
|
||||
// Habit.roundTimestamps();
|
||||
|
||||
setContentView(R.layout.main_activity);
|
||||
showHabitsFragment = (ShowHabitsFragment) getFragmentManager().findFragmentById(
|
||||
R.id.fragment1);
|
||||
@@ -48,23 +47,39 @@ public class MainActivity extends Activity
|
||||
undoList = new LinkedList<Command>();
|
||||
redoList = new LinkedList<Command>();
|
||||
|
||||
// startAlarm("http://hello-world.com/", 5);
|
||||
// startAlarm("http://ola-mundo.com.br/", 10);
|
||||
createReminderAlarms();
|
||||
}
|
||||
|
||||
private void startAlarm(String data, int interval)
|
||||
public void createReminderAlarms()
|
||||
{
|
||||
Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
|
||||
alarmIntent.setData(Uri.parse(data));
|
||||
for(Habit habit : Habit.getHabitsWithReminder())
|
||||
{
|
||||
Uri uri = Uri.parse("content://org.isoron.uhabits/habit/" + habit.getId());
|
||||
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeInMillis(System.currentTimeMillis());
|
||||
calendar.set(Calendar.HOUR_OF_DAY, habit.reminder_hour);
|
||||
calendar.set(Calendar.MINUTE, habit.reminder_min);
|
||||
calendar.set(Calendar.SECOND, 0);
|
||||
|
||||
long reminderTime = calendar.getTimeInMillis();
|
||||
|
||||
if(System.currentTimeMillis() > reminderTime) {
|
||||
reminderTime += AlarmManager.INTERVAL_DAY;
|
||||
}
|
||||
|
||||
Intent alarmIntent = new Intent(MainActivity.this, ReminderAlarmReceiver.class);
|
||||
alarmIntent.setData(uri);
|
||||
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 100,
|
||||
alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
|
||||
SystemClock.elapsedRealtime() +
|
||||
interval * 1000, pendingIntent);
|
||||
manager.setExact(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
|
||||
|
||||
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
|
||||
Log.d("Alarm", String.format("Setting alarm (%s): %s", DateFormat.getDateTimeInstance()
|
||||
.format(new Date(reminderTime)), habit.name));
|
||||
}
|
||||
}
|
||||
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
package org.isoron.uhabits;
|
||||
|
||||
import org.isoron.helpers.DateHelper;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ContentUris;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.media.RingtoneManager;
|
||||
import android.net.Uri;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.util.Log;
|
||||
|
||||
public class AlarmReceiver extends BroadcastReceiver
|
||||
public class ReminderAlarmReceiver extends BroadcastReceiver
|
||||
{
|
||||
static int k = 1;
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent)
|
||||
{
|
||||
@@ -24,6 +27,17 @@ public class AlarmReceiver extends BroadcastReceiver
|
||||
|
||||
private void createNotification(Context context, Uri data, String text)
|
||||
{
|
||||
Log.d("Alarm", "Alarm received!");
|
||||
Habit habit = Habit.get(ContentUris.parseId(data));
|
||||
|
||||
// Check if user already did the habit repetition
|
||||
if(habit.hasRep(DateHelper.getStartOfDay(DateHelper.getLocalTime())))
|
||||
return;
|
||||
|
||||
// Check if reminder has been turned off after alarm was scheduled
|
||||
if(habit.reminder_hour == null)
|
||||
return;
|
||||
|
||||
Intent resultIntent = new Intent(context, MainActivity.class);
|
||||
resultIntent.setData(data);
|
||||
|
||||
@@ -35,15 +49,17 @@ public class AlarmReceiver extends BroadcastReceiver
|
||||
new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setContentTitle("Habit Reminder")
|
||||
.setContentText(text)
|
||||
.setContentText(habit.name)
|
||||
.setContentIntent(notificationIntent)
|
||||
.setSound(soundUri)
|
||||
.build();
|
||||
|
||||
notification.flags = Notification.FLAG_AUTO_CANCEL;
|
||||
|
||||
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(k++, notification);
|
||||
NotificationManager notificationManager = (NotificationManager) context
|
||||
.getSystemService(Activity.NOTIFICATION_SERVICE);
|
||||
|
||||
notificationManager.notify(0, notification);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.isoron.uhabits.dialogs;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
@@ -20,7 +19,6 @@ import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
import android.os.Vibrator;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.ContextMenu.ContextMenuInfo;
|
||||
import android.view.Display;
|
||||
@@ -53,6 +51,7 @@ public class ShowHabitsFragment extends Fragment implements OnSavedListener, OnI
|
||||
private int button_count;
|
||||
ShowHabitsAdapter adapter;
|
||||
DragSortListView listView;
|
||||
MainActivity mainActivity;
|
||||
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* Adapter *
|
||||
@@ -246,6 +245,8 @@ public class ShowHabitsFragment extends Fragment implements OnSavedListener, OnI
|
||||
day.add(GregorianCalendar.DAY_OF_MONTH, -1);
|
||||
}
|
||||
|
||||
mainActivity = (MainActivity) getActivity();
|
||||
|
||||
setHasOptionsMenu(true);
|
||||
return view;
|
||||
}
|
||||
@@ -311,6 +312,7 @@ public class ShowHabitsFragment extends Fragment implements OnSavedListener, OnI
|
||||
public void onSaved(Command command)
|
||||
{
|
||||
executeCommand(command);
|
||||
mainActivity.createReminderAlarms();
|
||||
}
|
||||
|
||||
public void notifyDataSetChanged()
|
||||
@@ -344,7 +346,7 @@ public class ShowHabitsFragment extends Fragment implements OnSavedListener, OnI
|
||||
|
||||
private void executeCommand(Command c)
|
||||
{
|
||||
((MainActivity) getActivity()).executeCommand(c, false);
|
||||
mainActivity.executeCommand(c, false);
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.isoron.uhabits.models;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.isoron.helpers.Command;
|
||||
@@ -9,7 +8,6 @@ import org.isoron.uhabits.R;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.graphics.Color;
|
||||
import android.util.Log;
|
||||
|
||||
import com.activeandroid.Model;
|
||||
import com.activeandroid.annotation.Column;
|
||||
@@ -237,6 +235,17 @@ public class Habit extends Model
|
||||
return select().offset(position).executeSingle();
|
||||
}
|
||||
|
||||
public static java.util.List<Habit> getHabits()
|
||||
{
|
||||
return select().execute();
|
||||
}
|
||||
|
||||
public static java.util.List<Habit> getHabitsWithReminder()
|
||||
{
|
||||
return select().where("reminder_hour is not null").execute();
|
||||
}
|
||||
|
||||
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* Repetitions *
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
Reference in New Issue
Block a user