1919import android .content .Intent ;
2020import android .os .AsyncTask ;
2121import 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 ;
2922import android .text .TextUtils ;
3023import android .util .Log ;
3124import android .view .LayoutInflater ;
3528import android .webkit .WebView ;
3629import android .widget .ArrayAdapter ;
3730import 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 ;
4041import 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+
4149import java .io .IOException ;
4250import java .util .ArrayList ;
4351import java .util .Collections ;
4452import java .util .Comparator ;
4553import java .util .List ;
54+
4655import javax .xml .parsers .DocumentBuilder ;
4756import javax .xml .parsers .DocumentBuilderFactory ;
4857import 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. */
5565public 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
0 commit comments