17bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam/*
27bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * Copyright (C) 2016 The Android Open Source Project
37bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam *
47bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * Licensed under the Apache License, Version 2.0 (the "License");
57bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * you may not use this file except in compliance with the License.
67bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * You may obtain a copy of the License at
77bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam *
87bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam *      http://www.apache.org/licenses/LICENSE-2.0
97bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam *
107bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * Unless required by applicable law or agreed to in writing, software
117bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * distributed under the License is distributed on an "AS IS" BASIS,
127bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * See the License for the specific language governing permissions and
147bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * limitations under the License.
157bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam */
167bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
177bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lampackage com.android.setupwizardlib.util;
187bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
197bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport android.os.Build;
207bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport android.widget.AbsListView;
217bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport android.widget.ListAdapter;
227bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport android.widget.ListView;
237bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
247bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport com.android.setupwizardlib.view.NavigationBar;
257bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
267bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam/**
277bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * Add this helper to require the list view to be scrolled to the bottom, making sure that the
287bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * user sees all content on the screen. This will change the navigation bar to show the more button
297bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * instead of the next button when there is more content to be seen. When the more button is
307bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * clicked, the list view will be scrolled one page down.
317bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam */
327bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lampublic class ListViewRequireScrollHelper extends AbstractRequireScrollHelper
337bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        implements AbsListView.OnScrollListener {
347bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
357bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    public static void requireScroll(NavigationBar navigationBar, ListView listView) {
367bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        new ListViewRequireScrollHelper(navigationBar, listView).requireScroll();
377bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
387bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
397bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    private final ListView mListView;
407bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
417bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    private ListViewRequireScrollHelper(NavigationBar navigationBar, ListView listView) {
427bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        super(navigationBar);
437bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        mListView = listView;
447bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
457bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
467bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    @Override
477bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    protected void requireScroll() {
487bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
497bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            // APIs to scroll a list only exists on Froyo or above.
507bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            super.requireScroll();
517bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            mListView.setOnScrollListener(this);
527bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
537bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            final ListAdapter adapter = mListView.getAdapter();
547bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            if (mListView.getLastVisiblePosition() < adapter.getCount()) {
557bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                notifyRequiresScroll();
567bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            }
577bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
587bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
597bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
607bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    @Override
617bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    protected void pageScrollDown() {
627bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
637bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            final int height = mListView.getHeight();
647bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            mListView.smoothScrollBy(height, 500);
657bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
667bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
677bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
687bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    @Override
697bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    public void onScrollStateChanged(AbsListView view, int scrollState) {
707bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
717bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
727bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    @Override
737bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
747bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            int totalItemCount) {
757bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        if (firstVisibleItem + visibleItemCount >= totalItemCount) {
767bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            notifyScrolledToBottom();
777bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        } else {
787bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            notifyRequiresScroll();
797bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
807bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
817bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam}
82