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
|
<uses-sdk
|
||||||
android:minSdkVersion="14"
|
android:minSdkVersion="14"
|
||||||
android:targetSdkVersion="20" />
|
android:targetSdkVersion="22" />
|
||||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||||
|
|
||||||
<application
|
<application
|
||||||
@@ -33,7 +33,7 @@
|
|||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<receiver android:name="AlarmReceiver">
|
<receiver android:name="ReminderAlarmReceiver">
|
||||||
</receiver>
|
</receiver>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -11,4 +11,4 @@
|
|||||||
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
|
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
|
||||||
|
|
||||||
# Project target.
|
# Project target.
|
||||||
target=android-18
|
target=android-22
|
||||||
|
|||||||
@@ -1,22 +1,22 @@
|
|||||||
package org.isoron.uhabits;
|
package org.isoron.uhabits;
|
||||||
|
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
import org.isoron.helpers.Command;
|
import org.isoron.helpers.Command;
|
||||||
import org.isoron.uhabits.dialogs.ShowHabitsFragment;
|
import org.isoron.uhabits.dialogs.ShowHabitsFragment;
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlarmManager;
|
import android.app.AlarmManager;
|
||||||
import android.app.Notification;
|
|
||||||
import android.app.NotificationManager;
|
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.media.RingtoneManager;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.SystemClock;
|
import android.util.Log;
|
||||||
import android.support.v4.app.NotificationCompat;
|
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
@@ -39,8 +39,7 @@ public class MainActivity extends Activity
|
|||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
// Habit.rebuildOrder();
|
|
||||||
// Habit.roundTimestamps();
|
|
||||||
setContentView(R.layout.main_activity);
|
setContentView(R.layout.main_activity);
|
||||||
showHabitsFragment = (ShowHabitsFragment) getFragmentManager().findFragmentById(
|
showHabitsFragment = (ShowHabitsFragment) getFragmentManager().findFragmentById(
|
||||||
R.id.fragment1);
|
R.id.fragment1);
|
||||||
@@ -48,23 +47,39 @@ public class MainActivity extends Activity
|
|||||||
undoList = new LinkedList<Command>();
|
undoList = new LinkedList<Command>();
|
||||||
redoList = new LinkedList<Command>();
|
redoList = new LinkedList<Command>();
|
||||||
|
|
||||||
// startAlarm("http://hello-world.com/", 5);
|
createReminderAlarms();
|
||||||
// startAlarm("http://ola-mundo.com.br/", 10);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startAlarm(String data, int interval)
|
public void createReminderAlarms()
|
||||||
{
|
{
|
||||||
Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
|
for(Habit habit : Habit.getHabitsWithReminder())
|
||||||
alarmIntent.setData(Uri.parse(data));
|
{
|
||||||
|
Uri uri = Uri.parse("content://org.isoron.uhabits/habit/" + habit.getId());
|
||||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
|
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
calendar.setTimeInMillis(System.currentTimeMillis());
|
||||||
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
|
calendar.set(Calendar.HOUR_OF_DAY, habit.reminder_hour);
|
||||||
SystemClock.elapsedRealtime() +
|
calendar.set(Calendar.MINUTE, habit.reminder_min);
|
||||||
interval * 1000, pendingIntent);
|
calendar.set(Calendar.SECOND, 0);
|
||||||
|
|
||||||
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
|
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.setExact(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
|
||||||
|
|
||||||
|
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;
|
package org.isoron.uhabits;
|
||||||
|
|
||||||
|
import org.isoron.helpers.DateHelper;
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.ContentUris;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.media.RingtoneManager;
|
import android.media.RingtoneManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.support.v4.app.NotificationCompat;
|
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
|
@Override
|
||||||
public void onReceive(Context context, Intent intent)
|
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)
|
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);
|
Intent resultIntent = new Intent(context, MainActivity.class);
|
||||||
resultIntent.setData(data);
|
resultIntent.setData(data);
|
||||||
|
|
||||||
@@ -35,15 +49,17 @@ public class AlarmReceiver extends BroadcastReceiver
|
|||||||
new NotificationCompat.Builder(context)
|
new NotificationCompat.Builder(context)
|
||||||
.setSmallIcon(R.drawable.ic_notification)
|
.setSmallIcon(R.drawable.ic_notification)
|
||||||
.setContentTitle("Habit Reminder")
|
.setContentTitle("Habit Reminder")
|
||||||
.setContentText(text)
|
.setContentText(habit.name)
|
||||||
.setContentIntent(notificationIntent)
|
.setContentIntent(notificationIntent)
|
||||||
.setSound(soundUri)
|
.setSound(soundUri)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
notification.flags = Notification.FLAG_AUTO_CANCEL;
|
notification.flags = Notification.FLAG_AUTO_CANCEL;
|
||||||
|
|
||||||
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);
|
NotificationManager notificationManager = (NotificationManager) context
|
||||||
notificationManager.notify(k++, notification);
|
.getSystemService(Activity.NOTIFICATION_SERVICE);
|
||||||
|
|
||||||
|
notificationManager.notify(0, notification);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.isoron.uhabits.dialogs;
|
package org.isoron.uhabits.dialogs;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
@@ -20,7 +19,6 @@ import android.graphics.Typeface;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Vibrator;
|
import android.os.Vibrator;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
import android.util.Log;
|
|
||||||
import android.view.ContextMenu;
|
import android.view.ContextMenu;
|
||||||
import android.view.ContextMenu.ContextMenuInfo;
|
import android.view.ContextMenu.ContextMenuInfo;
|
||||||
import android.view.Display;
|
import android.view.Display;
|
||||||
@@ -53,6 +51,7 @@ public class ShowHabitsFragment extends Fragment implements OnSavedListener, OnI
|
|||||||
private int button_count;
|
private int button_count;
|
||||||
ShowHabitsAdapter adapter;
|
ShowHabitsAdapter adapter;
|
||||||
DragSortListView listView;
|
DragSortListView listView;
|
||||||
|
MainActivity mainActivity;
|
||||||
|
|
||||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* Adapter *
|
* Adapter *
|
||||||
@@ -245,6 +244,8 @@ public class ShowHabitsFragment extends Fragment implements OnSavedListener, OnI
|
|||||||
|
|
||||||
day.add(GregorianCalendar.DAY_OF_MONTH, -1);
|
day.add(GregorianCalendar.DAY_OF_MONTH, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mainActivity = (MainActivity) getActivity();
|
||||||
|
|
||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
return view;
|
return view;
|
||||||
@@ -311,6 +312,7 @@ public class ShowHabitsFragment extends Fragment implements OnSavedListener, OnI
|
|||||||
public void onSaved(Command command)
|
public void onSaved(Command command)
|
||||||
{
|
{
|
||||||
executeCommand(command);
|
executeCommand(command);
|
||||||
|
mainActivity.createReminderAlarms();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void notifyDataSetChanged()
|
public void notifyDataSetChanged()
|
||||||
@@ -344,7 +346,7 @@ public class ShowHabitsFragment extends Fragment implements OnSavedListener, OnI
|
|||||||
|
|
||||||
private void executeCommand(Command c)
|
private void executeCommand(Command c)
|
||||||
{
|
{
|
||||||
((MainActivity) getActivity()).executeCommand(c, false);
|
mainActivity.executeCommand(c, false);
|
||||||
adapter.notifyDataSetChanged();
|
adapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.isoron.uhabits.models;
|
package org.isoron.uhabits.models;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.isoron.helpers.Command;
|
import org.isoron.helpers.Command;
|
||||||
@@ -9,7 +8,6 @@ import org.isoron.uhabits.R;
|
|||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import com.activeandroid.Model;
|
import com.activeandroid.Model;
|
||||||
import com.activeandroid.annotation.Column;
|
import com.activeandroid.annotation.Column;
|
||||||
@@ -236,6 +234,17 @@ public class Habit extends Model
|
|||||||
{
|
{
|
||||||
return select().offset(position).executeSingle();
|
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 *
|
* Repetitions *
|
||||||
|
|||||||
Reference in New Issue
Block a user