|
| 1 | +/* |
| 2 | + * Copyright 2018 akaita |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
1 | 17 | package com.akaita.java.rxjava2debug.exampleandroid |
2 | 18 |
|
3 | | -import android.support.v7.app.AppCompatActivity |
4 | 19 | import android.os.Bundle |
| 20 | +import android.support.v7.app.AppCompatActivity |
| 21 | +import android.text.Html |
| 22 | +import android.util.Log |
| 23 | +import com.akaita.java.rxjava2debug.RxJava2Debug |
| 24 | +import io.reactivex.Single |
| 25 | +import io.reactivex.android.schedulers.AndroidSchedulers |
| 26 | +import io.reactivex.functions.Consumer |
| 27 | +import io.reactivex.schedulers.Schedulers |
| 28 | +import kotlinx.android.synthetic.main.activity_main.* |
5 | 29 |
|
6 | 30 | class MainActivity : AppCompatActivity() { |
7 | 31 |
|
8 | 32 | override fun onCreate(savedInstanceState: Bundle?) { |
9 | 33 | super.onCreate(savedInstanceState) |
10 | 34 | setContentView(R.layout.activity_main) |
| 35 | + |
| 36 | + toggleAssembly.setOnCheckedChangeListener({ _, isChecked -> toggleRxJava2Debug(isChecked) }) |
| 37 | + handledException.setOnClickListener { generateHandledException() } |
| 38 | + unhandledException.setOnClickListener { generateUnhandledException() } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Toggle RxJava2Debug on and off. |
| 43 | + */ |
| 44 | + private fun toggleRxJava2Debug( enable: Boolean ) { |
| 45 | + if (enable) { |
| 46 | + RxJava2Debug.enableRxJava2AssemblyTracking(ExampleApplication.MY_CODE_PACKAGES) |
| 47 | + } else { |
| 48 | + RxJava2Debug.disableRxJava2AssemblyTracking() |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Generates an exception in a computation thread, by inserting a `null` event into a stream |
| 54 | + * |
| 55 | + * By handling this exception in `onError`, it prevents the app from crashing. |
| 56 | + * |
| 57 | + * It handles the exception by printing it in LogCat and the screen, so the developer can fix it |
| 58 | + */ |
| 59 | + private fun generateHandledException() { |
| 60 | + Single.just("event") |
| 61 | + .subscribeOn(Schedulers.computation()) |
| 62 | + .doOnEvent { _, _ -> Log.i("HandledException", "Start") } |
| 63 | + .map { null } |
| 64 | + .observeOn(AndroidSchedulers.mainThread()) |
| 65 | + .subscribe( |
| 66 | + { Log.i("HandledException", "Subscribe") }, |
| 67 | + { t: Throwable -> |
| 68 | + val enhancedStackTrace = RxJava2Debug.getEnhancedStackTrace(t) |
| 69 | + Log.e("HandledException", "Error", enhancedStackTrace) |
| 70 | + displayStackTrace(enhancedStackTrace) |
| 71 | + } |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Generates an exception in a computation thread, by inserting a `null` event into a stream |
| 77 | + * |
| 78 | + * No implementation for `onError` is provided, so the exception will be thrown and it will eventually crash the app O_o |
| 79 | + */ |
| 80 | + private fun generateUnhandledException() { |
| 81 | + Single.just("event") |
| 82 | + .subscribeOn(Schedulers.computation()) |
| 83 | + .doOnEvent { _, _ -> Log.i("HandledException", "Start") } |
| 84 | + .map { null } |
| 85 | + .observeOn(AndroidSchedulers.mainThread()) |
| 86 | + .subscribe(Consumer { Log.i("UnhandledException", "Subscribe") }) |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Obtain an HTML version of the stacktrace, so it looks nice on the screen |
| 91 | + */ |
| 92 | + private fun displayStackTrace(throwable: Throwable?) { |
| 93 | + textView.text = Html.fromHtml( |
| 94 | + throwable |
| 95 | + ?.toHtml() |
| 96 | + ?.highlight(ExampleApplication.MY_CODE_PACKAGES) ?: "Something horrible happened!") // |
11 | 97 | } |
12 | 98 | } |
0 commit comments