1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.support.design.widget;
18
19import com.example.android.support.design.R;
20import com.example.android.support.design.Shakespeare;
21
22import android.os.Bundle;
23import android.support.annotation.Nullable;
24import android.support.design.widget.BottomSheetBehavior;
25import android.support.v7.app.AppCompatActivity;
26import android.text.TextUtils;
27import android.widget.LinearLayout;
28import android.widget.TextView;
29
30/**
31 * This demonstrates basic usage of {@link BottomSheetBehavior}.
32 */
33abstract class BottomSheetUsageBase extends AppCompatActivity {
34
35    protected BottomSheetBehavior<LinearLayout> mBehavior;
36
37    @Override
38    protected void onCreate(@Nullable Bundle savedInstanceState) {
39        super.onCreate(savedInstanceState);
40        setContentView(getLayoutId());
41        ((TextView) findViewById(R.id.dialogue)).setText(TextUtils.concat(Shakespeare.DIALOGUE));
42        mBehavior = BottomSheetBehavior.from((LinearLayout) findViewById(R.id.bottom_sheet));
43    }
44
45    @Override
46    public void onBackPressed() {
47        if (mBehavior != null) {
48            int state = mBehavior.getState();
49            if (state != BottomSheetBehavior.STATE_COLLAPSED &&
50                    state != BottomSheetBehavior.STATE_HIDDEN) {
51                mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
52                return;
53            }
54        }
55        super.onBackPressed();
56    }
57
58    protected abstract int getLayoutId();
59
60}
61