Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.testshell; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.graphics.drawable.ClipDrawable; | |
| 9 import android.text.TextUtils; | |
| 10 import android.util.AttributeSet; | |
| 11 import android.view.KeyEvent; | |
| 12 import android.view.View; | |
| 13 import android.view.View.OnClickListener; | |
| 14 import android.view.inputmethod.EditorInfo; | |
| 15 import android.view.inputmethod.InputMethodManager; | |
| 16 import android.widget.EditText; | |
| 17 import android.widget.ImageButton; | |
| 18 import android.widget.LinearLayout; | |
| 19 import android.widget.TextView; | |
| 20 import android.widget.TextView.OnEditorActionListener; | |
| 21 | |
| 22 import org.chromium.chrome.browser.TabBase; | |
| 23 import org.chromium.content.browser.LoadUrlParams; | |
| 24 | |
| 25 /** | |
| 26 * A Toolbar {@link View} that shows the URL and navigation buttons. | |
| 27 */ | |
| 28 public class TestShellToolbar extends LinearLayout { | |
| 29 private static final long COMPLETED_PROGRESS_TIMEOUT_MS = 200; | |
| 30 | |
| 31 private Runnable mClearProgressRunnable = new Runnable() { | |
| 32 @Override | |
| 33 public void run() { | |
| 34 mProgressDrawable.setLevel(0); | |
| 35 } | |
| 36 }; | |
| 37 | |
| 38 private EditText mUrlTextView; | |
| 39 private ImageButton mPrevButton; | |
| 40 private ImageButton mNextButton; | |
| 41 | |
| 42 private ClipDrawable mProgressDrawable; | |
| 43 | |
| 44 private TabBase mTab; | |
| 45 | |
| 46 /** | |
| 47 * @param context The Context the view is running in. | |
| 48 * @param attrs The attributes of the XML tag that is inflating the view. | |
| 49 */ | |
| 50 public TestShellToolbar(Context context, AttributeSet attrs) { | |
| 51 super(context, attrs); | |
| 52 } | |
| 53 | |
| 54 /** | |
| 55 * The toolbar will visually represent the state of {@code tab}. | |
| 56 * @param tab The TabBase that should be represented. | |
| 57 */ | |
| 58 public void showTab(TabBase tab) { | |
| 59 mTab = tab; | |
| 60 mUrlTextView.setText(mTab.getContentView().getUrl()); | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * To be called when the URL of the represented {@link TabBase} updates. | |
| 65 * | |
| 66 * @param url The new URL of the currently represented {@link TabBase}. | |
| 67 */ | |
| 68 public void onUpdateUrl(String url) { | |
| 69 mUrlTextView.setText(url); | |
| 70 } | |
| 71 | |
| 72 /** | |
| 73 * To be called when the load progress of the represented {@link TabBase} up dates. | |
| 74 * | |
| 75 * @param progress The current load progress. Should be a number between 0 and 1. | |
| 76 */ | |
| 77 public void onLoadProgressChanged(double progress) { | |
| 78 removeCallbacks(mClearProgressRunnable); | |
| 79 mProgressDrawable.setLevel((int) (10000.0 * progress)); | |
| 80 if (progress == 1.0) postDelayed(mClearProgressRunnable, COMPLETED_PROGR ESS_TIMEOUT_MS); | |
|
nilesh
2012/09/24 20:46:41
Is progress capped at 1.0?
Can we just change the
David Trainor- moved to gerrit
2012/09/24 23:39:57
I'll use 0 - 100. I don't want to expose 10,000 b
| |
| 81 } | |
| 82 | |
| 83 @Override | |
| 84 protected void onFinishInflate() { | |
| 85 super.onFinishInflate(); | |
| 86 | |
| 87 mProgressDrawable = (ClipDrawable) findViewById(R.id.toolbar).getBackgro und(); | |
| 88 initializeUrlField(); | |
| 89 initializeNavigationButtons(); | |
| 90 } | |
| 91 | |
| 92 private void initializeUrlField() { | |
| 93 mUrlTextView = (EditText) findViewById(R.id.url); | |
| 94 mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() { | |
| 95 @Override | |
| 96 public boolean onEditorAction(TextView v, int actionId, KeyEvent eve nt) { | |
| 97 if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null || | |
| 98 event.getKeyCode() != KeyEvent.KEYCODE_ENTER || | |
| 99 event.getKeyCode() != KeyEvent.ACTION_UP)) { | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 mTab.loadUrlWithSanitization(mUrlTextView.getText().toString()); | |
| 104 mUrlTextView.clearFocus(); | |
| 105 setKeyboardVisibilityForUrl(false); | |
| 106 mTab.getContentView().requestFocus(); | |
| 107 return true; | |
| 108 } | |
| 109 }); | |
| 110 mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() { | |
| 111 @Override | |
| 112 public void onFocusChange(View v, boolean hasFocus) { | |
| 113 setKeyboardVisibilityForUrl(hasFocus); | |
| 114 mNextButton.setVisibility(hasFocus ? GONE : VISIBLE); | |
| 115 mPrevButton.setVisibility(hasFocus ? GONE : VISIBLE); | |
| 116 if (!hasFocus) { | |
| 117 mUrlTextView.setText(mTab.getContentView().getUrl()); | |
| 118 } | |
| 119 } | |
| 120 }); | |
| 121 } | |
| 122 | |
| 123 private void initializeNavigationButtons() { | |
| 124 mPrevButton = (ImageButton) findViewById(R.id.prev); | |
| 125 mPrevButton.setOnClickListener(new OnClickListener() { | |
| 126 @Override | |
| 127 public void onClick(View arg0) { | |
| 128 if (mTab.getContentView().canGoBack()) mTab.getContentView().goB ack(); | |
| 129 } | |
| 130 }); | |
| 131 | |
| 132 mNextButton = (ImageButton) findViewById(R.id.next); | |
| 133 mNextButton.setOnClickListener(new OnClickListener() { | |
| 134 @Override | |
| 135 public void onClick(View v) { | |
| 136 if (mTab.getContentView().canGoForward()) mTab.getContentView(). goForward(); | |
| 137 } | |
| 138 }); | |
| 139 } | |
| 140 | |
| 141 private void setKeyboardVisibilityForUrl(boolean visible) { | |
| 142 InputMethodManager imm = (InputMethodManager) getContext().getSystemServ ice( | |
| 143 Context.INPUT_METHOD_SERVICE); | |
| 144 if (visible) { | |
| 145 imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT); | |
| 146 } else { | |
| 147 imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0); | |
| 148 } | |
| 149 } | |
| 150 } | |
| OLD | NEW |