Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect the platform at runtime using MvvMCross?

Tags:

mvvmcross

I want the user to be able to send feedback about my app to an address. Using the email plugin, this is all good, but in the body of the email, I want to pre-populate some information about the app they're running.

Ideally, I'd like the device, the OS, the screen res, the orientation etc, but for now I'd just settle for the OS

like image 293
Coatsy Avatar asked Mar 02 '14 00:03

Coatsy


2 Answers

It feels quite strange, but I can't remember anyone ever asking for this as a feature and I don't think anyone's made a plugin for it either.

Given your future requirements (screen res, orientation, etc) the easiest way to this is probably to define an interface in your core project:

 public enum OS
 {
    Droid, Touch, WinPhone, WinStore, Mac, Wpf
 }

 public IDetails
 {
     OS OS { get; }
     // whatever else you need
 }

You can then register implementations for this in each UI project - e.g. in Setup for WinPhone, add:

 protected override void InitializeLastChance()
 {
     base. Setup.InitializeLastChance();
     Mvx.RegisterSingleton<IDetails>(new WindowsPhoneDetails());
 }

Where:

 public class WindowsPhoneDetails : IDetails
 {
     public OS OS { get { return OS.WinPhone; } }
 }

For more on this approach, see https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#wiki-registering-platform-specific-business-objects-in-setupinitializefirstchance-and-setupinitializelastchance

(Specifically for screen size, also see IDisplayDimensionsService in https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/FractalGen)

like image 181
Stuart Avatar answered Jan 01 '23 12:01

Stuart


Turns out there's already a project up that does this - https://github.com/aritchie/acrmvvmcross Kudos Allan Ritchie!

Unfortunately, there's no Windows Phone implementation et, so I implemented sme of the functionality as follows:

public class WindowsPhoneDeviceInfoService : IDeviceInfoService
{
    public bool IsFrontCameraAvailable
    {
        get { throw new NotImplementedException(); }
    }

    public bool IsRearCameraAvailable
    {
        get { throw new NotImplementedException(); }
    }

    public bool IsSimulator
    {
        get { throw new NotImplementedException(); }
    }

    public string Manufacturer
    {
        get { return DeviceStatus.DeviceManufacturer; }
    }

    public string Model
    {
        get { return DeviceStatus.DeviceName; }
    }

    public string OperatingSystem
    {
        get { return Environment.OSVersion.Version.ToString(); }
    }

    public int ScreenHeight
    {
        get 
        {
            object szo;
            if (DeviceExtendedProperties.TryGetValue("PhysicalScreenResolution", out szo))
            {
                Size sz = (Size)szo;
                if (sz == null)
                    return 0;
                return (int)sz.Height;
            }
            else
            {
                return 0;
            }
        }
    }

    public int ScreenWidth
    {
        get
        {
            object szo;
            if (DeviceExtendedProperties.TryGetValue("PhysicalScreenResolution", out szo))
            {
                Size sz = (Size)szo;
                if (sz == null)
                    return 0;
                return (int)sz.Width;
            }
            else
            {
                return 0;
            }
        }
    }
}

Of course, I also had to register the class in setup.cs:

Mvx.RegisterSingleton<IDeviceInfoService>(new WindowsPhoneDeviceInfoService());
like image 27
Coatsy Avatar answered Jan 01 '23 11:01

Coatsy