165ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley/*
265ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley * Copyright (C) 2017 The Android Open Source Project
365ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley *
465ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley * Licensed under the Apache License, Version 2.0 (the "License");
565ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley * you may not use this file except in compliance with the License.
665ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley * You may obtain a copy of the License at
765ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley *
865ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley *      http://www.apache.org/licenses/LICENSE-2.0
965ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley *
1065ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley * Unless required by applicable law or agreed to in writing, software
1165ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley * distributed under the License is distributed on an "AS IS" BASIS,
1265ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley * See the License for the specific language governing permissions and
1465ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley * limitations under the License.
1565ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley */
1665ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley
1765ab191c2a396a77d24227719c7c1e7656b48084Sean Kelleypackage com.example.android.support.wear.app.drawers;
1865ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley
1965ab191c2a396a77d24227719c7c1e7656b48084Sean Kelleyimport android.app.Activity;
2065ab191c2a396a77d24227719c7c1e7656b48084Sean Kelleyimport android.app.Fragment;
2165ab191c2a396a77d24227719c7c1e7656b48084Sean Kelleyimport android.os.Bundle;
2265ab191c2a396a77d24227719c7c1e7656b48084Sean Kelleyimport android.support.wear.widget.drawer.WearableActionDrawerView;
2365ab191c2a396a77d24227719c7c1e7656b48084Sean Kelleyimport android.support.wear.widget.drawer.WearableNavigationDrawerView;
2465ab191c2a396a77d24227719c7c1e7656b48084Sean Kelleyimport android.util.Log;
2565ab191c2a396a77d24227719c7c1e7656b48084Sean Kelleyimport android.view.MenuItem;
2665ab191c2a396a77d24227719c7c1e7656b48084Sean Kelleyimport android.widget.Toast;
2765ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley
2865ab191c2a396a77d24227719c7c1e7656b48084Sean Kelleyimport com.example.android.support.wear.R;
2965ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley
3065ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley/** Main {@link Activity} for demoing the Wearable Drawers. */
3165ab191c2a396a77d24227719c7c1e7656b48084Sean Kelleypublic class WearableDrawersDemo extends Activity {
3265ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley    private static final String TAG = "WearableDrawersDemo";
3365ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley
3465ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley    private WearableNavigationDrawerView mNavDrawer;
3565ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley    private WearableActionDrawerView mActionDrawer;
3665ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley    private NavItem[] mNavItems;
3765ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley
3865ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley    @Override
3965ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley    protected void onCreate(Bundle savedInstanceState) {
4065ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        super.onCreate(savedInstanceState);
4165ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        setContentView(R.layout.wearable_drawers_demo);
4265ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        mNavItems = new NavItem[] {
4365ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley                new NavItem(
4465ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley                        ScrollViewFragment.class,
4565ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley                        "ScrollView",
4665ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley                        getDrawable(android.R.drawable.star_big_off)),
4765ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley                new NavItem(
4865ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley                        FrameLayoutFragment.class,
4965ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley                        "FrameLayout",
5065ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley                        getDrawable(android.R.drawable.star_big_on)),
5165ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        };
5265ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley
5365ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        onNavItemSelected(0);
5465ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley
5565ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        mNavDrawer = findViewById(R.id.nav_drawer);
5665ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        mNavDrawer.setAdapter(new DemoNavDrawerAdapter(mNavItems));
5765ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        mNavDrawer.addOnItemSelectedListener(this::onNavItemSelected);
5865ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        mNavDrawer.getController().peekDrawer();
5965ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley
6065ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        mActionDrawer = findViewById(R.id.action_drawer);
6165ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        mActionDrawer.setOnMenuItemClickListener(this::onActionClicked);
6265ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        mActionDrawer.getController().peekDrawer();
6365ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley    }
6465ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley
6565ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley    private boolean onActionClicked(MenuItem menuItem) {
6665ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        Toast.makeText(this, menuItem.getTitle() + " clicked", Toast.LENGTH_SHORT).show();
6765ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        mActionDrawer.getController().peekDrawer();
6865ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        return true;
6965ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley    }
7065ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley
7165ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley    private void onNavItemSelected(int pos) {
7265ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        Fragment fragment;
7365ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        try {
7465ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley            fragment = mNavItems[pos].getFragment().newInstance();
7565ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        } catch (InstantiationException | IllegalAccessException e) {
7665ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley            Log.e(TAG, "Failed to instantiate fragment", e);
7765ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley            return;
7865ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        }
7965ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley
8065ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley        getFragmentManager()
8165ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley                .beginTransaction()
8265ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley                .replace(R.id.fragment_container, fragment)
8365ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley                .commit();
8465ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley    }
8565ab191c2a396a77d24227719c7c1e7656b48084Sean Kelley}
86