getInstalledUPIApps function

Future<List<UPIApp>> getInstalledUPIApps({
  1. required List<String> allowedPspApps,
  2. required bool isGatewayPhonePe,
})

Implementation

Future<List<UPIApp>> getInstalledUPIApps({
  required List<String> allowedPspApps,
  required bool isGatewayPhonePe,
}) async {
  final List<UPIApp> installedApps = [];
  var pspApps = allowedPspApps;

  final allUpiApps = [..._allUpiApps, if (isGatewayPhonePe) _phonePeSimulator];

  if (buildConfig.allowAllUPIApps) {
    pspApps = allUpiApps.map((app) => app.backendName).toList();
  }

  for (final app in allUpiApps) {
    final isEnabled = pspApps.contains(app.backendName);

    if (kDebugMode) {
      Logger.log('App ${app.name}: ${isEnabled ? 'enabled' : 'disabled'}');
    }
    if (!isEnabled) continue;

    var isInstalled = false;
    if (PlatformUtils.isAndroid) {
      final intent = AndroidIntent(
        action: 'action_view',
        data: 'upi://pay',
        package: app.androidPackageName,
      );
      isInstalled = (await intent.canResolveActivity()) ?? false;
    }

    if (PlatformUtils.isIOS) {
      isInstalled = await canLaunchUrl(Uri.parse('${app.deeplinkScheme}://'));
    }

    if (PlatformUtils.isMobileWeb) {
      isInstalled = true;
    }

    if (kDebugMode) {
      Logger.log(
          '${app.androidPackageName} is ${isInstalled ? 'installed' : 'not installed'}');
    }

    if (isInstalled && isEnabled) {
      installedApps.add(app);
    }
  }

  if (kDebugMode) {
    Logger.log('Installed apps $installedApps');
  }

  return installedApps;
}