PlaybackOverlayFragment.java revision 6dca725412977bb56b933bdec120e31909233cdb
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package android.support.v17.leanback.app;
15
16import android.os.Bundle;
17import android.support.v17.leanback.R;
18import android.support.v17.leanback.widget.ObjectAdapter;
19import android.support.v17.leanback.widget.ObjectAdapter.DataObserver;
20import android.support.v17.leanback.widget.VerticalGridView;
21
22
23/**
24 * A fragment for displaying playback controls and related content.
25 * The {@link android.support.v17.leanback.widget.PlaybackControlsRow} is expected to be
26 * at position 0 in the adapter.
27 */
28public class PlaybackOverlayFragment extends DetailsFragment {
29
30    private int mAlignPosition;
31
32    /**
33     * Sets the list of rows for the fragment.
34     */
35    @Override
36    public void setAdapter(ObjectAdapter adapter) {
37        if (getAdapter() != null) {
38            getAdapter().unregisterObserver(mObserver);
39        }
40        super.setAdapter(adapter);
41        if (adapter != null) {
42            adapter.registerObserver(mObserver);
43        }
44        setVerticalGridViewLayout(getVerticalGridView());
45    }
46
47    @Override
48    void setVerticalGridViewLayout(VerticalGridView listview) {
49        if (listview == null || getAdapter() == null) {
50            return;
51        }
52        final int alignPosition = getAdapter().size() > 1 ? mAlignPosition : 0;
53        listview.setItemAlignmentOffset(alignPosition);
54        listview.setItemAlignmentOffsetPercent(100);
55        listview.setWindowAlignmentOffset(0);
56        listview.setWindowAlignmentOffsetPercent(100);
57        listview.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_HIGH_EDGE);
58    }
59
60    @Override
61    public void onCreate(Bundle savedInstanceState) {
62        super.onCreate(savedInstanceState);
63
64        mAlignPosition =
65            getResources().getDimensionPixelSize(R.dimen.lb_playback_controls_align_bottom);
66    }
67
68    private final DataObserver mObserver = new DataObserver() {
69        public void onChanged() {
70            setVerticalGridViewLayout(getVerticalGridView());
71        }
72    };
73}
74