Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
circles-android
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Circles
circles-android
Commits
07c7f7f3
Commit
07c7f7f3
authored
2 years ago
by
Taras
Browse files
Options
Downloads
Patches
Plain Diff
Start working on unified push helper
parent
f85a1365
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/src/main/java/org/futo/circles/feature/notifications/UnifiedPushHelper.kt
+139
-0
139 additions, 0 deletions
...g/futo/circles/feature/notifications/UnifiedPushHelper.kt
with
139 additions
and
0 deletions
app/src/main/java/org/futo/circles/feature/notifications/UnifiedPushHelper.kt
0 → 100644
+
139
−
0
View file @
07c7f7f3
package
org.futo.circles.feature.notifications
import
android.content.Context
import
androidx.appcompat.app.AppCompatActivity
import
androidx.lifecycle.lifecycleScope
import
com.google.gson.Gson
import
kotlinx.coroutines.launch
import
org.futo.circles.R
import
org.futo.circles.feature.notifications.model.DiscoveryResponse
import
org.futo.circles.provider.MatrixInstanceProvider
import
org.futo.circles.provider.PreferencesProvider
import
org.matrix.android.sdk.api.cache.CacheStrategy
import
org.unifiedpush.android.connector.UnifiedPush
import
java.net.URL
class
UnifiedPushHelper
(
private
val
context
:
Context
,
private
val
preferencesProvider
:
PreferencesProvider
,
private
val
fcmHelper
:
FcmHelper
,
)
{
fun
register
(
activity
:
AppCompatActivity
,
onDoneRunnable
:
Runnable
?
=
null
,
)
{
registerInternal
(
activity
,
onDoneRunnable
=
onDoneRunnable
)
}
private
fun
registerInternal
(
activity
:
AppCompatActivity
,
force
:
Boolean
=
false
,
pushersManager
:
PushersManager
?
=
null
,
onDoneRunnable
:
Runnable
?
=
null
)
{
activity
.
lifecycleScope
.
launch
{
if
(!
vectorFeatures
.
allowExternalUnifiedPushDistributors
())
{
UnifiedPush
.
saveDistributor
(
context
,
context
.
packageName
)
UnifiedPush
.
registerApp
(
context
)
onDoneRunnable
?.
run
()
return
@launch
}
if
(
force
)
{
// Un-register first
unregister
(
pushersManager
)
}
// the !force should not be needed
if
(!
force
&&
UnifiedPush
.
getDistributor
(
context
).
isNotEmpty
())
{
UnifiedPush
.
registerApp
(
context
)
onDoneRunnable
?.
run
()
return
@launch
}
val
distributors
=
UnifiedPush
.
getDistributors
(
context
)
if
(!
force
&&
distributors
.
size
==
1
)
{
UnifiedPush
.
saveDistributor
(
context
,
distributors
.
first
())
UnifiedPush
.
registerApp
(
context
)
onDoneRunnable
?.
run
()
}
else
{
openDistributorDialogInternal
(
activity
=
activity
,
onDoneRunnable
=
onDoneRunnable
,
distributors
=
distributors
)
}
}
}
suspend
fun
unregister
(
pushersManager
:
PushersManager
?
=
null
)
{
val
mode
=
BackgroundSyncMode
.
FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME
vectorPreferences
.
setFdroidSyncBackgroundMode
(
mode
)
try
{
getEndpointOrToken
()
?.
let
{
pushersManager
?.
unregisterPusher
(
it
)
}
}
catch
(
ignore
:
Exception
)
{
}
preferencesProvider
.
storeUnifiedPushEndpoint
(
null
)
preferencesProvider
.
storePushGateway
(
null
)
UnifiedPush
.
unregisterApp
(
context
)
}
suspend
fun
storeCustomOrDefaultGateway
(
endpoint
:
String
,
onDoneRunnable
:
Runnable
?
=
null
)
{
if
(
UnifiedPush
.
getDistributor
(
context
)
==
context
.
packageName
)
{
preferencesProvider
.
storePushGateway
(
context
.
getString
(
R
.
string
.
pusher_http_url
))
onDoneRunnable
?.
run
()
return
}
val
gateway
=
context
.
getString
(
R
.
string
.
default_push_gateway_http_url
)
val
parsed
=
URL
(
endpoint
)
val
custom
=
"${parsed.protocol}://${parsed.host}/_matrix/push/v1/notify"
try
{
val
response
=
MatrixInstanceProvider
.
matrix
.
rawService
().
getUrl
(
custom
,
CacheStrategy
.
NoCache
)
val
discoveryResponse
=
Gson
().
fromJson
(
response
,
DiscoveryResponse
::
class
.
java
)
if
(
discoveryResponse
.
unifiedpush
.
gateway
==
"matrix"
)
{
preferencesProvider
.
storePushGateway
(
custom
)
onDoneRunnable
?.
run
()
return
}
}
catch
(
ignore
:
Throwable
)
{
}
preferencesProvider
.
storePushGateway
(
gateway
)
onDoneRunnable
?.
run
()
}
fun
isEmbeddedDistributor
():
Boolean
{
return
isInternalDistributor
()
&&
fcmHelper
.
isFirebaseAvailable
()
}
fun
isBackgroundSync
():
Boolean
{
return
isInternalDistributor
()
&&
!
fcmHelper
.
isFirebaseAvailable
()
}
private
fun
isInternalDistributor
():
Boolean
{
return
UnifiedPush
.
getDistributor
(
context
).
isEmpty
()
||
UnifiedPush
.
getDistributor
(
context
)
==
context
.
packageName
}
fun
getEndpointOrToken
():
String
?
{
return
if
(
isEmbeddedDistributor
())
fcmHelper
.
getFcmToken
()
else
preferencesProvider
.
getUnifiedPushEndpoint
()
}
fun
getPushGateway
():
String
?
{
return
if
(
isEmbeddedDistributor
())
context
.
getString
(
R
.
string
.
pusher_http_url
)
else
preferencesProvider
.
getPushGateway
()
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment