MVVMCross IMvxGeoLocationWatcher Success Action never fires on Android - xamarin.android

I created a very simple test app to try and reverse geocode my current lat/long into an address.
Here is the code for my ViewModel:
namespace LoginProductsMVVM.Core.ViewModels
{
public class ProductDetailViewModel
: MvxViewModel
{
public void Init(Product product)
{
Product = product;
}
private Product _product;
public Product Product
{
get { return _product; }
set { _product = value;
RaisePropertyChanged (() => Product); }
}
private string _latitude;
public string Latitude{
get { return _latitude; }
set { _latitude = value; RaisePropertyChanged(() => Latitude); }
}
private string _longitude;
public string Longitude{
get { return _longitude; }
set { _longitude = value; RaisePropertyChanged(() => Longitude); }
}
private string _address;
public string Address{
get { return _address; }
set { _address = value; RaisePropertyChanged(() => Address); }
}
private IMvxGeoLocationWatcher _watcher;
public IMvxGeoLocationWatcher Watcher
{
get
{
_watcher = Mvx.Resolve<IMvxGeoLocationWatcher> ();
return _watcher;
}
}
public ProductDetailViewModel(IMvxGeoLocationWatcher watcher)
{
_watcher = watcher;
_watcher.Start (new MvxGeoLocationOptions (), OnLocation, OnError);
}
void OnLocation (MvxGeoLocation location)
{
Latitude = location.Coordinates.Latitude.ToString();
Longitude = location.Coordinates.Longitude.ToString();
// Android Location specific stuff
var activity = Mvx.Resolve<IMvxAndroidCurrentTopActivity> ().Activity;
Geocoder geocdr = new Geocoder (activity.BaseContext);
IList<Address> addresses = geocdr.GetFromLocation (double.Parse(Latitude), double.Parse(Longitude), 1);
addresses.ToList().ForEach ((addr) => Address += addr.ToString() + "\r\n\r\n");
}
void OnError (MvxLocationError error)
{
Mvx.Error ("Seen location error {0}", error);
}
}
}
I have a break point in my OnLocation method but it never gets in there. Am I missing something for this to work properly on Android? It appears to work just fine for iOS...

Per Odahan here:
Well... I investigated a bit more : The problem is known and can be seen on many devices. This is not a MvvmCross problem. Short answer : the device needs to be rebooted and all is working like a charm... It seems Google sent some updates that are causing the problem.
Here is a thread speaking about this problem and a similar one concerning the GeoCode class :
https://code.google.com/p/android/issues/detail?id=38009
So : can be closed, MvvmCross is ok but others can face this bug so my explanations and the link here.

When you say it never fires 'success', does the success actually ever occur?
There could be lots of things going wrong in the GPS code - eg your app might not have privilege, your phone might have A-GPS or GPS disabled, or you might even be running on a location-less emulator - all of this is possible from your description.
It'a also worth noting that Xamarin.Android has long standing issues with hitting breakpoints - so it's better to add trace than to rely on breakpoint hitting :/
Perhaps try running though N=8 from http://mvvmcross.wordpress.com/ - does that help at all? (N=9 is also worth watching as it shows one way to allow multiple viewmodels to use the geowatcher)

Related

MvvmCross Realm access from incorrect thread

Exception
Realm access from incorrect thread in MainViewModel
Application Flow
SplashScreen> MainActivity(Exception)
[Activity(MainLauncher = true
, Icon = "#mipmap/ic_launcher"
, Theme = "#style/Theme.Splash"
, NoHistory = true
, ScreenOrientation = ScreenOrientation.Portrait)]
public class SplashScreen : MvxSplashScreenActivity
{
public SplashScreen()
: base(Resource.Layout.SplashScreen)
{
}
}
MainActivity
public class MainActivity : MvxAppCompatActivity<MainViewModel>,ViewPager.IOnPageChangeListener,View.IOnTouchListener
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_tutorial);
if (ViewModel.IsCompletedOrNot)
ViewModel.OpenMainViewModel.Execute();
}
MainViewModel
[AddINotifyPropertyChangedInterface]
public class MainViewModel : MvxViewModel
{
private Realm _realm;
private bool isCompleted = false;
public TutorialViewModel(IMvxNavigationService navigationService)
{
_realm = Mvx.Resolve<Realm>();
}
public bool IsCompletedOrNot{
get
{
if (_realm.All<IsFirstTimeAppStartUpRealm>().Count() > 0)
{
isCompleted=true;
}else{
isCompleted = false;
}
return isCompleted;
}
}
}
App.CS
var key = ConfigManager.Settings?.DatabaseEcryption?.EncryptionKey;
if (key != null && key.Length > 0)
{
config.EncryptionKey = key;
}
Mvx.RegisterSingleton<Realm>(() => Realm.GetInstance(config));
Realm _realm = Mvx.Resolve<Realm>();
int count = _realm.All<IsFirstTimeAppStartUpRealm>().Count();
//RegisterCustomAppStart<CustomAppStart>();
// App start
if (count>0)
{
RegisterNavigationServiceAppStart<MainViewModel>();
}
else
{
RegisterNavigationServiceAppStart<OtherViewModel>();
}
The below line throws the exception.
_realm.All<IsFirstTimeAppStartUpRealm>().Count() > 0
App always crashes when it comes through SplashScreen and it works fine if started from MainActivity.
MvvmCross does not guarantee that App start is run on the UI thread. I will most likely run on a ThreadPool Thread.
In order to marshal a piece of code to the main thread, you can resolve the IMvxMainThreadAsyncDispatcher (>= 6.1.x) or IMvxMainThreadDispatcher and request an Action to run on the main thread:
var dispatcher = Mvx.Resolve<IMvxMainThreadAsyncDispatcher>();
int count;
await dispatcher.ExecuteOnMainThreadAsync(() =>
{
count = _realm.All<IsFirstTimeAppStartUpRealm>().Count();
});
I have a reasonably nice way of removing these code smells from my applications. I've just recently started using Realm (liking it so far), but I have always used ReactiveProperty to notify my view layer of VM changes - and it's really nice.
https://github.com/runceel/ReactiveProperty
ReactiveProperty is a Rx framework for .NET, which wraps your properties in an instance that produces your INotifyPropertyChanged events as needed. You chain all these properties together as they depend on each other, and events propagate throughout them. You no longer have these long lists of "notify of this, notify of that" after a property change.
Instead you declare how all your members inter-depend, in a single section of your code (typically your constructor)
As a result, you can place the root of the chain on the Realm thread, and all dependent notifications are published on that thread.
So, my ViewModels look something like this (pseudocode):
class VM
{
public ReactiveProperty<AppStartInfo> Entity { get; set; }
public ReactiveProperty<bool> IsFirstLaunch { get; set; }
public VM(){
var syncCtx = SynchronizationContext.Current;
Entity = new ReactiveProperty<AppStartInfo>();
// this property will fire its notifications on the syncCtx.
// remember to bind your view to "IsFirstLaunch.Value"
IsFirstLaunch = Entity.SubscribeOn(syncCtx).Select(x => x.IsFirstLaunch).ToReactiveProperty()
}
public async Task Init()
{
// let's get our realm instance on to the syncCtx.
syncCtx.Post(() => {
Entity.Value = Realm.Find(typeof(AppStartInfo), 0); // or whatever you need.
});
}
}

Get BBM Chat Logs

A Simple question for every one , is there any possible way to get Blackberry BBM Logs in application , via Programming.
Following task I have done :-
Download & integrate BBM SDK in Project.
Follow the BBM Development Guide.
Here are My Code :-
public void getBBM_Logs()
{
BBMPlatformContext platformContext =null;
try
{
platformContext = BBMPlatformManager.register(new MyBBMAppPlugin());
if(platformContext != null)
{
ContactListService contactListService = platformContext.getContactListService();
BBMPlatformContactList contacts = contactListService.getContactList();
Enumeration contactsEnum = contacts.getAll();
while(contactsEnum.hasMoreElements())
{
BBMPlatformContact contact = (BBMPlatformContact)contactsEnum.nextElement();
add(new LabelField(contact.getDisplayName()));
}
}
}
catch (ControlledAccessException e)
{
// The BBM platform has been disabled
}
if (platformContext != null)
{
MyBBMPlatformContextListener platformContextListener;
platformContextListener = new MyBBMPlatformContextListener();
platformContext.setListener(platformContextListener);
}
}
private class MyBBMPlatformContextListener extends BBMPlatformContextListener
{
public void accessChanged(boolean isAccessAllowed, int accessErrorCode)
{
if (!isAccessAllowed)
{
// You cannot access the BBM platform
}
}
public void appInvoked(int reason, Object param)
{
// Code for handling different contexts for invocation
}
}
private class MyBBMAppPlugin extends BBMPlatformApplication
{
public MyBBMAppPlugin()
{
super("57888721-1e52-4171-a8a4-0559eab8efdf");
}
}
Please Let Me know , is there any possible way to get ChatLogs.
Sorry this is not possible - as I think BB regard access to chat logs from a program as a potential security exposure.

Is ASP.NET MVC 4's Mobile "Selector" Unreliable at this time?

I ask because I'm noticing on my site, if I hit it with an iPhone, sometimes it shows the mobile views, sometimes it shows regular views.
I've also read that MVC 4's not particularly effective at determining if the browser is from a mobile device or not, is that true? If so, anything we can do about it?
Update: Microsoft have published a workaround package for this bug.
I have added this workaround at here,
public class MyDefaultViewLocationCache : DefaultViewLocationCache, IViewLocationCache
{
public MyDefaultViewLocationCache(TimeSpan timeSpan): base(timeSpan)
{
}
public MyDefaultViewLocationCache()
: base()
{
}
public new string GetViewLocation(HttpContextBase httpContext, string key)
{
var location = base.GetViewLocation(httpContext, key);
if (location == null)
{
var cache = httpContext.Cache;
RemoveAllCacheStartWith(key, cache);
}
return location;
}
private static void RemoveAllCacheStartWith(string key, System.Web.Caching.Cache cache)
{
var keyWithoutDisplayMode = key.Substring(0, key.Substring(0, key.Length - 1).LastIndexOf(':') + 1);
var items = new List<string>();
var enumerator = cache.GetEnumerator();
while (enumerator.MoveNext())
{
var _key = enumerator.Key.ToString();
if (_key.StartsWith(keyWithoutDisplayMode))
{
items.Add(_key);
}
}
foreach (string item in items)
{
cache.Remove(item);
}
}
public new void InsertViewLocation(HttpContextBase httpContext, string key, string virtualPath)
{
base.InsertViewLocation(httpContext, key, virtualPath);
}
}
// In App Start
ViewEngines.Engines.OfType<RazorViewEngine>().First().ViewLocationCache =
new MyDefaultViewLocationCache();
For detail see this.

db4o Tranparent Persistence doesn't store later objects in my own ActivatableCollection<T>

I'm rolling my own ActivatableCollection<T> for db4o but cribbing heavily from the builtin ActivatableList<T> implementation. I'm running into the problem where transparent persistence doesn't seem to be working correctly. In the test code below:
[Fact]
void CanStoreActivatableCollection()
{
var planets = new ActivatableCollection<Planet>();
var pagingMemoryStorage = new PagingMemoryStorage();
var config = Db4oEmbedded.NewConfiguration();
config.Common.Add(new TransparentActivationSupport());
config.Common.Add(new TransparentPersistenceSupport());
config.File.Storage = pagingMemoryStorage;
var objectContainer = Db4oEmbedded.OpenFile(config, "Memory.yap");
planets.Add(new Planet("Mercury"));
objectContainer.Store(planets);
planets.Add(new Planet("Venus"));
planets.Add(new Planet("Earth"));
objectContainer.Commit();
objectContainer.Close();
config = Db4oEmbedded.NewConfiguration();
config.Common.Add(new TransparentActivationSupport());
config.Common.Add(new TransparentPersistenceSupport());
config.File.Storage = pagingMemoryStorage;
objectContainer = Db4oEmbedded.OpenFile(config, "Memory.yap");
planets = objectContainer.Query<ActivatableCollection<Planet>>().FirstOrDefault();
Assert.NotNull(planets);
Assert.Equal(3, planets.Count);
objectContainer.Close();
}
The planet "Mercury" is stored, but not "Venus" and "Earth". If I change from ActivatableCollection to ActivatableList, then all 3 planets are stored.
What am I missing? My ActivatableCollection is just minimal implementation of ActivatableList as best as I can tell.
Below is my implementation of ActivatableCollection:
public class ActivatableCollection<T>
: ICollection<T>
, IActivatable
, INotifyCollectionChanged
{
List<T> _list;
List<T> List
{
get
{
if (_list == null)
_list = new List<T>();
return _list;
}
}
public ActivatableCollection()
{
}
public int Count
{
get
{
ActivateForRead();
return List.Count;
}
}
public bool IsReadOnly
{
get
{
ActivateForRead();
return ((IList) List).IsReadOnly;
}
}
public void Add(T t)
{
ActivateForWrite();
List.Add(t);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, t));
}
public void Clear()
{
ActivateForWrite();
List.Clear();
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public bool Contains(T t)
{
ActivateForRead();
return List.Contains(t);
}
public void CopyTo(T[] array, int index)
{
ActivateForRead();
List.CopyTo(array, index);
}
public IEnumerator<T> GetEnumerator()
{
ActivateForRead();
return List.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool Remove(T t)
{
ActivateForWrite();
bool removed = List.Remove(t);
if (removed)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, t));
return removed;
}
[Transient]
private IActivator _activator;
public virtual void Bind(IActivator activator)
{
if (_activator == activator)
return;
if (activator != null && _activator != null)
throw new InvalidOperationException();
_activator = activator;
}
public virtual void Activate(ActivationPurpose purpose)
{
if (_activator == null)
return;
_activator.Activate(purpose);
}
protected virtual void ActivateForRead()
{
Activate(ActivationPurpose.Read);
}
protected virtual void ActivateForWrite()
{
Activate(ActivationPurpose.Write);
}
[Transient]
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
CollectionChanged(this, e);
}
}
I've also tried copying the code from GenericTypeHandlerPredicate and registering my ActivatableCollection to use the GenericCollectionTypeHandler. That results in a crash in GenericTypeFor() throwing an InvalidOperationException() when "Mercury" is being stored.
Just want to mention my answers from the db4o forums also here, for people with a similar problem:
First part of the issue:
From db4o's point of view nothing has changed in the 'ActivatableCollection' object and therefore no changes are stored. This is what is happening:
When you add the items, the ActivatableCollection is marked as changed.
When you commit the changes are stored. However the ' ActivatableCollection' holds the reference to the same object. db4o only stores the changes in the ActivatableCollection-object, which is the reference to the List. Since it is the same, no actual change is stored.
The List of the ActivatableCollection is never updated, because it wasn't marked as 'changed'
So the transparent activation doesn't see the changes in the list. You can fix your issue simply by using an ActivatableList in you're ActivatableCollection implementation. Just change the List with a IList interface and instantiate a ActivatableList instead of an List.
The second part of the issue: Why doesn't it work even when registering the GenericCollectionTypeHandler for this type? Here we hit a implementation detail. The GenericCollectionTypeHandler has an internal list of supported types, which doesn't include the self made 'ActivatableCollection'. GenericCollectionTypeHandler is not really part of the public API and intendet for internal use only.
Workaround / Fix
Just use an ActivatableList<T> instead of a List<T>. then everything works fine.

Sharepoint 2007 - cant find my modifications to web.config in SpWebApplication.WebConfigModifications

I cant seem to find the modifications I made to web.config in my FeatureRecievers Activated event. I try to get the modifications from the SpWebApplication.WebConfigModifications collection in the deactivate event, but this is always empty.... And the strangest thing is that my changes are still reverted after deactivating the feature...
My question is, should I not be able to view all changes made to the web.config files when accessing the SpWebApplication.WebConfigModifications collection in the Deactivating event? How should I go about to remove my changes explicitly?
public class FeatureReciever : SPFeatureReceiver
{
private const string FEATURE_NAME = "HelloWorld";
private class Modification
{
public string Name;
public string XPath;
public string Value;
public SPWebConfigModification.SPWebConfigModificationType ModificationType;
public bool createOnly;
public Modification(string name, string xPath, string value, SPWebConfigModification.SPWebConfigModificationType modificationType, bool createOnly)
{
Name = name;
XPath = xPath;
Value = value;
ModificationType = modificationType;
this.createOnly = createOnly;
}
}
private Modification[] modifications =
{
new Modification("connectionStrings", "configuration", "<connectionStrings/>", SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, true),
new Modification("add[#name='ConnectionString'][#connectionString='Data Source=serverName;Initial Catalog=DBName;User Id=UserId;Password=Pass']", "configuration/connectionStrings", "<add name='ConnectionString' connectionString='Data Source=serverName;Initial Catalog=DBName;User Id=UserId;Password=Pass'/>", SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, false)
};
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite siteCollection = (properties.Feature.Parent as SPWeb).Site as SPSite;
SPWebApplication webApplication = siteCollection.WebApplication;
siteCollection.RootWeb.Title = "Set from activating code at " + DateTime.Now.ToString();
foreach (Modification entry in modifications)
{
SPWebConfigModification webConfigModification = CreateModification(entry);
webApplication.WebConfigModifications.Add(webConfigModification);
}
webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApplication.WebService.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite siteCollection = (properties.Feature.Parent as SPWeb).Site as SPSite;
SPWebApplication webApplication = siteCollection.WebApplication;
siteCollection.RootWeb.Title = "Set from deactivating code at " + DateTime.Now.ToString();
IList<SPWebConfigModification> modifications = webApplication.WebConfigModifications;
foreach (SPWebConfigModification modification in modifications)
{
if (modification.Owner == FEATURE_NAME)
{
webApplication.WebConfigModifications.Remove(modification);
}
}
webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApplication.WebService.Update();
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
private SPWebConfigModification CreateModification(Modification entry)
{
SPWebConfigModification spWebConfigModification = new SPWebConfigModification()
{
Name = entry.Name,
Path = entry.XPath,
Owner = FEATURE_NAME,
Sequence = 0,
Type = entry.ModificationType,
Value = entry.Value
};
return spWebConfigModification;
}
}
Thanks for your time.
/Hans
Finally today I figured out what was wrong with my code (that is why the WebConfigModifications collection was empty when i queryied it in the deactivate event) it seems you must apply the changes in a different manner than I had done.
My original approach to applying the changes involved the following code:
Activate event
webApplication.Farm.Services.GetValue().ApplyWebConfigModifications();
webApplication.WebService.Update();
The "correct" way of doing it is this:
SPWebService.ContentService.ApplyWebConfigModifications();
webApplication.Update();
Although I am still at a loss why my original code did not work.. could someone with more knowlege of the configuration object in Sharepoint enlighten me?

Resources