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.content_shell; |
| 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.inputmethod.EditorInfo; |
| 14 import android.view.inputmethod.InputMethodManager; |
| 15 import android.widget.EditText; |
| 16 import android.widget.FrameLayout; |
| 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.base.CalledByNative; |
| 23 import org.chromium.content.browser.ContentView; |
| 24 |
| 25 /** |
| 26 * Container for the various UI components that make up a shell window. |
| 27 */ |
| 28 public class ShellView extends LinearLayout { |
| 29 |
| 30 private static final long COMPLETED_PROGRESS_TIMEOUT_MS = 200; |
| 31 |
| 32 private int mNativeShellView; |
| 33 private ContentView mContentView; |
| 34 private EditText mUrlTextView; |
| 35 private ImageButton mPrevButton; |
| 36 private ImageButton mNextButton; |
| 37 |
| 38 private ClipDrawable mProgressDrawable; |
| 39 |
| 40 /** |
| 41 * Constructor for inflating via XML. |
| 42 */ |
| 43 public ShellView(Context context, AttributeSet attrs) { |
| 44 super(context, attrs); |
| 45 |
| 46 mNativeShellView = nativeInit(); |
| 47 } |
| 48 |
| 49 @Override |
| 50 protected void onFinishInflate() { |
| 51 super.onFinishInflate(); |
| 52 |
| 53 mProgressDrawable = (ClipDrawable) findViewById(R.id.toolbar).getBackgro
und(); |
| 54 |
| 55 initilizeUrlField(); |
| 56 initializeNavigationButtons(); |
| 57 } |
| 58 |
| 59 /** |
| 60 * @return the native shell view pointer. |
| 61 */ |
| 62 protected int getNativeShellView() { |
| 63 return mNativeShellView; |
| 64 } |
| 65 |
| 66 private void initilizeUrlField() { |
| 67 mUrlTextView = (EditText) findViewById(R.id.url); |
| 68 mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() { |
| 69 @Override |
| 70 public boolean onEditorAction(TextView v, int actionId, KeyEvent eve
nt) { |
| 71 if (actionId != EditorInfo.IME_ACTION_GO) return false; |
| 72 loadUrl(mUrlTextView.getText().toString()); |
| 73 setKeyboardVisibilityForUrl(false); |
| 74 mUrlTextView.clearFocus(); |
| 75 return true; |
| 76 } |
| 77 }); |
| 78 mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() { |
| 79 @Override |
| 80 public void onFocusChange(View v, boolean hasFocus) { |
| 81 setKeyboardVisibilityForUrl(hasFocus); |
| 82 mNextButton.setVisibility(hasFocus ? GONE : VISIBLE); |
| 83 mPrevButton.setVisibility(hasFocus ? GONE : VISIBLE); |
| 84 } |
| 85 }); |
| 86 } |
| 87 |
| 88 /** |
| 89 * Loads an URL. This will perform minimal amounts of sanitizing of the URL
to attempt to |
| 90 * make it valid. |
| 91 * |
| 92 * @param url The URL to be loaded by the shell. |
| 93 */ |
| 94 public void loadUrl(String url) { |
| 95 if (url == null) return; |
| 96 |
| 97 if (TextUtils.equals(url, mContentView.getUrl())) { |
| 98 mContentView.reload(); |
| 99 } else { |
| 100 mContentView.loadUrlWithoutUrlSanitization(sanitizeUrl(url)); |
| 101 } |
| 102 mUrlTextView.clearFocus(); |
| 103 } |
| 104 |
| 105 /** |
| 106 * Given an URL, this performs minimal sanitizing to ensure it will be valid
. |
| 107 * @param url The url to be sanitized. |
| 108 * @return The sanitized URL. |
| 109 */ |
| 110 public static String sanitizeUrl(String url) { |
| 111 if (url == null) return url; |
| 112 if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" +
url; |
| 113 return url; |
| 114 } |
| 115 |
| 116 private void initializeNavigationButtons() { |
| 117 mPrevButton = (ImageButton) findViewById(R.id.prev); |
| 118 mPrevButton.setOnClickListener(new OnClickListener() { |
| 119 @Override |
| 120 public void onClick(View v) { |
| 121 if (mContentView.canGoBack()) mContentView.goBack(); |
| 122 } |
| 123 }); |
| 124 |
| 125 mNextButton = (ImageButton) findViewById(R.id.next); |
| 126 mNextButton.setOnClickListener(new OnClickListener() { |
| 127 @Override |
| 128 public void onClick(View v) { |
| 129 if (mContentView.canGoForward()) mContentView.goForward(); |
| 130 } |
| 131 }); |
| 132 } |
| 133 |
| 134 /** |
| 135 * Initializes the ContentView based on the native tab contents pointer pass
ed in. |
| 136 * @param nativeTabContents The pointer to the native tab contents object. |
| 137 */ |
| 138 @SuppressWarnings("unused") |
| 139 @CalledByNative |
| 140 private void initFromNativeTabContents(int nativeTabContents) { |
| 141 // TODO(tedchoc): Pass along native tab contents. |
| 142 mContentView = new ContentView(getContext()); |
| 143 ((FrameLayout) findViewById(R.id.contentview_holder)).addView(mContentVi
ew, |
| 144 new FrameLayout.LayoutParams( |
| 145 FrameLayout.LayoutParams.MATCH_PARENT, |
| 146 FrameLayout.LayoutParams.MATCH_PARENT)); |
| 147 } |
| 148 |
| 149 /** |
| 150 * @return The {@link ContentView} currently shown by this Shell. |
| 151 */ |
| 152 public ContentView getContentView() { |
| 153 return mContentView; |
| 154 } |
| 155 |
| 156 private void setKeyboardVisibilityForUrl(boolean visible) { |
| 157 InputMethodManager imm = (InputMethodManager) getContext().getSystemServ
ice( |
| 158 Context.INPUT_METHOD_SERVICE); |
| 159 if (visible) { |
| 160 imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT); |
| 161 } else { |
| 162 imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0); |
| 163 } |
| 164 } |
| 165 |
| 166 private native int nativeInit(); |
| 167 } |
OLD | NEW |