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