/// <summary>
        /// Hides the registration cards, reset the counter and show the step counting card
        /// </summary>
        private void ShowCountingCards()
        {
            // Hide the registration cards
            CardStream.HideCard(CARD_REGISTER_DETECTOR);
            CardStream.HideCard(CARD_REGISTER_COUNTER);

            // show the explanation card if it has not been dismissed
            CardStream.ShowCard(CARD_EXPLANATION);

            // Reset the step counter, then show the step counting card
            ResetCounter();

            // Set the initial text for the step counting card before a step is recorded
            String sensor = "-";

            if (mState == STATE_COUNTER)
            {
                sensor = GetString(Resource.String.sensor_counter);
            }
            else if (mState == STATE_DETECTOR)
            {
                sensor = GetString(Resource.String.sensor_detector);
            }

            // Set initial text
            CardStream.GetCard(CARD_COUNTING)
            .SetTitle(GetString(Resource.String.counting_title, 0))
            .SetDescription(GetString(Resource.String.counting_description, sensor, mMaxDelay, "-"));

            // Show the counting card and make it undismissible
            CardStream.ShowCard(CARD_COUNTING, false);
        }
        /// <summary>
        /// Show the introduction card
        /// </summary>
        private void ShowIntroCard()
        {
            Card c = new Card.Builder(this, CARD_INTRO)
                     .SetTitle(GetString(Resource.String.intro_title))
                     .SetDescription(GetString(Resource.String.intro_message))
                     .Build(Activity);

            CardStream.AddCard(c, true);
        }
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);


            mListener = new SensorEventListener()
            {
                OnSensorChangedAction = (SensorEvent e) =>
                {
                    // Store the delay of this event
                    RecordDelay(e);
                    string delayString = DelayString;

                    if (e.Sensor.Type == SensorType.StepDetector)
                    {
                        // A step detector event is received for each step.
                        // This means we need to count steps ourselves

                        mSteps += e.Values.Count;

                        // Update the card with the latest step count
                        CardStream.GetCard(CARD_COUNTING).SetTitle(GetString(Resource.String.counting_title, new Java.Lang.Integer(mSteps)))
                        .SetDescription(GetString(Resource.String.counting_description,
                                                  GetString(Resource.String.sensor_detector), new Java.Lang.Integer(mMaxDelay), new Java.Lang.String(delayString)));
                        Log.Info(TAG, "New step detected by STEP_DETECTOR sensor. Total step count: " + mSteps);
                    }
                    else if (e.Sensor.Type == SensorType.StepCounter)
                    {
                        // A step counter event contains the total number of steps since the listener was first registered.
                        // We need to keep track of this initial value to calculate the number of steps taken,
                        // as the first value a listener receives is undefined

                        if (mCounterSteps < 1)
                        {
                            // Initial value
                            mCounterSteps = (int)e.Values[0];
                        }

                        // Calculate stes taken based on first counter value received
                        mSteps = (int)e.Values[0] - mCounterSteps;

                        // Add the number of steps previously taken, otherwise the counter would start as 0.
                        // This is needed to keep the counter consistent across rotation changes.
                        mSteps += mPreviousCounterSteps;

                        // Update the card with the latest step count
                        CardStream.GetCard(CARD_COUNTING).SetTitle(GetString(Resource.String.counting_title, new Java.Lang.Integer(mSteps)))
                        .SetDescription(GetString(Resource.String.counting_description,
                                                  GetString(Resource.String.sensor_counter), new Java.Lang.Integer(mMaxDelay), new Java.Lang.String(delayString)));
                        Log.Info(TAG, "New step detected by STEP_COUNTER sensor. Total step count: " + mSteps);
                    }
                }
            };
        }
        /// <summary>
        /// Show two registration cards, one for the step detector and counter sensors
        /// </summary>
        private void ShowRegisterCard()
        {
            // Hide the counting and explanation cards
            CardStream.HideCard(CARD_BATCHING_DESCRIPTION);
            CardStream.HideCard(CARD_EXPLANATION);
            CardStream.HideCard(CARD_COUNTING);

            // Show two undismissable registration cards, one for each step sensor
            CardStream.ShowCard(CARD_REGISTER_DETECTOR, false);
            CardStream.ShowCard(CARD_REGISTER_COUNTER, false);
        }
        /// <summary>
        /// Handles a click on a card action.
        /// Registers a SensorEventListener with the selected delay, dismisses cards or unregisters the listener.
        /// Actions are defined when a card is created
        /// </summary>
        /// <param name="cardActionId"></param>
        /// <param name="cardTag"></param>
        public void OnCardClick(int cardActionId, string cardTag)
        {
            switch (cardActionId)
            {
            // Register Step Counter card
            case ACTION_REGISTER_COUNT_NOBATCHING:
                RegisterEventListener(BATCH_LATENCY_0, SensorType.StepCounter);
                break;

            case ACTION_REGISTER_COUNT_BATCHING_5s:
                RegisterEventListener(BATCH_LATENCY_5s, SensorType.StepCounter);
                break;

            case ACTION_REGISTER_COUNT_BATCHING_10s:
                RegisterEventListener(BATCH_LATENCY_10s, SensorType.StepCounter);
                break;

            // Register Step Detector card
            case ACTION_REGISTER_DETECT_NOBATCHING:
                RegisterEventListener(BATCH_LATENCY_0, SensorType.StepDetector);
                break;

            case ACTION_REGISTER_DETECT_BATCHING_5s:
                RegisterEventListener(BATCH_LATENCY_5s, SensorType.StepDetector);
                break;

            case ACTION_REGISTER_DETECT_BATCHING_10s:
                RegisterEventListener(BATCH_LATENCY_10s, SensorType.StepDetector);
                break;

            // Unregister card
            case ACTION_UNREGISTER:
                ShowRegisterCard();
                UnregisterListeners();
                // reset the application state when explicitly unregistered
                mState = STATE_OTHER;
                break;

            // Explanation cards
            case ACTION_BATCHING_DESCRIPTION_DISMISS:
                // permanently remove the batch description card, it will not be shown again
                CardStream.RemoveCard(CARD_BATCHING_DESCRIPTION);
                break;

            case ACTION_EXPLANATION_DISMISS:
                // permanently remove the explanation card, it will not be shown again
                CardStream.RemoveCard(CARD_EXPLANATION);
                break;
            }
            if (cardTag == CARD_REGISTER_COUNTER || cardTag == CARD_REGISTER_DETECTOR)
            {
                ShowCountingCards();
            }
        }
        /// <summary>
        /// Initializes the cards.
        /// </summary>
        private void InitializeCards()
        {
            // Step counting
            Card c = new Card.Builder(this, CARD_COUNTING)
                     .SetTitle("Steps")
                     .SetDescription("")
                     .AddAction("Unregister Listener", ACTION_UNREGISTER, Card.ACTION_NEGATIVE)
                     .Build(Activity);

            CardStream.AddCard(c);

            // Register step detector listener
            c = new Card.Builder(this, CARD_REGISTER_DETECTOR)
                .SetTitle(GetString(Resource.String.register_detector_title))
                .SetDescription(GetString(Resource.String.register_detector_description))
                .AddAction(GetString(Resource.String.register_0), ACTION_REGISTER_DETECT_NOBATCHING, Card.ACTION_NEUTRAL)
                .AddAction(GetString(Resource.String.register_5), ACTION_REGISTER_DETECT_BATCHING_5s, Card.ACTION_NEUTRAL)
                .AddAction(GetString(Resource.String.register_10), ACTION_REGISTER_DETECT_BATCHING_10s, Card.ACTION_NEUTRAL)
                .Build(Activity);
            CardStream.AddCard(c);

            // Register step count listener
            c = new Card.Builder(this, CARD_REGISTER_COUNTER)
                .SetTitle(GetString(Resource.String.register_counter_title))
                .SetDescription(GetString(Resource.String.register_counter_description))
                .AddAction(GetString(Resource.String.register_0), ACTION_REGISTER_COUNT_NOBATCHING, Card.ACTION_NEUTRAL)
                .AddAction(GetString(Resource.String.register_5), ACTION_REGISTER_COUNT_BATCHING_5s, Card.ACTION_NEUTRAL)
                .AddAction(GetString(Resource.String.register_10), ACTION_REGISTER_COUNT_BATCHING_10s, Card.ACTION_NEUTRAL)
                .Build(Activity);
            CardStream.AddCard(c);

            // Batching description
            c = new Card.Builder(this, CARD_BATCHING_DESCRIPTION)
                .SetTitle(GetString(Resource.String.batching_queue_title))
                .SetDescription(GetString(Resource.String.batching_queue_description))
                .AddAction(GetString(Resource.String.action_notagain), ACTION_BATCHING_DESCRIPTION_DISMISS, Card.ACTION_POSITIVE)
                .Build(Activity);
            CardStream.AddCard(c);

            // Explanation
            c = new Card.Builder(this, CARD_EXPLANATION)
                .SetDescription(GetString(Resource.String.explanation_description))
                .AddAction(GetString(Resource.String.action_notagain), ACTION_EXPLANATION_DISMISS, Card.ACTION_POSITIVE)
                .Build(Activity);
            CardStream.AddCard(c);

            // Error
            c = new Card.Builder(this, CARD_NOBATCHSUPPORT)
                .SetTitle(GetString(Resource.String.error_title))
                .SetDescription(GetString(Resource.String.error_nosensor))
                .Build(Activity);
            CardStream.AddCard(c);
        }
        /// <summary>
        /// Register a SensorEventListener for the sensor and max batch delay.
        /// The maximum batch delay specifies the maximum duration in microseconds for which subsequent sensor events can be temporarily stored
        /// before they are delivered to the registered SensorEventListener.
        /// </summary>
        /// <param name="maxDelay">Max delay.</param>
        /// <param name="sensorType">Sensor type.</param>
        void RegisterEventListener(int maxDelay, SensorType sensorType)
        {
            // Keep track of the state so that the correct sensor type and batch delay can be set up when the app is restored (for example, on screen rotation)

            mMaxDelay = maxDelay;

            if (sensorType == SensorType.StepCounter)
            {
                mState = STATE_COUNTER;
                // Reset the initial step counter calue, the first event received by the event listener is
                // stored in mCounterSteps and used to calculate the total number of steps taken.
                mCounterSteps = 0;
                Log.Info(TAG, "Event listener for step counter sensor registered with a max delay of " + mMaxDelay);
            }
            else
            {
                mState = STATE_DETECTOR;
                Log.Info(TAG, "Event listener for step detector sensor registered with a max delay of " + mMaxDelay);
            }

            // Get the default sensor for the sensor type from the SensorManager
            SensorManager sensorManager = (SensorManager)Activity.GetSystemService(Service.SensorService);
            // sensorType is either SensorType.StepCounter or SensorType.StepDetector
            Sensor sensor = sensorManager.GetDefaultSensor(sensorType);

            // Register the listener for this sensor in batch mode.
            // If the max delay is 0, events will be delivered in continuous mode without batching.
            bool batchMode = sensorManager.RegisterListener(mListener, sensor, SensorDelay.Normal, maxDelay);

            if (!batchMode)
            {
                // Batch mode could not be enabled, show a warning message and switch to continuous mode
                CardStream.GetCard(CARD_NOBATCHSUPPORT).SetDescription(GetString(Resource.String.warning_nobatching));
                CardStream.ShowCard(CARD_NOBATCHSUPPORT);
                Log.Warn(TAG, "Could not register sensor listener in batch mode, falling back to continuous mode.");
            }

            if (maxDelay > 0 && batchMode)
            {
                // Batch mode was enabled successfully, show a description card
                CardStream.ShowCard(CARD_BATCHING_DESCRIPTION);
            }

            // Show the explanation card
            CardStream.ShowCard(CARD_EXPLANATION);
        }
 /// <summary>
 /// Shows the error card.
 /// </summary>
 private void ShowErrorCard()
 {
     CardStream.ShowCard(CARD_NOBATCHSUPPORT, false);
 }