1 package leakcanary.internal.tv 2 3 import android.annotation.SuppressLint 4 import android.app.Activity 5 import android.content.Context 6 import android.view.Gravity 7 import android.view.LayoutInflater 8 import android.widget.TextView 9 import android.widget.Toast 10 import com.squareup.leakcanary.core.R 11 12 /** 13 * Toast helper for Android TV preconfigured with LeakCanary icon. 14 * 15 * Shows toast with custom view layout, custom toast views are deprecated from API level 30 on (see 16 * [docs](https://developer.android.com/reference/android/widget/Toast#getView())) 17 */ 18 internal object TvToast { 19 20 /** 21 * Make an Android TV toast. 22 * Don't forget to call [Toast.show] to display the toast! 23 * @param activity Currently resumed [Activity] to display toast on. Note that it's not [Context] 24 * to prevent passing application context that could lead to crashes on older platforms. 25 * @param text The text to show. Can be formatted text. 26 */ 27 @SuppressLint("ShowToast") makeTextnull28 fun makeText( 29 activity: Activity, 30 text: CharSequence 31 ): Toast { 32 val inflater = LayoutInflater.from(activity) 33 val toast = Toast(activity) 34 35 toast.apply { 36 setGravity(Gravity.CENTER_VERTICAL, 0, 0) 37 duration = Toast.LENGTH_LONG 38 view = inflater.inflate(R.layout.leak_canary_heap_dump_toast, null).also { 39 it.findViewById<TextView>(R.id.leak_canary_toast_text).text = text 40 } 41 } 42 43 return toast 44 } 45 } 46