1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-14 18: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 fun getSize(
paint: Paint,
text: CharSequence,
start: Int,
end: Int,
fm: FontMetricsInt?
): Int {
fm?.apply {
val pfm = paint.fontMetricsInt
ascent = pfm.ascent
descent = pfm.descent
top = pfm.top
bottom = pfm.bottom
}
return cachedDrawable.bounds.right
} }
@Override override fun draw(
public int getSize(@NonNull final Paint paint, final CharSequence text, final int start, final int end, @Nullable final Paint.FontMetricsInt fm) { canvas: Canvas,
final Drawable drawable = getCachedDrawable(); text: CharSequence,
final Rect rect = drawable.getBounds(); start: Int,
end: Int,
if (fm != null) { x: Float,
final Paint.FontMetricsInt pfm = paint.getFontMetricsInt(); top: Int,
fm.ascent = pfm.ascent; y: Int,
fm.descent = pfm.descent; bottom: Int,
fm.top = pfm.top; paint: Paint
fm.bottom = pfm.bottom; ) {
canvas.save()
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()
} }
return rect.right; private val cachedDrawable: Drawable
} get() = drawable?.get() ?: getDrawable().also { drawable = WeakReference(it) }
@Override
public void draw(@NonNull final Canvas canvas, final CharSequence text, final int start, final int end, final float x, final int top,
final int y, final int bottom, @NonNull final Paint paint) {
final Drawable drawable = getCachedDrawable();
canvas.save();
final int drawableHeight = drawable.getIntrinsicHeight();
final Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
int transY = bottom - drawable.getBounds().bottom + (drawableHeight - fontMetricsInt.descent + fontMetricsInt.ascent) / 2;
canvas.translate(x, transY);
drawable.draw(canvas);
canvas.restore();
}
private Drawable getCachedDrawable() {
Drawable d = null;
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"