Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ed2a54cf authored by Clara Bayarri's avatar Clara Bayarri
Browse files

Floating Toolbars: Wrap the ActionMode creation in DecorView

This change will allow us to create ActionMode representations on the
fly after onCreateActionMode by using the Decorator pattern. The new
ActionModeWrapper will be responsible for the creating the
appropriate ActionMode depending on the type chosen by the client,
and setting it up.

Things pending that are NOT addressed by this CL:
- ActionModes created by callback.onWindowStartingActionMode(). This
includes all current usages in an existing ActionBar, as it is
handled by Activity. This requires some additional refactoring.
- Representing the floating type
- Moving the view creation code specific to StandaloneActionMode
from DecorView to ActionModeWrapper, decoupling DecorView from
StandaloneActionMode completely
- Supporting two ActionModes in parallel in DecorView, one of each type

Change-Id: I1a8db711f53b771eac74f0e6496106acf1ca2727
parent 421d6ad1
Loading
Loading
Loading
Loading
+69 −61
Original line number Diff line number Diff line
@@ -25,7 +25,9 @@ import static android.view.WindowManager.LayoutParams.*;
import android.app.ActivityManagerNative;
import android.app.SearchManager;
import android.os.UserHandle;

import com.android.internal.R;
import com.android.internal.view.ActionModeWrapper;
import com.android.internal.view.RootViewSurfaceTaker;
import com.android.internal.view.StandaloneActionMode;
import com.android.internal.view.menu.ContextMenuBuilder;
@@ -2689,6 +2691,13 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
            if (mode != null) {
                mActionMode = mode;
            } else {
                if (mActionModeView != null) {
                    mActionModeView.killMode();
                }
                ActionModeWrapper wrapperMode =
                        new ActionModeWrapper(mContext, wrappedCallback);
                if (callback.onCreateActionMode(wrapperMode, wrapperMode.getMenu())) {
                    if (wrapperMode.getType() == ActionMode.TYPE_PRIMARY) {
                        if (mActionModeView == null) {
                            if (isFloating()) {
                                // Use the action bar theme.
@@ -2737,26 +2746,25 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
                                }
                            }
                        }

                        if (mActionModeView != null) {
                    mActionModeView.killMode();
                    mode = new StandaloneActionMode(mActionModeView.getContext(), mActionModeView,
                            wrappedCallback, mActionModePopup == null);
                    if (callback.onCreateActionMode(mode, mode.getMenu())) {
                        mode.invalidate();
                        mActionModeView.initForMode(mode);
                            wrapperMode.setActionModeView(mActionModeView);
                            wrapperMode.setFocusable(mActionModePopup == null);
                            wrapperMode.lockType();
                            wrapperMode.invalidate();
                            mActionModeView.initForMode(wrapperMode);
                            mActionModeView.setVisibility(View.VISIBLE);
                        mActionMode = mode;
                            mActionMode = wrapperMode;
                            if (mActionModePopup != null) {
                                post(mShowActionModePopup);
                            }
                            mActionModeView.sendAccessibilityEvent(
                                    AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
                        }
                    }
                } else {
                    mActionMode = null;
                }
            }
            }
            if (mActionMode != null && getCallback() != null && !isDestroyed()) {
                try {
                    getCallback().onActionModeStarted(mActionMode);
+200 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.view;

import android.content.Context;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.android.internal.view.menu.MenuBuilder;
import com.android.internal.widget.ActionBarContextView;

/**
 * ActionMode implementation that wraps several actions modes and creates them on the fly depending
 * on the ActionMode type chosen by the client.
 */
public class ActionModeWrapper extends ActionMode {

    private ActionMode mActionMode;
    private final Context mContext;
    private MenuBuilder mMenu;
    private final ActionMode.Callback mCallback;
    private boolean mTypeLocked = false;

    private CharSequence mTitle;
    private CharSequence mSubtitle;
    private View mCustomView;

    // Fields for StandaloneActionMode
    private ActionBarContextView mActionModeView;
    private boolean mIsFocusable;

    public ActionModeWrapper(Context context, ActionMode.Callback callback) {
        mContext = context;
        mMenu = new MenuBuilder(context).setDefaultShowAsAction(
                MenuItem.SHOW_AS_ACTION_IF_ROOM);
        mCallback = callback;
    }

    @Override
    public void setTitle(CharSequence title) {
        if (mActionMode != null) {
            mActionMode.setTitle(title);
        } else {
            mTitle = title;
        }
    }

    @Override
    public void setTitle(int resId) {
        if (mActionMode != null) {
            mActionMode.setTitle(resId);
        } else {
            mTitle = resId != 0 ? mContext.getString(resId) : null;
        }
    }

    @Override
    public void setSubtitle(CharSequence subtitle) {
        if (mActionMode != null) {
            mActionMode.setSubtitle(subtitle);
        } else {
            mSubtitle = subtitle;
        }
    }

    @Override
    public void setSubtitle(int resId) {
        if (mActionMode != null) {
            mActionMode.setSubtitle(resId);
        } else {
            mSubtitle = resId != 0 ? mContext.getString(resId) : null;
        }
    }

    @Override
    public void setCustomView(View view) {
        if (mActionMode != null) {
            mActionMode.setCustomView(view);
        } else {
            mCustomView = view;
        }
    }

    /**
     * Set the current type as final and create the necessary ActionMode. After this call, any
     * changes to the ActionMode type will be ignored.
     */
    public void lockType() {
        mTypeLocked = true;
        switch (getType()) {
            case ActionMode.TYPE_PRIMARY:
            default:
                mActionMode = new StandaloneActionMode(
                        mActionModeView.getContext(),
                        mActionModeView, mCallback, mIsFocusable, mMenu);
                break;
            case ActionMode.TYPE_FLOATING:
                // Not implemented yet.
                break;
        }

        if (mActionMode == null) {
            return;
        }

        mActionMode.setTitle(mTitle);
        mActionMode.setSubtitle(mSubtitle);
        if (mCustomView != null) {
            mActionMode.setCustomView(mCustomView);
        }

        mTitle = null;
        mSubtitle = null;
        mCustomView = null;
    }

    @Override
    public void setType(int type) {
        if (!mTypeLocked) {
            super.setType(type);
        } else {
            throw new IllegalStateException(
                    "You can't change the ActionMode's type after onCreateActionMode.");
        }
    }

    @Override
    public void invalidate() {
        if (mActionMode != null) {
            mActionMode.invalidate();
        }
    }

    @Override
    public void finish() {
        if (mActionMode != null) {
            mActionMode.finish();
        }
    }

    @Override
    public Menu getMenu() {
        return mMenu;
    }

    @Override
    public CharSequence getTitle() {
        if (mActionMode != null) {
            return mActionMode.getTitle();
        }
        return mTitle;
    }

    @Override
    public CharSequence getSubtitle() {
        if (mActionMode != null) {
            return mActionMode.getSubtitle();
        }
        return mSubtitle;
    }

    @Override
    public View getCustomView() {
        if (mActionMode != null) {
            return mActionMode.getCustomView();
        }
        return mCustomView;
    }

    @Override
    public MenuInflater getMenuInflater() {
        return new MenuInflater(mContext);
    }

    public void setActionModeView(ActionBarContextView actionModeView) {
        mActionModeView = actionModeView;
    }

    public void setFocusable(boolean focusable) {
        mIsFocusable = focusable;
    }

}
+8 −5
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import com.android.internal.view.menu.MenuPopupHelper;
import com.android.internal.view.menu.SubMenuBuilder;
import com.android.internal.widget.ActionBarContextView;

import android.annotation.Nullable;
import android.content.Context;
import android.view.ActionMode;
import android.view.Menu;
@@ -41,12 +42,14 @@ public class StandaloneActionMode extends ActionMode implements MenuBuilder.Call
    private MenuBuilder mMenu;

    public StandaloneActionMode(Context context, ActionBarContextView view,
            ActionMode.Callback callback, boolean isFocusable) {
            ActionMode.Callback callback, boolean isFocusable, @Nullable MenuBuilder menuBuilder) {
        mContext = context;
        mContextView = view;
        mCallback = callback;

        mMenu = new MenuBuilder(view.getContext()).setDefaultShowAsAction(
        mMenu = (menuBuilder != null)
                ? menuBuilder
                : new MenuBuilder(view.getContext()).setDefaultShowAsAction(
                        MenuItem.SHOW_AS_ACTION_IF_ROOM);
        mMenu.setCallback(this);
        mFocusable = isFocusable;
@@ -64,12 +67,12 @@ public class StandaloneActionMode extends ActionMode implements MenuBuilder.Call

    @Override
    public void setTitle(int resId) {
        setTitle(mContext.getString(resId));
        setTitle(resId != 0 ? mContext.getString(resId) : null);
    }

    @Override
    public void setSubtitle(int resId) {
        setSubtitle(mContext.getString(resId));
        setSubtitle(resId != 0 ? mContext.getString(resId) : null);
    }

    @Override
+1 −1
Original line number Diff line number Diff line
@@ -158,7 +158,7 @@ public class ActionBarContextView extends AbsActionBarView implements AnimatorLi
            removeView(mCustomView);
        }
        mCustomView = view;
        if (mTitleLayout != null) {
        if (view != null && mTitleLayout != null) {
            removeView(mTitleLayout);
            mTitleLayout = null;
        }