-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean pedo trash code, fix service stop when phone was locked
- Loading branch information
Dan Yin
authored and
Dan Yin
committed
Dec 6, 2020
1 parent
6eb5d0e
commit bd5420a
Showing
31 changed files
with
754 additions
and
1,721 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
app/src/main/java/neu/madcourse/walkwithme/Pedometer/CheckAppRunService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package neu.madcourse.walkwithme.Pedometer; | ||
|
||
import android.app.AlarmManager; | ||
import android.app.PendingIntent; | ||
import android.app.Service; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.os.IBinder; | ||
import android.util.Log; | ||
|
||
import neu.madcourse.walkwithme.MainActivity; | ||
|
||
public class CheckAppRunService extends Service { | ||
//If user hasn't use the app for 3 days, send notification | ||
|
||
private final static String TAG = "CheckRecentPlay"; | ||
private static Long MILLISECS_PER_DAY = 86400000L; | ||
private static Long MILLISECS_PER_MIN = 60000L; | ||
private static String CHANNEL_ID = "WalkWithMe"; | ||
|
||
//private static long delay = MILLISECS_PER_MIN ; // 30s (for testing) | ||
private static long delay = MILLISECS_PER_DAY * 3; // 3 days | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
|
||
Log.d(TAG, "Set Alarm to check last active"); | ||
SharedPreferences settings = getSharedPreferences(MainActivity.CHANNEL, MODE_PRIVATE); | ||
|
||
// Are notifications enabled? | ||
if (settings.getBoolean("notification", true)) { | ||
// Is it time for a notification? | ||
if (settings.getLong("preRun", Long.MAX_VALUE) < System.currentTimeMillis() - delay) { | ||
NotificationCenter notificationCenter = new NotificationCenter(getApplicationContext()); | ||
notificationCenter.createNotification(NofiticationConstants.THREE_DAYS_INACTIVE); | ||
} | ||
|
||
} else { | ||
Log.i(TAG, "Notifications are disabled"); | ||
} | ||
|
||
// Set an alarm for the next time this service should run: | ||
setAlarm(); | ||
stopSelf(); | ||
} | ||
|
||
public void setAlarm() { | ||
|
||
Intent serviceIntent = new Intent(this, CheckAppRunService.class); | ||
PendingIntent pendingIntent = PendingIntent.getService(this, 1, serviceIntent, | ||
PendingIntent.FLAG_CANCEL_CURRENT); | ||
|
||
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); | ||
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pendingIntent); | ||
Log.v(TAG, "Recent activity check alarm set"); | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return null; | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
.../madcourse/walkwithme/Test/Constants.java → ...ourse/walkwithme/Pedometer/Constants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
package neu.madcourse.walkwithme.Test; | ||
package neu.madcourse.walkwithme.Pedometer; | ||
|
||
public class Constants { | ||
public static final String START_FOREGROUND = "start_foreground"; | ||
public static final String STOP_FOREGROUND = "stop_foreground"; | ||
public static final String RESET_COUNT = "reset_count"; | ||
public static final String STOP_SAVE_COUNT = "stop_save_count"; | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/neu/madcourse/walkwithme/Pedometer/GetHistory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package neu.madcourse.walkwithme.Pedometer; | ||
|
||
import java.util.Calendar; | ||
import java.util.Date; | ||
|
||
|
||
public class GetHistory { | ||
//final String timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); | ||
//to get past 6 days data in "yyyy-MM-dd" string | ||
|
||
public static Date getToday() | ||
{ | ||
Date d=new Date(); | ||
return new Date(d.getYear(),d.getMonth(),d.getDate()); | ||
} | ||
|
||
public static Date add(Date d, int n) | ||
{ | ||
Calendar calendar= Calendar.getInstance(); | ||
calendar.setTime(d); | ||
calendar.add(Calendar.DAY_OF_MONTH,n); | ||
return calendar.getTime(); | ||
} | ||
|
||
public static String[] get7days() | ||
{ | ||
Date today=getToday(); | ||
|
||
String[] days=new String[7]; | ||
for (int i=0;i<7;i++) | ||
{ | ||
Date t=add(today,i-6); | ||
days[i]=t.getMonth()+1+"."+t.getDate(); | ||
} | ||
return days; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
app/src/main/java/neu/madcourse/walkwithme/Pedometer/MeetGoalReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package neu.madcourse.walkwithme.Pedometer; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.util.Log; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.google.firebase.database.DataSnapshot; | ||
import com.google.firebase.database.DatabaseError; | ||
import com.google.firebase.database.DatabaseReference; | ||
import com.google.firebase.database.FirebaseDatabase; | ||
import com.google.firebase.database.ValueEventListener; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
|
||
import neu.madcourse.walkwithme.userlog.LoginActivity; | ||
|
||
public class MeetGoalReceiver extends BroadcastReceiver { | ||
private static String CHANNEL_ID = "WalkWithMe"; | ||
private static String TAG = "Notification Recever"; | ||
private FirebaseDatabase mdb; | ||
private DatabaseReference step_ref; | ||
private int step; | ||
private SharedPreferences settings; | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
Log.d(TAG, "onReceive: "); | ||
mdb = FirebaseDatabase.getInstance(); | ||
settings = context.getSharedPreferences("WalkWithMe", Context.MODE_PRIVATE); | ||
//int goal = Integer.parseInt(settings.getString("dailyGoal", null)); | ||
//Log.d(TAG, "goal: " + goal); | ||
|
||
try{ | ||
step_ref = mdb.getReference().child("users").child(LoginActivity.currentUser); | ||
}catch (Exception e){ | ||
} | ||
//get steps to check whether need to send notification | ||
getStep(context); | ||
|
||
} | ||
|
||
private void getStep(Context context) { | ||
final String timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); | ||
NotificationCenter notificationCenter = new NotificationCenter(context); | ||
|
||
Log.d(TAG,"access firebase data: " + timestamp); | ||
try{ | ||
step_ref.addValueEventListener(new ValueEventListener() { | ||
@Override | ||
public void onDataChange(@NonNull DataSnapshot dataSnapshot) { | ||
if (dataSnapshot.child("Step Count").child(timestamp).exists()) { | ||
Steps steps = dataSnapshot.child("Step Count").child(timestamp).getValue(Steps.class); | ||
step = (int) steps.getSteps(); //get previous steps | ||
Log.d(TAG,"exist"); | ||
} else { | ||
step = 0; | ||
} | ||
Log.d(TAG,"Step: " + step); | ||
Log.d(TAG,"Goal: " + StepsFragment2.dailyGoal); | ||
if(StepsFragment2.dailyGoal > step && StepsFragment2.dailyGoal - step <= 300){ | ||
//close to goal | ||
Log.d(TAG, "onDataChange: diff is less than 300"); | ||
notificationCenter.createNotification(NofiticationConstants.b1elowGoal); | ||
}else if(StepsFragment2.dailyGoal > step){ | ||
Log.d(TAG, "onDataChange: diff is more than 300"); | ||
notificationCenter.createNotification(NofiticationConstants.b1elowGoal2); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void onCancelled(@NonNull DatabaseError databaseError) { | ||
} | ||
}); | ||
}catch (Exception e){ | ||
Log.d(TAG,"exception " + e.getLocalizedMessage()); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/neu/madcourse/walkwithme/Pedometer/NofiticationConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package neu.madcourse.walkwithme.Pedometer; | ||
|
||
|
||
import neu.madcourse.walkwithme.R; | ||
|
||
public class NofiticationConstants{ | ||
//id 1 = service | ||
//id 2 = level change | ||
//id 3 = daily goal | ||
//id 4 = inactive | ||
//Remember to kill 2, 3 when log out | ||
|
||
public static final NotificationMessage SERVICE_START = new NotificationMessage("Your Step Service is now activated ", "Let's walk with me!", 1, R.drawable.happy); | ||
public static final NotificationMessage SERVICE_STOP = new NotificationMessage("\uD83D\uDE2D \uD83D\uDE2DStep Service is no longer activated ", "", 1, R.drawable.sleep); | ||
|
||
public static final NotificationMessage L2 = new NotificationMessage("Congratulations \uD83C\uDF8A \uD83C\uDF8A","You've leveled up to level 2!", 2, R.drawable.fireworks); | ||
public static final NotificationMessage L3 = new NotificationMessage("Hooray \uD83C\uDF89 \uD83C\uDF89", "That's level 3!", 2, R.drawable.fireworks); | ||
public static final NotificationMessage L4 = new NotificationMessage("Woo Hoo \uD83D\uDC4F \uD83D\uDC4F \uD83D\uDC4F", "You made it to level 4!", 2, R.drawable.fireworks); | ||
public static final NotificationMessage L5 = new NotificationMessage("❤Amazing❤", "You've reached level 5!", 2, R.drawable.fireworks); | ||
|
||
public static final NotificationMessage meetGoal = new NotificationMessage("\uD83D\uDC4F \uD83D\uDC4F Excellent! ", "You have achieved today's goal!", 3, R.drawable.happy); | ||
public static final NotificationMessage b1elowGoal = new NotificationMessage("\uD83D\uDD08 Almost there", "Only few steps away from your daily goal! \uD83D\uDEB6\u200D♂️ \uD83D\uDEB6\u200D♂️", 3, R.drawable.happy); | ||
public static final NotificationMessage b1elowGoal2 = new NotificationMessage("\uD83D\uDE30 Looks like you forgot the goal", "It't not too late to meet your daily goal!️", 3, R.drawable.sleep); | ||
|
||
public static final NotificationMessage THREE_DAYS_INACTIVE = new NotificationMessage("\uD83D\uDE30 We miss you!", "Why don't you check what is going in your app?", 4, R.drawable.music); | ||
|
||
} |
Oops, something went wrong.