NavigationBarView.java revision c3fc32228679e6d32f3194e676508c67e6332d92
1/*
2 * Copyright (C) 2008 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 com.android.systemui.statusbar.phone;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.LayoutTransition;
22import android.app.StatusBarManager;
23import android.content.Context;
24import android.content.res.Resources;
25import android.graphics.Point;
26import android.graphics.Rect;
27import android.graphics.drawable.Drawable;
28import android.os.Handler;
29import android.os.Message;
30import android.os.ServiceManager;
31import android.util.AttributeSet;
32import android.util.Slog;
33import android.view.animation.AccelerateInterpolator;
34import android.view.Display;
35import android.view.MotionEvent;
36import android.view.View;
37import android.view.Surface;
38import android.view.ViewGroup;
39import android.view.WindowManager;
40import android.widget.ImageView;
41import android.widget.LinearLayout;
42
43import java.io.FileDescriptor;
44import java.io.PrintWriter;
45
46import com.android.internal.statusbar.IStatusBarService;
47import com.android.systemui.R;
48import com.android.systemui.statusbar.BaseStatusBar;
49import com.android.systemui.statusbar.DelegateViewHelper;
50import com.android.systemui.statusbar.policy.DeadZone;
51
52public class NavigationBarView extends LinearLayout {
53    final static boolean DEBUG = false;
54    final static String TAG = "PhoneStatusBar/NavigationBarView";
55
56    final static boolean NAVBAR_ALWAYS_AT_RIGHT = true;
57
58    // slippery nav bar when everything is disabled, e.g. during setup
59    final static boolean SLIPPERY_WHEN_DISABLED= true;
60
61    final static boolean ANIMATE_HIDE_TRANSITION = false; // turned off because it introduces unsightly delay when videos goes to full screen
62
63    protected IStatusBarService mBarService;
64    final Display mDisplay;
65    View mCurrentView = null;
66    View[] mRotatedViews = new View[4];
67
68    int mBarSize;
69    boolean mVertical;
70    boolean mScreenOn;
71
72    boolean mHidden, mLowProfile, mShowMenu;
73    int mDisabledFlags = 0;
74    int mNavigationIconHints = 0;
75
76    private Drawable mBackIcon, mBackLandIcon, mBackAltIcon, mBackAltLandIcon;
77
78    private DelegateViewHelper mDelegateHelper;
79    private DeadZone mDeadZone;
80
81    // workaround for LayoutTransitions leaving the nav buttons in a weird state (bug 5549288)
82    final static boolean WORKAROUND_INVALID_LAYOUT = true;
83    final static int MSG_CHECK_INVALID_LAYOUT = 8686;
84
85    private class H extends Handler {
86        public void handleMessage(Message m) {
87            switch (m.what) {
88                case MSG_CHECK_INVALID_LAYOUT:
89                    final String how = "" + m.obj;
90                    final int w = getWidth();
91                    final int h = getHeight();
92                    final int vw = mCurrentView.getWidth();
93                    final int vh = mCurrentView.getHeight();
94
95                    if (h != vh || w != vw) {
96                        Slog.w(TAG, String.format(
97                            "*** Invalid layout in navigation bar (%s this=%dx%d cur=%dx%d)",
98                            how, w, h, vw, vh));
99                        if (WORKAROUND_INVALID_LAYOUT) {
100                            requestLayout();
101                        }
102                    }
103                    break;
104            }
105        }
106    }
107
108    public void setDelegateView(View view) {
109        mDelegateHelper.setDelegateView(view);
110    }
111
112    public void setBar(BaseStatusBar phoneStatusBar) {
113        mDelegateHelper.setBar(phoneStatusBar);
114    }
115
116    @Override
117    public boolean onTouchEvent(MotionEvent event) {
118        if (mDeadZone != null && event.getAction() == MotionEvent.ACTION_OUTSIDE) {
119            mDeadZone.poke(event);
120        }
121        if (mDelegateHelper != null) {
122            boolean ret = mDelegateHelper.onInterceptTouchEvent(event);
123            if (ret) return true;
124        }
125        return super.onTouchEvent(event);
126    }
127
128    @Override
129    public boolean onInterceptTouchEvent(MotionEvent event) {
130        return mDelegateHelper.onInterceptTouchEvent(event);
131    }
132
133    private H mHandler = new H();
134
135    public View getRecentsButton() {
136        return mCurrentView.findViewById(R.id.recent_apps);
137    }
138
139    public View getMenuButton() {
140        return mCurrentView.findViewById(R.id.menu);
141    }
142
143    public View getBackButton() {
144        return mCurrentView.findViewById(R.id.back);
145    }
146
147    public View getHomeButton() {
148        return mCurrentView.findViewById(R.id.home);
149    }
150
151    // for when home is disabled, but search isn't
152    public View getSearchLight() {
153        return mCurrentView.findViewById(R.id.search_light);
154    }
155
156    public NavigationBarView(Context context, AttributeSet attrs) {
157        super(context, attrs);
158
159        mHidden = false;
160
161        mDisplay = ((WindowManager)context.getSystemService(
162                Context.WINDOW_SERVICE)).getDefaultDisplay();
163        mBarService = IStatusBarService.Stub.asInterface(
164                ServiceManager.getService(Context.STATUS_BAR_SERVICE));
165
166        final Resources res = mContext.getResources();
167        mBarSize = res.getDimensionPixelSize(R.dimen.navigation_bar_size);
168        mVertical = false;
169        mShowMenu = false;
170        mDelegateHelper = new DelegateViewHelper(this);
171
172        mBackIcon = res.getDrawable(R.drawable.ic_sysbar_back);
173        mBackLandIcon = res.getDrawable(R.drawable.ic_sysbar_back_land);
174        mBackAltIcon = res.getDrawable(R.drawable.ic_sysbar_back_ime);
175        mBackAltLandIcon = res.getDrawable(R.drawable.ic_sysbar_back_ime);
176    }
177
178    public void notifyScreenOn(boolean screenOn) {
179        mScreenOn = screenOn;
180        setDisabledFlags(mDisabledFlags, true);
181    }
182
183    View.OnTouchListener mLightsOutListener = new View.OnTouchListener() {
184        @Override
185        public boolean onTouch(View v, MotionEvent ev) {
186            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
187                // even though setting the systemUI visibility below will turn these views
188                // on, we need them to come up faster so that they can catch this motion
189                // event
190                setLowProfile(false, false, false);
191
192                try {
193                    mBarService.setSystemUiVisibility(0, View.SYSTEM_UI_FLAG_LOW_PROFILE);
194                } catch (android.os.RemoteException ex) {
195                }
196            }
197            return false;
198        }
199    };
200
201    public void setNavigationIconHints(int hints) {
202        setNavigationIconHints(hints, false);
203    }
204
205    public void setNavigationIconHints(int hints, boolean force) {
206        if (!force && hints == mNavigationIconHints) return;
207
208        if (DEBUG) {
209            android.widget.Toast.makeText(mContext,
210                "Navigation icon hints = " + hints,
211                500).show();
212        }
213
214        mNavigationIconHints = hints;
215
216        getBackButton().setAlpha(
217            (0 != (hints & StatusBarManager.NAVIGATION_HINT_BACK_NOP)) ? 0.5f : 1.0f);
218        getHomeButton().setAlpha(
219            (0 != (hints & StatusBarManager.NAVIGATION_HINT_HOME_NOP)) ? 0.5f : 1.0f);
220        getRecentsButton().setAlpha(
221            (0 != (hints & StatusBarManager.NAVIGATION_HINT_RECENT_NOP)) ? 0.5f : 1.0f);
222
223        ((ImageView)getBackButton()).setImageDrawable(
224            (0 != (hints & StatusBarManager.NAVIGATION_HINT_BACK_ALT))
225                ? (mVertical ? mBackAltLandIcon : mBackAltIcon)
226                : (mVertical ? mBackLandIcon : mBackIcon));
227    }
228
229    public void setDisabledFlags(int disabledFlags) {
230        setDisabledFlags(disabledFlags, false);
231    }
232
233    public void setDisabledFlags(int disabledFlags, boolean force) {
234        if (!force && mDisabledFlags == disabledFlags) return;
235
236        mDisabledFlags = disabledFlags;
237
238        final boolean disableHome = ((disabledFlags & View.STATUS_BAR_DISABLE_HOME) != 0);
239        final boolean disableRecent = ((disabledFlags & View.STATUS_BAR_DISABLE_RECENT) != 0);
240        final boolean disableBack = ((disabledFlags & View.STATUS_BAR_DISABLE_BACK) != 0);
241        final boolean disableSearch = ((disabledFlags & View.STATUS_BAR_DISABLE_SEARCH) != 0);
242
243        if (SLIPPERY_WHEN_DISABLED) {
244            setSlippery(disableHome && disableRecent && disableBack && disableSearch);
245        }
246
247        if (!mScreenOn && mCurrentView != null) {
248            ViewGroup navButtons = (ViewGroup) mCurrentView.findViewById(R.id.nav_buttons);
249            LayoutTransition lt = navButtons == null ? null : navButtons.getLayoutTransition();
250            if (lt != null) {
251                lt.disableTransitionType(
252                        LayoutTransition.CHANGE_APPEARING | LayoutTransition.CHANGE_DISAPPEARING |
253                        LayoutTransition.APPEARING | LayoutTransition.DISAPPEARING);
254            }
255        }
256
257        getBackButton()   .setVisibility(disableBack       ? View.INVISIBLE : View.VISIBLE);
258        getHomeButton()   .setVisibility(disableHome       ? View.INVISIBLE : View.VISIBLE);
259        getRecentsButton().setVisibility(disableRecent     ? View.INVISIBLE : View.VISIBLE);
260
261        getSearchLight().setVisibility((disableHome && !disableSearch) ? View.VISIBLE : View.GONE);
262    }
263
264    public void setSlippery(boolean newSlippery) {
265        WindowManager.LayoutParams lp = (WindowManager.LayoutParams) getLayoutParams();
266        if (lp != null) {
267            boolean oldSlippery = (lp.flags & WindowManager.LayoutParams.FLAG_SLIPPERY) != 0;
268            if (!oldSlippery && newSlippery) {
269                lp.flags |= WindowManager.LayoutParams.FLAG_SLIPPERY;
270            } else if (oldSlippery && !newSlippery) {
271                lp.flags &= ~WindowManager.LayoutParams.FLAG_SLIPPERY;
272            } else {
273                return;
274            }
275            WindowManager wm = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
276            wm.updateViewLayout(this, lp);
277        }
278    }
279
280    public void setMenuVisibility(final boolean show) {
281        setMenuVisibility(show, false);
282    }
283
284    public void setMenuVisibility(final boolean show, final boolean force) {
285        if (!force && mShowMenu == show) return;
286
287        mShowMenu = show;
288
289        getMenuButton().setVisibility(mShowMenu ? View.VISIBLE : View.INVISIBLE);
290    }
291
292    public void setLowProfile(final boolean lightsOut) {
293        setLowProfile(lightsOut, true, false);
294    }
295
296    public void setLowProfile(final boolean lightsOut, final boolean animate, final boolean force) {
297        if (!force && lightsOut == mLowProfile) return;
298
299        mLowProfile = lightsOut;
300
301        if (DEBUG) Slog.d(TAG, "setting lights " + (lightsOut?"out":"on"));
302
303        final View navButtons = mCurrentView.findViewById(R.id.nav_buttons);
304        final View lowLights = mCurrentView.findViewById(R.id.lights_out);
305
306        // ok, everyone, stop it right there
307        navButtons.animate().cancel();
308        lowLights.animate().cancel();
309
310        if (!animate) {
311            navButtons.setAlpha(lightsOut ? 0f : 1f);
312
313            lowLights.setAlpha(lightsOut ? 1f : 0f);
314            lowLights.setVisibility(lightsOut ? View.VISIBLE : View.GONE);
315        } else {
316            navButtons.animate()
317                .alpha(lightsOut ? 0f : 1f)
318                .setDuration(lightsOut ? 750 : 250)
319                .start();
320
321            lowLights.setOnTouchListener(mLightsOutListener);
322            if (lowLights.getVisibility() == View.GONE) {
323                lowLights.setAlpha(0f);
324                lowLights.setVisibility(View.VISIBLE);
325            }
326            lowLights.animate()
327                .alpha(lightsOut ? 1f : 0f)
328                .setDuration(lightsOut ? 750 : 250)
329                .setInterpolator(new AccelerateInterpolator(2.0f))
330                .setListener(lightsOut ? null : new AnimatorListenerAdapter() {
331                    @Override
332                    public void onAnimationEnd(Animator _a) {
333                        lowLights.setVisibility(View.GONE);
334                    }
335                })
336                .start();
337        }
338    }
339
340    public void setHidden(final boolean hide) {
341        if (hide == mHidden) return;
342
343        mHidden = hide;
344        Slog.d(TAG,
345            (hide ? "HIDING" : "SHOWING") + " navigation bar");
346
347        // bring up the lights no matter what
348        setLowProfile(false);
349    }
350
351    @Override
352    public void onFinishInflate() {
353        mRotatedViews[Surface.ROTATION_0] =
354        mRotatedViews[Surface.ROTATION_180] = findViewById(R.id.rot0);
355
356        mRotatedViews[Surface.ROTATION_90] = findViewById(R.id.rot90);
357
358        mRotatedViews[Surface.ROTATION_270] = NAVBAR_ALWAYS_AT_RIGHT
359                                                ? findViewById(R.id.rot90)
360                                                : findViewById(R.id.rot270);
361
362        mCurrentView = mRotatedViews[Surface.ROTATION_0];
363    }
364
365    public void reorient() {
366        final int rot = mDisplay.getRotation();
367        for (int i=0; i<4; i++) {
368            mRotatedViews[i].setVisibility(View.GONE);
369        }
370        mCurrentView = mRotatedViews[rot];
371        mCurrentView.setVisibility(View.VISIBLE);
372
373        mDeadZone = (DeadZone) mCurrentView.findViewById(R.id.deadzone);
374
375        // force the low profile & disabled states into compliance
376        setLowProfile(mLowProfile, false, true /* force */);
377        setDisabledFlags(mDisabledFlags, true /* force */);
378        setMenuVisibility(mShowMenu, true /* force */);
379
380        if (DEBUG) {
381            Slog.d(TAG, "reorient(): rot=" + mDisplay.getRotation());
382        }
383
384        setNavigationIconHints(mNavigationIconHints, true);
385    }
386
387    @Override
388    protected void onLayout(boolean changed, int l, int t, int r, int b) {
389        super.onLayout(changed, l, t, r, b);
390        mDelegateHelper.setInitialTouchRegion(getHomeButton(), getBackButton(), getRecentsButton());
391    }
392
393    @Override
394    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
395        if (DEBUG) Slog.d(TAG, String.format(
396                    "onSizeChanged: (%dx%d) old: (%dx%d)", w, h, oldw, oldh));
397
398        final boolean newVertical = w > 0 && h > w;
399        if (newVertical != mVertical) {
400            mVertical = newVertical;
401            //Slog.v(TAG, String.format("onSizeChanged: h=%d, w=%d, vert=%s", h, w, mVertical?"y":"n"));
402            reorient();
403        }
404
405        postCheckForInvalidLayout("sizeChanged");
406        super.onSizeChanged(w, h, oldw, oldh);
407    }
408
409    /*
410    @Override
411    protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
412        if (DEBUG) Slog.d(TAG, String.format(
413                    "onLayout: %s (%d,%d,%d,%d)",
414                    changed?"changed":"notchanged", left, top, right, bottom));
415        super.onLayout(changed, left, top, right, bottom);
416    }
417
418    // uncomment this for extra defensiveness in WORKAROUND_INVALID_LAYOUT situations: if all else
419    // fails, any touch on the display will fix the layout.
420    @Override
421    public boolean onInterceptTouchEvent(MotionEvent ev) {
422        if (DEBUG) Slog.d(TAG, "onInterceptTouchEvent: " + ev.toString());
423        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
424            postCheckForInvalidLayout("touch");
425        }
426        return super.onInterceptTouchEvent(ev);
427    }
428    */
429
430
431    private String getResourceName(int resId) {
432        if (resId != 0) {
433            final android.content.res.Resources res = mContext.getResources();
434            try {
435                return res.getResourceName(resId);
436            } catch (android.content.res.Resources.NotFoundException ex) {
437                return "(unknown)";
438            }
439        } else {
440            return "(null)";
441        }
442    }
443
444    private void postCheckForInvalidLayout(final String how) {
445        mHandler.obtainMessage(MSG_CHECK_INVALID_LAYOUT, 0, 0, how).sendToTarget();
446    }
447
448    private static String visibilityToString(int vis) {
449        switch (vis) {
450            case View.INVISIBLE:
451                return "INVISIBLE";
452            case View.GONE:
453                return "GONE";
454            default:
455                return "VISIBLE";
456        }
457    }
458
459    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
460        pw.println("NavigationBarView {");
461        final Rect r = new Rect();
462        final Point size = new Point();
463        mDisplay.getRealSize(size);
464
465        pw.println(String.format("      this: " + PhoneStatusBar.viewInfo(this)
466                        + " " + visibilityToString(getVisibility())));
467
468        getWindowVisibleDisplayFrame(r);
469        final boolean offscreen = r.right > size.x || r.bottom > size.y;
470        pw.println("      window: "
471                + r.toShortString()
472                + " " + visibilityToString(getWindowVisibility())
473                + (offscreen ? " OFFSCREEN!" : ""));
474
475        pw.println(String.format("      mCurrentView: id=%s (%dx%d) %s",
476                        getResourceName(mCurrentView.getId()),
477                        mCurrentView.getWidth(), mCurrentView.getHeight(),
478                        visibilityToString(mCurrentView.getVisibility())));
479
480        pw.println(String.format("      disabled=0x%08x vertical=%s hidden=%s low=%s menu=%s",
481                        mDisabledFlags,
482                        mVertical ? "true" : "false",
483                        mHidden ? "true" : "false",
484                        mLowProfile ? "true" : "false",
485                        mShowMenu ? "true" : "false"));
486
487        final View back = getBackButton();
488        final View home = getHomeButton();
489        final View recent = getRecentsButton();
490        final View menu = getMenuButton();
491
492        pw.println("      back: "
493                + PhoneStatusBar.viewInfo(back)
494                + " " + visibilityToString(back.getVisibility())
495                );
496        pw.println("      home: "
497                + PhoneStatusBar.viewInfo(home)
498                + " " + visibilityToString(home.getVisibility())
499                );
500        pw.println("      rcnt: "
501                + PhoneStatusBar.viewInfo(recent)
502                + " " + visibilityToString(recent.getVisibility())
503                );
504        pw.println("      menu: "
505                + PhoneStatusBar.viewInfo(menu)
506                + " " + visibilityToString(menu.getVisibility())
507                );
508        pw.println("    }");
509    }
510
511}
512