1/*
2 * Copyright (C) 2017 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.wear.widget.drawer;
18
19import android.app.Activity;
20import android.content.Intent;
21import android.graphics.drawable.Drawable;
22import android.os.Bundle;
23import android.os.Handler;
24import android.os.Looper;
25import android.util.ArrayMap;
26import android.view.Gravity;
27
28import androidx.annotation.IntDef;
29import androidx.wear.test.R;
30import androidx.wear.widget.drawer.WearableDrawerLayout.DrawerStateCallback;
31import androidx.wear.widget.drawer.WearableNavigationDrawerView.WearableNavigationDrawerAdapter;
32
33import java.lang.annotation.Retention;
34import java.lang.annotation.RetentionPolicy;
35import java.util.Map;
36
37/**
38 * Test {@link Activity} for {@link WearableDrawerLayout} and implementations of {@link
39 * androidx.wear.widget.drawer.WearableDrawerView}.
40 */
41public class DrawerTestActivity extends Activity {
42
43    private static final int DRAWER_SIZE = 5;
44    private static final String STYLE_EXTRA = "style";
45    private static final String OPEN_TOP_IN_ONCREATE_EXTRA = "openTopInOnCreate";
46    private static final String OPEN_BOTTOM_IN_ONCREATE_EXTRA = "openBottomInOnCreate";
47    private static final String CLOSE_FIRST_DRAWER_OPENED = "closeFirstDrawerOpened";
48    private static final Map<Integer, Integer> STYLE_TO_RES_ID = new ArrayMap<>();
49
50    static {
51        STYLE_TO_RES_ID.put(
52                DrawerStyle.BOTH_DRAWER_NAV_MULTI_PAGE,
53                R.layout.test_multi_page_nav_drawer_layout);
54        STYLE_TO_RES_ID.put(
55                DrawerStyle.BOTH_DRAWER_NAV_SINGLE_PAGE,
56                R.layout.test_single_page_nav_drawer_layout);
57        STYLE_TO_RES_ID.put(
58                DrawerStyle.ONLY_ACTION_DRAWER_WITH_TITLE,
59                R.layout.test_only_action_drawer_with_title_layout);
60
61    }
62
63    private final Handler mMainThreadHandler = new Handler(Looper.getMainLooper());
64    private final WearableNavigationDrawerAdapter mDrawerAdapter =
65            new WearableNavigationDrawerAdapter() {
66                @Override
67                public String getItemText(int pos) {
68                    return Integer.toString(pos);
69                }
70
71                @Override
72                public Drawable getItemDrawable(int pos) {
73                    return getDrawable(android.R.drawable.star_on);
74                }
75
76                @Override
77                public int getCount() {
78                    return DRAWER_SIZE;
79                }
80            };
81    private WearableActionDrawerView mActionDrawer;
82    private WearableDrawerLayout mDrawerLayout;
83    private WearableNavigationDrawerView mNavigationDrawer;
84    private final Runnable mCloseTopDrawerRunnable =
85            new Runnable() {
86                @Override
87                public void run() {
88                    mNavigationDrawer.getController().closeDrawer();
89                }
90            };
91    private final DrawerStateCallback mCloseFirstDrawerOpenedCallback =
92            new DrawerStateCallback() {
93                @Override
94                public void onDrawerOpened(WearableDrawerLayout layout,
95                        WearableDrawerView drawerView) {
96                    mMainThreadHandler.postDelayed(mCloseTopDrawerRunnable, 1000);
97                }
98            };
99    @DrawerStyle private int mNavigationStyle;
100    private boolean mOpenTopDrawerInOnCreate;
101    private boolean mOpenBottomDrawerInOnCreate;
102    private boolean mCloseFirstDrawerOpened;
103
104    @Override
105    protected void onCreate(Bundle savedInstanceState) {
106        super.onCreate(savedInstanceState);
107
108        parseIntent(getIntent());
109
110        setContentView(STYLE_TO_RES_ID.get(mNavigationStyle));
111
112        mDrawerLayout = (WearableDrawerLayout) findViewById(R.id.drawer_layout);
113        mNavigationDrawer = (WearableNavigationDrawerView) findViewById(R.id.navigation_drawer);
114        mActionDrawer = (WearableActionDrawerView) findViewById(R.id.action_drawer);
115
116        if (mCloseFirstDrawerOpened) {
117            mDrawerLayout.setDrawerStateCallback(mCloseFirstDrawerOpenedCallback);
118        }
119
120        if (mNavigationDrawer != null) {
121            mNavigationDrawer.setAdapter(mDrawerAdapter);
122            if (mOpenTopDrawerInOnCreate) {
123                mDrawerLayout.openDrawer(Gravity.TOP);
124            } else {
125                mDrawerLayout.peekDrawer(Gravity.TOP);
126            }
127        }
128
129        if (mActionDrawer != null) {
130            if (mOpenBottomDrawerInOnCreate) {
131                mDrawerLayout.openDrawer(Gravity.BOTTOM);
132            } else {
133                mDrawerLayout.peekDrawer(Gravity.BOTTOM);
134            }
135        }
136    }
137
138    private void parseIntent(Intent intent) {
139        //noinspection WrongConstant - Linter doesn't know intent contains a NavigationStyle
140        mNavigationStyle = intent.getIntExtra(STYLE_EXTRA, DrawerStyle.BOTH_DRAWER_NAV_SINGLE_PAGE);
141        mOpenTopDrawerInOnCreate = intent.getBooleanExtra(OPEN_TOP_IN_ONCREATE_EXTRA, false);
142        mOpenBottomDrawerInOnCreate = intent.getBooleanExtra(OPEN_BOTTOM_IN_ONCREATE_EXTRA, false);
143        mCloseFirstDrawerOpened = intent.getBooleanExtra(CLOSE_FIRST_DRAWER_OPENED, false);
144    }
145
146    /**
147     * Which configuration of drawers should be used.
148     */
149    @Retention(RetentionPolicy.SOURCE)
150    @IntDef({
151            DrawerStyle.BOTH_DRAWER_NAV_SINGLE_PAGE,
152            DrawerStyle.BOTH_DRAWER_NAV_MULTI_PAGE,
153            DrawerStyle.ONLY_ACTION_DRAWER_WITH_TITLE
154    })
155    public @interface DrawerStyle {
156        int BOTH_DRAWER_NAV_SINGLE_PAGE = 0;
157        int BOTH_DRAWER_NAV_MULTI_PAGE = 1;
158        int ONLY_ACTION_DRAWER_WITH_TITLE = 2;
159    }
160
161    /**
162     * Builds an {@link Intent} to start this {@link Activity} with the appropriate extras.
163     */
164    public static class Builder {
165
166        @DrawerStyle private int mStyle = DrawerStyle.BOTH_DRAWER_NAV_SINGLE_PAGE;
167        private boolean mOpenTopDrawerInOnCreate = false;
168        private boolean mOpenBottomDrawerInOnCreate = false;
169        private boolean mCloseFirstDrawerOpened = false;
170
171        public Builder setStyle(@DrawerStyle int style) {
172            mStyle = style;
173            return this;
174        }
175
176        public Builder openTopDrawerInOnCreate() {
177            mOpenTopDrawerInOnCreate = true;
178            return this;
179        }
180
181        public Builder openBottomDrawerInOnCreate() {
182            mOpenBottomDrawerInOnCreate = true;
183            return this;
184        }
185
186        public Builder closeFirstDrawerOpened() {
187            mCloseFirstDrawerOpened = true;
188            return this;
189        }
190
191        public Intent build() {
192            return new Intent()
193                    .putExtra(STYLE_EXTRA, mStyle)
194                    .putExtra(OPEN_TOP_IN_ONCREATE_EXTRA, mOpenTopDrawerInOnCreate)
195                    .putExtra(OPEN_BOTTOM_IN_ONCREATE_EXTRA, mOpenBottomDrawerInOnCreate)
196                    .putExtra(CLOSE_FIRST_DRAWER_OPENED, mCloseFirstDrawerOpened);
197        }
198    }
199}
200