v1.0.9 - 메인 및 설정 화면 상단 빈 여백 완전 제거 및 인앱 APK 원클릭 업데이트 복원
This commit is contained in:
@@ -108,16 +108,7 @@ object AppUpdateManager {
|
||||
|
||||
btnNow.setOnClickListener {
|
||||
dialog.dismiss()
|
||||
try {
|
||||
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(apkUrl)).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
}
|
||||
activity.startActivity(intent)
|
||||
Toast.makeText(activity, "최신 버전 다운로드 페이지로 이동합니다.", Toast.LENGTH_SHORT).show()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
Toast.makeText(activity, "다운로드 링크 열기 실패: ${e.message}", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
downloadAndInstallApk(activity, apkUrl, version)
|
||||
}
|
||||
|
||||
dialog.show()
|
||||
@@ -125,4 +116,100 @@ object AppUpdateManager {
|
||||
val width = (activity.resources.displayMetrics.widthPixels * 0.88).toInt()
|
||||
dialog.window?.setLayout(width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||
}
|
||||
|
||||
private fun downloadAndInstallApk(activity: Activity, apkUrl: String, version: String) {
|
||||
if (activity.isFinishing || activity.isDestroyed) return
|
||||
|
||||
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_update_progress_oneui, null)
|
||||
val tvSub = view.findViewById<TextView>(R.id.tvProgressSub)
|
||||
val progressBar = view.findViewById<ProgressBar>(R.id.downloadProgressBar)
|
||||
val tvPercent = view.findViewById<TextView>(R.id.tvProgressPercent)
|
||||
|
||||
tvSub.text = "v$version 다운로드 중..."
|
||||
|
||||
val progressDialog = AlertDialog.Builder(activity)
|
||||
.setView(view)
|
||||
.setCancelable(false)
|
||||
.create()
|
||||
|
||||
progressDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
|
||||
progressDialog.show()
|
||||
|
||||
val width = (activity.resources.displayMetrics.widthPixels * 0.88).toInt()
|
||||
progressDialog.window?.setLayout(width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||
|
||||
Thread {
|
||||
try {
|
||||
val url = URL(apkUrl)
|
||||
val connection = url.openConnection() as HttpURLConnection
|
||||
connection.connectTimeout = 15000
|
||||
connection.readTimeout = 15000
|
||||
connection.requestMethod = "GET"
|
||||
connection.connect()
|
||||
|
||||
val fileLength = connection.contentLength
|
||||
val inputStream = BufferedInputStream(connection.inputStream)
|
||||
|
||||
val apkFile = File(activity.cacheDir, "update.apk")
|
||||
val outputStream = FileOutputStream(apkFile)
|
||||
|
||||
val buffer = ByteArray(8192)
|
||||
var total: Long = 0
|
||||
var count: Int
|
||||
|
||||
while (inputStream.read(buffer).also { count = it } != -1) {
|
||||
total += count
|
||||
outputStream.write(buffer, 0, count)
|
||||
|
||||
if (fileLength > 0) {
|
||||
val progress = (total * 100 / fileLength).toInt()
|
||||
activity.runOnUiThread {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
progressBar.setProgress(progress, true)
|
||||
} else {
|
||||
progressBar.progress = progress
|
||||
}
|
||||
tvPercent.text = "$progress%"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
outputStream.flush()
|
||||
outputStream.close()
|
||||
inputStream.close()
|
||||
connection.disconnect()
|
||||
|
||||
activity.runOnUiThread {
|
||||
progressDialog.dismiss()
|
||||
installApk(activity, apkFile)
|
||||
}
|
||||
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
activity.runOnUiThread {
|
||||
progressDialog.dismiss()
|
||||
Toast.makeText(activity, "다운로드 실패: ${e.message}", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
|
||||
private fun installApk(activity: Activity, apkFile: File) {
|
||||
try {
|
||||
val apkUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
FileProvider.getUriForFile(activity, "${activity.packageName}.provider", apkFile)
|
||||
} else {
|
||||
Uri.fromFile(apkFile)
|
||||
}
|
||||
|
||||
val intent = Intent(Intent.ACTION_VIEW).apply {
|
||||
setDataAndType(apkUri, "application/vnd.android.package-archive")
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||
}
|
||||
activity.startActivity(intent)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
Toast.makeText(activity, "설치 실패: ${e.message}", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,9 +63,7 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
|
||||
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||
val density = resources.displayMetrics.density
|
||||
val p = (8 * density).toInt()
|
||||
v.setPadding(systemBars.left + p, systemBars.top + p, systemBars.right + p, systemBars.bottom + p)
|
||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
||||
insets
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user