/*
 * Copyright (c) 2016 Diona Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of Diona
 * ("Confidential Information"). You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement you
 * entered into with Diona.
 */
package org.odk.collect.android.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.widget.DatePicker;

/**
 * A DatePicker View that intercepts scroll events from its parent. This means that its content will scroll correctly.
 */
public class ScrollableDatePicker extends DatePicker {

  /**
   * @param context
   *     the Context in which the View will be inflated.
   */
  public ScrollableDatePicker(final Context context) {
    super(context);
  }

  /**
   * @param context
   *     the Context in which the View will be inflated.
   * @param attrs
   *     XML defined attributes.
   */
  public ScrollableDatePicker(final Context context, final AttributeSet attrs) {
    super(context, attrs);
  }

  /**
   * @param context
   *     the Context in which the View will be inflated.
   * @param attrs
   *     XML defined attributes.
   * @param defStyleAttr
   *     the defined style for the View.
   */
  public ScrollableDatePicker(final Context context, final AttributeSet attrs, final int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
      ViewParent p = getParent();
      if (p != null) {
        // We want to intercept touch events so we can scroll content correctly.
        p.requestDisallowInterceptTouchEvent(true);
      }
    }

    return false;
  }
}
