1/*
2 * Copyright (C) 2016 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 androidx.appcompat.app;
18
19import android.view.View;
20import android.widget.AdapterView;
21import android.widget.ArrayAdapter;
22import android.widget.ListView;
23import android.widget.TextView;
24
25import androidx.appcompat.test.R;
26import androidx.appcompat.testutils.BaseTestActivity;
27import androidx.appcompat.testutils.Shakespeare;
28import androidx.appcompat.widget.Toolbar;
29import androidx.core.view.GravityCompat;
30import androidx.drawerlayout.widget.DrawerLayout;
31
32/**
33 * Test activity for testing various APIs and interactions for DrawerLayout with start and end
34 * drawers.
35 */
36public class DrawerLayoutDoubleActivity extends BaseTestActivity {
37    private DrawerLayout mDrawerLayout;
38    private ListView mStartDrawer;
39    private View mEndDrawer;
40    private TextView mContent;
41
42    private Toolbar mToolbar;
43
44    @Override
45    protected int getContentViewLayoutResId() {
46        return R.layout.drawer_double_layout;
47    }
48
49    @Override
50    protected void onContentViewSet() {
51        super.onContentViewSet();
52
53        mDrawerLayout = findViewById(R.id.drawer_layout);
54        mStartDrawer = findViewById(R.id.start_drawer);
55        mEndDrawer = findViewById(R.id.end_drawer);
56        mContent = findViewById(R.id.content_text);
57
58        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
59
60        // The drawer title must be set in order to announce state changes when
61        // accessibility is turned on. This is typically a simple description,
62        // e.g. "Navigation".
63        mDrawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.drawer_title));
64
65        mStartDrawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
66                Shakespeare.TITLES));
67        mStartDrawer.setOnItemClickListener(new DrawerItemClickListener());
68
69        // Find the toolbar in our layout and set it as the support action bar on the activity.
70        // This is required to have the drawer slide "over" the toolbar.
71        mToolbar = findViewById(R.id.toolbar);
72        mToolbar.setTitle(R.string.drawer_title);
73        setSupportActionBar(mToolbar);
74
75        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
76        getSupportActionBar().setDisplayShowHomeEnabled(false);
77
78        // Configure the background color fill of the system status bar (on supported platform
79        // versions) and the toolbar itself. We're using the same color, and android:statusBar
80        // from the theme makes the status bar slightly darker.
81        final int metalBlueColor = getResources().getColor(R.color.drawer_sample_metal_blue);
82        mDrawerLayout.setStatusBarBackgroundColor(metalBlueColor);
83        mToolbar.setBackgroundColor(metalBlueColor);
84    }
85
86    @Override
87    public void onBackPressed() {
88        // Is the start drawer open?
89        if (mDrawerLayout.isDrawerOpen(mStartDrawer)) {
90            // Close the drawer and return.
91            mDrawerLayout.closeDrawer(mStartDrawer);
92            return;
93        }
94
95        // Is the end drawer open?
96        if (mDrawerLayout.isDrawerOpen(mEndDrawer)) {
97            // Close the drawer and return.
98            mDrawerLayout.closeDrawer(mEndDrawer);
99            return;
100        }
101
102        super.onBackPressed();
103    }
104
105    /**
106     * This list item click listener implements very simple view switching by changing
107     * the primary content text. The drawer is closed when a selection is made.
108     */
109    private class DrawerItemClickListener implements ListView.OnItemClickListener {
110        @Override
111        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
112            mContent.setText(Shakespeare.DIALOGUE[position]);
113            mToolbar.setTitle(Shakespeare.TITLES[position]);
114            mDrawerLayout.closeDrawer(mStartDrawer);
115        }
116    }
117}
118