Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 17 | package com.android.systemui; |
| 18 | |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 19 | import android.animation.LayoutTransition; |
Jim Miller | 998bb76 | 2012-05-21 13:37:27 -0700 | [diff] [blame] | 20 | import android.app.ActivityOptions; |
Jim Miller | 3b29187 | 2012-04-24 18:54:20 -0700 | [diff] [blame] | 21 | import android.app.SearchManager; |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 22 | import android.content.ActivityNotFoundException; |
Jim Miller | 3b29187 | 2012-04-24 18:54:20 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Philip Milne | a01a2a89 | 2012-05-29 14:46:54 -0700 | [diff] [blame] | 26 | import android.content.res.Resources; |
| 27 | import android.os.Vibrator; |
| 28 | import android.provider.Settings; |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Jim Miller | 3b29187 | 2012-04-24 18:54:20 -0700 | [diff] [blame] | 30 | import android.util.Slog; |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 31 | import android.view.MotionEvent; |
| 32 | import android.view.View; |
| 33 | import android.view.ViewGroup; |
Jim Miller | 2083042 | 2012-05-15 20:46:03 -0700 | [diff] [blame] | 34 | import android.view.ViewTreeObserver; |
| 35 | import android.view.ViewTreeObserver.OnPreDrawListener; |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 36 | import android.widget.FrameLayout; |
Jeff Brown | de7a8ea | 2012-06-13 18:28:57 -0700 | [diff] [blame] | 37 | |
Jim Miller | 955a016 | 2012-06-11 21:06:13 -0700 | [diff] [blame] | 38 | import com.android.internal.widget.multiwaveview.GlowPadView; |
| 39 | import com.android.internal.widget.multiwaveview.GlowPadView.OnTriggerListener; |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 40 | import com.android.systemui.R; |
| 41 | import com.android.systemui.recent.StatusBarTouchProxy; |
| 42 | import com.android.systemui.statusbar.BaseStatusBar; |
Jim Miller | 9a720f5 | 2012-05-30 03:19:43 -0700 | [diff] [blame] | 43 | import com.android.systemui.statusbar.CommandQueue; |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 44 | import com.android.systemui.statusbar.phone.PhoneStatusBar; |
| 45 | import com.android.systemui.statusbar.tablet.StatusBarPanel; |
| 46 | import com.android.systemui.statusbar.tablet.TabletStatusBar; |
| 47 | |
| 48 | public class SearchPanelView extends FrameLayout implements |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 49 | StatusBarPanel, ActivityOptions.OnAnimationStartedListener { |
| 50 | private static final int SEARCH_PANEL_HOLD_DURATION = 0; |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 51 | static final String TAG = "SearchPanelView"; |
| 52 | static final boolean DEBUG = TabletStatusBar.DEBUG || PhoneStatusBar.DEBUG || false; |
Jim Miller | 3294b6b | 2012-05-31 17:49:13 -0700 | [diff] [blame] | 53 | private static final String ASSIST_ICON_METADATA_NAME = |
| 54 | "com.android.systemui.action_assist_icon"; |
Jim Miller | 9a720f5 | 2012-05-30 03:19:43 -0700 | [diff] [blame] | 55 | private final Context mContext; |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 56 | private BaseStatusBar mBar; |
| 57 | private StatusBarTouchProxy mStatusBarTouchProxy; |
| 58 | |
| 59 | private boolean mShowing; |
| 60 | private View mSearchTargetsContainer; |
Jim Miller | 955a016 | 2012-06-11 21:06:13 -0700 | [diff] [blame] | 61 | private GlowPadView mGlowPadView; |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 62 | |
| 63 | public SearchPanelView(Context context, AttributeSet attrs) { |
| 64 | this(context, attrs, 0); |
| 65 | } |
| 66 | |
| 67 | public SearchPanelView(Context context, AttributeSet attrs, int defStyle) { |
| 68 | super(context, attrs, defStyle); |
| 69 | mContext = context; |
| 70 | } |
| 71 | |
Jim Miller | 0799440 | 2012-05-02 14:22:27 -0700 | [diff] [blame] | 72 | private void startAssistActivity() { |
Fabrice Di Meglio | 4d0d38b | 2012-05-25 11:36:03 -0700 | [diff] [blame] | 73 | // Close Recent Apps if needed |
Jim Miller | 9a720f5 | 2012-05-30 03:19:43 -0700 | [diff] [blame] | 74 | mBar.animateCollapse(CommandQueue.FLAG_EXCLUDE_SEARCH_PANEL); |
Fabrice Di Meglio | 4d0d38b | 2012-05-25 11:36:03 -0700 | [diff] [blame] | 75 | // Launch Assist |
Jim Miller | 45308b1 | 2012-06-18 19:23:39 -0700 | [diff] [blame^] | 76 | Intent intent = SearchManager.getAssistIntent(mContext); |
Jim Miller | 8ea8a29 | 2012-05-30 09:50:00 -0700 | [diff] [blame] | 77 | if (intent == null) return; |
Jim Miller | 998bb76 | 2012-05-21 13:37:27 -0700 | [diff] [blame] | 78 | try { |
Jim Miller | c150b19 | 2012-05-17 15:47:25 -0700 | [diff] [blame] | 79 | ActivityOptions opts = ActivityOptions.makeCustomAnimation(mContext, |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 80 | R.anim.search_launch_enter, R.anim.search_launch_exit, |
| 81 | getHandler(), this); |
Jim Miller | c150b19 | 2012-05-17 15:47:25 -0700 | [diff] [blame] | 82 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 83 | mContext.startActivity(intent, opts.toBundle()); |
Jim Miller | 998bb76 | 2012-05-21 13:37:27 -0700 | [diff] [blame] | 84 | } catch (ActivityNotFoundException e) { |
| 85 | Slog.w(TAG, "Activity not found for " + intent.getAction()); |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 86 | onAnimationStarted(); |
Jim Miller | 0799440 | 2012-05-02 14:22:27 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Jim Miller | 955a016 | 2012-06-11 21:06:13 -0700 | [diff] [blame] | 90 | class GlowPadTriggerListener implements GlowPadView.OnTriggerListener { |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 91 | boolean mWaitingForLaunch; |
Jim Miller | 998bb76 | 2012-05-21 13:37:27 -0700 | [diff] [blame] | 92 | |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 93 | public void onGrabbed(View v, int handle) { |
| 94 | } |
| 95 | |
| 96 | public void onReleased(View v, int handle) { |
| 97 | } |
| 98 | |
| 99 | public void onGrabbedStateChange(View v, int handle) { |
Jim Miller | a073e57 | 2012-05-23 17:03:27 -0700 | [diff] [blame] | 100 | if (!mWaitingForLaunch && OnTriggerListener.NO_HANDLE == handle) { |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 101 | mBar.hideSearchPanel(); |
| 102 | } |
| 103 | } |
| 104 | |
Jim Miller | a073e57 | 2012-05-23 17:03:27 -0700 | [diff] [blame] | 105 | public void onTrigger(View v, final int target) { |
Jim Miller | 955a016 | 2012-06-11 21:06:13 -0700 | [diff] [blame] | 106 | final int resId = mGlowPadView.getResourceIdForTarget(target); |
Jim Miller | a073e57 | 2012-05-23 17:03:27 -0700 | [diff] [blame] | 107 | switch (resId) { |
Jim Miller | 20039ad | 2012-06-15 17:26:28 -0700 | [diff] [blame] | 108 | case com.android.internal.R.drawable.ic_action_assist_generic: |
Jim Miller | a073e57 | 2012-05-23 17:03:27 -0700 | [diff] [blame] | 109 | mWaitingForLaunch = true; |
| 110 | startAssistActivity(); |
Philip Milne | a01a2a89 | 2012-05-29 14:46:54 -0700 | [diff] [blame] | 111 | vibrate(); |
Philip Milne | a01a2a89 | 2012-05-29 14:46:54 -0700 | [diff] [blame] | 112 | break; |
Jim Miller | a073e57 | 2012-05-23 17:03:27 -0700 | [diff] [blame] | 113 | } |
Jim Miller | 998bb76 | 2012-05-21 13:37:27 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | public void onFinishFinalAnimation() { |
Jim Miller | 3b29187 | 2012-04-24 18:54:20 -0700 | [diff] [blame] | 117 | } |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 118 | } |
Jim Miller | 955a016 | 2012-06-11 21:06:13 -0700 | [diff] [blame] | 119 | final GlowPadTriggerListener mGlowPadViewListener = new GlowPadTriggerListener(); |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 120 | |
| 121 | @Override |
| 122 | public void onAnimationStarted() { |
| 123 | postDelayed(new Runnable() { |
| 124 | public void run() { |
Jim Miller | 955a016 | 2012-06-11 21:06:13 -0700 | [diff] [blame] | 125 | mGlowPadViewListener.mWaitingForLaunch = false; |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 126 | mBar.hideSearchPanel(); |
| 127 | } |
| 128 | }, SEARCH_PANEL_HOLD_DURATION); |
| 129 | } |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 130 | |
| 131 | @Override |
| 132 | protected void onFinishInflate() { |
| 133 | super.onFinishInflate(); |
| 134 | mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Jim Miller | 9a720f5 | 2012-05-30 03:19:43 -0700 | [diff] [blame] | 135 | mSearchTargetsContainer = findViewById(R.id.search_panel_container); |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 136 | mStatusBarTouchProxy = (StatusBarTouchProxy) findViewById(R.id.status_bar_touch_proxy); |
| 137 | // TODO: fetch views |
Jim Miller | 955a016 | 2012-06-11 21:06:13 -0700 | [diff] [blame] | 138 | mGlowPadView = (GlowPadView) findViewById(R.id.glow_pad_view); |
| 139 | mGlowPadView.setOnTriggerListener(mGlowPadViewListener); |
Jim Miller | 45308b1 | 2012-06-18 19:23:39 -0700 | [diff] [blame^] | 140 | } |
| 141 | |
| 142 | private void maybeSwapSearchIcon() { |
| 143 | Intent intent = SearchManager.getAssistIntent(mContext); |
| 144 | if (intent != null) { |
| 145 | ComponentName component = intent.getComponent(); |
| 146 | if (component == null || !mGlowPadView.replaceTargetDrawablesIfPresent(component, |
| 147 | ASSIST_ICON_METADATA_NAME, |
| 148 | com.android.internal.R.drawable.ic_action_assist_generic)) { |
| 149 | if (DEBUG) Slog.v(TAG, "Couldn't grab icon for component " + component); |
Jim Miller | 3294b6b | 2012-05-31 17:49:13 -0700 | [diff] [blame] | 150 | } |
Jim Miller | 3294b6b | 2012-05-31 17:49:13 -0700 | [diff] [blame] | 151 | } |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | private boolean pointInside(int x, int y, View v) { |
| 155 | final int l = v.getLeft(); |
| 156 | final int r = v.getRight(); |
| 157 | final int t = v.getTop(); |
| 158 | final int b = v.getBottom(); |
| 159 | return x >= l && x < r && y >= t && y < b; |
| 160 | } |
| 161 | |
| 162 | public boolean isInContentArea(int x, int y) { |
| 163 | if (pointInside(x, y, mSearchTargetsContainer)) { |
| 164 | return true; |
| 165 | } else if (mStatusBarTouchProxy != null && |
| 166 | pointInside(x, y, mStatusBarTouchProxy)) { |
| 167 | return true; |
| 168 | } else { |
| 169 | return false; |
| 170 | } |
| 171 | } |
| 172 | |
Jim Miller | 9a720f5 | 2012-05-30 03:19:43 -0700 | [diff] [blame] | 173 | private final OnPreDrawListener mPreDrawListener = new ViewTreeObserver.OnPreDrawListener() { |
Jim Miller | 2083042 | 2012-05-15 20:46:03 -0700 | [diff] [blame] | 174 | public boolean onPreDraw() { |
| 175 | getViewTreeObserver().removeOnPreDrawListener(this); |
Jim Miller | 955a016 | 2012-06-11 21:06:13 -0700 | [diff] [blame] | 176 | mGlowPadView.resumeAnimations(); |
Jim Miller | 2083042 | 2012-05-15 20:46:03 -0700 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | }; |
| 180 | |
Philip Milne | a01a2a89 | 2012-05-29 14:46:54 -0700 | [diff] [blame] | 181 | private void vibrate() { |
| 182 | Context context = getContext(); |
| 183 | if (Settings.System.getInt(context.getContentResolver(), |
| 184 | Settings.System.HAPTIC_FEEDBACK_ENABLED, 1) != 0) { |
| 185 | Resources res = context.getResources(); |
| 186 | Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 187 | vibrator.vibrate(res.getInteger(R.integer.config_search_panel_view_vibration_duration)); |
| 188 | } |
| 189 | } |
| 190 | |
Jim Miller | d99e7fd | 2012-05-08 16:30:42 -0700 | [diff] [blame] | 191 | public void show(final boolean show, boolean animate) { |
Jim Miller | a073e57 | 2012-05-23 17:03:27 -0700 | [diff] [blame] | 192 | if (!show) { |
| 193 | final LayoutTransition transitioner = animate ? createLayoutTransitioner() : null; |
Philip Milne | a01a2a89 | 2012-05-29 14:46:54 -0700 | [diff] [blame] | 194 | ((ViewGroup) mSearchTargetsContainer).setLayoutTransition(transitioner); |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 195 | } |
Jim Miller | a073e57 | 2012-05-23 17:03:27 -0700 | [diff] [blame] | 196 | mShowing = show; |
Jim Miller | 2083042 | 2012-05-15 20:46:03 -0700 | [diff] [blame] | 197 | if (show) { |
Jim Miller | 45308b1 | 2012-06-18 19:23:39 -0700 | [diff] [blame^] | 198 | maybeSwapSearchIcon(); |
Jim Miller | 2083042 | 2012-05-15 20:46:03 -0700 | [diff] [blame] | 199 | if (getVisibility() != View.VISIBLE) { |
| 200 | setVisibility(View.VISIBLE); |
| 201 | // Don't start the animation until we've created the layer, which is done |
| 202 | // right before we are drawn |
Jim Miller | 955a016 | 2012-06-11 21:06:13 -0700 | [diff] [blame] | 203 | mGlowPadView.suspendAnimations(); |
| 204 | mGlowPadView.ping(); |
Jim Miller | 2083042 | 2012-05-15 20:46:03 -0700 | [diff] [blame] | 205 | getViewTreeObserver().addOnPreDrawListener(mPreDrawListener); |
Philip Milne | a01a2a89 | 2012-05-29 14:46:54 -0700 | [diff] [blame] | 206 | vibrate(); |
Jim Miller | d99e7fd | 2012-05-08 16:30:42 -0700 | [diff] [blame] | 207 | } |
Jim Miller | 2083042 | 2012-05-15 20:46:03 -0700 | [diff] [blame] | 208 | setFocusable(true); |
| 209 | setFocusableInTouchMode(true); |
| 210 | requestFocus(); |
| 211 | } else { |
| 212 | setVisibility(View.INVISIBLE); |
| 213 | } |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | public void hide(boolean animate) { |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 217 | if (mBar != null) { |
| 218 | // This will indirectly cause show(false, ...) to get called |
Jim Miller | 9a720f5 | 2012-05-30 03:19:43 -0700 | [diff] [blame] | 219 | mBar.animateCollapse(CommandQueue.FLAG_EXCLUDE_NONE); |
Jim Miller | d99e7fd | 2012-05-08 16:30:42 -0700 | [diff] [blame] | 220 | } else { |
| 221 | setVisibility(View.INVISIBLE); |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 225 | /** |
| 226 | * We need to be aligned at the bottom. LinearLayout can't do this, so instead, |
| 227 | * let LinearLayout do all the hard work, and then shift everything down to the bottom. |
| 228 | */ |
| 229 | @Override |
| 230 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 231 | super.onLayout(changed, l, t, r, b); |
| 232 | // setPanelHeight(mSearchTargetsContainer.getHeight()); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public boolean dispatchHoverEvent(MotionEvent event) { |
| 237 | // Ignore hover events outside of this panel bounds since such events |
| 238 | // generate spurious accessibility events with the panel content when |
| 239 | // tapping outside of it, thus confusing the user. |
| 240 | final int x = (int) event.getX(); |
| 241 | final int y = (int) event.getY(); |
| 242 | if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight()) { |
| 243 | return super.dispatchHoverEvent(event); |
| 244 | } |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Whether the panel is showing, or, if it's animating, whether it will be |
| 250 | * when the animation is done. |
| 251 | */ |
| 252 | public boolean isShowing() { |
| 253 | return mShowing; |
| 254 | } |
| 255 | |
| 256 | public void setBar(BaseStatusBar bar) { |
| 257 | mBar = bar; |
| 258 | } |
| 259 | |
| 260 | public void setStatusBarView(final View statusBarView) { |
| 261 | if (mStatusBarTouchProxy != null) { |
| 262 | mStatusBarTouchProxy.setStatusBar(statusBarView); |
Jim Miller | 955a016 | 2012-06-11 21:06:13 -0700 | [diff] [blame] | 263 | // mGlowPadView.setOnTouchListener(new OnTouchListener() { |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 264 | // public boolean onTouch(View v, MotionEvent event) { |
| 265 | // return statusBarView.onTouchEvent(event); |
| 266 | // } |
| 267 | // }); |
| 268 | } |
| 269 | } |
| 270 | |
Jim Miller | a073e57 | 2012-05-23 17:03:27 -0700 | [diff] [blame] | 271 | private LayoutTransition createLayoutTransitioner() { |
| 272 | LayoutTransition transitioner = new LayoutTransition(); |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 273 | transitioner.setDuration(200); |
| 274 | transitioner.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0); |
| 275 | transitioner.setAnimator(LayoutTransition.DISAPPEARING, null); |
Jim Miller | a073e57 | 2012-05-23 17:03:27 -0700 | [diff] [blame] | 276 | return transitioner; |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 277 | } |
Jim Miller | 45308b1 | 2012-06-18 19:23:39 -0700 | [diff] [blame^] | 278 | |
| 279 | public boolean isAssistantAvailable() { |
| 280 | return SearchManager.getAssistIntent(mContext) != null; |
| 281 | } |
Jim Miller | e898ac5 | 2012-04-06 17:10:57 -0700 | [diff] [blame] | 282 | } |