1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-21 15:40:32 +00:00

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

View File

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