Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2014 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 package androidx.appcompat.view;
     17 
     18 import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
     19 
     20 import android.content.Context;
     21 import android.view.Menu;
     22 import android.view.MenuInflater;
     23 import android.view.MenuItem;
     24 import android.view.View;
     25 import android.view.accessibility.AccessibilityEvent;
     26 
     27 import androidx.annotation.RestrictTo;
     28 import androidx.appcompat.view.menu.MenuBuilder;
     29 import androidx.appcompat.view.menu.MenuPopupHelper;
     30 import androidx.appcompat.view.menu.SubMenuBuilder;
     31 import androidx.appcompat.widget.ActionBarContextView;
     32 
     33 import java.lang.ref.WeakReference;
     34 
     35 /**
     36  * @hide
     37  */
     38 @RestrictTo(LIBRARY_GROUP)
     39 public class StandaloneActionMode extends ActionMode implements MenuBuilder.Callback {
     40     private Context mContext;
     41     private ActionBarContextView mContextView;
     42     private ActionMode.Callback mCallback;
     43     private WeakReference<View> mCustomView;
     44     private boolean mFinished;
     45     private boolean mFocusable;
     46 
     47     private MenuBuilder mMenu;
     48 
     49     public StandaloneActionMode(Context context, ActionBarContextView view,
     50             ActionMode.Callback callback, boolean isFocusable) {
     51         mContext = context;
     52         mContextView = view;
     53         mCallback = callback;
     54 
     55         mMenu = new MenuBuilder(view.getContext()).setDefaultShowAsAction(
     56                 MenuItem.SHOW_AS_ACTION_IF_ROOM);
     57         mMenu.setCallback(this);
     58         mFocusable = isFocusable;
     59     }
     60 
     61     @Override
     62     public void setTitle(CharSequence title) {
     63         mContextView.setTitle(title);
     64     }
     65 
     66     @Override
     67     public void setSubtitle(CharSequence subtitle) {
     68         mContextView.setSubtitle(subtitle);
     69     }
     70 
     71     @Override
     72     public void setTitle(int resId) {
     73         setTitle(mContext.getString(resId));
     74     }
     75 
     76     @Override
     77     public void setSubtitle(int resId) {
     78         setSubtitle(mContext.getString(resId));
     79     }
     80 
     81     @Override
     82     public void setTitleOptionalHint(boolean titleOptional) {
     83         super.setTitleOptionalHint(titleOptional);
     84         mContextView.setTitleOptional(titleOptional);
     85     }
     86 
     87     @Override
     88     public boolean isTitleOptional() {
     89         return mContextView.isTitleOptional();
     90     }
     91 
     92     @Override
     93     public void setCustomView(View view) {
     94         mContextView.setCustomView(view);
     95         mCustomView = view != null ? new WeakReference<View>(view) : null;
     96     }
     97 
     98     @Override
     99     public void invalidate() {
    100         mCallback.onPrepareActionMode(this, mMenu);
    101     }
    102 
    103     @Override
    104     public void finish() {
    105         if (mFinished) {
    106             return;
    107         }
    108         mFinished = true;
    109 
    110         mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
    111         mCallback.onDestroyActionMode(this);
    112     }
    113 
    114     @Override
    115     public Menu getMenu() {
    116         return mMenu;
    117     }
    118 
    119     @Override
    120     public CharSequence getTitle() {
    121         return mContextView.getTitle();
    122     }
    123 
    124     @Override
    125     public CharSequence getSubtitle() {
    126         return mContextView.getSubtitle();
    127     }
    128 
    129     @Override
    130     public View getCustomView() {
    131         return mCustomView != null ? mCustomView.get() : null;
    132     }
    133 
    134     @Override
    135     public MenuInflater getMenuInflater() {
    136         return new SupportMenuInflater(mContextView.getContext());
    137     }
    138 
    139     @Override
    140     public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
    141         return mCallback.onActionItemClicked(this, item);
    142     }
    143 
    144     public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
    145     }
    146 
    147     public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
    148         if (!subMenu.hasVisibleItems()) {
    149             return true;
    150         }
    151 
    152         new MenuPopupHelper(mContextView.getContext(), subMenu).show();
    153         return true;
    154     }
    155 
    156     public void onCloseSubMenu(SubMenuBuilder menu) {
    157     }
    158 
    159     @Override
    160     public void onMenuModeChange(MenuBuilder menu) {
    161         invalidate();
    162         mContextView.showOverflowMenu();
    163     }
    164 
    165     @Override
    166     public boolean isUiFocusable() {
    167         return mFocusable;
    168     }
    169 }
    170