1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-24 23:57:30 +00:00

Convert ErrorReporterActivity to kotlin

This commit is contained in:
Ammar Githam 2021-07-12 21:57:51 +09:00
parent ab2a1a438b
commit 863de885ea
2 changed files with 80 additions and 90 deletions

View File

@ -1,102 +1,96 @@
package awaisomereport; package awaisomereport
import android.app.Activity; import android.app.Activity
import android.content.Context; import android.content.Context
import android.graphics.Canvas; import android.graphics.Canvas
import android.graphics.Paint; import android.graphics.Paint
import android.graphics.Rect; import android.graphics.Paint.FontMetricsInt
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable
import android.os.Bundle; import android.os.Bundle
import android.text.Spannable; import android.text.Spannable
import android.text.SpannableString; import android.text.SpannableString
import android.text.style.ImageSpan; import android.text.style.ImageSpan
import android.view.View; import android.view.View
import android.view.ViewGroup; import android.view.ViewGroup
import androidx.annotation.DrawableRes
import awais.instagrabber.R
import awais.instagrabber.databinding.ActivityCrashErrorBinding
import awaisomereport.CrashReporterHelper.startCrashEmailIntent
import java.lang.ref.WeakReference
import kotlin.system.exitProcess
import androidx.annotation.NonNull; class ErrorReporterActivity : Activity(), View.OnClickListener {
import androidx.annotation.Nullable;
import java.lang.ref.WeakReference; private lateinit var binding: ActivityCrashErrorBinding
import awais.instagrabber.R; override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
public final class ErrorReporterActivity extends Activity implements View.OnClickListener { binding = ActivityCrashErrorBinding.inflate(layoutInflater)
private View btnReport; setContentView(binding.root)
setFinishOnTouchOutside(false)
@Override window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
protected void onCreate(@Nullable final Bundle savedInstanceState) { val crashTitle = SpannableString(" " + getString(R.string.crash_title))
super.onCreate(savedInstanceState); crashTitle.setSpan(
setContentView(R.layout.activity_crash_error); CenteredImageSpan(this, android.R.drawable.stat_notify_error),
0,
setFinishOnTouchOutside(false); 1,
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); Spannable.SPAN_INCLUSIVE_EXCLUSIVE
)
final SpannableString crashTitle = new SpannableString(" " + getString(R.string.crash_title)); title = crashTitle
crashTitle.setSpan(new CenteredImageSpan(this, android.R.drawable.stat_notify_error), binding.btnReport.setOnClickListener(this)
0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); binding.btnCancel.setOnClickListener(this)
setTitle(crashTitle);
btnReport = findViewById(R.id.btnReport);
btnReport.setOnClickListener(this);
findViewById(R.id.btnCancel).setOnClickListener(this);
} }
@Override override fun onClick(v: View) {
public void onClick(@NonNull final View v) { if (v === binding.btnReport) {
if (v == btnReport) { startCrashEmailIntent(this)
CrashReporterHelper.startCrashEmailIntent(this);
} }
finish(); finish()
System.exit(10); exitProcess(10)
} }
public static class CenteredImageSpan extends ImageSpan { private class CenteredImageSpan(context: Context, @DrawableRes drawableRes: Int) : ImageSpan(context, drawableRes) {
private WeakReference<Drawable> drawable;
public CenteredImageSpan(final Context context, final int drawableRes) { private var drawable: WeakReference<Drawable>? = null
super(context, drawableRes);
}
@Override override fun getSize(
public int getSize(@NonNull final Paint paint, final CharSequence text, final int start, final int end, @Nullable final Paint.FontMetricsInt fm) { paint: Paint,
final Drawable drawable = getCachedDrawable(); text: CharSequence,
final Rect rect = drawable.getBounds(); start: Int,
end: Int,
if (fm != null) { fm: FontMetricsInt?
final Paint.FontMetricsInt pfm = paint.getFontMetricsInt(); ): Int {
fm.ascent = pfm.ascent; fm?.apply {
fm.descent = pfm.descent; val pfm = paint.fontMetricsInt
fm.top = pfm.top; ascent = pfm.ascent
fm.bottom = pfm.bottom; descent = pfm.descent
top = pfm.top
bottom = pfm.bottom
} }
return cachedDrawable.bounds.right
return rect.right;
} }
@Override override fun draw(
public void draw(@NonNull final Canvas canvas, final CharSequence text, final int start, final int end, final float x, final int top, canvas: Canvas,
final int y, final int bottom, @NonNull final Paint paint) { text: CharSequence,
final Drawable drawable = getCachedDrawable(); start: Int,
canvas.save(); end: Int,
x: Float,
final int drawableHeight = drawable.getIntrinsicHeight(); top: Int,
final Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt(); y: Int,
int transY = bottom - drawable.getBounds().bottom + (drawableHeight - fontMetricsInt.descent + fontMetricsInt.ascent) / 2; bottom: Int,
paint: Paint
canvas.translate(x, transY); ) {
drawable.draw(canvas); canvas.save()
canvas.restore(); val drawableHeight = cachedDrawable.intrinsicHeight
val fontMetricsInt = paint.fontMetricsInt
val transY = bottom - cachedDrawable.bounds.bottom + (drawableHeight - fontMetricsInt.descent + fontMetricsInt.ascent) / 2
canvas.translate(x, transY.toFloat())
cachedDrawable.draw(canvas)
canvas.restore()
} }
private Drawable getCachedDrawable() { private val cachedDrawable: Drawable
Drawable d = null; get() = drawable?.get() ?: getDrawable().also { drawable = WeakReference(it) }
if (drawable != null) d = drawable.get();
if (d == null) {
d = getDrawable();
drawable = new WeakReference<>(d);
}
return d;
}
} }
} }

View File

@ -1,11 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:orientation="vertical" android:orientation="vertical">
tools:viewBindingIgnore="true">
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -25,7 +23,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:gravity="end|center_vertical"> android:gravity="end|center_vertical">
<Button <Button
@ -35,7 +32,6 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="end|bottom" android:layout_gravity="end|bottom"
android:layout_marginEnd="5dp" android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:gravity="center" android:gravity="center"
android:padding="8dp" android:padding="8dp"
android:text="@string/cancel" android:text="@string/cancel"