private ToggleButton CreateDeviceSelectionItem(Device moj)
        {
            ToggleButton button = new ToggleButton (this);
            if (devicesToShow.Contains (moj))
                button.Checked = true;
            else
                button.Checked = false;

            button.SetBackgroundResource (Resource.Drawable.android_blue_button);
            button.Text = string.Format ("{0}", moj.Name);
            button.Tag = moj.Id;
            button.Click += OnDeviceSelected;
            return button;
        }
 public void RemoveFromSubscriptionList(EventType eventType, Device device)
 {
     switch (eventType) {
     case EventType.TowStart:
         if (_devicesForTowEvent.Find (x => x.Id == device.Id && x.Name == device.Name) != null)
             _devicesForTowEvent.RemoveAll (x => x.Id == device.Id && x.Name == device.Name);
         break;
     default:
         break;
     }
 }
 public bool GetSubscriptionStatus(EventType eventType, Device device)
 {
     switch (eventType) {
     case EventType.TowStart:
         return _devicesForTowEvent.Find (x => x.Id == device.Id && x.Name == device.Name) != null;
     default:
         return false;
     }
 }
 private Button CreateDeviceSelectionItem(Device moj)
 {
     Button button = new Button (this);
     button.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.android_button));
     button.SetMaxHeight (25);
     button.Text = string.Format (moj.Name);
     button.Tag = moj.Id;
     button.Click += OnDeviceSelected;
     return button;
 }
        private Marker AddDeviceMarkerToMap(Device dev)
        {
            var loc = new LatLng (dev.LastLocation.Lat, dev.LastLocation.Lng);
            MarkerOptions marker = new MarkerOptions ();
            marker.InvokeIcon (BitmapDescriptorFactory.DefaultMarker (BitmapDescriptorFactory.HueGreen));
            marker.SetPosition (loc);
            String location;
            location = GetAddress (loc.Latitude, loc.Longitude);
            if (location == null)
                location = loc.Latitude.ToString() + ", " + loc.Longitude.ToString();
            marker.SetSnippet (location);
            marker.SetTitle (dev.Name);

            MyLogger.Information (this.LocalClassName, string.Format ("Device Marker Added: {0} at {1}", dev.Name, loc.ToString ()));
            return AddMarkerToMap (marker);
        }
 private void OnDeviceSubscriptionToggleClicked(Device dev, EventType eventType, bool isChecked)
 {
     MyLogger.Information (this.LocalClassName, string.Format ("User Preference: {0} for {1} Set to {2}", eventType, dev, isChecked));
     if (isChecked)
         CurrentUserPreference.AddToSubscriptionList (EventType.TowStart, dev);
     else
         CurrentUserPreference.RemoveFromSubscriptionList (EventType.TowStart, dev);
     OnSubscriptionChanged (dev, eventType, isChecked);
     SaveUserPreferences ();
 }
		protected bool SubscribeForEvent (Device mojioDevice, EventType eventToSubscribe)
		{
			HttpStatusCode statusCode;
			string msg;
			bool succeed = true;
			Subscription subscription = SubscribeForEvent (RegistrationId, out statusCode, out msg, mojioDevice, eventToSubscribe);
			Subscriptions.Add (subscription);
			if (subscription != null)
				MyLogger.Information (this.LocalClassName, string.Format ("Event Subscription: {0} - {1}.", "Successful", msg));	                    
			if (IsAlreadySubscribed (statusCode))
				MyLogger.Information (this.LocalClassName, string.Format ("Event Subscription: {0} Event already subscribed.", eventToSubscribe.ToString ()));	
			if (subscription == null && !IsAlreadySubscribed (statusCode)) {
				succeed = false;         
				MyLogger.Error (this.LocalClassName, string.Format ("Event Subscription: {0} - {1}.", "Fail", msg));
			}
				                    
			
			return succeed;
		}
		//TODO [GROUP 32] I know this is bad implementation. For now, given a device, it checks the user preference to see
		//which events need to be subscribed for the device. Should improve the implementation.
		protected void RegisterEventForNotice (Device dev, EventType eventType, bool toSubscribe)
		{
			if (string.IsNullOrEmpty (RegistrationId))
				RegistrationId = PushClient.GetRegistrationId (this.ApplicationContext);
			var trials = 3; 
			var device = UserDevices.First (x => x.Id == dev.Id);
			if (device == null) {
				MyLogger.Error (this.LocalClassName, string.Format ("Device {0} not found. Subscription canceled.", dev.Id));
				return;
			}
			do {
				if (!toSubscribe) {
					if (UnsubscribeForEvent (device, eventType))
						return;	
				} else {
					if (SubscribeForEvent (device, eventType))
						return;
				}
				trials--;
			} while (trials > 0);
			MyLogger.Error (this.LocalClassName, string.Format ("{0} Subscription/Unsubscription failed.", dev));
		}
		protected bool UnsubscribeForEvent (Device device, EventType eventType)
		{
			Subscription subscription = Subscriptions.FirstOrDefault (x => x.EntityId == device.Id);
			if (subscription == null)
				return true;
			bool succeed = Client.Delete (subscription);
			MyLogger.Information (this.LocalClassName, string.Format ("Unsubscription: {0} for event type {1} - {2}", device.Id, eventType, succeed ? "Successful" : "Failed"));
			return succeed;
		}
		private Subscription SubscribeForEvent (string registrationId, out HttpStatusCode httpStatusCode, out string msg, Device mojioDevice, EventType eventToSubscribe)
		{
			return Client.SubscribeGcm (registrationId, new Subscription () {
				Event = eventToSubscribe,
				EntityId = mojioDevice.Id,
				EntityType = SubscriptionType.Mojio,
			}, out httpStatusCode, out msg);
		}