public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
            case Resource.Id.menu_refresh:
                Finish();
                StartActivity(typeof(MainActivity));
                OverridePendingTransition(Resource.Animation.abc_fade_in, Resource.Animation.abc_fade_out);
                if (CompareVersionReceiver.VersionCompare(installedWhatsAppVersion, latestWhatsAppVersion) < 0)
                {
                    Toast.MakeText(this, string.Format(Resources.GetString(Resource.String.available), "WhatsApp " + latestWhatsAppVersion), ToastLength.Long).Show();
                }
                else
                {
                    Toast.MakeText(this, string.Format(Resources.GetString(Resource.String.latest_installed_description), "WhatsApp " + installedWhatsAppVersion), ToastLength.Long).Show();
                }
                return(true);

            case Resource.Id.menu_settings:
                Finish();
                StartActivity(typeof(SettingsActivity));
                return(true);

            case Resource.Id.menu_share:
                Intent shareIntent = new Intent(Intent.ActionSend);
                shareIntent.SetType("text/plain");
                shareIntent.PutExtra(Intent.ExtraText, string.Format(Resources.GetString(Resource.String.share_description), Resources.GetString(Resource.String.app_name)) + ": WhatsApp " + latestWhatsAppVersion + " " + "http://bit.ly/BetaUpdaterAndroid");
                StartActivity(Intent.CreateChooser(shareIntent, Resources.GetString(Resource.String.share)));
                return(true);
            }
            return(base.OnOptionsItemSelected(item));
        }
Exemplo n.º 2
0
 public static bool IsAvailableVersion(Context context)
 {
     if (CompareVersionReceiver.VersionCompare(GetInstalledVersion(context), GetLatestVersion()) < 0)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
        // Retrieve latest version of Beta Updater
        public async Task GetLatestAppVersion(string pageUrl)
        {
            var    getVersion  = new WebClient();
            string htmlAndroid = getVersion.DownloadString(new Uri(pageUrl));

            // Get WhatsApp latest version
            string[] split = htmlAndroid.Split(new char[] { '>' });
            int      i     = 0;

            while (i < split.Length)
            {
                if (split.GetValue(i).ToString().StartsWith("v"))
                {
                    split            = split.GetValue(i).ToString().Split(new char[] { 'v', '<' });
                    latestAppVersion = split.GetValue(1).ToString().Trim();
                    break;
                }
                i++;
            }

            installedAppVersion = PackageManager.GetPackageInfo("com.javiersantos.whatsappbetaupdater", 0).VersionName.Trim();

            if (CompareVersionReceiver.VersionCompare(installedAppVersion, latestAppVersion) < 0)
            {
                appApk = appApk + "v" + latestAppVersion + "/com.javiersantos.whatsappbetaupdater.apk";

                AlertDialog appUpdateDialog = new AlertDialog.Builder(this).Create();
                appUpdateDialog.SetTitle(string.Format(Resources.GetString(Resource.String.app_update), latestAppVersion));
                appUpdateDialog.SetMessage(string.Format(Resources.GetString(Resource.String.app_update_description), Resources.GetString(Resource.String.app_name)));
                appUpdateDialog.SetButton((int)DialogButtonType.Positive, Resources.GetString(Resource.String.update_button), (object senderUpdateAppOK, DialogClickEventArgs eUpdateAppOK) => {
                    SetDownloadDialog();
                    var webClient = new WebClient();
                    webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgressChanged);
                    webClient.DownloadFileCompleted   += new AsyncCompletedEventHandler(downloadAppFileCompleted);
                    webClient.DownloadFileAsync(new Uri(appApk), fullLatestAppFilename);
                    progressDialog.SetTitle(string.Format(Resources.GetString(Resource.String.downloading), Resources.GetString(Resource.String.app_name) + " " + latestAppVersion + "..."));
                    progressDialog.SetButton(Resources.GetString(Resource.String.cancel_button), (object senderCancel, DialogClickEventArgs eCancel) => { webClient.CancelAsync(); progressDialog.Dismiss(); });
                    progressDialog.Show();
                });
                appUpdateDialog.SetButton((int)DialogButtonType.Negative, Resources.GetString(Resource.String.cancel_button), (object senderUpdateAppCancel, DialogClickEventArgs eUpdateAppCancel) => appUpdateDialog.Dismiss());
                appUpdateDialog.SetButton((int)DialogButtonType.Neutral, Resources.GetString(Resource.String.never_button), (object senderUpdateAppNever, DialogClickEventArgs eUpdateAppNever) => { prefs.Edit().PutBoolean("prefShowAppUpdates", false).Commit(); appUpdateDialog.Dismiss(); });
                appUpdateDialog.SetCancelable(false);
                appUpdateDialog.Show();
            }
        }
        // Retrieve latest version of WhatsApp
        public async Task GetLatestWhatsAppVersion(string pageUrl)
        {
            var    getVersion  = new WebClient();
            string htmlAndroid = getVersion.DownloadString(new Uri(pageUrl));

            // Get WhatsApp latest version
            string[] split = htmlAndroid.Split(new char[] { '>' });
            int      i     = 0;

            while (i < split.Length)
            {
                if (split.GetValue(i).ToString().StartsWith("Version"))
                {
                    split = split.GetValue(i).ToString().Split(new char[] { ' ', '<' });
                    latestWhatsAppVersion = split.GetValue(1).ToString().Trim();
                    break;
                }
                i++;
            }

            // Display WhatsApp installed and latest version
            TextView whatsapp_installed_version = FindViewById <TextView> (Resource.Id.whatsapp_installed_version);
            TextView whatsapp_latest_version    = FindViewById <TextView> (Resource.Id.whatsapp_latest_version);

            installedWhatsAppVersion = PackageManager.GetPackageInfo("com.whatsapp", 0).VersionName.Trim();

            whatsapp_installed_version.Text = installedWhatsAppVersion;
            whatsapp_latest_version.Text    = latestWhatsAppVersion;

            fullLatestWhatsAppFilename = filename + "WhatsApp_" + latestWhatsAppVersion + ".apk";

            // Load Floating Button
            var fab = FindViewById <FloatingActionButton> (Resource.Id.fab);

            // Compare installed and latest WhatsApp version
            if (CompareVersionReceiver.VersionCompare(installedWhatsAppVersion, latestWhatsAppVersion) < 0)               // There is a new version
            {
                fab.SetImageDrawable(Resources.GetDrawable(Resource.Drawable.ic_download));
                // Preference: Autodownload
                if (prefAutoDownload)
                {
                    SetDownloadDialog();
                    var webClient = new WebClient();
                    webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgressChanged);
                    webClient.DownloadFileCompleted   += new AsyncCompletedEventHandler(downloadWhatsAppFileCompleted);
                    webClient.DownloadFileAsync(new Uri(whatsAppApk), fullLatestWhatsAppFilename);
                    progressDialog.SetTitle(string.Format(Resources.GetString(Resource.String.downloading), "WhatsApp " + latestWhatsAppVersion + "..."));
                    progressDialog.SetButton(Resources.GetString(Resource.String.cancel_button), (object senderCancel, DialogClickEventArgs eCancel) => { webClient.CancelAsync(); progressDialog.Dismiss(); });
                    progressDialog.Show();
                }
                else
                {
                    fab.Click += delegate {
                        SetDownloadDialog();
                        var webClient = new WebClient();
                        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgressChanged);
                        webClient.DownloadFileCompleted   += new AsyncCompletedEventHandler(downloadWhatsAppFileCompleted);
                        webClient.DownloadFileAsync(new Uri(whatsAppApk), fullLatestWhatsAppFilename);
                        progressDialog.SetTitle(string.Format(Resources.GetString(Resource.String.downloading), "WhatsApp " + latestWhatsAppVersion + "..."));
                        progressDialog.SetButton(Resources.GetString(Resource.String.cancel_button), (object senderCancel, DialogClickEventArgs eCancel) => { webClient.CancelAsync(); progressDialog.Dismiss(); });
                        progressDialog.Show();
                    };
                }
                // There is not a new version
            }
            else
            {
                fab.SetImageDrawable(Resources.GetDrawable(Resource.Drawable.ic_menu_about));
                fab.Click += delegate {
                    AlertDialog errorInstalled = new AlertDialog.Builder(this).Create();
                    errorInstalled.SetTitle(Resources.GetString(Resource.String.latest_installed));
                    errorInstalled.SetMessage(string.Format(Resources.GetString(Resource.String.latest_installed_description), "WhatsApp " + installedWhatsAppVersion));
                    errorInstalled.SetButton((int)DialogButtonType.Positive, Resources.GetString(Resource.String.ok), (object senderClose, DialogClickEventArgs eClose) => errorInstalled.Dismiss());
                    errorInstalled.Show();
                };
            }
        }