Skip to content

Commit

Permalink
Internal support for DeviceTokenHuaweiPush and DeviceTokenSimplePush
Browse files Browse the repository at this point in the history
  • Loading branch information
vkryl committed Dec 25, 2023
1 parent 4dbccfe commit 55f98a9
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* File created on 22/10/2022
*/
package org.thunderdog.challegram;
package org.thunderdog.challegram.location;

import android.location.Location;

Expand All @@ -26,6 +26,8 @@
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStatusCodes;

import org.thunderdog.challegram.BaseActivity;
import org.thunderdog.challegram.Log;
import org.thunderdog.challegram.tool.Intents;
import org.thunderdog.challegram.tool.UI;
import org.thunderdog.challegram.unsorted.LocationRetriever;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
*
* File created on 22/10/2022
*/
package org.thunderdog.challegram;
package org.thunderdog.challegram.location;

import org.thunderdog.challegram.BaseActivity;
import org.thunderdog.challegram.unsorted.LocationRetriever;

public class LocationRetrieverFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,29 @@
*
* File created on 22/10/2022
*/
package org.thunderdog.challegram;
package org.thunderdog.challegram.push;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;

import org.drinkless.tdlib.TdApi;
import org.thunderdog.challegram.Log;
import org.thunderdog.challegram.TDLib;
import org.thunderdog.challegram.tool.UI;
import org.thunderdog.challegram.util.TokenRetriever;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import me.vkryl.core.StringUtils;

public final class FirebaseTokenRetriever extends TokenRetriever {
final class FirebaseTokenRetriever extends TokenRetriever {
@Override
protected boolean performInitialization (Context context) {
try {
Expand All @@ -44,6 +51,19 @@ protected boolean performInitialization (Context context) {
return false;
}

@NonNull
@Override
public String getName () {
return "firebase";
}

@Override
@Nullable
public String getConfiguration () {
FirebaseOptions options = FirebaseOptions.fromResource(UI.getAppContext());
return options != null ? options.toString() : null;
}

private static String extractFirebaseErrorName (Throwable e) {
String message = e.getMessage();
if (!StringUtils.isEmpty(message)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
*
* File created on 22/10/2022
*/
package org.thunderdog.challegram;
package org.thunderdog.challegram.push;

import android.content.Context;

import org.thunderdog.challegram.util.TokenRetriever;

public class TokenRetrieverFactory {
public final class TokenRetrieverFactory {
public static TokenRetriever newRetriever (Context context) {
return new FirebaseTokenRetriever();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import me.vkryl.td.JSON;

public class FirebaseListenerService extends FirebaseMessagingService {
public final class FirebaseListenerService extends FirebaseMessagingService {
@Override
public void onNewToken (@NonNull String newToken) {
UI.initApp(getApplicationContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.thunderdog.challegram.telegram.Tdlib;

public class MapControllerFactory {
public final class MapControllerFactory {
public static MapController<?,?> newMapController (Context context, Tdlib tdlib) {
return new MapGoogleController(context, tdlib);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
import me.vkryl.core.lambda.Destroyable;
import me.vkryl.td.MessageId;

public class MapGoogleController extends MapController<MapView, MapGoogleController.MarkerData> implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener, GoogleMap.OnCameraMoveStartedListener, GoogleMap.OnMarkerClickListener {
final class MapGoogleController extends MapController<MapView, MapGoogleController.MarkerData> implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener, GoogleMap.OnCameraMoveStartedListener, GoogleMap.OnMarkerClickListener {
private static final float DEFAULT_ZOOM_LEVEL = 16.0f;
private static final float CLICK_ZOOM_LEVEL = 17.0f;

Expand Down
25 changes: 16 additions & 9 deletions app/src/main/java/org/thunderdog/challegram/telegram/Tdlib.java
Original file line number Diff line number Diff line change
Expand Up @@ -5757,31 +5757,38 @@ private Map<String, Object> newConnectionParams () {
if (deviceToken != null && (state == TdlibManager.TokenState.NONE || state == TdlibManager.TokenState.INITIALIZING)) {
state = TdlibManager.TokenState.OK;
}
String tokenProvider = TdlibNotificationUtils.getTokenRetriever().getName();
String error = context().getTokenError();
switch (state) {
case TdlibManager.TokenState.ERROR: {
params.put("device_token", "FIREBASE_ERROR");
params.put("device_token", tokenProvider.toUpperCase() + "_ERROR");
if (!StringUtils.isEmpty(error)) {
params.put("firebase_error", error);
params.put(tokenProvider + "_error", error);
}
break;
}
case TdlibManager.TokenState.INITIALIZING: {
params.put("device_token", "FIREBASE_INITIALIZING");
params.put("device_token", tokenProvider.toUpperCase() + "_INITIALIZING");
break;
}
case TdlibManager.TokenState.OK: {
String tokenOrEndpoint;
switch (deviceToken.getConstructor()) {
// TODO more push services
case TdApi.DeviceTokenFirebaseCloudMessaging.CONSTRUCTOR: {
String token = ((TdApi.DeviceTokenFirebaseCloudMessaging) deviceToken).token;
params.put("device_token", token);
case TdApi.DeviceTokenFirebaseCloudMessaging.CONSTRUCTOR:
tokenOrEndpoint = ((TdApi.DeviceTokenFirebaseCloudMessaging) deviceToken).token;
break;
case TdApi.DeviceTokenHuaweiPush.CONSTRUCTOR:
tokenOrEndpoint = ((TdApi.DeviceTokenHuaweiPush) deviceToken).token;
break;
case TdApi.DeviceTokenSimplePush.CONSTRUCTOR:
tokenOrEndpoint = ((TdApi.DeviceTokenSimplePush) deviceToken).endpoint;
break;
}
default: {
throw new UnsupportedOperationException(deviceToken.toString());
Td.assertDeviceToken_de4a4f61();
throw Td.unsupported(deviceToken);
}
}
params.put("device_token", tokenOrEndpoint);
break;
}
case TdlibManager.TokenState.NONE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
import org.thunderdog.challegram.Log;
import org.thunderdog.challegram.R;
import org.thunderdog.challegram.TDLib;
import org.thunderdog.challegram.TokenRetrieverFactory;
import org.thunderdog.challegram.U;
import org.thunderdog.challegram.config.Config;
import org.thunderdog.challegram.config.Device;
import org.thunderdog.challegram.data.TD;
import org.thunderdog.challegram.loader.ImageCache;
import org.thunderdog.challegram.loader.ImageFile;
import org.thunderdog.challegram.loader.ImageReader;
import org.thunderdog.challegram.push.TokenRetrieverFactory;
import org.thunderdog.challegram.theme.ColorId;
import org.thunderdog.challegram.theme.Theme;
import org.thunderdog.challegram.theme.ThemeId;
Expand All @@ -53,6 +53,8 @@
import org.thunderdog.challegram.util.TokenRetriever;
import org.thunderdog.challegram.util.text.Letters;

import me.vkryl.td.Td;

public class TdlibNotificationUtils {
private static TextPaint lettersPaint;
private static TextPaint lettersPaintFake;
Expand Down Expand Up @@ -252,14 +254,25 @@ public static synchronized boolean initialize () {
return tokenRetriever.initialize(UI.getAppContext());
}

public static @NonNull TokenRetriever getTokenRetriever () {
if (tokenRetriever == null) {
initialize();
}
return tokenRetriever;
}

@DeviceTokenType
public static int getDeviceTokenType (TdApi.DeviceToken deviceToken) {
switch (deviceToken.getConstructor()) {
// TODO more push services
case TdApi.DeviceTokenFirebaseCloudMessaging.CONSTRUCTOR:
return DeviceTokenType.FIREBASE_CLOUD_MESSAGING;
case TdApi.DeviceTokenHuaweiPush.CONSTRUCTOR:
return DeviceTokenType.HUAWEI_PUSH_SERVICE;
case TdApi.DeviceTokenSimplePush.CONSTRUCTOR:
return DeviceTokenType.SIMPLE_PUSH_SERVICE;
default:
throw new UnsupportedOperationException(deviceToken.toString());
Td.assertDeviceToken_de4a4f61();
throw Td.unsupported(deviceToken);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class TdlibSettingsManager implements CleanupStartupDelegate {
private static final String NOTIFICATION_DATA_PREFIX = "notification_data_";
public static final String CONVERSION_PREFIX = "pending_conversion_";

public static final String DEVICE_TOKEN_KEY = "registered_device_token";
public static final String DEVICE_TOKEN_OR_ENDPOINT_KEY = "registered_device_token";
public static final String DEVICE_TOKEN_TYPE_KEY = "registered_device_token_type";
public static final String DEVICE_UID_KEY = "registered_device_uid";
public static final String DEVICE_OTHER_UID_KEY = "registered_device_uid_other";
Expand Down Expand Up @@ -616,49 +616,32 @@ private static String getRegisteredDeviceTdlibVersion2 (int accountId) {
@Nullable
private static TdApi.DeviceToken getRegisteredDeviceToken (int accountId) {
@DeviceTokenType int tokenType = Settings.instance().getInt(key(DEVICE_TOKEN_TYPE_KEY, accountId), DeviceTokenType.FIREBASE_CLOUD_MESSAGING);
switch (tokenType) {
case DeviceTokenType.FIREBASE_CLOUD_MESSAGING:
default: {
String token = Settings.instance().getString(key(DEVICE_TOKEN_KEY, accountId), null);
if (!StringUtils.isEmpty(token)) {
return new TdApi.DeviceTokenFirebaseCloudMessaging(token, true);
}
break;
}
}
return null;
String tokenOrEndpoint = Settings.instance().getString(key(DEVICE_TOKEN_OR_ENDPOINT_KEY, accountId), null);
return Settings.newDeviceToken(tokenType, tokenOrEndpoint);
}

private static long[] getRegisteredDeviceOtherUserIds (int accountId) {
return Settings.instance().pmc().getLongArray(key(DEVICE_OTHER_UID_KEY, accountId));
}

public static void setRegisteredDevice (int accountId, long userId, TdApi.DeviceToken deviceToken, @Nullable long[] otherUserIds) {
public static void setRegisteredDevice (int accountId, long userId, @Nullable TdApi.DeviceToken deviceToken, @Nullable long[] otherUserIds) {
if (deviceToken == null) {
unregisterDevice(accountId);
return;
}
LevelDB pmc = Settings.instance().edit();
Settings.storeDeviceToken(deviceToken, pmc,
key(DEVICE_TOKEN_TYPE_KEY, accountId),
key(DEVICE_TOKEN_OR_ENDPOINT_KEY, accountId)
);
pmc.putLong(key(DEVICE_UID_KEY, accountId), userId);
pmc.putString(key(DEVICE_TDLIB_VERSION2_KEY, accountId), BuildConfig.TDLIB_VERSION);
if (otherUserIds != null && otherUserIds.length > 0) {
pmc.putLongArray(key(DEVICE_OTHER_UID_KEY, accountId), otherUserIds);
} else {
int tokenType = TdlibNotificationUtils.getDeviceTokenType(deviceToken);
LevelDB pmc = Settings.instance().edit();
switch (deviceToken.getConstructor()) {
case TdApi.DeviceTokenFirebaseCloudMessaging.CONSTRUCTOR: {
String token = ((TdApi.DeviceTokenFirebaseCloudMessaging) deviceToken).token;
pmc.putInt(key(DEVICE_TOKEN_TYPE_KEY, accountId), tokenType)
.putString(key(DEVICE_TOKEN_KEY, accountId), token);
break;
}
default: {
throw new UnsupportedOperationException(deviceToken.toString());
}
}
pmc.putLong(key(DEVICE_UID_KEY, accountId), userId);
pmc.putString(key(DEVICE_TDLIB_VERSION2_KEY, accountId), BuildConfig.TDLIB_VERSION);
if (otherUserIds != null && otherUserIds.length > 0) {
pmc.putLongArray(key(DEVICE_OTHER_UID_KEY, accountId), otherUserIds);
} else {
pmc.remove(key(DEVICE_OTHER_UID_KEY, accountId));
}
pmc.apply();
pmc.remove(key(DEVICE_OTHER_UID_KEY, accountId));
}
pmc.apply();
}

public static boolean checkRegisteredDeviceToken (int accountId, long userId, TdApi.DeviceToken token, long[] otherUserIds, boolean skipOtherUserIdsCheck) {
Expand All @@ -671,7 +654,7 @@ public static boolean checkRegisteredDeviceToken (int accountId, long userId, Td

public static void unregisterDevice (int accountId) {
Settings.instance().edit()
.remove(key(DEVICE_TOKEN_KEY, accountId))
.remove(key(DEVICE_TOKEN_OR_ENDPOINT_KEY, accountId))
.remove(key(DEVICE_TOKEN_TYPE_KEY, accountId))
.remove(key(DEVICE_UID_KEY, accountId))
.remove(key(DEVICE_OTHER_UID_KEY, accountId))
Expand Down
Loading

0 comments on commit 55f98a9

Please sign in to comment.