From 0c44f3f483be4d3350fd6157f221b0913805d83e Mon Sep 17 00:00:00 2001 From: S1m Date: Tue, 7 Dec 2021 22:53:13 +0100 Subject: [PATCH] Open market app if Nextcloud App isn't installed --- .../nextpush/account/AccountUtils.kt | 49 ++++++++++++++++++- .../nextpush/activities/MainActivity.kt | 3 +- .../nextpush/services/StartService.kt | 4 +- app/src/main/res/values/strings.xml | 7 +++ 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountUtils.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountUtils.kt index 825b96b..67d1bd3 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountUtils.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountUtils.kt @@ -2,7 +2,12 @@ package org.unifiedpush.distributor.nextpush.account import android.app.Activity import android.content.Context +import android.text.SpannableString +import android.text.method.LinkMovementMethod +import android.text.util.Linkify import android.util.Log +import android.widget.TextView +import androidx.appcompat.app.AlertDialog import com.nextcloud.android.sso.AccountImporter import com.nextcloud.android.sso.exceptions.AndroidGetAccountsPermissionNotGranted import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException @@ -11,6 +16,10 @@ import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException import com.nextcloud.android.sso.helper.SingleAccountHelper import com.nextcloud.android.sso.model.SingleSignOnAccount import com.nextcloud.android.sso.ui.UiExceptionManager +import android.content.DialogInterface +import android.content.Intent +import android.net.Uri +import org.unifiedpush.distributor.nextpush.R private const val TAG = "AccountUtils" @@ -20,11 +29,47 @@ const val PREF_URL = "url" lateinit var ssoAccount: SingleSignOnAccount +fun nextcloudAppNotInstalledDialog(context: Context) { + val message = TextView(context) + val builder = AlertDialog.Builder(context) + var messageContent = context.getString(R.string.message_missing_nextcloud_app) + val installIntent = + Intent(Intent.ACTION_VIEW, Uri.parse(context.getString(R.string.uri_market_nextcloud_app))) + messageContent += if ( + installIntent.resolveActivity(context.applicationContext.packageManager) != null + ) { + val callback = { + context.startActivity( + Intent.createChooser( + installIntent, + context.getString(R.string.market_chooser_title)) + ) + } + builder.setPositiveButton(context.getString(R.string.install)) { + _: DialogInterface, _: Int -> callback() + } + builder.setNegativeButton(context.getString(R.string.dismiss)) { + _: DialogInterface, _: Int -> + } + "." + } else { + ": " + context.getString(R.string.uri_fdroid_nextcloud_app) + } + val s = SpannableString(messageContent) + Linkify.addLinks(s, Linkify.ALL) + message.text = s + message.movementMethod = LinkMovementMethod.getInstance() + message.setPadding(32,32,32,32) + builder.setTitle(context.getString(R.string.nextcloud_files_not_found_title)) + builder.setView(message) + builder.show() +} + fun isConnected(context: Context) : Boolean { try { ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context) } catch (e: NextcloudFilesAppAccountNotFoundException) { - UiExceptionManager.showDialogForException(context, e) + nextcloudAppNotInstalledDialog(context) } catch (e: NoCurrentAccountSelectedException) { Log.d(TAG,"Device is not connected") return false @@ -36,7 +81,7 @@ fun connect(activity: Activity) { try { AccountImporter.pickNewAccount(activity) } catch (e: NextcloudFilesAppNotInstalledException) { - UiExceptionManager.showDialogForException(activity, e) + nextcloudAppNotInstalledDialog(activity) } catch (e: AndroidGetAccountsPermissionNotGranted) { UiExceptionManager.showDialogForException(activity, e) } 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 c2084de..0a55a86 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 @@ -26,6 +26,7 @@ import com.nextcloud.android.sso.exceptions.* import org.unifiedpush.distributor.nextpush.R import org.unifiedpush.distributor.nextpush.account.isConnected import org.unifiedpush.distributor.nextpush.account.connect +import org.unifiedpush.distributor.nextpush.account.nextcloudAppNotInstalledDialog import org.unifiedpush.distributor.nextpush.account.ssoAccount import org.unifiedpush.distributor.nextpush.api.apiDeleteApp import org.unifiedpush.distributor.nextpush.api.apiDeleteDevice @@ -83,7 +84,7 @@ class MainActivity : AppCompatActivity() { try { ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context) } catch (e: NextcloudFilesAppAccountNotFoundException) { - UiExceptionManager.showDialogForException(context, e) + nextcloudAppNotInstalledDialog(context) } catch (e: NoCurrentAccountSelectedException) { UiExceptionManager.showDialogForException(context, e) } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt index b2f1adb..1a96542 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt @@ -11,9 +11,9 @@ import android.util.Log import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException import com.nextcloud.android.sso.helper.SingleAccountHelper -import com.nextcloud.android.sso.ui.UiExceptionManager import org.unifiedpush.distributor.nextpush.account.ssoAccount +import org.unifiedpush.distributor.nextpush.account.nextcloudAppNotInstalledDialog import android.net.Network @@ -76,7 +76,7 @@ class StartService: Service(){ try { ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(this) } catch (e: NextcloudFilesAppAccountNotFoundException) { - UiExceptionManager.showDialogForException(this, e) + nextcloudAppNotInstalledDialog(this) } catch (e: NoCurrentAccountSelectedException) { return } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8606922..1d775b1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -19,4 +19,11 @@ NextPush is disconnected Warning Listening + Nextcloud Files is not installed on your device.\nPlease install it + market://details?id=com.nextcloud.client + Market + Install + Dismiss + https://f-droid.org/repository/browse/?fdid=com.nextcloud.client + Nextcloud Files not found