Skip to content

Commit

Permalink
Merge pull request #56 from cyclexuxu/new1
Browse files Browse the repository at this point in the history
Clean pedo trash code, fix service stop when phone was locked
  • Loading branch information
dyin4 authored Dec 6, 2020
2 parents 6eb5d0e + bd5420a commit bc0135c
Show file tree
Hide file tree
Showing 31 changed files with 754 additions and 1,721 deletions.
8 changes: 5 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK" />


<application
android:allowBackup="true"
Expand All @@ -19,12 +21,12 @@
<activity android:name=".ranking.RankingActivity"></activity>

<service
android:name=".Test.StepService3"
android:name=".Pedometer.StepService3"
android:enabled="true"
android:exported="true" />

<service
android:name=".Test.CheckAppRunService"
android:name=".Pedometer.CheckAppRunService"
android:enabled="true"
android:exported="true" />

Expand All @@ -33,7 +35,7 @@
android:enabled="true"
android:exported="true" />

<receiver android:name=".Test.NotiReceiver" />
<receiver android:name=".Pedometer.MeetGoalReceiver" />

<activity
android:name=".stepcounter.ProgressActivity"
Expand Down
20 changes: 7 additions & 13 deletions app/src/main/java/neu/madcourse/walkwithme/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@

import com.google.android.material.bottomnavigation.BottomNavigationView;

import neu.madcourse.walkwithme.Test.CheckAppRunService;
import neu.madcourse.walkwithme.Pedometer.CheckAppRunService;
import neu.madcourse.walkwithme.profile.ProfileActivity;
import neu.madcourse.walkwithme.profile.ProfileFragment;
import neu.madcourse.walkwithme.ranking.RankingActivity;
import neu.madcourse.walkwithme.Test.Constants;
import neu.madcourse.walkwithme.Test.StepService3;
import neu.madcourse.walkwithme.Test.StepsFragment2;
import neu.madcourse.walkwithme.Pedometer.Constants;
import neu.madcourse.walkwithme.Pedometer.StepService3;
import neu.madcourse.walkwithme.Pedometer.StepsFragment2;

import neu.madcourse.walkwithme.NotiPet.PetActivity;

import neu.madcourse.walkwithme.rankingFra.RankFragment;
import neu.madcourse.walkwithme.stepcounter.ProgressActivity;
import neu.madcourse.walkwithme.userlog.LoginActivity;
import neu.madcourse.walkwithme.userlog.LogoutFragment;

Expand Down Expand Up @@ -136,6 +135,8 @@ public void onClick(View view) {
Intent login = new Intent(this, LoginActivity.class);
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(1); //close all notification for current users
notificationManager.cancel(2); //level
notificationManager.cancel(3); //steps
startActivity(login);
break;
case R.id.not_logout:
Expand All @@ -145,10 +146,6 @@ public void onClick(View view) {
}
}

public void startProgressActivity(View view){
startActivity(new Intent(MainActivity.this, ProgressActivity.class));
}

public void startStepsFragment(View view){
//startActivity(new Intent(MainActivity.this, StepsFragment2.class));
showFragment(new StepsFragment2());
Expand All @@ -160,17 +157,14 @@ private void showFragment(Fragment frag) {
fragmentTransaction.replace(R.id.nav_host_fragment, frag).commit();
manager.executePendingTransactions();
}

@Override
protected void onStop() {
super.onStop();
editor.putLong("preRun", System.currentTimeMillis());
editor.commit();
Log.v(TAG, "On Stop, Starting CheckRecentRun service...");
startService(new Intent(this, CheckAppRunService.class));
// Intent startIntent = new Intent(this, StepService3.class);
// startIntent.setAction(Constants.START_FOREGROUND);
// startService(startIntent);

}

@Override
Expand Down
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;
}
}
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";
}
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;
}
}
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());
}
}
}
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);

}
Loading

0 comments on commit bc0135c

Please sign in to comment.