summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/ui/BugleActionBarActivity.java
blob: 80dabb8f01b8d875fb1a85e7e94a6d844cb81a32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * 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.messaging.ui;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.android.messaging.R;
import com.android.messaging.util.BugleActivityUtil;
import com.android.messaging.util.ImeUtil;
import com.android.messaging.util.LogUtil;
import com.android.messaging.util.UiUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Base class for app activities that use an action bar. Responsible for logging/telemetry/other
 * needs that will be common for all activities.  We can break out the common code if/when we need
 * a version that doesn't use an actionbar.
 */
public class BugleActionBarActivity extends ActionBarActivity implements ImeUtil.ImeStateHost {
    // Tracks the list of observers opting in for IME state change.
    private final Set<ImeUtil.ImeStateObserver> mImeStateObservers = new HashSet<>();

    // Tracks the soft keyboard display state
    private boolean mImeOpen;

    // The ActionMode that represents the modal contextual action bar, using our own implementation
    // rather than the built in contextual action bar to reduce jank
    private CustomActionMode mActionMode;

    // The menu for the action bar
    private Menu mActionBarMenu;

    // Used to determine if a onDisplayHeightChanged was due to the IME opening or rotation of the
    // device
    private int mLastScreenHeight;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (UiUtils.redirectToPermissionCheckIfNeeded(this)) {
            return;
        }

        mLastScreenHeight = getResources().getDisplayMetrics().heightPixels;
        if (LogUtil.isLoggable(LogUtil.BUGLE_TAG, LogUtil.VERBOSE)) {
            LogUtil.v(LogUtil.BUGLE_TAG, this.getLocalClassName() + ".onCreate");
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (LogUtil.isLoggable(LogUtil.BUGLE_TAG, LogUtil.VERBOSE)) {
            LogUtil.v(LogUtil.BUGLE_TAG, this.getLocalClassName() + ".onStart");
        }
    }

    @Override
    protected void onRestart() {
        super.onStop();
        if (LogUtil.isLoggable(LogUtil.BUGLE_TAG, LogUtil.VERBOSE)) {
            LogUtil.v(LogUtil.BUGLE_TAG, this.getLocalClassName() + ".onRestart");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (LogUtil.isLoggable(LogUtil.BUGLE_TAG, LogUtil.VERBOSE)) {
            LogUtil.v(LogUtil.BUGLE_TAG, this.getLocalClassName() + ".onResume");
        }
        BugleActivityUtil.onActivityResume(this, BugleActionBarActivity.this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (LogUtil.isLoggable(LogUtil.BUGLE_TAG, LogUtil.VERBOSE)) {
            LogUtil.v(LogUtil.BUGLE_TAG, this.getLocalClassName() + ".onPause");
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (LogUtil.isLoggable(LogUtil.BUGLE_TAG, LogUtil.VERBOSE)) {
            LogUtil.v(LogUtil.BUGLE_TAG, this.getLocalClassName() + ".onStop");
        }
    }

    private boolean mDestroyed;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDestroyed = true;
    }

    public boolean getIsDestroyed() {
        return mDestroyed;
    }

    @Override
    public void onDisplayHeightChanged(final int heightSpecification) {
        int screenHeight = getResources().getDisplayMetrics().heightPixels;

        if (screenHeight != mLastScreenHeight) {
            // Appears to be an orientation change, don't fire ime updates
            mLastScreenHeight = screenHeight;
            LogUtil.v(LogUtil.BUGLE_TAG, this.getLocalClassName() + ".onDisplayHeightChanged " +
                    " screenHeight: " + screenHeight + " lastScreenHeight: " + mLastScreenHeight +
                    " Skipped, appears to be orientation change.");
            return;
        }
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null && actionBar.isShowing()) {
            screenHeight -= actionBar.getHeight();
        }
        final int height = View.MeasureSpec.getSize(heightSpecification);

        final boolean imeWasOpen = mImeOpen;
        mImeOpen = screenHeight - height > 100;

        if (LogUtil.isLoggable(LogUtil.BUGLE_TAG, LogUtil.VERBOSE)) {
            LogUtil.v(LogUtil.BUGLE_TAG, this.getLocalClassName() + ".onDisplayHeightChanged " +
                    "imeWasOpen: " + imeWasOpen + " mImeOpen: " + mImeOpen + " screenHeight: " +
                    screenHeight + " height: " + height);
        }

        if (imeWasOpen != mImeOpen) {
            for (final ImeUtil.ImeStateObserver observer : mImeStateObservers) {
                observer.onImeStateChanged(mImeOpen);
            }
        }
    }

    @Override
    public void registerImeStateObserver(final ImeUtil.ImeStateObserver observer) {
        mImeStateObservers.add(observer);
    }

    @Override
    public void unregisterImeStateObserver(final ImeUtil.ImeStateObserver observer) {
        mImeStateObservers.remove(observer);
    }

    @Override
    public boolean isImeOpen() {
        return mImeOpen;
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        mActionBarMenu = menu;
        if (mActionMode != null &&
                mActionMode.getCallback().onCreateActionMode(mActionMode, menu)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean onPrepareOptionsMenu(final Menu menu) {
        mActionBarMenu = menu;
        if (mActionMode != null &&
                mActionMode.getCallback().onPrepareActionMode(mActionMode, menu)) {
            return true;
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem menuItem) {
        if (mActionMode != null &&
                mActionMode.getCallback().onActionItemClicked(mActionMode, menuItem)) {
            return true;
        }

        switch (menuItem.getItemId()) {
            case android.R.id.home:
                if (mActionMode != null) {
                    dismissActionMode();
                    return true;
                }
        }
        return super.onOptionsItemSelected(menuItem);
    }

    @Override
    public ActionMode startActionMode(final ActionMode.Callback callback) {
        mActionMode = new CustomActionMode(callback);
        supportInvalidateOptionsMenu();
        invalidateActionBar();
        return mActionMode;
    }

    public void dismissActionMode() {
        if (mActionMode != null) {
            mActionMode.finish();
            mActionMode = null;
            invalidateActionBar();
        }
    }

    public ActionMode getActionMode() {
        return mActionMode;
    }

    protected ActionMode.Callback getActionModeCallback() {
        if (mActionMode == null) {
            return null;
        }

        return mActionMode.getCallback();
    }

    /**
     * Receives and handles action bar invalidation request from sub-components of this activity.
     *
     * <p>Normally actions have sole control over the action bar, but in order to support seamless
     * transitions for components such as the full screen media picker, we have to let it take over
     * the action bar and then restore its state afterwards</p>
     *
     * <p>If a fragment does anything that may change the action bar, it should call this method
     * and then it is this method's responsibility to figure out which component "controls" the
     * action bar and delegate the updating of the action bar to that component</p>
     */
    public final void invalidateActionBar() {
        if (mActionMode != null) {
            mActionMode.updateActionBar(getSupportActionBar());
        } else {
            updateActionBar(getSupportActionBar());
        }
    }

    protected void updateActionBar(final ActionBar actionBar) {
        actionBar.setHomeAsUpIndicator(null);
    }

    /**
     * Custom ActionMode implementation which allows us to just replace the contents of the main
     * action bar rather than overlay over it
     */
    private class CustomActionMode extends ActionMode {
        private CharSequence mTitle;
        private CharSequence mSubtitle;
        private View mCustomView;
        private final Callback mCallback;

        public CustomActionMode(final Callback callback) {
            mCallback = callback;
        }

        @Override
        public void setTitle(final CharSequence title) {
            mTitle = title;
        }

        @Override
        public void setTitle(final int resId) {
            mTitle = getResources().getString(resId);
        }

        @Override
        public void setSubtitle(final CharSequence subtitle) {
            mSubtitle = subtitle;
        }

        @Override
        public void setSubtitle(final int resId) {
            mSubtitle = getResources().getString(resId);
        }

        @Override
        public void setCustomView(final View view) {
            mCustomView = view;
        }

        @Override
        public void invalidate() {
            invalidateActionBar();
        }

        @Override
        public void finish() {
            mActionMode = null;
            mCallback.onDestroyActionMode(this);
            supportInvalidateOptionsMenu();
            invalidateActionBar();
        }

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

        @Override
        public CharSequence getTitle() {
            return mTitle;
        }

        @Override
        public CharSequence getSubtitle() {
            return mSubtitle;
        }

        @Override
        public View getCustomView() {
            return mCustomView;
        }

        @Override
        public MenuInflater getMenuInflater() {
            return BugleActionBarActivity.this.getMenuInflater();
        }

        public Callback getCallback() {
            return mCallback;
        }

        public void updateActionBar(final ActionBar actionBar) {
            actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayShowCustomEnabled(false);
            mActionMode.getCallback().onPrepareActionMode(mActionMode, mActionBarMenu);
            actionBar.setBackgroundDrawable(new ColorDrawable(
                    getResources().getColor(R.color.contextual_action_bar_background_color)));
            actionBar.setHomeAsUpIndicator(R.drawable.ic_cancel_small_dark);
            actionBar.show();
        }
    }
}