1 /* <lambda>null2 * Copyright 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.virtualization.terminal 17 18 import android.annotation.MainThread 19 import android.content.Intent 20 import android.os.Bundle 21 import android.util.Log 22 import android.view.View 23 import com.google.android.material.snackbar.Snackbar 24 import java.io.IOException 25 import java.util.concurrent.ExecutorService 26 import java.util.concurrent.Executors 27 28 class UpgradeActivity : BaseActivity() { 29 private lateinit var executorService: ExecutorService 30 31 override fun onCreate(savedInstanceState: Bundle?) { 32 super.onCreate(savedInstanceState) 33 34 executorService = 35 Executors.newSingleThreadExecutor(TerminalThreadFactory(applicationContext)) 36 37 setContentView(R.layout.activity_upgrade) 38 39 val button = findViewById<View>(R.id.upgrade) 40 button.setOnClickListener { _ -> upgrade() } 41 } 42 43 override fun onDestroy() { 44 super.onDestroy() 45 46 executorService.shutdown() 47 } 48 49 private fun upgrade() { 50 findViewById<View>(R.id.progress).visibility = View.VISIBLE 51 52 executorService.execute { 53 val image = InstalledImage.getDefault(this) 54 try { 55 image.uninstallAndBackup() 56 } catch (e: IOException) { 57 Snackbar.make( 58 findViewById<View>(android.R.id.content), 59 R.string.upgrade_error, 60 Snackbar.LENGTH_SHORT, 61 ) 62 .show() 63 Log.e(MainActivity.Companion.TAG, "Failed to upgrade ", e) 64 return@execute 65 } 66 67 runOnUiThread { 68 findViewById<View>(R.id.progress).visibility = View.INVISIBLE 69 restartTerminal() 70 } 71 } 72 } 73 74 @MainThread 75 private fun restartTerminal() { 76 val intent = baseContext.packageManager.getLaunchIntentForPackage(baseContext.packageName) 77 intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) 78 finish() 79 startActivity(intent) 80 } 81 } 82