-
Notifications
You must be signed in to change notification settings - Fork 2
/
BitCoinApp.java
137 lines (117 loc) · 6.14 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
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.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javinator9889.bitcoinpools.BackgroundJobs.CacheJobSchedulerService;
import javinator9889.bitcoinpools.BackgroundJobs.JobSchedulerService;
/**
* 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;
private static boolean isCacheCreated = false;
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 {
isCacheCreated = CacheManaging.newInstance(this).setupFile();
} catch (IOException e) {
isCacheCreated = false;
}
startBackgroundJobs();
super.onCreate();
Log.d(Constants.LOG.BCTAG, Constants.LOG.CREATED_APP);
}
private static void startBackgroundJobs() {
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 (!(SHARED_PREFERENCES.getBoolean(Constants.SHARED_PREFERENCES.CACHE_JOB, false)) || isCacheCreated) {
JobScheduler cacheJobScheduler = (JobScheduler) APPLICATION_CONTEXT.getSystemService(Context.JOB_SCHEDULER_SERVICE);
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);
assert cacheJobScheduler != null;
if (cacheJobScheduler.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();
}
}
}
private void initSharedPreferences() {
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();
}
}
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";
}
}
}