From c53f304c030cd47391e2f83bad53631cdbefc423 Mon Sep 17 00:00:00 2001 From: sim Date: Mon, 18 Nov 2024 09:33:48 +0000 Subject: [PATCH] Migrate start activity to jetpack --- app/build.gradle.kts | 1 + .../nextpush/account/AccountFactory.kt | 12 +- .../nextpush/activities/AppAction.kt | 20 ++ .../nextpush/activities/MainActivity.kt | 1 + .../nextpush/activities/PermissionsRequest.kt | 68 ------- .../nextpush/activities/StartActivity.kt | 109 ++++++---- .../nextpush/activities/StartViewModel.kt | 5 + .../nextpush/activities/UiAction.kt | 1 + .../nextpush/activities/ui/PermissionsUi.kt | 46 +++++ .../nextpush/activities/ui/StartUi.kt | 191 ++++++++++++++++++ .../main/res/drawable/ic_visibility_24.xml | 5 + .../res/drawable/ic_visibility_off_24.xml | 5 + app/src/main/res/layout/activity_start.xml | 134 ------------ app/src/main/res/values/strings.xml | 6 +- gradle/libs.versions.toml | 2 + 15 files changed, 356 insertions(+), 250 deletions(-) delete mode 100644 app/src/main/java/org/unifiedpush/distributor/nextpush/activities/PermissionsRequest.kt create mode 100644 app/src/main/java/org/unifiedpush/distributor/nextpush/activities/StartViewModel.kt create mode 100644 app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/PermissionsUi.kt create mode 100644 app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/StartUi.kt create mode 100644 app/src/main/res/drawable/ic_visibility_24.xml create mode 100644 app/src/main/res/drawable/ic_visibility_off_24.xml delete mode 100644 app/src/main/res/layout/activity_start.xml diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 0d66924..1b7f18f 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -64,6 +64,7 @@ if (project.hasProperty("sign")) { } dependencies { + implementation(libs.accompanist.permissions) implementation(libs.androidx.activity.compose) implementation(libs.androidx.constraintlayout) implementation(libs.androidx.coordinatorlayout) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountFactory.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountFactory.kt index 9736807..fa2365c 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountFactory.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountFactory.kt @@ -28,15 +28,15 @@ object AccountFactory { account = null } - fun Context.setTypeSSO() { + fun setTypeSSO(context: Context) { account = null - AccountStore(this).accountType = AccountType.SSO - DirectAccountStore(this).wipe() // Just in case + AccountStore(context).accountType = AccountType.SSO + DirectAccountStore(context).wipe() // Just in case } - fun Context.setTypeDirect(url: String, username: String, password: String) { + fun setTypeDirect(context: Context, url: String, username: String, password: String) { account = null - AccountStore(this).accountType = AccountType.Direct - DirectAccount.setCredentials(this, url, username, password) + AccountStore(context).accountType = AccountType.Direct + DirectAccount.setCredentials(context, url, username, password) } } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/AppAction.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/AppAction.kt index ef3fe20..5536b73 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/AppAction.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/AppAction.kt @@ -32,10 +32,14 @@ class AppAction(private val type: Type, private val argv: Map? = nu DisableBatteryOptimisation, CopyEndpoint, DeleteRegistration, + LoginSSO, + LoginDirect } fun handle(context: Context) { when (type) { + Type.LoginSSO -> loginSSO(context) + Type.LoginDirect -> loginDirect(context, argv) Type.RestartService -> restartService(context) Type.Logout -> logout(context) Type.AddChannel -> addChannel(context, argv) @@ -45,6 +49,19 @@ class AppAction(private val type: Type, private val argv: Map? = nu } } + private fun loginSSO(context: Context) { + AccountFactory.setTypeSSO(context) + UiAction.publish(UiAction.Type.Login) + } + + private fun loginDirect(context: Context, argv: Map?) { + val username = argv?.get(ARG_USERNAME) as String? ?: return + val password = argv?.get(ARG_PASSWORD) as String? ?: return + val url = argv?.get(ARG_URL) as String? ?: return + AccountFactory.setTypeDirect(context, url, username, password) + UiAction.publish(UiAction.Type.Login) + } + private fun restartService(context: Context) { Log.d(TAG, "Restarting the Listener") FailureHandler.clearFails() @@ -112,6 +129,9 @@ class AppAction(private val type: Type, private val argv: Map? = nu } companion object { + const val ARG_USERNAME = "username" + const val ARG_PASSWORD = "password" + const val ARG_URL = "url" const val ARG_NEW_CHANNEL_TITLE = "title" const val ARG_TOKEN = "token" const val ARG_REGISTRATIONS = "registrations" diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt index 8e6e343..717808d 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt @@ -55,6 +55,7 @@ class MainActivity : ComponentActivity() { StartActivity.goToStartActivity(this@MainActivity) finish() } + else -> {} } } } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/PermissionsRequest.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/PermissionsRequest.kt deleted file mode 100644 index fbbac69..0000000 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/PermissionsRequest.kt +++ /dev/null @@ -1,68 +0,0 @@ -package org.unifiedpush.distributor.nextpush.activities - -import android.Manifest -import android.content.pm.PackageManager -import android.os.Build -import android.util.Log -import androidx.activity.result.contract.ActivityResultContracts -import androidx.appcompat.app.AlertDialog -import androidx.appcompat.app.AppCompatActivity -import org.unifiedpush.distributor.nextpush.R -import org.unifiedpush.distributor.nextpush.activities.PermissionsRequest.requestAppPermissions -import org.unifiedpush.distributor.nextpush.utils.TAG - -object PermissionsRequest { - - fun AppCompatActivity.requestAppPermissions() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) - != PackageManager.PERMISSION_GRANTED - ) { - Log.d(TAG, "Requesting POST_NOTIFICATIONS permission") - this.registerForActivityResult( - ActivityResultContracts.RequestPermission() - ) { granted -> - Log.d(TAG, "POST_NOTIFICATIONS permission granted: $granted") - if (!granted) { - if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) { - Log.d(TAG, "Show POST_NOTIFICATIONS permission rationale") - AlertDialog.Builder(this) - .setTitle(getString(R.string.no_notification_dialog_title)) - .setMessage(R.string.no_notification_dialog_message) - .show() - } - } - }.launch( - Manifest.permission.POST_NOTIFICATIONS - ) - } - } else if (Build.VERSION.SDK_INT < 26) { - this.requestAccountPermission() - } - } - - @Deprecated("For SDK<26 only") - fun AppCompatActivity.requestAccountPermission() { - if (checkSelfPermission(Manifest.permission.GET_ACCOUNTS) - != PackageManager.PERMISSION_GRANTED - ) { - Log.d(TAG, "Requesting GET_ACCOUNT permission") - this.registerForActivityResult( - ActivityResultContracts.RequestPermission() - ) { granted -> - Log.d(TAG, "GET_ACCOUNT permission granted: $granted") - if (!granted) { - if (shouldShowRequestPermissionRationale(Manifest.permission.GET_ACCOUNTS)) { - Log.d(TAG, "Show GET_ACCOUNTS permission rationale") - AlertDialog.Builder(this) - .setTitle(getString(R.string.no_notification_dialog_title)) - .setMessage(R.string.no_notification_dialog_message) - .show() - } - } - }.launch( - Manifest.permission.GET_ACCOUNTS - ) - } - } -} diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/StartActivity.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/StartActivity.kt index f06d72e..2affffa 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/StartActivity.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/StartActivity.kt @@ -1,61 +1,82 @@ package org.unifiedpush.distributor.nextpush.activities -import android.annotation.SuppressLint import android.app.Activity import android.content.Context import android.content.Intent import android.os.Bundle import android.util.Log -import android.widget.Button -import android.widget.EditText -import android.widget.LinearLayout -import android.widget.TextView import android.widget.Toast -import androidx.appcompat.app.AppCompatActivity -import androidx.core.view.isGone -import org.unifiedpush.distributor.nextpush.R +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.lifecycle.viewmodel.compose.viewModel +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch +import org.unifiedpush.distributor.nextpush.EventBus import org.unifiedpush.distributor.nextpush.account.AccountFactory -import org.unifiedpush.distributor.nextpush.account.AccountFactory.setTypeDirect -import org.unifiedpush.distributor.nextpush.account.AccountFactory.setTypeSSO import org.unifiedpush.distributor.nextpush.activities.MainActivity.Companion.goToMainActivity -import org.unifiedpush.distributor.nextpush.activities.PermissionsRequest.requestAppPermissions +import org.unifiedpush.distributor.nextpush.activities.ui.StartUi +import org.unifiedpush.distributor.nextpush.activities.ui.theme.AppTheme import org.unifiedpush.distributor.nextpush.utils.TAG -class StartActivity : AppCompatActivity() { - private var onResult: ((activity: Activity, requestCode: Int, resultCode: Int, data: Intent?, block: (success: Boolean) -> Unit) -> Unit)? = null +class StartActivity : ComponentActivity() { + private var jobs: MutableList = emptyList().toMutableList() + + private var onResult: ( + ( + activity: Activity, + requestCode: Int, + resultCode: Int, + data: Intent?, + block: (success: Boolean) -> Unit + ) -> Unit + )? = null - @SuppressLint("SetTextI18n") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_start) - this.requestAppPermissions() + if (AccountFactory.getAccount(this)?.connected == true) { goToMainActivity(this) finish() } - findViewById