Skip to content

Commit

Permalink
Add dialog to reinstall action
Browse files Browse the repository at this point in the history
  • Loading branch information
alufers committed Feb 5, 2023
1 parent 85ff29b commit 0ecb55f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
49 changes: 33 additions & 16 deletions app/app/src/main/java/com/octo4a/ui/fragments/ServerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.octo4a.ui.fragments

import android.app.Activity
import android.app.ActivityManager
import android.app.AlertDialog
import android.content.ComponentName
import android.content.Context
import android.content.Intent
Expand All @@ -21,6 +22,7 @@ import androidx.core.view.isGone
import androidx.fragment.app.Fragment
import androidx.lifecycle.asLiveData
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.octo4a.Octo4aApplication
import com.octo4a.R
import com.octo4a.camera.CameraService
import com.octo4a.repository.GithubRelease
Expand All @@ -29,6 +31,7 @@ import com.octo4a.repository.ServerStatus
import com.octo4a.serial.VirtualSerialDriver
import com.octo4a.ui.InitialActivity
import com.octo4a.ui.WebinterfaceActivity
import com.octo4a.ui.showBugReportingDialog
import com.octo4a.ui.views.UsbDeviceView
import com.octo4a.utils.preferences.MainPreferences
import com.octo4a.viewmodel.StatusViewModel
Expand Down Expand Up @@ -226,25 +229,39 @@ class ServerFragment : Fragment() {
}

private fun clearDataAndRestartApp() {
try {
val activityManager = requireActivity().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
// clearing app data
if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) {
activityManager.clearApplicationUserData()
} else {
val packageName = requireActivity().applicationContext.packageName
val runtime = Runtime.getRuntime();
runtime.exec("pm clear $packageName");
val builder = AlertDialog.Builder(context)
builder.apply {
setTitle(getString(R.string.reinstall_dialog_title))
setMessage(R.string.app_will_restart_to_clear)
setNegativeButton(getString(R.string.reinstall_dialog_dismiss)) { dialog, id ->

}
setPositiveButton(getString(R.string.reinstall_dialog_continue)) { dialog, id ->
try {
val activityManager = requireActivity().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
// clearing app data
if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) {
activityManager.clearApplicationUserData()
} else {
val packageName = requireActivity().applicationContext.packageName
val runtime = Runtime.getRuntime();
runtime.exec("pm clear $packageName");
}

// restart the app
val intent = Intent(context, InitialActivity::class.java)
intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
requireActivity().startActivity(intent)
// restart the app
val intent = Intent(context, InitialActivity::class.java)
intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
requireActivity().startActivity(intent)

Runtime.getRuntime().exit(0)
} catch (e: Exception) {
e.printStackTrace()
Runtime.getRuntime().exit(0)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
val dialog = builder.create()
dialog.show()


}
}
4 changes: 4 additions & 0 deletions app/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
<string name="bugreport_dialog_msg">Octo4a is currently under rapid development. Enabling anonymous bug reporting makes it easier for developers to see what caused the app to malfunction and improve it. You can always change this setting later in the app\'s preferences.</string>
<string name="bugreport_dialog_dismiss">No</string>
<string name="bugreport_dialog_enable">Yes, enable reporting</string>
<string name="app_will_restart_to_clear">The app will stop to clear it\'s data. Please start it again and re-install.</string>
<string name="reinstall_dialog_title">Do you want to clear the app\'s data and restart?</string>
<string name="reinstall_dialog_continue">Continue</string>
<string name="reinstall_dialog_dismiss">Cancel</string>


</resources>
7 changes: 7 additions & 0 deletions local.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Sun Feb 05 13:08:54 UTC 2023

0 comments on commit 0ecb55f

Please sign in to comment.