Skip to content

Commit

Permalink
Implemented Snooze Feature
Browse files Browse the repository at this point in the history
Merge branch 'release-testing'
  • Loading branch information
fei0316 committed Feb 7, 2019
2 parents e9a36ea + d29ef0e commit f506d97
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .idea/assetWizardSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

<service android:name=".resetService"/>
<service android:name=".reminderOff"/>
<service android:name=".SnoozeService"/>

</application>

Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/com/iatfei/streakalarm/AlarmReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,29 @@ public void onReceive(Context context, Intent intent) {
Intent openApp = new Intent(context, MainActivity.class);
Intent openSnap = context.getPackageManager().getLaunchIntentForPackage("com.snapchat.android");
Intent resetTime = new Intent(context, resetService.class);
Intent snooze = new Intent(context, SnoozeService.class);

PendingIntent pendingApp = PendingIntent.getActivity(context, 0, openApp, 0);
PendingIntent pendingReset = PendingIntent.getService(context, 0, resetTime, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingSnooze = PendingIntent.getService(context, 1, snooze, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context, "streak")
.setSmallIcon(R.drawable.timer_sand)
.setContentTitle(context.getString(R.string.notif_title))
.setColor(ContextCompat.getColor(context, R.color.colorAccentDark))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOngoing(true)
.setWhen(when)
.setContentIntent(pendingApp)
.addAction(R.drawable.ic_done_black_24dp, context.getString(R.string.notif_sent), pendingReset);
.setContentIntent(pendingApp);

if (openSnap != null) {
PendingIntent pendingSnap = PendingIntent.getActivity(context, 1, openSnap, 0);
nBuilder.addAction(R.drawable.snapchat, context.getString(R.string.menu_opensnapchat), pendingSnap);
}

nBuilder.addAction(R.drawable.ic_done_black_24dp, context.getString(R.string.notif_sent), pendingReset)
.addAction(R.drawable.ic_snooze_black_24dp, context.getString(R.string.notif_snooze), pendingSnooze);

Resources res = context.getResources();

if (showHours <= 0) {
Expand All @@ -76,7 +82,6 @@ public void onReceive(Context context, Intent intent) {
nBuilder.setContentText(convertToEnglishDigits.convert(res.getQuantityString(R.plurals.notif_body_multi, showHours, (notifCount - 1), showHours)))
.setStyle(new NotificationCompat.BigTextStyle().bigText(convertToEnglishDigits.convert(res.getQuantityString(R.plurals.notif_body_multi, showHours, (notifCount - 1), showHours))));
}

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Objects.requireNonNull(notificationManager).cancel(2);
notificationManager.notify(2, nBuilder.build());
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/java/com/iatfei/streakalarm/NotificationManage.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

public class NotificationManage extends MainActivity {

//todo:Next Release: add postpone notification option

public static void MakeNotif (Context c) {
long laststreak = Time.ReadTime(c);
long notifint = Time.LongInterval(c);
Expand All @@ -57,6 +55,21 @@ public static void MakeNotif (Context c) {
PendingIntent pendingIntent245 = PendingIntent.getBroadcast(c, 3, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, (laststreak + 1000 * 60 * 60 * 25 - 1000 * 60 * 30), pendingIntent245);
}

public static void Snooze (Context c) {
Intent intent = new Intent(c, AlarmReceiver.class);
AlarmManager am = (AlarmManager) c.getSystemService(ALARM_SERVICE);

PendingIntent pendingSnooze = PendingIntent.getBroadcast(c, 4, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + 1000 * 60 * 30), pendingSnooze);
}

public static void CloseNotif (Context c) {
NotificationManager notif =
(NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);
notif.cancel(2);
}

public static void CancelNotif (Context c) {
Intent intent1 = new Intent(c, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(c, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/com/iatfei/streakalarm/SnoozeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2017-2019 Fei Kuan.
*
* This file is part of Streak Alarm
* (see <https://github.com/fei0316/snapstreak-alarm>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.iatfei.streakalarm;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;

public class SnoozeService extends IntentService {
public SnoozeService() {
super("SnoozeService");
}

@Override
protected void onHandleIntent(Intent intent) {
Context c = getApplicationContext();
NotificationManage.CloseNotif(c);
NotificationManage.Snooze(c);
int currentTally = Time.ReadNotifCount(c);
Time.setTally(c, (currentTally-1));
}
}
46 changes: 16 additions & 30 deletions app/src/main/java/com/iatfei/streakalarm/Time.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,30 @@
import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class Time extends MainActivity{
public class Time extends MainActivity {

//todo:Implement RTL instead of disabling it

public static void ResetTime (Context c) {
public static void ResetTime(Context c) {
long saveLongTime = System.currentTimeMillis();
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor editor = pref.edit();
editor.putLong("lastsnaptime", saveLongTime);
editor.putInt("notifcount",1);
editor.putInt("notifcount", 1);
editor.apply();
}
public static long ReadTime (Context c) {

public static long ReadTime(Context c) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
return settings.getLong("lastsnaptime", 123);
}

public static int ReadNotifCount (Context c) {
public static int ReadNotifCount(Context c) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
return settings.getInt("notifcount", 1);
}

public static void NotifCountTally (Context c) {
public static void NotifCountTally(Context c) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
int count = settings.getInt("notifcount", 0);
count++;
Expand All @@ -58,15 +59,15 @@ public static void NotifCountTally (Context c) {
editor.apply();
}

public static void setTally (Context c, int count) {
public static void setTally(Context c, int count) {
//Only for reboot/update to prevent excessive notifications count
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("notifcount", count);
editor.apply();
}

public static String ReadFormatTime (Context c) {
public static String ReadFormatTime(Context c) {
String formatted;
long time_last = ReadTime(c);
long time_till = System.currentTimeMillis() - time_last;
Expand All @@ -86,23 +87,24 @@ else if (time_till < 0)
}
return formatted;
}
public static void SetTime (Context c, long millis) {

public static void SetTime(Context c, long millis) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor editor = pref.edit();
editor.putLong("lastsnaptime", millis);
editor.putInt("notifcount",1);
editor.putInt("notifcount", 1);
editor.apply();
}

public static int NotifTime (Context c){
public static int NotifTime(Context c) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(c);
long time_last = pref.getLong("lastsnaptime", 0);
long time_to_send = (time_last + 86700000) - System.currentTimeMillis();
double Hours = (double) (time_to_send / 1000 / 60 / 60);
return (int) Hours;
}

public static void SetInterval (Context c, int hours){
public static void SetInterval(Context c, int hours) {
long Lhours = hours * 1000 * 60 * 60;
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor editor = pref.edit();
Expand All @@ -111,29 +113,13 @@ public static void SetInterval (Context c, int hours){
editor.apply();
}

public static int IntInterval (Context c) {
public static int IntInterval(Context c) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(c);
return pref.getInt("intervalInt", 8);
}

public static long LongInterval (Context c) {
public static long LongInterval(Context c) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(c);
return pref.getLong("intervalLong", 28800000);
}


//may not need anymore...

/*public static void setLastFire (Context c, long millis){
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor editor = pref.edit();
editor.putLong("nextnotif", millis);
editor.apply();
}
public static long getLastFire (Context c){
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(c);
long l = pref.getLong("nextnotif", 0);
return l;
}*/
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_snooze_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M7.88,3.39L6.6,1.86 2,5.71l1.29,1.53 4.59,-3.85zM22,5.72l-4.6,-3.86 -1.29,1.53 4.6,3.86L22,5.72zM12,4c-4.97,0 -9,4.03 -9,9s4.02,9 9,9c4.97,0 9,-4.03 9,-9s-4.03,-9 -9,-9zM12,20c-3.87,0 -7,-3.13 -7,-7s3.13,-7 7,-7 7,3.13 7,7 -3.13,7 -7,7zM9,11h3.63L9,15.2L9,17h6v-2h-3.63L15,10.8L15,9L9,9v2z"/>
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@
<string name="dialog_battery_button_positive">前往設定</string>
<string name="notif_turnoff_reminder">關閉提示</string>
<string name="main_new">不明</string>
<string name="notif_snooze">稍後通知</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@
<string name="dialog_battery_button_positive">前往設定</string>
<string name="notif_turnoff_reminder">關閉提示</string>
<string name="main_new">不明</string>
<string name="notif_snooze">稍後通知</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@
<string name="chineseHK_content" translatable="false">如果你是香港澳門用戶,你將你的語言設定爲台灣中文或者簡體中文了,建議在設定改爲香港中文,否則用語可能比較奇怪。</string>
<string name="main_long_ago">Long ago...</string>
<string name="main_new">Unknown</string>
<string name="notif_snooze">Snooze</string>
</resources>

0 comments on commit f506d97

Please sign in to comment.