| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
|
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.BroadcastReceiver; |
| 22 |
import android.content.ComponentName; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.Intent; |
| 25 |
import android.content.IntentFilter; |
| 26 |
import android.content.pm.ActivityInfo; |
| 27 |
import android.content.pm.PackageManager; |
| 28 |
import android.content.pm.PackageManager.NameNotFoundException; |
| 29 |
import android.content.res.Configuration; |
| 30 |
import android.graphics.drawable.Drawable; |
| 31 |
import android.net.Uri; |
| 32 |
import android.os.Bundle; |
| 33 |
import android.speech.RecognizerIntent; |
| 34 |
import android.text.InputType; |
| 35 |
import android.text.TextUtils; |
| 36 |
import android.util.AttributeSet; |
| 37 |
import android.util.Log; |
| 38 |
import android.util.TypedValue; |
| 39 |
import android.view.ActionMode; |
| 40 |
import android.view.Gravity; |
| 41 |
import android.view.KeyEvent; |
| 42 |
import android.view.MotionEvent; |
| 43 |
import android.view.View; |
| 44 |
import android.view.ViewConfiguration; |
| 45 |
import android.view.ViewGroup; |
| 46 |
import android.view.Window; |
| 47 |
import android.view.WindowManager; |
| 48 |
import android.view.inputmethod.InputMethodManager; |
| 49 |
import android.widget.AutoCompleteTextView; |
| 50 |
import android.widget.Filterable; |
| 51 |
import android.widget.ImageView; |
| 52 |
import android.widget.LinearLayout; |
| 53 |
import android.widget.ListPopupWindow; |
| 54 |
import android.widget.SearchView; |
| 55 |
import android.widget.TextView; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public class SearchDialog extends Dialog { |
| 64 |
|
| 65 |
|
| 66 |
private static final boolean DBG = false; |
| 67 |
private static final String LOG_TAG = "SearchDialog"; |
| 68 |
|
| 69 |
private static final String INSTANCE_KEY_COMPONENT = "comp"; |
| 70 |
private static final String INSTANCE_KEY_APPDATA = "data"; |
| 71 |
private static final String INSTANCE_KEY_USER_QUERY = "uQry"; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private static final String IME_OPTION_NO_MICROPHONE = "nm"; |
| 76 |
|
| 77 |
private static final int SEARCH_PLATE_LEFT_PADDING_NON_GLOBAL = 7; |
| 78 |
|
| 79 |
|
| 80 |
private TextView mBadgeLabel; |
| 81 |
private ImageView mAppIcon; |
| 82 |
private AutoCompleteTextView mSearchAutoComplete; |
| 83 |
private View mSearchPlate; |
| 84 |
private SearchView mSearchView; |
| 85 |
private Drawable mWorkingSpinner; |
| 86 |
private View mCloseSearch; |
| 87 |
|
| 88 |
|
| 89 |
private SearchableInfo mSearchable; |
| 90 |
private ComponentName mLaunchComponent; |
| 91 |
private Bundle mAppSearchData; |
| 92 |
private Context mActivityContext; |
| 93 |
|
| 94 |
|
| 95 |
private final Intent mVoiceWebSearchIntent; |
| 96 |
private final Intent mVoiceAppSearchIntent; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
private String mUserQuery; |
| 102 |
|
| 103 |
|
| 104 |
private int mSearchAutoCompleteImeOptions; |
| 105 |
|
| 106 |
private BroadcastReceiver mConfChangeListener = new BroadcastReceiver() { |
| 107 |
@Override |
| 108 |
public void onReceive(Context context, Intent intent) { |
| 109 |
if (intent.getAction().equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
| 110 |
onConfigurationChanged(); |
| 111 |
} |
| 112 |
} |
| 113 |
}; |
| 114 |
|
| 115 |
static int resolveDialogTheme(Context context) { |
| 116 |
TypedValue outValue = new TypedValue(); |
| 117 |
context.getTheme().resolveAttribute(com.android.internal.R.attr.searchDialogTheme, |
| 118 |
outValue, true); |
| 119 |
return outValue.resourceId; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public SearchDialog(Context context, SearchManager searchManager) { |
| 128 |
super(context, resolveDialogTheme(context)); |
| 129 |
|
| 130 |
|
| 131 |
mVoiceWebSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH); |
| 132 |
mVoiceWebSearchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 133 |
mVoiceWebSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, |
| 134 |
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); |
| 135 |
|
| 136 |
mVoiceAppSearchIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
| 137 |
mVoiceAppSearchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
@Override |
| 145 |
protected void onCreate(Bundle savedInstanceState) { |
| 146 |
super.onCreate(savedInstanceState); |
| 147 |
|
| 148 |
Window theWindow = getWindow(); |
| 149 |
WindowManager.LayoutParams lp = theWindow.getAttributes(); |
| 150 |
lp.width = ViewGroup.LayoutParams.MATCH_PARENT; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
lp.height = ViewGroup.LayoutParams.MATCH_PARENT; |
| 155 |
lp.gravity = Gravity.TOP | Gravity.FILL_HORIZONTAL; |
| 156 |
lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE; |
| 157 |
theWindow.setAttributes(lp); |
| 158 |
|
| 159 |
|
| 160 |
setCanceledOnTouchOutside(true); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
private void createContentView() { |
| 168 |
setContentView(com.android.internal.R.layout.search_bar); |
| 169 |
|
| 170 |
|
| 171 |
mSearchView = findViewById(com.android.internal.R.id.search_view); |
| 172 |
mSearchView.setIconified(false); |
| 173 |
mSearchView.setOnCloseListener(mOnCloseListener); |
| 174 |
mSearchView.setOnQueryTextListener(mOnQueryChangeListener); |
| 175 |
mSearchView.setOnSuggestionListener(mOnSuggestionSelectionListener); |
| 176 |
mSearchView.onActionViewExpanded(); |
| 177 |
|
| 178 |
mCloseSearch = findViewById(com.android.internal.R.id.closeButton); |
| 179 |
mCloseSearch.setOnClickListener(new View.OnClickListener() { |
| 180 |
@Override |
| 181 |
public void onClick(View v) { |
| 182 |
dismiss(); |
| 183 |
} |
| 184 |
}); |
| 185 |
|
| 186 |
|
| 187 |
mBadgeLabel = (TextView) mSearchView.findViewById(com.android.internal.R.id.search_badge); |
| 188 |
mSearchAutoComplete = (AutoCompleteTextView) |
| 189 |
mSearchView.findViewById(com.android.internal.R.id.search_src_text); |
| 190 |
mAppIcon = findViewById(com.android.internal.R.id.search_app_icon); |
| 191 |
mSearchPlate = mSearchView.findViewById(com.android.internal.R.id.search_plate); |
| 192 |
mWorkingSpinner = getContext().getDrawable(com.android.internal.R.drawable.search_spinner); |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
setWorking(false); |
| 197 |
|
| 198 |
|
| 199 |
mBadgeLabel.setVisibility(View.GONE); |
| 200 |
|
| 201 |
|
| 202 |
mSearchAutoCompleteImeOptions = mSearchAutoComplete.getImeOptions(); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public boolean show(String initialQuery, boolean selectInitialQuery, |
| 211 |
ComponentName componentName, Bundle appSearchData) { |
| 212 |
boolean success = doShow(initialQuery, selectInitialQuery, componentName, appSearchData); |
| 213 |
if (success) { |
| 214 |
|
| 215 |
|
| 216 |
mSearchAutoComplete.showDropDownAfterLayout(); |
| 217 |
} |
| 218 |
return success; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
private boolean doShow(String initialQuery, boolean selectInitialQuery, |
| 228 |
ComponentName componentName, Bundle appSearchData) { |
| 229 |
|
| 230 |
if (!show(componentName, appSearchData)) { |
| 231 |
return false; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
setUserQuery(initialQuery); |
| 236 |
if (selectInitialQuery) { |
| 237 |
mSearchAutoComplete.selectAll(); |
| 238 |
} |
| 239 |
|
| 240 |
return true; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
private boolean show(ComponentName componentName, Bundle appSearchData) { |
| 249 |
|
| 250 |
if (DBG) { |
| 251 |
Log.d(LOG_TAG, "show(" + componentName + ", " |
| 252 |
+ appSearchData + ")"); |
| 253 |
} |
| 254 |
|
| 255 |
SearchManager searchManager = (SearchManager) |
| 256 |
mContext.getSystemService(Context.SEARCH_SERVICE); |
| 257 |
|
| 258 |
mSearchable = searchManager.getSearchableInfo(componentName); |
| 259 |
|
| 260 |
if (mSearchable == null) { |
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
mLaunchComponent = componentName; |
| 265 |
mAppSearchData = appSearchData; |
| 266 |
mActivityContext = mSearchable.getActivityContext(getContext()); |
| 267 |
|
| 268 |
|
| 269 |
if (!isShowing()) { |
| 270 |
|
| 271 |
|
| 272 |
createContentView(); |
| 273 |
mSearchView.setSearchableInfo(mSearchable); |
| 274 |
mSearchView.setAppSearchData(mAppSearchData); |
| 275 |
|
| 276 |
show(); |
| 277 |
} |
| 278 |
updateUI(); |
| 279 |
|
| 280 |
return true; |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
public void onStart() { |
| 285 |
super.onStart(); |
| 286 |
|
| 287 |
|
| 288 |
IntentFilter filter = new IntentFilter(); |
| 289 |
filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
| 290 |
getContext().registerReceiver(mConfChangeListener, filter); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
@Override |
| 300 |
public void onStop() { |
| 301 |
super.onStop(); |
| 302 |
|
| 303 |
getContext().unregisterReceiver(mConfChangeListener); |
| 304 |
|
| 305 |
|
| 306 |
mLaunchComponent = null; |
| 307 |
mAppSearchData = null; |
| 308 |
mSearchable = null; |
| 309 |
mUserQuery = null; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
@UnsupportedAppUsage |
| 319 |
public void setWorking(boolean working) { |
| 320 |
mWorkingSpinner.setAlpha(working ? 255 : 0); |
| 321 |
mWorkingSpinner.setVisible(working, false); |
| 322 |
mWorkingSpinner.invalidateSelf(); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
@Override |
| 332 |
public Bundle onSaveInstanceState() { |
| 333 |
if (!isShowing()) return null; |
| 334 |
|
| 335 |
Bundle bundle = new Bundle(); |
| 336 |
|
| 337 |
|
| 338 |
bundle.putParcelable(INSTANCE_KEY_COMPONENT, mLaunchComponent); |
| 339 |
bundle.putBundle(INSTANCE_KEY_APPDATA, mAppSearchData); |
| 340 |
bundle.putString(INSTANCE_KEY_USER_QUERY, mUserQuery); |
| 341 |
|
| 342 |
return bundle; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
@Override |
| 352 |
public void onRestoreInstanceState(Bundle savedInstanceState) { |
| 353 |
if (savedInstanceState == null) return; |
| 354 |
|
| 355 |
ComponentName launchComponent = savedInstanceState.getParcelable(INSTANCE_KEY_COMPONENT); |
| 356 |
Bundle appSearchData = savedInstanceState.getBundle(INSTANCE_KEY_APPDATA); |
| 357 |
String userQuery = savedInstanceState.getString(INSTANCE_KEY_USER_QUERY); |
| 358 |
|
| 359 |
|
| 360 |
if (!doShow(userQuery, false, launchComponent, appSearchData)) { |
| 361 |
|
| 362 |
return; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
public void onConfigurationChanged() { |
| 370 |
if (mSearchable != null && isShowing()) { |
| 371 |
|
| 372 |
updateSearchAppIcon(); |
| 373 |
updateSearchBadge(); |
| 374 |
if (isLandscapeMode(getContext())) { |
| 375 |
mSearchAutoComplete.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED); |
| 376 |
if (mSearchAutoComplete.isDropDownAlwaysVisible() || enoughToFilter()) { |
| 377 |
mSearchAutoComplete.showDropDown(); |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
@UnsupportedAppUsage |
| 384 |
static boolean isLandscapeMode(Context context) { |
| 385 |
return context.getResources().getConfiguration().orientation |
| 386 |
== Configuration.ORIENTATION_LANDSCAPE; |
| 387 |
} |
| 388 |
|
| 389 |
private boolean enoughToFilter() { |
| 390 |
Filterable filterableAdapter = (Filterable) mSearchAutoComplete.getAdapter(); |
| 391 |
if (filterableAdapter == null || filterableAdapter.getFilter() == null) { |
| 392 |
return false; |
| 393 |
} |
| 394 |
|
| 395 |
return mSearchAutoComplete.enoughToFilter(); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
private void updateUI() { |
| 402 |
if (mSearchable != null) { |
| 403 |
mDecor.setVisibility(View.VISIBLE); |
| 404 |
updateSearchAutoComplete(); |
| 405 |
updateSearchAppIcon(); |
| 406 |
updateSearchBadge(); |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
int inputType = mSearchable.getInputType(); |
| 414 |
|
| 415 |
|
| 416 |
if ((inputType & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_TEXT) { |
| 417 |
|
| 418 |
|
| 419 |
inputType &= ~InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE; |
| 420 |
if (mSearchable.getSuggestAuthority() != null) { |
| 421 |
inputType |= InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE; |
| 422 |
} |
| 423 |
} |
| 424 |
mSearchAutoComplete.setInputType(inputType); |
| 425 |
mSearchAutoCompleteImeOptions = mSearchable.getImeOptions(); |
| 426 |
mSearchAutoComplete.setImeOptions(mSearchAutoCompleteImeOptions); |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
if (mSearchable.getVoiceSearchEnabled()) { |
| 431 |
mSearchAutoComplete.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE); |
| 432 |
} else { |
| 433 |
mSearchAutoComplete.setPrivateImeOptions(null); |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
private void updateSearchAutoComplete() { |
| 442 |
|
| 443 |
mSearchAutoComplete.setDropDownDismissedOnCompletion(false); |
| 444 |
mSearchAutoComplete.setForceIgnoreOutsideTouch(false); |
| 445 |
} |
| 446 |
|
| 447 |
private void updateSearchAppIcon() { |
| 448 |
PackageManager pm = getContext().getPackageManager(); |
| 449 |
Drawable icon; |
| 450 |
try { |
| 451 |
ActivityInfo info = pm.getActivityInfo(mLaunchComponent, 0); |
| 452 |
icon = pm.getApplicationIcon(info.applicationInfo); |
| 453 |
if (DBG) |
| 454 |
Log.d(LOG_TAG, "Using app-specific icon"); |
| 455 |
} catch (NameNotFoundException e) { |
| 456 |
icon = pm.getDefaultActivityIcon(); |
| 457 |
Log.w(LOG_TAG, mLaunchComponent + " not found, using generic app icon"); |
| 458 |
} |
| 459 |
mAppIcon.setImageDrawable(icon); |
| 460 |
mAppIcon.setVisibility(View.VISIBLE); |
| 461 |
mSearchPlate.setPadding(SEARCH_PLATE_LEFT_PADDING_NON_GLOBAL, mSearchPlate.getPaddingTop(), mSearchPlate.getPaddingRight(), mSearchPlate.getPaddingBottom()); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
private void updateSearchBadge() { |
| 468 |
|
| 469 |
int visibility = View.GONE; |
| 470 |
Drawable icon = null; |
| 471 |
CharSequence text = null; |
| 472 |
|
| 473 |
|
| 474 |
if (mSearchable.useBadgeIcon()) { |
| 475 |
icon = mActivityContext.getDrawable(mSearchable.getIconId()); |
| 476 |
visibility = View.VISIBLE; |
| 477 |
if (DBG) Log.d(LOG_TAG, "Using badge icon: " + mSearchable.getIconId()); |
| 478 |
} else if (mSearchable.useBadgeLabel()) { |
| 479 |
text = mActivityContext.getResources().getText(mSearchable.getLabelId()).toString(); |
| 480 |
visibility = View.VISIBLE; |
| 481 |
if (DBG) Log.d(LOG_TAG, "Using badge label: " + mSearchable.getLabelId()); |
| 482 |
} |
| 483 |
|
| 484 |
mBadgeLabel.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); |
| 485 |
mBadgeLabel.setText(text); |
| 486 |
mBadgeLabel.setVisibility(visibility); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
@Override |
| 499 |
public boolean onTouchEvent(MotionEvent event) { |
| 500 |
|
| 501 |
if (!mSearchAutoComplete.isPopupShowing() && isOutOfBounds(mSearchPlate, event)) { |
| 502 |
if (DBG) Log.d(LOG_TAG, "Pop-up not showing and outside of search plate."); |
| 503 |
cancel(); |
| 504 |
return true; |
| 505 |
} |
| 506 |
|
| 507 |
return super.onTouchEvent(event); |
| 508 |
} |
| 509 |
|
| 510 |
private boolean isOutOfBounds(View v, MotionEvent event) { |
| 511 |
final int x = (int) event.getX(); |
| 512 |
final int y = (int) event.getY(); |
| 513 |
final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop(); |
| 514 |
return (x < -slop) || (y < -slop) |
| 515 |
|| (x > (v.getWidth()+slop)) |
| 516 |
|| (y > (v.getHeight()+slop)); |
| 517 |
} |
| 518 |
|
| 519 |
@Override |
| 520 |
public void hide() { |
| 521 |
if (!isShowing()) return; |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
InputMethodManager imm = getContext().getSystemService(InputMethodManager.class); |
| 526 |
if (imm != null) { |
| 527 |
imm.hideSoftInputFromWindow( |
| 528 |
getWindow().getDecorView().getWindowToken(), 0); |
| 529 |
} |
| 530 |
|
| 531 |
super.hide(); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
@UnsupportedAppUsage |
| 538 |
public void launchQuerySearch() { |
| 539 |
launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
@UnsupportedAppUsage |
| 551 |
protected void launchQuerySearch(int actionKey, String actionMsg) { |
| 552 |
String query = mSearchAutoComplete.getText().toString(); |
| 553 |
String action = Intent.ACTION_SEARCH; |
| 554 |
Intent intent = createIntent(action, null, null, query, actionKey, actionMsg); |
| 555 |
launchIntent(intent); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
private void launchIntent(Intent intent) { |
| 562 |
if (intent == null) { |
| 563 |
return; |
| 564 |
} |
| 565 |
Log.d(LOG_TAG, "launching " + intent); |
| 566 |
try { |
| 567 |
|
| 568 |
|
| 569 |
getContext().startActivity(intent); |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
dismiss(); |
| 575 |
} catch (RuntimeException ex) { |
| 576 |
Log.e(LOG_TAG, "Failed launch activity: " + intent, ex); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
public void setListSelection(int index) { |
| 584 |
mSearchAutoComplete.setListSelection(index); |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
private Intent createIntent(String action, Uri data, String extraData, String query, |
| 603 |
int actionKey, String actionMsg) { |
| 604 |
|
| 605 |
Intent intent = new Intent(action); |
| 606 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
if (data != null) { |
| 611 |
intent.setData(data); |
| 612 |
} |
| 613 |
intent.putExtra(SearchManager.USER_QUERY, mUserQuery); |
| 614 |
if (query != null) { |
| 615 |
intent.putExtra(SearchManager.QUERY, query); |
| 616 |
} |
| 617 |
if (extraData != null) { |
| 618 |
intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData); |
| 619 |
} |
| 620 |
if (mAppSearchData != null) { |
| 621 |
intent.putExtra(SearchManager.APP_DATA, mAppSearchData); |
| 622 |
} |
| 623 |
if (actionKey != KeyEvent.KEYCODE_UNKNOWN) { |
| 624 |
intent.putExtra(SearchManager.ACTION_KEY, actionKey); |
| 625 |
intent.putExtra(SearchManager.ACTION_MSG, actionMsg); |
| 626 |
} |
| 627 |
intent.setComponent(mSearchable.getSearchActivity()); |
| 628 |
return intent; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
public static class SearchBar extends LinearLayout { |
| 636 |
|
| 637 |
public SearchBar(Context context, AttributeSet attrs) { |
| 638 |
super(context, attrs); |
| 639 |
} |
| 640 |
|
| 641 |
public SearchBar(Context context) { |
| 642 |
super(context); |
| 643 |
} |
| 644 |
|
| 645 |
@Override |
| 646 |
public ActionMode startActionModeForChild( |
| 647 |
View child, ActionMode.Callback callback, int type) { |
| 648 |
|
| 649 |
if (type != ActionMode.TYPE_PRIMARY) { |
| 650 |
return super.startActionModeForChild(child, callback, type); |
| 651 |
} |
| 652 |
return null; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
private boolean isEmpty(AutoCompleteTextView actv) { |
| 657 |
return TextUtils.getTrimmedLength(actv.getText()) == 0; |
| 658 |
} |
| 659 |
|
| 660 |
@Override |
| 661 |
public void onBackPressed() { |
| 662 |
|
| 663 |
|
| 664 |
InputMethodManager imm = getContext().getSystemService(InputMethodManager.class); |
| 665 |
if (imm != null && imm.isFullscreenMode() && |
| 666 |
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0)) { |
| 667 |
return; |
| 668 |
} |
| 669 |
|
| 670 |
cancel(); |
| 671 |
} |
| 672 |
|
| 673 |
private boolean onClosePressed() { |
| 674 |
|
| 675 |
if (isEmpty(mSearchAutoComplete)) { |
| 676 |
dismiss(); |
| 677 |
return true; |
| 678 |
} |
| 679 |
|
| 680 |
return false; |
| 681 |
} |
| 682 |
|
| 683 |
private final SearchView.OnCloseListener mOnCloseListener = new SearchView.OnCloseListener() { |
| 684 |
|
| 685 |
public boolean onClose() { |
| 686 |
return onClosePressed(); |
| 687 |
} |
| 688 |
}; |
| 689 |
|
| 690 |
private final SearchView.OnQueryTextListener mOnQueryChangeListener = |
| 691 |
new SearchView.OnQueryTextListener() { |
| 692 |
|
| 693 |
public boolean onQueryTextSubmit(String query) { |
| 694 |
dismiss(); |
| 695 |
return false; |
| 696 |
} |
| 697 |
|
| 698 |
public boolean onQueryTextChange(String newText) { |
| 699 |
return false; |
| 700 |
} |
| 701 |
}; |
| 702 |
|
| 703 |
private final SearchView.OnSuggestionListener mOnSuggestionSelectionListener = |
| 704 |
new SearchView.OnSuggestionListener() { |
| 705 |
|
| 706 |
public boolean onSuggestionSelect(int position) { |
| 707 |
return false; |
| 708 |
} |
| 709 |
|
| 710 |
public boolean onSuggestionClick(int position) { |
| 711 |
dismiss(); |
| 712 |
return false; |
| 713 |
} |
| 714 |
}; |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
private void setUserQuery(String query) { |
| 720 |
if (query == null) { |
| 721 |
query = ""; |
| 722 |
} |
| 723 |
mUserQuery = query; |
| 724 |
mSearchAutoComplete.setText(query); |
| 725 |
mSearchAutoComplete.setSelection(query.length()); |
| 726 |
} |
| 727 |
} |