diff --git a/docs/notifications.md b/docs/notifications.md new file mode 100644 index 0000000000..ad8a1c8430 --- /dev/null +++ b/docs/notifications.md @@ -0,0 +1,281 @@ +This document aims to describe how Riot X android displays notifications to the end user. It also clarifies notifications and background settings in the app. + +# Table of Contents +1. [Prerequisites Knowledge](#prerequisites-knowledge) + * [How does a matrix client gets a message from a Home Server?](#how-does-a-matrix-client-gets-a-message-from-a-home-server) + * [How does a mobile app receives push notification?](#how-does-a-mobile-app-receives-push-notification) + * [Push VS Notification](#push-vs-notification) + * [Push in the matrix federated world](#push-in-the-matrix-federated-world) + * [How does the Home Server knows when to notify a client?](#how-does-the-home-server-knows-when-to-notify-a-client) + * [Push vs privacy, and mitigation](#push-vs-privacy-and-mitigation) + * [Background processing limitations](#background-processing-limitations) +2. [RiotX Notification implementations](#riotx-notification-implementations) + * [Requirements](#requirements) + * [Foreground sync mode (Gplay & Fdroid)](#foreground-sync-mode-gplay-fdroid) + * [Push (FCM) received in background](#push-fcm-received-in-background) + * [FCM Fallback mode](#fcm-fallback-mode) + * [f-droid background Mode](#f-droid-background-mode) +3. [Application Settings](#application-settings) + + +First let's start with some prerequisite knowledge + +# Prerequisites Knowledge + +## How does a matrix client gets a message from a Home Server? + +In order to get messages from a home server, a matrix client need to perform a ``sync`` operation. + +`To read events, the intended flow of operation is for clients to first call the /sync API without a since parameter. This returns the most recent message events for each room, as well as the state of the room at the start of the returned timeline. ` + +The client need to call the `sync`API periodically in order to get incremental updates of the server state (new messages). +This mechanism is known as **HTTP long pooling**. + +Using the **HTTP Long pooling** mechanism a client polls a server requesting new information. +The server *holds the request open until new data is available*. +Once available, the server responds and sends the new information. +When the client receives the new information, it immediately sends another request, and the operation is repeated. +This effectively emulates a server push feature. + +The HTTP long pooling can be fine tuned in the **SDK** using two parameters: +* timout (Sync request timeout) +* delay (Delay between each sync) + +**timeout** is a server paramter, defined by: +``` +The maximum time to wait, in milliseconds, before returning this request.` +If no events (or other data) become available before this time elapses, the server will return a response with empty fields. +By default, this is 0, so the server will return immediately even if the response is empty. +``` + +**delay** is a client preference. When the server responds to a sync request, the client waits for `delay`before calling a new sync. + +When the Riot X Android app is open (i.e in foreground state), the default timeout is 30 seconds, and delay is 0. + +## How does a mobile app receives push notification + +Push notification is used as a way to wake up a mobile application when some important information is available and should be processed. + +Typically in order to get push notification, an application relies on a **Push Notification Service** or **Push Provider**. + +For example iOS uses APNS (Apple Push Notification Service). +Most of android devices relies on Google's Firebase Cloud Messaging (FCM). + > FCM has replaced Google Cloud Messaging (GCM - deprecated April 10 2018) + +FCM will only work on android devices that have Google plays services installed +(In simple terms, Google Play Services is a background service that runs on Android, which in turn helps in integrating Google’s advanced functionalities to other applications) + +De-Googlified devices need to rely on something else in order to stay up to date with a server. +There some cases when devices with google services cannot use FCM (network infrastructure limitations -firewalls- , + privacy and or independency requirement, source code licence) + +## Push VS Notification + +This need some disambiguation, because it is the source of common confusion: + + +*The fact that you see a notification on your screen does not mean that you have successfully configured your PUSH plateform.* + + Technically there is a difference between a push and a notification. A notification is what you see on screen and/or in the notification Menu/Drawer (in the top bar of the phone). + + Notifications are not always triggered by a push (One can display a notification locally triggered by an alarm) + + +## Push in the matrix federated world + +In order to send a push to a mobile, App developers need to have a server that will use the FCM APIs, and these APIs requires authentication! +This server is called a **Push Gateway** in the matrix world + +That means that Riot X Android, a matrix client created by New Vector, is using a **Push Gateway** with the needed credentials (FCM API secret Key) in order to send push to the New Vector client. + +If you create your own matrix client, you will also need to deploy an instance of a **Push Gateway** with the credentials needed to use FCM for your app. + +On registration, a matrix client must tell to it's Home Server what Push Gateway to use. + +See [Sygnal](https://github.com/matrix-org/sygnal/) for a reference implementation. +``` + + +--------------------+ +-------------------+ + Matrix HTTP | | | | + Notification Protocol | App Developer | | Device Vendor | + | | | | + +-------------------+ | +----------------+ | | +---------------+ | + | | | | | | | | | | + | Matrix homeserver +-----> Push Gateway +------> Push Provider | | + | | | | | | | | | | + +-^-----------------+ | +----------------+ | | +----+----------+ | + | | | | | | + Matrix | | | | | | +Client/Server API + | | | | | + | | +--------------------+ +-------------------+ + | +--+-+ | + | | <-------------------------------------------+ + +---+ | + | | Provider Push Protocol + +----+ + + Mobile Device or Client +``` + +Recommended reading: + * https://thomask.sdf.org/blog/2016/12/11/riots-magical-push-notifications-in-ios.html +* https://matrix.org/docs/spec/client_server/r0.4.0.html#id128 + + +## How does the Home Server knows when to notify a client? + +This is defined by [**push rules**](https://matrix.org/docs/spec/client_server/r0.4.0.html#push-rules-). + +`A push rule is a single rule that states under what conditions an event should be passed onto a push gateway and how the notification should be presented (sound / importance).` + +A Home Server can be configured with default rules (for Direct messages, group messages, mentions, etc.. ). + +There are different kind of push rules, it can be per room (each new message on this room should be notified), it can also define a pattern that a message should match (when you are mentioned, or key word based). + +Notifications have 2 'levels' (`highlighted = true/false sound = default/custom`). In RiotX these notifications level are reflected as Noisy/Silent. + +**What about encrypted messages?** + +Of course, content patterns matching cannot be used for encrypted messages server side (as the content is encrypted). + +That is why clients are able to **process the push rules client side** to decide what kind of notification should be presented for a given event. + +## Push vs privacy, and mitigation + +As seen previously, App developers don't directly send a push to the end user's device, they use a Push Provider as intermediary. So technically this intermediary is able to read the content of what is sent. + +App developers usually mitigate this by sending a `silent notification`, that is a notification with no identifiable data, or with an encrypted payload. When the push is received the app can then synchronise to it's server in order to generate a local notification. + + +## Background processing limitations + +A mobile applications process live in a managed word, meaning that its process can be limited (e.g no network access), stopped or killed at almost anytime by the Operating System. + +In order to improve the battery life of their devices some constructors started to implement mechanism to drastically limit background execution of applications (e.g MIUI/Xiaomi restrictions, Sony stamina mode). +Then starting android M, android has also put more focus on improving device performances, introducing several IDLE modes, App-Standby, Light Doze, Doze. + +In a nutshell, apps can't do much in background now. + +If the devices is not plugged and stays IDLE for a certain amount of time, radio (mobile connectivity) and CPU can/will be turned off. + +For an application like riot X, where users can receive important information at anytime, the best option is to rely on a push system (Google's Firebase Message a.k.a FCM). FCM high priority push can wake up the device and whitelist an application to perform background task (for a limited but unspecified amount of time). + +Notice that this is still evolving, and in future versions application that has been 'background restricted' by users won't be able to wake up even when a high priority push is received. Also high priority notifications could be rate limited (not defined anywhere) + +It's getting a lot more complicated when you cannot rely on FCM (because: closed sources, network/firewall restrictions, privacy concerns). +The documentation on this subject is vague, and as per our experiments not always exact, also device's behaviour is fragmented. + +It is getting more and more complex to have reliable notifications when FCM is not used. + +# RiotX Notification implementations + +## Requirements + +RiotX Android must work with and without FCM. +* The riotX android app published on fdroid do not rely on FCM (all related dependencies are not present) +* The RiotX android app published on google play rely on FCM, with a fallback mode when FCM registration has failed (e.g outdated or missing Google Play Services) + +## Foreground sync mode (Gplay & Fdroid) + +When in foreground, riotX performs sync continuously with a timeout value set to 10 seconds (see HttpPooling). + +As this mode does not need to live beyond the scope of the application, and as per Google recommendation, riotX uses the internal app resources (Thread and Timers) to perform the syncs. + +This mode is turned on when the app enters foreground, and off when enters background. + +In background, and depending on wether push is available or not, riotX will use different methods to perform the syncs (Workers / Alarms / Service) + +## Push (FCM) received in background + +In order to enable Push, riotX must first get a push token from the firebase SDK, then register a pusher with this token on the HomeServer. + +When a message should be notified to a user, the user's homeserver notifies the registered `push gateway` for riotX, that is [sygnal](https://github.com/matrix-org/sygnal) _- The reference implementation for push gateways -_ hosted by matrix.org. + +This sygnal instance is configured with the required FCM API authentication token, and will then use the FCM API in order to notify the user's device running riotX. + +``` +Homeserver ----> Sygnal (configured for riotX) ----> FCM ----> RiotX +``` + +The push gateway is configured to only send `(eventId,roomId)` in the push payload (for better [privacy](#push-vs-privacy-and-mitigation)). + +RiotX needs then to synchronise with the user's HomeServer, in order to resolve the event and create a notification. + +As per [Google recommendation](https://android-developers.googleblog.com/2018/09/notifying-your-users-with-fcm.html), riotX will then use the WorkManager API in order to trigger a background sync. + +**Google recommendations:** +> We recommend using FCM messages in combination with the WorkManager 1 or JobScheduler API + +> Avoid background services. One common pitfall is using a background service to fetch data in the FCM message handler, since background service will be stopped by the system per recent changes to Google Play Policy + +``` +Homeserver ----> Sygnal ----> FCM ----> RiotX + (Sync) ----> Homeserver + <---- + Display notification +``` + +**Possible outcomes** + +Upon reception of the FCM push, RiotX will perform a sync call to the Home Server, during this process it is possible that: + * Happy path, the sync is performed, the message resolved and displayed in the notification drawer + * The notified message is not in the sync. Can happen if a lot of things did happen since the push (`gappy sync`) + * The sync generates additional notifications (e.g an encrypted message where the user is mentioned detected locally) + * The sync takes too long and the process is killed before completion, or network is not reliable and the sync fails. + +Riot X implements several strategies in these cases (TODO document) + +## FCM Fallback mode + +It is possible that riotX is not able to get a FCM push token. +Common errors (amoung several others) that can cause that: +* Google Play Services is outdated +* Google Play Service fails in someways with FCM servers (infamous `SERVICE_NOT_AVAILABLE`) + +If riotX is able to detect one of this cases, it will notifies it to the users and when possible help him fix it via a dedicated troubleshoot screen. + +Meanwhile, in order to offer a minimal service, and as per Google's recommendation for background activities, riotX will launch periodic background sync in order to stays in sync with servers. + +The fallback mode is impacted by all the battery life saving mechanism implemented by android. Meaning that if the app is not used for a certain amount of time (`App-Standby`), or the device stays still and unplugged (`Light Doze`) , the sync will become less frequent. + +And if the device stays unplugged and still for too long (`Doze Mode`), no background sync will be perform at all (the system's `Ignore Battery Optimization option` has no effect on that). + + Also the time interval between sync is elastic, controlled by the system to group other apps background sync request and start radio/cpu only once for all. + +Usually in this mode, what happen is when you take back your phone in your hand, you suddenly receive notifications. + +The fallback mode is supposed to be a temporary state waiting for the user to fix issues for FCM, or for App Developers that has done a fork to correctly configure their FCM settings. + +## f-droid background Mode + +The f-droid riotX flavor has no dependencies to FCM, therefore cannot relies on Push. + +Also Google's recommended background processing method cannot be applied. This is because all of these methods are affected by IDLE modes, and will result on the user not being notified at all when the app is in a Doze mode (only in maintenance windows that could happens only after hours). + +Only solution left is to use `AlarmManager`, that offers new API to allow launching some process even if the App is in IDLE modes. + +Notice that these alarms, due to their potential impact on battery life, can still be restricted by the system. Documentation says that they will not be triggered more than every minutes under normal system operation, and when in low power mode about every 15 mn. + +These restrictions can be relaxed by requirering the app to be white listed from battery optimization. + +F-droid version will schedule alarms that will then trigger a Broadcast Receiver, that in turn will launch a Service (in the classic android way), and the reschedule an alarm for next time. + +Depending on the system status (or device make), it is still possible that the app is not given enough time to launch the service, or that the radio is still turned off thus preventing the sync to success (that's why Alarms are not recommended for network related tasks). + +That is why on riotX Fdroid, the broadcast receiver will acquire a temporary WAKE_LOCK for several seconds (thus securing cpu/network), and launch the service in foreground. The service performs the sync. + +Note that foreground services require to put a notification informing the user that the app is doing something even if not launched). + + + +# Application Settings + +**Notifications > Enable notifications for this account** + +Configure Sygnal to send or not notifications to all user devices. + +**Notifications > Enable notifications for this device** + +Disable notifications locally. The push server will continue to send notifications to the device but this one will ignore them. + + diff --git a/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxRoom.kt b/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxRoom.kt index c9dbbdd068..6c5724be29 100644 --- a/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxRoom.kt +++ b/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxRoom.kt @@ -23,7 +23,7 @@ import io.reactivex.Observable class RxRoom(private val room: Room) { fun liveRoomSummary(): Observable { - return room.roomSummary.asObservable() + return room.liveRoomSummary.asObservable() } fun liveRoomMemberIds(): Observable> { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/Matrix.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/Matrix.kt index 3844e27b7a..d6665f7fa6 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/Matrix.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/Matrix.kt @@ -21,7 +21,10 @@ import androidx.lifecycle.ProcessLifecycleOwner import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.BuildConfig import im.vector.matrix.android.api.auth.Authenticator +import im.vector.matrix.android.api.pushrules.Action +import im.vector.matrix.android.api.pushrules.PushRuleService import im.vector.matrix.android.api.session.Session +import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.sync.FilterService import im.vector.matrix.android.internal.auth.AuthModule import im.vector.matrix.android.internal.di.MatrixKoinComponent @@ -30,7 +33,9 @@ import im.vector.matrix.android.internal.di.MatrixModule import im.vector.matrix.android.internal.di.NetworkModule import im.vector.matrix.android.internal.network.UserAgentHolder import im.vector.matrix.android.internal.util.BackgroundDetectionObserver +import org.koin.standalone.get import org.koin.standalone.inject +import timber.log.Timber import java.util.concurrent.atomic.AtomicBoolean /** diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/Action.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/Action.kt new file mode 100644 index 0000000000..797830f491 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/Action.kt @@ -0,0 +1,95 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.pushrules + +import im.vector.matrix.android.api.pushrules.rest.PushRule +import timber.log.Timber + + +class Action(val type: Type) { + + enum class Type(val value: String) { + NOTIFY("notify"), + DONT_NOTIFY("dont_notify"), + COALESCE("coalesce"), + SET_TWEAK("set_tweak"); + + companion object { + + fun safeValueOf(value: String): Type? { + try { + return valueOf(value) + } catch (e: IllegalArgumentException) { + return null + } + } + } + } + + var tweak_action: String? = null + var stringValue: String? = null + var boolValue: Boolean? = null + + companion object { + fun mapFrom(pushRule: PushRule): List? { + val actions = ArrayList() + pushRule.actions.forEach { actionStrOrObj -> + if (actionStrOrObj is String) { + when (actionStrOrObj) { + Action.Type.NOTIFY.value -> Action(Action.Type.NOTIFY) + Action.Type.DONT_NOTIFY.value -> Action(Action.Type.DONT_NOTIFY) + else -> { + Timber.w("Unsupported action type ${actionStrOrObj}") + null + } + }?.let { + actions.add(it) + } + } else if (actionStrOrObj is Map<*, *>) { + val tweakAction = actionStrOrObj["set_tweak"] as? String + when (tweakAction) { + "sound" -> { + (actionStrOrObj["value"] as? String)?.let { stringValue -> + Action(Action.Type.SET_TWEAK).also { + it.tweak_action = "sound" + it.stringValue = stringValue + actions.add(it) + } + } + } + "highlight" -> { + (actionStrOrObj["value"] as? Boolean)?.let { boolValue -> + Action(Action.Type.SET_TWEAK).also { + it.tweak_action = "highlight" + it.boolValue = boolValue + actions.add(it) + } + } + } + else -> { + Timber.w("Unsupported action type ${actionStrOrObj}") + } + } + } else { + Timber.w("Unsupported action type ${actionStrOrObj}") + return null + } + } + return if (actions.isEmpty()) null else actions + } + } +} + diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/Condition.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/Condition.kt new file mode 100644 index 0000000000..c0bb4f16f4 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/Condition.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.pushrules + +abstract class Condition(val kind: Kind) { + + enum class Kind(val value: String) { + event_match("event_match"), + contains_display_name("contains_display_name"), + room_member_count("room_member_count"), + sender_notification_permission("sender_notification_permission"), + UNRECOGNIZE(""); + + companion object { + + fun fromString(value: String): Kind { + return when (value) { + "event_match" -> event_match + "contains_display_name" -> contains_display_name + "room_member_count" -> room_member_count + "sender_notification_permission" -> sender_notification_permission + else -> UNRECOGNIZE + } + } + + } + + } + + abstract fun isSatisfied(conditionResolver: ConditionResolver): Boolean + + open fun technicalDescription(): String { + return "Kind: $kind" + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/ConditionResolver.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/ConditionResolver.kt new file mode 100644 index 0000000000..4d15d5deec --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/ConditionResolver.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.pushrules + +/** + * Acts like a visitor on Conditions. + * This class as all required context needed to evaluate rules + */ +interface ConditionResolver { + + fun resolveEventMatchCondition(eventMatchCondition: EventMatchCondition): Boolean + fun resolveRoomMemberCountCondition(roomMemberCountCondition: RoomMemberCountCondition): Boolean + fun resolveSenderNotificationPermissionCondition(senderNotificationPermissionCondition: SenderNotificationPermissionCondition): Boolean + fun resolveContainsDisplayNameCondition(containsDisplayNameCondition: ContainsDisplayNameCondition) : Boolean +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/ContainsDisplayNameCondition.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/ContainsDisplayNameCondition.kt new file mode 100644 index 0000000000..ce9f88e377 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/ContainsDisplayNameCondition.kt @@ -0,0 +1,80 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.pushrules + +import android.text.TextUtils +import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.api.session.events.model.EventType +import im.vector.matrix.android.api.session.events.model.toModel +import im.vector.matrix.android.api.session.room.model.message.MessageContent +import im.vector.matrix.android.api.session.room.timeline.TimelineEvent +import timber.log.Timber +import java.util.regex.Pattern + +class ContainsDisplayNameCondition : Condition(Kind.contains_display_name) { + + override fun isSatisfied(conditionResolver: ConditionResolver): Boolean { + return conditionResolver.resolveContainsDisplayNameCondition(this) + } + + override fun technicalDescription(): String { + return "User is mentioned" + } + + fun isSatisfied(event: Event, displayName: String): Boolean { + //TODO the spec says: + // Matches any message whose content is unencrypted and contains the user's current display name + var message = when (event.type) { + EventType.MESSAGE -> { + event.content.toModel() + } +// EventType.ENCRYPTED -> { +// event.root.getClearContent()?.toModel() +// } + else -> null + } ?: return false + + return caseInsensitiveFind(displayName, message.body) + } + + + companion object { + /** + * Returns whether a string contains an occurrence of another, as a standalone word, regardless of case. + * + * @param subString the string to search for + * @param longString the string to search in + * @return whether a match was found + */ + fun caseInsensitiveFind(subString: String, longString: String): Boolean { + // add sanity checks + if (TextUtils.isEmpty(subString) || TextUtils.isEmpty(longString)) { + return false + } + + var res = false + + try { + val pattern = Pattern.compile("(\\W|^)" + Pattern.quote(subString) + "(\\W|$)", Pattern.CASE_INSENSITIVE) + res = pattern.matcher(longString).find() + } catch (e: Exception) { + Timber.e(e, "## caseInsensitiveFind() : failed") + } + + return res + } + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/EventMatchCondition.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/EventMatchCondition.kt new file mode 100644 index 0000000000..7325abba2a --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/EventMatchCondition.kt @@ -0,0 +1,97 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.pushrules + +import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.internal.di.MoshiProvider +import timber.log.Timber + +class EventMatchCondition(val key: String, val pattern: String) : Condition(Kind.event_match) { + + override fun isSatisfied(conditionResolver: ConditionResolver) : Boolean { + return conditionResolver.resolveEventMatchCondition(this) + } + + override fun technicalDescription(): String { + return "'$key' Matches '$pattern'" + } + + + fun isSatisfied(event: Event): Boolean { + //TODO encrypted events? + val rawJson = MoshiProvider.providesMoshi().adapter(Event::class.java).toJsonValue(event) as? Map<*, *> + ?: return false + val value = extractField(rawJson, key) ?: return false + + //Patterns with no special glob characters should be treated as having asterisks prepended + // and appended when testing the condition. + try { + val modPattern = if (hasSpecialGlobChar(pattern)) simpleGlobToRegExp(pattern) else simpleGlobToRegExp("*$pattern*") + val regex = Regex(modPattern, RegexOption.DOT_MATCHES_ALL) + return regex.containsMatchIn(value) + } catch (e: Throwable) { + //e.g PatternSyntaxException + Timber.e(e, "Failed to evaluate push condition") + return false + } + + } + + + private fun extractField(jsonObject: Map<*, *>, fieldPath: String): String? { + val fieldParts = fieldPath.split(".") + if (fieldParts.isEmpty()) return null + + var jsonElement: Map<*, *> = jsonObject + fieldParts.forEachIndexed { index, pathSegment -> + if (index == fieldParts.lastIndex) { + return jsonElement[pathSegment]?.toString() + } else { + val sub = jsonElement[pathSegment] ?: return null + if (sub is Map<*, *>) { + jsonElement = sub + } else { + return null + } + } + } + return null + } + + companion object { + + private fun hasSpecialGlobChar(glob: String): Boolean { + return glob.contains("*") || glob.contains("?") + } + + //Very simple glob to regexp converter + private fun simpleGlobToRegExp(glob: String): String { + var out = ""//"^" + for (i in 0 until glob.length) { + val c = glob[i] + when (c) { + '*' -> out += ".*" + '?' -> out += '.'.toString() + '.' -> out += "\\." + '\\' -> out += "\\\\" + else -> out += c + } + } + out += ""//'$'.toString() + return out + } + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/PushRuleService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/PushRuleService.kt new file mode 100644 index 0000000000..b00450b561 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/PushRuleService.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.pushrules + +import im.vector.matrix.android.api.MatrixCallback +import im.vector.matrix.android.api.pushrules.rest.PushRule +import im.vector.matrix.android.api.session.events.model.Event + +interface PushRuleService { + + /** + * Fetch the push rules from the server + */ + fun fetchPushRules(scope: String = "global") + + //TODO get push rule set + fun getPushRules(scope: String = "global"): List + + //TODO update rule + + fun updatePushRuleEnableStatus(kind: String, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback) + + fun addPushRuleListener(listener: PushRuleListener) + + fun removePushRuleListener(listener: PushRuleListener) + +// fun fulfilledBingRule(event: Event, rules: List): PushRule? + + interface PushRuleListener { + fun onMatchRule(event: Event, actions: List) + fun batchFinish() + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RoomMemberCountCondition.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RoomMemberCountCondition.kt new file mode 100644 index 0000000000..b2bed4c230 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RoomMemberCountCondition.kt @@ -0,0 +1,71 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.pushrules + +import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.api.session.room.RoomService +import timber.log.Timber +import java.util.regex.Pattern + +private val regex = Pattern.compile("^(==|<=|>=|<|>)?(\\d*)$") + +class RoomMemberCountCondition(val `is`: String) : Condition(Kind.room_member_count) { + + override fun isSatisfied(conditionResolver: ConditionResolver): Boolean { + return conditionResolver.resolveRoomMemberCountCondition(this) + } + + override fun technicalDescription(): String { + return "Room member count is $`is`" + } + + fun isSatisfied(event: Event, session: RoomService?): Boolean { + // sanity check^ + val roomId = event.roomId ?: return false + val room = session?.getRoom(roomId) ?: return false + + // Parse the is field into prefix and number the first time + val (prefix, count) = parseIsField() ?: return false + + val numMembers = room.getNumberOfJoinedMembers() + + return when (prefix) { + "<" -> numMembers < count + ">" -> numMembers > count + "<=" -> numMembers <= count + ">=" -> numMembers >= count + else -> numMembers == count + } + } + + /** + * Parse the is field to extract meaningful information. + */ + private fun parseIsField(): Pair? { + try { + val match = regex.matcher(`is`) + if (match.find()) { + val prefix = match.group(1) + val count = match.group(2).toInt() + return prefix to count + } + } catch (t: Throwable) { + Timber.d(t) + } + return null + + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleIds.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleIds.kt new file mode 100644 index 0000000000..38a64adf27 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleIds.kt @@ -0,0 +1,47 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.matrix.android.api.pushrules + +/** + * Known rule ids + * + * Ref: https://matrix.org/docs/spec/client_server/latest#predefined-rules + */ +object RuleIds { + // Default Override Rules + const val RULE_ID_DISABLE_ALL = ".m.rule.master" + const val RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS = ".m.rule.suppress_notices" + const val RULE_ID_INVITE_ME = ".m.rule.invite_for_me" + const val RULE_ID_PEOPLE_JOIN_LEAVE = ".m.rule.member_event" + const val RULE_ID_CONTAIN_DISPLAY_NAME = ".m.rule.contains_display_name" + + const val RULE_ID_TOMBSTONE = ".m.rule.tombstone" + const val RULE_ID_ROOM_NOTIF = ".m.rule.roomnotif" + + // Default Content Rules + const val RULE_ID_CONTAIN_USER_NAME = ".m.rule.contains_user_name" + + // Default Underride Rules + const val RULE_ID_CALL = ".m.rule.call" + const val RULE_ID_one_to_one_encrypted_room = ".m.rule.encrypted_room_one_to_one" + const val RULE_ID_ONE_TO_ONE_ROOM = ".m.rule.room_one_to_one" + const val RULE_ID_ALL_OTHER_MESSAGES_ROOMS = ".m.rule.message" + const val RULE_ID_ENCRYPTED = ".m.rule.encrypted" + + // Not documented + const val RULE_ID_FALLBACK = ".m.rule.fallback" +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RulesetKey.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RulesetKey.kt new file mode 100644 index 0000000000..834bdf2add --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RulesetKey.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.pushrules + + +enum class RulesetKey(val value: String) { + CONTENT("content"), + OVERRIDE("override"), + ROOM("room"), + SENDER("sender"), + UNDERRIDE("underride"), + UNKNOWN("") +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/SenderNotificationPermissionCondition.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/SenderNotificationPermissionCondition.kt new file mode 100644 index 0000000000..5f553f7828 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/SenderNotificationPermissionCondition.kt @@ -0,0 +1,36 @@ +package im.vector.matrix.android.api.pushrules + +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.api.session.room.model.PowerLevels + + +class SenderNotificationPermissionCondition(val key: String) : Condition(Kind.sender_notification_permission) { + + override fun isSatisfied(conditionResolver: ConditionResolver): Boolean { + return conditionResolver.resolveSenderNotificationPermissionCondition(this) + } + + override fun technicalDescription(): String { + return "User power level <$key>" + } + + + fun isSatisfied(event: Event, powerLevels: PowerLevels): Boolean { + return event.senderId != null && powerLevels.getUserPowerLevel(event.senderId) >= powerLevels.notificationLevel(key) + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/GetPushRulesResponse.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/GetPushRulesResponse.kt new file mode 100644 index 0000000000..8d567f9464 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/GetPushRulesResponse.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.pushrules.rest + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +/** + * All push rulesets for a user. + */ +@JsonClass(generateAdapter = true) +data class GetPushRulesResponse( + /** + * Global rules, account level applying to all devices + */ + @Json(name = "global") + val global: Ruleset, + + /** + * Device specific rules, apply only to current device + */ + @Json(name = "device") + val device: Ruleset? = null +) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/PushCondition.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/PushCondition.kt new file mode 100644 index 0000000000..8e63e8640d --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/PushCondition.kt @@ -0,0 +1,78 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.pushrules.rest + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass +import im.vector.matrix.android.api.pushrules.* +import timber.log.Timber + +@JsonClass(generateAdapter = true) +data class PushCondition( + /** + * Required. The kind of condition to apply. + */ + val kind: String, + + /** + * Required for event_match conditions. The dot- separated field of the event to match. + */ + + val key: String? = null, + /** + *Required for event_match conditions. + */ + + val pattern: String? = null, + /** + * Required for room_member_count conditions. + * A decimal integer optionally prefixed by one of, ==, <, >, >= or <=. + * A prefix of < matches rooms where the member count is strictly less than the given number and so forth. If no prefix is present, this parameter defaults to ==. + */ + @Json(name = "is") val iz: String? = null +) { + + fun asExecutableCondition(): Condition? { + return when (Condition.Kind.fromString(this.kind)) { + Condition.Kind.event_match -> { + if (this.key != null && this.pattern != null) { + EventMatchCondition(key, pattern) + } else { + Timber.e("Malformed Event match condition") + null + } + } + Condition.Kind.contains_display_name -> { + ContainsDisplayNameCondition() + } + Condition.Kind.room_member_count -> { + if (this.iz.isNullOrBlank()) { + Timber.e("Malformed ROOM_MEMBER_COUNT condition") + null + } else { + RoomMemberCountCondition(this.iz) + } + } + Condition.Kind.sender_notification_permission -> { + this.key?.let { SenderNotificationPermissionCondition(it) } + } + Condition.Kind.UNRECOGNIZE -> { + Timber.e("Unknwon kind $kind") + null + } + } + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/PushRule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/PushRule.kt new file mode 100644 index 0000000000..1e36a0d867 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/PushRule.kt @@ -0,0 +1,50 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.matrix.android.api.pushrules.rest + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + + +@JsonClass(generateAdapter = true) +data class PushRule( + /** + * Required. The actions to perform when this rule is matched. + */ + val actions: List, + /** + * Required. Whether this is a default rule, or has been set explicitly. + */ + val default: Boolean? = false, + /** + * Required. Whether the push rule is enabled or not. + */ + val enabled: Boolean, + /** + * Required. The ID of this rule. + */ + @Json(name = "rule_id") val ruleId: String, + /** + * The conditions that must hold true for an event in order for a rule to be applied to an event + */ + val conditions: List? = null, + /** + * The glob-style pattern to match against. Only applicable to content rules. + */ + val pattern: String? = null +) + diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/Ruleset.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/Ruleset.kt new file mode 100644 index 0000000000..5f4ca15ac0 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/Ruleset.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.pushrules.rest + +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class Ruleset( + val content: List? = null, + val override: List? = null, + val room: List? = null, + val sender: List? = null, + val underride: List? = null +) \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/Session.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/Session.kt index 6f8d745e85..ac41267870 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/Session.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/Session.kt @@ -19,11 +19,13 @@ package im.vector.matrix.android.api.session import androidx.annotation.MainThread import androidx.lifecycle.LiveData import im.vector.matrix.android.api.auth.data.SessionParams +import im.vector.matrix.android.api.pushrules.PushRuleService import im.vector.matrix.android.api.session.cache.CacheService import im.vector.matrix.android.api.session.content.ContentUploadStateTracker import im.vector.matrix.android.api.session.content.ContentUrlResolver import im.vector.matrix.android.api.session.crypto.CryptoService import im.vector.matrix.android.api.session.group.GroupService +import im.vector.matrix.android.api.session.pushers.PushersService import im.vector.matrix.android.api.session.room.RoomDirectoryService import im.vector.matrix.android.api.session.room.RoomService import im.vector.matrix.android.api.session.signout.SignOutService @@ -43,7 +45,9 @@ interface Session : CryptoService, CacheService, SignOutService, - FilterService { + FilterService, + PushRuleService, + PushersService { /** * The params associated to the session @@ -56,6 +60,20 @@ interface Session : @MainThread fun open() + /** + * Requires a one time background sync + */ + fun requireBackgroundSync() + + /** + * Launches infinite periodic background syncs + * THis does not work in doze mode :/ + * If battery optimization is on it can work in app standby but that's all :/ + */ + fun startAutomaticBackgroundSync(repeatDelay: Long = 30_000L) + + fun stopAnyBackgroundSync() + /** * This method start the sync thread. */ diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/pushers/Pusher.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/pushers/Pusher.kt new file mode 100644 index 0000000000..7b1fa3bb52 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/pushers/Pusher.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.session.pushers + +data class Pusher( + + val userId: String, + + val pushKey: String, + val kind: String, + val appId: String, + val appDisplayName: String?, + val deviceDisplayName: String?, + val profileTag: String? = null, + val lang: String?, + val data: PusherData, + + val state: PusherState +) + +enum class PusherState { + UNREGISTERED, + REGISTERING, + UNREGISTERING, + REGISTERED, + FAILED_TO_REGISTER +} + +data class PusherData( + val url: String? = null, + val format: String? = null +) \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/pushers/PushersService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/pushers/PushersService.kt new file mode 100644 index 0000000000..21b2464dc2 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/pushers/PushersService.kt @@ -0,0 +1,64 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.session.pushers + +import androidx.lifecycle.LiveData +import im.vector.matrix.android.api.MatrixCallback +import java.util.* + + +interface PushersService { + + /** + * Refresh pushers from server state + */ + fun refreshPushers() + + /** + * Add a new HTTP pusher. + * + * @param pushkey the pushkey + * @param appId the application id + * @param profileTag the profile tag + * @param lang the language + * @param appDisplayName a human-readable application name + * @param deviceDisplayName a human-readable device name + * @param url the URL that should be used to send notifications + * @param append append the pusher + * @param withEventIdOnly true to limit the push content + * + * @return A work request uuid. Can be used to listen to the status + * (LiveData status = workManager.getWorkInfoByIdLiveData()) + */ + fun addHttpPusher(pushkey: String, + appId: String, + profileTag: String, + lang: String, + appDisplayName: String, + deviceDisplayName: String, + url: String, + append: Boolean, + withEventIdOnly: Boolean): UUID + + + fun removeHttpPusher(pushkey: String, appId: String, callback: MatrixCallback) + + companion object { + const val EVENT_ID_ONLY = "event_id_only" + } + + fun livePushers(): LiveData> +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/Room.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/Room.kt index fe6a5443e9..3e893a091d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/Room.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/Room.kt @@ -47,6 +47,8 @@ interface Room : * A live [RoomSummary] associated with the room * You can observe this summary to get dynamic data from this room. */ - val roomSummary: LiveData + val liveRoomSummary: LiveData + + val roomSummary: RoomSummary? } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/members/MembershipService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/members/MembershipService.kt index 59054dc184..7c21695ec6 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/members/MembershipService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/members/MembershipService.kt @@ -49,6 +49,8 @@ interface MembershipService { */ fun getRoomMemberIdsLive(): LiveData> + fun getNumberOfJoinedMembers() : Int + /** * Invite a user in the room */ diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/call/CallInviteContent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/call/CallInviteContent.kt index b47e7ba291..2ec393a0cc 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/call/CallInviteContent.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/call/CallInviteContent.kt @@ -39,4 +39,6 @@ data class CallInviteContent( } } + + fun isVideo(): Boolean = offer.sdp.contains(Offer.SDP_VIDEO) } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/read/ReadService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/read/ReadService.kt index ed67c1db4a..ab406b8e54 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/read/ReadService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/read/ReadService.kt @@ -38,4 +38,5 @@ interface ReadService { */ fun setReadMarker(fullyReadEventId: String, callback: MatrixCallback) + fun isEventRead(eventId: String): Boolean } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/PushConditionMapper.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/PushConditionMapper.kt new file mode 100644 index 0000000000..eef122a575 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/PushConditionMapper.kt @@ -0,0 +1,26 @@ +package im.vector.matrix.android.internal.database.mapper + +import im.vector.matrix.android.api.pushrules.rest.PushCondition +import im.vector.matrix.android.internal.database.model.PushConditionEntity + + +internal object PushConditionMapper { + + fun map(entity: PushConditionEntity): PushCondition { + return PushCondition( + kind = entity.kind, + iz = entity.iz, + key = entity.key, + pattern = entity.pattern + ) + } + + fun map(domain: PushCondition): PushConditionEntity { + return PushConditionEntity( + kind = domain.kind, + iz = domain.iz, + key = domain.key, + pattern = domain.pattern + ) + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/PushRulesMapper.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/PushRulesMapper.kt new file mode 100644 index 0000000000..1ed2151e68 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/PushRulesMapper.kt @@ -0,0 +1,105 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.database.mapper + +import com.squareup.moshi.Types +import im.vector.matrix.android.api.pushrules.Condition +import im.vector.matrix.android.api.pushrules.rest.PushCondition +import im.vector.matrix.android.api.pushrules.rest.PushRule +import im.vector.matrix.android.internal.database.model.PushRuleEntity +import im.vector.matrix.android.internal.di.MoshiProvider +import io.realm.RealmList +import timber.log.Timber + + +internal object PushRulesMapper { + + private val moshiActionsAdapter = MoshiProvider.providesMoshi().adapter>(Types.newParameterizedType(List::class.java, Any::class.java)) + +// private val listOfAnyAdapter: JsonAdapter> = +// moshi.adapter>(Types.newParameterizedType(List::class.java, Any::class.java), kotlin.collections.emptySet(), "actions") + + fun mapContentRule(pushrule: PushRuleEntity): PushRule { + return PushRule( + actions = fromActionStr(pushrule.actionsStr), + default = pushrule.default, + enabled = pushrule.enabled, + ruleId = pushrule.ruleId, + conditions = listOf( + PushCondition(Condition.Kind.event_match.name, "content.body", pushrule.pattern) + ) + ) + } + + private fun fromActionStr(actionsStr: String?): List { + try { + return actionsStr?.let { moshiActionsAdapter.fromJson(it) } ?: emptyList() + } catch (e: Throwable) { + Timber.e(e, "## failed to map push rule actions <$actionsStr>") + return emptyList() + } + } + + + fun mapRoomRule(pushrule: PushRuleEntity): PushRule { + return PushRule( + actions = fromActionStr(pushrule.actionsStr), + default = pushrule.default, + enabled = pushrule.enabled, + ruleId = pushrule.ruleId, + conditions = listOf( + PushCondition(Condition.Kind.event_match.name, "room_id", pushrule.ruleId) + ) + ) + } + + fun mapSenderRule(pushrule: PushRuleEntity): PushRule { + return PushRule( + actions = fromActionStr(pushrule.actionsStr), + default = pushrule.default, + enabled = pushrule.enabled, + ruleId = pushrule.ruleId, + conditions = listOf( + PushCondition(Condition.Kind.event_match.name, "user_id", pushrule.ruleId) + ) + ) + } + + + fun map(pushrule: PushRuleEntity): PushRule { + return PushRule( + actions = fromActionStr(pushrule.actionsStr), + default = pushrule.default, + enabled = pushrule.enabled, + ruleId = pushrule.ruleId, + conditions = pushrule.conditions?.map { PushConditionMapper.map(it) } + ) + } + + fun map(pushRule: PushRule): PushRuleEntity { + return PushRuleEntity( + actionsStr = moshiActionsAdapter.toJson(pushRule.actions), + default = pushRule.default ?: false, + enabled = pushRule.enabled, + ruleId = pushRule.ruleId, + pattern = pushRule.pattern, + conditions = pushRule.conditions?.let { + RealmList(*pushRule.conditions.map { PushConditionMapper.map(it) }.toTypedArray()) + } ?: RealmList() + ) + } + +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/PushersMapper.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/PushersMapper.kt new file mode 100644 index 0000000000..8cac50a468 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/PushersMapper.kt @@ -0,0 +1,63 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.database.mapper + +import im.vector.matrix.android.api.session.pushers.Pusher +import im.vector.matrix.android.api.session.pushers.PusherData +import im.vector.matrix.android.internal.database.model.PusherDataEntity +import im.vector.matrix.android.internal.database.model.PusherEntity +import im.vector.matrix.android.internal.session.pushers.JsonPusher + +internal object PushersMapper { + + fun map(pushEntity: PusherEntity): Pusher { + + return Pusher( + userId = pushEntity.userId, + pushKey = pushEntity.pushKey, + kind = pushEntity.kind ?: "", + appId = pushEntity.appId, + appDisplayName = pushEntity.appDisplayName, + deviceDisplayName = pushEntity.deviceDisplayName, + profileTag = pushEntity.profileTag, + lang = pushEntity.lang, + data = PusherData(pushEntity.data?.url, pushEntity.data?.format), + state = pushEntity.state + ) + } + + fun map(pusher: JsonPusher, userId: String): PusherEntity { + return PusherEntity( + userId = userId, + pushKey = pusher.pushKey, + kind = pusher.kind, + appId = pusher.appId, + appDisplayName = pusher.appDisplayName, + deviceDisplayName = pusher.deviceDisplayName, + profileTag = pusher.profileTag, + lang = pusher.lang, + data = PusherDataEntity(pusher.data?.url, pusher.data?.format) + ) + } +} + +internal fun PusherEntity.asDomain(): Pusher { + return PushersMapper.map(this) +} + +internal fun JsonPusher.toEntity(userId: String): PusherEntity { + return PushersMapper.map(this, userId) +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushConditionEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushConditionEntity.kt new file mode 100644 index 0000000000..e6c3d406c8 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushConditionEntity.kt @@ -0,0 +1,29 @@ +/* + * copyright 2019 new vector ltd + * + * licensed under the apache license, version 2.0 (the "license"); + * you may not use this file except in compliance with the license. + * you may obtain a copy of the license at + * + * http://www.apache.org/licenses/license-2.0 + * + * unless required by applicable law or agreed to in writing, software + * distributed under the license is distributed on an "as is" basis, + * without warranties or conditions of any kind, either express or implied. + * see the license for the specific language governing permissions and + * limitations under the license. + */ +package im.vector.matrix.android.internal.database.model + +import io.realm.RealmObject + +internal open class PushConditionEntity( + var kind: String = "", + var key: String? = null, + var pattern: String? = null, + var iz: String? = null +) : RealmObject() { + + companion object + +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushRuleEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushRuleEntity.kt new file mode 100644 index 0000000000..dd6cc79022 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushRuleEntity.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.database.model + +import io.realm.RealmList +import io.realm.RealmObject + +internal open class PushRuleEntity( + //Required. The actions to perform when this rule is matched. + var actionsStr: String? = null, + //Required. Whether this is a default rule, or has been set explicitly. + var default: Boolean = false, + //Required. Whether the push rule is enabled or not. + var enabled: Boolean = true, + //Required. The ID of this rule. + var ruleId: String = "", + //The conditions that must hold true for an event in order for a rule to be applied to an event + var conditions: RealmList? = RealmList(), + //The glob-style pattern to match against. Only applicable to content rules. + var pattern: String? = null +) : RealmObject() { + + companion object + +} + diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushRulesEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushRulesEntity.kt new file mode 100644 index 0000000000..f504192550 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushRulesEntity.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.database.model + +import io.realm.RealmList +import io.realm.RealmObject +import io.realm.annotations.Index + + +internal open class PushRulesEntity( + @Index var userId: String = "", + var scope: String = "", + // "content", etc. + var rulesetKey: String = "", + var pushRules: RealmList = RealmList() +) : RealmObject() { + companion object +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PusherDataEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PusherDataEntity.kt new file mode 100644 index 0000000000..87694fb723 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PusherDataEntity.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.database.model + +import io.realm.RealmObject + +internal open class PusherDataEntity( + var url: String? = null, + var format: String? = null +) : RealmObject() { + companion object +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PusherEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PusherEntity.kt new file mode 100644 index 0000000000..6e567c0a2d --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PusherEntity.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.database.model + +import im.vector.matrix.android.api.session.pushers.PusherState +import io.realm.RealmObject +import io.realm.annotations.Index + +//TODO +// at java.lang.Thread.run(Thread.java:764) +// Caused by: java.lang.IllegalArgumentException: 'value' is not a valid managed object. +// at io.realm.ProxyState.checkValidObject(ProxyState.java:213) +// at io.realm.im_vector_matrix_android_internal_database_model_PusherEntityRealmProxy.realmSet$data(im_vector_matrix_android_internal_database_model_PusherEntityRealmProxy.java:413) +// at im.vector.matrix.android.internal.database.model.PusherEntity.setData(PusherEntity.kt:16) +// at im.vector.matrix.android.internal.session.pushers.AddHttpPusherWorker$doWork$$inlined$fold$lambda$2.execute(AddHttpPusherWorker.kt:70) +// at io.realm.Realm.executeTransaction(Realm.java:1493) +internal open class PusherEntity( + @Index var userId: String = "", + var pushKey: String = "", + var kind: String? = null, + var appId: String = "", + var appDisplayName: String? = null, + var deviceDisplayName: String? = null, + var profileTag: String? = null, + var lang: String? = null, + var data: PusherDataEntity? = null +) : RealmObject() { + private var stateStr: String = PusherState.UNREGISTERED.name + + var state: PusherState + get() { + try { + return PusherState.valueOf(stateStr) + } catch (e: Exception) { + //can this happen? + return PusherState.UNREGISTERED + } + + } + set(value) { + stateStr = value.name + } + + companion object +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/SessionRealmModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/SessionRealmModule.kt index 20ba8d44d0..b926da9907 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/SessionRealmModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/SessionRealmModule.kt @@ -36,6 +36,11 @@ import io.realm.annotations.RealmModule UserEntity::class, EventAnnotationsSummaryEntity::class, ReactionAggregatedSummaryEntity::class, - EditAggregatedSummaryEntity::class + EditAggregatedSummaryEntity::class, + PushRulesEntity::class, + PushRuleEntity::class, + PushConditionEntity::class, + PusherEntity::class, + PusherDataEntity::class ]) internal class SessionRealmModule diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/EventEntityQueries.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/EventEntityQueries.kt index 8fca0abd38..6f4db6f4b4 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/EventEntityQueries.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/EventEntityQueries.kt @@ -53,6 +53,14 @@ internal fun EventEntity.Companion.where(realm: Realm, } +internal fun EventEntity.Companion.types(realm: Realm, + typeList: List = emptyList()): RealmQuery { + val query = realm.where() + query.`in`(EventEntityFields.TYPE, typeList.toTypedArray()) + return query +} + + internal fun EventEntity.Companion.latestEvent(realm: Realm, roomId: String, includedTypes: List = emptyList(), diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/PushersQueries.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/PushersQueries.kt new file mode 100644 index 0000000000..b7f54f17ea --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/PushersQueries.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.database.query + +import im.vector.matrix.android.internal.database.model.PushRulesEntity +import im.vector.matrix.android.internal.database.model.PushRulesEntityFields +import im.vector.matrix.android.internal.database.model.PusherEntity +import im.vector.matrix.android.internal.database.model.PusherEntityFields +import io.realm.Realm +import io.realm.RealmQuery +import io.realm.kotlin.where + +internal fun PusherEntity.Companion.where(realm: Realm, + userId: String, + pushKey: String? = null): RealmQuery { + return realm.where() + .equalTo(PusherEntityFields.USER_ID, userId) + .apply { + if (pushKey != null) { + equalTo(PusherEntityFields.PUSH_KEY, pushKey) + } + } +} + +internal fun PushRulesEntity.Companion.where(realm: Realm, + userId: String, + scope: String, + ruleSetKey: String): RealmQuery { + return realm.where() + .equalTo(PushRulesEntityFields.USER_ID, userId) + .equalTo(PushRulesEntityFields.SCOPE, scope) + .equalTo(PushRulesEntityFields.RULESET_KEY, ruleSetKey) +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MoshiProvider.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MoshiProvider.kt index acd3353b93..3849a2f749 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MoshiProvider.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MoshiProvider.kt @@ -25,6 +25,7 @@ import im.vector.matrix.android.internal.session.sync.model.UserAccountDataDirec import im.vector.matrix.android.internal.session.sync.model.UserAccountDataFallback import im.vector.matrix.android.internal.util.JsonCanonicalizer + object MoshiProvider { private val moshi: Moshi = Moshi.Builder() @@ -42,6 +43,7 @@ object MoshiProvider { .registerSubtype(MessageLocationContent::class.java, MessageType.MSGTYPE_LOCATION) .registerSubtype(MessageFileContent::class.java, MessageType.MSGTYPE_FILE) ) + .add(SerializeNulls.JSON_ADAPTER_FACTORY) .build() fun providesMoshi(): Moshi { @@ -63,3 +65,4 @@ object MoshiProvider { } } + diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/SerializeNulls.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/SerializeNulls.kt new file mode 100644 index 0000000000..b758a4de92 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/SerializeNulls.kt @@ -0,0 +1,23 @@ +package im.vector.matrix.android.internal.di + +import androidx.annotation.Nullable +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.JsonQualifier +import com.squareup.moshi.Moshi +import com.squareup.moshi.Types +import java.lang.reflect.Type + +@Retention(AnnotationRetention.RUNTIME) +@JsonQualifier +annotation class SerializeNulls { + companion object { + val JSON_ADAPTER_FACTORY: JsonAdapter.Factory = object : JsonAdapter.Factory { + @Nullable + override fun create(type: Type, annotations: Set, moshi: Moshi): JsonAdapter<*>? { + val nextAnnotations = Types.nextAnnotations(annotations, SerializeNulls::class.java) + ?: return null + return moshi.nextAdapter(this, type, nextAnnotations).serializeNulls() + } + } + } +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt index ecbdd15603..7666f49dc9 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt @@ -24,6 +24,8 @@ import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.api.listeners.ProgressListener +import im.vector.matrix.android.api.pushrules.PushRuleService +import im.vector.matrix.android.api.pushrules.rest.PushRule import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.cache.CacheService import im.vector.matrix.android.api.session.content.ContentUploadStateTracker @@ -36,6 +38,8 @@ import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.group.Group import im.vector.matrix.android.api.session.group.GroupService import im.vector.matrix.android.api.session.group.model.GroupSummary +import im.vector.matrix.android.api.session.pushers.Pusher +import im.vector.matrix.android.api.session.pushers.PushersService import im.vector.matrix.android.api.session.room.Room import im.vector.matrix.android.api.session.room.RoomDirectoryService import im.vector.matrix.android.api.session.room.RoomService @@ -65,17 +69,21 @@ import im.vector.matrix.android.internal.di.MatrixKoinComponent import im.vector.matrix.android.internal.di.MatrixKoinHolder import im.vector.matrix.android.internal.session.content.ContentModule import im.vector.matrix.android.internal.session.group.GroupModule +import im.vector.matrix.android.internal.session.notification.BingRuleWatcher import im.vector.matrix.android.internal.session.room.RoomModule import im.vector.matrix.android.internal.session.signout.SignOutModule import im.vector.matrix.android.internal.session.sync.SyncModule import im.vector.matrix.android.internal.session.sync.job.SyncThread +import im.vector.matrix.android.internal.session.sync.job.SyncWorker import im.vector.matrix.android.internal.session.user.UserModule import org.koin.core.scope.Scope import org.koin.standalone.inject import timber.log.Timber +import java.util.* internal class DefaultSession(override val sessionParams: SessionParams) : Session, MatrixKoinComponent { + companion object { const val SCOPE: String = "session" } @@ -96,8 +104,12 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi private val syncThread by inject() private val contentUrlResolver by inject() private val contentUploadProgressTracker by inject() + private val pushRuleService by inject() + private val pushersService by inject() private var isOpen = false + private val bingRuleWatcher by inject() + @MainThread override fun open() { assertMainThread() @@ -124,12 +136,30 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi monarchy.openManually() } liveEntityUpdaters.forEach { it.start() } + bingRuleWatcher.start() + } + + override fun requireBackgroundSync() { + SyncWorker.requireBackgroundSync() + } + + override fun startAutomaticBackgroundSync(repeatDelay: Long) { + SyncWorker.automaticallyBackgroundSync(0, repeatDelay) + } + + override fun stopAnyBackgroundSync() { + SyncWorker.stopAnyBackgroundSync() } @MainThread override fun startSync() { assert(isOpen) - syncThread.start() + if (!syncThread.isAlive) { + syncThread.start() + } else { + syncThread.restart() + Timber.w("Attempt to start an already started thread") + } } @MainThread @@ -144,6 +174,7 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi assert(isOpen) liveEntityUpdaters.forEach { it.dispose() } cryptoService.close() + bingRuleWatcher.dispose() if (monarchy.isMonarchyThreadOpen) { monarchy.closeManually() } @@ -160,8 +191,8 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi Timber.w("SIGN_OUT: start") assert(isOpen) - Timber.w("SIGN_OUT: kill sync thread") - syncThread.kill() + //Timber.w("SIGN_OUT: kill sync thread") + //syncThread.kill() Timber.w("SIGN_OUT: call webservice") return signOutService.signOut(object : MatrixCallback { @@ -261,11 +292,11 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi override fun clearCache(callback: MatrixCallback) { assert(isOpen) - syncThread.pause() + // syncThread.pause() cacheService.clearCache(object : MatrixCallbackDelegate(callback) { override fun onSuccess(data: Unit) { // Restart the sync - syncThread.restart() + // syncThread.restart() super.onSuccess(data) } @@ -426,6 +457,16 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi cryptoService.clearCryptoCache(callback) } + // PUSH RULE SERVICE + + override fun addPushRuleListener(listener: PushRuleService.PushRuleListener) { + pushRuleService.addPushRuleListener(listener) + } + + override fun removePushRuleListener(listener: PushRuleService.PushRuleListener) { + pushRuleService.removePushRuleListener(listener) + } + // Private methods ***************************************************************************** private fun assertMainThread() { @@ -434,4 +475,45 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi } } + override fun refreshPushers() { + pushersService.refreshPushers() + } + + override fun addHttpPusher( + pushkey: String, + appId: String, + profileTag: String, + lang: String, + appDisplayName: String, + deviceDisplayName: String, + url: String, + append: Boolean, + withEventIdOnly: Boolean): UUID { + return pushersService + .addHttpPusher( + pushkey, appId, profileTag, lang, appDisplayName, deviceDisplayName, url, append, withEventIdOnly + ) + } + + override fun removeHttpPusher(pushkey: String, appId: String, callback: MatrixCallback) { + pushersService.removeHttpPusher(pushkey, appId, callback) + } + + override fun livePushers(): LiveData> { + return pushersService.livePushers() + } + + override fun getPushRules(scope: String): List { + return pushRuleService.getPushRules(scope) + } + + override fun updatePushRuleEnableStatus(kind: String, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback) { + pushRuleService.updatePushRuleEnableStatus(kind, pushRule, enabled, callback) + } + + override fun fetchPushRules(scope: String) { + pushRuleService.fetchPushRules(scope) + } + + } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt index 4a5d3b4e53..1ea4803a56 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt @@ -19,8 +19,10 @@ package im.vector.matrix.android.internal.session import android.content.Context import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.auth.data.SessionParams +import im.vector.matrix.android.api.pushrules.PushRuleService import im.vector.matrix.android.api.session.cache.CacheService import im.vector.matrix.android.api.session.group.GroupService +import im.vector.matrix.android.api.session.pushers.PushersService import im.vector.matrix.android.api.session.room.RoomDirectoryService import im.vector.matrix.android.api.session.room.RoomService import im.vector.matrix.android.api.session.signout.SignOutService @@ -34,6 +36,11 @@ import im.vector.matrix.android.internal.session.cache.RealmClearCacheTask import im.vector.matrix.android.internal.session.filter.* import im.vector.matrix.android.internal.session.group.DefaultGroupService import im.vector.matrix.android.internal.session.group.GroupSummaryUpdater +import im.vector.matrix.android.internal.session.notification.BingRuleWatcher +import im.vector.matrix.android.internal.session.notification.DefaultProcessEventForPushTask +import im.vector.matrix.android.internal.session.notification.DefaultPushRuleService +import im.vector.matrix.android.internal.session.notification.ProcessEventForPushTask +import im.vector.matrix.android.internal.session.pushers.* import im.vector.matrix.android.internal.session.room.* import im.vector.matrix.android.internal.session.room.directory.DefaultGetPublicRoomTask import im.vector.matrix.android.internal.session.room.directory.DefaultGetThirdPartyProtocolsTask @@ -163,6 +170,35 @@ internal class SessionModule(private val sessionParams: SessionParams) { retrofit.create(FilterApi::class.java) } + scope(DefaultSession.SCOPE) { + val retrofit: Retrofit = get() + retrofit.create(PushRulesApi::class.java) + } + + scope(DefaultSession.SCOPE) { + get() as PushRuleService + } + + scope(DefaultSession.SCOPE) { + DefaultPushRuleService(get(), get(), get(), get(), get()) + } + + scope(DefaultSession.SCOPE) { + DefaultGetPushRulesTask(get()) as GetPushRulesTask + } + + scope(DefaultSession.SCOPE) { + DefaultUpdatePushRuleEnableStatusTask(get()) as UpdatePushRuleEnableStatusTask + } + + scope(DefaultSession.SCOPE) { + DefaultProcessEventForPushTask(get()) as ProcessEventForPushTask + } + + scope(DefaultSession.SCOPE) { + BingRuleWatcher(get(), get(), get(), get(), get()) + } + scope(DefaultSession.SCOPE) { val groupSummaryUpdater = GroupSummaryUpdater(get()) val userEntityUpdater = UserEntityUpdater(get(), get(), get()) @@ -172,6 +208,20 @@ internal class SessionModule(private val sessionParams: SessionParams) { listOf(groupSummaryUpdater, userEntityUpdater, aggregationUpdater, eventsPruner) } + scope(DefaultSession.SCOPE) { + get().create(PushersAPI::class.java) + } + + scope(DefaultSession.SCOPE) { + DefaultGetPusherTask(get()) as GetPushersTask + } + scope(DefaultSession.SCOPE) { + DefaultRemovePusherTask(get(), get()) as RemovePusherTask + } + + scope(DefaultSession.SCOPE) { + DefaultPusherService(get(), get(), get(), get(), get()) as PushersService + } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/BingRuleWatcher.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/BingRuleWatcher.kt new file mode 100644 index 0000000000..2e6a1a9a77 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/BingRuleWatcher.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.notification + +import com.zhuinden.monarchy.Monarchy +import im.vector.matrix.android.api.auth.data.SessionParams +import im.vector.matrix.android.api.session.events.model.EventType +import im.vector.matrix.android.internal.database.RealmLiveEntityObserver +import im.vector.matrix.android.internal.database.mapper.asDomain +import im.vector.matrix.android.internal.database.model.EventEntity +import im.vector.matrix.android.internal.database.query.types +import im.vector.matrix.android.internal.task.TaskExecutor +import im.vector.matrix.android.internal.task.configureWith + + +internal class BingRuleWatcher(monarchy: Monarchy, + private val task: ProcessEventForPushTask, + private val defaultPushRuleService: DefaultPushRuleService, + private val sessionParams: SessionParams, + private val taskExecutor: TaskExecutor) : + RealmLiveEntityObserver(monarchy) { + + override val query = Monarchy.Query { + + EventEntity.types(it, listOf( + EventType.REDACTION, EventType.MESSAGE, EventType.REDACTION, EventType.ENCRYPTED) + ) + + } + + override fun processChanges(inserted: List, updated: List, deleted: List) { + val rules = defaultPushRuleService.getPushRules("global") + inserted.map { it.asDomain() } + .filter { it.senderId != sessionParams.credentials.userId } + .let { events -> + task.configureWith(ProcessEventForPushTask.Params(events, rules)) + .executeBy(taskExecutor) + } + } + + +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/DefaultPushRuleService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/DefaultPushRuleService.kt new file mode 100644 index 0000000000..c85f0b442f --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/DefaultPushRuleService.kt @@ -0,0 +1,185 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.notification + +import com.zhuinden.monarchy.Monarchy +import im.vector.matrix.android.api.MatrixCallback +import im.vector.matrix.android.api.auth.data.SessionParams +import im.vector.matrix.android.api.pushrules.Action +import im.vector.matrix.android.api.pushrules.PushRuleService +import im.vector.matrix.android.api.pushrules.rest.GetPushRulesResponse +import im.vector.matrix.android.api.pushrules.rest.PushRule +import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.internal.database.mapper.PushRulesMapper +import im.vector.matrix.android.internal.database.model.PushRulesEntity +import im.vector.matrix.android.internal.database.model.PusherEntityFields +import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.pushers.GetPushRulesTask +import im.vector.matrix.android.internal.session.pushers.UpdatePushRuleEnableStatusTask +import im.vector.matrix.android.internal.task.TaskExecutor +import im.vector.matrix.android.internal.task.configureWith +import timber.log.Timber + + +internal class DefaultPushRuleService( + private val sessionParams: SessionParams, + private val pushRulesTask: GetPushRulesTask, + private val updatePushRuleEnableStatusTask: UpdatePushRuleEnableStatusTask, + private val taskExecutor: TaskExecutor, + private val monarchy: Monarchy +) : PushRuleService { + + + private var listeners = ArrayList() + + + override fun fetchPushRules(scope: String) { + pushRulesTask + .configureWith(Unit) + .dispatchTo(object : MatrixCallback { + override fun onSuccess(data: GetPushRulesResponse) { + monarchy.runTransactionSync { realm -> + //clear existings? + //TODO + realm.where(PushRulesEntity::class.java) + .equalTo(PusherEntityFields.USER_ID, sessionParams.credentials.userId) + .findAll().deleteAllFromRealm() + + val content = PushRulesEntity(sessionParams.credentials.userId, scope, "content") + data.global.content?.forEach { rule -> + PushRulesMapper.map(rule).also { + content.pushRules.add(it) + } + } + realm.insertOrUpdate(content) + + val override = PushRulesEntity(sessionParams.credentials.userId, scope, "override") + data.global.override?.forEach { rule -> + PushRulesMapper.map(rule).also { + override.pushRules.add(it) + } + } + realm.insertOrUpdate(override) + + val rooms = PushRulesEntity(sessionParams.credentials.userId, scope, "room") + data.global.room?.forEach { rule -> + PushRulesMapper.map(rule).also { + rooms.pushRules.add(it) + } + } + realm.insertOrUpdate(rooms) + + val senders = PushRulesEntity(sessionParams.credentials.userId, scope, "sender") + data.global.sender?.forEach { rule -> + PushRulesMapper.map(rule).also { + senders.pushRules.add(it) + } + } + realm.insertOrUpdate(senders) + + val underrides = PushRulesEntity(sessionParams.credentials.userId, scope, "underride") + data.global.underride?.forEach { rule -> + PushRulesMapper.map(rule).also { + underrides.pushRules.add(it) + } + } + realm.insertOrUpdate(underrides) + } + } + }) + .executeBy(taskExecutor) + } + + override fun getPushRules(scope: String): List { + + var contentRules: List = emptyList() + var overrideRules: List = emptyList() + var roomRules: List = emptyList() + var senderRules: List = emptyList() + var underrideRules: List = emptyList() + + // TODO Create const for ruleSetKey + monarchy.doWithRealm { realm -> + PushRulesEntity.where(realm, sessionParams.credentials.userId, scope, "content").findFirst()?.let { re -> + contentRules = re.pushRules.map { PushRulesMapper.mapContentRule(it) } + } + PushRulesEntity.where(realm, sessionParams.credentials.userId, scope, "override").findFirst()?.let { re -> + overrideRules = re.pushRules.map { PushRulesMapper.map(it) } + } + PushRulesEntity.where(realm, sessionParams.credentials.userId, scope, "room").findFirst()?.let { re -> + roomRules = re.pushRules.map { PushRulesMapper.mapRoomRule(it) } + } + PushRulesEntity.where(realm, sessionParams.credentials.userId, scope, "sender").findFirst()?.let { re -> + senderRules = re.pushRules.map { PushRulesMapper.mapSenderRule(it) } + } + PushRulesEntity.where(realm, sessionParams.credentials.userId, scope, "underride").findFirst()?.let { re -> + underrideRules = re.pushRules.map { PushRulesMapper.map(it) } + } + } + + return contentRules + overrideRules + roomRules + senderRules + underrideRules + } + + override fun updatePushRuleEnableStatus(kind: String, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback) { + updatePushRuleEnableStatusTask + .configureWith(UpdatePushRuleEnableStatusTask.Params(kind, pushRule, enabled)) + // TODO Fetch the rules + .dispatchTo(callback) + .executeBy(taskExecutor) + } + + override fun removePushRuleListener(listener: PushRuleService.PushRuleListener) { + listeners.remove(listener) + } + + + override fun addPushRuleListener(listener: PushRuleService.PushRuleListener) { + if (!listeners.contains(listener)) + listeners.add(listener) + } + +// fun processEvents(events: List) { +// var hasDoneSomething = false +// events.forEach { event -> +// fulfilledBingRule(event)?.let { +// hasDoneSomething = true +// dispatchBing(event, it) +// } +// } +// if (hasDoneSomething) +// dispatchFinish() +// } + + fun dispatchBing(event: Event, rule: PushRule) { + try { + listeners.forEach { + it.onMatchRule(event, Action.mapFrom(rule) ?: emptyList()) + } + } catch (e: Throwable) { + Timber.e(e, "Error while dispatching bing") + } + } + + fun dispatchFinish() { + try { + listeners.forEach { + it.batchFinish() + } + } catch (e: Throwable) { + + } + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/ProcessEventForPushTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/ProcessEventForPushTask.kt new file mode 100644 index 0000000000..36b7f7206f --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/ProcessEventForPushTask.kt @@ -0,0 +1,51 @@ +package im.vector.matrix.android.internal.session.notification + +import arrow.core.Try +import im.vector.matrix.android.api.pushrules.rest.PushRule +import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.internal.session.pushers.DefaultConditionResolver +import im.vector.matrix.android.internal.task.Task +import timber.log.Timber + +internal interface ProcessEventForPushTask : Task { + data class Params( + val events: List, + val rules: List + ) +} + +internal class DefaultProcessEventForPushTask( + private val defaultPushRuleService: DefaultPushRuleService +) : ProcessEventForPushTask { + + + override suspend fun execute(params: ProcessEventForPushTask.Params): Try { + return Try { + params.events.forEach { event -> + fulfilledBingRule(event, params.rules)?.let { + Timber.v("Rule $it match for event ${event.eventId}") + defaultPushRuleService.dispatchBing(event, it) + } + } + defaultPushRuleService.dispatchFinish() + } + } + + private fun fulfilledBingRule(event: Event, rules: List): PushRule? { + val conditionResolver = DefaultConditionResolver(event) + rules.filter { it.enabled }.forEach { rule -> + val isFullfilled = rule.conditions?.map { + it.asExecutableCondition()?.isSatisfied(conditionResolver) ?: false + }?.fold(true/*A rule with no conditions always matches*/, { acc, next -> + //All conditions must hold true for an event in order to apply the action for the event. + acc && next + }) ?: false + + if (isFullfilled) { + return rule + } + } + return null + } + +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/AddHttpPusherWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/AddHttpPusherWorker.kt new file mode 100644 index 0000000000..1a3577ee92 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/AddHttpPusherWorker.kt @@ -0,0 +1,98 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.pushers + +import android.content.Context +import androidx.work.CoroutineWorker +import androidx.work.WorkerParameters +import com.squareup.moshi.JsonClass +import com.zhuinden.monarchy.Monarchy +import im.vector.matrix.android.api.failure.Failure +import im.vector.matrix.android.api.session.pushers.PusherState +import im.vector.matrix.android.internal.database.mapper.toEntity +import im.vector.matrix.android.internal.database.model.PusherEntity +import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.di.MatrixKoinComponent +import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.util.WorkerParamsFactory +import org.koin.standalone.inject + +class AddHttpPusherWorker(context: Context, params: WorkerParameters) + : CoroutineWorker(context, params), MatrixKoinComponent { + + + @JsonClass(generateAdapter = true) + internal data class Params( + val pusher: JsonPusher, + val userId: String + ) + + private val pushersAPI by inject() + private val monarchy by inject() + + override suspend fun doWork(): Result { + + val params = WorkerParamsFactory.fromData(inputData) + ?: return Result.failure() + + val pusher = params.pusher + + if (pusher.pushKey.isBlank()) { + return Result.failure() + } + + val result = executeRequest { + apiCall = pushersAPI.setPusher(pusher) + } + return result.fold({ + when (it) { + is Failure.NetworkConnection -> Result.retry() + else -> { + monarchy.runTransactionSync { realm -> + PusherEntity.where(realm, params.userId, pusher.pushKey).findFirst()?.let { + //update it + it.state = PusherState.FAILED_TO_REGISTER + } + } + //always return success, or the chain will be stuck for ever! + Result.failure() + } + } + }, { + monarchy.runTransactionSync { realm -> + val echo = PusherEntity.where(realm, params.userId, pusher.pushKey).findFirst() + if (echo != null) { + //update it + echo.appDisplayName = pusher.appDisplayName + echo.appId = pusher.appId + echo.kind = pusher.kind + echo.lang = pusher.lang + echo.profileTag = pusher.profileTag + echo.data?.format = pusher.data?.format + echo.data?.url = pusher.data?.url + echo.state = PusherState.REGISTERED + } else { + pusher.toEntity(params.userId).also { + it.state = PusherState.REGISTERED + realm.insertOrUpdate(it) + } + } + } + Result.success() + + }) + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/DefaultConditionResolver.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/DefaultConditionResolver.kt new file mode 100644 index 0000000000..e85f43470f --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/DefaultConditionResolver.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.pushers + +import im.vector.matrix.android.api.auth.data.SessionParams +import im.vector.matrix.android.api.pushrules.* +import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.api.session.room.RoomService +import im.vector.matrix.android.internal.di.MatrixKoinComponent +import org.koin.standalone.inject +import timber.log.Timber + +internal class DefaultConditionResolver(val event: Event) : ConditionResolver, MatrixKoinComponent { + + private val roomService by inject() + + private val sessionParams by inject() + + override fun resolveEventMatchCondition(eventMatchCondition: EventMatchCondition): Boolean { + return eventMatchCondition.isSatisfied(event) + } + + override fun resolveRoomMemberCountCondition(roomMemberCountCondition: RoomMemberCountCondition): Boolean { + return roomMemberCountCondition.isSatisfied(event, roomService) + } + + override fun resolveSenderNotificationPermissionCondition(senderNotificationPermissionCondition: SenderNotificationPermissionCondition): Boolean { +// val roomId = event.roomId ?: return false +// val room = roomService.getRoom(roomId) ?: return false + //TODO RoomState not yet managed + Timber.e("POWER LEVELS STATE NOT YET MANAGED BY RIOTX") + return false //senderNotificationPermissionCondition.isSatisfied(event, ) + } + + override fun resolveContainsDisplayNameCondition(containsDisplayNameCondition: ContainsDisplayNameCondition): Boolean { + val roomId = event.roomId ?: return false + val room = roomService.getRoom(roomId) ?: return false + val myDisplayName = room.getRoomMember(sessionParams.credentials.userId)?.displayName + ?: return false + return containsDisplayNameCondition.isSatisfied(event, myDisplayName) + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/DefaultPusherService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/DefaultPusherService.kt new file mode 100644 index 0000000000..522387758b --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/DefaultPusherService.kt @@ -0,0 +1,114 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.pushers + +import androidx.lifecycle.LiveData +import androidx.work.* +import com.zhuinden.monarchy.Monarchy +import im.vector.matrix.android.api.MatrixCallback +import im.vector.matrix.android.api.auth.data.SessionParams +import im.vector.matrix.android.api.session.pushers.Pusher +import im.vector.matrix.android.api.session.pushers.PusherState +import im.vector.matrix.android.api.session.pushers.PushersService +import im.vector.matrix.android.internal.database.mapper.asDomain +import im.vector.matrix.android.internal.database.mapper.toEntity +import im.vector.matrix.android.internal.database.model.PusherEntity +import im.vector.matrix.android.internal.database.model.PusherEntityFields +import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.task.TaskExecutor +import im.vector.matrix.android.internal.task.configureWith +import im.vector.matrix.android.internal.util.WorkerParamsFactory +import java.util.* +import java.util.concurrent.TimeUnit + + +internal class DefaultPusherService( + private val monarchy: Monarchy, + private val sessionParam: SessionParams, + private val getPusherTask: GetPushersTask, + private val removePusherTask: RemovePusherTask, + private val taskExecutor: TaskExecutor +) : PushersService { + + + override fun refreshPushers() { + getPusherTask + .configureWith(Unit) + .dispatchTo(object : MatrixCallback { + override fun onSuccess(data: GetPushersResponse) { + monarchy.runTransactionSync { realm -> + //clear existings? + realm.where(PusherEntity::class.java) + .equalTo(PusherEntityFields.USER_ID, sessionParam.credentials.userId) + .findAll().deleteAllFromRealm() + data.pushers?.forEach { jsonPusher -> + jsonPusher.toEntity(sessionParam.credentials.userId).also { + it.state = PusherState.REGISTERED + realm.insertOrUpdate(it) + } + } + } + } + }) + .executeBy(taskExecutor) + } + + override fun addHttpPusher(pushkey: String, appId: String, profileTag: String, + lang: String, appDisplayName: String, deviceDisplayName: String, + url: String, append: Boolean, withEventIdOnly: Boolean) + : UUID { + + val pusher = JsonPusher( + pushKey = pushkey, + kind = "http", + appId = appId, + appDisplayName = appDisplayName, + deviceDisplayName = deviceDisplayName, + profileTag = profileTag, + lang = lang, + data = JsonPusherData(url, if (withEventIdOnly) PushersService.EVENT_ID_ONLY else null), + append = append) + + + val params = AddHttpPusherWorker.Params(pusher, sessionParam.credentials.userId) + + val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build() + + val request = OneTimeWorkRequestBuilder() + .setConstraints(constraints) + .setInputData(WorkerParamsFactory.toData(params)) + .setBackoffCriteria(BackoffPolicy.LINEAR, 10_000L, TimeUnit.MILLISECONDS) + .build() + WorkManager.getInstance().enqueue(request) + return request.id + } + + override fun removeHttpPusher(pushkey: String, appId: String, callback: MatrixCallback) { + val params = RemovePusherTask.Params(sessionParam.credentials.userId,pushkey,appId) + removePusherTask + .configureWith(params) + .dispatchTo(callback) + //.enableRetry() ?? + .executeBy(taskExecutor) + } + + override fun livePushers(): LiveData> { + return monarchy.findAllMappedWithChanges( + { realm -> PusherEntity.where(realm, sessionParam.credentials.userId) }, + { it.asDomain() } + ) + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushRulesTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushRulesTask.kt new file mode 100644 index 0000000000..b2c04842de --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushRulesTask.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.pushers + +import arrow.core.Try +import im.vector.matrix.android.api.pushrules.rest.GetPushRulesResponse +import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.task.Task + + +internal interface GetPushRulesTask : Task + +internal class DefaultGetPushRulesTask(private val pushRulesApi: PushRulesApi) : GetPushRulesTask { + + override suspend fun execute(params: Unit): Try { + return executeRequest { + apiCall = pushRulesApi.getAllRules() + } + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushersResponse.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushersResponse.kt new file mode 100644 index 0000000000..4f4cac9302 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushersResponse.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.pushers + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + + +@JsonClass(generateAdapter = true) +internal class GetPushersResponse( + @Json(name = "pushers") + val pushers: List? = null +) \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushersTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushersTask.kt new file mode 100644 index 0000000000..8587ab8576 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushersTask.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.pushers + +import arrow.core.Try +import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.task.Task + +internal interface GetPushersTask : Task + +internal class DefaultGetPusherTask(private val pushersAPI: PushersAPI) : GetPushersTask { + + override suspend fun execute(params: Unit): Try { + return executeRequest { + apiCall = pushersAPI.getPushers() + } + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/JsonPusher.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/JsonPusher.kt new file mode 100644 index 0000000000..d262eeb745 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/JsonPusher.kt @@ -0,0 +1,103 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.pushers + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass +import im.vector.matrix.android.internal.di.SerializeNulls + +/** + * Example: + * + * + * { + * "pushers": [ + * { + * "pushkey": "Xp/MzCt8/9DcSNE9cuiaoT5Ac55job3TdLSSmtmYl4A=", + * "kind": "http", + * "app_id": "face.mcapp.appy.prod", + * "app_display_name": "Appy McAppface", + * "device_display_name": "Alice's Phone", + * "profile_tag": "xyz", + * "lang": "en-US", + * "data": { + * "url": "https://example.com/_matrix/push/v1/notify" + * } + * }] + * } + * + */ + +@JsonClass(generateAdapter = true) +internal data class JsonPusher( + /** + * Required. This is a unique identifier for this pusher. See /set for more detail. Max length, 512 bytes. + */ + @Json(name = "pushkey") + val pushKey: String, + + /** + * Required. The kind of pusher. "http" is a pusher that sends HTTP pokes. + */ + @SerializeNulls + @Json(name = "kind") + val kind: String?, + + /** + * Required. This is a reverse-DNS style identifier for the application. Max length, 64 chars. + */ + @Json(name = "app_id") + val appId: String, + + /** + * Required. A string that will allow the user to identify what application owns this pusher. + */ + @Json(name = "app_display_name") + val appDisplayName: String? = null, + + /** + * Required. A string that will allow the user to identify what device owns this pusher. + */ + @Json(name = "device_display_name") + val deviceDisplayName: String? = null, + + /** + * This string determines which set of device specific rules this pusher executes. + */ + @Json(name = "profile_tag") + val profileTag: String? = null, + + /** + * Required. The preferred language for receiving notifications (e.g. 'en' or 'en-US') + */ + @Json(name = "lang") + val lang: String? = null, + + /** + * Required. A dictionary of information for the pusher implementation itself. + */ + @Json(name = "data") + val data: JsonPusherData? = null, + + // Only used to update add Pusher (body of api request) + // Used If true, the homeserver should add another pusher with the given pushkey and App ID in addition + // to any others with different user IDs. + // Otherwise, the homeserver must remove any other pushers with the same App ID and pushkey for different users. + // The default is false. + @Json(name = "append") + val append: Boolean? = false +) + diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/JsonPusherData.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/JsonPusherData.kt new file mode 100644 index 0000000000..95898acae2 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/JsonPusherData.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.pushers + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +internal data class JsonPusherData( + /** + * Required if kind is http. The URL to use to send notifications to. + */ + @Json(name = "url") + val url: String? = null, + + /** + * The format to use when sending notifications to the Push Gateway. + */ + @Json(name = "format") + val format: String? = null +) \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/PushRulesApi.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/PushRulesApi.kt new file mode 100644 index 0000000000..eed08622db --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/PushRulesApi.kt @@ -0,0 +1,83 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.pushers + +import im.vector.matrix.android.api.pushrules.rest.PushRule +import im.vector.matrix.android.api.pushrules.rest.GetPushRulesResponse +import im.vector.matrix.android.internal.network.NetworkConstants +import retrofit2.Call +import retrofit2.http.* + + +internal interface PushRulesApi { + /** + * Get all push rules + */ + @GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/") + fun getAllRules(): Call + + /** + * Update the ruleID enable status + * + * @param kind the notification kind (sender, room...) + * @param ruleId the ruleId + * @param enable the new enable status + */ + @PUT(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/global/{kind}/{ruleId}/enabled") + fun updateEnableRuleStatus(@Path("kind") kind: String, + @Path("ruleId") ruleId: String, + @Body enable: Boolean?) + : Call + + + /** + * Update the ruleID action + * + * @param kind the notification kind (sender, room...) + * @param ruleId the ruleId + * @param actions the actions + */ + @PUT(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/global/{kind}/{ruleId}/actions") + fun updateRuleActions(@Path("kind") kind: String, + @Path("ruleId") ruleId: String, + @Body actions: Any) + : Call + + + /** + * Delete a rule + * + * @param kind the notification kind (sender, room...) + * @param ruleId the ruleId + */ + @DELETE(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/global/{kind}/{ruleId}") + fun deleteRule(@Path("kind") kind: String, + @Path("ruleId") ruleId: String) + : Call + + /** + * Add the ruleID enable status + * + * @param kind the notification kind (sender, room...) + * @param ruleId the ruleId. + * @param rule the rule to add. + */ + @PUT(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/global/{kind}/{ruleId}") + fun addRule(@Path("kind") kind: String, + @Path("ruleId") ruleId: String, + @Body rule: PushRule) + : Call +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/PushersAPI.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/PushersAPI.kt new file mode 100644 index 0000000000..7516e48a0c --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/PushersAPI.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.pushers + +import im.vector.matrix.android.internal.network.NetworkConstants +import retrofit2.Call +import retrofit2.http.Body +import retrofit2.http.GET +import retrofit2.http.POST + + +internal interface PushersAPI { + + /** + * Get the pushers for this user. + * + * Ref: https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-pushers + */ + @GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushers") + fun getPushers(): Call + + /** + * This endpoint allows the creation, modification and deletion of pushers for this user ID. + * The behaviour of this endpoint varies depending on the values in the JSON body. + * + * Ref: https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-pushers-set + */ + @POST(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushers/set") + fun setPusher(@Body jsonPusher: JsonPusher): Call + +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/RemovePusherTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/RemovePusherTask.kt new file mode 100644 index 0000000000..3548afb526 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/RemovePusherTask.kt @@ -0,0 +1,63 @@ +package im.vector.matrix.android.internal.session.pushers + +import arrow.core.Try +import com.zhuinden.monarchy.Monarchy +import im.vector.matrix.android.api.session.pushers.Pusher +import im.vector.matrix.android.api.session.pushers.PusherState +import im.vector.matrix.android.internal.database.mapper.asDomain +import im.vector.matrix.android.internal.database.model.PusherEntity +import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.task.Task +import im.vector.matrix.android.internal.util.tryTransactionSync + +internal interface RemovePusherTask : Task { + data class Params(val userId: String, + val pushKey: String, + val pushAppId: String) +} + +internal class DefaultRemovePusherTask( + private val pushersAPI: PushersAPI, + private val monarchy: Monarchy +) : RemovePusherTask { + + override suspend fun execute(params: RemovePusherTask.Params): Try { + return Try { + var existing: Pusher? = null + monarchy.runTransactionSync { + val existingEntity = PusherEntity.where(it, params.userId, params.pushKey).findFirst() + existingEntity?.state == PusherState.UNREGISTERING + existing = existingEntity?.asDomain() + } + if (existing == null) { + throw Exception("No existing pusher") + } else { + existing!! + } + }.flatMap { + executeRequest { + val deleteBody = JsonPusher( + pushKey = params.pushKey, + appId = params.pushAppId, + // kind null deletes the pusher + kind = null, + appDisplayName = it.appDisplayName ?: "", + deviceDisplayName = it.deviceDisplayName ?: "", + profileTag = it.profileTag ?: "", + lang = it.lang, + data = JsonPusherData(it.data.url, it.data.format), + append = false + ) + apiCall = pushersAPI.setPusher(deleteBody) + } + }.flatMap { + monarchy.tryTransactionSync { + val existing = PusherEntity.where(it, params.userId, params.pushKey).findFirst() + existing?.deleteFromRealm() + } + } + } + + +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/UpdatePushRuleEnableStatusTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/UpdatePushRuleEnableStatusTask.kt new file mode 100644 index 0000000000..2deedcc944 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/UpdatePushRuleEnableStatusTask.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.pushers + +import arrow.core.Try +import im.vector.matrix.android.api.pushrules.rest.PushRule +import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.task.Task + + +internal interface UpdatePushRuleEnableStatusTask : Task { + data class Params(val kind: String, + val pushRule: PushRule, + val enabled: Boolean) +} + +internal class DefaultUpdatePushRuleEnableStatusTask(private val pushRulesApi: PushRulesApi) : UpdatePushRuleEnableStatusTask { + + override suspend fun execute(params: UpdatePushRuleEnableStatusTask.Params): Try { + return executeRequest { + apiCall = pushRulesApi.updateEnableRuleStatus(params.kind, params.pushRule.ruleId, params.enabled) + } + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoom.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoom.kt index 5c37fa7d87..3715e37007 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoom.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoom.kt @@ -33,6 +33,7 @@ import im.vector.matrix.android.internal.database.mapper.asDomain import im.vector.matrix.android.internal.database.model.RoomSummaryEntity import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.util.fetchCopied internal class DefaultRoom( override val roomId: String, @@ -52,7 +53,7 @@ internal class DefaultRoom( RelationService by relationService, MembershipService by roomMembersService { - override val roomSummary: LiveData by lazy { + override val liveRoomSummary: LiveData by lazy { val liveRealmData = RealmLiveData(monarchy.realmConfiguration) { realm -> RoomSummaryEntity.where(realm, roomId).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME) } @@ -68,6 +69,12 @@ internal class DefaultRoom( } } + override val roomSummary: RoomSummary? + get() { + var sum: RoomSummaryEntity? = monarchy.fetchCopied { RoomSummaryEntity.where(it, roomId).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME).findFirst() } + return sum?.asDomain() + } + override fun isEncrypted(): Boolean { return cryptoService.isRoomEncrypted(roomId) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomFactory.kt index ae1c30003b..42f2299ed7 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomFactory.kt @@ -17,6 +17,7 @@ package im.vector.matrix.android.internal.session.room import com.zhuinden.monarchy.Monarchy +import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.api.session.crypto.CryptoService import im.vector.matrix.android.api.session.room.Room import im.vector.matrix.android.internal.session.room.membership.DefaultMembershipService @@ -51,7 +52,8 @@ internal class RoomFactory(private val monarchy: Monarchy, private val cryptoService: CryptoService, private val findReactionEventForUndoTask: FindReactionEventForUndoTask, private val joinRoomTask: JoinRoomTask, - private val leaveRoomTask: LeaveRoomTask) { + private val leaveRoomTask: LeaveRoomTask, + private val sessionParams: SessionParams) { fun instantiate(roomId: String): Room { val roomMemberExtractor = SenderRoomMemberExtractor(roomId) @@ -61,7 +63,7 @@ internal class RoomFactory(private val monarchy: Monarchy, val sendService = DefaultSendService(roomId, eventFactory, cryptoService, monarchy) val stateService = DefaultStateService(roomId, taskExecutor, sendStateTask) val roomMembersService = DefaultMembershipService(roomId, monarchy, taskExecutor, loadRoomMembersTask, inviteTask, joinRoomTask, leaveRoomTask) - val readService = DefaultReadService(roomId, monarchy, taskExecutor, setReadMarkersTask) + val readService = DefaultReadService(roomId, monarchy, taskExecutor, setReadMarkersTask, sessionParams) val reactionService = DefaultRelationService(roomId, eventFactory, findReactionEventForUndoTask, monarchy, taskExecutor) return DefaultRoom( diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt index ead2c8e429..1cefe088a9 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt @@ -80,7 +80,7 @@ class RoomModule { } scope(DefaultSession.SCOPE) { - RoomFactory(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) + RoomFactory(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) } scope(DefaultSession.SCOPE) { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/DefaultMembershipService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/DefaultMembershipService.kt index a9b48ea69a..a3c2fc1392 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/DefaultMembershipService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/DefaultMembershipService.kt @@ -66,6 +66,14 @@ internal class DefaultMembershipService(private val roomId: String, ) } + override fun getNumberOfJoinedMembers(): Int { + var result = 0 + monarchy.runTransactionSync { + result = RoomMembers(it, roomId).getNumberOfJoinedMembers() + } + return result + } + override fun invite(userId: String, callback: MatrixCallback) { val params = InviteTask.Params(roomId, userId) inviteTask.configureWith(params) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/read/DefaultReadService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/read/DefaultReadService.kt index 30bbe30c28..46e0848203 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/read/DefaultReadService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/read/DefaultReadService.kt @@ -18,9 +18,15 @@ package im.vector.matrix.android.internal.session.room.read import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.MatrixCallback +import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.api.session.room.read.ReadService +import im.vector.matrix.android.internal.database.model.ChunkEntity import im.vector.matrix.android.internal.database.model.EventEntity +import im.vector.matrix.android.internal.database.model.ReadReceiptEntity +import im.vector.matrix.android.internal.database.query.find +import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom import im.vector.matrix.android.internal.database.query.latestEvent +import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith import im.vector.matrix.android.internal.util.fetchCopied @@ -28,7 +34,8 @@ import im.vector.matrix.android.internal.util.fetchCopied internal class DefaultReadService(private val roomId: String, private val monarchy: Monarchy, private val taskExecutor: TaskExecutor, - private val setReadMarkersTask: SetReadMarkersTask) : ReadService { + private val setReadMarkersTask: SetReadMarkersTask, + private val sessionParams: SessionParams) : ReadService { override fun markAllAsRead(callback: MatrixCallback) { val latestEvent = getLatestEvent() @@ -50,5 +57,20 @@ internal class DefaultReadService(private val roomId: String, return monarchy.fetchCopied { EventEntity.latestEvent(it, roomId) } } + override fun isEventRead(eventId: String): Boolean { + var isEventRead = false + monarchy.doWithRealm { + val readReceipt = ReadReceiptEntity.where(it, roomId, sessionParams.credentials.userId).findFirst() + ?: return@doWithRealm + val liveChunk = ChunkEntity.findLastLiveChunkFromRoom(it, roomId) + ?: return@doWithRealm + val readReceiptIndex = liveChunk.events.find(readReceipt.eventId)?.displayIndex + ?: Int.MIN_VALUE + val eventToCheckIndex = liveChunk.events.find(eventId)?.displayIndex + ?: Int.MAX_VALUE + isEventRead = eventToCheckIndex <= readReceiptIndex + } + return isEventRead + } } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimelineService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimelineService.kt index c8629ecce8..828964d837 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimelineService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimelineService.kt @@ -20,7 +20,11 @@ import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.session.room.timeline.Timeline import im.vector.matrix.android.api.session.room.timeline.TimelineEvent import im.vector.matrix.android.api.session.room.timeline.TimelineService +import im.vector.matrix.android.internal.database.model.ChunkEntity import im.vector.matrix.android.internal.database.model.EventEntity +import im.vector.matrix.android.internal.database.model.ReadReceiptEntity +import im.vector.matrix.android.internal.database.query.find +import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.util.fetchCopyMap @@ -45,4 +49,5 @@ internal class DefaultTimelineService(private val roomId: String, }) } + } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncTask.kt index 1f09f4c611..01df21281c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncTask.kt @@ -29,7 +29,7 @@ import im.vector.matrix.android.internal.task.Task internal interface SyncTask : Task { - data class Params(val token: String?) + data class Params(val token: String?, var timeout: Long = 30_000L) } @@ -42,10 +42,10 @@ internal class DefaultSyncTask(private val syncAPI: SyncAPI, override suspend fun execute(params: SyncTask.Params): Try { val requestParams = HashMap() - var timeout = 0 + var timeout = 0L if (params.token != null) { requestParams["since"] = params.token - timeout = 30000 + timeout = params.timeout } requestParams["timeout"] = timeout.toString() requestParams["filter"] = filterRepository.getFilter() diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncService.kt new file mode 100644 index 0000000000..504c621aaf --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncService.kt @@ -0,0 +1,243 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.sync.job + +import android.app.Service +import android.content.Intent +import android.os.Binder +import android.os.IBinder +import com.squareup.moshi.JsonEncodingException +import im.vector.matrix.android.api.MatrixCallback +import im.vector.matrix.android.api.failure.Failure +import im.vector.matrix.android.api.failure.MatrixError +import im.vector.matrix.android.api.util.Cancelable +import im.vector.matrix.android.internal.di.MatrixKoinComponent +import im.vector.matrix.android.internal.network.NetworkConnectivityChecker +import im.vector.matrix.android.internal.session.sync.SyncTask +import im.vector.matrix.android.internal.session.sync.SyncTokenStore +import im.vector.matrix.android.internal.session.sync.model.SyncResponse +import im.vector.matrix.android.internal.task.TaskExecutor +import im.vector.matrix.android.internal.task.TaskThread +import im.vector.matrix.android.internal.task.configureWith +import org.koin.standalone.inject +import timber.log.Timber +import java.net.SocketTimeoutException +import java.util.* +import kotlin.collections.ArrayList + +private const val DEFAULT_LONG_POOL_TIMEOUT = 10_000L +private const val BACKGROUND_LONG_POOL_TIMEOUT = 0L + +/** + * Can execute periodic sync task. + * An IntentService is used in conjunction with the AlarmManager and a Broadcast Receiver + * in order to be able to perform a sync even if the app is not running. + * The and must be declared in the Manifest or the app using the SDK + */ +open class SyncService : Service(), MatrixKoinComponent { + + private var mIsSelfDestroyed: Boolean = false + private var cancelableTask: Cancelable? = null + private val syncTokenStore: SyncTokenStore by inject() + + private val syncTask: SyncTask by inject() + private val networkConnectivityChecker: NetworkConnectivityChecker by inject() + private val taskExecutor: TaskExecutor by inject() + + private var localBinder = LocalBinder() + + var timer = Timer() + + var nextBatchDelay = 0L + var timeout = 10_000L + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + Timber.i("onStartCommand ${intent}") + nextBatchDelay = 60_000L + timeout = 0 + intent?.let { + if (cancelableTask == null) { + timer.cancel() + timer = Timer() + doSync(true) + } else { + //Already syncing ignore + Timber.i("Received a start while was already syncking... ignore") + } + } + //No intent just start the service, an alarm will should call with intent + return START_STICKY + } + + override fun onDestroy() { + Timber.i("## onDestroy() : $this") + + if (!mIsSelfDestroyed) { + Timber.w("## Destroy by the system : $this") + } + + cancelableTask?.cancel() + super.onDestroy() + } + + fun stopMe() { + timer.cancel() + timer = Timer() + cancelableTask?.cancel() + mIsSelfDestroyed = true + stopSelf() + } + + fun doSync(once: Boolean = false) { + var nextBatch = syncTokenStore.getLastToken() + if (!networkConnectivityChecker.isConnected()) { + Timber.v("Sync is Paused. Waiting...") + //TODO Retry in ? + timer.schedule(object : TimerTask() { + override fun run() { + doSync() + } + }, 5_000L) + } else { + Timber.v("Execute sync request with token $nextBatch and timeout $timeout") + val params = SyncTask.Params(nextBatch, timeout) + cancelableTask = syncTask.configureWith(params) + .callbackOn(TaskThread.CALLER) + .executeOn(TaskThread.CALLER) + .dispatchTo(object : MatrixCallback { + override fun onSuccess(data: SyncResponse) { + cancelableTask = null + nextBatch = data.nextBatch + syncTokenStore.saveToken(nextBatch) + localBinder.notifySyncFinish() + if (!once) { + timer.schedule(object : TimerTask() { + override fun run() { + doSync() + } + }, nextBatchDelay) + } else { + //stop + stopMe() + } + } + + override fun onFailure(failure: Throwable) { + Timber.e(failure) + cancelableTask = null + localBinder.notifyFailure(failure) + if (failure is Failure.NetworkConnection + && failure.cause is SocketTimeoutException) { + // Timeout are not critical + timer.schedule(object : TimerTask() { + override fun run() { + doSync() + } + }, 5_000L) + } + + if (failure !is Failure.NetworkConnection + || failure.cause is JsonEncodingException) { + // Wait 10s before retrying + timer.schedule(object : TimerTask() { + override fun run() { + doSync() + } + }, 5_000L) + } + + if (failure is Failure.ServerError + && (failure.error.code == MatrixError.UNKNOWN_TOKEN || failure.error.code == MatrixError.MISSING_TOKEN)) { + // No token or invalid token, stop the thread + stopSelf() + } + + } + + }) + .executeBy(taskExecutor) + + } + } + + override fun onBind(intent: Intent?): IBinder { + return localBinder + } + + inner class LocalBinder : Binder() { + + private var listeners = ArrayList() + + fun addListener(listener: SyncListener) { + if (!listeners.contains(listener)) { + listeners.add(listener) + } + } + + fun removeListener(listener: SyncListener) { + listeners.remove(listener) + } + + internal fun notifySyncFinish() { + + listeners.forEach { + try { + it.onSyncFinsh() + } catch (t: Throwable) { + Timber.e("Failed to notify listener $it") + } + } + } + + internal fun notifyNetworkNotAvailable() { + listeners.forEach { + try { + it.networkNotAvailable() + } catch (t: Throwable) { + Timber.e("Failed to notify listener $it") + } + } + } + + internal fun notifyFailure(throwable: Throwable) { + + listeners.forEach { + try { + it.onFailed(throwable) + } catch (t: Throwable) { + Timber.e("Failed to notify listener $it") + } + } + + } + + fun getService(): SyncService = this@SyncService + + } + + interface SyncListener { + fun onSyncFinsh() + fun networkNotAvailable() + fun onFailed(throwable: Throwable) + } + + companion object { + + fun startLongPool(delay: Long) { + + } + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncServiceOld.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncServiceOld.kt new file mode 100644 index 0000000000..a76aa99d84 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncServiceOld.kt @@ -0,0 +1,201 @@ +package im.vector.matrix.android.internal.session.sync.job + +import android.app.Service +import android.content.Intent +import android.os.Binder +import android.os.IBinder +import com.squareup.moshi.JsonEncodingException +import im.vector.matrix.android.api.MatrixCallback +import im.vector.matrix.android.api.failure.Failure +import im.vector.matrix.android.api.failure.MatrixError +import im.vector.matrix.android.api.util.Cancelable +import im.vector.matrix.android.internal.di.MatrixKoinComponent +import im.vector.matrix.android.internal.network.NetworkConnectivityChecker +import im.vector.matrix.android.internal.session.sync.SyncTask +import im.vector.matrix.android.internal.session.sync.SyncTokenStore +import im.vector.matrix.android.internal.session.sync.model.SyncResponse +import im.vector.matrix.android.internal.task.TaskExecutor +import im.vector.matrix.android.internal.task.TaskThread +import im.vector.matrix.android.internal.task.configureWith +import org.koin.standalone.inject +import timber.log.Timber +import java.net.SocketTimeoutException +import java.util.* +import kotlin.collections.ArrayList + +private const val DEFAULT_LONG_POOL_TIMEOUT = 10_000L +private const val BACKGROUND_LONG_POOL_TIMEOUT = 0L + +/** + * Can execute periodic sync task. + * An IntentService is used in conjunction with the AlarmManager and a Broadcast Receiver + * in order to be able to perform a sync even if the app is not running. + * The and must be declared in the Manifest or the app using the SDK + */ +open class SyncServiceOld : Service(), MatrixKoinComponent { + + private var mIsSelfDestroyed: Boolean = false + private var cancelableTask: Cancelable? = null + private val syncTokenStore: SyncTokenStore by inject() + + private val syncTask: SyncTask by inject() + private val networkConnectivityChecker: NetworkConnectivityChecker by inject() + private val taskExecutor: TaskExecutor by inject() + + private var localBinder = LocalBinder() + + val timer = Timer() + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + Timber.i("onStartCommand ${intent}") + intent?.let { + if (cancelableTask == null) { + doSync(BACKGROUND_LONG_POOL_TIMEOUT) + } else { + //Already syncing ignore + Timber.i("Received a start while was already syncking... ignore") + } + } + //No intent just start the service, an alarm will should call with intent + return START_STICKY + } + + override fun onDestroy() { + Timber.i("## onDestroy() : $this") + + if (!mIsSelfDestroyed) { + Timber.w("## Destroy by the system : $this") + } + + cancelableTask?.cancel() + super.onDestroy() + } + + fun stopMe() { + cancelableTask?.cancel() + mIsSelfDestroyed = true + stopSelf() + } + + fun doSync(currentLongPoolTimeoutMs: Long = DEFAULT_LONG_POOL_TIMEOUT) { + var nextBatch = syncTokenStore.getLastToken() + if (!networkConnectivityChecker.isConnected()) { + Timber.v("Sync is Paused. Waiting...") + //TODO Retry in ? + } else { + Timber.v("Execute sync request with token $nextBatch and timeout $currentLongPoolTimeoutMs") + val params = SyncTask.Params(nextBatch, currentLongPoolTimeoutMs) + cancelableTask = syncTask.configureWith(params) + .callbackOn(TaskThread.CALLER) + .executeOn(TaskThread.CALLER) + .dispatchTo(object : MatrixCallback { + override fun onSuccess(data: SyncResponse) { + cancelableTask = null + nextBatch = data.nextBatch + syncTokenStore.saveToken(nextBatch) + localBinder.notifySyncFinish() + } + + override fun onFailure(failure: Throwable) { + Timber.e(failure) + cancelableTask = null + localBinder.notifyFailure(failure) +// if (failure is Failure.NetworkConnection +// && failure.cause is SocketTimeoutException) { +// // Timeout are not critical +// localBinder.notifyFailure() +// } +// +// if (failure !is Failure.NetworkConnection +// || failure.cause is JsonEncodingException) { +// // Wait 10s before retrying +//// Thread.sleep(RETRY_WAIT_TIME_MS) +// //TODO Retry in 10S? +// } +// +// if (failure is Failure.ServerError +// && (failure.error.code == MatrixError.UNKNOWN_TOKEN || failure.error.code == MatrixError.MISSING_TOKEN)) { +// // No token or invalid token, stop the thread +// stopSelf() +// } + + } + + }) + .executeBy(taskExecutor) + + //TODO return and schedule a new one? +// Timber.v("Waiting for $currentLongPoolDelayMs delay before new pool...") +// if (currentLongPoolDelayMs > 0) Thread.sleep(currentLongPoolDelayMs) +// Timber.v("...Continue") + } + } + + override fun onBind(intent: Intent?): IBinder { + return localBinder + } + + inner class LocalBinder : Binder() { + + private var listeners = ArrayList() + + fun addListener(listener: SyncListener) { + if (!listeners.contains(listener)) { + listeners.add(listener) + } + } + + fun removeListener(listener: SyncListener) { + listeners.remove(listener) + } + + internal fun notifySyncFinish() { + + listeners.forEach { + try { + it.onSyncFinsh() + } catch (t: Throwable) { + Timber.e("Failed to notify listener $it") + } + } + } + + internal fun notifyNetworkNotAvailable() { + listeners.forEach { + try { + it.networkNotAvailable() + } catch (t: Throwable) { + Timber.e("Failed to notify listener $it") + } + } + } + + internal fun notifyFailure(throwable: Throwable) { + + listeners.forEach { + try { + it.onFailed(throwable) + } catch (t: Throwable) { + Timber.e("Failed to notify listener $it") + } + } + + } + + fun getService(): SyncServiceOld = this@SyncServiceOld + + } + + interface SyncListener { + fun onSyncFinsh() + fun networkNotAvailable() + fun onFailed(throwable: Throwable) + } + + companion object { + + fun startLongPool(delay: Long) { + + } + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncThread.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncThread.kt index 7bad5e64d9..abb27a8a11 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncThread.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncThread.kt @@ -37,6 +37,8 @@ import java.net.SocketTimeoutException import java.util.concurrent.CountDownLatch private const val RETRY_WAIT_TIME_MS = 10_000L +private const val DEFAULT_LONG_POOL_TIMEOUT = 30_000L +private const val DEFAULT_LONG_POOL_DELAY = 0L internal class SyncThread(private val syncTask: SyncTask, private val networkConnectivityChecker: NetworkConnectivityChecker, @@ -91,14 +93,14 @@ internal class SyncThread(private val syncTask: SyncTask, while (state != SyncState.KILLING) { if (!networkConnectivityChecker.isConnected() || state == SyncState.PAUSED) { - Timber.v("Waiting...") + Timber.v("Sync is Paused. Waiting...") synchronized(lock) { lock.wait() } } else { - Timber.v("Execute sync request with token $nextBatch") + Timber.v("Execute sync request with token $nextBatch and timeout $DEFAULT_LONG_POOL_TIMEOUT") val latch = CountDownLatch(1) - val params = SyncTask.Params(nextBatch) + val params = SyncTask.Params(nextBatch, DEFAULT_LONG_POOL_TIMEOUT) cancelableTask = syncTask.configureWith(params) .callbackOn(TaskThread.CALLER) .executeOn(TaskThread.CALLER) @@ -135,10 +137,15 @@ internal class SyncThread(private val syncTask: SyncTask, }) .executeBy(taskExecutor) - latch.await() + + latch.await() if (state is SyncState.RUNNING) { updateStateTo(SyncState.RUNNING(catchingUp = false)) } + + Timber.v("Waiting for $DEFAULT_LONG_POOL_DELAY delay before new pool...") + if (DEFAULT_LONG_POOL_DELAY > 0) sleep(DEFAULT_LONG_POOL_DELAY) + Timber.v("...Continue") } } Timber.v("Sync killed") @@ -166,7 +173,6 @@ internal class SyncThread(private val syncTask: SyncTask, pause() } - } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncWorker.kt new file mode 100644 index 0000000000..dd5b91f688 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncWorker.kt @@ -0,0 +1,121 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.internal.session.sync.job + +import android.content.Context +import androidx.work.* +import com.squareup.moshi.JsonClass +import im.vector.matrix.android.api.failure.Failure +import im.vector.matrix.android.api.failure.MatrixError +import im.vector.matrix.android.internal.auth.SessionParamsStore +import im.vector.matrix.android.internal.di.MatrixKoinComponent +import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.filter.FilterRepository +import im.vector.matrix.android.internal.session.sync.SyncAPI +import im.vector.matrix.android.internal.session.sync.SyncResponseHandler +import im.vector.matrix.android.internal.session.sync.SyncTokenStore +import im.vector.matrix.android.internal.session.sync.model.SyncResponse +import im.vector.matrix.android.internal.util.WorkerParamsFactory +import org.koin.standalone.inject +import timber.log.Timber +import java.util.concurrent.TimeUnit + + +private const val DEFAULT_LONG_POOL_TIMEOUT = 0L + +internal class SyncWorker(context: Context, + workerParameters: WorkerParameters +) : CoroutineWorker(context, workerParameters), MatrixKoinComponent { + + @JsonClass(generateAdapter = true) + internal data class Params( + val timeout: Long = DEFAULT_LONG_POOL_TIMEOUT, + val automaticallyRetry: Boolean = false + ) + + private val syncAPI by inject() + private val filterRepository by inject() + private val syncResponseHandler by inject() + private val sessionParamsStore by inject() + private val syncTokenStore by inject() + + + override suspend fun doWork(): Result { + Timber.i("Sync work starting") + val params = WorkerParamsFactory.fromData(inputData) + ?: Params() + + val requestParams = HashMap() + requestParams["timeout"] = params.timeout.toString() + requestParams["filter"] = filterRepository.getFilter() + val token = syncTokenStore.getLastToken()?.also { requestParams["since"] = it } + Timber.i("Sync work last token $token") + + return executeRequest { + apiCall = syncAPI.sync(requestParams) + }.fold( + { + if (it is Failure.ServerError + && it.error.code == MatrixError.UNKNOWN_TOKEN) { + sessionParamsStore.delete() + Result.failure() + } else { + Timber.i("Sync work failed $it") + Result.retry() + } + }, + { + Timber.i("Sync work success next batch ${it.nextBatch}") + if (!isStopped) { + syncResponseHandler.handleResponse(it, token, false) + syncTokenStore.saveToken(it.nextBatch) + } + if (params.automaticallyRetry) Result.retry() else Result.success() + } + ) + } + + companion object { + fun requireBackgroundSync(serverTimeout: Long = 0) { + val data = WorkerParamsFactory.toData(Params(serverTimeout, false)) + val workRequest = OneTimeWorkRequestBuilder() + .setInputData(data) + .setConstraints(Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build()) + .setBackoffCriteria(BackoffPolicy.LINEAR, 1_000, TimeUnit.MILLISECONDS) + .build() + WorkManager.getInstance().enqueueUniqueWork("BG_SYNCP", ExistingWorkPolicy.REPLACE, workRequest) + } + + fun automaticallyBackgroundSync(serverTimeout: Long = 0, delay: Long = 30_000) { + val data = WorkerParamsFactory.toData(Params(serverTimeout, true)) + val workRequest = OneTimeWorkRequestBuilder() + .setInputData(data) + .setConstraints(Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build()) + .setBackoffCriteria(BackoffPolicy.LINEAR, delay, TimeUnit.MILLISECONDS) + .build() + WorkManager.getInstance().enqueueUniqueWork("BG_SYNCP", ExistingWorkPolicy.REPLACE, workRequest) + } + + fun stopAnyBackgroundSync() { + WorkManager.getInstance().cancelUniqueWork("BG_SYNCP") + } + } + +} \ No newline at end of file diff --git a/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushRuleActionsTest.kt b/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushRuleActionsTest.kt new file mode 100644 index 0000000000..9c0e4e611f --- /dev/null +++ b/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushRuleActionsTest.kt @@ -0,0 +1,74 @@ +package im.vector.matrix.android.api.pushrules + +import im.vector.matrix.android.api.pushrules.rest.PushRule +import im.vector.matrix.android.internal.di.MoshiProvider +import org.junit.Assert +import org.junit.Test + + +class PushRuleActionsTest { + + @Test + fun test_action_parsing() { + val rawPushRule = """ + { + "rule_id": ".m.rule.invite_for_me", + "default": true, + "enabled": true, + "conditions": [ + { + "key": "type", + "kind": "event_match", + "pattern": "m.room.member" + }, + { + "key": "content.membership", + "kind": "event_match", + "pattern": "invite" + }, + { + "key": "state_key", + "kind": "event_match", + "pattern": "[the user's Matrix ID]" + } + ], + "actions": [ + "notify", + { + "set_tweak": "sound", + "value": "default" + }, + { + "set_tweak": "highlight", + "value": false + } + ] + } + """.trimIndent() + + + val pushRule = MoshiProvider.providesMoshi().adapter(PushRule::class.java).fromJson(rawPushRule) + + Assert.assertNotNull("Should have parsed the rule", pushRule) + Assert.assertNotNull("Failed to parse actions", Action.mapFrom(pushRule!!)) + + val actions = Action.mapFrom(pushRule) + Assert.assertEquals(3, actions!!.size) + + + Assert.assertEquals("First action should be notify", Action.Type.NOTIFY, actions[0].type) + + + Assert.assertEquals("Second action should be tweak", Action.Type.SET_TWEAK, actions[1].type) + Assert.assertEquals("Second action tweak key should be sound", "sound", actions[1].tweak_action) + Assert.assertEquals("Second action should have default as stringValue", "default", actions[1].stringValue) + Assert.assertNull("Second action boolValue should be null", actions[1].boolValue) + + + Assert.assertEquals("Third action should be tweak", Action.Type.SET_TWEAK, actions[2].type) + Assert.assertEquals("Third action tweak key should be highlight", "highlight", actions[2].tweak_action) + Assert.assertEquals("Third action tweak param should be false", false, actions[2].boolValue) + Assert.assertNull("Third action stringValue should be null", actions[2].stringValue) + + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt b/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt new file mode 100644 index 0000000000..11c2be1db9 --- /dev/null +++ b/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt @@ -0,0 +1,294 @@ +package im.vector.matrix.android.api.pushrules + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import im.vector.matrix.android.api.MatrixCallback +import im.vector.matrix.android.api.session.content.ContentAttachmentData +import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.api.session.events.model.toContent +import im.vector.matrix.android.api.session.room.Room +import im.vector.matrix.android.api.session.room.RoomService +import im.vector.matrix.android.api.session.room.model.EventAnnotationsSummary +import im.vector.matrix.android.api.session.room.model.Membership +import im.vector.matrix.android.api.session.room.model.RoomMember +import im.vector.matrix.android.api.session.room.model.RoomSummary +import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams +import im.vector.matrix.android.api.session.room.model.message.MessageTextContent +import im.vector.matrix.android.api.session.room.timeline.Timeline +import im.vector.matrix.android.api.session.room.timeline.TimelineEvent +import im.vector.matrix.android.api.util.Cancelable +import org.junit.Assert +import org.junit.Test + +class PushrulesConditionTest { + + + @Test + fun test_eventmatch_type_condition() { + val condition = EventMatchCondition("type", "m.room.message") + + val simpleTextEvent = Event( + type = "m.room.message", + eventId = "mx0", + content = MessageTextContent("m.text", "Yo wtf?").toContent(), + originServerTs = 0) + + val rm = RoomMember( + Membership.INVITE, + displayName = "Foo", + avatarUrl = "mxc://matrix.org/EqMZYbREvHXvYFyfxOlkf" + ) + val simpleRoomMemberEvent = Event( + type = "m.room.member", + eventId = "mx0", + stateKey = "@foo:matrix.org", + content = rm.toContent(), + originServerTs = 0) + + assert(condition.isSatisfied(simpleTextEvent)) + assert(!condition.isSatisfied(simpleRoomMemberEvent)) + } + + @Test + fun test_eventmatch_path_condition() { + val condition = EventMatchCondition("content.msgtype", "m.text") + + val simpleTextEvent = Event( + type = "m.room.message", + eventId = "mx0", + content = MessageTextContent("m.text", "Yo wtf?").toContent(), + originServerTs = 0) + + assert(condition.isSatisfied(simpleTextEvent)) + + Event( + type = "m.room.member", + eventId = "mx0", + stateKey = "@foo:matrix.org", + content = RoomMember( + Membership.INVITE, + displayName = "Foo", + avatarUrl = "mxc://matrix.org/EqMZYbREvHXvYFyfxOlkf" + ).toContent(), + originServerTs = 0 + ).apply { + assert(EventMatchCondition("content.membership", "invite").isSatisfied(this)) + } + + } + + @Test + fun test_eventmatch_cake_condition() { + val condition = EventMatchCondition("content.body", "cake") + + Event( + type = "m.room.message", + eventId = "mx0", + content = MessageTextContent("m.text", "How was the cake?").toContent(), + originServerTs = 0 + ).apply { + assert(condition.isSatisfied(this)) + } + + Event( + type = "m.room.message", + eventId = "mx0", + content = MessageTextContent("m.text", "Howwasthecake?").toContent(), + originServerTs = 0 + ).apply { + assert(condition.isSatisfied(this)) + } + + } + + @Test + fun test_eventmatch_cakelie_condition() { + val condition = EventMatchCondition("content.body", "cake*lie") + + val simpleTextEvent = Event( + type = "m.room.message", + eventId = "mx0", + content = MessageTextContent("m.text", "How was the cakeisalie?").toContent(), + originServerTs = 0) + + assert(condition.isSatisfied(simpleTextEvent)) + } + + + @Test + fun test_roommember_condition() { + + + val conditionEqual3 = RoomMemberCountCondition("3") + val conditionEqual3Bis = RoomMemberCountCondition("==3") + val conditionLessThan3 = RoomMemberCountCondition("<3") + + val session = MockRoomService() + + Event( + type = "m.room.message", + eventId = "mx0", + content = MessageTextContent("m.text", "A").toContent(), + originServerTs = 0, + roomId = "2joined").also { + Assert.assertFalse("This room does not have 3 members", conditionEqual3.isSatisfied(it, session)) + Assert.assertFalse("This room does not have 3 members", conditionEqual3Bis.isSatisfied(it, session)) + Assert.assertTrue("This room has less than 3 members", conditionLessThan3.isSatisfied(it, session)) + } + + Event( + type = "m.room.message", + eventId = "mx0", + content = MessageTextContent("m.text", "A").toContent(), + originServerTs = 0, + roomId = "3joined").also { + Assert.assertTrue("This room has 3 members",conditionEqual3.isSatisfied(it, session)) + Assert.assertTrue("This room has 3 members",conditionEqual3Bis.isSatisfied(it, session)) + Assert.assertFalse("This room has more than 3 members",conditionLessThan3.isSatisfied(it, session)) + } + } + + + class MockRoomService() : RoomService { + override fun createRoom(createRoomParams: CreateRoomParams, callback: MatrixCallback) { + + } + + override fun getRoom(roomId: String): Room? { + return when (roomId) { + "2joined" -> MockRoom(roomId, 2) + "3joined" -> MockRoom(roomId, 3) + else -> null + } + } + + override fun liveRoomSummaries(): LiveData> { + return MutableLiveData() + } + + } + + class MockRoom(override val roomId: String, val _numberOfJoinedMembers: Int) : Room { + + + override fun getNumberOfJoinedMembers(): Int { + return _numberOfJoinedMembers + } + + override val liveRoomSummary: LiveData + get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates. + + override val roomSummary: RoomSummary? + get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates. + + override fun createTimeline(eventId: String?, allowedTypes: List?): Timeline { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun getTimeLineEvent(eventId: String): TimelineEvent? { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun sendTextMessage(text: String, msgType: String, autoMarkdown: Boolean): Cancelable { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun sendFormattedTextMessage(text: String, formattedText: String): Cancelable { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun sendMedia(attachment: ContentAttachmentData): Cancelable { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun sendMedias(attachments: List): Cancelable { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun redactEvent(event: Event, reason: String?): Cancelable { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun markAllAsRead(callback: MatrixCallback) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun setReadReceipt(eventId: String, callback: MatrixCallback) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun setReadMarker(fullyReadEventId: String, callback: MatrixCallback) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun isEventRead(eventId: String): Boolean { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun loadRoomMembersIfNeeded(): Cancelable { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun getRoomMember(userId: String): RoomMember? { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun getRoomMemberIdsLive(): LiveData> { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun invite(userId: String, callback: MatrixCallback) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun join(callback: MatrixCallback) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun leave(callback: MatrixCallback) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun updateTopic(topic: String, callback: MatrixCallback) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun sendReaction(reaction: String, targetEventId: String): Cancelable { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun undoReaction(reaction: String, targetEventId: String, myUserId: String) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun updateQuickReaction(reaction: String, oppositeReaction: String, targetEventId: String, myUserId: String) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun editTextMessage(targetEventId: String, newBodyText: String, newBodyAutoMarkdown: Boolean, compatibilityBodyText: String): Cancelable { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun replyToMessage(eventReplied: Event, replyText: String): Cancelable? { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun getEventSummaryLive(eventId: String): LiveData> { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun isEncrypted(): Boolean { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun encryptionAlgorithm(): String? { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun shouldEncryptForInvitedMembers(): Boolean { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + } + +} diff --git a/tools/check/forbidden_strings_in_resources.txt b/tools/check/forbidden_strings_in_resources.txt index 26d7725591..120438efde 100644 --- a/tools/check/forbidden_strings_in_resources.txt +++ b/tools/check/forbidden_strings_in_resources.txt @@ -70,13 +70,13 @@ DO NOT COMMIT layout_constraintRight_ layout_constraintLeft_ -### Use Preference from v7 library (android.support.v7.preference.PreferenceScreen) +### Use Preference from androidx library (androidx.preference.PreferenceScreen) + + + - + + + + + \ No newline at end of file diff --git a/vector/src/fdroid/java/im/vector/riotredesign/push/fcm/FcmHelper.java b/vector/src/fdroid/java/im/vector/riotredesign/push/fcm/FcmHelper.kt similarity index 69% rename from vector/src/fdroid/java/im/vector/riotredesign/push/fcm/FcmHelper.java rename to vector/src/fdroid/java/im/vector/riotredesign/push/fcm/FcmHelper.kt index 82f2df9aca..11826a7b35 100755 --- a/vector/src/fdroid/java/im/vector/riotredesign/push/fcm/FcmHelper.java +++ b/vector/src/fdroid/java/im/vector/riotredesign/push/fcm/FcmHelper.kt @@ -14,24 +14,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package im.vector.riotredesign.push.fcm; +package im.vector.riotredesign.push.fcm -import android.app.Activity; -import android.content.Context; +import android.app.Activity +import android.content.Context -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; +import im.vector.riotredesign.core.pushers.PushersManager -public class FcmHelper { +object FcmHelper { + + fun isPushSupported(): Boolean = false /** * Retrieves the FCM registration token. * * @return the FCM token or null if not received from FCM */ - @Nullable - public static String getFcmToken(Context context) { - return null; + fun getFcmToken(context: Context): String? { + return null } /** @@ -40,8 +40,7 @@ public class FcmHelper { * @param context android context * @param token the token to store */ - public static void storeFcmToken(@NonNull Context context, - @Nullable String token) { + fun storeFcmToken(context: Context, token: String?) { // No op } @@ -50,7 +49,7 @@ public class FcmHelper { * * @param activity the first launch Activity */ - public static void ensureFcmTokenIsRetrieved(final Activity activity) { + fun ensureFcmTokenIsRetrieved(activity: Activity, pushersManager: PushersManager) { // No op } } diff --git a/vector/src/fdroid/java/im/vector/riotredesign/push/fcm/NotificationTroubleshootTestManagerFactory.kt b/vector/src/fdroid/java/im/vector/riotredesign/push/fcm/NotificationTroubleshootTestManagerFactory.kt index 5663603b8f..0a3fdd227d 100644 --- a/vector/src/fdroid/java/im/vector/riotredesign/push/fcm/NotificationTroubleshootTestManagerFactory.kt +++ b/vector/src/fdroid/java/im/vector/riotredesign/push/fcm/NotificationTroubleshootTestManagerFactory.kt @@ -16,12 +16,8 @@ package im.vector.riotredesign.push.fcm import androidx.fragment.app.Fragment -import im.vector.fragments.troubleshoot.TestAccountSettings import im.vector.matrix.android.api.session.Session -import im.vector.riotredesign.features.settings.troubleshoot.NotificationTroubleshootTestManager -import im.vector.riotredesign.features.settings.troubleshoot.TestBingRulesSettings -import im.vector.riotredesign.features.settings.troubleshoot.TestDeviceSettings -import im.vector.riotredesign.features.settings.troubleshoot.TestSystemSettings +import im.vector.riotredesign.features.settings.troubleshoot.* import im.vector.riotredesign.push.fcm.troubleshoot.TestAutoStartBoot import im.vector.riotredesign.push.fcm.troubleshoot.TestBackgroundRestrictions diff --git a/vector/src/fdroid/java/im/vector/riotredesign/receiver/OnApplicationUpgradeReceiver.java b/vector/src/fdroid/java/im/vector/riotredesign/receiver/OnApplicationUpgradeOrRebootReceiver.kt similarity index 53% rename from vector/src/fdroid/java/im/vector/riotredesign/receiver/OnApplicationUpgradeReceiver.java rename to vector/src/fdroid/java/im/vector/riotredesign/receiver/OnApplicationUpgradeOrRebootReceiver.kt index 4cb092b1a7..a13bc71ada 100644 --- a/vector/src/fdroid/java/im/vector/riotredesign/receiver/OnApplicationUpgradeReceiver.java +++ b/vector/src/fdroid/java/im/vector/riotredesign/receiver/OnApplicationUpgradeOrRebootReceiver.kt @@ -1,5 +1,6 @@ /* * Copyright 2018 New Vector Ltd + * Copyright 2019 New Vector Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,21 +15,18 @@ * limitations under the License. */ -package im.vector.riotredesign.receiver; +package im.vector.riotredesign.receiver -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import im.vector.riotredesign.core.services.AlarmSyncBroadcastReceiver +import timber.log.Timber -import timber.log.Timber; +class OnApplicationUpgradeOrRebootReceiver : BroadcastReceiver() { -public class OnApplicationUpgradeReceiver extends BroadcastReceiver { - - @Override - public void onReceive(Context context, Intent intent) { - Timber.v("## onReceive() : Application has been upgraded, restart event stream service."); - - // Start Event stream - // TODO EventStreamServiceX.Companion.onApplicationUpgrade(context); + override fun onReceive(context: Context, intent: Intent) { + Timber.v("## onReceive() ${intent.action}") + AlarmSyncBroadcastReceiver.scheduleAlarm(context, 10) } } diff --git a/vector/src/gplay/google-services.json b/vector/src/gplay/google-services.json index 8ffc2cef44..187c727a24 100644 --- a/vector/src/gplay/google-services.json +++ b/vector/src/gplay/google-services.json @@ -10,14 +10,10 @@ "client_info": { "mobilesdk_app_id": "1:912726360885:android:448c9b63161abc9c", "android_client_info": { - "package_name": "im.vector.riotredesign" + "package_name": "im.vector.alpha" } }, "oauth_client": [ - { - "client_id": "912726360885-rsae0i66rgqt6ivnudu1pv4tksg9i8b2.apps.googleusercontent.com", - "client_type": 3 - }, { "client_id": "912726360885-e87n3jva9uoj4vbidvijq78ebg02asv2.apps.googleusercontent.com", "client_type": 3 @@ -29,15 +25,100 @@ } ], "services": { - "analytics_service": { - "status": 1 - }, "appinvite_service": { - "status": 1, - "other_platform_oauth_client": [] - }, - "ads_service": { - "status": 2 + "other_platform_oauth_client": [ + { + "client_id": "912726360885-rsae0i66rgqt6ivnudu1pv4tksg9i8b2.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + }, + { + "client_info": { + "mobilesdk_app_id": "1:912726360885:android:3120c24f6ef22f2b", + "android_client_info": { + "package_name": "im.vector.app" + } + }, + "oauth_client": [ + { + "client_id": "912726360885-e87n3jva9uoj4vbidvijq78ebg02asv2.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyAFZX8IhIfgzdOZvxDP_ISO5WYoU7jmQ5c" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "912726360885-rsae0i66rgqt6ivnudu1pv4tksg9i8b2.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + }, + { + "client_info": { + "mobilesdk_app_id": "1:912726360885:android:25ef253beaff462e", + "android_client_info": { + "package_name": "im.vector.riotredesign" + } + }, + "oauth_client": [ + { + "client_id": "912726360885-e87n3jva9uoj4vbidvijq78ebg02asv2.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyAFZX8IhIfgzdOZvxDP_ISO5WYoU7jmQ5c" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "912726360885-rsae0i66rgqt6ivnudu1pv4tksg9i8b2.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + }, + { + "client_info": { + "mobilesdk_app_id": "1:912726360885:android:bb204b7a7b08a10b", + "android_client_info": { + "package_name": "im.veon" + } + }, + "oauth_client": [ + { + "client_id": "912726360885-e87n3jva9uoj4vbidvijq78ebg02asv2.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyAFZX8IhIfgzdOZvxDP_ISO5WYoU7jmQ5c" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "912726360885-rsae0i66rgqt6ivnudu1pv4tksg9i8b2.apps.googleusercontent.com", + "client_type": 3 + } + ] } } } diff --git a/vector/src/gplay/java/im/vector/riotredesign/push/fcm/FcmHelper.java b/vector/src/gplay/java/im/vector/riotredesign/push/fcm/FcmHelper.java deleted file mode 100755 index 00fff7dd93..0000000000 --- a/vector/src/gplay/java/im/vector/riotredesign/push/fcm/FcmHelper.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright 2014 OpenMarket Ltd - * Copyright 2017 Vector Creations Ltd - * Copyright 2018 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package im.vector.riotredesign.push.fcm; - -import android.app.Activity; -import android.content.Context; -import android.preference.PreferenceManager; -import android.text.TextUtils; -import android.widget.Toast; - -import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.GoogleApiAvailability; -import com.google.android.gms.tasks.OnFailureListener; -import com.google.android.gms.tasks.OnSuccessListener; -import com.google.firebase.iid.FirebaseInstanceId; -import com.google.firebase.iid.InstanceIdResult; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import im.vector.riotredesign.R; -import timber.log.Timber; - -/** - * This class store the FCM token in SharedPrefs and ensure this token is retrieved. - * It has an alter ego in the fdroid variant. - */ -public class FcmHelper { - private static final String LOG_TAG = FcmHelper.class.getSimpleName(); - - private static final String PREFS_KEY_FCM_TOKEN = "FCM_TOKEN"; - - /** - * Retrieves the FCM registration token. - * - * @return the FCM token or null if not received from FCM - */ - @Nullable - public static String getFcmToken(Context context) { - return PreferenceManager.getDefaultSharedPreferences(context).getString(PREFS_KEY_FCM_TOKEN, null); - } - - /** - * Store FCM token to the SharedPrefs - * - * @param context android context - * @param token the token to store - */ - public static void storeFcmToken(@NonNull Context context, - @Nullable String token) { - PreferenceManager.getDefaultSharedPreferences(context) - .edit() - .putString(PREFS_KEY_FCM_TOKEN, token) - .apply(); - } - - /** - * onNewToken may not be called on application upgrade, so ensure my shared pref is set - * - * @param activity the first launch Activity - */ - public static void ensureFcmTokenIsRetrieved(final Activity activity) { - if (TextUtils.isEmpty(getFcmToken(activity))) { - - - //vfe: according to firebase doc - //'app should always check the device for a compatible Google Play services APK before accessing Google Play services features' - if (checkPlayServices(activity)) { - try { - FirebaseInstanceId.getInstance().getInstanceId() - .addOnSuccessListener(activity, new OnSuccessListener() { - @Override - public void onSuccess(InstanceIdResult instanceIdResult) { - storeFcmToken(activity, instanceIdResult.getToken()); - } - }) - .addOnFailureListener(activity, new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception e) { - Timber.e(e, "## ensureFcmTokenIsRetrieved() : failed " + e.getMessage()); - } - }); - } catch (Throwable e) { - Timber.e(e, "## ensureFcmTokenIsRetrieved() : failed " + e.getMessage()); - } - } else { - Toast.makeText(activity, R.string.no_valid_google_play_services_apk, Toast.LENGTH_SHORT).show(); - Timber.e("No valid Google Play Services found. Cannot use FCM."); - } - } - } - - /** - * Check the device to make sure it has the Google Play Services APK. If - * it doesn't, display a dialog that allows users to download the APK from - * the Google Play Store or enable it in the device's system settings. - */ - private static boolean checkPlayServices(Activity activity) { - GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); - int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity); - if (resultCode != ConnectionResult.SUCCESS) { - return false; - } - return true; - } -} diff --git a/vector/src/gplay/java/im/vector/riotredesign/push/fcm/FcmHelper.kt b/vector/src/gplay/java/im/vector/riotredesign/push/fcm/FcmHelper.kt new file mode 100755 index 0000000000..25f380f42f --- /dev/null +++ b/vector/src/gplay/java/im/vector/riotredesign/push/fcm/FcmHelper.kt @@ -0,0 +1,102 @@ +/* + * Copyright 2014 OpenMarket Ltd + * Copyright 2017 Vector Creations Ltd + * Copyright 2018 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.riotredesign.push.fcm + +import android.app.Activity +import android.content.Context +import android.preference.PreferenceManager +import android.widget.Toast +import com.google.android.gms.common.ConnectionResult +import com.google.android.gms.common.GoogleApiAvailability +import com.google.firebase.iid.FirebaseInstanceId +import im.vector.riotredesign.R +import im.vector.riotredesign.core.pushers.PushersManager +import timber.log.Timber + +/** + * This class store the FCM token in SharedPrefs and ensure this token is retrieved. + * It has an alter ego in the fdroid variant. + */ +object FcmHelper { + private val PREFS_KEY_FCM_TOKEN = "FCM_TOKEN" + + + fun isPushSupported(): Boolean = true + + /** + * Retrieves the FCM registration token. + * + * @return the FCM token or null if not received from FCM + */ + fun getFcmToken(context: Context): String? { + return PreferenceManager.getDefaultSharedPreferences(context).getString(PREFS_KEY_FCM_TOKEN, null) + } + + /** + * Store FCM token to the SharedPrefs + * TODO Store in realm + * + * @param context android context + * @param token the token to store + */ + fun storeFcmToken(context: Context, + token: String?) { + PreferenceManager.getDefaultSharedPreferences(context) + .edit() + .putString(PREFS_KEY_FCM_TOKEN, token) + .apply() + + } + + /** + * onNewToken may not be called on application upgrade, so ensure my shared pref is set + * + * @param activity the first launch Activity + */ + fun ensureFcmTokenIsRetrieved(activity: Activity, pushersManager: PushersManager) { + // if (TextUtils.isEmpty(getFcmToken(activity))) { + //'app should always check the device for a compatible Google Play services APK before accessing Google Play services features' + if (checkPlayServices(activity)) { + try { + FirebaseInstanceId.getInstance().instanceId + .addOnSuccessListener(activity) { instanceIdResult -> + storeFcmToken(activity, instanceIdResult.token) + pushersManager.registerPusherWithFcmKey(instanceIdResult.token) + } + .addOnFailureListener(activity) { e -> Timber.e(e, "## ensureFcmTokenIsRetrieved() : failed " + e.message) } + } catch (e: Throwable) { + Timber.e(e, "## ensureFcmTokenIsRetrieved() : failed " + e.message) + } + + } else { + Toast.makeText(activity, R.string.no_valid_google_play_services_apk, Toast.LENGTH_SHORT).show() + Timber.e("No valid Google Play Services found. Cannot use FCM.") + } + } + + /** + * Check the device to make sure it has the Google Play Services APK. If + * it doesn't, display a dialog that allows users to download the APK from + * the Google Play Store or enable it in the device's system settings. + */ + private fun checkPlayServices(activity: Activity): Boolean { + val apiAvailability = GoogleApiAvailability.getInstance() + val resultCode = apiAvailability.isGooglePlayServicesAvailable(activity) + return resultCode == ConnectionResult.SUCCESS + } +} diff --git a/vector/src/gplay/java/im/vector/riotredesign/push/fcm/NotificationTroubleshootTestManagerFactory.kt b/vector/src/gplay/java/im/vector/riotredesign/push/fcm/NotificationTroubleshootTestManagerFactory.kt index d306fa194e..ee4e59e4ab 100644 --- a/vector/src/gplay/java/im/vector/riotredesign/push/fcm/NotificationTroubleshootTestManagerFactory.kt +++ b/vector/src/gplay/java/im/vector/riotredesign/push/fcm/NotificationTroubleshootTestManagerFactory.kt @@ -16,15 +16,11 @@ package im.vector.riotredesign.push.fcm import androidx.fragment.app.Fragment -import im.vector.fragments.troubleshoot.TestAccountSettings import im.vector.matrix.android.api.session.Session +import im.vector.riotredesign.features.settings.troubleshoot.* import im.vector.riotredesign.push.fcm.troubleshoot.TestFirebaseToken import im.vector.riotredesign.push.fcm.troubleshoot.TestPlayServices import im.vector.riotredesign.push.fcm.troubleshoot.TestTokenRegistration -import im.vector.riotredesign.features.settings.troubleshoot.NotificationTroubleshootTestManager -import im.vector.riotredesign.features.settings.troubleshoot.TestBingRulesSettings -import im.vector.riotredesign.features.settings.troubleshoot.TestDeviceSettings -import im.vector.riotredesign.features.settings.troubleshoot.TestSystemSettings class NotificationTroubleshootTestManagerFactory { diff --git a/vector/src/gplay/java/im/vector/riotredesign/push/fcm/VectorFirebaseMessagingService.kt b/vector/src/gplay/java/im/vector/riotredesign/push/fcm/VectorFirebaseMessagingService.kt index dfdfe6be77..0dacb9399c 100755 --- a/vector/src/gplay/java/im/vector/riotredesign/push/fcm/VectorFirebaseMessagingService.kt +++ b/vector/src/gplay/java/im/vector/riotredesign/push/fcm/VectorFirebaseMessagingService.kt @@ -22,18 +22,23 @@ package im.vector.riotredesign.push.fcm import android.os.Handler import android.os.Looper import android.text.TextUtils +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.ProcessLifecycleOwner import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage +import im.vector.matrix.android.api.Matrix import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.events.model.Event import im.vector.riotredesign.BuildConfig import im.vector.riotredesign.R import im.vector.riotredesign.core.preference.BingRule +import im.vector.riotredesign.core.pushers.PushersManager import im.vector.riotredesign.features.badge.BadgeProxy import im.vector.riotredesign.features.notifications.NotifiableEventResolver import im.vector.riotredesign.features.notifications.NotifiableMessageEvent import im.vector.riotredesign.features.notifications.NotificationDrawerManager import im.vector.riotredesign.features.notifications.SimpleNotifiableEvent +import im.vector.riotredesign.features.settings.PreferencesManager import org.koin.android.ext.android.inject import timber.log.Timber @@ -42,12 +47,10 @@ import timber.log.Timber */ class VectorFirebaseMessagingService : FirebaseMessagingService() { - val notificationDrawerManager by inject() - - private val notifiableEventResolver by lazy { - NotifiableEventResolver(this) - } + private val notificationDrawerManager by inject() + private val pusherManager by inject() + private val notifiableEventResolver by inject() // UI handler private val mUIHandler by lazy { Handler(Looper.getMainLooper()) @@ -59,26 +62,27 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() { * @param message the message */ override fun onMessageReceived(message: RemoteMessage?) { + if (!PreferencesManager.areNotificationEnabledForDevice(applicationContext)) { + Timber.i("Notification are disabled for this device") + return + } + if (message == null || message.data == null) { Timber.e("## onMessageReceived() : received a null message or message with no data") return } if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) { - Timber.i("## onMessageReceived()" + message.data.toString()) - Timber.i("## onMessageReceived() from FCM with priority " + message.priority) + Timber.i("## onMessageReceived() %s", message.data.toString()) + Timber.i("## onMessageReceived() from FCM with priority %s", message.priority) } - - //safe guard - /* TODO - val pushManager = Matrix.getInstance(applicationContext).pushManager - if (!pushManager.areDeviceNotificationsAllowed()) { - Timber.i("## onMessageReceived() : the notifications are disabled") - return + mUIHandler.post { + if (ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) { + //we are in foreground, let the sync do the things? + Timber.v("PUSH received in a foreground state, ignore") + } else { + onMessageReceivedInternal(message.data) + } } - */ - - //TODO if the app is in foreground, we could just ignore this. The sync loop is already going? - // TODO mUIHandler.post { onMessageReceivedInternal(message.data, pushManager) } } /** @@ -90,9 +94,24 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() { override fun onNewToken(refreshedToken: String?) { Timber.i("onNewToken: FCM Token has been updated") FcmHelper.storeFcmToken(this, refreshedToken) - // TODO Matrix.getInstance(this)?.pushManager?.resetFCMRegistration(refreshedToken) + if (refreshedToken == null) { + Timber.w("onNewToken:received null token") + } else { + if (PreferencesManager.areNotificationEnabledForDevice(applicationContext)) { + pusherManager.registerPusherWithFcmKey(refreshedToken) + } + } } + /** + * Called when the FCM server deletes pending messages. This may be due to: + * - Too many messages stored on the FCM server. + * This can occur when an app's servers send a bunch of non-collapsible messages to FCM servers while the device is offline. + * - The device hasn't connected in a long time and the app server has recently (within the last 4 weeks) + * sent a message to the app on that device. + * + * It is recommended that the app do a full sync with the app server after receiving this call. + */ override fun onDeletedMessages() { Timber.v("## onDeletedMessages()") } @@ -103,55 +122,58 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() { * @param data Data map containing message data as key/value pairs. * For Set of keys use data.keySet(). */ - private fun onMessageReceivedInternal(data: Map /*, pushManager: PushManager*/) { + private fun onMessageReceivedInternal(data: Map) { try { if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) { Timber.i("## onMessageReceivedInternal() : $data") } + val eventId = data["event_id"] + val roomId = data["room_id"] + if (eventId == null || roomId == null) { + Timber.e("## onMessageReceivedInternal() missing eventId and/or roomId") + return + } // update the badge counter val unreadCount = data.get("unread")?.let { Integer.parseInt(it) } ?: 0 BadgeProxy.updateBadgeCount(applicationContext, unreadCount) - /* TODO - val session = Matrix.getInstance(applicationContext)?.defaultSession + val session = safeGetCurrentSession() - if (VectorApp.isAppInBackground() && !pushManager.isBackgroundSyncAllowed) { - //Notification contains metadata and maybe data information - handleNotificationWithoutSyncingMode(data, session) + if (session == null) { + Timber.w("## Can't sync from push, no current session") } else { - // Safe guard... (race?) - if (isEventAlreadyKnown(data["event_id"], data["room_id"])) return - //Catch up!! - EventStreamServiceX.onPushReceived(this) + if (isEventAlreadyKnown(eventId, roomId)) { + Timber.i("Ignoring push, event already knwown") + } else { + Timber.v("Requesting background sync") + session.requireBackgroundSync() + } } - */ + } catch (e: Exception) { Timber.e(e, "## onMessageReceivedInternal() failed : " + e.message) } } + fun safeGetCurrentSession(): Session? { + try { + return Matrix.getInstance().currentSession + } catch (e: Throwable) { + Timber.e(e, "## Failed to get current session") + return null + } + } + // check if the event was not yet received // a previous catchup might have already retrieved the notified event private fun isEventAlreadyKnown(eventId: String?, roomId: String?): Boolean { if (null != eventId && null != roomId) { try { - /* TODO - val sessions = Matrix.getInstance(applicationContext).sessions - - if (null != sessions && !sessions.isEmpty()) { - for (session in sessions) { - if (session.dataHandler?.store?.isReady == true) { - session.dataHandler.store?.getEvent(eventId, roomId)?.let { - Timber.e("## isEventAlreadyKnown() : ignore the event " + eventId - + " in room " + roomId + " because it is already known") - return true - } - } - } - } - */ + val session = safeGetCurrentSession() ?: return false + val room = session.getRoom(roomId) ?: return false + return room.getTimeLineEvent(eventId) != null } catch (e: Exception) { - Timber.e(e, "## isEventAlreadyKnown() : failed to check if the event was already defined " + e.message) + Timber.e(e, "## isEventAlreadyKnown() : failed to check if the event was already defined") } } @@ -187,48 +209,44 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() { isPushGatewayEvent = true ) notificationDrawerManager.onNotifiableEventReceived(simpleNotifiableEvent) - notificationDrawerManager.refreshNotificationDrawer(null) + notificationDrawerManager.refreshNotificationDrawer() return } else { - val event = parseEvent(data) - if (event?.roomId == null) { - //unsupported event - Timber.e("Received an event with no room id") - return + val event = parseEvent(data) ?: return + + val notifiableEvent = notifiableEventResolver.resolveEvent(event, session) + + if (notifiableEvent == null) { + Timber.e("Unsupported notifiable event ${eventId}") + if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) { + Timber.e("--> ${event}") + } } else { - var notifiableEvent = notifiableEventResolver.resolveEvent(event, null, null /* TODO session.fulfillRule(event) */, session) - if (notifiableEvent == null) { - Timber.e("Unsupported notifiable event ${eventId}") - if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) { - Timber.e("--> ${event}") + if (notifiableEvent is NotifiableMessageEvent) { + if (TextUtils.isEmpty(notifiableEvent.senderName)) { + notifiableEvent.senderName = data["sender_display_name"] + ?: data["sender"] ?: "" } - } else { - - - if (notifiableEvent is NotifiableMessageEvent) { - if (TextUtils.isEmpty(notifiableEvent.senderName)) { - notifiableEvent.senderName = data["sender_display_name"] ?: data["sender"] ?: "" - } - if (TextUtils.isEmpty(notifiableEvent.roomName)) { - notifiableEvent.roomName = findRoomNameBestEffort(data, session) ?: "" - } + if (TextUtils.isEmpty(notifiableEvent.roomName)) { + notifiableEvent.roomName = findRoomNameBestEffort(data, session) ?: "" } - - notifiableEvent.isPushGatewayEvent = true - notifiableEvent.matrixID = session.sessionParams.credentials.userId - notificationDrawerManager.onNotifiableEventReceived(notifiableEvent) - notificationDrawerManager.refreshNotificationDrawer(null) } + + notifiableEvent.isPushGatewayEvent = true + notifiableEvent.matrixID = session.sessionParams.credentials.userId + notificationDrawerManager.onNotifiableEventReceived(notifiableEvent) + notificationDrawerManager.refreshNotificationDrawer() } } + } private fun findRoomNameBestEffort(data: Map, session: Session?): String? { - var roomName: String? = data["room_name"] + val roomName: String? = data["room_name"] val roomId = data["room_id"] if (null == roomName && null != roomId) { // Try to get the room name from our store @@ -263,7 +281,7 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() { // TODO content = data.getValue("content"), originServerTs = System.currentTimeMillis()) } catch (e: Exception) { - Timber.e(e, "buildEvent fails " + e.localizedMessage) + Timber.e(e, "buildEvent fails ") } return null diff --git a/vector/src/main/AndroidManifest.xml b/vector/src/main/AndroidManifest.xml index fdbbd370f0..aaae3edb6b 100644 --- a/vector/src/main/AndroidManifest.xml +++ b/vector/src/main/AndroidManifest.xml @@ -3,7 +3,6 @@ xmlns:tools="http://schemas.android.com/tools" package="im.vector.riotredesign"> - - @@ -58,17 +56,37 @@ android:label="@string/encryption_message_recovery" /> - + + + + + + + + + + + + + + + + - \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/VectorApplication.kt b/vector/src/main/java/im/vector/riotredesign/VectorApplication.kt index 2f2de8ed08..9bef712e0c 100644 --- a/vector/src/main/java/im/vector/riotredesign/VectorApplication.kt +++ b/vector/src/main/java/im/vector/riotredesign/VectorApplication.kt @@ -23,6 +23,10 @@ import android.os.Handler import android.os.HandlerThread import androidx.core.provider.FontRequest import androidx.core.provider.FontsContractCompat +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleObserver +import androidx.lifecycle.OnLifecycleEvent +import androidx.lifecycle.ProcessLifecycleOwner import androidx.multidex.MultiDex import com.airbnb.epoxy.EpoxyAsyncUtil import com.airbnb.epoxy.EpoxyController @@ -32,14 +36,21 @@ import com.github.piasy.biv.loader.glide.GlideImageLoader import com.jakewharton.threetenabp.AndroidThreeTen import im.vector.matrix.android.api.Matrix import im.vector.riotredesign.core.di.AppModule +import im.vector.riotredesign.core.services.AlarmSyncBroadcastReceiver import im.vector.riotredesign.features.configuration.VectorConfiguration import im.vector.riotredesign.features.crypto.keysbackup.KeysBackupModule import im.vector.riotredesign.features.home.HomeModule import im.vector.riotredesign.features.lifecycle.VectorActivityLifecycleCallbacks +import im.vector.riotredesign.features.notifications.NotificationDrawerManager +import im.vector.riotredesign.features.notifications.NotificationUtils +import im.vector.riotredesign.features.notifications.PushRuleTriggerListener import im.vector.riotredesign.features.rageshake.VectorFileLogger import im.vector.riotredesign.features.rageshake.VectorUncaughtExceptionHandler import im.vector.riotredesign.features.roomdirectory.RoomDirectoryModule +import im.vector.riotredesign.features.settings.PreferencesManager import im.vector.riotredesign.features.version.getVersion +import im.vector.riotredesign.push.fcm.FcmHelper +import org.koin.android.ext.android.get import org.koin.android.ext.android.inject import org.koin.log.EmptyLogger import org.koin.standalone.StandAloneContext.startKoin @@ -47,15 +58,20 @@ import timber.log.Timber import java.text.SimpleDateFormat import java.util.* - class VectorApplication : Application() { + lateinit var appContext: Context //font thread handler private var mFontThreadHandler: Handler? = null val vectorConfiguration: VectorConfiguration by inject() + private val notificationDrawerManager by inject() + +// var slowMode = false + + override fun onCreate() { super.onCreate() appContext = this @@ -91,10 +107,51 @@ class VectorApplication : Application() { R.array.com_google_android_gms_fonts_certs ) -// val efp = koin.koinContext.get() FontsContractCompat.requestFont(this, fontRequest, koin.koinContext.get(), getFontThreadHandler()) vectorConfiguration.initConfiguration() + + NotificationUtils.createNotificationChannels(applicationContext) + + ProcessLifecycleOwner.get().lifecycle.addObserver(object : LifecycleObserver { + + @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) + fun entersForeground() { + AlarmSyncBroadcastReceiver.cancelAlarm(appContext) + Matrix.getInstance().currentSession?.also { + it.stopAnyBackgroundSync() + } + } + + @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) + fun entersBackground() { + Timber.i("App entered background") // call persistInfo + + notificationDrawerManager.persistInfo() + + if (FcmHelper.isPushSupported()) { + //TODO FCM fallback + } else { + //TODO check if notifications are enabled for this device + //We need to use alarm in this mode + if (PreferencesManager.areNotificationEnabledForDevice(applicationContext)) { + if (Matrix.getInstance().currentSession != null) { + AlarmSyncBroadcastReceiver.scheduleAlarm(applicationContext, 4_000L) + Timber.i("Alarm scheduled to restart service") + } + } + } + } + + }) + + + Matrix.getInstance().currentSession?.let { + it.refreshPushers() + //bind to the sync service + get().startWithSession(it) + it.fetchPushRules() + } } private fun logInfo() { diff --git a/vector/src/main/java/im/vector/riotredesign/core/di/AppModule.kt b/vector/src/main/java/im/vector/riotredesign/core/di/AppModule.kt index 5b875fd1ca..85f0d3a584 100644 --- a/vector/src/main/java/im/vector/riotredesign/core/di/AppModule.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/di/AppModule.kt @@ -21,6 +21,8 @@ import android.content.Context.MODE_PRIVATE import im.vector.matrix.android.api.Matrix import im.vector.riotredesign.EmojiCompatFontProvider import im.vector.riotredesign.core.error.ErrorFormatter +import im.vector.riotredesign.core.pushers.PushersManager +import im.vector.riotredesign.core.resources.AppNameProvider import im.vector.riotredesign.core.resources.LocaleProvider import im.vector.riotredesign.core.resources.StringArrayProvider import im.vector.riotredesign.core.resources.StringProvider @@ -33,7 +35,10 @@ import im.vector.riotredesign.features.home.room.list.AlphabeticalRoomComparator import im.vector.riotredesign.features.home.room.list.ChronologicalRoomComparator import im.vector.riotredesign.features.navigation.DefaultNavigator import im.vector.riotredesign.features.navigation.Navigator +import im.vector.riotredesign.features.notifications.NotifiableEventResolver import im.vector.riotredesign.features.notifications.NotificationDrawerManager +import im.vector.riotredesign.features.notifications.OutdatedEventDetector +import im.vector.riotredesign.features.notifications.PushRuleTriggerListener import org.koin.dsl.module.module class AppModule(private val context: Context) { @@ -81,7 +86,19 @@ class AppModule(private val context: Context) { } single { - NotificationDrawerManager(context) + PushRuleTriggerListener(get(), get()) + } + + single { + OutdatedEventDetector() + } + + single { + NotificationDrawerManager(context, get()) + } + + single { + NotifiableEventResolver(get(), get()) } factory { @@ -103,5 +120,13 @@ class AppModule(private val context: Context) { single { EmojiCompatFontProvider() } + + single { + AppNameProvider(context) + } + + single { + PushersManager(get(), get(), get(), get()) + } } } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/core/platform/VectorPreferenceFragment.kt b/vector/src/main/java/im/vector/riotredesign/core/platform/VectorPreferenceFragment.kt index a915ea8aff..ddd908090b 100644 --- a/vector/src/main/java/im/vector/riotredesign/core/platform/VectorPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/platform/VectorPreferenceFragment.kt @@ -28,6 +28,8 @@ abstract class VectorPreferenceFragment : PreferenceFragmentCompat() { activity as VectorBaseActivity } + abstract var titleRes: Int + /* ========================================================================================== * Life cycle * ========================================================================================== */ @@ -36,6 +38,7 @@ abstract class VectorPreferenceFragment : PreferenceFragmentCompat() { override fun onResume() { super.onResume() + (activity as? VectorBaseActivity)?.supportActionBar?.setTitle(titleRes) Timber.v("onResume Fragment ${this.javaClass.simpleName}") } diff --git a/vector/src/main/java/im/vector/riotredesign/core/preference/BingRulePreference.kt b/vector/src/main/java/im/vector/riotredesign/core/preference/BingRulePreference.kt index bd10f70941..9a17390bb9 100755 --- a/vector/src/main/java/im/vector/riotredesign/core/preference/BingRulePreference.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/preference/BingRulePreference.kt @@ -25,7 +25,7 @@ import android.widget.TextView import androidx.preference.PreferenceViewHolder import im.vector.riotredesign.R -// TODO Replace by real Bingrule class +// TODO Replace by real Bingrule class, then delete class BingRule(rule: BingRule) { fun shouldNotNotify() = false fun shouldNotify() = false diff --git a/vector/src/main/java/im/vector/riotredesign/core/preference/UserAvatarPreference.kt b/vector/src/main/java/im/vector/riotredesign/core/preference/UserAvatarPreference.kt index f8391da85e..89eb654923 100755 --- a/vector/src/main/java/im/vector/riotredesign/core/preference/UserAvatarPreference.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/preference/UserAvatarPreference.kt @@ -39,7 +39,8 @@ open class UserAvatarPreference : Preference { init { widgetLayoutResource = R.layout.vector_settings_round_avatar - isIconSpaceReserved = false + // Set to false to remove the space when there is no icon + isIconSpaceReserved = true } override fun onBindViewHolder(holder: PreferenceViewHolder) { diff --git a/vector/src/main/java/im/vector/riotredesign/core/preference/VectorEditTextPreference.kt b/vector/src/main/java/im/vector/riotredesign/core/preference/VectorEditTextPreference.kt index 973857b1d9..2d7453eff5 100644 --- a/vector/src/main/java/im/vector/riotredesign/core/preference/VectorEditTextPreference.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/preference/VectorEditTextPreference.kt @@ -37,7 +37,8 @@ class VectorEditTextPreference : EditTextPreference { init { dialogLayoutResource = R.layout.dialog_preference_edit_text - isIconSpaceReserved = false + // Set to false to remove the space when there is no icon + isIconSpaceReserved = true } // No single line for title diff --git a/vector/src/main/java/im/vector/riotredesign/core/preference/VectorListPreference.kt b/vector/src/main/java/im/vector/riotredesign/core/preference/VectorListPreference.kt index 5ea5db3664..2eced2500d 100644 --- a/vector/src/main/java/im/vector/riotredesign/core/preference/VectorListPreference.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/preference/VectorListPreference.kt @@ -54,7 +54,8 @@ class VectorListPreference : ListPreference { init { widgetLayoutResource = R.layout.vector_settings_list_preference_with_warning - isIconSpaceReserved = false + // Set to false to remove the space when there is no icon + isIconSpaceReserved = true } override fun onBindViewHolder(holder: PreferenceViewHolder) { diff --git a/vector/src/main/java/im/vector/riotredesign/core/preference/VectorPreference.kt b/vector/src/main/java/im/vector/riotredesign/core/preference/VectorPreference.kt index b93b80153c..0bd5e7dbef 100755 --- a/vector/src/main/java/im/vector/riotredesign/core/preference/VectorPreference.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/preference/VectorPreference.kt @@ -75,7 +75,8 @@ open class VectorPreference : Preference { constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) init { - isIconSpaceReserved = false + // Set to false to remove the space when there is no icon + isIconSpaceReserved = true } var isHighlighted = false @@ -156,8 +157,4 @@ open class VectorPreference : Preference { } } } - - companion object { - private val LOG_TAG = VectorPreference::class.java.simpleName - } } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/core/preference/VectorPreferenceCategory.kt b/vector/src/main/java/im/vector/riotredesign/core/preference/VectorPreferenceCategory.kt index ebe49127d8..0b0b501649 100644 --- a/vector/src/main/java/im/vector/riotredesign/core/preference/VectorPreferenceCategory.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/preference/VectorPreferenceCategory.kt @@ -36,7 +36,8 @@ class VectorPreferenceCategory : PreferenceCategory { constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) init { - isIconSpaceReserved = false + // Set to false to remove the space when there is no icon + isIconSpaceReserved = true } override fun onBindViewHolder(holder: PreferenceViewHolder) { @@ -47,6 +48,8 @@ class VectorPreferenceCategory : PreferenceCategory { titleTextView?.setTypeface(null, Typeface.BOLD) // "isIconSpaceReserved = false" does not work for preference category, so remove the padding - (titleTextView?.parent as? ViewGroup)?.setPadding(0, 0, 0, 0) + if (!isIconSpaceReserved) { + (titleTextView?.parent as? ViewGroup)?.setPadding(0, 0, 0, 0) + } } } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/core/preference/VectorSwitchPreference.kt b/vector/src/main/java/im/vector/riotredesign/core/preference/VectorSwitchPreference.kt index 27af34551b..7693480c13 100644 --- a/vector/src/main/java/im/vector/riotredesign/core/preference/VectorSwitchPreference.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/preference/VectorSwitchPreference.kt @@ -37,7 +37,8 @@ class VectorSwitchPreference : SwitchPreference { constructor(context: Context) : super(context) init { - isIconSpaceReserved = false + // Set to false to remove the space when there is no icon + isIconSpaceReserved = true } override fun onBindViewHolder(holder: PreferenceViewHolder) { diff --git a/vector/src/main/java/im/vector/riotredesign/core/pushers/PushersManager.kt b/vector/src/main/java/im/vector/riotredesign/core/pushers/PushersManager.kt new file mode 100644 index 0000000000..1182efb104 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/core/pushers/PushersManager.kt @@ -0,0 +1,38 @@ +package im.vector.riotredesign.core.pushers + +import im.vector.matrix.android.api.MatrixCallback +import im.vector.matrix.android.api.session.Session +import im.vector.riotredesign.R +import im.vector.riotredesign.core.resources.AppNameProvider +import im.vector.riotredesign.core.resources.LocaleProvider +import im.vector.riotredesign.core.resources.StringProvider + +private const val DEFAULT_PUSHER_FILE_TAG = "mobile" + +class PushersManager( + private val currentSession: Session, + private val localeProvider: LocaleProvider, + private val stringProvider: StringProvider, + private val appNameProvider: AppNameProvider +) { + + fun registerPusherWithFcmKey(pushKey: String) { + var profileTag = DEFAULT_PUSHER_FILE_TAG + "_" + Math.abs(currentSession.sessionParams.credentials.userId.hashCode()) + + currentSession.addHttpPusher( + pushKey, + stringProvider.getString(R.string.pusher_app_id), + profileTag, + localeProvider.current().language, + appNameProvider.getAppName(), + currentSession.sessionParams.credentials.deviceId ?: "MOBILE", + stringProvider.getString(R.string.pusher_http_url), + false, + true + ) + } + + fun unregisterPusher(pushKey: String, callback: MatrixCallback) { + currentSession.removeHttpPusher(pushKey, stringProvider.getString(R.string.pusher_app_id),callback) + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/core/resources/AppNameProvider.kt b/vector/src/main/java/im/vector/riotredesign/core/resources/AppNameProvider.kt new file mode 100644 index 0000000000..4adfde792d --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/core/resources/AppNameProvider.kt @@ -0,0 +1,26 @@ +package im.vector.riotredesign.core.resources + +import android.content.Context +import timber.log.Timber + + +class AppNameProvider(private val context: Context) { + + fun getAppName(): String { + try { + val appPackageName = context.applicationContext.packageName + val pm = context.packageManager + val appInfo = pm.getApplicationInfo(appPackageName, 0) + var appName = pm.getApplicationLabel(appInfo).toString() + + // Use appPackageName instead of appName if appName contains any non-ASCII character + if (!appName.matches("\\A\\p{ASCII}*\\z".toRegex())) { + appName = appPackageName + } + return appName + } catch (e: Exception) { + Timber.e(e, "## AppNameProvider() : failed " + e.message) + return "RiotXAndroid" + } + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/core/resources/LocaleProvider.kt b/vector/src/main/java/im/vector/riotredesign/core/resources/LocaleProvider.kt index 991177a7e7..8393bc0960 100644 --- a/vector/src/main/java/im/vector/riotredesign/core/resources/LocaleProvider.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/resources/LocaleProvider.kt @@ -25,6 +25,4 @@ class LocaleProvider(private val resources: Resources) { fun current(): Locale { return ConfigurationCompat.getLocales(resources.configuration)[0] } - - } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/core/services/AlarmSyncBroadcastReceiver.kt b/vector/src/main/java/im/vector/riotredesign/core/services/AlarmSyncBroadcastReceiver.kt new file mode 100644 index 0000000000..f89f39e3ab --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/core/services/AlarmSyncBroadcastReceiver.kt @@ -0,0 +1,73 @@ +package im.vector.riotredesign.core.services + +import android.app.AlarmManager +import android.app.PendingIntent +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.os.Build +import android.os.PowerManager +import androidx.core.content.ContextCompat +import timber.log.Timber + +class AlarmSyncBroadcastReceiver : BroadcastReceiver() { + + + override fun onReceive(context: Context, intent: Intent) { + + //Aquire a lock to give enough time for the sync :/ + (context.getSystemService(Context.POWER_SERVICE) as PowerManager).run { + newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "riotx:fdroidSynclock").apply { + acquire((10_000).toLong()) + } + } + + + // This method is called when the BroadcastReceiver is receiving an Intent broadcast. + Timber.d("RestartBroadcastReceiver received intent") + Intent(context, VectorSyncService::class.java).also { + it.action = "SLOW" + context.startService(it) + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + ContextCompat.startForegroundService(context, intent) + } else { + context.startService(intent) + } + } catch (ex: Throwable) { + //TODO + Timber.e(ex) + } + } + + scheduleAlarm(context, 30_000L) + + Timber.i("Alarm scheduled to restart service") + } + + companion object { + const val REQUEST_CODE = 0 + + fun scheduleAlarm(context: Context, delay: Long) { + //Reschedule + val intent = Intent(context, AlarmSyncBroadcastReceiver::class.java) + val pIntent = PendingIntent.getBroadcast(context, AlarmSyncBroadcastReceiver.REQUEST_CODE, + intent, PendingIntent.FLAG_UPDATE_CURRENT) + val firstMillis = System.currentTimeMillis() + delay + val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, firstMillis, pIntent) + } else { + alarmMgr.set(AlarmManager.RTC_WAKEUP, firstMillis, pIntent) + } + } + + fun cancelAlarm(context: Context) { + val intent = Intent(context, AlarmSyncBroadcastReceiver::class.java) + val pIntent = PendingIntent.getBroadcast(context, AlarmSyncBroadcastReceiver.REQUEST_CODE, + intent, PendingIntent.FLAG_UPDATE_CURRENT) + val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager + alarmMgr.cancel(pIntent) + } + } +} diff --git a/vector/src/main/java/im/vector/riotredesign/core/services/EventStreamServiceX.kt b/vector/src/main/java/im/vector/riotredesign/core/services/EventStreamServiceX.kt deleted file mode 100644 index 108b27d5de..0000000000 --- a/vector/src/main/java/im/vector/riotredesign/core/services/EventStreamServiceX.kt +++ /dev/null @@ -1,582 +0,0 @@ -/* - * Copyright 2019 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package im.vector.riotredesign.core.services - -import android.content.Context -import android.content.Intent -import androidx.core.content.ContextCompat -import androidx.work.Constraints -import androidx.work.NetworkType -import androidx.work.OneTimeWorkRequestBuilder -import androidx.work.WorkManager -import im.vector.matrix.android.api.session.Session -import im.vector.matrix.android.api.session.events.model.Event -import im.vector.riotredesign.R -import im.vector.riotredesign.features.notifications.NotifiableEventResolver -import im.vector.riotredesign.features.notifications.NotificationUtils -import org.koin.android.ext.android.inject -import timber.log.Timber -import java.util.concurrent.TimeUnit - -/** - * A service in charge of controlling whether the event stream is running or not. - * - * It manages messages notifications displayed to the end user. - */ -class EventStreamServiceX : VectorService() { - - /** - * Managed session (no multi session for Riot) - */ - private val mSession by inject() - - /** - * Set to true to simulate a push immediately when service is destroyed - */ - private var mSimulatePushImmediate = false - - /** - * The current state. - */ - private var serviceState = ServiceState.INIT - set(newServiceState) { - Timber.i("setServiceState from $field to $newServiceState") - field = newServiceState - } - - /** - * Push manager - */ - // TODO private var mPushManager: PushManager? = null - - private var mNotifiableEventResolver: NotifiableEventResolver? = null - - /** - * Live events listener - */ - /* TODO - private val mEventsListener = object : MXEventListener() { - override fun onBingEvent(event: Event, roomState: RoomState, bingRule: BingRule) { - if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) { - Timber.i("%%%%%%%% MXEventListener: the event $event") - } - - Timber.i("prepareNotification : " + event.eventId + " in " + roomState.roomId) - val session = Matrix.getMXSession(applicationContext, event.matrixId) - - // invalid session ? - // should never happen. - // But it could be triggered because of multi accounts management. - // The dedicated account is removing but some pushes are still received. - if (null == session || !session.isAlive) { - Timber.i("prepareNotification : don't bing - no session") - return - } - - if (EventType.CALL_INVITE == event.getClearType()) { - handleCallInviteEvent(event) - return - } - - - val notifiableEvent = mNotifiableEventResolver!!.resolveEvent(event, roomState, bingRule, session) - if (notifiableEvent != null) { - VectorApp.getInstance().notificationDrawerManager.onNotifiableEventReceived(notifiableEvent) - } - } - - override fun onLiveEventsChunkProcessed(fromToken: String, toToken: String) { - Timber.i("%%%%%%%% MXEventListener: onLiveEventsChunkProcessed[$fromToken->$toToken]") - - VectorApp.getInstance().notificationDrawerManager.refreshNotificationDrawer(OutdatedEventDetector(this@EventStreamServiceX)) - - // do not suspend the application if there is some active calls - if (ServiceState.CATCHUP == serviceState) { - val hasActiveCalls = session?.mCallsManager?.hasActiveCalls() == true - - // if there are some active calls, the catchup should not be stopped. - // because an user could answer to a call from another device. - // there will no push because it is his own message. - // so, the client has no choice to catchup until the ring is shutdown - if (hasActiveCalls) { - Timber.i("onLiveEventsChunkProcessed : Catchup again because there are active calls") - catchup(false) - } else if (ServiceState.CATCHUP == serviceState) { - Timber.i("onLiveEventsChunkProcessed : no Active call") - CallsManager.getSharedInstance().checkDeadCalls() - stop() - } - } - } - } */ - - /** - * Service internal state - */ - private enum class ServiceState { - // Initial state - INIT, - // Service is started for a Catchup. Once the catchup is finished the service will be stopped - CATCHUP, - // Service is started, and session is monitored - STARTED - } - - override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - // Cancel any previous worker - cancelAnySimulatedPushSchedule() - - // no intent : restarted by Android - if (null == intent) { - // Cannot happen anymore - Timber.e("onStartCommand : null intent") - myStopSelf() - return START_NOT_STICKY - } - - val action = intent.action - - Timber.i("onStartCommand with action : $action (current state $serviceState)") - - // Manage foreground notification - when (action) { - ACTION_BOOT_COMPLETE, - ACTION_APPLICATION_UPGRADE, - ACTION_SIMULATED_PUSH_RECEIVED -> { - // Display foreground notification - Timber.i("startForeground") - val notification = NotificationUtils.buildForegroundServiceNotification(this, R.string.notification_sync_in_progress) - startForeground(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification) - } - ACTION_GO_TO_FOREGROUND -> { - // Stop foreground notification display - Timber.i("stopForeground") - stopForeground(true) - } - } - - if (null == mSession) { - Timber.e("onStartCommand : no sessions") - myStopSelf() - return START_NOT_STICKY - } - - when (action) { - ACTION_START, - ACTION_GO_TO_FOREGROUND -> - when (serviceState) { - ServiceState.INIT -> - start(false) - ServiceState.CATCHUP -> - // A push has been received before, just change state, to avoid stopping the service when catchup is over - serviceState = ServiceState.STARTED - ServiceState.STARTED -> { - // Nothing to do - } - } - ACTION_STOP, - ACTION_GO_TO_BACKGROUND, - ACTION_LOGOUT -> - stop() - ACTION_PUSH_RECEIVED, - ACTION_SIMULATED_PUSH_RECEIVED -> - when (serviceState) { - ServiceState.INIT -> - start(true) - ServiceState.CATCHUP -> - catchup(true) - ServiceState.STARTED -> - // Nothing to do - Unit - } - ACTION_PUSH_UPDATE -> pushStatusUpdate() - ACTION_BOOT_COMPLETE -> { - // No FCM only - mSimulatePushImmediate = true - stop() - } - ACTION_APPLICATION_UPGRADE -> { - // FDroid only - catchup(true) - } - else -> { - // Should not happen - } - } - - // We don't want the service to be restarted automatically by the System - return START_NOT_STICKY - } - - override fun onDestroy() { - super.onDestroy() - - // Schedule worker? - scheduleSimulatedPushIfNeeded() - } - - /** - * Tell the WorkManager to cancel any schedule of push simulation - */ - private fun cancelAnySimulatedPushSchedule() { - WorkManager.getInstance().cancelAllWorkByTag(PUSH_SIMULATOR_REQUEST_TAG) - } - - /** - * Configure the WorkManager to schedule a simulated push, if necessary - */ - private fun scheduleSimulatedPushIfNeeded() { - if (shouldISimulatePush()) { - val delay = if (mSimulatePushImmediate) 0 else 60_000 // TODO mPushManager?.backgroundSyncDelay ?: let { 60_000 } - Timber.i("## service is schedule to restart in $delay millis, if network is connected") - - val pushSimulatorRequest = OneTimeWorkRequestBuilder() - .setInitialDelay(delay.toLong(), TimeUnit.MILLISECONDS) - .setConstraints(Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build()) - .addTag(PUSH_SIMULATOR_REQUEST_TAG) - .build() - - WorkManager.getInstance().let { - // Cancel any previous worker - it.cancelAllWorkByTag(PUSH_SIMULATOR_REQUEST_TAG) - it.enqueue(pushSimulatorRequest) - } - } - } - - /** - * Start the even stream. - * - * @param session the session - */ - private fun startEventStream(session: Session) { - /* TODO - // resume if it was only suspended - if (null != session.currentSyncToken) { - session.resumeEventStream() - } else { - session.startEventStream(store?.eventStreamToken) - } - */ - } - - /** - * Monitor the provided session. - * - * @param session the session - */ - private fun monitorSession(session: Session) { - /* TODO - session.dataHandler.addListener(mEventsListener) - CallsManager.getSharedInstance().addSession(session) - - val store = session.dataHandler.store - - // the store is ready (no data loading in progress...) - if (store!!.isReady) { - startEventStream(session, store) - } else { - // wait that the store is ready before starting the events stream - store.addMXStoreListener(object : MXStoreListener() { - override fun onStoreReady(accountId: String) { - startEventStream(session, store) - - store.removeMXStoreListener(this) - } - - override fun onStoreCorrupted(accountId: String, description: String) { - // start a new initial sync - if (null == store.eventStreamToken) { - startEventStream(session, store) - } else { - // the data are out of sync - Matrix.getInstance(applicationContext)!!.reloadSessions(applicationContext) - } - - store.removeMXStoreListener(this) - } - - override fun onStoreOOM(accountId: String, description: String) { - val uiHandler = Handler(mainLooper) - - uiHandler.post { - Toast.makeText(applicationContext, "$accountId : $description", Toast.LENGTH_LONG).show() - Matrix.getInstance(applicationContext)!!.reloadSessions(applicationContext) - } - } - }) - - store.open() - } - */ - } - - /** - * internal start. - */ - private fun start(forPush: Boolean) { - val applicationContext = applicationContext - // TODO mPushManager = Matrix.getInstance(applicationContext)!!.pushManager - mNotifiableEventResolver = NotifiableEventResolver(applicationContext) - - monitorSession(mSession!!) - - serviceState = if (forPush) { - ServiceState.CATCHUP - } else { - ServiceState.STARTED - } - } - - /** - * internal stop. - */ - private fun stop() { - Timber.i("## stop(): the service is stopped") - - /* TODO - if (null != session && session!!.isAlive) { - session!!.stopEventStream() - session!!.dataHandler.removeListener(mEventsListener) - CallsManager.getSharedInstance().removeSession(session) - } - session = null - */ - - // Stop the service - myStopSelf() - } - - /** - * internal catchup method. - * - * @param checkState true to check if the current state allow to perform a catchup - */ - private fun catchup(checkState: Boolean) { - var canCatchup = true - - if (!checkState) { - Timber.i("catchup without checking serviceState ") - } else { - Timber.i("catchup with serviceState " + serviceState + " CurrentActivity ") // TODO + VectorApp.getCurrentActivity()) - - /* TODO - // the catchup should only be done - // 1- the serviceState is in catchup : the event stream might have gone to sleep between two catchups - // 2- the thread is suspended - // 3- the application has been launched by a push so there is no displayed activity - canCatchup = (serviceState == ServiceState.CATCHUP - //|| (serviceState == ServiceState.PAUSE) - || ServiceState.STARTED == serviceState && null == VectorApp.getCurrentActivity()) - */ - } - - if (canCatchup) { - if (mSession != null) { - // TODO session!!.catchupEventStream() - } else { - Timber.i("catchup no session") - } - - serviceState = ServiceState.CATCHUP - } else { - Timber.i("No catchup is triggered because there is already a running event thread") - } - } - - /** - * The push status has been updated (i.e disabled or enabled). - * TODO Useless now? - */ - private fun pushStatusUpdate() { - Timber.i("## pushStatusUpdate") - } - - /* ========================================================================================== - * Push simulator - * ========================================================================================== */ - - /** - * @return true if the FCM is disable or not setup, user allowed background sync, user wants notification - */ - private fun shouldISimulatePush(): Boolean { - return false - - /* TODO - - if (Matrix.getInstance(applicationContext)?.defaultSession == null) { - Timber.i("## shouldISimulatePush: NO: no session") - - return false - } - - mPushManager?.let { pushManager -> - if (pushManager.useFcm() - && !TextUtils.isEmpty(pushManager.currentRegistrationToken) - && pushManager.isServerRegistered) { - // FCM is ok - Timber.i("## shouldISimulatePush: NO: FCM is up") - return false - } - - if (!pushManager.isBackgroundSyncAllowed) { - // User has disabled background sync - Timber.i("## shouldISimulatePush: NO: background sync not allowed") - return false - } - - if (!pushManager.areDeviceNotificationsAllowed()) { - // User does not want notifications - Timber.i("## shouldISimulatePush: NO: user does not want notification") - return false - } - } - - // Lets simulate push - Timber.i("## shouldISimulatePush: YES") - return true - */ - } - - - //================================================================================ - // Call management - //================================================================================ - - private fun handleCallInviteEvent(event: Event) { - /* - TODO - val session = Matrix.getMXSession(applicationContext, event.matrixId) - - // invalid session ? - // should never happen. - // But it could be triggered because of multi accounts management. - // The dedicated account is removing but some pushes are still received. - if (null == session || !session.isAlive) { - Timber.v("prepareCallNotification : don't bing - no session") - return - } - - val room: Room? = session.dataHandler.getRoom(event.roomId) - - // invalid room ? - if (null == room) { - Timber.i("prepareCallNotification : don't bing - the room does not exist") - return - } - - var callId: String? = null - var isVideo = false - - try { - callId = event.contentAsJsonObject?.get("call_id")?.asString - - // Check if it is a video call - val offer = event.contentAsJsonObject?.get("offer")?.asJsonObject - val sdp = offer?.get("sdp") - val sdpValue = sdp?.asString - - isVideo = sdpValue?.contains("m=video") == true - } catch (e: Exception) { - Timber.e(e, "prepareNotification : getContentAsJsonObject") - } - - if (!TextUtils.isEmpty(callId)) { - CallService.onIncomingCall(this, - isVideo, - room.getRoomDisplayName(this), - room.roomId, - session.myUserId!!, - callId!!) - } - */ - } - - companion object { - private const val PUSH_SIMULATOR_REQUEST_TAG = "PUSH_SIMULATOR_REQUEST_TAG" - - private const val ACTION_START = "im.vector.riotredesign.core.services.EventStreamServiceX.START" - private const val ACTION_LOGOUT = "im.vector.riotredesign.core.services.EventStreamServiceX.LOGOUT" - private const val ACTION_GO_TO_FOREGROUND = "im.vector.riotredesign.core.services.EventStreamServiceX.GO_TO_FOREGROUND" - private const val ACTION_GO_TO_BACKGROUND = "im.vector.riotredesign.core.services.EventStreamServiceX.GO_TO_BACKGROUND" - private const val ACTION_PUSH_UPDATE = "im.vector.riotredesign.core.services.EventStreamServiceX.PUSH_UPDATE" - private const val ACTION_PUSH_RECEIVED = "im.vector.riotredesign.core.services.EventStreamServiceX.PUSH_RECEIVED" - private const val ACTION_SIMULATED_PUSH_RECEIVED = "im.vector.riotredesign.core.services.EventStreamServiceX.SIMULATED_PUSH_RECEIVED" - private const val ACTION_STOP = "im.vector.riotredesign.core.services.EventStreamServiceX.STOP" - private const val ACTION_BOOT_COMPLETE = "im.vector.riotredesign.core.services.EventStreamServiceX.BOOT_COMPLETE" - private const val ACTION_APPLICATION_UPGRADE = "im.vector.riotredesign.core.services.EventStreamServiceX.APPLICATION_UPGRADE" - - /* ========================================================================================== - * Events sent to the service - * ========================================================================================== */ - - fun onApplicationStarted(context: Context) { - sendAction(context, ACTION_START) - } - - fun onLogout(context: Context) { - sendAction(context, ACTION_LOGOUT) - } - - fun onAppGoingToForeground(context: Context) { - sendAction(context, ACTION_GO_TO_FOREGROUND) - } - - fun onAppGoingToBackground(context: Context) { - sendAction(context, ACTION_GO_TO_BACKGROUND) - } - - fun onPushUpdate(context: Context) { - sendAction(context, ACTION_PUSH_UPDATE) - } - - fun onPushReceived(context: Context) { - sendAction(context, ACTION_PUSH_RECEIVED) - } - - fun onSimulatedPushReceived(context: Context) { - sendAction(context, ACTION_SIMULATED_PUSH_RECEIVED, true) - } - - fun onApplicationStopped(context: Context) { - sendAction(context, ACTION_STOP) - } - - fun onBootComplete(context: Context) { - sendAction(context, ACTION_BOOT_COMPLETE, true) - } - - fun onApplicationUpgrade(context: Context) { - sendAction(context, ACTION_APPLICATION_UPGRADE, true) - } - - private fun sendAction(context: Context, action: String, foreground: Boolean = false) { - Timber.i("sendAction $action") - - val intent = Intent(context, EventStreamServiceX::class.java) - intent.action = action - - if (foreground) { - ContextCompat.startForegroundService(context, intent) - } else { - context.startService(intent) - } - } - } -} diff --git a/vector/src/main/java/im/vector/riotredesign/core/services/PushSimulatorWorker.kt b/vector/src/main/java/im/vector/riotredesign/core/services/PushSimulatorWorker.kt deleted file mode 100644 index d3f93f3280..0000000000 --- a/vector/src/main/java/im/vector/riotredesign/core/services/PushSimulatorWorker.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2019 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package im.vector.riotredesign.core.services - -import android.content.Context -import androidx.work.Worker -import androidx.work.WorkerParameters - -/** - * This class simulate push event when FCM is not working/disabled - */ -class PushSimulatorWorker(val context: Context, - workerParams: WorkerParameters) : Worker(context, workerParams) { - - override fun doWork(): Result { - // Simulate a Push - EventStreamServiceX.onSimulatedPushReceived(context) - - // Indicate whether the task finished successfully with the Result - return Result.success() - } -} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/core/services/VectorSyncService.kt b/vector/src/main/java/im/vector/riotredesign/core/services/VectorSyncService.kt new file mode 100644 index 0000000000..6f105c94fd --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/core/services/VectorSyncService.kt @@ -0,0 +1,72 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.riotredesign.core.services + +import android.app.NotificationManager +import android.content.Context +import android.content.Intent +import android.os.Build +import android.os.IBinder +import im.vector.matrix.android.internal.session.sync.job.SyncService +import im.vector.riotredesign.R +import im.vector.riotredesign.features.notifications.NotificationUtils +import timber.log.Timber + +class VectorSyncService : SyncService() { + + override fun onCreate() { + Timber.v("VectorSyncService - onCreate ") + super.onCreate() + } + + override fun onDestroy() { + Timber.v("VectorSyncService - onDestroy ") + removeForegroundNotif() + super.onDestroy() + } + + private fun removeForegroundNotif() { + val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + notificationManager.cancel(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE) + } + + /** + * Service is started only in fdroid mode when no FCM is available + * Otherwise it is bounded + */ + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + Timber.v("VectorSyncService - onStartCommand ") + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val notification = NotificationUtils.buildForegroundServiceNotification(applicationContext, R.string.notification_listening_for_events, false) + startForeground(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification) + } + return super.onStartCommand(intent, flags, startId) + } + + /** + * If the service is bounded and the service was previously started we can remove foreground notif + */ + override fun onBind(intent: Intent?): IBinder { + Timber.v("VectorSyncService - onBind ") + stopForeground(true) + return super.onBind(intent) + } + + override fun onUnbind(intent: Intent?): Boolean { + Timber.v("VectorSyncService - onUnbind ") + return super.onUnbind(intent) + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/core/ui/list/GenericFooterItem.kt b/vector/src/main/java/im/vector/riotredesign/core/ui/list/GenericFooterItem.kt new file mode 100644 index 0000000000..30cd69a4cd --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/core/ui/list/GenericFooterItem.kt @@ -0,0 +1,62 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.riotredesign.core.ui.list + +import android.widget.TextView +import com.airbnb.epoxy.EpoxyAttribute +import com.airbnb.epoxy.EpoxyModelClass +import im.vector.riotredesign.R +import im.vector.riotredesign.core.epoxy.VectorEpoxyHolder +import im.vector.riotredesign.core.epoxy.VectorEpoxyModel +import im.vector.riotredesign.core.extensions.setTextOrHide + +/** + * A generic list item. + * Displays an item with a title, and optional description. + * Can display an accessory on the right, that can be an image or an indeterminate progress. + * If provided with an action, will display a button at the bottom of the list item. + */ +@EpoxyModelClass(layout = R.layout.item_generic_footer) +abstract class GenericFooterItem : VectorEpoxyModel() { + + + @EpoxyAttribute + var text: String? = null + + @EpoxyAttribute + var style: GenericItem.STYLE = GenericItem.STYLE.NORMAL_TEXT + + @EpoxyAttribute + var itemClickAction: GenericItem.Action? = null + + override fun bind(holder: Holder) { + + holder.text.setTextOrHide(text) + when (style) { + GenericItem.STYLE.BIG_TEXT -> holder.text.textSize = 18f + GenericItem.STYLE.NORMAL_TEXT -> holder.text.textSize = 14f + } + + + holder.view.setOnClickListener { + itemClickAction?.perform?.run() + } + } + + class Holder : VectorEpoxyHolder() { + val text by bind(R.id.itemGenericFooterText) + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/MainActivity.kt b/vector/src/main/java/im/vector/riotredesign/features/MainActivity.kt index 2cc31dff1e..f22655a30d 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/MainActivity.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/MainActivity.kt @@ -59,17 +59,24 @@ class MainActivity : VectorBaseActivity() { } else { // Handle some wanted cleanup when { - clearCredentials -> session.signOut(object : MatrixCallback { - override fun onSuccess(data: Unit) { - Timber.w("SIGN_OUT: success, start app") - start() - } - }) - clearCache -> session.clearCache(object : MatrixCallback { - override fun onSuccess(data: Unit) { - start() - } - }) + clearCredentials -> { + session.signOut(object : MatrixCallback { + override fun onSuccess(data: Unit) { + Timber.w("SIGN_OUT: success, start app") + //TODO stop sync service + start() + } + }) + } + clearCache -> { + //TODO stop sync service + session.clearCache(object : MatrixCallback { + override fun onSuccess(data: Unit) { + //TODO start sync service + start() + } + }) + } else -> start() } } diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomeActivity.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomeActivity.kt index ffe59919c6..572d865e17 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/HomeActivity.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomeActivity.kt @@ -37,17 +37,19 @@ import im.vector.riotredesign.core.extensions.replaceFragment import im.vector.riotredesign.core.platform.OnBackPressed import im.vector.riotredesign.core.platform.ToolbarConfigurable import im.vector.riotredesign.core.platform.VectorBaseActivity +import im.vector.riotredesign.core.pushers.PushersManager import im.vector.riotredesign.features.crypto.keysrequest.KeyRequestHandler import im.vector.riotredesign.features.crypto.verification.IncomingVerificationRequestHandler +import im.vector.riotredesign.features.notifications.NotificationDrawerManager import im.vector.riotredesign.features.rageshake.BugReporter import im.vector.riotredesign.features.rageshake.VectorUncaughtExceptionHandler import im.vector.riotredesign.features.workers.signout.SignOutUiWorker +import im.vector.riotredesign.features.workers.signout.SignOutViewModel +import im.vector.riotredesign.push.fcm.FcmHelper import kotlinx.android.synthetic.main.activity_home.* import org.koin.android.ext.android.inject import org.koin.android.scope.ext.android.bindScope import org.koin.android.scope.ext.android.getOrCreateScope -import im.vector.riotredesign.features.workers.signout.SignOutViewModel - class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { @@ -60,6 +62,9 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { private val homeActivityViewModel: HomeActivityViewModel by viewModel() private lateinit var navigationViewModel: HomeNavigationViewModel private val homeNavigator by inject() + private val pushManager by inject() + + private val notificationDrawerManager by inject() // TODO Move this elsewhere private val incomingVerificationRequestHandler by inject() @@ -80,6 +85,7 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { super.onCreate(savedInstanceState) bindScope(getOrCreateScope(HomeModule.HOME_SCOPE)) homeNavigator.activity = this + FcmHelper.ensureFcmTokenIsRetrieved(this, pushManager) navigationViewModel = ViewModelProviders.of(this).get(HomeNavigationViewModel::class.java) @@ -111,6 +117,20 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { incomingVerificationRequestHandler.ensureStarted() keyRequestHandler.ensureStarted() + + if (intent.hasExtra(EXTRA_CLEAR_EXISTING_NOTIFICATION)) { + notificationDrawerManager.clearAllEvents() + intent.removeExtra(EXTRA_CLEAR_EXISTING_NOTIFICATION) + } + } + + override fun onNewIntent(intent: Intent?) { + super.onNewIntent(intent) + + if (intent?.hasExtra(EXTRA_CLEAR_EXISTING_NOTIFICATION) == true) { + notificationDrawerManager.clearAllEvents() + intent.removeExtra(EXTRA_CLEAR_EXISTING_NOTIFICATION) + } } override fun onDestroy() { @@ -146,7 +166,8 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.sliding_menu_sign_out -> { - SignOutUiWorker(this).perform(Matrix.getInstance().currentSession!!) + SignOutUiWorker(this, notificationDrawerManager) + .perform(Matrix.getInstance().currentSession!!) return true } } @@ -185,10 +206,14 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { companion object { - fun newIntent(context: Context): Intent { - return Intent(context, HomeActivity::class.java) - } + private const val EXTRA_CLEAR_EXISTING_NOTIFICATION = "EXTRA_CLEAR_EXISTING_NOTIFICATION" + fun newIntent(context: Context, clearNotification: Boolean = false): Intent { + return Intent(context, HomeActivity::class.java) + .apply { + putExtra(EXTRA_CLEAR_EXISTING_NOTIFICATION, clearNotification) + } + } } } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt index 9f5a748c6f..f8f6f297f1 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt @@ -43,7 +43,6 @@ import butterknife.BindView import com.airbnb.epoxy.EpoxyVisibilityTracker import com.airbnb.mvrx.args import com.airbnb.mvrx.fragmentViewModel -import com.airbnb.mvrx.withState import com.github.piasy.biv.BigImageViewer import com.github.piasy.biv.loader.ImageLoader import com.google.android.material.snackbar.Snackbar @@ -92,6 +91,7 @@ import im.vector.riotredesign.features.media.ImageContentRenderer import im.vector.riotredesign.features.media.ImageMediaViewerActivity import im.vector.riotredesign.features.media.VideoContentRenderer import im.vector.riotredesign.features.media.VideoMediaViewerActivity +import im.vector.riotredesign.features.notifications.NotificationDrawerManager import im.vector.riotredesign.features.reactions.EmojiReactionPickerActivity import im.vector.riotredesign.features.settings.PreferencesManager import kotlinx.android.parcel.Parcelize @@ -168,6 +168,8 @@ class RoomDetailFragment : private val autocompleteUserPresenter: AutocompleteUserPresenter by inject { parametersOf(this) } private val permalinkHandler: PermalinkHandler by inject() + private val notificationDrawerManager by inject() + private lateinit var scrollOnNewMessageCallback: ScrollOnNewMessageCallback private lateinit var scrollOnHighlightedEventCallback: ScrollOnHighlightedEventCallback @@ -277,6 +279,18 @@ class RoomDetailFragment : } } + override fun onResume() { + super.onResume() + + notificationDrawerManager.setCurrentRoom(roomDetailArgs.roomId) + } + + override fun onPause() { + super.onPause() + + notificationDrawerManager.setCurrentRoom(null) + } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (resultCode == RESULT_OK && data != null) { diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/format/NoticeEventFormatter.kt b/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/format/NoticeEventFormatter.kt index 595fa766c3..cf2dc4786b 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/format/NoticeEventFormatter.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/format/NoticeEventFormatter.kt @@ -46,6 +46,22 @@ class NoticeEventFormatter(private val stringProvider: StringProvider) { } } + fun format(event: Event, senderName: String?): CharSequence? { + return when (val type = event.getClearType()) { + EventType.STATE_ROOM_NAME -> formatRoomNameEvent(event, senderName) + EventType.STATE_ROOM_TOPIC -> formatRoomTopicEvent(event, senderName) + EventType.STATE_ROOM_MEMBER -> formatRoomMemberEvent(event, senderName) + EventType.STATE_HISTORY_VISIBILITY -> formatRoomHistoryVisibilityEvent(event, senderName) + EventType.CALL_INVITE, + EventType.CALL_HANGUP, + EventType.CALL_ANSWER -> formatCallEvent(event, senderName) + else -> { + Timber.v("Type $type not handled by this formatter") + null + } + } + } + private fun formatRoomNameEvent(event: Event, senderName: String?): CharSequence? { val content = event.getClearContent().toModel() ?: return null return if (!TextUtils.isEmpty(content.name)) { diff --git a/vector/src/main/java/im/vector/riotredesign/features/login/LoginActivity.kt b/vector/src/main/java/im/vector/riotredesign/features/login/LoginActivity.kt index ffadac1186..a3c4d426b1 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/login/LoginActivity.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/login/LoginActivity.kt @@ -32,10 +32,12 @@ import im.vector.riotredesign.R import im.vector.riotredesign.core.extensions.showPassword import im.vector.riotredesign.core.platform.VectorBaseActivity import im.vector.riotredesign.features.home.HomeActivity +import im.vector.riotredesign.features.notifications.PushRuleTriggerListener import io.reactivex.Observable import io.reactivex.functions.Function3 import io.reactivex.rxkotlin.subscribeBy import kotlinx.android.synthetic.main.activity_login.* +import org.koin.android.ext.android.get private const val DEFAULT_HOME_SERVER_URI = "https://matrix.org" private const val DEFAULT_IDENTITY_SERVER_URI = "https://vector.im" @@ -75,7 +77,8 @@ class LoginActivity : VectorBaseActivity() { data.open() data.setFilter(FilterService.FilterPreset.RiotFilter) data.startSync() - + get().startWithSession(data) + data.fetchPushRules() goToHome() } diff --git a/vector/src/main/java/im/vector/riotredesign/features/notifications/BitmapLoader.kt b/vector/src/main/java/im/vector/riotredesign/features/notifications/BitmapLoader.kt new file mode 100644 index 0000000000..9a52ea5dbc --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/notifications/BitmapLoader.kt @@ -0,0 +1,124 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.notifications + +import android.content.Context +import android.graphics.Bitmap +import android.os.Handler +import android.os.HandlerThread +import androidx.annotation.WorkerThread +import com.bumptech.glide.Glide +import com.bumptech.glide.load.DecodeFormat +import timber.log.Timber + +/** + * FIXME It works, but it does not refresh the notification, when it's already displayed + */ +class BitmapLoader(val context: Context, + val listener: BitmapLoaderListener) { + + /** + * Avatar Url -> Icon + */ + private val cache = HashMap() + + // URLs to load + private val toLoad = HashSet() + + // Black list of URLs (broken URL, etc.) + private val blacklist = HashSet() + + private var uiHandler = Handler() + + private val handlerThread: HandlerThread = HandlerThread("BitmapLoader", Thread.MIN_PRIORITY) + private var backgroundHandler: Handler + + init { + handlerThread.start() + backgroundHandler = Handler(handlerThread.looper) + } + + /** + * Get icon of a room. + * If already in cache, use it, else load it and call BitmapLoaderListener.onBitmapsLoaded() when ready + */ + fun getRoomBitmap(path: String?): Bitmap? { + if (path == null) { + return null + } + + synchronized(cache) { + if (cache[path] != null) { + return cache[path] + } + + // Add to the queue, if not blacklisted + if (!blacklist.contains(path)) { + if (toLoad.contains(path)) { + // Wait + } else { + toLoad.add(path) + + backgroundHandler.post { + loadRoomBitmap(path) + } + } + } + } + + return null + } + + @WorkerThread + private fun loadRoomBitmap(path: String) { + val bitmap = path.let { + try { + Glide.with(context) + .asBitmap() + .load(path) + .format(DecodeFormat.PREFER_ARGB_8888) + .submit() + .get() + } catch (e: Exception) { + Timber.e(e, "decodeFile failed") + null + } + } + + synchronized(cache) { + if (bitmap == null) { + // Add to the blacklist + blacklist.add(path) + } else { + cache[path] = bitmap + } + + toLoad.remove(path) + + if (toLoad.isEmpty()) { + uiHandler.post { + listener.onBitmapsLoaded() + } + } + } + } + + + interface BitmapLoaderListener { + fun onBitmapsLoaded() + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/notifications/NotifiableEventResolver.kt b/vector/src/main/java/im/vector/riotredesign/features/notifications/NotifiableEventResolver.kt index eb83b12a7e..ca2ec6142e 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/notifications/NotifiableEventResolver.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/notifications/NotifiableEventResolver.kt @@ -15,16 +15,21 @@ */ package im.vector.riotredesign.features.notifications -import android.content.Context +import androidx.core.app.NotificationCompat import im.vector.matrix.android.api.session.Session +import im.vector.matrix.android.api.session.content.ContentUrlResolver import im.vector.matrix.android.api.session.events.model.Event -import im.vector.riotredesign.core.preference.BingRule - -// TODO Remove -class RoomState { - -} - +import im.vector.matrix.android.api.session.events.model.EventType +import im.vector.matrix.android.api.session.events.model.toModel +import im.vector.matrix.android.api.session.room.model.Membership +import im.vector.matrix.android.api.session.room.model.RoomMember +import im.vector.matrix.android.api.session.room.model.message.MessageContent +import im.vector.matrix.android.api.session.room.timeline.TimelineEvent +import im.vector.riotredesign.BuildConfig +import im.vector.riotredesign.R +import im.vector.riotredesign.core.resources.StringProvider +import im.vector.riotredesign.features.home.room.detail.timeline.format.NoticeEventFormatter +import timber.log.Timber /** * The notifiable event resolver is able to create a NotifiableEvent (view model for notifications) from an sdk Event. @@ -32,146 +37,137 @@ class RoomState { * The NotifiableEventResolver is the only aware of session/store, the NotificationDrawerManager has no knowledge of that, * this pattern allow decoupling between the object responsible of displaying notifications and the matrix sdk. */ -class NotifiableEventResolver(val context: Context) { +class NotifiableEventResolver(private val stringProvider: StringProvider, + private val noticeEventFormatter: NoticeEventFormatter) { //private val eventDisplay = RiotEventDisplay(context) - fun resolveEvent(event: Event, roomState: RoomState?, bingRule: BingRule?, session: Session): NotifiableEvent? { - // TODO - return null - /* - val store = session.dataHandler.store - if (store == null) { - Log.e("## NotifiableEventResolver, unable to get store") - //TODO notify somehow that something did fail? - return null - } + fun resolveEvent(event: Event/*, roomState: RoomState?, bingRule: PushRule?*/, session: Session): NotifiableEvent? { + val roomID = event.roomId ?: return null + val eventId = event.eventId ?: return null + val timelineEvent = session.getRoom(roomID)?.getTimeLineEvent(eventId) ?: return null when (event.getClearType()) { - EventType.MESSAGE -> { - return resolveMessageEvent(event, bingRule, session, store) + EventType.MESSAGE -> { + return resolveMessageEvent(timelineEvent, session) } - EventType.ENCRYPTED -> { - val messageEvent = resolveMessageEvent(event, bingRule, session, store) + EventType.ENCRYPTED -> { + val messageEvent = resolveMessageEvent(timelineEvent, session) messageEvent?.lockScreenVisibility = NotificationCompat.VISIBILITY_PRIVATE return messageEvent } EventType.STATE_ROOM_MEMBER -> { - return resolveStateRoomEvent(event, bingRule, session, store) + return resolveStateRoomEvent(event, session) } - else -> { + else -> { //If the event can be displayed, display it as is - eventDisplay.getTextualDisplay(event, roomState)?.toString()?.let { body -> - return SimpleNotifiableEvent( - session.myUserId, - eventId = event.eventId, - noisy = bingRule?.notificationSound != null, - timestamp = event.originServerTs, - description = body, - soundName = bingRule?.notificationSound, - title = context.getString(R.string.notification_unknown_new_event), - type = event.getClearType()) - } + //TODO Better event text display + val bodyPreview = event.type + + return SimpleNotifiableEvent( + session.sessionParams.credentials.userId, + eventId = event.eventId!!, + noisy = false,//will be updated + timestamp = event.originServerTs ?: System.currentTimeMillis(), + description = bodyPreview, + title = stringProvider.getString(R.string.notification_unknown_new_event), + soundName = null, + type = event.type) + //Unsupported event Timber.w("NotifiableEventResolver Received an unsupported event matching a bing rule") return null } } - */ } - /* - private fun resolveMessageEvent(event: Event, bingRule: BingRule?, session: MXSession, store: IMXStore): NotifiableEvent? { - //If we are here, that means that the event should be notified to the user, we check now how it should be presented (sound) - val soundName = bingRule?.notificationSound - val noisy = bingRule?.notificationSound != null + + private fun resolveMessageEvent(event: TimelineEvent, session: Session): NotifiableEvent? { //The event only contains an eventId, and roomId (type is m.room.*) , we need to get the displayable content (names, avatar, text, etc...) - val room = store.getRoom(event.roomId /*roomID cannot be null (see Matrix SDK code)*/) + val room = session.getRoom(event.root.roomId!! /*roomID cannot be null*/) + if (room == null) { - Timber.e("## Unable to resolve room for eventId [${event.eventId}] and roomID [${event.roomId}]") + Timber.e("## Unable to resolve room for eventId [${event}]") // Ok room is not known in store, but we can still display something - val body = eventDisplay.getTextualDisplay(event, null)?.toString() - ?: context.getString(R.string.notification_unknown_new_event) - val roomName = context.getString(R.string.notification_unknown_room_name) - val senderDisplayName = event.sender ?: "" + val body = + event.annotations?.editSummary?.aggregatedContent?.toModel()?.body + ?: event.root.getClearContent().toModel()?.body + ?: stringProvider.getString(R.string.notification_unknown_new_event) + val roomName = stringProvider.getString(R.string.notification_unknown_room_name) + val senderDisplayName = event.senderName val notifiableEvent = NotifiableMessageEvent( - eventId = event.eventId, - timestamp = event.originServerTs, - noisy = noisy, + eventId = event.root.eventId!!, + timestamp = event.root.originServerTs ?: 0, + noisy = false,//will be updated senderName = senderDisplayName, - senderId = event.sender, + senderId = event.root.senderId, body = body, - roomId = event.roomId, + roomId = event.root.roomId!!, roomName = roomName) - notifiableEvent.matrixID = session.myUserId - notifiableEvent.soundName = soundName - + notifiableEvent.matrixID = session.sessionParams.credentials.userId return notifiableEvent } else { - - val body = eventDisplay.getTextualDisplay(event, room.state)?.toString() - ?: context.getString(R.string.notification_unknown_new_event) - val roomName = room.getRoomDisplayName(context) - val senderDisplayName = room.state.getMemberName(event.sender) ?: event.sender ?: "" + val body = event.annotations?.editSummary?.aggregatedContent?.toModel()?.body + ?: event.root.getClearContent().toModel()?.body + ?: stringProvider.getString(R.string.notification_unknown_new_event) + val roomName = room.roomSummary?.displayName ?: "" + val senderDisplayName = event.senderName ?: "" val notifiableEvent = NotifiableMessageEvent( - eventId = event.eventId, - timestamp = event.originServerTs, - noisy = noisy, + eventId = event.root.eventId!!, + timestamp = event.root.originServerTs ?: 0, + noisy = false,//will be updated senderName = senderDisplayName, - senderId = event.sender, + senderId = event.root.senderId, body = body, - roomId = event.roomId, + roomId = event.root.roomId!!, roomName = roomName, - roomIsDirect = room.isDirect) + roomIsDirect = room.roomSummary?.isDirect ?: false) - notifiableEvent.matrixID = session.myUserId - notifiableEvent.soundName = soundName + notifiableEvent.matrixID = session.sessionParams.credentials.userId + notifiableEvent.soundName = null + // Get the avatars URL + // TODO They will be not displayed the first time (known limitation) + notifiableEvent.roomAvatarPath = session.contentUrlResolver() + .resolveThumbnail(room.roomSummary?.avatarUrl, + 250, + 250, + ContentUrlResolver.ThumbnailMethod.SCALE) - val roomAvatarPath = session.mediaCache?.thumbnailCacheFile(room.avatarUrl, 50) - if (roomAvatarPath != null) { - notifiableEvent.roomAvatarPath = roomAvatarPath.path - } else { - // prepare for the next time - session.mediaCache?.loadAvatarThumbnail(session.homeServerConfig, ImageView(context), room.avatarUrl, 50) - } - - room.state.getMember(event.sender)?.avatarUrl?.let { - val size = context.resources.getDimensionPixelSize(R.dimen.profile_avatar_size) - val userAvatarUrlPath = session.mediaCache?.thumbnailCacheFile(it, size) - if (userAvatarUrlPath != null) { - notifiableEvent.senderAvatarPath = userAvatarUrlPath.path - } else { - // prepare for the next time - session.mediaCache?.loadAvatarThumbnail(session.homeServerConfig, ImageView(context), it, size) - } - } + notifiableEvent.senderAvatarPath = session.contentUrlResolver() + .resolveThumbnail(event.senderAvatar, + 250, + 250, + ContentUrlResolver.ThumbnailMethod.SCALE) return notifiableEvent } } - private fun resolveStateRoomEvent(event: Event, bingRule: BingRule?, session: MXSession, store: IMXStore): NotifiableEvent? { - if (RoomMember.MEMBERSHIP_INVITE == event.contentAsJsonObject?.getAsJsonPrimitive("membership")?.asString) { - val room = store.getRoom(event.roomId /*roomID cannot be null (see Matrix SDK code)*/) - val body = eventDisplay.getTextualDisplay(event, room.state)?.toString() - ?: context.getString(R.string.notification_new_invitation) + + private fun resolveStateRoomEvent(event: Event, session: Session): NotifiableEvent? { + val content = event.content?.toModel() ?: return null + val roomId = event.roomId ?: return null + val dName = event.senderId?.let { session.getUser(it)?.displayName } + if (Membership.INVITE == content.membership) { + val body = noticeEventFormatter.format(event, dName) + ?: stringProvider.getString(R.string.notification_new_invitation) return InviteNotifiableEvent( - session.myUserId, - eventId = event.eventId, - roomId = event.roomId, - timestamp = event.originServerTs, - noisy = bingRule?.notificationSound != null, - title = context.getString(R.string.notification_new_invitation), - description = body, - soundName = bingRule?.notificationSound, + session.sessionParams.credentials.userId, + eventId = event.eventId!!, + roomId = roomId, + timestamp = event.originServerTs ?: 0, + noisy = false,//will be set later + title = stringProvider.getString(R.string.notification_new_invitation), + description = body.toString(), + soundName = null, //will be set later type = event.getClearType(), isPushGatewayEvent = false) } else { @@ -182,6 +178,6 @@ class NotifiableEventResolver(val context: Context) { //TODO generic handling? } return null - } */ + } } diff --git a/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationAction.kt b/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationAction.kt new file mode 100644 index 0000000000..a1eac94610 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationAction.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.riotredesign.features.notifications + +import im.vector.matrix.android.api.pushrules.Action + +data class NotificationAction( + val shouldNotify: Boolean, + val highlight: Boolean = false, + val soundName: String? = null +) { + companion object { + fun extractFrom(ruleActions: List): NotificationAction { + var shouldNotify = false + var highlight = false + var sound: String? = null + ruleActions.forEach { + if (it.type == Action.Type.NOTIFY) shouldNotify = true + if (it.type == Action.Type.DONT_NOTIFY) shouldNotify = false + if (it.type == Action.Type.SET_TWEAK) { + if (it.tweak_action == "highlight") highlight = it.boolValue ?: false + if (it.tweak_action == "sound") sound = it.stringValue + } + } + return NotificationAction(shouldNotify, highlight, sound) + } + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationBroadcastReceiver.kt b/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationBroadcastReceiver.kt index 36692b7def..501e8e499f 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationBroadcastReceiver.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationBroadcastReceiver.kt @@ -20,11 +20,15 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import androidx.core.app.RemoteInput +import im.vector.matrix.android.api.Matrix +import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.room.Room +import im.vector.riotredesign.R import org.koin.standalone.KoinComponent import org.koin.standalone.inject import timber.log.Timber +import java.util.* /** * Receives actions broadcast by notification (on click, on dismiss, inline replies, etc.) @@ -36,18 +40,18 @@ class NotificationBroadcastReceiver : BroadcastReceiver(), KoinComponent { override fun onReceive(context: Context?, intent: Intent?) { if (intent == null || context == null) return - Timber.v("ReplyNotificationBroadcastReceiver received : $intent") + Timber.v("NotificationBroadcastReceiver received : $intent") when (intent.action) { - NotificationUtils.SMART_REPLY_ACTION -> + NotificationUtils.SMART_REPLY_ACTION -> handleSmartReply(intent, context) NotificationUtils.DISMISS_ROOM_NOTIF_ACTION -> intent.getStringExtra(KEY_ROOM_ID)?.let { notificationDrawerManager.clearMessageEventOfRoom(it) } - NotificationUtils.DISMISS_SUMMARY_ACTION -> + NotificationUtils.DISMISS_SUMMARY_ACTION -> notificationDrawerManager.clearAllEvents() - NotificationUtils.MARK_ROOM_READ_ACTION -> + NotificationUtils.MARK_ROOM_READ_ACTION -> intent.getStringExtra(KEY_ROOM_ID)?.let { notificationDrawerManager.clearMessageEventOfRoom(it) handleMarkAsRead(context, it) @@ -56,67 +60,61 @@ class NotificationBroadcastReceiver : BroadcastReceiver(), KoinComponent { } private fun handleMarkAsRead(context: Context, roomId: String) { - /* - TODO - Matrix.getInstance(context)?.defaultSession?.let { session -> - session.dataHandler - ?.getRoom(roomId) - ?.markAllAsRead(object : SimpleApiCallback() { - override fun onSuccess(void: Void?) { - // Ignore - } - }) + Matrix.getInstance().currentSession?.let { session -> + session.getRoom(roomId) + ?.markAllAsRead(object : MatrixCallback {}) } - */ } private fun handleSmartReply(intent: Intent, context: Context) { - /* - TODO val message = getReplyMessage(intent) val roomId = intent.getStringExtra(KEY_ROOM_ID) - if (TextUtils.isEmpty(message) || TextUtils.isEmpty(roomId)) { + if (message.isNullOrBlank() || roomId.isBlank()) { //ignore this event //Can this happen? should we update notification? return } val matrixId = intent.getStringExtra(EXTRA_MATRIX_ID) - Matrix.getInstance(context)?.getSession(matrixId)?.let { session -> - session.dataHandler?.getRoom(roomId)?.let { room -> - sendMatrixEvent(message!!, session, roomId!!, room, context) + Matrix.getInstance().currentSession?.let { session -> + session.getRoom(roomId)?.let { room -> + sendMatrixEvent(message, session, room, context) } } - */ } - private fun sendMatrixEvent(message: String, session: Session, roomId: String, room: Room, context: Context?) { - /* - TODO + private fun sendMatrixEvent(message: String, session: Session, room: Room, context: Context?) { - val mxMessage = Message() - mxMessage.msgtype = Message.MSGTYPE_TEXT - mxMessage.body = message + room.sendTextMessage(message) + + // Create a new event to be displayed in the notification drawer, right now + + val notifiableMessageEvent = NotifiableMessageEvent( + // Generate a Fake event id + UUID.randomUUID().toString(), + false, + System.currentTimeMillis(), + session.getUser(session.sessionParams.credentials.userId)?.displayName + ?: context?.getString(R.string.notification_sender_me), + session.sessionParams.credentials.userId, + message, + room.roomId, + room.roomSummary?.displayName ?: room.roomId, + room.roomSummary?.isDirect == true + ) + notifiableMessageEvent.outGoingMessage = true + + notificationDrawerManager.onNotifiableEventReceived(notifiableMessageEvent) + notificationDrawerManager.refreshNotificationDrawer() + + /* + // TODO Error cannot be managed the same way than in Riot val event = Event(mxMessage, session.credentials.userId, roomId) room.storeOutgoingEvent(event) room.sendEvent(event, object : MatrixCallback { override fun onSuccess(info: Void?) { Timber.v("Send message : onSuccess ") - val notifiableMessageEvent = NotifiableMessageEvent( - event.eventId, - false, - System.currentTimeMillis(), - session.myUser?.displayname - ?: context?.getString(R.string.notification_sender_me), - session.myUserId, - message, - roomId, - room.getRoomDisplayName(context), - room.isDirect) - notifiableMessageEvent.outGoingMessage = true - VectorApp.getInstance().notificationDrawerManager.onNotifiableEventReceived(notifiableMessageEvent) - VectorApp.getInstance().notificationDrawerManager.refreshNotificationDrawer(null) } override fun onNetworkError(e: Exception) { diff --git a/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationDrawerManager.kt b/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationDrawerManager.kt index ce207397c2..67c4aa85ae 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationDrawerManager.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationDrawerManager.kt @@ -18,14 +18,14 @@ package im.vector.riotredesign.features.notifications import android.app.Notification import android.content.Context import android.graphics.Bitmap -import android.graphics.BitmapFactory -import android.text.TextUtils import androidx.core.app.NotificationCompat import androidx.core.app.Person -import im.vector.matrix.android.api.session.Session +import im.vector.matrix.android.api.Matrix +import im.vector.matrix.android.api.session.content.ContentUrlResolver import im.vector.riotredesign.BuildConfig import im.vector.riotredesign.R import im.vector.riotredesign.core.utils.SecretStoringUtils +import im.vector.riotredesign.features.settings.PreferencesManager import timber.log.Timber import java.io.File import java.io.FileInputStream @@ -36,14 +36,14 @@ import java.io.FileOutputStream * organise them in order to display them in the notification drawer. * Events can be grouped into the same notification, old (already read) events can be removed to do some cleaning. */ -class NotificationDrawerManager(val context: Context) { +class NotificationDrawerManager(val context: Context, + private val outdatedDetector: OutdatedEventDetector?) { + //The first time the notification drawer is refreshed, we force re-render of all notifications private var firstTime = true private var eventList = loadEventInfo() - private var myUserDisplayName: String = "" - private var myUserAvatarUrl: String = "" private val avatarSize = context.resources.getDimensionPixelSize(R.dimen.profile_avatar_size) @@ -53,31 +53,17 @@ class NotificationDrawerManager(val context: Context) { object : IconLoader.IconLoaderListener { override fun onIconsLoaded() { // Force refresh - refreshNotificationDrawer(null) + refreshNotificationDrawer() } }) - /** - * No multi session support for now - */ - private fun initWithSession(session: Session?) { - session?.let { - /* - myUserDisplayName = it.myUser?.displayname ?: it.myUserId - - // User Avatar - it.myUser?.avatarUrl?.let { avatarUrl -> - val userAvatarUrlPath = it.mediaCache?.thumbnailCacheFile(avatarUrl, avatarSize) - if (userAvatarUrlPath != null) { - myUserAvatarUrl = userAvatarUrlPath.path - } else { - // prepare for the next time - session.mediaCache?.loadAvatarThumbnail(session.homeServerConfig, ImageView(context), avatarUrl, avatarSize) + private var bitmapLoader = BitmapLoader(context, + object : BitmapLoader.BitmapLoaderListener { + override fun onBitmapsLoaded() { + // Force refresh + refreshNotificationDrawer() } - } - */ - } - } + }) /** Should be called as soon as a new event is ready to be displayed. @@ -86,6 +72,10 @@ class NotificationDrawerManager(val context: Context) { Events might be grouped and there might not be one notification per event! */ fun onNotifiableEventReceived(notifiableEvent: NotifiableEvent) { + if (!PreferencesManager.areNotificationEnabledForDevice(context)) { + Timber.i("Notification are disabled for this device") + return + } //If we support multi session, event list should be per userId //Currently only manage single session if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) { @@ -112,7 +102,6 @@ class NotificationDrawerManager(val context: Context) { } else { eventList.add(notifiableEvent) } - } } @@ -123,7 +112,7 @@ class NotificationDrawerManager(val context: Context) { synchronized(eventList) { eventList.clear() } - refreshNotificationDrawer(null) + refreshNotificationDrawer() } /** Clear all known message events for this room and refresh the notification drawer */ @@ -139,12 +128,12 @@ class NotificationDrawerManager(val context: Context) { } NotificationUtils.cancelNotificationMessage(context, roomId, ROOM_MESSAGES_NOTIFICATION_ID) } - refreshNotificationDrawer(null) + refreshNotificationDrawer() } /** - Should be called when the application is currently opened and showing timeline for the given roomId. - Used to ignore events related to that room (no need to display notification) and clean any existing notification on this room. + * Should be called when the application is currently opened and showing timeline for the given roomId. + * Used to ignore events related to that room (no need to display notification) and clean any existing notification on this room. */ fun setCurrentRoom(roomId: String?) { var hasChanged: Boolean @@ -177,17 +166,12 @@ class NotificationDrawerManager(val context: Context) { } - fun refreshNotificationDrawer(outdatedDetector: OutdatedEventDetector?) { - if (myUserDisplayName.isBlank()) { - // TODO - // initWithSession(Matrix.getInstance(context).defaultSession) - } - - if (myUserDisplayName.isBlank()) { - // Should not happen, but myUserDisplayName cannot be blank if used to create a Person - return - } + fun refreshNotificationDrawer() { + val session = Matrix.getInstance().currentSession ?: return + val user = session.getUser(session.sessionParams.credentials.userId) + val myUserDisplayName = user?.displayName ?: "" + val myUserAvatarUrl = session.contentUrlResolver().resolveThumbnail(user?.avatarUrl, avatarSize, avatarSize, ContentUrlResolver.ThumbnailMethod.SCALE) synchronized(eventList) { Timber.v("%%%%%%%% REFRESH NOTIFICATION DRAWER ") @@ -238,22 +222,23 @@ class NotificationDrawerManager(val context: Context) { continue } - val roomGroup = RoomEventGroupInfo(roomId) - roomGroup.hasNewEvent = false - roomGroup.shouldBing = false - roomGroup.isDirect = events[0].roomIsDirect val roomName = events[0].roomName ?: events[0].senderName ?: "" + + val roomEventGroupInfo = RoomEventGroupInfo( + roomId = roomId, + isDirect = events[0].roomIsDirect, + roomDisplayName = roomName) + val style = NotificationCompat.MessagingStyle(Person.Builder() .setName(myUserDisplayName) .setIcon(iconLoader.getUserIcon(myUserAvatarUrl)) .setKey(events[0].matrixID) .build()) - roomGroup.roomDisplayName = roomName - style.isGroupConversation = !roomGroup.isDirect + style.isGroupConversation = !roomEventGroupInfo.isDirect - if (!roomGroup.isDirect) { - style.conversationTitle = roomName + if (!roomEventGroupInfo.isDirect) { + style.conversationTitle = roomEventGroupInfo.roomDisplayName } val largeBitmap = getRoomBitmap(events) @@ -262,10 +247,10 @@ class NotificationDrawerManager(val context: Context) { for (event in events) { //if all events in this room have already been displayed there is no need to update it if (!event.hasBeenDisplayed) { - roomGroup.shouldBing = roomGroup.shouldBing || event.noisy - roomGroup.customSound = event.soundName + roomEventGroupInfo.shouldBing = roomEventGroupInfo.shouldBing || event.noisy + roomEventGroupInfo.customSound = event.soundName } - roomGroup.hasNewEvent = roomGroup.hasNewEvent || !event.hasBeenDisplayed + roomEventGroupInfo.hasNewEvent = roomEventGroupInfo.hasNewEvent || !event.hasBeenDisplayed val senderPerson = Person.Builder() .setName(event.senderName) @@ -275,7 +260,7 @@ class NotificationDrawerManager(val context: Context) { if (event.outGoingMessage && event.outGoingMessageFailed) { style.addMessage(context.getString(R.string.notification_inline_reply_failed), event.timestamp, senderPerson) - roomGroup.hasSmartReplyError = true + roomEventGroupInfo.hasSmartReplyError = true } else { style.addMessage(event.body, event.timestamp, senderPerson) } @@ -296,7 +281,7 @@ class NotificationDrawerManager(val context: Context) { summaryInboxStyle.addLine(roomName) } - if (firstTime || roomGroup.hasNewEvent) { + if (firstTime || roomEventGroupInfo.hasNewEvent) { //Should update displayed notification Timber.v("%%%%%%%% REFRESH NOTIFICATION DRAWER $roomId need refresh") val lastMessageTimestamp = events.last().timestamp @@ -305,14 +290,14 @@ class NotificationDrawerManager(val context: Context) { globalLastMessageTimestamp = lastMessageTimestamp } - NotificationUtils.buildMessagesListNotification(context, style, roomGroup, largeBitmap, lastMessageTimestamp, myUserDisplayName) + NotificationUtils.buildMessagesListNotification(context, style, roomEventGroupInfo, largeBitmap, lastMessageTimestamp, myUserDisplayName) ?.let { //is there an id for this room? notifications.add(it) NotificationUtils.showNotificationMessage(context, roomId, ROOM_MESSAGES_NOTIFICATION_ID, it) } hasNewEvent = true - summaryIsNoisy = summaryIsNoisy || roomGroup.shouldBing + summaryIsNoisy = summaryIsNoisy || roomEventGroupInfo.shouldBing } else { Timber.v("%%%%%%%% REFRESH NOTIFICATION DRAWER $roomId is up to date") } @@ -393,19 +378,10 @@ class NotificationDrawerManager(val context: Context) { if (events.isEmpty()) return null //Use the last event (most recent?) - val roomAvatarPath = events[events.size - 1].roomAvatarPath - ?: events[events.size - 1].senderAvatarPath - if (!TextUtils.isEmpty(roomAvatarPath)) { - val options = BitmapFactory.Options() - options.inPreferredConfig = Bitmap.Config.ARGB_8888 - try { - return BitmapFactory.decodeFile(roomAvatarPath, options) - } catch (oom: OutOfMemoryError) { - Timber.e(oom, "decodeFile failed with an oom") - } + val roomAvatarPath = events.last().roomAvatarPath + ?: events.last().senderAvatarPath - } - return null + return bitmapLoader.getRoomBitmap(roomAvatarPath) } private fun shouldIgnoreMessageEventInRoom(roomId: String?): Boolean { diff --git a/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationUtils.kt b/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationUtils.kt index 9596fdad42..67e30c57da 100755 --- a/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationUtils.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/notifications/NotificationUtils.kt @@ -39,6 +39,8 @@ import im.vector.riotredesign.BuildConfig import im.vector.riotredesign.R import im.vector.riotredesign.core.utils.startNotificationChannelSettingsIntent import im.vector.riotredesign.features.home.HomeActivity +import im.vector.riotredesign.features.home.room.detail.RoomDetailActivity +import im.vector.riotredesign.features.home.room.detail.RoomDetailArgs import im.vector.riotredesign.features.settings.PreferencesManager import timber.log.Timber import java.util.* @@ -180,7 +182,7 @@ object NotificationUtils { * @return the polling thread listener notification */ @SuppressLint("NewApi") - fun buildForegroundServiceNotification(context: Context, @StringRes subTitleResId: Int): Notification { + fun buildForegroundServiceNotification(context: Context, @StringRes subTitleResId: Int, withProgress: Boolean = true): Notification { // build the pending intent go to the home screen if this is clicked. val i = Intent(context, HomeActivity::class.java) i.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP @@ -190,16 +192,21 @@ object NotificationUtils { val builder = NotificationCompat.Builder(context, LISTENING_FOR_EVENTS_NOTIFICATION_CHANNEL_ID) .setContentTitle(context.getString(subTitleResId)) - .setCategory(NotificationCompat.CATEGORY_PROGRESS) - .setSmallIcon(R.drawable.logo_transparent) - .setProgress(0, 0, true) + .setSmallIcon(R.drawable.sync) + .setCategory(NotificationCompat.CATEGORY_SERVICE) .setColor(accentColor) .setContentIntent(pi) + .apply { + if (withProgress) { + setProgress(0, 0, true) + } + } - // hide the notification from the status bar - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - builder.priority = NotificationCompat.PRIORITY_MIN - } + // PRIORITY_MIN should not be used with Service#startForeground(int, Notification) + builder.priority = NotificationCompat.PRIORITY_LOW +// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { +// builder.priority = NotificationCompat.PRIORITY_MIN +// } val notification = builder.build() @@ -220,7 +227,7 @@ object NotificationUtils { PendingIntent::class.java) deprecatedMethod.invoke(notification, context, context.getString(R.string.app_name), context.getString(subTitleResId), pi) } catch (ex: Exception) { - Timber.e(ex, "## buildNotification(): Exception - setLatestEventInfo() Msg=" + ex.message) + Timber.e(ex, "## buildNotification(): Exception - setLatestEventInfo() Msg=") } } @@ -540,27 +547,21 @@ object NotificationUtils { } private fun buildOpenRoomIntent(context: Context, roomId: String): PendingIntent? { - // TODO - return null - /* - val roomIntentTap = Intent(context, VectorRoomActivity::class.java) - roomIntentTap.putExtra(VectorRoomActivity.EXTRA_ROOM_ID, roomId) + val roomIntentTap = RoomDetailActivity.newIntent(context, RoomDetailArgs(roomId)) roomIntentTap.action = TAP_TO_VIEW_ACTION //pending intent get reused by system, this will mess up the extra params, so put unique info to avoid that roomIntentTap.data = Uri.parse("foobar://openRoom?$roomId") // Recreate the back stack return TaskStackBuilder.create(context) - .addNextIntentWithParentStack(Intent(context, VectorHomeActivity::class.java)) + .addNextIntentWithParentStack(Intent(context, HomeActivity::class.java)) .addNextIntent(roomIntentTap) .getPendingIntent(System.currentTimeMillis().toInt(), PendingIntent.FLAG_UPDATE_CURRENT) - */ } private fun buildOpenHomePendingIntentForSummary(context: Context): PendingIntent { - val intent = Intent(context, HomeActivity::class.java) + val intent = HomeActivity.newIntent(context, clearNotification = true) intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP - // TODO intent.putExtra(VectorHomeActivity.EXTRA_CLEAR_EXISTING_NOTIFICATION, true) intent.data = Uri.parse("foobar://tapSummary") return PendingIntent.getActivity(context, Random().nextInt(1000), intent, PendingIntent.FLAG_UPDATE_CURRENT) } diff --git a/vector/src/main/java/im/vector/riotredesign/features/notifications/OutdatedEventDetector.kt b/vector/src/main/java/im/vector/riotredesign/features/notifications/OutdatedEventDetector.kt index 5ee0cccdd9..a93f5dfedd 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/notifications/OutdatedEventDetector.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/notifications/OutdatedEventDetector.kt @@ -15,9 +15,9 @@ */ package im.vector.riotredesign.features.notifications -import android.content.Context +import im.vector.matrix.android.api.Matrix -class OutdatedEventDetector(val context: Context) { +class OutdatedEventDetector { /** * Returns true if the given event is outdated. @@ -28,20 +28,9 @@ class OutdatedEventDetector(val context: Context) { if (notifiableEvent is NotifiableMessageEvent) { val eventID = notifiableEvent.eventId val roomID = notifiableEvent.roomId - /* - TODO - Matrix.getMXSession(context.applicationContext, notifiableEvent.matrixID)?.let { session -> - //find the room - if (session.isAlive) { - session.dataHandler.getRoom(roomID)?.let { room -> - if (room.isEventRead(eventID)) { - Timber.v("Notifiable Event $eventID is read, and should be removed") - return true - } - } - } - } - */ + val session = Matrix.getInstance().currentSession ?: return false + val room = session.getRoom(roomID) ?: return false + return room.isEventRead(eventID) } return false } diff --git a/vector/src/main/java/im/vector/riotredesign/features/notifications/PushRuleTriggerListener.kt b/vector/src/main/java/im/vector/riotredesign/features/notifications/PushRuleTriggerListener.kt new file mode 100644 index 0000000000..f3aed6ad05 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/notifications/PushRuleTriggerListener.kt @@ -0,0 +1,74 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.notifications + +import im.vector.matrix.android.api.pushrules.Action +import im.vector.matrix.android.api.pushrules.PushRuleService +import im.vector.matrix.android.api.session.Session +import im.vector.matrix.android.api.session.events.model.Event +import timber.log.Timber + + +class PushRuleTriggerListener( + private val resolver: NotifiableEventResolver, + private val drawerManager: NotificationDrawerManager +) : PushRuleService.PushRuleListener { + + + var session: Session? = null + + override fun onMatchRule(event: Event, actions: List) { + Timber.v("Push rule match for event ${event.eventId}") + if (session == null) { + Timber.e("Called without active session") + return + } + val notificationAction = NotificationAction.extractFrom(actions) + if (notificationAction.shouldNotify) { + val resolveEvent = resolver.resolveEvent(event, session!!) + if (resolveEvent == null) { + Timber.v("## Failed to resolve event") + //TODO + } else { + resolveEvent.noisy = !notificationAction.soundName.isNullOrBlank() + Timber.v("New event to notify $resolveEvent tweaks:$notificationAction") + drawerManager.onNotifiableEventReceived(resolveEvent) + } + } else { + Timber.v("Matched push rule is set to not notify") + } + } + + override fun batchFinish() { + drawerManager.refreshNotificationDrawer() + } + + fun startWithSession(session: Session) { + if (this.session != null) { + stop() + } + this.session = session + session.addPushRuleListener(this) + } + + fun stop() { + session?.removePushRuleListener(this) + session = null + drawerManager.clearAllEvents() + } + +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/notifications/RoomEventGroupInfo.kt b/vector/src/main/java/im/vector/riotredesign/features/notifications/RoomEventGroupInfo.kt index e1c4e58280..eac6e4d99c 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/notifications/RoomEventGroupInfo.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/notifications/RoomEventGroupInfo.kt @@ -20,15 +20,14 @@ package im.vector.riotredesign.features.notifications * Data class to hold information about a group of notifications for a room */ data class RoomEventGroupInfo( - val roomId: String + val roomId: String, + val roomDisplayName: String = "", + val isDirect: Boolean = false ) { - var roomDisplayName: String = "" - var roomAvatarPath: String? = null - //An event in the list has not yet been display + // An event in the list has not yet been display var hasNewEvent: Boolean = false - //true if at least one on the not yet displayed event is noisy + // true if at least one on the not yet displayed event is noisy var shouldBing: Boolean = false var customSound: String? = null - var hasSmartReplyError = false - var isDirect = false -} \ No newline at end of file + var hasSmartReplyError: Boolean = false +} diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/PreferencesManager.java b/vector/src/main/java/im/vector/riotredesign/features/settings/PreferencesManager.java index 4a91759299..c57e0fa2fb 100755 --- a/vector/src/main/java/im/vector/riotredesign/features/settings/PreferencesManager.java +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/PreferencesManager.java @@ -50,6 +50,8 @@ public class PreferencesManager { public static final String SETTINGS_IDENTITY_SERVER_PREFERENCE_KEY = "SETTINGS_IDENTITY_SERVER_PREFERENCE_KEY"; public static final String SETTINGS_APP_TERM_CONDITIONS_PREFERENCE_KEY = "SETTINGS_APP_TERM_CONDITIONS_PREFERENCE_KEY"; public static final String SETTINGS_PRIVACY_POLICY_PREFERENCE_KEY = "SETTINGS_PRIVACY_POLICY_PREFERENCE_KEY"; + + //TODO delete public static final String SETTINGS_NOTIFICATION_PRIVACY_PREFERENCE_KEY = "SETTINGS_NOTIFICATION_PRIVACY_PREFERENCE_KEY"; public static final String SETTINGS_NOTIFICATION_TROUBLESHOOT_PREFERENCE_KEY = "SETTINGS_NOTIFICATION_TROUBLESHOOT_PREFERENCE_KEY"; public static final String SETTINGS_NOTIFICATION_ADVANCED_PREFERENCE_KEY = "SETTINGS_NOTIFICATION_ADVANCED_PREFERENCE_KEY"; @@ -241,6 +243,10 @@ public class PreferencesManager { editor.apply(); } + public static boolean areNotificationEnabledForDevice(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY, false); + } + /** * Tells if we have already asked the user to disable battery optimisations on android >= M devices. * diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsActivity.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsActivity.kt index 6208c75e4b..85f1e0bec9 100755 --- a/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsActivity.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsActivity.kt @@ -26,6 +26,7 @@ import im.vector.riotredesign.R import im.vector.riotredesign.core.platform.VectorBaseActivity import kotlinx.android.synthetic.main.activity_vector_settings.* import org.koin.android.ext.android.inject +import timber.log.Timber /** * Displays the client settings. @@ -35,7 +36,6 @@ class VectorSettingsActivity : VectorBaseActivity(), FragmentManager.OnBackStackChangedListener, VectorSettingsFragmentInteractionListener { - private lateinit var vectorSettingsPreferencesFragment: VectorSettingsPreferencesFragment override fun getLayoutRes() = R.layout.activity_vector_settings @@ -48,14 +48,15 @@ class VectorSettingsActivity : VectorBaseActivity(), override fun initUiAndData() { configureToolbar(settingsToolbar) + var vectorSettingsPreferencesFragment: Fragment? = null if (isFirstCreation()) { - vectorSettingsPreferencesFragment = VectorSettingsPreferencesFragment.newInstance(session.sessionParams.credentials.userId) + vectorSettingsPreferencesFragment = VectorSettingsPreferencesFragmentV2.newInstance() // display the fragment supportFragmentManager.beginTransaction() .replace(R.id.vector_settings_page, vectorSettingsPreferencesFragment, FRAGMENT_TAG) .commit() } else { - vectorSettingsPreferencesFragment = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG) as VectorSettingsPreferencesFragment + vectorSettingsPreferencesFragment = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG) } @@ -77,19 +78,33 @@ class VectorSettingsActivity : VectorBaseActivity(), override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat?, pref: Preference?): Boolean { var oFragment: Fragment? = null - if (PreferencesManager.SETTINGS_NOTIFICATION_TROUBLESHOOT_PREFERENCE_KEY == pref?.key) { + if ("Legacy" == pref?.title) { + oFragment = VectorSettingsPreferencesFragment.newInstance(session.sessionParams.credentials.userId) + } else if (PreferencesManager.SETTINGS_NOTIFICATION_TROUBLESHOOT_PREFERENCE_KEY == pref?.key) { oFragment = VectorSettingsNotificationsTroubleshootFragment.newInstance(session.sessionParams.credentials.userId) } else if (PreferencesManager.SETTINGS_NOTIFICATION_ADVANCED_PREFERENCE_KEY == pref?.key) { oFragment = VectorSettingsAdvancedNotificationPreferenceFragment.newInstance(session.sessionParams.credentials.userId) + } else { + try { + pref?.fragment?.let { + oFragment = supportFragmentManager.fragmentFactory + .instantiate( + classLoader, + it, pref.extras) + } + } catch (e: Throwable) { + showSnackbar(getString(R.string.not_implemented)) + Timber.e(e) + } } if (oFragment != null) { - oFragment.setTargetFragment(caller, 0) + oFragment!!.setTargetFragment(caller, 0) // Replace the existing Fragment with the new Fragment supportFragmentManager.beginTransaction() - .setCustomAnimations(R.anim.anim_slide_in_bottom, R.anim.anim_slide_out_bottom, - R.anim.anim_slide_in_bottom, R.anim.anim_slide_out_bottom) - .replace(R.id.vector_settings_page, oFragment, pref?.title.toString()) + .setCustomAnimations(R.anim.right_in, R.anim.fade_out, + R.anim.fade_in, R.anim.right_out) + .replace(R.id.vector_settings_page, oFragment!!, pref?.title.toString()) .addToBackStack(null) .commit() return true diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsAdvancedNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsAdvancedNotificationPreferenceFragment.kt index eee04547ec..05109c283e 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsAdvancedNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsAdvancedNotificationPreferenceFragment.kt @@ -51,6 +51,8 @@ class VectorSettingsAdvancedNotificationPreferenceFragment : VectorPreferenceFra } } */ + override var titleRes: Int = R.string.settings_notification_advanced + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { // define the layout addPreferencesFromResource(R.xml.vector_settings_notification_advanced_preferences) @@ -177,7 +179,6 @@ class VectorSettingsAdvancedNotificationPreferenceFragment : VectorPreferenceFra override fun onResume() { super.onResume() - (activity as? VectorBaseActivity)?.supportActionBar?.setTitle(R.string.settings_notification_advanced) // find the view from parent activity mLoadingView = activity!!.findViewById(R.id.vector_settings_spinner_views) @@ -222,14 +223,14 @@ class VectorSettingsAdvancedNotificationPreferenceFragment : VectorPreferenceFra if (TextUtils.equals(ruleId, BingRule.RULE_ID_DISABLE_ALL) || TextUtils.equals(ruleId, BingRule.RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS)) { isEnabled = !isEnabled } else if (isEnabled) { - val actions = rule!!.actions + val domainActions = rule!!.domainActions // no action -> noting will be done - if (null == actions || actions.isEmpty()) { + if (null == domainActions || domainActions.isEmpty()) { isEnabled = false - } else if (1 == actions.size) { + } else if (1 == domainActions.size) { try { - isEnabled = !TextUtils.equals(actions[0] as String, BingRule.ACTION_DONT_NOTIFY) + isEnabled = !TextUtils.equals(domainActions[0] as String, BingRule.ACTION_DONT_NOTIFY) } catch (e: Exception) { Timber.e(e, "## refreshPreferences failed") } diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsNotificationFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsNotificationFragment.kt new file mode 100644 index 0000000000..1a967db072 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsNotificationFragment.kt @@ -0,0 +1,72 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.settings + +import android.os.Bundle +import androidx.preference.Preference +import androidx.preference.SwitchPreference +import im.vector.matrix.android.api.Matrix +import im.vector.matrix.android.api.MatrixCallback +import im.vector.riotredesign.R +import im.vector.riotredesign.core.platform.VectorPreferenceFragment +import im.vector.riotredesign.core.pushers.PushersManager +import im.vector.riotredesign.push.fcm.FcmHelper +import org.koin.android.ext.android.inject + +// Referenced in vector_settings_preferences_root.xml +class VectorSettingsNotificationPreferenceFragment : VectorPreferenceFragment() { + + override var titleRes: Int = R.string.settings_notifications + + val pushManager: PushersManager by inject() + + + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + addPreferencesFromResource(R.xml.vector_settings_notifications) + } + + override fun onResume() { + super.onResume() + Matrix.getInstance().currentSession?.refreshPushers() + } + + override fun onPreferenceTreeClick(preference: Preference?): Boolean { + if (preference?.key == PreferencesManager.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY) { + val switchPref = preference as SwitchPreference + if (switchPref.isChecked) { + FcmHelper.getFcmToken(requireContext())?.let { + if (PreferencesManager.areNotificationEnabledForDevice(requireContext())) { + pushManager.registerPusherWithFcmKey(it) + } + } + } else { + FcmHelper.getFcmToken(requireContext())?.let { + pushManager.unregisterPusher(it, object : MatrixCallback { + override fun onSuccess(data: Unit) { + super.onSuccess(data) + } + + override fun onFailure(failure: Throwable) { + super.onFailure(failure) + } + }) + } + } + } + return super.onPreferenceTreeClick(preference) + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsPreferencesFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsPreferencesFragment.kt index d821c7f7fa..6ce235f6b3 100755 --- a/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsPreferencesFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsPreferencesFragment.kt @@ -84,6 +84,7 @@ import java.util.* class VectorSettingsPreferencesFragment : VectorPreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener { + override var titleRes: Int = R.string.title_activity_settings // members private val mSession by inject() @@ -1493,14 +1494,14 @@ class VectorSettingsPreferencesFragment : VectorPreferenceFragment(), SharedPref if (TextUtils.equals(ruleId, BingRule.RULE_ID_DISABLE_ALL) || TextUtils.equals(ruleId, BingRule.RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS)) { isEnabled = !isEnabled } else if (isEnabled) { - val actions = rule?.actions + val domainActions = rule?.domainActions // no action -> noting will be done - if (null == actions || actions.isEmpty()) { + if (null == domainActions || domainActions.isEmpty()) { isEnabled = false - } else if (1 == actions.size) { + } else if (1 == domainActions.size) { try { - isEnabled = !TextUtils.equals(actions[0] as String, BingRule.ACTION_DONT_NOTIFY) + isEnabled = !TextUtils.equals(domainActions[0] as String, BingRule.ACTION_DONT_NOTIFY) } catch (e: Exception) { Timber.e(e, "## refreshPreferences failed " + e.message) } @@ -1670,15 +1671,15 @@ class VectorSettingsPreferencesFragment : VectorPreferenceFragment(), SharedPref var index = 0 - for (pusher in mDisplayedPushers) { - if (null != pusher.lang) { - val isThisDeviceTarget = TextUtils.equals(pushManager.currentRegistrationToken, pusher.pushkey) + for (pushRule in mDisplayedPushers) { + if (null != pushRule.lang) { + val isThisDeviceTarget = TextUtils.equals(pushManager.currentRegistrationToken, pushRule.pushkey) val preference = VectorPreference(activity).apply { mTypeface = if (isThisDeviceTarget) Typeface.BOLD else Typeface.NORMAL } - preference.title = pusher.deviceDisplayName - preference.summary = pusher.appDisplayName + preference.title = pushRule.deviceDisplayName + preference.summary = pushRule.appDisplayName preference.key = PUSHER_PREFERENCE_KEY_BASE + index index++ mPushersSettingsCategory.addPreference(preference) @@ -1693,7 +1694,7 @@ class VectorSettingsPreferencesFragment : VectorPreferenceFragment(), SharedPref .setPositiveButton(R.string.remove) { _, _ -> displayLoadingView() - pushManager.unregister(session, pusher, object : MatrixCallback { + pushManager.unregister(session, pushRule, object : MatrixCallback { override fun onSuccess(info: Void?) { refreshPushersList() onCommonDone(null) diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsPreferencesFragmentV2.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsPreferencesFragmentV2.kt new file mode 100644 index 0000000000..b29db44015 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsPreferencesFragmentV2.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.settings + +import android.os.Bundle +import im.vector.riotredesign.R +import im.vector.riotredesign.core.extensions.withArgs +import im.vector.riotredesign.core.platform.VectorPreferenceFragment + +class VectorSettingsPreferencesFragmentV2 : VectorPreferenceFragment() { + + override var titleRes: Int = R.string.title_activity_settings + + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + addPreferencesFromResource(R.xml.vector_settings_preferences_root) + } + + + companion object { + fun newInstance() = VectorSettingsPreferencesFragmentV2() + .withArgs { + //putString(ARG_MATRIX_ID, matrixId) + } + } + +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushGatewayItem.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushGatewayItem.kt new file mode 100644 index 0000000000..4e3e116647 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushGatewayItem.kt @@ -0,0 +1,63 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.settings.push + +import android.widget.TextView +import com.airbnb.epoxy.EpoxyAttribute +import com.airbnb.epoxy.EpoxyModelClass +import com.airbnb.epoxy.EpoxyModelWithHolder +import im.vector.matrix.android.api.session.pushers.Pusher +import im.vector.riotredesign.R +import im.vector.riotredesign.core.epoxy.VectorEpoxyHolder + + +@EpoxyModelClass(layout = R.layout.item_pushgateway) +abstract class PushGatewayItem : EpoxyModelWithHolder() { + + @EpoxyAttribute + lateinit var pusher: Pusher + + override fun bind(holder: Holder) { + holder.kind.text = when (pusher.kind) { + // TODO Create const + "http" -> "Http Pusher" + "mail" -> "Email Pusher" + else -> pusher.kind + } + + holder.appId.text = pusher.appId + holder.pushKey.text = pusher.pushKey + holder.appName.text = pusher.appDisplayName + holder.url.text = pusher.data.url + holder.format.text = pusher.data.format + holder.deviceName.text = pusher.deviceDisplayName + } + + + class Holder : VectorEpoxyHolder() { + val kind by bind(R.id.pushGatewayKind) + val pushKey by bind(R.id.pushGatewayKeyValue) + val deviceName by bind(R.id.pushGatewayDeviceNameValue) + val format by bind(R.id.pushGatewayFormatValue) + val url by bind(R.id.pushGatewayURLValue) + val appName by bind(R.id.pushGatewayAppNameValue) + val appId by bind(R.id.pushGatewayAppIdValue) + } +} + +// +//abstract class ReactionInfoSimpleItem : EpoxyModelWithHolder() { \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushGatewaysFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushGatewaysFragment.kt new file mode 100644 index 0000000000..1ea57264da --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushGatewaysFragment.kt @@ -0,0 +1,86 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.settings.push + +import android.os.Bundle +import androidx.recyclerview.widget.DividerItemDecoration +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import com.airbnb.epoxy.TypedEpoxyController +import com.airbnb.mvrx.fragmentViewModel +import com.airbnb.mvrx.withState +import im.vector.riotredesign.R +import im.vector.riotredesign.core.platform.VectorBaseActivity +import im.vector.riotredesign.core.platform.VectorBaseFragment +import im.vector.riotredesign.core.resources.StringProvider +import im.vector.riotredesign.core.ui.list.genericFooterItem +import kotlinx.android.synthetic.main.fragment_generic_recycler_epoxy.* + +// Referenced in vector_settings_notifications.xml +class PushGatewaysFragment : VectorBaseFragment() { + + override fun getLayoutResId(): Int = R.layout.fragment_generic_recycler_epoxy + + private val viewModel: PushGatewaysViewModel by fragmentViewModel(PushGatewaysViewModel::class) + + private val epoxyController by lazy { PushGateWayController(StringProvider(requireContext().resources)) } + + override fun onResume() { + super.onResume() + (activity as? VectorBaseActivity)?.supportActionBar?.setTitle(R.string.settings_notifications_targets) + } + + override fun onActivityCreated(savedInstanceState: Bundle?) { + super.onActivityCreated(savedInstanceState) + val lmgr = LinearLayoutManager(context, RecyclerView.VERTICAL, false) + epoxyRecyclerView.layoutManager = lmgr + val dividerItemDecoration = DividerItemDecoration(epoxyRecyclerView.context, + lmgr.orientation) + epoxyRecyclerView.addItemDecoration(dividerItemDecoration) + epoxyRecyclerView.setController(epoxyController) + } + + override fun invalidate() = withState(viewModel) { state -> + epoxyController.setData(state) + } + + class PushGateWayController(private val stringProvider: StringProvider) : TypedEpoxyController() { + override fun buildModels(data: PushGatewayViewState?) { + data?.pushGateways?.invoke()?.let { pushers -> + if (pushers.isEmpty()) { + genericFooterItem { + id("footer") + text(stringProvider.getString(R.string.settings_push_gateway_no_pushers)) + } + } else { + pushers.forEach { + pushGatewayItem { + id("${it.pushKey}_${it.appId}") + pusher(it) + } + } + } + } ?: run { + genericFooterItem { + id("footer") + text(stringProvider.getString(R.string.loading)) + } + } + } + + } +} diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushGatewaysViewModel.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushGatewaysViewModel.kt new file mode 100644 index 0000000000..edb032a0e9 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushGatewaysViewModel.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.settings.push + +import androidx.lifecycle.Observer +import com.airbnb.mvrx.* +import im.vector.matrix.android.api.session.Session +import im.vector.matrix.android.api.session.pushers.Pusher +import im.vector.riotredesign.core.platform.VectorViewModel +import org.koin.android.ext.android.get + + +data class PushGatewayViewState( + val pushGateways: Async> = Uninitialized +) : MvRxState + +class PushGatewaysViewModel(initialState: PushGatewayViewState) : VectorViewModel(initialState) { + + companion object : MvRxViewModelFactory { + + override fun create(viewModelContext: ViewModelContext, state: PushGatewayViewState): PushGatewaysViewModel? { + val session = viewModelContext.activity.get() + val fragment = (viewModelContext as FragmentViewModelContext).fragment + + val livePushers = session.livePushers() + + val viewModel = PushGatewaysViewModel(state) + + livePushers.observe(fragment, Observer { + viewModel.setState { + PushGatewayViewState(Success(it)) + } + }) + return viewModel + } + + } + +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushRuleItem.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushRuleItem.kt new file mode 100644 index 0000000000..c723f4ef87 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushRuleItem.kt @@ -0,0 +1,72 @@ +package im.vector.riotredesign.features.settings.push + +import android.annotation.SuppressLint +import android.graphics.Color +import android.widget.ImageView +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.view.isInvisible +import androidx.core.view.isVisible +import com.airbnb.epoxy.EpoxyAttribute +import com.airbnb.epoxy.EpoxyModelClass +import com.airbnb.epoxy.EpoxyModelWithHolder +import im.vector.matrix.android.api.pushrules.Action +import im.vector.matrix.android.api.pushrules.rest.PushRule +import im.vector.riotredesign.R +import im.vector.riotredesign.core.epoxy.VectorEpoxyHolder +import im.vector.riotredesign.features.notifications.NotificationAction + + +@EpoxyModelClass(layout = R.layout.item_pushrule_raw) +abstract class PushRuleItem : EpoxyModelWithHolder() { + + @EpoxyAttribute + lateinit var pushRule: PushRule + + // TODO i18n + @SuppressLint("SetTextI18n") + override fun bind(holder: Holder) { + val context = holder.view.context + if (pushRule.enabled) { + holder.view.setBackgroundColor(Color.TRANSPARENT) + holder.ruleId.text = pushRule.ruleId + } else { + holder.view.setBackgroundColor(ContextCompat.getColor(context, R.color.vector_silver_color)) + holder.ruleId.text = "[Disabled] ${pushRule.ruleId}" + } + val actions = Action.mapFrom(pushRule) + if (actions.isNullOrEmpty()) { + holder.actionIcon.isInvisible = true + } else { + holder.actionIcon.isVisible = true + val notifAction = NotificationAction.extractFrom(actions) + + if (notifAction.shouldNotify && !notifAction.soundName.isNullOrBlank()) { + holder.actionIcon.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_action_notify_noisy)) + } else if (notifAction.shouldNotify) { + holder.actionIcon.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_action_notify_silent)) + } else { + holder.actionIcon.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_action_dont_notify)) + } + + val description = StringBuffer() + pushRule.conditions?.forEachIndexed { i, condition -> + if (i > 0) description.append("\n") + description.append(condition.asExecutableCondition()?.technicalDescription() + ?: "UNSUPPORTED") + } + if (description.isBlank()) { + holder.description.text = "No Conditions" + } else { + holder.description.text = description + } + + } + } + + class Holder : VectorEpoxyHolder() { + val ruleId by bind(R.id.pushRuleId) + val description by bind(R.id.pushRuleDescription) + val actionIcon by bind(R.id.pushRuleActionIcon) + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushRulesFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushRulesFragment.kt new file mode 100644 index 0000000000..3d5881db22 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushRulesFragment.kt @@ -0,0 +1,80 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.riotredesign.features.settings.push + +import android.os.Bundle +import androidx.recyclerview.widget.DividerItemDecoration +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import com.airbnb.epoxy.TypedEpoxyController +import com.airbnb.mvrx.fragmentViewModel +import com.airbnb.mvrx.withState +import im.vector.riotredesign.R +import im.vector.riotredesign.core.platform.VectorBaseActivity +import im.vector.riotredesign.core.platform.VectorBaseFragment +import im.vector.riotredesign.core.resources.StringProvider +import im.vector.riotredesign.core.ui.list.genericFooterItem +import kotlinx.android.synthetic.main.fragment_generic_recycler_epoxy.* + +// Referenced in vector_settings_notifications.xml +class PushRulesFragment : VectorBaseFragment() { + + override fun getLayoutResId(): Int = R.layout.fragment_generic_recycler_epoxy + + private val viewModel: PushRulesViewModel by fragmentViewModel(PushRulesViewModel::class) + + private val epoxyController by lazy { PushRulesController(StringProvider(requireContext().resources)) } + + + override fun onResume() { + super.onResume() + (activity as? VectorBaseActivity)?.supportActionBar?.setTitle(R.string.settings_push_rules) + } + + override fun onActivityCreated(savedInstanceState: Bundle?) { + super.onActivityCreated(savedInstanceState) + val lmgr = LinearLayoutManager(context, RecyclerView.VERTICAL, false) + epoxyRecyclerView.layoutManager = lmgr + val dividerItemDecoration = DividerItemDecoration(epoxyRecyclerView.context, + lmgr.orientation) + epoxyRecyclerView.addItemDecoration(dividerItemDecoration) + epoxyRecyclerView.setController(epoxyController) + } + + override fun invalidate() = withState(viewModel) { state -> + epoxyController.setData(state) + } + + class PushRulesController(private val stringProvider: StringProvider) : TypedEpoxyController() { + + override fun buildModels(data: PushRulesViewState?) { + data?.let { + it.rules.forEach { + pushRuleItem { + id(it.ruleId) + pushRule(it) + } + } + } ?: run { + genericFooterItem { + id("footer") + text(stringProvider.getString(R.string.settings_push_rules_no_rules)) + } + } + } + + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushRulesViewModel.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushRulesViewModel.kt new file mode 100644 index 0000000000..03a2e600e0 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/push/PushRulesViewModel.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.riotredesign.features.settings.push + +import com.airbnb.mvrx.MvRxState +import com.airbnb.mvrx.MvRxViewModelFactory +import com.airbnb.mvrx.ViewModelContext +import im.vector.matrix.android.api.pushrules.rest.PushRule +import im.vector.matrix.android.api.session.Session +import im.vector.riotredesign.core.platform.VectorViewModel +import org.koin.android.ext.android.get + +data class PushRulesViewState( + val rules: List = emptyList() +) : MvRxState + + +class PushRulesViewModel(initialState: PushRulesViewState) : VectorViewModel(initialState) { + + companion object : MvRxViewModelFactory { + + override fun initialState(viewModelContext: ViewModelContext): PushRulesViewState? { + val session = viewModelContext.activity.get() + val rules = session.getPushRules() + + return PushRulesViewState(rules) + } + + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/troubleshoot/TestAccountSettings.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/troubleshoot/TestAccountSettings.kt index 7a5441bfc3..4580f097f9 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/settings/troubleshoot/TestAccountSettings.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/settings/troubleshoot/TestAccountSettings.kt @@ -13,24 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package im.vector.fragments.troubleshoot +package im.vector.riotredesign.features.settings.troubleshoot import androidx.fragment.app.Fragment +import im.vector.matrix.android.api.MatrixCallback +import im.vector.matrix.android.api.pushrules.RuleIds import im.vector.matrix.android.api.session.Session import im.vector.riotredesign.R -import im.vector.riotredesign.features.settings.troubleshoot.TroubleshootTest /** * Check that the main pushRule (RULE_ID_DISABLE_ALL) is correctly setup */ -class TestAccountSettings(val fragment: Fragment, val session: Session) : TroubleshootTest(R.string.settings_troubleshoot_test_account_settings_title) { +class TestAccountSettings(val fragment: Fragment, val session: Session) + : TroubleshootTest(R.string.settings_troubleshoot_test_account_settings_title) { override fun perform() { - /* - TODO - val defaultRule = session?.dataHandler?.bingRulesManager?.pushRules()?.findDefaultRule(BingRule.RULE_ID_DISABLE_ALL) + val defaultRule = session.getPushRules() + .find { it.ruleId == RuleIds.RULE_ID_DISABLE_ALL } + if (defaultRule != null) { - if (!defaultRule.isEnabled) { + if (!defaultRule.enabled) { description = fragment.getString(R.string.settings_troubleshoot_test_account_settings_success) quickFix = null status = TestStatus.SUCCESS @@ -39,14 +41,16 @@ class TestAccountSettings(val fragment: Fragment, val session: Session) : Troubl quickFix = object : TroubleshootQuickFix(R.string.settings_troubleshoot_test_account_settings_quickfix) { override fun doFix() { if (manager?.diagStatus == TestStatus.RUNNING) return //wait before all is finished - session?.dataHandler?.bingRulesManager?.updateEnableRuleStatus(defaultRule, !defaultRule.isEnabled, - object : BingRulesManager.onBingRuleUpdateListener { - override fun onBingRuleUpdateSuccess() { + // TODO Use constant for kind + session.updatePushRuleEnableStatus("override", defaultRule, !defaultRule.enabled, + object : MatrixCallback { + + override fun onSuccess(data: Unit) { manager?.retry() } - override fun onBingRuleUpdateFailure(errorMessage: String) { + override fun onFailure(failure: Throwable) { manager?.retry() } }) @@ -58,8 +62,5 @@ class TestAccountSettings(val fragment: Fragment, val session: Session) : Troubl //should not happen? status = TestStatus.FAILED } - */ - - status = TestStatus.FAILED } } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/workers/signout/SignOutUiWorker.kt b/vector/src/main/java/im/vector/riotredesign/features/workers/signout/SignOutUiWorker.kt index 81f80b9f02..3257685186 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/workers/signout/SignOutUiWorker.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/workers/signout/SignOutUiWorker.kt @@ -21,8 +21,10 @@ import im.vector.matrix.android.api.session.Session import im.vector.riotredesign.R import im.vector.riotredesign.core.platform.VectorBaseActivity import im.vector.riotredesign.features.MainActivity +import im.vector.riotredesign.features.notifications.NotificationDrawerManager -class SignOutUiWorker(val activity: VectorBaseActivity) { +class SignOutUiWorker(private val activity: VectorBaseActivity, + private val notificationDrawerManager: NotificationDrawerManager) { fun perform(session: Session) { if (SignOutViewModel.doYouNeedToBeDisplayed(session)) { @@ -45,6 +47,9 @@ class SignOutUiWorker(val activity: VectorBaseActivity) { } private fun doSignOut() { + // Dismiss all notifications + notificationDrawerManager.clearAllEvents() + MainActivity.restartApp(activity, clearCache = true, clearCredentials = true) } } diff --git a/vector/src/main/res/anim/fade_in.xml b/vector/src/main/res/anim/fade_in.xml new file mode 100644 index 0000000000..fbbaaf17d0 --- /dev/null +++ b/vector/src/main/res/anim/fade_in.xml @@ -0,0 +1,11 @@ + + + + + + diff --git a/vector/src/main/res/anim/fade_out.xml b/vector/src/main/res/anim/fade_out.xml new file mode 100644 index 0000000000..fe99ebf7c2 --- /dev/null +++ b/vector/src/main/res/anim/fade_out.xml @@ -0,0 +1,11 @@ + + + + + + diff --git a/vector/src/main/res/anim/right_in.xml b/vector/src/main/res/anim/right_in.xml new file mode 100644 index 0000000000..35f261c926 --- /dev/null +++ b/vector/src/main/res/anim/right_in.xml @@ -0,0 +1,10 @@ + + + + diff --git a/vector/src/main/res/anim/right_out.xml b/vector/src/main/res/anim/right_out.xml new file mode 100644 index 0000000000..504c39b8bf --- /dev/null +++ b/vector/src/main/res/anim/right_out.xml @@ -0,0 +1,16 @@ + + + + + + + diff --git a/vector/src/main/res/drawable-hdpi/ic_settings.png b/vector/src/main/res/drawable-hdpi/ic_settings.png deleted file mode 100755 index 7ef944dbd0..0000000000 Binary files a/vector/src/main/res/drawable-hdpi/ic_settings.png and /dev/null differ diff --git a/vector/src/main/res/drawable-hdpi/sync.png b/vector/src/main/res/drawable-hdpi/sync.png new file mode 100644 index 0000000000..f191e2dddf Binary files /dev/null and b/vector/src/main/res/drawable-hdpi/sync.png differ diff --git a/vector/src/main/res/drawable-mdpi/ic_settings.png b/vector/src/main/res/drawable-mdpi/ic_settings.png deleted file mode 100755 index 10f0d15d0f..0000000000 Binary files a/vector/src/main/res/drawable-mdpi/ic_settings.png and /dev/null differ diff --git a/vector/src/main/res/drawable-mdpi/sync.png b/vector/src/main/res/drawable-mdpi/sync.png new file mode 100644 index 0000000000..2542f326ca Binary files /dev/null and b/vector/src/main/res/drawable-mdpi/sync.png differ diff --git a/vector/src/main/res/drawable-xhdpi/ic_settings.png b/vector/src/main/res/drawable-xhdpi/ic_settings.png deleted file mode 100755 index 56d5d9fc27..0000000000 Binary files a/vector/src/main/res/drawable-xhdpi/ic_settings.png and /dev/null differ diff --git a/vector/src/main/res/drawable-xhdpi/sync.png b/vector/src/main/res/drawable-xhdpi/sync.png new file mode 100644 index 0000000000..64478c4e13 Binary files /dev/null and b/vector/src/main/res/drawable-xhdpi/sync.png differ diff --git a/vector/src/main/res/drawable-xxhdpi/ic_settings.png b/vector/src/main/res/drawable-xxhdpi/ic_settings.png deleted file mode 100755 index 879eb9a702..0000000000 Binary files a/vector/src/main/res/drawable-xxhdpi/ic_settings.png and /dev/null differ diff --git a/vector/src/main/res/drawable-xxhdpi/sync.png b/vector/src/main/res/drawable-xxhdpi/sync.png new file mode 100644 index 0000000000..b613b0d7a0 Binary files /dev/null and b/vector/src/main/res/drawable-xxhdpi/sync.png differ diff --git a/vector/src/main/res/drawable-xxxhdpi/ic_settings.png b/vector/src/main/res/drawable-xxxhdpi/ic_settings.png deleted file mode 100755 index 82d679bdc9..0000000000 Binary files a/vector/src/main/res/drawable-xxxhdpi/ic_settings.png and /dev/null differ diff --git a/vector/src/main/res/drawable-xxxhdpi/sync.png b/vector/src/main/res/drawable-xxxhdpi/sync.png new file mode 100644 index 0000000000..99adbef69c Binary files /dev/null and b/vector/src/main/res/drawable-xxxhdpi/sync.png differ diff --git a/vector/src/main/res/drawable/ic_action_dont_notify.xml b/vector/src/main/res/drawable/ic_action_dont_notify.xml new file mode 100644 index 0000000000..b199ce0493 --- /dev/null +++ b/vector/src/main/res/drawable/ic_action_dont_notify.xml @@ -0,0 +1,25 @@ + + + + + diff --git a/vector/src/main/res/drawable/ic_action_notify_noisy.xml b/vector/src/main/res/drawable/ic_action_notify_noisy.xml new file mode 100644 index 0000000000..fb07819881 --- /dev/null +++ b/vector/src/main/res/drawable/ic_action_notify_noisy.xml @@ -0,0 +1,18 @@ + + + + diff --git a/vector/src/main/res/drawable/ic_action_notify_silent.xml b/vector/src/main/res/drawable/ic_action_notify_silent.xml new file mode 100644 index 0000000000..c2d325e1fd --- /dev/null +++ b/vector/src/main/res/drawable/ic_action_notify_silent.xml @@ -0,0 +1,24 @@ + + + + + diff --git a/vector/src/main/res/drawable/ic_bell.xml b/vector/src/main/res/drawable/ic_bell.xml new file mode 100644 index 0000000000..cb648585ce --- /dev/null +++ b/vector/src/main/res/drawable/ic_bell.xml @@ -0,0 +1,14 @@ + + + diff --git a/vector/src/main/res/drawable/ic_flair.xml b/vector/src/main/res/drawable/ic_flair.xml new file mode 100644 index 0000000000..8d30334432 --- /dev/null +++ b/vector/src/main/res/drawable/ic_flair.xml @@ -0,0 +1,22 @@ + + + + diff --git a/vector/src/main/res/drawable/ic_lock.xml b/vector/src/main/res/drawable/ic_lock.xml new file mode 100644 index 0000000000..2929f41225 --- /dev/null +++ b/vector/src/main/res/drawable/ic_lock.xml @@ -0,0 +1,22 @@ + + + + diff --git a/vector/src/main/res/drawable/ic_settings_general.xml b/vector/src/main/res/drawable/ic_settings_general.xml new file mode 100644 index 0000000000..97b060f644 --- /dev/null +++ b/vector/src/main/res/drawable/ic_settings_general.xml @@ -0,0 +1,22 @@ + + + + diff --git a/vector/src/main/res/drawable/ic_settings_lab.xml b/vector/src/main/res/drawable/ic_settings_lab.xml new file mode 100644 index 0000000000..d1f5aca7ff --- /dev/null +++ b/vector/src/main/res/drawable/ic_settings_lab.xml @@ -0,0 +1,14 @@ + + + diff --git a/vector/src/main/res/drawable/ic_settings_x.xml b/vector/src/main/res/drawable/ic_settings_x.xml index 994da00c19..08af7d6539 100644 --- a/vector/src/main/res/drawable/ic_settings_x.xml +++ b/vector/src/main/res/drawable/ic_settings_x.xml @@ -9,7 +9,7 @@ android:strokeWidth="1.2" android:fillColor="#00000000" android:fillType="evenOdd" - android:strokeColor="#FFF" + android:strokeColor="#454545" android:strokeLineCap="round"/> diff --git a/vector/src/main/res/drawable/ic_sliders.xml b/vector/src/main/res/drawable/ic_sliders.xml new file mode 100644 index 0000000000..55ccfcfb3d --- /dev/null +++ b/vector/src/main/res/drawable/ic_sliders.xml @@ -0,0 +1,14 @@ + + + diff --git a/vector/src/main/res/layout/fragment_generic_recycler_epoxy.xml b/vector/src/main/res/layout/fragment_generic_recycler_epoxy.xml new file mode 100644 index 0000000000..c794e8a5fa --- /dev/null +++ b/vector/src/main/res/layout/fragment_generic_recycler_epoxy.xml @@ -0,0 +1,9 @@ + + diff --git a/vector/src/main/res/layout/fragment_home_drawer.xml b/vector/src/main/res/layout/fragment_home_drawer.xml index fd60368be1..f12c5f8536 100644 --- a/vector/src/main/res/layout/fragment_home_drawer.xml +++ b/vector/src/main/res/layout/fragment_home_drawer.xml @@ -74,6 +74,7 @@ android:background="?attr/selectableItemBackground" android:padding="16dp" android:src="@drawable/ic_settings_x" + android:tint="@android:color/white" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> diff --git a/vector/src/main/res/layout/item_generic_footer.xml b/vector/src/main/res/layout/item_generic_footer.xml new file mode 100644 index 0000000000..c5c4e7fcd5 --- /dev/null +++ b/vector/src/main/res/layout/item_generic_footer.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/vector/src/main/res/layout/item_pushgateway.xml b/vector/src/main/res/layout/item_pushgateway.xml new file mode 100644 index 0000000000..c4a01be169 --- /dev/null +++ b/vector/src/main/res/layout/item_pushgateway.xml @@ -0,0 +1,122 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/vector/src/main/res/layout/item_pushrule_raw.xml b/vector/src/main/res/layout/item_pushrule_raw.xml new file mode 100644 index 0000000000..0d2ba360bd --- /dev/null +++ b/vector/src/main/res/layout/item_pushrule_raw.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/vector/src/main/res/values/config.xml b/vector/src/main/res/values/config.xml index 60789cd651..39c654e989 100755 --- a/vector/src/main/res/values/config.xml +++ b/vector/src/main/res/values/config.xml @@ -20,9 +20,12 @@ https://scalar.vector.im/api - - - + + https://matrix.org/_matrix/push/v1/notify + im.vector.app.android matrix.org diff --git a/vector/src/main/res/values/strings_riotX.xml b/vector/src/main/res/values/strings_riotX.xml index af56eb605a..7ff0148911 100644 --- a/vector/src/main/res/values/strings_riotX.xml +++ b/vector/src/main/res/values/strings_riotX.xml @@ -11,4 +11,23 @@ You are already viewing this room! Quick Reactions + + + General + Preferences + Security & Privacy + Expert + Push Rules + No push rules defined + No registered push gateways + + app_id: + push_key: + app_display_name: + device_name: + Url: + Format: + + Legacy + \ No newline at end of file diff --git a/vector/src/main/res/values/theme_dark.xml b/vector/src/main/res/values/theme_dark.xml index 986ed33d5d..76cd365e99 100644 --- a/vector/src/main/res/values/theme_dark.xml +++ b/vector/src/main/res/values/theme_dark.xml @@ -28,11 +28,14 @@ @color/riotx_fab_label_bg_dark @color/riotx_fab_label_color_dark @color/riotx_touch_guard_bg_dark + @color/riotx_keys_backup_banner_accent_color_dark @drawable/highlighted_message_background_dark + @color/riotx_header_panel_border_mobile_dark + @color/riotx_accent @color/primary_color_dark_light diff --git a/vector/src/main/res/values/theme_light.xml b/vector/src/main/res/values/theme_light.xml index ad487d1d61..f7f9a26051 100644 --- a/vector/src/main/res/values/theme_light.xml +++ b/vector/src/main/res/values/theme_light.xml @@ -33,6 +33,8 @@ @drawable/highlighted_message_background_light + @color/riotx_header_panel_border_mobile_light + @color/riotx_accent diff --git a/vector/src/main/res/xml/vector_settings_notification_advanced_preferences.xml b/vector/src/main/res/xml/vector_settings_notification_advanced_preferences.xml index 5ff42694ee..bb5de919c5 100644 --- a/vector/src/main/res/xml/vector_settings_notification_advanced_preferences.xml +++ b/vector/src/main/res/xml/vector_settings_notification_advanced_preferences.xml @@ -1,7 +1,7 @@ - @@ -34,7 +34,7 @@ android:key="SETTINGS_MESSAGES_SENT_BY_BOT_PREFERENCE_KEY_2" android:title="@string/settings_messages_sent_by_bot" /> - + diff --git a/vector/src/main/res/xml/vector_settings_notifications.xml b/vector/src/main/res/xml/vector_settings_notifications.xml new file mode 100644 index 0000000000..325c5fe11e --- /dev/null +++ b/vector/src/main/res/xml/vector_settings_notifications.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/vector/src/main/res/xml/vector_settings_preferences_root.xml b/vector/src/main/res/xml/vector_settings_preferences_root.xml new file mode 100644 index 0000000000..968aa68c6b --- /dev/null +++ b/vector/src/main/res/xml/vector_settings_preferences_root.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file