| 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.base.JNINamespace; | |
| 24 import org.chromium.content.browser.ContentView; | |
| 25 import org.chromium.content.browser.ContentViewClient; | |
| 26 | |
| 27 /** | |
| 28 * Container for the various UI components that make up a shell window. | |
| 29 */ | |
| 30 @JNINamespace("content") | |
| 31 public class ShellView extends LinearLayout { | |
| 32 | |
| 33 private static final long COMPLETED_PROGRESS_TIMEOUT_MS = 200; | |
| 34 | |
| 35 private int mNativeShellView; | |
| 36 // TODO(jrg): a mContentView.destroy() call is needed, both upstream and dow
nstream. | |
| 37 private ContentView mContentView; | |
| 38 private EditText mUrlTextView; | |
| 39 private ImageButton mPrevButton; | |
| 40 private ImageButton mNextButton; | |
| 41 | |
| 42 private ClipDrawable mProgressDrawable; | |
| 43 | |
| 44 /** | |
| 45 * Constructor for inflating via XML. | |
| 46 */ | |
| 47 public ShellView(Context context, AttributeSet attrs) { | |
| 48 super(context, attrs); | |
| 49 | |
| 50 mNativeShellView = nativeInit(); | |
| 51 } | |
| 52 | |
| 53 @Override | |
| 54 protected void onFinishInflate() { | |
| 55 super.onFinishInflate(); | |
| 56 | |
| 57 mProgressDrawable = (ClipDrawable) findViewById(R.id.toolbar).getBackgro
und(); | |
| 58 | |
| 59 initilizeUrlField(); | |
| 60 initializeNavigationButtons(); | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * @return the native shell view pointer. | |
| 65 */ | |
| 66 protected int getNativeShellView() { | |
| 67 return mNativeShellView; | |
| 68 } | |
| 69 | |
| 70 private void initilizeUrlField() { | |
| 71 mUrlTextView = (EditText) findViewById(R.id.url); | |
| 72 mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() { | |
| 73 @Override | |
| 74 public boolean onEditorAction(TextView v, int actionId, KeyEvent eve
nt) { | |
| 75 if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null || | |
| 76 event.getKeyCode() != KeyEvent.KEYCODE_ENTER || | |
| 77 event.getAction() != KeyEvent.ACTION_UP)) { | |
| 78 return false; | |
| 79 } | |
| 80 loadUrl(mUrlTextView.getText().toString()); | |
| 81 setKeyboardVisibilityForUrl(false); | |
| 82 mContentView.requestFocus(); | |
| 83 return true; | |
| 84 } | |
| 85 }); | |
| 86 mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() { | |
| 87 @Override | |
| 88 public void onFocusChange(View v, boolean hasFocus) { | |
| 89 setKeyboardVisibilityForUrl(hasFocus); | |
| 90 mNextButton.setVisibility(hasFocus ? GONE : VISIBLE); | |
| 91 mPrevButton.setVisibility(hasFocus ? GONE : VISIBLE); | |
| 92 if (!hasFocus) { | |
| 93 mUrlTextView.setText(mContentView.getUrl()); | |
| 94 } | |
| 95 } | |
| 96 }); | |
| 97 } | |
| 98 | |
| 99 /** | |
| 100 * Loads an URL. This will perform minimal amounts of sanitizing of the URL
to attempt to | |
| 101 * make it valid. | |
| 102 * | |
| 103 * @param url The URL to be loaded by the shell. | |
| 104 */ | |
| 105 public void loadUrl(String url) { | |
| 106 if (url == null) return; | |
| 107 | |
| 108 if (TextUtils.equals(url, mContentView.getUrl())) { | |
| 109 mContentView.reload(); | |
| 110 } else { | |
| 111 mContentView.loadUrlWithoutUrlSanitization(sanitizeUrl(url)); | |
| 112 } | |
| 113 mUrlTextView.clearFocus(); | |
| 114 } | |
| 115 | |
| 116 /** | |
| 117 * Given an URL, this performs minimal sanitizing to ensure it will be valid
. | |
| 118 * @param url The url to be sanitized. | |
| 119 * @return The sanitized URL. | |
| 120 */ | |
| 121 public static String sanitizeUrl(String url) { | |
| 122 if (url == null) return url; | |
| 123 if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" +
url; | |
| 124 return url; | |
| 125 } | |
| 126 | |
| 127 private void initializeNavigationButtons() { | |
| 128 mPrevButton = (ImageButton) findViewById(R.id.prev); | |
| 129 mPrevButton.setOnClickListener(new OnClickListener() { | |
| 130 @Override | |
| 131 public void onClick(View v) { | |
| 132 if (mContentView.canGoBack()) mContentView.goBack(); | |
| 133 } | |
| 134 }); | |
| 135 | |
| 136 mNextButton = (ImageButton) findViewById(R.id.next); | |
| 137 mNextButton.setOnClickListener(new OnClickListener() { | |
| 138 @Override | |
| 139 public void onClick(View v) { | |
| 140 if (mContentView.canGoForward()) mContentView.goForward(); | |
| 141 } | |
| 142 }); | |
| 143 } | |
| 144 | |
| 145 /** | |
| 146 * Initializes the ContentView based on the native tab contents pointer pass
ed in. | |
| 147 * @param nativeTabContents The pointer to the native tab contents object. | |
| 148 */ | |
| 149 @SuppressWarnings("unused") | |
| 150 @CalledByNative | |
| 151 private void initFromNativeTabContents(int nativeTabContents) { | |
| 152 mContentView = new ContentView( | |
| 153 getContext(), nativeTabContents, ContentView.PERSONALITY_CHROME)
; | |
| 154 mContentView.setContentViewClient(new ShellContentViewClient()); | |
| 155 ((FrameLayout) findViewById(R.id.contentview_holder)).addView(mContentVi
ew, | |
| 156 new FrameLayout.LayoutParams( | |
| 157 FrameLayout.LayoutParams.MATCH_PARENT, | |
| 158 FrameLayout.LayoutParams.MATCH_PARENT)); | |
| 159 mContentView.requestFocus(); | |
| 160 } | |
| 161 | |
| 162 /** | |
| 163 * @return The {@link ContentView} currently shown by this Shell. | |
| 164 */ | |
| 165 public ContentView getContentView() { | |
| 166 return mContentView; | |
| 167 } | |
| 168 | |
| 169 private void setKeyboardVisibilityForUrl(boolean visible) { | |
| 170 InputMethodManager imm = (InputMethodManager) getContext().getSystemServ
ice( | |
| 171 Context.INPUT_METHOD_SERVICE); | |
| 172 if (visible) { | |
| 173 imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT); | |
| 174 } else { | |
| 175 imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0); | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 private native int nativeInit(); | |
| 180 | |
| 181 private class ShellContentViewClient extends ContentViewClient { | |
| 182 private Runnable mClearProgressRunnable = new Runnable() { | |
| 183 @Override | |
| 184 public void run() { | |
| 185 mProgressDrawable.setLevel(0); | |
| 186 } | |
| 187 }; | |
| 188 | |
| 189 @Override | |
| 190 public void onUpdateUrl(String url) { | |
| 191 super.onUpdateUrl(url); | |
| 192 mUrlTextView.setText(url); | |
| 193 } | |
| 194 | |
| 195 @Override | |
| 196 public void onLoadProgressChanged(final double progress) { | |
| 197 super.onLoadProgressChanged(progress); | |
| 198 removeCallbacks(mClearProgressRunnable); | |
| 199 mProgressDrawable.setLevel((int) (10000.0 * progress)); | |
| 200 if (progress == 1.0) postDelayed(mClearProgressRunnable, COMPLETED_P
ROGRESS_TIMEOUT_MS); | |
| 201 } | |
| 202 | |
| 203 @Override | |
| 204 public void onTabCrash(int pid) { | |
| 205 super.onTabCrash(pid); | |
| 206 mProgressDrawable.setLevel(0); | |
| 207 } | |
| 208 } | |
| 209 } | |
| OLD | NEW |