newpipe/error: declare variables from onCreate as lateinit

We will init them at the beginning of the view lifecycle, so they are
definitly not null.
This commit is contained in:
Profpatsch 2024-03-30 14:17:22 +01:00
parent 726cdebcd3
commit e20122958d
2 changed files with 43 additions and 43 deletions

View File

@ -50,9 +50,9 @@ import java.util.stream.Collectors
* This activity is used to show error details and allow reporting them in various ways. Use [ ][ErrorUtil.openActivity] to correctly open this activity. * This activity is used to show error details and allow reporting them in various ways. Use [ ][ErrorUtil.openActivity] to correctly open this activity.
*/ */
class ErrorActivity : AppCompatActivity() { class ErrorActivity : AppCompatActivity() {
private var errorInfo: ErrorInfo? = null private lateinit var errorInfo: ErrorInfo
private var currentTimeStamp: String? = null private lateinit var currentTimeStamp: String
private var activityErrorBinding: ActivityErrorBinding? = null private lateinit var activityErrorBinding: ActivityErrorBinding
// ////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////
// Activity lifecycle // Activity lifecycle
@ -65,33 +65,33 @@ class ErrorActivity : AppCompatActivity() {
activityErrorBinding = ActivityErrorBinding.inflate( activityErrorBinding = ActivityErrorBinding.inflate(
layoutInflater layoutInflater
) )
setContentView(activityErrorBinding!!.root) setContentView(activityErrorBinding.root)
val intent = intent val intent = intent
setSupportActionBar(activityErrorBinding!!.toolbarLayout.toolbar) setSupportActionBar(activityErrorBinding.toolbarLayout.toolbar)
val actionBar = supportActionBar val actionBar = supportActionBar
if (actionBar != null) { if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true) actionBar.setDisplayHomeAsUpEnabled(true)
actionBar.setTitle(R.string.error_report_title) actionBar.setTitle(R.string.error_report_title)
actionBar.setDisplayShowTitleEnabled(true) actionBar.setDisplayShowTitleEnabled(true)
} }
errorInfo = IntentCompat.getParcelableExtra(intent, ERROR_INFO, ErrorInfo::class.java) errorInfo = IntentCompat.getParcelableExtra(intent, ERROR_INFO, ErrorInfo::class.java)!!
// important add guru meditation // important add guru meditation
addGuruMeditation() addGuruMeditation()
currentTimeStamp = CURRENT_TIMESTAMP_FORMATTER.format(LocalDateTime.now()) currentTimeStamp = CURRENT_TIMESTAMP_FORMATTER.format(LocalDateTime.now())
activityErrorBinding!!.errorReportEmailButton.setOnClickListener { _: View? -> activityErrorBinding.errorReportEmailButton.setOnClickListener { _: View? ->
openPrivacyPolicyDialog( openPrivacyPolicyDialog(
this, this,
"EMAIL" "EMAIL"
) )
} }
activityErrorBinding!!.errorReportCopyButton.setOnClickListener { _: View? -> activityErrorBinding.errorReportCopyButton.setOnClickListener { _: View? ->
ShareUtils.copyToClipboard( ShareUtils.copyToClipboard(
this, this,
buildMarkdown() buildMarkdown()
) )
} }
activityErrorBinding!!.errorReportGitHubButton.setOnClickListener { _: View? -> activityErrorBinding.errorReportGitHubButton.setOnClickListener { _: View? ->
openPrivacyPolicyDialog( openPrivacyPolicyDialog(
this, this,
"GITHUB" "GITHUB"
@ -100,11 +100,11 @@ class ErrorActivity : AppCompatActivity() {
// normal bugreport // normal bugreport
buildInfo(errorInfo) buildInfo(errorInfo)
activityErrorBinding!!.errorMessageView.setText(errorInfo!!.messageStringId) activityErrorBinding.errorMessageView.setText(errorInfo.messageStringId)
activityErrorBinding!!.errorView.text = formErrorText(errorInfo!!.stackTraces) activityErrorBinding.errorView.text = formErrorText(errorInfo.stackTraces)
// print stack trace once again for debugging: // print stack trace once again for debugging:
for (e in errorInfo!!.stackTraces) { for (e in errorInfo.stackTraces) {
Log.e(TAG, e) Log.e(TAG, e)
} }
} }
@ -185,12 +185,12 @@ class ErrorActivity : AppCompatActivity() {
) )
} }
private fun buildInfo(info: ErrorInfo?) { private fun buildInfo(info: ErrorInfo) {
var text = "" var text = ""
activityErrorBinding!!.errorInfoLabelsView.text = getString(R.string.info_labels) activityErrorBinding.errorInfoLabelsView.text = getString(R.string.info_labels)
.replace("\\n", "\n") .replace("\\n", "\n")
text += """ text += """
${getUserActionString(info!!.userAction)} ${getUserActionString(info.userAction)}
${info.request} ${info.request}
$contentLanguageString $contentLanguageString
$contentCountryString $contentCountryString
@ -201,27 +201,27 @@ class ErrorActivity : AppCompatActivity() {
${BuildConfig.VERSION_NAME} ${BuildConfig.VERSION_NAME}
$osString $osString
""".trimIndent() """.trimIndent()
activityErrorBinding!!.errorInfosView.text = text activityErrorBinding.errorInfosView.text = text
} }
private fun buildJson(): String { private fun buildJson(): String {
try { try {
return JsonWriter.string() return JsonWriter.string()
.`object`() .`object`()
.value("user_action", getUserActionString(errorInfo!!.userAction)) .value("user_action", getUserActionString(errorInfo.userAction))
.value("request", errorInfo!!.request) .value("request", errorInfo.request)
.value("content_language", contentLanguageString) .value("content_language", contentLanguageString)
.value("content_country", contentCountryString) .value("content_country", contentCountryString)
.value("app_language", appLanguage) .value("app_language", appLanguage)
.value("service", errorInfo!!.serviceName) .value("service", errorInfo.serviceName)
.value("package", packageName) .value("package", packageName)
.value("version", BuildConfig.VERSION_NAME) .value("version", BuildConfig.VERSION_NAME)
.value("os", osString) .value("os", osString)
.value("time", currentTimeStamp) .value("time", currentTimeStamp)
.array("exceptions", listOf(*errorInfo!!.stackTraces)) .array("exceptions", listOf(*errorInfo.stackTraces))
.value( .value(
"user_comment", "user_comment",
activityErrorBinding!!.errorCommentBox.text activityErrorBinding.errorCommentBox.text
.toString() .toString()
) )
.end() .end()
@ -236,7 +236,7 @@ class ErrorActivity : AppCompatActivity() {
private fun buildMarkdown(): String { private fun buildMarkdown(): String {
return try { return try {
val htmlErrorReport = StringBuilder() val htmlErrorReport = StringBuilder()
val userComment = activityErrorBinding!!.errorCommentBox.text.toString() val userComment = activityErrorBinding.errorCommentBox.text.toString()
if (userComment.isNotEmpty()) { if (userComment.isNotEmpty()) {
htmlErrorReport.append(userComment).append("\n") htmlErrorReport.append(userComment).append("\n")
} }
@ -245,38 +245,38 @@ class ErrorActivity : AppCompatActivity() {
htmlErrorReport htmlErrorReport
.append("## Exception") .append("## Exception")
.append("\n* __User Action:__ ") .append("\n* __User Action:__ ")
.append(getUserActionString(errorInfo!!.userAction)) .append(getUserActionString(errorInfo.userAction))
.append("\n* __Request:__ ").append(errorInfo!!.request) .append("\n* __Request:__ ").append(errorInfo.request)
.append("\n* __Content Country:__ ").append(contentCountryString) .append("\n* __Content Country:__ ").append(contentCountryString)
.append("\n* __Content Language:__ ").append(contentLanguageString) .append("\n* __Content Language:__ ").append(contentLanguageString)
.append("\n* __App Language:__ ").append(appLanguage) .append("\n* __App Language:__ ").append(appLanguage)
.append("\n* __Service:__ ").append(errorInfo!!.serviceName) .append("\n* __Service:__ ").append(errorInfo.serviceName)
.append("\n* __Version:__ ").append(BuildConfig.VERSION_NAME) .append("\n* __Version:__ ").append(BuildConfig.VERSION_NAME)
.append("\n* __OS:__ ").append(osString).append("\n") .append("\n* __OS:__ ").append(osString).append("\n")
// Collapse all logs to a single paragraph when there are more than one // Collapse all logs to a single paragraph when there are more than one
// to keep the GitHub issue clean. // to keep the GitHub issue clean.
if (errorInfo!!.stackTraces.size > 1) { if (errorInfo.stackTraces.size > 1) {
htmlErrorReport htmlErrorReport
.append("<details><summary><b>Exceptions (") .append("<details><summary><b>Exceptions (")
.append(errorInfo!!.stackTraces.size) .append(errorInfo.stackTraces.size)
.append(")</b></summary><p>\n") .append(")</b></summary><p>\n")
} }
// add the logs // add the logs
for (i in errorInfo!!.stackTraces.indices) { for (i in errorInfo.stackTraces.indices) {
htmlErrorReport.append("<details><summary><b>Crash log ") htmlErrorReport.append("<details><summary><b>Crash log ")
if (errorInfo!!.stackTraces.size > 1) { if (errorInfo.stackTraces.size > 1) {
htmlErrorReport.append(i + 1) htmlErrorReport.append(i + 1)
} }
htmlErrorReport.append("</b>") htmlErrorReport.append("</b>")
.append("</summary><p>\n") .append("</summary><p>\n")
.append("\n```\n").append(errorInfo!!.stackTraces[i]).append("\n```\n") .append("\n```\n").append(errorInfo.stackTraces[i]).append("\n```\n")
.append("</details>\n") .append("</details>\n")
} }
// make sure to close everything // make sure to close everything
if (errorInfo!!.stackTraces.size > 1) { if (errorInfo.stackTraces.size > 1) {
htmlErrorReport.append("</p></details>\n") htmlErrorReport.append("</p></details>\n")
} }
htmlErrorReport.append("<hr>\n") htmlErrorReport.append("<hr>\n")
@ -312,12 +312,12 @@ class ErrorActivity : AppCompatActivity() {
private fun addGuruMeditation() { private fun addGuruMeditation() {
// just an easter egg // just an easter egg
var text = activityErrorBinding!!.errorSorryView.text.toString() var text = activityErrorBinding.errorSorryView.text.toString()
text += """ text += """
${getString(R.string.guru_meditation)} ${getString(R.string.guru_meditation)}
""".trimIndent() """.trimIndent()
activityErrorBinding!!.errorSorryView.text = text activityErrorBinding.errorSorryView.text = text
} }
companion object { companion object {

View File

@ -41,7 +41,7 @@ import java.io.UnsupportedEncodingException
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>. * along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/ */
class ReCaptchaActivity : AppCompatActivity() { class ReCaptchaActivity : AppCompatActivity() {
private var recaptchaBinding: ActivityRecaptchaBinding? = null private lateinit var recaptchaBinding: ActivityRecaptchaBinding
private var foundCookies = "" private var foundCookies = ""
@SuppressLint("SetJavaScriptEnabled") @SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -50,17 +50,17 @@ class ReCaptchaActivity : AppCompatActivity() {
recaptchaBinding = ActivityRecaptchaBinding.inflate( recaptchaBinding = ActivityRecaptchaBinding.inflate(
layoutInflater layoutInflater
) )
setContentView(recaptchaBinding!!.root) setContentView(recaptchaBinding.root)
setSupportActionBar(recaptchaBinding!!.toolbar) setSupportActionBar(recaptchaBinding.toolbar)
val url = sanitizeRecaptchaUrl(intent.getStringExtra(RECAPTCHA_URL_EXTRA)) val url = sanitizeRecaptchaUrl(intent.getStringExtra(RECAPTCHA_URL_EXTRA))
// set return to Cancel by default // set return to Cancel by default
setResult(RESULT_CANCELED) setResult(RESULT_CANCELED)
// enable Javascript // enable Javascript
val webSettings = recaptchaBinding!!.reCaptchaWebView.settings val webSettings = recaptchaBinding.reCaptchaWebView.settings
webSettings.javaScriptEnabled = true webSettings.javaScriptEnabled = true
webSettings.userAgentString = DownloaderImpl.USER_AGENT webSettings.userAgentString = DownloaderImpl.USER_AGENT
recaptchaBinding!!.reCaptchaWebView.webViewClient = object : WebViewClient() { recaptchaBinding.reCaptchaWebView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading( override fun shouldOverrideUrlLoading(
view: WebView, view: WebView,
request: WebResourceRequest request: WebResourceRequest
@ -79,10 +79,10 @@ class ReCaptchaActivity : AppCompatActivity() {
} }
// cleaning cache, history and cookies from webView // cleaning cache, history and cookies from webView
recaptchaBinding!!.reCaptchaWebView.clearCache(true) recaptchaBinding.reCaptchaWebView.clearCache(true)
recaptchaBinding!!.reCaptchaWebView.clearHistory() recaptchaBinding.reCaptchaWebView.clearHistory()
CookieManager.getInstance().removeAllCookies(null) CookieManager.getInstance().removeAllCookies(null)
recaptchaBinding!!.reCaptchaWebView.loadUrl(url) recaptchaBinding.reCaptchaWebView.loadUrl(url)
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -111,7 +111,7 @@ class ReCaptchaActivity : AppCompatActivity() {
private fun saveCookiesAndFinish() { private fun saveCookiesAndFinish() {
// try to get cookies of unclosed page // try to get cookies of unclosed page
handleCookiesFromUrl(recaptchaBinding!!.reCaptchaWebView.url) handleCookiesFromUrl(recaptchaBinding.reCaptchaWebView.url)
if (MainActivity.DEBUG) { if (MainActivity.DEBUG) {
Log.d(TAG, "saveCookiesAndFinish: foundCookies=$foundCookies") Log.d(TAG, "saveCookiesAndFinish: foundCookies=$foundCookies")
} }
@ -129,7 +129,7 @@ class ReCaptchaActivity : AppCompatActivity() {
} }
// Navigate to blank page (unloads youtube to prevent background playback) // Navigate to blank page (unloads youtube to prevent background playback)
recaptchaBinding!!.reCaptchaWebView.loadUrl("about:blank") recaptchaBinding.reCaptchaWebView.loadUrl("about:blank")
val intent = Intent(this, MainActivity::class.java) val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
NavUtils.navigateUpTo(this, intent) NavUtils.navigateUpTo(this, intent)