/
BitCoinApp.java
210 lines (188 loc) · 8.86 KB
/
BitCoinApp.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package javinator9889.bitcoinpools;
import android.annotation.SuppressLint;
import android.app.Application;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import com.crashlytics.android.Crashlytics;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import javinator9889.bitcoinpools.BackgroundJobs.CacheJobSchedulerService;
import javinator9889.bitcoinpools.BackgroundJobs.JobSchedulerService;
import static javinator9889.bitcoinpools.Constants.MILLIS_A_DAY;
/**
* Created by Javinator9889 on 22/01/2018.
* Based on: https://github.com/ZonaRMR/SimpleForFacebook/blob/master/app/src/main/java/com/creativetrends/simple/app/activities/SimpleApp.java
*/
public class BitCoinApp extends Application {
@SuppressLint("StaticFieldLeak")
private static Context APPLICATION_CONTEXT;
private static SharedPreferences SHARED_PREFERENCES;
public static Context getAppContext() {
return APPLICATION_CONTEXT;
}
public static SharedPreferences getSharedPreferences() {
return SHARED_PREFERENCES;
}
@Override
public void onCreate() {
APPLICATION_CONTEXT = getApplicationContext();
SHARED_PREFERENCES = getSharedPreferences(
Constants.SHARED_PREFERENCES.SHARED_PREFERENCES_KEY,
Context.MODE_PRIVATE);
initSharedPreferences();
try {
CacheManaging.newInstance(this).setupFile();
} catch (IOException ignored) {} // This error should never happen
startBackgroundJobs();
super.onCreate();
Log.d(Constants.LOG.BCTAG, Constants.LOG.CREATED_APP);
}
private static void startBackgroundJobs() {
Thread backgroundJobsThread = new Thread(new Runnable() {
@Override
public void run() {
JobScheduler mJobScheduler = (JobScheduler) APPLICATION_CONTEXT
.getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo.Builder builder = new JobInfo.Builder(
1,
new ComponentName(APPLICATION_CONTEXT.getPackageName(),
JobSchedulerService.class.getName()));
builder.setPeriodic(Constants.SCHEDULING_TIME);
builder.setPersisted(Constants.PERSISTED);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
builder.setBackoffCriteria(Constants.BACKOFF_CRITERIA,
JobInfo.BACKOFF_POLICY_LINEAR);
assert mJobScheduler != null;
if (mJobScheduler.schedule(builder.build()) == JobScheduler.RESULT_FAILURE) {
Log.e(Constants.LOG.BCTAG,
Constants.LOG.NO_INIT + "JobScheduler"
+ mJobScheduler.getAllPendingJobs().toString());
}
if (isJobCreationNeeded(mJobScheduler)) {
JobInfo.Builder cacheBuilder = new JobInfo.Builder(
2,
new ComponentName(APPLICATION_CONTEXT.getPackageName(),
CacheJobSchedulerService.class.getName()));
cacheBuilder.setPeriodic(TimeUnit.DAYS.toMillis(1));
cacheBuilder.setPersisted(Constants.PERSISTED);
cacheBuilder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
cacheBuilder.setBackoffCriteria(Constants.BACKOFF_CRITERIA,
JobInfo.BACKOFF_POLICY_LINEAR);
if (mJobScheduler.schedule(cacheBuilder.build()) == JobScheduler
.RESULT_FAILURE)
{
Log.e(Constants.LOG.BCTAG,
Constants.LOG.NO_INIT + "JobScheduler" + mJobScheduler
.getAllPendingJobs().toString());
} else {
SharedPreferences.Editor newValueForStartedJob = SHARED_PREFERENCES.edit();
newValueForStartedJob.putBoolean(Constants.SHARED_PREFERENCES.CACHE_JOB,
true);
newValueForStartedJob.apply();
}
}
}
});
backgroundJobsThread.start();
}
private void initSharedPreferences() {
Thread sharedPreferencesThread = new Thread(new Runnable() {
@Override
public void run() {
if (!SHARED_PREFERENCES.contains(
Constants.SHARED_PREFERENCES.SHARED_PREFERENCES_INITIALIZED))
{
Log.d(Constants.LOG.BCTAG, Constants.LOG.INIT_PREF);
SharedPreferences.Editor sharedPreferencesEditor = SHARED_PREFERENCES.edit();
sharedPreferencesEditor.putBoolean(
Constants.SHARED_PREFERENCES.SHARED_PREFERENCES_INITIALIZED, true);
sharedPreferencesEditor.putBoolean(
Constants.SHARED_PREFERENCES.NOTIFICATIONS_ENABLED, false);
sharedPreferencesEditor.putBoolean(
Constants.SHARED_PREFERENCES.NOTIFIED_LOW, false);
sharedPreferencesEditor.putBoolean(
Constants.SHARED_PREFERENCES.NOTIFIED_HIGH, false);
sharedPreferencesEditor.putInt(
Constants.SHARED_PREFERENCES.DAYS_TO_CHECK, 1);
sharedPreferencesEditor.putInt(
Constants.SHARED_PREFERENCES.VALUE_TO_CHECK, 1000);
sharedPreferencesEditor.putString(
Constants.SHARED_PREFERENCES.APP_VERSION, appVersion());
sharedPreferencesEditor.apply();
}
}
});
sharedPreferencesThread.start();
}
public static void forceRestartBackgroundJobs() {
Log.d(Constants.LOG.BCTAG, Constants.LOG.RESTART_JOB);
JobScheduler jobScheduler = (JobScheduler) APPLICATION_CONTEXT
.getSystemService(Context.JOB_SCHEDULER_SERVICE);
assert jobScheduler != null;
jobScheduler.cancelAll();
startBackgroundJobs();
}
public static boolean isOnline() {
ConnectivityManager connectionManager = (ConnectivityManager) getAppContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
assert connectionManager != null;
NetworkInfo netInfo = connectionManager.getActiveNetworkInfo();
return ((netInfo != null) && netInfo.isConnected());
}
public static String appVersion() {
try {
PackageInfo pInfo = getAppContext().getPackageManager()
.getPackageInfo(getAppContext().getPackageName(), 0);
return pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
Crashlytics.logException(e);
return "1.0";
}
}
public static boolean isJobCreationNeeded(JobScheduler applicationJobScheduler) {
if (SHARED_PREFERENCES.contains(Constants.SHARED_PREFERENCES.CACHE_JOB)) {
List<JobInfo> pendingJobs = applicationJobScheduler.getAllPendingJobs();
Iterator<JobInfo> currentPendingJob = pendingJobs.iterator();
boolean equals = false;
while (currentPendingJob.hasNext() && !equals) {
JobInfo pending = currentPendingJob.next();
equals = pending.toString().equals(Constants.JOBINFO);
}
long timeDiff = timeDifference();
return timeDiff >= MILLIS_A_DAY && !equals;
} else {
SharedPreferences.Editor newEntry = SHARED_PREFERENCES.edit();
newEntry.putBoolean(Constants.SHARED_PREFERENCES.CACHE_JOB, false);
newEntry.apply();
return true;
}
}
private static long timeDifference() {
CacheManaging cache = CacheManaging.newInstance(APPLICATION_CONTEXT);
try {
HashMap<String, String> cachedValues = cache.readCache();
String date = cachedValues.get("date");
DateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.US);
Date dateInCache = format.parse(date);
return (Calendar.getInstance().getTime().getTime() - dateInCache.getTime());
} catch (Exception e) {
return Long.MAX_VALUE;
}
}
}