Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
mediaexplorer74 committed Feb 26, 2022
1 parent 34216d0 commit e5f02c3
Show file tree
Hide file tree
Showing 58 changed files with 9,338 additions and 236 deletions.
4 changes: 2 additions & 2 deletions CompleteWeatherApp.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29509.3
# Visual Studio Version 17
VisualStudioVersion = 17.2.32210.308
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompleteWeatherApp.Android", "CompleteWeatherApp\CompleteWeatherApp.Android\CompleteWeatherApp.Android.csproj", "{92BC1451-92A9-467E-BB65-F2131790AD3C}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<PackageReference Include="Xamarin.Essentials" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="DependencyService.cs" />
<Compile Include="MainActivity.cs" />
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down Expand Up @@ -177,4 +178,9 @@
<AndroidResource Include="Resources\drawable\wind.png" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
<UserProperties XamarinHotReloadXFormsNugetUpgradeInfoBarCompleteWeatherAppAndroidHideInfoBar="True" />
</VisualStudio>
</ProjectExtensions>
</Project>
331 changes: 331 additions & 0 deletions CompleteWeatherApp/CompleteWeatherApp.Android/DependencyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,331 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Diagnostics;
using System.ServiceModel;


//using Windows.Devices.Geolocation; // !
using Android.Locations;

using static CompleteWeatherApp.MainPage;
using System.Threading;



//[assembly: Dependency(typeof(CompleteWeatherApp.UWP.DeviceInfo))]
[assembly: Dependency(typeof(CompleteWeatherApp.Droid.GeoInfo))]
namespace CompleteWeatherApp.Droid
{


// 3 IGeoInfo "interface realization"
public class GeoInfo : IGeoInfo
{

// Proides access to location data
private Geolocator _geolocator = null;

// our super client (see details of realization in MegaLib project)

//public static GeoInfo geoinfo = new GeoInfo();

private static double Latitude = 0;
private static double Longitude = 0;
private static int GPSStatus = 0;


// GetInfo: Say Hello to Service =)
public string GetInfo()
{
// TODO: write some description of service's interface
return $"GeoInfo Service";
}//GetInfo


//GetStatus
public int GetStatus()
{
return GPSStatus;
}//GetStatus

//GetLatitude
public double GetLatitude()
{
return Latitude;
}//GetLongitude

//GetLongitude
public double GetLongitude()
{
return Longitude;
}//GetLongitude



// StartTracking
//private async void StartTracking(object sender, RoutedEventArgs e)
public async Task<bool> StartTracking()
{
bool Flag = true;

// Request permission to access location
//GeolocationAccessStatus accessStatus;// = null;

try
{
var accessStatus = Geolocator.RequestAccessAsync(); // await


await Task.Delay(5000);

Debug.WriteLine("[i] " + accessStatus.ToString());
}
catch (Exception ex)
{
Debug.WriteLine("GPS: Error! GeolocationAccessStatus denied ? : " + ex.Message);

Flag = false;

return Flag;
}



try
{
// Value of 2000 milliseconds (2 seconds)
// isn't a requirement, it is just an example.
_geolocator = new Geolocator { ReportInterval = 2000 };

// Subscribe to PositionChanged event to get updated tracking positions
_geolocator.PositionChanged += OnPositionChanged;

// Subscribe to StatusChanged event to get updates of location status changes
_geolocator.StatusChanged += OnStatusChanged;
}
catch (Exception ex2)
{

Debug.WriteLine("GPS: Error! GeolocationAccessStatus denied ? Check app permissions : "
+ ex2.Message);

Flag = false;
}

return Flag;

}


// StopTracking
public bool StopTracking()
{
if (_geolocator != null)
{
_geolocator.PositionChanged -= OnPositionChanged;
_geolocator.StatusChanged -= OnStatusChanged;
_geolocator = null;

//StartTrackingButton.IsEnabled = true;
//StopTrackingButton.IsEnabled = false;

// Clear status
Debug.WriteLine("GPS: Status cleared!");
}

return true;
}


//
private void OnPositionChanged(Geolocator sender, PositionChangedEventArgs e)
{
//await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
//{
// Debug.WriteLine("Location updated.");
UpdateLocationData(e.Position);
//});
}


// Event handler for StatusChanged events. It is raised when the
// location status in the system changes.
private async void OnStatusChanged(Geolocator sender, StatusChangedEventArgs e)
{
//await System.ServiceModel.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
//{
// Show the location setting message only if status is disabled.
//LocationDisabledMessage.Visibility = Visibility.Collapsed;

switch (e.Status)
{
/*
case PositionStatus.Ready:
// Location platform is providing valid data.
//ScenarioOutput_Status.Text = "Ready";
GPSStatus = 5; // "OK"
Debug.WriteLine("GPS : Location platform is ready.");
break;
case PositionStatus.Initializing:
// Location platform is attempting to acquire a fix.
//ScenarioOutput_Status.Text = "Initializing";
GPSStatus = 4; // "Initializing"
Debug.WriteLine("GPS: Location platform is attempting to obtain a position.");
break;
case PositionStatus.NoData:
// Location platform could not obtain location data.
//ScenarioOutput_Status.Text = "No data";
GPSStatus = 3; // "Not able to determine the location"
Debug.WriteLine("GPS: Not able to determine the location.");
break;
case PositionStatus.Disabled:
// The permission to access location data is denied by the user or other policies.
//ScenarioOutput_Status.Text = "Disabled";
GPSStatus = 2; // "Disabled"
Debug.WriteLine("GPS: Access to location is denied.");
// Show message to the user to go to location settings
//LocationDisabledMessage.Visibility = Visibility.Visible;
// Clear cached location data if any
UpdateLocationData(null);
break;
case PositionStatus.NotInitialized:
// The location platform is not initialized. This indicates that the application
// has not made a request for location data.
//ScenarioOutput_Status.Text = "Not initialized";
GPSStatus = 1; // "Not initialized"
Debug.WriteLine("No request for location is made yet.");
break;
case PositionStatus.NotAvailable:
// The location platform is not available on this version of the OS.
//ScenarioOutput_Status.Text = "Not available";
GPSStatus = 10; // "Service not available on this version of the OS"
Debug.WriteLine("Location is not available on this version of the OS.");
break;
*/
default:
//ScenarioOutput_Status.Text = "Unknown";
GPSStatus = 9; // "Unknown"
Debug.WriteLine("Unknown");
break;
}
//});
}


// GetGeoInfo
private async Task GetGeoInfo()
{
//bool Flag = false;

GeolocationAccessStatus accessStatus;// = null;

try
{
/*
accessStatus = await Geolocator.RequestAccessAsync();
switch (accessStatus)
{
case GeolocationAccessStatus.Allowed:
// Create Geolocator and define perodic-based tracking (2 second interval).
_geolocator = new Geolocator { ReportInterval = 2000 };
// Subscribe to the PositionChanged event to get location updates.
_geolocator.PositionChanged += OnPositionChanged;
// Subscribe to StatusChanged event to get updates of location status changes.
_geolocator.StatusChanged += OnStatusChanged;
Debug.WriteLine("Waiting for update...");
//LocationDisabledMessage.Visibility = Visibility.Collapsed;
//StartTrackingButton.IsEnabled = false;
//StopTrackingButton.IsEnabled = true;
//Flag = true;
break;
case GeolocationAccessStatus.Denied:
Debug.WriteLine("Access to location is denied.");
//LocationDisabledMessage.Visibility = Visibility.Visible;
break;
case GeolocationAccessStatus.Unspecified:
Debug.WriteLine("Unspecificed error!");
//LocationDisabledMessage.Visibility = Visibility.Collapsed;
break;
}
*/
}
catch (Exception ex)
{
Debug.WriteLine("Exception: " + ex.Message);
}

//return Flag;
}//GetGeoInfo


private void UpdateLocationData(Geoposition position)
{
if (position == null)
{
//ScenarioOutput_Latitude.Text = "No data";
//ScenarioOutput_Longitude.Text = "No data";
//ScenarioOutput_Accuracy.Text = "No data";
}
else
{
Latitude = 0;//position.Coordinate.Point.Position.Latitude;
Longitude = 0;// position.Coordinate.Point.Position.Longitude;
//ScenarioOutput_Latitude.Text = position.Coordinate.Point.Position.Latitude.ToString();
//ScenarioOutput_Longitude.Text = position.Coordinate.Point.Position.Longitude.ToString();
//ScenarioOutput_Accuracy.Text = position.Coordinate.Accuracy.ToString();
}
}


}//GeoInfo : IGeoInfo

internal class GeolocationAccessStatus
{
}

internal class PositionChangedEventArgs
{
public Geoposition Position { get; internal set; }
}

internal class Geoposition
{
}

internal class Geolocator
{
public int ReportInterval { get; set; }
public Action<Geolocator, PositionChangedEventArgs> PositionChanged { get; internal set; }
public Action<Geolocator, StatusChangedEventArgs> StatusChanged { get; internal set; }

internal static object RequestAccessAsync()
{
throw new NotImplementedException();
}
}
}
Loading

0 comments on commit e5f02c3

Please sign in to comment.