Skip to content

Commit

Permalink
Aded analytics events all pages, update interfaces and throwed some e…
Browse files Browse the repository at this point in the history
…rror in adb operation and also added multiple device install.
  • Loading branch information
Andronovo-bit committed Feb 26, 2024
1 parent 3a14640 commit 9ac29b0
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 87 deletions.
1 change: 1 addition & 0 deletions src/HuaweiHMSInstaller.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aptabase.Maui" Version="0.0.8" />
<PackageReference Include="CommunityToolkit.Maui" Version="7.0.1" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
Expand Down
40 changes: 16 additions & 24 deletions src/Pages/DownloadandInstallPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AdvancedSharpAdbClient;
using HuaweiHMSInstaller.Helper;
using HuaweiHMSInstaller.Integrations.Analytics;
using HuaweiHMSInstaller.Models;
using HuaweiHMSInstaller.Services;
using HuaweiHMSInstaller.ViewModels;
Expand Down Expand Up @@ -54,8 +55,10 @@ public partial class DownloadandInstallPage : ContentPage, IQueryAttributable
private readonly ILocalizationResourceManager _localizationResourceManager;
private readonly IHttpClientFactory _httpClient;
private readonly DownloadAndInstallPageViewModel _viewModel;
private readonly AnalyticsSubject _analyticsSubject;

public DownloadandInstallPage(DownloadAndInstallPageViewModel viewModel)

public DownloadandInstallPage(DownloadAndInstallPageViewModel viewModel, AnalyticsSubject analyticsSubject)
{
Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;

Expand All @@ -65,6 +68,7 @@ public DownloadandInstallPage(DownloadAndInstallPageViewModel viewModel)
_localizationResourceManager = ServiceProvider.GetService<ILocalizationResourceManager>();
_options = ServiceProvider.GetService<IOptions<GlobalOptions>>().Value;
_httpClient = ServiceProvider.GetService<IHttpClientFactory>();
_analyticsSubject = analyticsSubject;
_viewModel = viewModel;
this.NavigatedTo += (s, e) => Initialize();

Expand All @@ -78,7 +82,6 @@ public void ApplyQueryAttributes(IDictionary<string, object> query)
}
OnPropertyChanged(nameof(SearchListItem));
}

private void Initialize()
{
// Assign the game item
Expand Down Expand Up @@ -137,11 +140,15 @@ private async Task DownloadAndInstallOperationAsync()
var checkHuaweiService = await _appGalleryService.CheckAppGalleryCloudServiceAsync();
if (checkInternet && checkHuaweiService)
{
await DownloadandInstallHmsAppsAndNavigateAsync(adbDevices.First());
foreach (var device in adbDevices)
{
await DownloadandInstallHmsAppsAndNavigateAsync(device);
}
}
else
{
var msg = checkInternet && !checkHuaweiService ? _localizationResourceManager.GetValue("huawei_server_unreachable") : _localizationResourceManager.GetValue("internet_connection_error");
await _analyticsSubject.NotifyAsync(msg);
await AlertForNotHaveInternetAsync(msg);
}
}
Expand Down Expand Up @@ -279,21 +286,11 @@ private void UpdateCommentLabel()
{
// Update the message
// Update the comment label text based on the current progress and message index
var adbProgressMsg = AdbProgressMessages.Where(x => x.Value).ToDictionary(x => x.Key, x => x.Value).Keys.ToList();
if (adbProgressMsg.Count > currentThresholdIndex)
var commentString = _viewModel.UpdateCommentLabel(AdbProgressMessages, currentThresholdIndex, GameName);
Dispatcher.Dispatch(delegate
{
commentBuilder.Clear();
var value = adbProgressMsg[currentThresholdIndex];
if (value == AdbMessagesConst.DownloadingGame || value == AdbMessagesConst.InstallingGame)
{
value += ": " + GameName;
}
commentBuilder.Append(value);
Dispatcher.Dispatch(delegate
{
this.commentLabel.Text = commentBuilder.ToString();
});
}
this.commentLabel.Text = commentString;
});
}
private void UpdateHMSInfoLabel()
{
Expand All @@ -320,15 +317,10 @@ await Task.Run(() =>
}
});
}
private async ValueTask<bool> CheckFileSize(FileInfo fileInfo, string url)
{
long fileSize = fileInfo.Length;
var downloadFileSize = await HttpClientExtensions.GetFileSizeAsync(_httpClient, url);
return fileSize == downloadFileSize;
}
private async Task AlertForNotHaveAdbDevices()
{
await Task.Delay(1000);
await _analyticsSubject.NotifyAsync("No Adb Devices");
var errorLanguage = _localizationResourceManager.GetValue("error");
var emulatorConnectLanguage = _localizationResourceManager.GetValue("emulator_try_again_connect");
var cancelLanguage = _localizationResourceManager.GetValue("cancel");
Expand Down Expand Up @@ -445,7 +437,7 @@ private async Task CheckApkFileSizeAsync(InstallApkModel model)
if (exist)
{
FileInfo fileInfo = new(filePath);
var checkFileSize = await CheckFileSize(fileInfo, model.DownloadUrl);
var checkFileSize = await _viewModel.CheckFileSize(fileInfo, model.DownloadUrl);
if (checkFileSize)
{
model.IsDownloaded = true;
Expand Down
93 changes: 43 additions & 50 deletions src/Pages/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HuaweiHMSInstaller.Helper;
using HuaweiHMSInstaller.Integrations.Analytics;
using HuaweiHMSInstaller.Models;
using HuaweiHMSInstaller.Pages;
using HuaweiHMSInstaller.Services;
Expand All @@ -10,7 +11,6 @@
using System.Collections.ObjectModel;
using System.Globalization;
using Path = Microsoft.Maui.Controls.Shapes.Path;
using ServiceProvider = HuaweiHMSInstaller.Services.ServiceProvider;

namespace HuaweiHMSInstaller;
public partial class MainPage : ContentPage
Expand All @@ -19,21 +19,30 @@ public partial class MainPage : ContentPage
private Button footerButton = new();
private readonly IAppGalleryService _appGalleryService;
private ObservableCollection<SearchListItem> FilteredItems = new();

private delegate void WorkerCompletedEventHandler();
private SearchListItem SelectedItem { get; set; }
private Frame SearchListFrame;
private readonly ILocalizationResourceManager _localizationResourceManager;
private readonly GlobalOptions _options;
private readonly MainViewModel _mainViewModel;
public MainPage(MainViewModel mainViewModel)
private readonly AnalyticsSubject _analyticsSubject;


public MainPage(
MainViewModel mainViewModel,
IAppGalleryService appGalleryService,
ILocalizationResourceManager localizationResourceManager,
AnalyticsSubject analyticsSubject,
IOptions<GlobalOptions> options)
{
Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;

InitializeComponent();
_appGalleryService = ServiceProvider.GetService<IAppGalleryService>();
_localizationResourceManager = ServiceProvider.GetService<ILocalizationResourceManager>();
_options = ServiceProvider.GetService<IOptions<GlobalOptions>>().Value;
_appGalleryService = appGalleryService;
_localizationResourceManager = localizationResourceManager;
_options = options.Value;
_mainViewModel = mainViewModel;
_analyticsSubject = analyticsSubject;
Init();
}
private void Init()
Expand All @@ -43,48 +52,20 @@ private void Init()
this.SearchListFrameGrid.BackgroundColor = Color.FromRgba(255, 255, 255, 0.05);
this.VersionNum.Text = $"{_localizationResourceManager.GetValue("version")}: {_options.VersionNumber}";

_ = CheckInternetAndAppGalleryService(AfterEventInternetAndHuaweiServiceCheck);
_mainViewModel.Init(CheckInternetConnectionAction);
_mainViewModel.AfterEventInternetAndHuaweiServiceCheck = AfterEventInternetAndHuaweiServiceCheck;
}
#region Internet&Huawei server check operation
private bool CheckNetworkConnectionInit()
private void CheckInternetConnectionAction() => Dispatcher.StartTimer(TimeSpan.FromSeconds(2), HasNoInternetConnection);
private bool HasNoInternetConnection()
{

var current = Connectivity.NetworkAccess;
if (current == NetworkAccess.Internet) return true;

this.sponsorGameLoader.IsVisible = false;
this.sponsorGameNotInternetorHuaweiService.IsVisible = true;
this.sponsorGameNotInternetorHuaweiServiceLabel.Text = _localizationResourceManager.GetValue("internet_connection_error");
this.sponsorGameStackLayout.IsVisible = false;
InstallButtonEnable(false);
return false;
}
private void CheckHuaweiService(Worker<bool>.WorkCompletedEventHandler func) =>
CheckService(func, _appGalleryService.CheckAppGalleryServiceAsync);
private void CheckHuaweiCloudService(Worker<bool>.WorkCompletedEventHandler func) =>
CheckService(func, _appGalleryService.CheckAppGalleryCloudServiceAsync);
private void CheckService(Worker<bool>.WorkCompletedEventHandler func, Func<Task<bool>> check)
{
var worker = new Worker<bool>();
worker.WorkCompleted += func;
_ = worker.DoWorkAsync(async () => await check());
}
private async Task CheckInternetAndAppGalleryService(Worker<bool>.WorkCompletedEventHandler func)
{
var networkState = CheckNetworkConnectionInit();
var internetState = await NetworkUtils.CheckForInternetConnectionAsync();
if (networkState && internetState)
{
CheckHuaweiService(func);
}
else
{
Dispatcher.StartTimer(TimeSpan.FromSeconds(2), () =>
{
this.sponsorGameLoader.IsVisible = false;
this.sponsorGameNotInternetorHuaweiService.IsVisible = true;
this.sponsorGameNotInternetorHuaweiServiceLabel.Text = _localizationResourceManager.GetValue("internet_connection_error");
this.sponsorGameStackLayout.IsVisible = false;
InstallButtonEnable(false);
return false;
});
}
}
private void AfterEventInternetAndHuaweiServiceCheck(object sender, bool result)
{
if (result)
Expand All @@ -98,7 +79,7 @@ private void AfterEventInternetAndHuaweiServiceCheck(object sender, bool result)
}
else
{
CheckHuaweiService(SposorGameCheck);
_mainViewModel.CheckHuaweiService(SposorGameCheck);
}
}
private void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
Expand Down Expand Up @@ -180,6 +161,14 @@ private async Task GetSponsorGameInfo()
this.sponsorGameLoader.IsVisible = false;
this.sponsorGameStackLayout.IsVisible = true;
SelectedItem = selectedGame;
await _analyticsSubject.NotifyAsync("Main Page Loaded",
new Dictionary<string, object>
{
{ "SelectedGame", selectedGame.Name },
{ "SelectedGameId", selectedGame.AppId }
}
);

InstallButtonEnable(true);
}
}
Expand Down Expand Up @@ -267,6 +256,7 @@ void OkayInternetandHuaweiService()

void NotInternetAndHuweiServiceState(string langKey)
{
_analyticsSubject.Notify("Unable to connect to the internet or Huawei service");
label.FontSize = 19;
label.Margin = new Thickness(0, 0, 0, 10);
label.Text = _localizationResourceManager.GetValue(langKey);
Expand Down Expand Up @@ -296,7 +286,7 @@ void CheckHuaweiCloudServicekCallBack(object sender, bool result)

if (resultCheckingInternetConnection)
{
CheckHuaweiCloudService(CheckHuaweiCloudServicekCallBack);
_mainViewModel.CheckHuaweiCloudService(CheckHuaweiCloudServicekCallBack);
}
else
{
Expand Down Expand Up @@ -477,6 +467,13 @@ private async void SearchBarPressedAsync(object sender, EventArgs e)

SearchLoader.IsVisible = true;

await _analyticsSubject.NotifyAsync("Search Bar Pressed",
new Dictionary<string, object>
{
{ "SearchQuery", query }
}
);

var searchResult = await _appGalleryService.SearchAppGalleryApp(query);

var items = searchResult.layoutData.SelectMany(t => t.dataList.Select(x =>
Expand Down Expand Up @@ -598,7 +595,6 @@ private void CreateListView()
});

}

private Image CreateImage()
{
var image = new Image
Expand All @@ -613,7 +609,6 @@ private Image CreateImage()

return image;
}

private Label CreateLabel()
{
var label = new Label
Expand All @@ -635,7 +630,6 @@ private Label CreateLabel()

return label;
}

private StackLayout CreateStackLayout(Image image, Label label)
{
return new StackLayout
Expand All @@ -661,8 +655,6 @@ public void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
InstallButtonEnable(false);
SelectedItem = null;
selectedItemLabel.Text = string.Empty;


}
}
private void langPicker_SelectedIndexChanged(object sender, EventArgs e)
Expand All @@ -685,6 +677,7 @@ private void Button_ChangeGame_Clicked(object sender, EventArgs e)
this.selectedGameFrame.IsVisible = false;
this.searchBar.IsVisible = true;
InstallButtonEnable(false);
_analyticsSubject.Notify("Change Game Button Clicked");
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/Pages/ThanksPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HuaweiHMSInstaller.Helper;
using HuaweiHMSInstaller.Integrations.Analytics;
using HuaweiHMSInstaller.ViewModels;
using LocalizationResourceManager.Maui;
using Syncfusion.Maui.Popup;
Expand All @@ -11,16 +12,20 @@ public partial class ThanksPage : ContentPage
private SfPopup _sfPopup;
private readonly ILocalizationResourceManager _localizationResourceManager;
private readonly ThanksPageViewModel _viewModel;
private readonly AnalyticsSubject _analyticsSubject;

public ThanksPage(ThanksPageViewModel viewModel)
public ThanksPage(ThanksPageViewModel viewModel, AnalyticsSubject analyticsSubject)
{
InitializeComponent();
_localizationResourceManager = ServiceProvider.GetService<ILocalizationResourceManager>();
_viewModel = viewModel;
BindingContext = _viewModel;
_analyticsSubject = analyticsSubject;
_analyticsSubject.Notify("Thanks Page Loaded");
}
private void OnFinishButtonClicked(object sender, EventArgs e)
{
_analyticsSubject.Notify("Thanks Page Finish Button Clicked");
//Create popup and push it to the navigation stack
//Initialize the popup
var popup = new SfPopup();
Expand Down Expand Up @@ -89,7 +94,8 @@ private void CloseButton_Clicked(object sender, EventArgs e)

private async void ButtonBack_Clicked(object sender, EventArgs e)
{
_viewModel.NavigateToMainPage();
await _analyticsSubject.NotifyAsync("Thanks Page Back Button Clicked");
await _viewModel.NavigateToMainPage();
}
}

16 changes: 10 additions & 6 deletions src/Services/AdbOperationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@ public async Task DownloadAdbFromInternetAsync(IProgress<float> progress = null)
}
public async Task<List<DeviceData>> GetDevices()
{
await GetDevicesDefaultAsync();
var devices = await _adbClient.GetDevicesAsync();
if (devices.Count == 0)
{
var defaultDevice = await GetDevicesDefaultAsync();
if(defaultDevice) devices = await _adbClient.GetDevicesAsync();
}
devices = devices.Where(t => t.State == DeviceState.Online).ToList();

return devices;
}
public async Task DownloadApkFromInternetAsync(string apkUrl, string apkName, IProgress<float> progress = null, CancellationToken cancellationToken = default)

Check warning on line 110 in src/Services/AdbOperationService.cs

View workflow job for this annotation

GitHub Actions / Windows Build

Cannot convert null literal to non-nullable reference type.

Check warning on line 110 in src/Services/AdbOperationService.cs

View workflow job for this annotation

GitHub Actions / Windows Build

Cannot convert null literal to non-nullable reference type.
Expand Down Expand Up @@ -187,12 +185,18 @@ public void InstallApkToDevice(string packageFilePath, ProgressHandler installPr
continue;
}

if(ex.Message.Contains("INSTALL_FAILED_UPDATE_INCOMPATIBLE"))
if (ex.Message.Contains("INSTALL_FAILED_UPDATE_INCOMPATIBLE"))
{
//TODO : Find uninstall package name using adb command
manager.UninstallPackage("com.huawei.hwid");
}

if (ex.Message.Contains("INSTALL_FAILED_VERSION_DOWNGRADE"))
{
installed = true;
continue;
}

// Catch any exceptions that occur during installation and restart the ADB server.
Debug.WriteLine(ex.Message);
RestartAdbServer().ConfigureAwait(false).GetAwaiter().GetResult();
Expand Down
2 changes: 1 addition & 1 deletion src/Services/IAppGalleryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HuaweiHMSInstaller.Services
{
internal interface IAppGalleryService
public interface IAppGalleryService
{
Task<AppGalleryAdvancedSearchResult> SearchAppGalleryApp(string keyword);
Task<AppGalleryAppDetailResult> GetAppDetail(string appId);
Expand Down
Loading

0 comments on commit 9ac29b0

Please sign in to comment.