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 
17 package androidx.wear.widget.drawer;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.content.res.TypedArray;
22 import android.graphics.drawable.Drawable;
23 import android.text.TextUtils;
24 import android.util.AttributeSet;
25 import android.util.Log;
26 import android.view.Gravity;
27 import android.view.LayoutInflater;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.MenuItem.OnMenuItemClickListener;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.view.accessibility.AccessibilityEvent;
35 import android.view.accessibility.AccessibilityManager;
36 import android.widget.ImageView;
37 import android.widget.LinearLayout;
38 import android.widget.TextView;
39 
40 import androidx.core.view.ViewCompat;
41 import androidx.recyclerview.widget.LinearLayoutManager;
42 import androidx.recyclerview.widget.RecyclerView;
43 import androidx.wear.R;
44 import androidx.wear.internal.widget.ResourcesUtil;
45 import androidx.wear.widget.drawer.WearableActionDrawerMenu.WearableActionDrawerMenuItem;
46 
47 import org.jspecify.annotations.Nullable;
48 
49 /**
50  * Ease of use class for creating a Wearable action drawer. This can be used with {@link
51  * WearableDrawerLayout} to create a drawer for users to easily pull up contextual actions. These
52  * contextual actions may be specified by using a {@link Menu}, which may be populated by either:
53  *
54  * <ul> <li>Specifying the {@code app:actionMenu} attribute in the XML layout file. Example:
55  * <pre>
56  * &lt;androidx.wear.widget.drawer.WearableActionDrawerView
57  *     xmlns:app="http://schemas.android.com/apk/res-auto"
58  *     android:layout_width=”match_parent”
59  *     android:layout_height=”match_parent”
60  *     app:actionMenu="@menu/action_drawer" /&gt;</pre>
61  *
62  * <li>Getting the menu with {@link #getMenu}, and then inflating it with {@link
63  * MenuInflater#inflate}. Example:
64  * <pre>
65  * Menu menu = actionDrawer.getMenu();
66  * getMenuInflater().inflate(R.menu.action_drawer, menu);</pre>
67  *
68  * </ul>
69  *
70  * <p><b>The full {@link Menu} and {@link MenuItem} APIs are not implemented.</b> The following
71  * methods are guaranteed to work:
72  *
73  * <p>For {@link Menu}, the add methods, {@link Menu#clear}, {@link Menu#removeItem}, {@link
74  * Menu#findItem}, {@link Menu#size}, and {@link Menu#getItem} are implemented.
75  *
76  * <p>For {@link MenuItem}, setting and getting the title and icon, {@link MenuItem#getItemId}, and
77  * {@link MenuItem#setOnMenuItemClickListener} are implemented.
78  */
79 public class WearableActionDrawerView extends WearableDrawerView {
80 
81     private static final String TAG = "WearableActionDrawer";
82 
83     final RecyclerView mActionList;
84     final int mTopPadding;
85     final int mBottomPadding;
86     final int mLeftPadding;
87     final int mRightPadding;
88     final int mFirstItemTopPadding;
89     final int mLastItemBottomPadding;
90     final int mIconRightMargin;
91     private final boolean mShowOverflowInPeek;
92     private final @Nullable ImageView mPeekActionIcon;
93     private final @Nullable ImageView mPeekExpandIcon;
94     final RecyclerView.Adapter<RecyclerView.ViewHolder> mActionListAdapter;
95     private OnMenuItemClickListener mOnMenuItemClickListener;
96     private Menu mMenu;
97     @Nullable CharSequence mTitle;
98 
WearableActionDrawerView(Context context)99     public WearableActionDrawerView(Context context) {
100         this(context, null);
101     }
102 
WearableActionDrawerView(Context context, AttributeSet attrs)103     public WearableActionDrawerView(Context context, AttributeSet attrs) {
104         this(context, attrs, 0);
105     }
106 
WearableActionDrawerView(Context context, AttributeSet attrs, int defStyleAttr)107     public WearableActionDrawerView(Context context, AttributeSet attrs, int defStyleAttr) {
108         this(context, attrs, defStyleAttr, 0);
109     }
110 
WearableActionDrawerView( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)111     public WearableActionDrawerView(
112             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
113         super(context, attrs, defStyleAttr, defStyleRes);
114 
115         setLockedWhenClosed(true);
116 
117         boolean showOverflowInPeek = false;
118         int menuRes = 0;
119         if (attrs != null) {
120             TypedArray typedArray = context.obtainStyledAttributes(
121                     attrs, R.styleable.WearableActionDrawerView, defStyleAttr, 0 /* defStyleRes */);
122 
123             ViewCompat.saveAttributeDataForStyleable(
124                     this, context, R.styleable.WearableActionDrawerView, attrs, typedArray,
125                         defStyleAttr, 0);
126 
127             try {
128                 mTitle = typedArray.getString(R.styleable.WearableActionDrawerView_drawerTitle);
129                 showOverflowInPeek = typedArray.getBoolean(
130                         R.styleable.WearableActionDrawerView_showOverflowInPeek, false);
131                 menuRes = typedArray
132                         .getResourceId(R.styleable.WearableActionDrawerView_actionMenu, 0);
133             } finally {
134                 typedArray.recycle();
135             }
136         }
137 
138         AccessibilityManager accessibilityManager =
139                 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
140         mShowOverflowInPeek = showOverflowInPeek || accessibilityManager.isEnabled();
141 
142         if (!mShowOverflowInPeek) {
143             LayoutInflater layoutInflater = LayoutInflater.from(context);
144             View peekView = layoutInflater.inflate(R.layout.ws_action_drawer_peek_view,
145                     getPeekContainer(), false /* attachToRoot */);
146             setPeekContent(peekView);
147             mPeekActionIcon = peekView.findViewById(R.id.ws_action_drawer_peek_action_icon);
148             mPeekExpandIcon = peekView.findViewById(R.id.ws_action_drawer_expand_icon);
149         } else {
150             mPeekActionIcon = null;
151             mPeekExpandIcon = null;
152             getPeekContainer().setContentDescription(
153                     context.getString(R.string.ws_action_drawer_content_description));
154         }
155 
156         if (menuRes != 0) {
157             // This must occur after initializing mPeekActionIcon, otherwise updatePeekIcons will
158             // exit early.
159             MenuInflater inflater = new MenuInflater(context);
160             inflater.inflate(menuRes, getMenu());
161         }
162 
163         int screenWidthPx = ResourcesUtil.getScreenWidthPx(context);
164         int screenHeightPx = ResourcesUtil.getScreenHeightPx(context);
165 
166         Resources res = getResources();
167         mTopPadding = res.getDimensionPixelOffset(R.dimen.ws_action_drawer_item_top_padding);
168         mBottomPadding = res.getDimensionPixelOffset(R.dimen.ws_action_drawer_item_bottom_padding);
169         mLeftPadding =
170                 ResourcesUtil.getFractionOfScreenPx(
171                         context, screenWidthPx, R.fraction.ws_action_drawer_item_left_padding);
172         mRightPadding =
173                 ResourcesUtil.getFractionOfScreenPx(
174                         context, screenWidthPx, R.fraction.ws_action_drawer_item_right_padding);
175 
176         mFirstItemTopPadding =
177                 ResourcesUtil.getFractionOfScreenPx(
178                         context, screenHeightPx,
179                         R.fraction.ws_action_drawer_item_first_item_top_padding);
180         mLastItemBottomPadding =
181                 ResourcesUtil.getFractionOfScreenPx(
182                         context, screenHeightPx,
183                         R.fraction.ws_action_drawer_item_last_item_bottom_padding);
184 
185         mIconRightMargin = res
186                 .getDimensionPixelOffset(R.dimen.ws_action_drawer_item_icon_right_margin);
187 
188         mActionList = new RecyclerView(context);
189         mActionList.setId(R.id.action_list);
190         mActionList.setLayoutManager(new LinearLayoutManager(context));
191         mActionListAdapter = new ActionListAdapter(getMenu());
192         // Do not bind the mActionListAdapter to the action list here. We will bind it when/if the
193         // drawer is first opened to avoid the inflation cost in the case that the drawer is never
194         // used
195         setDrawerContent(mActionList);
196     }
197 
198     @Override
onDrawerOpened()199     public void onDrawerOpened() {
200         setContentIfFirstCall();
201         if (mActionListAdapter.getItemCount() > 0) {
202             RecyclerView.ViewHolder holder = mActionList.findViewHolderForAdapterPosition(0);
203             if (holder != null && holder.itemView != null) {
204                 holder.itemView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
205             }
206         }
207     }
208 
setContentIfFirstCall()209     private void setContentIfFirstCall() {
210         if (mActionList.getAdapter() == null) {
211             mActionList.setAdapter(mActionListAdapter);
212         }
213     }
214 
215     @Override
canScrollHorizontally(int direction)216     public boolean canScrollHorizontally(int direction) {
217         // Prevent the window from being swiped closed while it is open by saying that it can scroll
218         // horizontally.
219         return isOpened();
220     }
221 
222     @Override
onPeekContainerClicked(View v)223     public void onPeekContainerClicked(View v) {
224         if (mShowOverflowInPeek) {
225             super.onPeekContainerClicked(v);
226         } else {
227             onMenuItemClicked(0);
228         }
229     }
230 
231     @Override
preferGravity()232   /* package */ int preferGravity() {
233         return Gravity.BOTTOM;
234     }
235 
236     /**
237      * Set a {@link OnMenuItemClickListener} for this action drawer.
238      */
setOnMenuItemClickListener(OnMenuItemClickListener listener)239     public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
240         mOnMenuItemClickListener = listener;
241     }
242 
243     /**
244      * Sets the title for this action drawer. If {@code title} is {@code null}, then the title will
245      * be removed.
246      */
setTitle(@ullable CharSequence title)247     public void setTitle(@Nullable CharSequence title) {
248         if (TextUtils.equals(title, mTitle)) {
249             return;
250         }
251 
252         CharSequence oldTitle = mTitle;
253         mTitle = title;
254         if (oldTitle == null) {
255             mActionListAdapter.notifyItemInserted(0);
256         } else if (title == null) {
257             mActionListAdapter.notifyItemRemoved(0);
258         } else {
259             mActionListAdapter.notifyItemChanged(0);
260         }
261     }
262 
hasTitle()263     boolean hasTitle() {
264         return mTitle != null;
265     }
266 
onMenuItemClicked(int position)267     void onMenuItemClicked(int position) {
268         if (position >= 0 && position < getMenu().size()) { // Sanity check.
269             WearableActionDrawerMenuItem menuItem =
270                     (WearableActionDrawerMenuItem) getMenu().getItem(position);
271             if (menuItem.invoke()) {
272                 return;
273             }
274 
275             if (mOnMenuItemClickListener != null) {
276                 mOnMenuItemClickListener.onMenuItemClick(menuItem);
277             }
278         }
279     }
280 
updatePeekIcons()281     void updatePeekIcons() {
282         if (mPeekActionIcon == null || mPeekExpandIcon == null) {
283             return;
284         }
285 
286         Menu menu = getMenu();
287         int numberOfActions = menu.size();
288 
289         // Only show drawer content (and allow it to be opened) when there's more than one action.
290         if (numberOfActions > 1) {
291             setDrawerContent(mActionList);
292             mPeekExpandIcon.setVisibility(VISIBLE);
293         } else {
294             setDrawerContent(null);
295             mPeekExpandIcon.setVisibility(GONE);
296         }
297 
298         if (numberOfActions >= 1) {
299             Drawable firstActionDrawable = menu.getItem(0).getIcon();
300             // Because the ImageView will tint the Drawable white, attempt to get a mutable copy of
301             // it. If a copy isn't made, the icon will be white in the expanded state, rendering it
302             // invisible.
303             if (firstActionDrawable != null) {
304                 firstActionDrawable = firstActionDrawable.getConstantState().newDrawable().mutate();
305                 firstActionDrawable.clearColorFilter();
306             }
307 
308             mPeekActionIcon.setImageDrawable(firstActionDrawable);
309             mPeekActionIcon.setContentDescription(menu.getItem(0).getTitle());
310         }
311     }
312 
313     /**
314      * Returns the Menu object that this WearableActionDrawer represents.
315      *
316      * <p>Applications should use this method to obtain the WearableActionDrawers's Menu object and
317      * inflate or add content to it as necessary.
318      *
319      * @return the Menu presented by this view
320      */
getMenu()321     public Menu getMenu() {
322         if (mMenu == null) {
323             mMenu = new WearableActionDrawerMenu(
324                     getContext(),
325                     new WearableActionDrawerMenu.WearableActionDrawerMenuListener() {
326                         @Override
327                         public void menuItemChanged(int position) {
328                             if (mActionListAdapter != null) {
329                                 int listPosition = hasTitle() ? position + 1 : position;
330                                 mActionListAdapter.notifyItemChanged(listPosition);
331                             }
332                             if (position == 0) {
333                                 updatePeekIcons();
334                             }
335                         }
336 
337                         @Override
338                         public void menuItemAdded(int position) {
339                             if (mActionListAdapter != null) {
340                                 int listPosition = hasTitle() ? position + 1 : position;
341                                 mActionListAdapter.notifyItemInserted(listPosition);
342                             }
343                             // Handle transitioning from 0->1 items (set peek icon) and
344                             // 1->2 (switch to ellipsis.)
345                             if (position <= 1) {
346                                 updatePeekIcons();
347                             }
348                         }
349 
350                         @Override
351                         public void menuItemRemoved(int position) {
352                             if (mActionListAdapter != null) {
353                                 int listPosition = hasTitle() ? position + 1 : position;
354                                 mActionListAdapter.notifyItemRemoved(listPosition);
355                             }
356                             // Handle transitioning from 2->1 items (remove ellipsis), and
357                             // also the removal of item 1, which could cause the peek icon
358                             // to change.
359                             if (position <= 1) {
360                                 updatePeekIcons();
361                             }
362                         }
363 
364                         @Override
365                         public void menuChanged() {
366                             if (mActionListAdapter != null) {
367                                 mActionListAdapter.notifyDataSetChanged();
368                             }
369                             updatePeekIcons();
370                         }
371                     });
372         }
373 
374         return mMenu;
375     }
376 
377     private static final class TitleViewHolder extends RecyclerView.ViewHolder {
378         public final TextView textView;
379 
TitleViewHolder(View view)380         TitleViewHolder(View view) {
381             super(view);
382             textView = (TextView) view.findViewById(R.id.ws_action_drawer_title);
383         }
384     }
385 
386     private final class ActionListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
387 
388         public static final int TYPE_ACTION = 0;
389         public static final int TYPE_TITLE = 1;
390         private final Menu mActionMenu;
391         private final View.OnClickListener mItemClickListener =
392                 new View.OnClickListener() {
393                     @Override
394                     public void onClick(View v) {
395                         int childPos =
396                                 mActionList.getChildAdapterPosition(v) - (hasTitle() ? 1 : 0);
397                         if (childPos == RecyclerView.NO_POSITION) {
398                             Log.w(TAG, "invalid child position");
399                             return;
400                         }
401                         onMenuItemClicked(childPos);
402                     }
403                 };
404 
405         @SuppressWarnings("UnusedVariable")
ActionListAdapter(Menu menu)406         ActionListAdapter(Menu menu) {
407             mActionMenu = getMenu();
408         }
409 
410         @Override
getItemCount()411         public int getItemCount() {
412             return mActionMenu.size() + (hasTitle() ? 1 : 0);
413         }
414 
415         @Override
onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position)416         public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
417             int titleAwarePosition = hasTitle() ? position - 1 : position;
418             if (viewHolder instanceof ActionItemViewHolder) {
419                 ActionItemViewHolder holder = (ActionItemViewHolder) viewHolder;
420                 holder.view.setPadding(
421                         mLeftPadding,
422                         position == 0 ? mFirstItemTopPadding : mTopPadding,
423                         mRightPadding,
424                         position == getItemCount() - 1 ? mLastItemBottomPadding : mBottomPadding);
425 
426                 Drawable icon = mActionMenu.getItem(titleAwarePosition).getIcon();
427                 if (icon != null) {
428                     icon = icon.getConstantState().newDrawable().mutate();
429                 }
430                 CharSequence title = mActionMenu.getItem(titleAwarePosition).getTitle();
431                 holder.textView.setText(title);
432                 holder.textView.setContentDescription(title);
433                 holder.iconView.setImageDrawable(icon);
434             } else if (viewHolder instanceof TitleViewHolder) {
435                 TitleViewHolder holder = (TitleViewHolder) viewHolder;
436                 holder.textView.setPadding(0, mFirstItemTopPadding, 0, mBottomPadding);
437                 holder.textView.setText(mTitle);
438             }
439         }
440 
441         @Override
onCreateViewHolder(ViewGroup parent, int viewType)442         public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
443             switch (viewType) {
444                 case TYPE_TITLE:
445                     View titleView =
446                             LayoutInflater.from(parent.getContext())
447                                     .inflate(R.layout.ws_action_drawer_title_view, parent, false);
448                     return new TitleViewHolder(titleView);
449 
450                 case TYPE_ACTION:
451                 default:
452                     View actionView =
453                             LayoutInflater.from(parent.getContext())
454                                     .inflate(R.layout.ws_action_drawer_item_view, parent, false);
455                     actionView.setOnClickListener(mItemClickListener);
456                     return new ActionItemViewHolder(actionView);
457             }
458         }
459 
460         @Override
getItemViewType(int position)461         public int getItemViewType(int position) {
462             return hasTitle() && position == 0 ? TYPE_TITLE : TYPE_ACTION;
463         }
464     }
465 
466     private final class ActionItemViewHolder extends RecyclerView.ViewHolder {
467 
468         public final View view;
469         public final ImageView iconView;
470         public final TextView textView;
471 
ActionItemViewHolder(View view)472         ActionItemViewHolder(View view) {
473             super(view);
474             this.view = view;
475             iconView = (ImageView) view.findViewById(R.id.ws_action_drawer_item_icon);
476             ((LinearLayout.LayoutParams) iconView.getLayoutParams()).setMarginEnd(mIconRightMargin);
477             textView = (TextView) view.findViewById(R.id.ws_action_drawer_item_text);
478         }
479     }
480 }
481