Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit ae5b20e

Browse files
committed
[Android] Update Mapbox package & fix offline service
1 parent 8d88fe7 commit ae5b20e

12 files changed

+551
-382
lines changed

Droid/MainActivity.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ protected override void OnCreate(Bundle savedInstanceState)
2020

2121
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
2222

23+
System.Diagnostics.Debug.WriteLine("Mapbox version: " + Com.Mapbox.Mapboxsdk.BuildConfig.MapboxVersionString);
24+
2325
LoadApplication(new App());
2426
}
2527
}

Droid/MapBoxQs.Droid.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,18 @@
174174
<Reference Include="Naxam.Mapbox.Services.Android.Telemetry">
175175
<HintPath>..\packages\Naxam.Mapbox.Services.Android.Telemetry.2.2.10\lib\MonoAndroid10\Naxam.Mapbox.Services.Android.Telemetry.dll</HintPath>
176176
</Reference>
177-
<Reference Include="Naxam.Mapbox.Droid">
178-
<HintPath>..\packages\Naxam.Mapbox.Droid.5.5.0\lib\MonoAndroid10\Naxam.Mapbox.Droid.dll</HintPath>
179-
</Reference>
180177
<Reference Include="GoogleGson">
181178
<HintPath>..\packages\GoogleGson.2.8.1\lib\MonoAndroid\GoogleGson.dll</HintPath>
182179
</Reference>
183180
<Reference Include="Newtonsoft.Json">
184181
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\netstandard2.0\Newtonsoft.Json.dll</HintPath>
185182
</Reference>
183+
<Reference Include="Naxam.Mapzen.Droid">
184+
<HintPath>..\packages\Naxam.Mapzen.Droid.1.2.1\lib\MonoAndroid10\Naxam.Mapzen.Droid.dll</HintPath>
185+
</Reference>
186+
<Reference Include="Naxam.Mapbox.Droid">
187+
<HintPath>..\packages\Naxam.Mapbox.Droid.5.5.1.1\lib\MonoAndroid10\Naxam.Mapbox.Droid.dll</HintPath>
188+
</Reference>
186189
</ItemGroup>
187190
<ItemGroup>
188191
<ProjectReference Include="..\MapBoxQs\MapBoxQs.csproj">

Droid/Resources/Resource.designer.cs

Lines changed: 363 additions & 275 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Droid/packages.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
<package id="Microsoft.NETCore.Platforms" version="1.1.0" targetFramework="monoandroid71" />
99
<package id="Microsoft.Win32.Primitives" version="4.3.0" targetFramework="monoandroid71" />
1010
<package id="Naxam.Jakewharton.Timber" version="4.5.1" targetFramework="monoandroid71" />
11-
<package id="Naxam.Mapbox.Droid" version="5.5.0" targetFramework="monoandroid81" />
11+
<package id="Naxam.Mapbox.Droid" version="5.5.1.1" targetFramework="monoandroid81" />
1212
<package id="Naxam.Mapbox.MapboxJavaCore" version="2.2.10" targetFramework="monoandroid81" />
1313
<package id="Naxam.Mapbox.MapboxJavaGeojson" version="2.2.10" targetFramework="monoandroid81" />
1414
<package id="Naxam.Mapbox.Services.Android.Telemetry" version="2.2.10" targetFramework="monoandroid81" />
15+
<package id="Naxam.Mapzen.Droid" version="1.2.1" targetFramework="monoandroid81" />
1516
<package id="Naxam.Mapzen.Lost.Droid" version="3.0.4" targetFramework="monoandroid81" />
1617
<package id="NETStandard.Library" version="1.6.1" targetFramework="monoandroid71" />
1718
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="monoandroid81" />

MapBoxQs/MainPageViewModel.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
34
using System.Linq;
45
using System.Threading.Tasks;
@@ -69,6 +70,10 @@ public MainPageViewModel()
6970
System.Diagnostics.Debug.WriteLine($"Downloaded tiles: {progress.CountOfTilesCompleted}");
7071
if (progress.CountOfResourcesExpected == progress.CountOfResourcesCompleted) {
7172
System.Diagnostics.Debug.WriteLine("Download completed");
73+
Device.BeginInvokeOnMainThread(() =>
74+
{
75+
UserDialogs.Instance.HideLoading();
76+
});
7277
}
7378
};
7479
}
@@ -192,25 +197,27 @@ private async void DownloadMap(object obj)
192197
NorthEast = new Position()
193198
{
194199
Lat = CenterLocation.Lat - 0.01,
195-
Long = CenterLocation.Long + 0.01
200+
Long = CenterLocation.Long + 0.005
196201
},
197202
SouthWest = new Position()
198203
{
199204
Lat = CenterLocation.Lat + 0.01,
200-
Long = CenterLocation.Long - 0.01
205+
Long = CenterLocation.Long - 0.005
201206
}
202207
}
203208
};
204209
UserDialogs.Instance.ShowLoading();
205210
var pack = await offlineService.DownloadMap(region, new System.Collections.Generic.Dictionary<string, string>() {
206-
{"name", "test"}
211+
{"name", "test"},
212+
{"started_at", DateTime.Now.ToString("HH:mm:ss dd/MM/yyyy")}
207213
});
208214
if (pack != null) {
209215
offlineService.RequestPackProgress(pack);
210216
}
211-
UserDialogs.Instance.HideLoading();
217+
else {
218+
UserDialogs.Instance.HideLoading();
219+
}
212220
}
213-
214221
}
215222

216223
private ICommand _ClearOfflinePacksCommand;
@@ -250,12 +257,27 @@ public ICommand LoadOfflinePackCommand
250257
private async void LoadOfflinePack(object obj)
251258
{
252259
var packs = await offlineService.GetPacks();
253-
if (packs?.FirstOrDefault() is OfflinePack pack)
254-
{
255-
forcedRegion = pack.Region;
256-
CurrentMapStyle = new MapStyle(pack.Region.StyleURL);
257-
}
260+
if (packs != null && packs.Length != 0) {
261+
var buttons = new List<string>();
262+
foreach (OfflinePack pack in packs) {
263+
if (pack.Info != null
264+
&& pack.Info.TryGetValue("name", out string name)
265+
&& pack.Info.TryGetValue("started_at", out string startTime)) {
266+
buttons.Add(name + " - " + startTime);
267+
}
268+
}
269+
var chosen = await UserDialogs.Instance.ActionSheetAsync("Load offline pack", "Cancel", null, null, buttons.ToArray());
270+
var chosenIndex = buttons.IndexOf(chosen);
271+
if (chosenIndex >= 0 && chosenIndex < packs.Length) {
272+
var chosenPack = packs[chosenIndex];
273+
forcedRegion = chosenPack.Region;
274+
CurrentMapStyle = new MapStyle(chosenPack.Region.StyleURL);
258275

276+
}
277+
}
278+
else {
279+
await UserDialogs.Instance.AlertAsync("There's no offline pack to load");
280+
}
259281
}
260282

261283

Naxam.Mapbox.Platform.Droid/Naxam.Mapbox.Platform.Droid.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,15 @@
173173
<Reference Include="Naxam.Mapbox.Services.Android.Telemetry">
174174
<HintPath>..\packages\Naxam.Mapbox.Services.Android.Telemetry.2.2.10\lib\MonoAndroid10\Naxam.Mapbox.Services.Android.Telemetry.dll</HintPath>
175175
</Reference>
176-
<Reference Include="Naxam.Mapbox.Droid">
177-
<HintPath>..\packages\Naxam.Mapbox.Droid.5.5.0\lib\MonoAndroid10\Naxam.Mapbox.Droid.dll</HintPath>
178-
</Reference>
179176
<Reference Include="GoogleGson">
180177
<HintPath>..\packages\GoogleGson.2.8.1\lib\MonoAndroid\GoogleGson.dll</HintPath>
181178
</Reference>
179+
<Reference Include="Naxam.Mapzen.Droid">
180+
<HintPath>..\packages\Naxam.Mapzen.Droid.1.2.1\lib\MonoAndroid10\Naxam.Mapzen.Droid.dll</HintPath>
181+
</Reference>
182+
<Reference Include="Naxam.Mapbox.Droid">
183+
<HintPath>..\packages\Naxam.Mapbox.Droid.5.5.1.1\lib\MonoAndroid10\Naxam.Mapbox.Droid.dll</HintPath>
184+
</Reference>
182185
</ItemGroup>
183186
<ItemGroup>
184187
<Compile Include="MapViewRenderer_MapEvents.cs" />

Naxam.Mapbox.Platform.Droid/Offline/CreateOfflineRegionCallback.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ namespace Naxam.Mapbox.Platform.Droid.Offline
55
{
66
public class CreateOfflineRegionCallback: Java.Lang.Object, OfflineManager.ICreateOfflineRegionCallback
77
{
8-
public CreateOfflineRegionCallback()
8+
public CreateOfflineRegionCallback(Action<OfflineRegion> onCreateHandle, Action<string> onErrorHandle)
99
{
10+
this.OnCreateHandle = onCreateHandle;
11+
this.OnErrorHandle = onErrorHandle;
1012
}
1113

12-
public Action<OfflineRegion> OnCreateHandle;
13-
public Action<string> OnErrorHandle;
14+
public CreateOfflineRegionCallback(IntPtr handle, Android.Runtime.JniHandleOwnership ownership)
15+
: base(handle, ownership)
16+
{
17+
}
18+
19+
public Action<OfflineRegion> OnCreateHandle { get; set; }
20+
public Action<string> OnErrorHandle { get; set; }
1421

1522
public void OnCreate(OfflineRegion p0)
1623
{

Naxam.Mapbox.Platform.Droid/Offline/OfflinePackExtensions.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System.Collections.Generic;
2-
using System.IO;
3-
using System.Runtime.Serialization.Formatters.Binary;
42
using Com.Mapbox.Mapboxsdk.Offline;
3+
using GoogleGson;
4+
using Java.Lang;
55
using Naxam.Controls.Mapbox.Forms;
6+
using Naxam.Controls.Mapbox.Platform.Droid;
67

78
namespace Naxam.Mapbox.Platform.Droid.Offline
89
{
@@ -19,14 +20,25 @@ public static OfflinePack ToFormsPack(this OfflineRegion mbRegion)
1920
if (definition is OfflineTilePyramidRegionDefinition def) {
2021
output.Region = def.ToFormsRegion();
2122
}
22-
if (mbRegion.GetMetadata() is byte[] metadata) {
23-
var mStream = new MemoryStream();
24-
var binFormatter = new BinaryFormatter();
25-
26-
mStream.Write(metadata, 0, metadata.Length);
27-
mStream.Position = 0;
28-
29-
output.Info = binFormatter.Deserialize(mStream) as Dictionary<string, string>;
23+
if (mbRegion.GetMetadata() is byte[] metadata) {
24+
String json = new String(metadata, OfflineStorageService.JSON_CHARSET);
25+
26+
try
27+
{
28+
JsonObject jsonObject = (JsonObject)new Gson().FromJson(json.ToString(), Java.Lang.Class.FromType(typeof(JsonObject)));
29+
if (jsonObject != null)
30+
{
31+
var keys = jsonObject.KeySet();
32+
output.Info = new Dictionary<string, string>(keys.Count);
33+
foreach (string key in keys)
34+
{
35+
output.Info.Add(key, jsonObject.Get(key).AsString);
36+
}
37+
}
38+
}
39+
catch (Exception ex) {
40+
System.Diagnostics.Debug.WriteLine("Failed to decode offline region metadata: " + ex.Message);
41+
}
3042
}
3143
return output;
3244
}

Naxam.Mapbox.Platform.Droid/Offline/OfflineRegionDeleteCallback.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ namespace Naxam.Mapbox.Platform.Droid.Offline
44
{
55
public class OfflineRegionDeleteCallback: Java.Lang.Object, OfflineRegion.IOfflineRegionDeleteCallback
66
{
7-
public OfflineRegionDeleteCallback()
7+
public OfflineRegionDeleteCallback(Action deleteHandle, Action<string> errorHandle)
88
{
9+
this.OnDeleteHandle = deleteHandle;
10+
this.OnErrorHandle = errorHandle;
911
}
1012

11-
public Action OnDeleteHandle;
12-
public Action<string> OnErrorHandle;
13+
public Action OnDeleteHandle { get; set; }
14+
public Action<string> OnErrorHandle { get; set; }
1315

1416
public void OnDelete()
1517
{

Naxam.Mapbox.Platform.Droid/Offline/OfflineRegionObserver.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@ namespace Naxam.Mapbox.Platform.Droid.Offline
44
{
55
public class OfflineRegionObserver: Java.Lang.Object, OfflineRegion.IOfflineRegionObserver
66
{
7-
public OfflineRegionObserver()
7+
public OfflineRegionObserver(Action<OfflineRegionStatus> statusHandle,
8+
Action<OfflineRegionError> errorHandle,
9+
Action<long> tileCountLimitExceededHanle)
810
{
11+
this.OnStatusChangedHandle = statusHandle;
12+
this.OnErrorHandle = errorHandle;
13+
this.OnMapboxTileCountLimitExceededHandle = tileCountLimitExceededHanle;
914
}
1015

11-
internal Action<long> MapboxTileCountLimitExceededHandle;
12-
internal Action<OfflineRegionError> OnErrorHandle;
13-
internal Action<OfflineRegionStatus> OnStatusChangedHandle;
16+
internal Action<long> OnMapboxTileCountLimitExceededHandle { get; set; }
17+
internal Action<OfflineRegionError> OnErrorHandle { get; set; }
18+
internal Action<OfflineRegionStatus> OnStatusChangedHandle { get; set; }
1419

1520
public void MapboxTileCountLimitExceeded(long p0)
1621
{
17-
MapboxTileCountLimitExceededHandle?.Invoke(p0);
22+
OnMapboxTileCountLimitExceededHandle?.Invoke(p0);
1823
}
1924

2025
public void OnError(OfflineRegionError p0)

0 commit comments

Comments
 (0)