| 
 | 1 | +import 'dart:io' show Platform;  | 
 | 2 | + | 
 | 3 | +import 'package:flutter/material.dart';  | 
 | 4 | + | 
 | 5 | +import 'package:package_info_plus/package_info_plus.dart';  | 
 | 6 | +import 'package:upgrader/upgrader.dart';  | 
 | 7 | +import 'package:url_launcher/url_launcher.dart';  | 
 | 8 | + | 
 | 9 | +/// A wrapper widget around [UpgradeAlert] that customizes the "Update now"  | 
 | 10 | +/// button behavior. It redirects users to the appropriate store based on the  | 
 | 11 | +/// installer source. If the installer is unknown (e.g., sideloaded APK), it  | 
 | 12 | +/// opens the project's GitHub Releases page.  | 
 | 13 | +class UpgradeDialog extends StatelessWidget {  | 
 | 14 | +  final Widget child;  | 
 | 15 | + | 
 | 16 | +  const UpgradeDialog({super.key, required this.child});  | 
 | 17 | + | 
 | 18 | +  @override  | 
 | 19 | +  Widget build(BuildContext context) {  | 
 | 20 | +    final upgrader = Upgrader(  | 
 | 21 | +      durationUntilAlertAgain: const Duration(hours: 48),  | 
 | 22 | +    );  | 
 | 23 | +    return UpgradeAlert(  | 
 | 24 | +      upgrader: upgrader,  | 
 | 25 | +      onUpdate: () {  | 
 | 26 | +        if (Platform.isAndroid) {  | 
 | 27 | +          _onUpdateAndroid();  | 
 | 28 | +          return false;  | 
 | 29 | +        } else {  | 
 | 30 | +          return true; // Return true to allow Upgrader's default behavior to proceed.  | 
 | 31 | +        }  | 
 | 32 | +      },  | 
 | 33 | +      child: child,  | 
 | 34 | +    );  | 
 | 35 | +  }  | 
 | 36 | + | 
 | 37 | +  // Handler for the "Update now" button on Android  | 
 | 38 | +  Future<bool> _onUpdateAndroid() async {  | 
 | 39 | +    // For Android and other platforms, resolve a custom URL.  | 
 | 40 | +    final uri = await _resolveUpdateUri();  | 
 | 41 | +    if (uri == null) return true;  | 
 | 42 | +    final can = await canLaunchUrl(uri);  | 
 | 43 | +    if (can) {  | 
 | 44 | +      await launchUrl(uri, mode: LaunchMode.externalApplication);  | 
 | 45 | +      return false;  | 
 | 46 | +    }  | 
 | 47 | +    return true;  | 
 | 48 | +  }  | 
 | 49 | + | 
 | 50 | +  // Decide which URL to open for the update action based on installer source.  | 
 | 51 | +  Future<Uri?> _resolveUpdateUri() async {  | 
 | 52 | +    try {  | 
 | 53 | +      final info = await PackageInfo.fromPlatform();  | 
 | 54 | +      final installer = info.installerStore;  | 
 | 55 | +      final packageName = info.packageName;  | 
 | 56 | +      if (Platform.isAndroid) {  | 
 | 57 | +        // Map known Android installer package names to their corresponding store URLs.  | 
 | 58 | +        final uri = _androidStoreUriFor(installer, packageName);  | 
 | 59 | +        if (uri != null) return uri;  | 
 | 60 | +      }  | 
 | 61 | +      // Fallback/update source for sideloaded APKs or unknown installers.  | 
 | 62 | +      // Use the public GitHub releases page for this project.  | 
 | 63 | +      return Uri.parse('https://github.com/mlcommons/mobile_app_open/releases');  | 
 | 64 | +    } catch (_) {  | 
 | 65 | +      // In case of any exception, fall back to the releases page.  | 
 | 66 | +      return Uri.parse('https://github.com/mlcommons/mobile_app_open/releases');  | 
 | 67 | +    }  | 
 | 68 | +  }  | 
 | 69 | + | 
 | 70 | +  Uri? _androidStoreUriFor(String? installer, String packageName) {  | 
 | 71 | +    switch (installer) {  | 
 | 72 | +      // Google Play Store  | 
 | 73 | +      case 'com.android.vending':  | 
 | 74 | +        return Uri.parse(  | 
 | 75 | +            'https://play.google.com/store/apps/details?id=$packageName');  | 
 | 76 | +      // Amazon Appstore  | 
 | 77 | +      case 'com.amazon.venezia':  | 
 | 78 | +        return Uri.parse(  | 
 | 79 | +            'https://www.amazon.com/gp/mas/dl/android?p=$packageName');  | 
 | 80 | +      // Samsung Galaxy Store  | 
 | 81 | +      case 'com.sec.android.app.samsungapps':  | 
 | 82 | +        return Uri.parse(  | 
 | 83 | +            'https://apps.samsung.com/appquery/appDetail.as?appId=$packageName');  | 
 | 84 | +      // Huawei AppGallery (best-effort web link; may require app-specific ID)  | 
 | 85 | +      case 'com.huawei.appmarket':  | 
 | 86 | +        return Uri.parse('https://appgallery.huawei.com/#/app/$packageName');  | 
 | 87 | +      // Xiaomi GetApps  | 
 | 88 | +      case 'com.xiaomi.mipicks':  | 
 | 89 | +        return Uri.parse('https://global.app.mi.com/details?id=$packageName');  | 
 | 90 | +      // Oppo App Market  | 
 | 91 | +      case 'com.oppo.market':  | 
 | 92 | +        return Uri.parse(  | 
 | 93 | +            'https://store.heytap.com/app/details?id=$packageName');  | 
 | 94 | +      // Vivo App Store  | 
 | 95 | +      case 'com.bbk.appstore':  | 
 | 96 | +        return Uri.parse('https://appstore.vivo.com.cn/appdetail/$packageName');  | 
 | 97 | +      default:  | 
 | 98 | +        return null;  | 
 | 99 | +    }  | 
 | 100 | +  }  | 
 | 101 | +}  | 
0 commit comments