Prepare non-SSO account
This commit is contained in:
parent
7ae9bce400
commit
44dd904e4c
@ -0,0 +1,36 @@
|
||||
package org.unifiedpush.distributor.nextpush.account
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException
|
||||
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException
|
||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
|
||||
object Account {
|
||||
private var account: AccountFactory? = null
|
||||
|
||||
fun getAccount(context: Context, uninitialized: Boolean = false): AccountFactory? {
|
||||
return account
|
||||
?: run {
|
||||
try {
|
||||
SSOAccountFactory().apply {
|
||||
initAccount(context)
|
||||
account = this
|
||||
}
|
||||
} catch (e: NextcloudFilesAppAccountNotFoundException) {
|
||||
Log.w(TAG, "Nextcloud application is not found")
|
||||
null
|
||||
} catch (e: NoCurrentAccountSelectedException) {
|
||||
if (uninitialized) {
|
||||
SSOAccountFactory()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun isConnected(context: Context): Boolean {
|
||||
return getAccount(context)?.isConnected(context) == true
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package org.unifiedpush.distributor.nextpush.account
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
|
||||
interface AccountFactory {
|
||||
val apiFactory: Class<*>
|
||||
val name: String
|
||||
val url: String
|
||||
fun initAccount(context: Context)
|
||||
fun isConnected(context: Context): Boolean
|
||||
fun connect(activity: Activity)
|
||||
fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?, block: (success: Boolean) -> Unit)
|
||||
fun getAccount(context: Context): Any?
|
||||
}
|
@ -1,26 +1,6 @@
|
||||
package org.unifiedpush.distributor.nextpush.account
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
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
|
||||
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotInstalledException
|
||||
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 org.unifiedpush.distributor.nextpush.R
|
||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
|
||||
private const val PREF_NAME = "NextPush"
|
||||
private const val PREF_DEVICE_ID = "deviceId"
|
||||
@ -28,72 +8,6 @@ private const val PREF_URL = "url"
|
||||
|
||||
object AccountUtils {
|
||||
|
||||
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, showDialog: Boolean = false): Boolean {
|
||||
try {
|
||||
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context)
|
||||
} catch (e: NextcloudFilesAppAccountNotFoundException) {
|
||||
if (showDialog) {
|
||||
nextcloudAppNotInstalledDialog(context)
|
||||
}
|
||||
return false
|
||||
} catch (e: NoCurrentAccountSelectedException) {
|
||||
Log.d(TAG, "Device is not connected")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fun connect(activity: Activity) {
|
||||
try {
|
||||
AccountImporter.pickNewAccount(activity)
|
||||
} catch (e: NextcloudFilesAppNotInstalledException) {
|
||||
nextcloudAppNotInstalledDialog(activity)
|
||||
} catch (e: AndroidGetAccountsPermissionNotGranted) {
|
||||
UiExceptionManager.showDialogForException(activity, e)
|
||||
}
|
||||
}
|
||||
|
||||
fun saveDeviceId(context: Context, deviceId: String) {
|
||||
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
.edit()
|
||||
|
@ -0,0 +1,134 @@
|
||||
package org.unifiedpush.distributor.nextpush.account
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
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.* // ktlint-disable no-wildcard-imports
|
||||
import com.nextcloud.android.sso.helper.SingleAccountHelper
|
||||
import com.nextcloud.android.sso.model.SingleSignOnAccount
|
||||
import com.nextcloud.android.sso.ui.UiExceptionManager
|
||||
import org.unifiedpush.distributor.nextpush.R
|
||||
import org.unifiedpush.distributor.nextpush.api.provider.ApiSSOFactory
|
||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
|
||||
class SSOAccountFactory : AccountFactory {
|
||||
override val apiFactory: Class<*> = ApiSSOFactory::class.java
|
||||
override val name: String
|
||||
get() = ssoAccount!!.name
|
||||
override val url: String
|
||||
get() = ssoAccount!!.url
|
||||
|
||||
private var ssoAccount: SingleSignOnAccount? = null
|
||||
|
||||
override fun initAccount(context: Context) {
|
||||
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context)
|
||||
}
|
||||
|
||||
override fun isConnected(context: Context): Boolean {
|
||||
try {
|
||||
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context)
|
||||
} catch (e: NextcloudFilesAppAccountNotFoundException) {
|
||||
return false
|
||||
} catch (e: NoCurrentAccountSelectedException) {
|
||||
Log.d(TAG, "Device is not connected")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun connect(activity: Activity) {
|
||||
Log.d(TAG, "Starting ResultActivity")
|
||||
try {
|
||||
AccountImporter.pickNewAccount(activity)
|
||||
} catch (e: NextcloudFilesAppNotInstalledException) {
|
||||
nextcloudAppNotInstalledDialog(activity)
|
||||
} catch (e: AndroidGetAccountsPermissionNotGranted) {
|
||||
UiExceptionManager.showDialogForException(activity, e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActivityResult(
|
||||
activity: Activity,
|
||||
requestCode: Int,
|
||||
resultCode: Int,
|
||||
data: Intent?,
|
||||
block: (success: Boolean) -> Unit
|
||||
) {
|
||||
var success = false
|
||||
try {
|
||||
AccountImporter.onActivityResult(
|
||||
requestCode,
|
||||
resultCode,
|
||||
data,
|
||||
activity
|
||||
) { account ->
|
||||
val context = activity.applicationContext
|
||||
|
||||
SingleAccountHelper.setCurrentAccount(context, account.name)
|
||||
|
||||
// Get the "default" account
|
||||
try {
|
||||
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context)
|
||||
success = true
|
||||
} catch (e: NextcloudFilesAppAccountNotFoundException) {
|
||||
nextcloudAppNotInstalledDialog(context)
|
||||
} catch (e: NoCurrentAccountSelectedException) {
|
||||
UiExceptionManager.showDialogForException(context, e)
|
||||
}
|
||||
}
|
||||
} catch (_: AccountImportCancelledException) {}
|
||||
block(success)
|
||||
}
|
||||
|
||||
override fun getAccount(context: Context): Any? {
|
||||
return ssoAccount
|
||||
}
|
||||
|
||||
private 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()
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package org.unifiedpush.distributor.nextpush.activities
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
@ -17,16 +18,9 @@ import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import androidx.core.view.isVisible
|
||||
import com.nextcloud.android.sso.AccountImporter
|
||||
import com.nextcloud.android.sso.AccountImporter.clearAllAuthTokens
|
||||
import com.nextcloud.android.sso.exceptions.AccountImportCancelledException
|
||||
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.R
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.connect
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.isConnected
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.nextcloudAppNotInstalledDialog
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.ssoAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.isConnected
|
||||
import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteApp
|
||||
import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteDevice
|
||||
import org.unifiedpush.distributor.nextpush.distributor.Distributor.getDb
|
||||
@ -40,54 +34,44 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var listView: ListView
|
||||
private var showLogout = false
|
||||
private var onResult: ((activity: Activity, requestCode: Int, resultCode: Int, data: Intent?, block: (success: Boolean) -> Unit) -> Unit)? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
setSupportActionBar(findViewById(R.id.toolbar))
|
||||
requestPermissions()
|
||||
if (isConnected(this, showDialog = true)) {
|
||||
if (isConnected(this)) {
|
||||
showMain()
|
||||
} else {
|
||||
findViewById<Button>(R.id.button_connection).setOnClickListener {
|
||||
connect(this)
|
||||
getAccount(this, uninitialized = true)?.let {
|
||||
onResult = { activity: Activity, i: Int, i1: Int, intent: Intent?, block: (success: Boolean) -> Unit ->
|
||||
it.onActivityResult(activity, i, i1, intent, block)
|
||||
}
|
||||
it.connect(this)
|
||||
}
|
||||
}
|
||||
showStart()
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
try {
|
||||
AccountImporter.onActivityResult(
|
||||
requestCode,
|
||||
resultCode,
|
||||
data,
|
||||
this
|
||||
) { account ->
|
||||
val context = applicationContext
|
||||
|
||||
SingleAccountHelper.setCurrentAccount(context, account.name)
|
||||
|
||||
// Get the "default" account
|
||||
try {
|
||||
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context)
|
||||
} catch (e: NextcloudFilesAppAccountNotFoundException) {
|
||||
nextcloudAppNotInstalledDialog(context)
|
||||
} catch (e: NoCurrentAccountSelectedException) {
|
||||
UiExceptionManager.showDialogForException(context, e)
|
||||
}
|
||||
showMain()
|
||||
}
|
||||
} catch (_: AccountImportCancelledException) {}
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
onResult?.let {
|
||||
it(this, requestCode, resultCode, data) { success ->
|
||||
if (success) {
|
||||
showMain()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showMain() {
|
||||
findViewById<ConstraintLayout>(R.id.sub_start).isVisible = false
|
||||
findViewById<ConstraintLayout>(R.id.sub_main).isVisible = true
|
||||
findViewById<TextView>(R.id.main_account_desc).text =
|
||||
format(getString(R.string.main_account_desc), ssoAccount.name)
|
||||
format(getString(R.string.main_account_desc), getAccount(this)?.name)
|
||||
showLogout = true
|
||||
invalidateOptionsMenu()
|
||||
RestartWorker.startPeriodic(this)
|
||||
|
@ -10,12 +10,12 @@ import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.sse.EventSource
|
||||
import okhttp3.sse.EventSources
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.getDeviceId
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.removeDeviceId
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.removeUrl
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.saveDeviceId
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.saveUrl
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.ssoAccount
|
||||
import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider
|
||||
import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.mApiEndpoint
|
||||
import org.unifiedpush.distributor.nextpush.api.provider.ApiProviderFactory
|
||||
@ -79,7 +79,8 @@ object Api {
|
||||
}
|
||||
|
||||
override fun onComplete() {
|
||||
saveUrl(this@apiSync, "${ssoAccount.url}$mApiEndpoint")
|
||||
saveUrl(this@apiSync, "${getAccount(this@apiSync)?.url}$mApiEndpoint")
|
||||
|
||||
// Sync once it is registered
|
||||
deviceId?.let {
|
||||
syncDevice(it)
|
||||
@ -96,7 +97,7 @@ object Api {
|
||||
.readTimeout(0, TimeUnit.SECONDS)
|
||||
.retryOnConnectionFailure(false)
|
||||
.build()
|
||||
val url = "${ssoAccount.url}$mApiEndpoint/device/$deviceId"
|
||||
val url = "${getAccount(this)?.url}$mApiEndpoint/device/$deviceId"
|
||||
|
||||
val request = Request.Builder().url(url)
|
||||
.get()
|
||||
|
@ -4,7 +4,8 @@ import android.content.Context
|
||||
import android.util.Log
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.nextcloud.android.sso.api.NextcloudAPI
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils
|
||||
import com.nextcloud.android.sso.model.SingleSignOnAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||
import retrofit2.NextcloudRetrofitApiBuilder
|
||||
|
||||
class ApiSSOFactory(val context: Context) : ApiProviderFactory {
|
||||
@ -14,6 +15,10 @@ class ApiSSOFactory(val context: Context) : ApiProviderFactory {
|
||||
private lateinit var nextcloudAPI: NextcloudAPI
|
||||
|
||||
override fun getProviderAndExecute(block: (ApiProvider) -> Unit) {
|
||||
val account = getAccount(context) ?: run {
|
||||
Log.w(TAG, "No account found")
|
||||
return
|
||||
}
|
||||
apiProvider?.let(block)
|
||||
?: run {
|
||||
Log.d(TAG, "Creating new provider")
|
||||
@ -33,7 +38,7 @@ class ApiSSOFactory(val context: Context) : ApiProviderFactory {
|
||||
}
|
||||
nextcloudAPI = NextcloudAPI(
|
||||
context,
|
||||
AccountUtils.ssoAccount,
|
||||
account.getAccount(context) as SingleSignOnAccount,
|
||||
GsonBuilder().create(),
|
||||
ssoApiCallback
|
||||
)
|
||||
|
@ -5,7 +5,7 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.PowerManager
|
||||
import android.util.Log
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.isConnected
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.isConnected
|
||||
import org.unifiedpush.distributor.nextpush.distributor.* // ktlint-disable no-wildcard-imports
|
||||
import org.unifiedpush.distributor.nextpush.distributor.Distributor.TOKEN_NEW
|
||||
import org.unifiedpush.distributor.nextpush.distributor.Distributor.TOKEN_NOK
|
||||
@ -56,7 +56,7 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
|
||||
connectorToken
|
||||
)
|
||||
TOKEN_NEW -> {
|
||||
if (!isConnected(context.applicationContext, showDialog = false)) {
|
||||
if (!isConnected(context.applicationContext)) {
|
||||
sendRegistrationFailed(
|
||||
context.applicationContext,
|
||||
application,
|
||||
|
@ -7,11 +7,7 @@ import android.os.Build
|
||||
import android.os.IBinder
|
||||
import android.os.PowerManager
|
||||
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 org.unifiedpush.distributor.nextpush.account.AccountUtils.nextcloudAppNotInstalledDialog
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountUtils.ssoAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.api.Api.apiDestroy
|
||||
import org.unifiedpush.distributor.nextpush.api.Api.apiSync
|
||||
import org.unifiedpush.distributor.nextpush.api.SSEListener.Companion.lastEventDate
|
||||
@ -61,15 +57,11 @@ class StartService : Service() {
|
||||
// If the service is running and we don't have any fail
|
||||
// In case somehow startService is called when everything is fine
|
||||
if (isServiceStarted && !FailureHandler.hasFailed()) return
|
||||
isServiceStarted = true
|
||||
|
||||
try {
|
||||
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(this)
|
||||
} catch (e: NextcloudFilesAppAccountNotFoundException) {
|
||||
nextcloudAppNotInstalledDialog(this)
|
||||
} catch (e: NoCurrentAccountSelectedException) {
|
||||
getAccount(this) ?: run {
|
||||
Log.d(TAG, "No account found")
|
||||
return
|
||||
}
|
||||
isServiceStarted = true
|
||||
|
||||
// we need this lock so our service gets not affected by Doze Mode
|
||||
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
|
||||
|
Loading…
x
Reference in New Issue
Block a user