Skip to content

Commit 557007a

Browse files
committed
Ability to replace Firebase services + HMS support
Pass `tgx.extension=hms` to build an app version with HMS. By default none of Huawei dependencies are used, even at compilation level. `SimplePush` support (in future) can be done by creating an extension in `extension/simplepush`.
1 parent 39e771c commit 557007a

File tree

19 files changed

+397
-57
lines changed

19 files changed

+397
-57
lines changed

app/build.gradle.kts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ data class PullRequest (
5757
)
5858
}
5959

60+
apply(from = "${rootProject.projectDir}/properties.gradle.kts")
61+
val extensionName = extra["extension"] as String
62+
val isHuawei = extra["huawei"] == true
63+
6064
android {
6165
namespace = "org.thunderdog.challegram"
6266

@@ -287,7 +291,11 @@ android {
287291
create(variant.flavor) {
288292
dimension = "abi"
289293
versionCode = (abi + 1)
290-
minSdk = variant.minSdkVersion
294+
minSdk = if (isHuawei) {
295+
maxOf(variant.minSdkVersion, Config.MIN_SDK_VERSION_HUAWEI)
296+
} else {
297+
variant.minSdkVersion
298+
}
291299
val ndkVersionKey = if (variant.is64Bit) {
292300
"version.ndk_primary"
293301
} else {
@@ -376,6 +384,7 @@ gradle.projectsEvaluated {
376384
}
377385

378386
dependencies {
387+
implementation(project(":extension:${extensionName}"))
379388
// TDLib: https://github.com/tdlib/td/blob/master/CHANGELOG.md
380389
implementation(project(":tdlib"))
381390
implementation(project(":vkryl:core"))

app/src/google/java/org/thunderdog/challegram/service/FirebaseListenerService.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,36 @@
2727
import org.thunderdog.challegram.telegram.TdlibAccount;
2828
import org.thunderdog.challegram.telegram.TdlibManager;
2929
import org.thunderdog.challegram.tool.UI;
30-
import org.thunderdog.challegram.unsorted.Settings;
3130

3231
import java.util.HashMap;
3332
import java.util.Map;
3433

35-
import tgx.td.JSON;
34+
import tgx.bridge.PushReceiverBridge;
3635

3736
public final class FirebaseListenerService extends FirebaseMessagingService {
3837
@Override
3938
public void onNewToken (@NonNull String newToken) {
40-
UI.initApp(getApplicationContext());
41-
TDLib.Tag.notifications("onNewToken %s, sending to all accounts", newToken);
42-
TdlibManager.instance().runWithWakeLock(manager -> {
43-
manager.setDeviceToken(new TdApi.DeviceTokenFirebaseCloudMessaging(newToken, true));
44-
});
39+
PushReceiverBridge.onNewToken(this, new TdApi.DeviceTokenFirebaseCloudMessaging(newToken, true));
4540
}
4641

4742
@Override
48-
public void onDeletedMessages () {
49-
UI.initApp(getApplicationContext());
50-
TDLib.Tag.notifications("onDeletedMessages: performing sync for all accounts");
51-
TdlibManager.makeSync(getApplicationContext(), TdlibAccount.NO_ID, TdlibManager.SYNC_CAUSE_DELETED_MESSAGES, 0, !TdlibManager.inUiThread(), 0);
43+
public void onMessageReceived (@NonNull RemoteMessage remoteMessage) {
44+
final Map<String, Object> payload = makePayload(remoteMessage);
45+
final long sentTime = remoteMessage.getSentTime();
46+
final int ttl = remoteMessage.getTtl();
47+
PushReceiverBridge.onMessageReceived(this, payload, sentTime, ttl);
5248
}
5349

5450
@Override
55-
public void onMessageReceived (@NonNull RemoteMessage remoteMessage) {
51+
public void onDeletedMessages () {
5652
UI.initApp(getApplicationContext());
57-
58-
final String payload = makePayload(remoteMessage);
59-
final long sentTime = remoteMessage.getSentTime();
60-
final long pushId = Settings.instance().newPushId();
61-
62-
PushProcessor pushProcessor = new PushProcessor(this);
63-
pushProcessor.processPush(pushId, payload, sentTime, remoteMessage.getTtl());
53+
TDLib.Tag.notifications("onDeletedMessages: performing sync for all accounts");
54+
TdlibManager.makeSync(getApplicationContext(), TdlibAccount.NO_ID, TdlibManager.SYNC_CAUSE_DELETED_MESSAGES, 0, !TdlibManager.inUiThread(), 0);
6455
}
6556

6657
// Utils
6758

68-
private static String makePayload (final RemoteMessage remoteMessage) {
59+
private static Map<String, Object> makePayload (final RemoteMessage remoteMessage) {
6960
Map<String, Object> payload = new HashMap<>();
7061
payload.put("google.sent_time", remoteMessage.getSentTime());
7162
RemoteMessage.Notification notification = remoteMessage.getNotification();
@@ -86,6 +77,6 @@ private static String makePayload (final RemoteMessage remoteMessage) {
8677
if (data != null) {
8778
payload.putAll(data);
8879
}
89-
return JSON.stringify(payload);
80+
return payload;
9081
}
9182
}

app/src/main/java/org/thunderdog/challegram/BaseApplication.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is a part of Telegram X
3+
* Copyright © 2014 (tgx-android@pm.me)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* You should have received a copy of the GNU General Public License
11+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
12+
*
13+
* File created on 05/04/2015 at 08:53
14+
*/
15+
package org.thunderdog.challegram
16+
17+
import androidx.multidex.MultiDexApplication
18+
import androidx.work.Configuration
19+
import com.google.firebase.messaging.FirebaseMessaging
20+
import org.thunderdog.challegram.service.PushHandler
21+
import org.thunderdog.challegram.tool.UI
22+
import tgx.bridge.PushReceiverBridge
23+
import tgx.extension.TelegramXExtension
24+
25+
class BaseApplication : MultiDexApplication(), Configuration.Provider {
26+
override fun onCreate() {
27+
super.onCreate()
28+
29+
PushReceiverBridge.registerReceiver(PushHandler())
30+
UI.initApp(applicationContext)
31+
32+
val googlePlayServicesAvailable = U.isGooglePlayServicesAvailable(applicationContext)
33+
34+
TelegramXExtension.configure(this, googlePlayServicesAvailable)
35+
if (TelegramXExtension.shouldDisableFirebaseMessaging(googlePlayServicesAvailable)) {
36+
FirebaseMessaging.getInstance().isAutoInitEnabled = false
37+
}
38+
}
39+
40+
override val workManagerConfiguration: Configuration
41+
get() = Configuration.Builder().build()
42+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.thunderdog.challegram.service;
2+
3+
import android.app.Service;
4+
5+
import org.drinkless.tdlib.TdApi;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.thunderdog.challegram.TDLib;
8+
import org.thunderdog.challegram.telegram.TdlibManager;
9+
import org.thunderdog.challegram.tool.UI;
10+
import org.thunderdog.challegram.unsorted.Settings;
11+
12+
import java.util.Map;
13+
14+
import tgx.bridge.PushReceiver;
15+
import tgx.td.JSON;
16+
17+
public final class PushHandler implements PushReceiver {
18+
@Override
19+
public void onNewToken (@NotNull Service service, @NotNull TdApi.DeviceToken token) {
20+
UI.initApp(service.getApplicationContext());
21+
TDLib.Tag.notifications("onNewToken %s, sending to all accounts", token);
22+
TdlibManager.instance().runWithWakeLock(manager -> {
23+
manager.setDeviceToken(token);
24+
});
25+
}
26+
27+
@Override
28+
public void onMessageReceived (@NotNull Service service, @NotNull Map<@NotNull String, ?> message, long sentTime, int ttl) {
29+
UI.initApp(service.getApplicationContext());
30+
final long pushId = Settings.instance().newPushId();
31+
32+
String payload = JSON.stringify(message);
33+
34+
PushProcessor pushProcessor = new PushProcessor(service);
35+
pushProcessor.processPush(pushId, payload, sentTime, ttl);
36+
}
37+
}

buildSrc/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ plugins {
44
`kotlin-dsl`
55
}
66

7+
apply(from = "${rootDir.parentFile}/properties.gradle.kts")
8+
79
java {
810
toolchain {
911
languageVersion = JavaLanguageVersion.of(21)
@@ -36,6 +38,9 @@ gradlePlugin {
3638
repositories {
3739
google()
3840
mavenCentral()
41+
if (extra["huawei"] == true) {
42+
maven(url = "https://developer.huawei.com/repo/")
43+
}
3944
}
4045

4146
dependencies {
@@ -46,4 +51,10 @@ dependencies {
4651
implementation("com.squareup.okhttp3:okhttp:4.12.0")
4752
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")
4853
implementation("com.beust:klaxon:5.6")
54+
}
55+
56+
if (extra["huawei"] == true) {
57+
dependencies {
58+
implementation("com.huawei.agconnect:agcp:1.9.3.302")
59+
}
4960
}

buildSrc/src/main/kotlin/Config.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import tgx.gradle.fatal
1818
object Config {
1919
const val PRIMARY_SDK_VERSION = 21
2020
const val MIN_SDK_VERSION = 16
21+
const val MIN_SDK_VERSION_HUAWEI = 17
2122
val JAVA_VERSION = org.gradle.api.JavaVersion.VERSION_21
2223
val ANDROIDX_MEDIA_EXTENSIONS = arrayOf("decoder_ffmpeg", "decoder_flac", "decoder_opus", "decoder_vp9")
2324
val SUPPORTED_ABI = arrayOf("armeabi-v7a", "arm64-v8a", "x86_64", "x86")
@@ -33,6 +34,7 @@ object LibraryVersions {
3334
const val ANNOTATIONS = "1.9.1"
3435
const val ANDROIDX_MEDIA = "1.4.1" // "1.5.0"+ requires minSdk 21+
3536
const val ANDROIDX_CAMERA = "1.4.2"
37+
const val HUAWEI_SERVICES = "6.13.0.300"
3638
}
3739

3840
class AbiVariant (val flavor: String, vararg val filters: String = arrayOf(), val displayName: String = filters[0]) {

extension/bridge/build.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
id("com.android.library")
3+
id("module-plugin")
4+
}
5+
6+
dependencies {
7+
implementation("androidx.core:core-ktx:${LibraryVersions.ANDROIDX_CORE}")
8+
implementation(project(":tdlib"))
9+
}
10+
11+
android {
12+
namespace = "tgx.bridge"
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tgx.bridge
2+
3+
import android.app.Application
4+
5+
open class Extension(@JvmField val name: String) {
6+
open fun configure(application: Application, googlePlayServicesAvailable: Boolean) { }
7+
open fun shouldDisableFirebaseMessaging(googlePlayServicesAvailable: Boolean): Boolean =
8+
false
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package tgx.bridge
2+
3+
import android.app.Service
4+
import org.drinkless.tdlib.TdApi.DeviceToken
5+
6+
interface PushReceiver {
7+
fun onNewToken (service: Service, token: DeviceToken)
8+
fun onMessageReceived (service: Service, message: Map<String, Any>, sentTime: Long, ttl: Int)
9+
}
10+
11+
object PushReceiverBridge {
12+
private var receiver: PushReceiver? = null
13+
14+
@JvmStatic fun registerReceiver (receiver: PushReceiver) {
15+
this.receiver = receiver
16+
}
17+
18+
@JvmStatic fun onNewToken (service: Service, token: DeviceToken) {
19+
receiver!!.onNewToken(service, token)
20+
}
21+
22+
@JvmStatic fun onMessageReceived (service: Service, payload: Map<String, Any>, sentTime: Long, ttl: Int) {
23+
receiver!!.onMessageReceived(service, payload, sentTime, ttl)
24+
}
25+
}

0 commit comments

Comments
 (0)