Skip to content

Commit

Permalink
Improved BitCoinApp speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed Mar 8, 2018
1 parent 4da78d3 commit d88a73e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 69 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Expand Up @@ -7,7 +7,7 @@ android {
applicationId "javinator9889.bitcoinpools"
minSdkVersion 21
targetSdkVersion 27
versionCode 59
versionCode 60
versionName "1.18"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Expand Down
Binary file modified app/release/BitCoinPools-1.18.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/release/output.json
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":59},"path":"app-release.apk","properties":{"packageId":"javinator9889.bitcoinpools","split":"","minSdkVersion":"21"}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":60},"path":"app-release.apk","properties":{"packageId":"javinator9889.bitcoinpools","split":"","minSdkVersion":"21"}}]
146 changes: 79 additions & 67 deletions app/src/main/java/javinator9889/bitcoinpools/BitCoinApp.java
Expand Up @@ -11,7 +11,6 @@
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.NonNull;
import android.util.Log;

import com.crashlytics.android.Crashlytics;
Expand Down Expand Up @@ -41,7 +40,6 @@ 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;
Expand All @@ -59,81 +57,95 @@ public void onCreate() {
Context.MODE_PRIVATE);
initSharedPreferences();
try {
isCacheCreated = CacheManaging.newInstance(this).setupFile();
} catch (IOException e) {
isCacheCreated = false;
}
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() {
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();
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() {
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();
}
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() {
Expand Down

0 comments on commit d88a73e

Please sign in to comment.