Skip to content

Commit f4e53d7

Browse files
committed
Merge branch '1-counter-falls-back'
2 parents d7c09cb + 44f54c5 commit f4e53d7

File tree

17 files changed

+109
-195
lines changed

17 files changed

+109
-195
lines changed

.idea/gradle.xml

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/example/stickbug/Main/MainActivity.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
package com.example.stickbug.Main;
22

33
import androidx.appcompat.app.AppCompatActivity;
4-
import androidx.core.app.NotificationCompat;
54

65
import android.annotation.TargetApi;
7-
import android.app.Notification;
8-
import android.app.NotificationChannel;
9-
import android.app.NotificationManager;
10-
import android.app.PendingIntent;
116
import android.content.BroadcastReceiver;
127
import android.content.Context;
138
import android.content.Intent;
149
import android.content.IntentFilter;
15-
import android.media.MediaPlayer;
16-
import android.os.Build;
1710
import android.os.Bundle;
1811
import android.widget.TextView;
19-
import android.widget.Toast;
2012

2113
import com.example.stickbug.Background.Background;
2214
import com.example.stickbug.R;
@@ -30,8 +22,6 @@ public class MainActivity extends AppCompatActivity {
3022
boolean appOpen = false;
3123
TextView textView;
3224
Savedata savedata;
33-
MediaPlayer mediaPlayer;
34-
Background background;
3525

3626
@Override
3727
protected void onCreate(Bundle savedInstanceState) {
@@ -40,7 +30,10 @@ protected void onCreate(Bundle savedInstanceState) {
4030
}
4131

4232

43-
33+
private void refresh() {
34+
savedata = new Savedata(new File(this.getFilesDir(), "yes.prop"));
35+
repeats = savedata.getRepeats();
36+
}
4437

4538

4639
private BroadcastReceiver receiver = new BroadcastReceiver() {
@@ -50,9 +43,11 @@ public void onReceive(Context context, Intent intent) {
5043
Bundle bundle = intent.getExtras();
5144
if (bundle != null) {
5245
String check = bundle.getString(Background.REQUEST);
53-
int Repeats = bundle.getInt(Background.REPEATS);
46+
int Repeats = bundle.getInt(Background.REPEATS, -1);
5447
repeats = Repeats;
55-
savedata.saveRepeats(repeats);
48+
if (Repeats != -1) {
49+
savedata.saveRepeats(repeats);
50+
}
5651
if (appOpen) {
5752
textView.setText("x" + repeats);
5853
}
@@ -67,6 +62,7 @@ public void onReceive(Context context, Intent intent) {
6762
protected void onResume() {
6863
super.onResume();
6964
appOpen = true;
65+
refresh();
7066
textView.setText("x" + repeats);
7167
}
7268
@Override
@@ -84,8 +80,7 @@ protected void onStart() {
8480
super.onStart();
8581
appOpen = true;
8682
registerReceiver(receiver, new IntentFilter(Background.PACKAGE));
87-
savedata = new Savedata(new File(this.getFilesDir(), "yes.prop"));
88-
repeats = savedata.getRepeats();
83+
refresh();
8984
textView = findViewById(R.id.times);
9085
textView.setText("x" + repeats);
9186
Intent intent = new Intent(this, Background.class);

app/src/main/java/com/example/stickbug/background/Background.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import android.os.Bundle;
1313
import android.os.IBinder;
1414
import android.util.Log;
15-
import android.widget.Toast;
1615

1716
import androidx.core.app.NotificationCompat;
1817
import androidx.core.app.NotificationManagerCompat;
1918

2019
import com.example.stickbug.R;
20+
import com.example.stickbug.Savedata.Savedata;
21+
22+
import java.io.File;
2123

2224

2325
public class Background extends Service {
@@ -28,15 +30,17 @@ public class Background extends Service {
2830
public static final String REPEATS = "Repeats";
2931
public static final String TOTAL_REPEATS = "Total Repeats";
3032
public static final String APP_STATE = "App State";
31-
public static final String APP_CLOSED = "Closed";
3233
public static final String APP_OPENED = "Opened";
3334
public static final String STOPPLZ = "Stop";
3435

3536
private int repeats;
3637
private boolean playing = false;
3738
NotificationManagerCompat managerCompat;
3839

39-
40+
private void refresh() {
41+
Savedata savedata = new com.example.stickbug.Savedata.Savedata(new File(this.getFilesDir(), "yes.prop"));
42+
savedata.saveRepeats(repeats);
43+
}
4044

4145
@Override
4246
public void onCreate() {
@@ -73,7 +77,7 @@ public Notification createNotification() {
7377
PendingIntent stopPending = PendingIntent.getForegroundService(this, 0, stopIntent, 0);
7478

7579
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "yes")
76-
.setSmallIcon(R.drawable.stickbug)
80+
.setSmallIcon(R.drawable.sb)
7781
.setContentTitle("You are being Stick Bugged")
7882
.setContentText("Song has played a total of x" + repeats)
7983
.setContentIntent(pendingIntent)
@@ -92,13 +96,15 @@ public int onStartCommand(Intent intent, int flags, int startId) {
9296
String request = "";
9397
String appState = "";
9498
try {
99+
assert incoming != null;
95100
request = incoming.getString(REQUEST);
96101
appState = incoming.getString(APP_STATE);
97102

98103
}catch (NullPointerException e) {
99104
e.printStackTrace();
100105
}
101106
try {
107+
assert request != null;
102108
if (request.equals(REPEATS)) {
103109
returnCounter();
104110
}
@@ -110,12 +116,11 @@ public int onStartCommand(Intent intent, int flags, int startId) {
110116
//System.exit(0);
111117
stopSelf();
112118
}
119+
assert appState != null;
113120
if (appState.equals(APP_OPENED)) {
114121
managerCompat.cancel(0);
115122
}
116-
if (appState.equals(APP_CLOSED)) {
117-
//managerCompat.notify(0, createNotification());
118-
}
123+
//managerCompat.notify(0, createNotification());
119124
}catch (NullPointerException e) {
120125
e.printStackTrace();
121126
Log.i("Error", "Error was sort of expected.");
@@ -152,6 +157,7 @@ public void onCompletion(MediaPlayer mediaPlayer) {
152157
}
153158

154159
private void returnCounter() {
160+
refresh();
155161
Intent intent = new Intent(PACKAGE);
156162
intent.putExtra(REPEATS, repeats);
157163
sendBroadcast(intent);

0 commit comments

Comments
 (0)