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