/
NotificationHandler.java
150 lines (135 loc) · 7.1 KB
/
NotificationHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package javinator9889.bitcoinpools.BackgroundJobs;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;
import org.json.JSONException;
import java.util.concurrent.ExecutionException;
import javinator9889.bitcoinpools.BitCoinApp;
import javinator9889.bitcoinpools.Constants;
import javinator9889.bitcoinpools.MainActivity;
import javinator9889.bitcoinpools.NetTools.net;
import javinator9889.bitcoinpools.R;
/**
* Created by Javinator9889 on 23/01/2018.
* Based on: https://stackoverflow.com/a/46991229
*/
class NotificationHandler {
private static boolean NOTIFICATIONS_ENABLED = false;
private static boolean NOTIFIED_HIGH = false;
private static boolean NOTIFIED_LOW = false;
private static int SPECIFIC_VALUE = 0;
private static float MPU;
private NotificationHandler() {
final SharedPreferences sp = BitCoinApp.getSharedPreferences();
Log.d(Constants.LOG.NTAG, Constants.LOG.CREATING_NOTIFICATION);
NOTIFICATIONS_ENABLED = sp.getBoolean(Constants.SHARED_PREFERENCES.NOTIFICATIONS_ENABLED, false);
NOTIFIED_HIGH = sp.getBoolean(Constants.SHARED_PREFERENCES.NOTIFIED_HIGH, false);
NOTIFIED_LOW = sp.getBoolean(Constants.SHARED_PREFERENCES.NOTIFIED_LOW, false);
SPECIFIC_VALUE = sp.getInt(Constants.SHARED_PREFERENCES.VALUE_TO_CHECK, 1000);
MPU = initMPU();
Log.d(Constants.LOG.NTAG, Constants.LOG.CURRRENT_NOT_SETTINGS
+ NOTIFICATIONS_ENABLED + "\n"
+ NOTIFIED_HIGH + "\n"
+ NOTIFIED_LOW + "\n"
+ SPECIFIC_VALUE + "\n"
+ MPU);
}
void putNotification() {
boolean notify = false;
String notificationTitle = "";
String notificationText = "";
String notificationTextLong = "";
NotificationManager notificationManager = (NotificationManager) BitCoinApp.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent clickIntent = PendingIntent.getActivity(
BitCoinApp.getAppContext(),
Constants.REQUEST_CODE,
new Intent(BitCoinApp.getAppContext(), MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT
);
if (NOTIFICATIONS_ENABLED) {
if ((MPU < SPECIFIC_VALUE) && !NOTIFIED_LOW) {
notificationTitle = BitCoinApp.getAppContext().getString(R.string.lowerPrice);
notificationTextLong = BitCoinApp.getAppContext().getString(R.string.lowerPriceX) + SPECIFIC_VALUE + ". " + BitCoinApp.getAppContext().getString(R.string.actualCost) + MPU;
notificationText = BitCoinApp.getAppContext().getString(R.string.lowerPriceX) + SPECIFIC_VALUE;
NOTIFIED_HIGH = false;
NOTIFIED_LOW = true;
//notify = true;
notify = (MPU != -1);
} else if ((MPU > SPECIFIC_VALUE) && !NOTIFIED_HIGH) {
notificationTitle = BitCoinApp.getAppContext().getString(R.string.morePrice);
notificationTextLong = BitCoinApp.getAppContext().getString(R.string.morePriceX) + SPECIFIC_VALUE + ". " + BitCoinApp.getAppContext().getString(R.string.actualCost) + MPU;
notificationText = BitCoinApp.getAppContext().getString(R.string.morePriceX) + SPECIFIC_VALUE;
NOTIFIED_HIGH = true;
NOTIFIED_LOW = false;
//notify = true;
notify = (MPU != -1);
}
if (notify) {
Log.d(Constants.LOG.NTAG, Constants.LOG.NOTIFYING);
String name = BitCoinApp.getAppContext().getString(R.string.alerts);
String description = BitCoinApp.getAppContext().getString(R.string.description);
Notification.Builder notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = android.app.NotificationManager.IMPORTANCE_HIGH;
assert notificationManager != null;
NotificationChannel mChannel = notificationManager.getNotificationChannel(Constants.CHANNEL_ID);
if (mChannel == null) {
mChannel = new NotificationChannel(Constants.CHANNEL_ID, name, importance);
mChannel.setDescription(description);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 100, 300});
notificationManager.createNotificationChannel(mChannel);
}
notification = new Notification.Builder(BitCoinApp.getAppContext(), Constants.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_stat_equalizer)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setAutoCancel(true)
.setTicker(notificationTitle)
.setStyle(new Notification.BigTextStyle().bigText(notificationTextLong));
} else {
notification = new Notification.Builder(BitCoinApp.getAppContext())
.setSmallIcon(R.drawable.ic_stat_equalizer)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setAutoCancel(true)
.setTicker(notificationTitle)
.setStyle(new Notification.BigTextStyle().bigText(notificationTextLong));
}
notification.setContentIntent(clickIntent);
assert notificationManager != null;
notificationManager.notify(Constants.NOTIFICATION_ID, notification.build());
} else
Log.d(Constants.LOG.NTAG, Constants.LOG.NNOTIFYING);
}
}
static NotificationHandler newInstance() {
return new NotificationHandler();
}
private static float initMPU() {
net market = new net();
market.execute("https://api.blockchain.info/stats");
try {
return MainActivity.round((float) market.get().getDouble("market_price_usd"), 2);
} catch (InterruptedException | ExecutionException | JSONException | NullPointerException e) {
return -1;
}
}
void updatePreferences() {
updateSharedPreferences();
}
private static void updateSharedPreferences() {
final SharedPreferences.Editor sharedPreferencesEditor = BitCoinApp.getSharedPreferences().edit();
sharedPreferencesEditor.putBoolean(Constants.SHARED_PREFERENCES.NOTIFIED_HIGH, NOTIFIED_HIGH);
sharedPreferencesEditor.putBoolean(Constants.SHARED_PREFERENCES.NOTIFIED_LOW, NOTIFIED_LOW);
sharedPreferencesEditor.apply();
}
}