/*
 * Copyright 2014 Soichiro Kashima
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.ksoichiro.android.observablescrollview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;

public class ObservableWebView extends WebView {
    private ObservableScrollViewCallbacks mCallbacks;
    private int mPrevScrollY;
    private int mScrollY;
    private ScrollState mScrollState;
    private boolean mFirstScroll;
    private boolean mDragging;

    public ObservableWebView(Context context) {
        super(context);
    }

    public ObservableWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ObservableWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mCallbacks != null) {
            mScrollY = t;

            mCallbacks.onScrollChanged(t, mFirstScroll, mDragging);
            if (mFirstScroll) {
                mFirstScroll = false;
            }

            if (mPrevScrollY < t) {
                //down
                mScrollState = ScrollState.UP;
            } else if (t < mPrevScrollY) {
                //up
                mScrollState = ScrollState.DOWN;
            } else {
                mScrollState = ScrollState.STOP;
            }
            mPrevScrollY = t;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mCallbacks != null) {
            switch (ev.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    mFirstScroll = mDragging = true;
                    mCallbacks.onDownMotionEvent();
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    mDragging = false;
                    mCallbacks.onUpOrCancelMotionEvent(mScrollState);
                    break;
            }
        }
        return super.onTouchEvent(ev);
    }

    public void setScrollViewCallbacks(ObservableScrollViewCallbacks listener) {
        mCallbacks = listener;
    }

    public int getCurrentScrollY() {
        return mScrollY;
    }
}

@@ -686,16 +686,72 @@
 ontext;%0A
+import android.os.Parcel;%0Aimport android.os.Parcelable;%0A
 import a
@@ -776,16 +776,16 @@
 uteSet;%0A
-
 import a
@@ -1411,32 +1411,553 @@
 fStyle);%0A    %7D%0A%0A
+    @Override%0A    public void onRestoreInstanceState(Parcelable state) %7B%0A        SavedState ss = (SavedState) state;%0A        mPrevScrollY = ss.prevScrollY;%0A        mScrollY = ss.scrollY;%0A        super.onRestoreInstanceState(ss.getSuperState());%0A    %7D%0A%0A    @Override%0A    public Parcelable onSaveInstanceState() %7B%0A        Parcelable superState = super.onSaveInstanceState();%0A        SavedState ss = new SavedState(superState);%0A        ss.prevScrollY = mPrevScrollY;%0A        ss.scrollY = mScrollY;%0A        return ss;%0A    %7D%0A%0A
     @Override%0A  
@@ -3408,24 +3408,24 @@
 ScrollY() %7B%0A
-
         retu
@@ -3442,11 +3442,970 @@
 Y;%0A    %7D
+%0A%0A    static class SavedState extends BaseSavedState %7B%0A        int prevScrollY;%0A        int scrollY;%0A%0A        SavedState(Parcelable superState) %7B%0A            super(superState);%0A        %7D%0A%0A        private SavedState(Parcel in) %7B%0A            super(in);%0A            prevScrollY = in.readInt();%0A            scrollY = in.readInt();%0A        %7D%0A%0A        @Override%0A        public void writeToParcel(Parcel out, int flags) %7B%0A            super.writeToParcel(out, flags);%0A            out.writeInt(prevScrollY);%0A            out.writeInt(scrollY);%0A        %7D%0A%0A        public static final Parcelable.Creator%3CSavedState%3E CREATOR%0A                = new Parcelable.Creator%3CSavedState%3E() %7B%0A            @Override%0A            public SavedState createFromParcel(Parcel in) %7B%0A                return new SavedState(in);%0A            %7D%0A%0A            @Override%0A            public SavedState%5B%5D newArray(int size) %7B%0A                return new SavedState%5Bsize%5D;%0A            %7D%0A        %7D;%0A    %7D
 %0A%7D%0A

Fix that the scroll position of ObservableWebView is not restored after rotating the screen.