Skip to content

Commit

Permalink
- Fix for WalletConnect V2 Foreground Service (#3367)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesSmartCell authored Mar 25, 2024
1 parent 8511200 commit 6f15a60
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 118 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/alphawallet/app/C.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public abstract class C {
public static final String APP_FOREGROUND_STATE = "com.alphawallet.APP_FOREGROUND_STATE";
public static final String EXTRA_APP_FOREGROUND = "com.alphawallet.IS_FOREGORUND";
public static final String QRCODE_SCAN = "com.alphawallet.QRSCAN";
public static final String AWALLET_CODE = "com.alphawallet.AWALLET";
public static final String SIGNAL_NFT_SYNC = "com.alphawallet.SYNC_NFT";
public static final String SYNC_STATUS = "com.alphawallet.SYNC_STATUS";

Expand Down Expand Up @@ -278,6 +279,7 @@ public interface Key {
public static final String DAPP_SUFFIX_RECEIVE = "receive";
public static final String DAPP_PREFIX_MAPS = "maps.google.com/maps?daddr=";
public static final String DAPP_PREFIX_WALLETCONNECT = "wc";
public static final String DAPP_PREFIX_AWALLET = "awallet";

public static final String ENS_SCAN_BLOCK = "ens_check_block";
public static final String ENS_HISTORY = "ensHistory";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.alphawallet.app.entity.walletconnect.WalletConnectSessionItem;
import com.alphawallet.app.entity.walletconnect.WalletConnectV2SessionItem;
import com.alphawallet.app.service.RealmManager;
import com.walletconnect.web3.wallet.client.Wallet;
import com.walletconnect.web3.wallet.client.Web3Wallet;

Expand All @@ -15,12 +14,10 @@

public class WalletConnectInteract
{
private final RealmManager realmManager;

@Inject
public WalletConnectInteract(RealmManager realmManager)
public WalletConnectInteract()
{
this.realmManager = realmManager;

}

public int getSessionsCount()
Expand All @@ -30,15 +27,25 @@ public int getSessionsCount()

public List<WalletConnectSessionItem> getSessions()
{
List<WalletConnectSessionItem> result = new ArrayList<>();
result.addAll(getWalletConnectV2SessionItems());
List<WalletConnectSessionItem> result = new ArrayList<>(getWalletConnectV2SessionItems());

//now sort for active/newness
result.sort((l, r) -> Long.compare(r.expiryTime, l.expiryTime));

return result;
}

public void fetchSessions(SessionFetchCallback sessionFetchCallback)
{
fetch(sessionFetchCallback);
}

private void fetch(SessionFetchCallback sessionFetchCallback)
{
List<WalletConnectSessionItem> result = new ArrayList<>(getWalletConnectV2SessionItems());
sessionFetchCallback.onFetched(result);
}

private List<WalletConnectSessionItem> getWalletConnectV2SessionItems()
{
List<WalletConnectSessionItem> result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
Expand All @@ -21,6 +20,9 @@
@AndroidEntryPoint
public class WalletConnectV2Service extends Service
{
private static final String TAG = WalletConnectV2Service.class.getName();

final String CHANNEL_ID = "WalletConnectV2Service";
@Override
public IBinder onBind(Intent intent)
{
Expand All @@ -32,32 +34,38 @@ public IBinder onBind(Intent intent)
public void onCreate()
{
super.onCreate();
String CHANNEL_ID = "my_channel_01";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"WalletConnect V2",
NotificationManager.IMPORTANCE_DEFAULT);
}

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
private Notification createNotification()
{
Intent notificationIntent = new Intent(this, WalletConnectNotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

Intent intent = new Intent(getApplicationContext(), WalletConnectNotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_IMMUTABLE);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_logo)
.setOnlyAlertOnce(true)
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(getString(R.string.notify_wallet_connect_title))
.setContentText(getString(R.string.notify_wallet_connect_content))
.setSmallIcon(R.drawable.ic_logo)
.setContentIntent(pendingIntent)
.build();

startForeground(1, notification);
notificationManager.notify(1, notification);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel()
{
return super.onStartCommand(intent, flags, startId);
CharSequence name = getString(R.string.notify_wallet_connect_title);
String description = getString(R.string.notify_wallet_connect_content);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
createNotificationChannel();
Notification notification = createNotification();
startForeground(1, notification);
return START_STICKY;
}

@Override
Expand Down
16 changes: 11 additions & 5 deletions app/src/main/java/com/alphawallet/app/ui/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public void onPageScrollStateChanged(int state)

if (data != null)
{
checkIntents(data.toString(), intent);
handleDeeplink(data.toString(), intent);
}

Intent i = new Intent(this, PriceAlertsService.class);
Expand Down Expand Up @@ -453,6 +453,13 @@ private void setupFragmentListeners()
hideDialog();
qrCodeScanner.launch(options);
});

getSupportFragmentManager()
.setFragmentResultListener(C.AWALLET_CODE, this, (requestKey, b) ->
{
String code = b.getString(C.AWALLET_CODE);
handleDeeplink(code, null);
});
}

//TODO: Implement all QR scan using this method
Expand All @@ -476,7 +483,7 @@ public void onNewIntent(Intent startIntent)

if (data != null)
{
checkIntents(data.toString(), startIntent);
handleDeeplink(data.toString(), startIntent);
}
}

Expand Down Expand Up @@ -603,7 +610,7 @@ public void onSaveInstanceState(@NonNull Bundle savedInstanceState)
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
int oldPage = savedInstanceState.getInt(STORED_PAGE);
Expand Down Expand Up @@ -1095,9 +1102,8 @@ private void showSystemUI()
inset.show(WindowInsetsCompat.Type.statusBars() | WindowInsetsCompat.Type.navigationBars());
}

private void checkIntents(String importData, Intent startIntent)
private void handleDeeplink(String importData, Intent startIntent)
{
//decode deeplink and handle
DeepLinkRequest request = DeepLinkService.parseIntent(importData, startIntent);
switch (request.type)
{
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/alphawallet/app/ui/WalletFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public class WalletFragment extends BaseFragment implements
private ActivityResultLauncher<Intent> handleBackupClick;
private ActivityResultLauncher<Intent> tokenManagementLauncher;
private boolean completed = false;
private boolean hasWCSession = false;

@Inject
AWWalletConnectClient awWalletConnectClient;
Expand Down Expand Up @@ -270,6 +271,7 @@ private void initViewModel()
viewModel.removeDisplayTokens().observe(getViewLifecycleOwner(), this::removeTokens);
viewModel.getTokensService().startWalletSync(this);
viewModel.activeWalletConnectSessions().observe(getViewLifecycleOwner(), walletConnectSessionItems -> {
hasWCSession = !walletConnectSessionItems.isEmpty();
adapter.showActiveWalletConnectSessions(walletConnectSessionItems);
});
}
Expand Down Expand Up @@ -828,7 +830,7 @@ public void onWCClicked()
@Override
public boolean hasWCSession()
{
return awWalletConnectClient != null && awWalletConnectClient.hasWalletConnectSessions();
return hasWCSession || (awWalletConnectClient != null && awWalletConnectClient.hasWalletConnectSessions());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,7 @@ public List<TokenCardMeta> getSelected()

public void showActiveWalletConnectSessions(List<WalletConnectSessionItem> sessions)
{
if (sessions.isEmpty())
{
removeItem(WalletConnectSessionHolder.VIEW_TYPE);
}
else
{
items.add(new WalletConnectSessionSortedItem(sessions, 2));
}
checkWalletConnect();
}

public void removeItem(int viewType)
Expand Down
14 changes: 11 additions & 3 deletions app/src/main/java/com/alphawallet/app/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -115,7 +113,7 @@ public static int dp2px(Context context, int dp)

public static String formatUrl(String url)
{
if (URLUtil.isHttpsUrl(url) || URLUtil.isHttpUrl(url))
if (URLUtil.isHttpsUrl(url) || URLUtil.isHttpUrl(url) || isWalletPrefix(url))
{
return url;
}
Expand All @@ -132,6 +130,16 @@ public static String formatUrl(String url)
}
}

public static boolean isWalletPrefix(String url)
{
return url.startsWith(C.DAPP_PREFIX_TELEPHONE) ||
url.startsWith(C.DAPP_PREFIX_MAILTO) ||
url.startsWith(C.DAPP_PREFIX_ALPHAWALLET) ||
url.startsWith(C.DAPP_PREFIX_MAPS) ||
url.startsWith(C.DAPP_PREFIX_WALLETCONNECT) ||
url.startsWith(C.DAPP_PREFIX_AWALLET);
}

public static boolean isValidUrl(String url)
{
if (TextUtils.isEmpty(url)) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ public void updateTokenScriptViewSize(Token token, int itemViewHeight)
public void checkForNewScript(Token token)
{
if (token == null) return;
//check server for new tokenscript
//check server for new TokenScript
scriptUpdate = assetDefinitionService.checkServerForScript(token, scriptUpdateInProgress)
.observeOn(Schedulers.io())
.subscribeOn(Schedulers.single())
Expand Down
Loading

0 comments on commit 6f15a60

Please sign in to comment.