1/*
2 * Copyright (C) 2013 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.v7.app;
18
19import android.support.v7.appcompat.R;
20import android.support.v7.internal.widget.NativeActionModeAwareLayout;
21import android.view.ActionMode;
22import android.view.Menu;
23import android.view.MenuItem;
24
25class ActionBarImplHC extends ActionBarImplBase
26        implements NativeActionModeAwareLayout.OnActionModeForChildListener {
27
28    final NativeActionModeAwareLayout mNativeActionModeAwareLayout;
29    private ActionMode mCurActionMode;
30
31    public ActionBarImplHC(ActionBarActivity activity, Callback callback) {
32        super(activity, callback);
33
34        // NativeActionModeAwareLayout is used to notify us whena native Action Mode is started
35        mNativeActionModeAwareLayout = (NativeActionModeAwareLayout) activity
36                .findViewById(R.id.action_bar_root);
37
38        // Can be null when using FEATURE_ACTION_BAR_OVERLAY
39        if (mNativeActionModeAwareLayout != null) {
40            mNativeActionModeAwareLayout.setActionModeForChildListener(this);
41        }
42    }
43
44    // From NativeActionModeAwareLayout.OnActionModeForChildListener
45    @Override
46    public ActionMode.Callback onActionModeForChild(ActionMode.Callback callback) {
47        return new CallbackWrapper(callback);
48    }
49
50    @Override
51    public void show() {
52        super.show();
53        if (mCurActionMode != null) {
54            mCurActionMode.finish();
55        }
56    }
57
58    @Override
59    public void hide() {
60        super.hide();
61        if (mCurActionMode != null) {
62            mCurActionMode.finish();
63        }
64    }
65
66    @Override
67    boolean isShowHideAnimationEnabled() {
68        // Only allow animation if we're not currently showing an action mode
69        return mCurActionMode == null && super.isShowHideAnimationEnabled();
70    }
71
72    private class CallbackWrapper implements ActionMode.Callback {
73        private final ActionMode.Callback mWrappedCallback;
74
75        CallbackWrapper(ActionMode.Callback callback) {
76            mWrappedCallback = callback;
77        }
78
79        @Override
80        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
81            final boolean wrappedResult = mWrappedCallback.onCreateActionMode(mode, menu);
82            if (wrappedResult) {
83                // Keep reference to action mode
84                mCurActionMode = mode;
85                // Make sure that the compat Action Bar is shown
86                showForActionMode();
87            }
88            return wrappedResult;
89        }
90
91        @Override
92        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
93            return mWrappedCallback.onPrepareActionMode(mode, menu);
94        }
95
96        @Override
97        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
98            return mWrappedCallback.onActionItemClicked(mode, item);
99        }
100
101        @Override
102        public void onDestroyActionMode(ActionMode mode) {
103            mWrappedCallback.onDestroyActionMode(mode);
104
105            // We previously shown the Action Bar for positioning purposes, now hide it again
106            hideForActionMode();
107            // Remove any reference to the mode
108            mCurActionMode = null;
109        }
110    }
111}