From e0ca1627e71b626981149a855207007e866294af Mon Sep 17 00:00:00 2001 From: Pavle Portic Date: Thu, 17 Jan 2019 00:45:59 +0100 Subject: [PATCH] Stop query request while app isn't active --- app/build.gradle | 12 +-- .../theedgeofrage/i3control/MainActivity.java | 82 +++++++++++++------ 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c7a5b5e..4e07530 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 27 + compileSdkVersion 28 defaultConfig { - applicationId "me.xor_hydrogen.i3control" + applicationId "com.theedgeofrage.i3control" minSdkVersion 27 - targetSdkVersion 27 - versionCode 1 - versionName "1.0" + targetSdkVersion 28 + versionCode 2 + versionName "1.0.1" vectorDrawables.useSupportLibrary true } buildTypes { @@ -20,7 +20,7 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'com.android.support:appcompat-v7:27.1.1' + implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.volley:volley:1.1.0' } diff --git a/app/src/main/java/com/theedgeofrage/i3control/MainActivity.java b/app/src/main/java/com/theedgeofrage/i3control/MainActivity.java index 6eef936..670e39c 100644 --- a/app/src/main/java/com/theedgeofrage/i3control/MainActivity.java +++ b/app/src/main/java/com/theedgeofrage/i3control/MainActivity.java @@ -24,6 +24,44 @@ import org.json.JSONException; import org.json.JSONObject; public class MainActivity extends AppCompatActivity { + class RunnableQuery implements Runnable { + private Handler mHandler; + private boolean mRun = false; + private MainActivity context; + + private RunnableQuery(MainActivity context) { + mHandler = new Handler(getMainLooper()); + this.context = context; + } + + private void start() { + if (mRun) { + return; + } + mRun = true; + mHandler.postDelayed(this, 1000); + } + + void stop() { + if (!mRun) { + return; + } + mRun = false; + mHandler.removeCallbacks(this); + } + + @Override + public void run() { + if (!mRun) { + return; + } + context.sendRequest("query", context); + mHandler.postDelayed(this, 1000); + } + } + + RunnableQuery runnableQuery; + public void sendRequest(String endpoint, final Context context) { RequestQueue queue = Volley.newRequestQueue(context); @@ -119,35 +157,25 @@ public class MainActivity extends AppCompatActivity { findViewById(R.id.nextButton).setOnClickListener(buttonClickListener); findViewById(R.id.shuffleButton).setOnClickListener(buttonClickListener); - class RunnableQuery implements Runnable { - private Handler mHandler; - private boolean mRun = false; + this.runnableQuery = new RunnableQuery(this); + this.runnableQuery.start(); + } - RunnableQuery() { - mHandler = new Handler(getMainLooper()); - } + @Override + protected void onResume() { + super.onResume(); + this.runnableQuery.start(); + } - public void start() { - mRun = true; - mHandler.postDelayed(this, 1000); - } + @Override + protected void onStop() { + super.onStop(); + this.runnableQuery.stop(); + } - void stop() { - mRun = false; - mHandler.removeCallbacks(this); - } - - @Override - public void run() { - if (!mRun) { - return; - } - ((MainActivity) context).sendRequest("query", context); - mHandler.postDelayed(this, 1000); - } - } - - RunnableQuery runnableQuery = new RunnableQuery(); - runnableQuery.start(); + @Override + protected void onPause() { + super.onPause(); + this.runnableQuery.stop(); } }