15d27977f9da482627ceb19317a2cd70467aff046Adam Powell/*
25d27977f9da482627ceb19317a2cd70467aff046Adam Powell * Copyright (C) 2010 The Android Open Source Project
35d27977f9da482627ceb19317a2cd70467aff046Adam Powell *
45d27977f9da482627ceb19317a2cd70467aff046Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
55d27977f9da482627ceb19317a2cd70467aff046Adam Powell * you may not use this file except in compliance with the License.
65d27977f9da482627ceb19317a2cd70467aff046Adam Powell * You may obtain a copy of the License at
75d27977f9da482627ceb19317a2cd70467aff046Adam Powell *
85d27977f9da482627ceb19317a2cd70467aff046Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
95d27977f9da482627ceb19317a2cd70467aff046Adam Powell *
105d27977f9da482627ceb19317a2cd70467aff046Adam Powell * Unless required by applicable law or agreed to in writing, software
115d27977f9da482627ceb19317a2cd70467aff046Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
125d27977f9da482627ceb19317a2cd70467aff046Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135d27977f9da482627ceb19317a2cd70467aff046Adam Powell * See the License for the specific language governing permissions and
145d27977f9da482627ceb19317a2cd70467aff046Adam Powell * limitations under the License.
155d27977f9da482627ceb19317a2cd70467aff046Adam Powell */
165d27977f9da482627ceb19317a2cd70467aff046Adam Powellpackage com.android.internal.view;
175d27977f9da482627ceb19317a2cd70467aff046Adam Powell
185d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport com.android.internal.view.menu.MenuBuilder;
195d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport com.android.internal.view.menu.MenuPopupHelper;
205d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport com.android.internal.view.menu.SubMenuBuilder;
215d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport com.android.internal.widget.ActionBarContextView;
225d27977f9da482627ceb19317a2cd70467aff046Adam Powell
235d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport android.content.Context;
245d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport android.view.ActionMode;
255d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport android.view.Menu;
269168f0b170c6a99371ae46e7d3f5d66c8c4c930dAdam Powellimport android.view.MenuInflater;
275d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport android.view.MenuItem;
285d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport android.view.View;
2986ed436b83d6b71ff00d1c3db910f2952018489eAdam Powellimport android.view.accessibility.AccessibilityEvent;
305d27977f9da482627ceb19317a2cd70467aff046Adam Powell
315d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport java.lang.ref.WeakReference;
325d27977f9da482627ceb19317a2cd70467aff046Adam Powell
335d27977f9da482627ceb19317a2cd70467aff046Adam Powellpublic class StandaloneActionMode extends ActionMode implements MenuBuilder.Callback {
345d27977f9da482627ceb19317a2cd70467aff046Adam Powell    private Context mContext;
355d27977f9da482627ceb19317a2cd70467aff046Adam Powell    private ActionBarContextView mContextView;
365d27977f9da482627ceb19317a2cd70467aff046Adam Powell    private ActionMode.Callback mCallback;
375d27977f9da482627ceb19317a2cd70467aff046Adam Powell    private WeakReference<View> mCustomView;
385d27977f9da482627ceb19317a2cd70467aff046Adam Powell    private boolean mFinished;
39f8419a0299680ed580975b0fcb758990b4367db8Adam Powell    private boolean mFocusable;
405d27977f9da482627ceb19317a2cd70467aff046Adam Powell
415d27977f9da482627ceb19317a2cd70467aff046Adam Powell    private MenuBuilder mMenu;
425d27977f9da482627ceb19317a2cd70467aff046Adam Powell
435d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public StandaloneActionMode(Context context, ActionBarContextView view,
444423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri            ActionMode.Callback callback, boolean isFocusable) {
455d27977f9da482627ceb19317a2cd70467aff046Adam Powell        mContext = context;
465d27977f9da482627ceb19317a2cd70467aff046Adam Powell        mContextView = view;
475d27977f9da482627ceb19317a2cd70467aff046Adam Powell        mCallback = callback;
485d27977f9da482627ceb19317a2cd70467aff046Adam Powell
494423d91de5300d3fd318bf5bc2d4d7e5bb856abfClara Bayarri        mMenu = new MenuBuilder(view.getContext()).setDefaultShowAsAction(
50ed2a54cfd336bb935f281c04509ecd48c8cf116dClara Bayarri                        MenuItem.SHOW_AS_ACTION_IF_ROOM);
515d27977f9da482627ceb19317a2cd70467aff046Adam Powell        mMenu.setCallback(this);
52f8419a0299680ed580975b0fcb758990b4367db8Adam Powell        mFocusable = isFocusable;
535d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
545d27977f9da482627ceb19317a2cd70467aff046Adam Powell
555d27977f9da482627ceb19317a2cd70467aff046Adam Powell    @Override
565d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public void setTitle(CharSequence title) {
575d27977f9da482627ceb19317a2cd70467aff046Adam Powell        mContextView.setTitle(title);
585d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
595d27977f9da482627ceb19317a2cd70467aff046Adam Powell
605d27977f9da482627ceb19317a2cd70467aff046Adam Powell    @Override
615d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public void setSubtitle(CharSequence subtitle) {
625d27977f9da482627ceb19317a2cd70467aff046Adam Powell        mContextView.setSubtitle(subtitle);
635d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
645d27977f9da482627ceb19317a2cd70467aff046Adam Powell
655d27977f9da482627ceb19317a2cd70467aff046Adam Powell    @Override
66c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell    public void setTitle(int resId) {
67ed2a54cfd336bb935f281c04509ecd48c8cf116dClara Bayarri        setTitle(resId != 0 ? mContext.getString(resId) : null);
68c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell    }
69c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell
70c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell    @Override
71c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell    public void setSubtitle(int resId) {
72ed2a54cfd336bb935f281c04509ecd48c8cf116dClara Bayarri        setSubtitle(resId != 0 ? mContext.getString(resId) : null);
73c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell    }
74c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell
75c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell    @Override
76b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell    public void setTitleOptionalHint(boolean titleOptional) {
77785c447b2bc625209706fd128ce61781c3a4183bAdam Powell        super.setTitleOptionalHint(titleOptional);
78b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell        mContextView.setTitleOptional(titleOptional);
79b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell    }
80b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell
81b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell    @Override
82b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell    public boolean isTitleOptional() {
83b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell        return mContextView.isTitleOptional();
84b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell    }
85b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell
86b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell    @Override
875d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public void setCustomView(View view) {
885d27977f9da482627ceb19317a2cd70467aff046Adam Powell        mContextView.setCustomView(view);
895d27977f9da482627ceb19317a2cd70467aff046Adam Powell        mCustomView = view != null ? new WeakReference<View>(view) : null;
905d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
915d27977f9da482627ceb19317a2cd70467aff046Adam Powell
925d27977f9da482627ceb19317a2cd70467aff046Adam Powell    @Override
935d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public void invalidate() {
945d27977f9da482627ceb19317a2cd70467aff046Adam Powell        mCallback.onPrepareActionMode(this, mMenu);
955d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
965d27977f9da482627ceb19317a2cd70467aff046Adam Powell
975d27977f9da482627ceb19317a2cd70467aff046Adam Powell    @Override
985d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public void finish() {
995d27977f9da482627ceb19317a2cd70467aff046Adam Powell        if (mFinished) {
1005d27977f9da482627ceb19317a2cd70467aff046Adam Powell            return;
1015d27977f9da482627ceb19317a2cd70467aff046Adam Powell        }
1025d27977f9da482627ceb19317a2cd70467aff046Adam Powell        mFinished = true;
1035d27977f9da482627ceb19317a2cd70467aff046Adam Powell
10486ed436b83d6b71ff00d1c3db910f2952018489eAdam Powell        mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
1055d27977f9da482627ceb19317a2cd70467aff046Adam Powell        mCallback.onDestroyActionMode(this);
1065d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
1075d27977f9da482627ceb19317a2cd70467aff046Adam Powell
1085d27977f9da482627ceb19317a2cd70467aff046Adam Powell    @Override
1095d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public Menu getMenu() {
1105d27977f9da482627ceb19317a2cd70467aff046Adam Powell        return mMenu;
1115d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
1125d27977f9da482627ceb19317a2cd70467aff046Adam Powell
1135d27977f9da482627ceb19317a2cd70467aff046Adam Powell    @Override
1145d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public CharSequence getTitle() {
1155d27977f9da482627ceb19317a2cd70467aff046Adam Powell        return mContextView.getTitle();
1165d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
1175d27977f9da482627ceb19317a2cd70467aff046Adam Powell
1185d27977f9da482627ceb19317a2cd70467aff046Adam Powell    @Override
1195d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public CharSequence getSubtitle() {
1205d27977f9da482627ceb19317a2cd70467aff046Adam Powell        return mContextView.getSubtitle();
1215d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
1225d27977f9da482627ceb19317a2cd70467aff046Adam Powell
1235d27977f9da482627ceb19317a2cd70467aff046Adam Powell    @Override
1245d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public View getCustomView() {
1255d27977f9da482627ceb19317a2cd70467aff046Adam Powell        return mCustomView != null ? mCustomView.get() : null;
1265d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
1275d27977f9da482627ceb19317a2cd70467aff046Adam Powell
1289168f0b170c6a99371ae46e7d3f5d66c8c4c930dAdam Powell    @Override
1299168f0b170c6a99371ae46e7d3f5d66c8c4c930dAdam Powell    public MenuInflater getMenuInflater() {
130a9ddb8dc23b253c53f24ceb81e9d596c072d834eAlan Viverette        return new MenuInflater(mContextView.getContext());
1319168f0b170c6a99371ae46e7d3f5d66c8c4c930dAdam Powell    }
1329168f0b170c6a99371ae46e7d3f5d66c8c4c930dAdam Powell
1335d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
1345d27977f9da482627ceb19317a2cd70467aff046Adam Powell        return mCallback.onActionItemClicked(this, item);
1355d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
1365d27977f9da482627ceb19317a2cd70467aff046Adam Powell
1375d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
1385d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
1395d27977f9da482627ceb19317a2cd70467aff046Adam Powell
1405d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
1415d27977f9da482627ceb19317a2cd70467aff046Adam Powell        if (!subMenu.hasVisibleItems()) {
1425d27977f9da482627ceb19317a2cd70467aff046Adam Powell            return true;
1435d27977f9da482627ceb19317a2cd70467aff046Adam Powell        }
1445d27977f9da482627ceb19317a2cd70467aff046Adam Powell
145a9ddb8dc23b253c53f24ceb81e9d596c072d834eAlan Viverette        new MenuPopupHelper(mContextView.getContext(), subMenu).show();
1465d27977f9da482627ceb19317a2cd70467aff046Adam Powell        return true;
1475d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
1485d27977f9da482627ceb19317a2cd70467aff046Adam Powell
1495d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public void onCloseSubMenu(SubMenuBuilder menu) {
1505d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
1515d27977f9da482627ceb19317a2cd70467aff046Adam Powell
1525d27977f9da482627ceb19317a2cd70467aff046Adam Powell    public void onMenuModeChange(MenuBuilder menu) {
153f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell        invalidate();
154696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell        mContextView.showOverflowMenu();
1555d27977f9da482627ceb19317a2cd70467aff046Adam Powell    }
156f8419a0299680ed580975b0fcb758990b4367db8Adam Powell
157f8419a0299680ed580975b0fcb758990b4367db8Adam Powell    public boolean isUiFocusable() {
158f8419a0299680ed580975b0fcb758990b4367db8Adam Powell        return mFocusable;
159f8419a0299680ed580975b0fcb758990b4367db8Adam Powell    }
1605d27977f9da482627ceb19317a2cd70467aff046Adam Powell}
161