From 9cb53a2c18a0d7d537240b6836665128da6d4295 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Sat, 29 Feb 2020 18:59:26 +0100 Subject: [PATCH] Add support for annotations Annotations like @Nullable/@NonNull help with static nullability checks while annotations like @UiThread/@WorkerThread help with ensuring that certain methods are only called from the correct thread. --- app/build.gradle | 1 + app/src/main/java/ch/fixme/status/Main.java | 6 ++++++ app/src/main/java/ch/fixme/status/Net.java | 13 +++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 8946a5c..1acc1de 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -21,4 +21,5 @@ android { } dependencies { + implementation "androidx.annotation:annotation:1.1.0" } diff --git a/app/src/main/java/ch/fixme/status/Main.java b/app/src/main/java/ch/fixme/status/Main.java index 798425a..751e288 100644 --- a/app/src/main/java/ch/fixme/status/Main.java +++ b/app/src/main/java/ch/fixme/status/Main.java @@ -59,6 +59,8 @@ import java.util.Set; import java.util.regex.Pattern; +import androidx.annotation.UiThread; + public class Main extends Activity { // API: https://spaceapi.io/ @@ -92,6 +94,7 @@ public class Main extends Activity { private GetImage getImageTask; @Override + @UiThread public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); @@ -110,12 +113,14 @@ public void onCreate(Bundle savedInstanceState) { } @Override + @UiThread protected void onNewIntent(Intent intent) { Log.d(TAG, "onNewIntent()=" + intent); showHsInfo(intent); } @Override + @UiThread protected void onDestroy() { if (getApiTask != null) { getApiTask.cancel(true); @@ -205,6 +210,7 @@ protected Dialog onCreateDialog(int id) { } @Override + @UiThread public void startActivity(Intent intent) { // http://stackoverflow.com/questions/13691241/autolink-not-working-on-htc-htclinkifydispatcher try { diff --git a/app/src/main/java/ch/fixme/status/Net.java b/app/src/main/java/ch/fixme/status/Net.java index 9b1e490..628bac3 100644 --- a/app/src/main/java/ch/fixme/status/Net.java +++ b/app/src/main/java/ch/fixme/status/Net.java @@ -18,6 +18,9 @@ import java.net.HttpURLConnection; import java.net.URL; +import androidx.annotation.NonNull; +import androidx.annotation.WorkerThread; + // From CommonsWare and Android Blog // https://github.com/commonsguy/cw-android/tree/master/Internet // http://android-developers.blogspot.ch/2010/07/multithreading-for-performance.html @@ -31,11 +34,13 @@ public class Net { private HttpURLConnection mUrlConnection; private InputStream mInputStream; - public Net(String urlStr) throws Throwable { + @WorkerThread + public Net(@NonNull String urlStr) throws Throwable { this(urlStr, true); } - public Net(String urlStr, boolean useCache) throws Throwable { + @WorkerThread + public Net(@NonNull String urlStr, boolean useCache) throws Throwable { // Connect to URL URL url; int responseCode; @@ -70,6 +75,8 @@ public Net(String urlStr, boolean useCache) throws Throwable { mInputStream = mUrlConnection.getInputStream(); } + @WorkerThread + @NonNull public String getString() throws Throwable { try { BufferedReader r = new BufferedReader(new InputStreamReader(mInputStream)); @@ -87,6 +94,8 @@ public String getString() throws Throwable { } } + @WorkerThread + @NonNull public Bitmap getBitmap() throws Throwable { try { return BitmapFactory.decodeStream(new FlushedInputStream(mInputStream));