Skip to content

Commit 70faa91

Browse files
Replace AsyncTask with a rxjava powered use case
1 parent 2b1d527 commit 70faa91

File tree

4 files changed

+122
-111
lines changed

4 files changed

+122
-111
lines changed

OpenScienceJournal/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ dependencies {
5959
implementation deps.appCompat
6060

6161
implementation deps.rxJava
62+
implementation deps.rxAndroid
6263
implementation(deps.guava) {
6364
// Avoid error "Program type already present: javax.annotation.CheckForNull"
6465
exclude group: 'com.google.code.findbugs'

OpenScienceJournal/app/src/main/java/com/google/android/apps/forscience/whistlepunk/opensource/licenses/LicenseActivity.java

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@
1919
import android.content.Intent;
2020
import android.os.AsyncTask;
2121
import android.os.Bundle;
22-
import androidx.annotation.Nullable;
23-
import androidx.fragment.app.Fragment;
24-
import androidx.fragment.app.FragmentTransaction;
25-
import androidx.fragment.app.ListFragment;
26-
import androidx.core.app.NavUtils;
27-
import androidx.appcompat.app.ActionBar;
28-
import androidx.appcompat.app.AppCompatActivity;
2922
import android.text.TextUtils;
3023
import android.util.Log;
3124
import android.view.LayoutInflater;
@@ -35,21 +28,38 @@
3528
import android.webkit.WebView;
3629
import android.widget.ArrayAdapter;
3730
import android.widget.ListView;
38-
import com.google.android.apps.forscience.whistlepunk.LoadStaticHtmlTask;
39-
import com.google.android.apps.forscience.whistlepunk.opensource.R;
31+
32+
import androidx.annotation.Nullable;
33+
import androidx.appcompat.app.ActionBar;
34+
import androidx.appcompat.app.AppCompatActivity;
35+
import androidx.core.app.NavUtils;
36+
import androidx.fragment.app.Fragment;
37+
import androidx.fragment.app.FragmentTransaction;
38+
import androidx.fragment.app.ListFragment;
39+
40+
import com.google.android.apps.forscience.whistlepunk.LoadStaticHtmlTaskUseCase;
4041
import com.google.android.apps.forscience.whistlepunk.SettingsActivity;
42+
import com.google.android.apps.forscience.whistlepunk.opensource.R;
43+
44+
import org.w3c.dom.Document;
45+
import org.w3c.dom.Node;
46+
import org.w3c.dom.NodeList;
47+
import org.xml.sax.SAXException;
48+
4149
import java.io.IOException;
4250
import java.util.ArrayList;
4351
import java.util.Collections;
4452
import java.util.Comparator;
4553
import java.util.List;
54+
4655
import javax.xml.parsers.DocumentBuilder;
4756
import javax.xml.parsers.DocumentBuilderFactory;
4857
import javax.xml.parsers.ParserConfigurationException;
49-
import org.w3c.dom.Document;
50-
import org.w3c.dom.Node;
51-
import org.w3c.dom.NodeList;
52-
import org.xml.sax.SAXException;
58+
59+
import io.reactivex.android.schedulers.AndroidSchedulers;
60+
import io.reactivex.annotations.NonNull;
61+
import io.reactivex.observers.DisposableSingleObserver;
62+
import io.reactivex.schedulers.Schedulers;
5363

5464
/** Displays list of code modules. */
5565
public class LicenseActivity extends AppCompatActivity {
@@ -190,6 +200,8 @@ public static class LicenseFragment extends Fragment {
190200

191201
private WebView webView;
192202

203+
private final LoadStaticHtmlTaskUseCase loadStaticHtmlTaskUseCase = new LoadStaticHtmlTaskUseCase(Schedulers.io(), AndroidSchedulers.mainThread());
204+
193205
public LicenseFragment() {}
194206

195207
public static LicenseFragment newInstance(License license) {
@@ -211,27 +223,31 @@ public View onCreateView(
211223
return view;
212224
}
213225

226+
@Override
227+
public void onDestroyView() {
228+
super.onDestroyView();
229+
loadStaticHtmlTaskUseCase.discard();
230+
}
231+
214232
@Override
215233
public void onStart() {
216234
super.onStart();
217-
new LoadStaticHtmlTask(
218-
new LoadStaticHtmlTask.StaticHtmlLoadListener() {
219-
@Override
220-
public void onDataLoaded(String data) {
221-
String copyrightHeader = getArguments().getString(TAG_HEADER);
222-
if (!TextUtils.isEmpty(copyrightHeader)) {
223-
data = data.replace(COPYRIGHT_HEADER_PLACEHOLDER, copyrightHeader);
224-
}
225-
webView.loadData(data, "text/html", "UTF-8");
226-
}
227-
},
228-
getResources(),
229-
getResources()
230-
.getIdentifier(
231-
getArguments().getString(TAG_RESOURCE),
232-
"raw",
233-
getActivity().getPackageName()))
234-
.execute();
235+
int fileId = getResources().getIdentifier(getArguments().getString(TAG_RESOURCE), "raw", requireActivity().getPackageName());
236+
loadStaticHtmlTaskUseCase.invoke(getResources(), fileId, new DisposableSingleObserver<String>() {
237+
@Override
238+
public void onSuccess(@NonNull String data) {
239+
String copyrightHeader = getArguments().getString(TAG_HEADER);
240+
if (!TextUtils.isEmpty(copyrightHeader)) {
241+
data = data.replace(COPYRIGHT_HEADER_PLACEHOLDER, copyrightHeader);
242+
}
243+
webView.loadData(data, "text/html", "UTF-8");
244+
}
245+
246+
@Override
247+
public void onError(@NonNull Throwable throwable) {
248+
Log.e(TAG, "loadStaticHtmlTaskUseCase", throwable);
249+
}
250+
});
235251
}
236252

237253
@Override

OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/LoadStaticHtmlTask.java

Lines changed: 0 additions & 80 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.google.android.apps.forscience.whistlepunk;
2+
3+
import android.content.res.Resources;
4+
import android.util.Log;
5+
6+
import androidx.annotation.NonNull;
7+
8+
import java.io.IOException;
9+
import java.io.InputStreamReader;
10+
11+
import io.reactivex.Scheduler;
12+
import io.reactivex.Single;
13+
import io.reactivex.disposables.CompositeDisposable;
14+
import io.reactivex.observers.DisposableSingleObserver;
15+
16+
/**
17+
* Loads static HTML files into string data.
18+
*/
19+
public class LoadStaticHtmlTaskUseCase {
20+
private static final String TAG = "LoadStaticHtmlTask";
21+
private final CompositeDisposable disposable = new CompositeDisposable();
22+
23+
private final Scheduler operationScheduler;
24+
private final Scheduler notificationScheduler;
25+
26+
public LoadStaticHtmlTaskUseCase(@NonNull Scheduler operationScheduler, @NonNull Scheduler notificationScheduler) {
27+
this.operationScheduler = operationScheduler;
28+
this.notificationScheduler = notificationScheduler;
29+
}
30+
31+
public void invoke(@NonNull Resources resources, int fileId, @NonNull DisposableSingleObserver<String> observer) {
32+
disposable.add(performOperation(resources, fileId)
33+
.subscribeOn(operationScheduler)
34+
.observeOn(notificationScheduler)
35+
.subscribeWith(observer));
36+
}
37+
38+
private Single<String> performOperation(@NonNull Resources resources, int fileId) {
39+
return Single.create(emitter -> {
40+
InputStreamReader inputReader = null;
41+
// This will store the HTML file as a string. 4kB seems comfortable: this string will
42+
// get released when we're done with this activity.
43+
StringBuilder data = new StringBuilder(4096);
44+
try {
45+
// Read the resource in 2kB chunks.
46+
char[] tmp = new char[2048];
47+
int numRead;
48+
49+
inputReader = new InputStreamReader(resources.openRawResource(fileId));
50+
while ((numRead = inputReader.read(tmp)) >= 0) {
51+
data.append(tmp, 0, numRead);
52+
}
53+
} catch (IOException e) {
54+
Log.e(TAG, "Could not read static HTML page", e);
55+
} finally {
56+
try {
57+
if (inputReader != null) {
58+
inputReader.close();
59+
}
60+
} catch (IOException e) {
61+
Log.e(TAG, "Could not close stream", e);
62+
}
63+
}
64+
if (Log.isLoggable(TAG, Log.DEBUG)) {
65+
Log.d(TAG, "Loaded string of " + data.length());
66+
}
67+
emitter.onSuccess(data.toString());
68+
});
69+
}
70+
71+
public void discard() {
72+
disposable.clear();
73+
}
74+
}

0 commit comments

Comments
 (0)