Commit 9d85816a authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[WebLayer] Add UrlBarController, createUrlBarView.

Create the UrlBarController interface and a simple version of the method
createUrlBarView() without UrlOptions.

Use it in WebLayerShellActivity to show a URL bar.

UrlBarController updates url text whenever the visible URL changes. This
means that the embedder doesn't have to listen to onVisibleUriChanged and
create a new URL Bar view.

Bug: 1025607, 1044571
Change-Id: I83bff6f82354ab970dbd84d3d24e5fac2cf66db7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2017408
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Reviewed-by: default avatarTobias Sargeant <tobiasjs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#737442}
parent 2bc068e4
...@@ -297,6 +297,13 @@ void BrowserImpl::RestoreStateIfNecessary( ...@@ -297,6 +297,13 @@ void BrowserImpl::RestoreStateIfNecessary(
} }
} }
void BrowserImpl::VisibleSecurityStateOfActiveTabChanged() {
#if defined(OS_ANDROID)
JNIEnv* env = base::android::AttachCurrentThread();
Java_BrowserImpl_onVisibleSecurityStateOfActiveTabChanged(env, java_impl_);
#endif
}
base::FilePath BrowserImpl::GetSessionServiceDataPath() { base::FilePath BrowserImpl::GetSessionServiceDataPath() {
base::FilePath base_path; base::FilePath base_path;
if (profile_->GetBrowserContext()->IsOffTheRecord()) { if (profile_->GetBrowserContext()->IsOffTheRecord()) {
......
...@@ -98,6 +98,7 @@ class BrowserImpl : public Browser { ...@@ -98,6 +98,7 @@ class BrowserImpl : public Browser {
std::vector<uint8_t> GetMinimalPersistenceState() override; std::vector<uint8_t> GetMinimalPersistenceState() override;
void AddObserver(BrowserObserver* observer) override; void AddObserver(BrowserObserver* observer) override;
void RemoveObserver(BrowserObserver* observer) override; void RemoveObserver(BrowserObserver* observer) override;
void VisibleSecurityStateOfActiveTabChanged() override;
private: private:
// For creation. // For creation.
......
...@@ -51,6 +51,7 @@ android_library("java") { ...@@ -51,6 +51,7 @@ android_library("java") {
"org/chromium/weblayer_private/TabCallbackProxy.java", "org/chromium/weblayer_private/TabCallbackProxy.java",
"org/chromium/weblayer_private/TabImpl.java", "org/chromium/weblayer_private/TabImpl.java",
"org/chromium/weblayer_private/TopControlsContainerView.java", "org/chromium/weblayer_private/TopControlsContainerView.java",
"org/chromium/weblayer_private/UrlBarControllerImpl.java",
"org/chromium/weblayer_private/WebContentsGestureStateTracker.java", "org/chromium/weblayer_private/WebContentsGestureStateTracker.java",
"org/chromium/weblayer_private/WebLayerFactoryImpl.java", "org/chromium/weblayer_private/WebLayerFactoryImpl.java",
"org/chromium/weblayer_private/WebLayerImpl.java", "org/chromium/weblayer_private/WebLayerImpl.java",
...@@ -203,6 +204,7 @@ android_aidl("aidl") { ...@@ -203,6 +204,7 @@ android_aidl("aidl") {
"org/chromium/weblayer_private/interfaces/IRemoteFragmentClient.aidl", "org/chromium/weblayer_private/interfaces/IRemoteFragmentClient.aidl",
"org/chromium/weblayer_private/interfaces/ITab.aidl", "org/chromium/weblayer_private/interfaces/ITab.aidl",
"org/chromium/weblayer_private/interfaces/ITabClient.aidl", "org/chromium/weblayer_private/interfaces/ITabClient.aidl",
"org/chromium/weblayer_private/interfaces/IUrlBarController.aidl",
"org/chromium/weblayer_private/interfaces/IWebLayer.aidl", "org/chromium/weblayer_private/interfaces/IWebLayer.aidl",
"org/chromium/weblayer_private/interfaces/IWebLayerFactory.aidl", "org/chromium/weblayer_private/interfaces/IWebLayerFactory.aidl",
] ]
......
...@@ -23,6 +23,7 @@ import org.chromium.weblayer_private.interfaces.IBrowserClient; ...@@ -23,6 +23,7 @@ import org.chromium.weblayer_private.interfaces.IBrowserClient;
import org.chromium.weblayer_private.interfaces.IObjectWrapper; import org.chromium.weblayer_private.interfaces.IObjectWrapper;
import org.chromium.weblayer_private.interfaces.IProfile; import org.chromium.weblayer_private.interfaces.IProfile;
import org.chromium.weblayer_private.interfaces.ITab; import org.chromium.weblayer_private.interfaces.ITab;
import org.chromium.weblayer_private.interfaces.IUrlBarController;
import org.chromium.weblayer_private.interfaces.ObjectWrapper; import org.chromium.weblayer_private.interfaces.ObjectWrapper;
import org.chromium.weblayer_private.interfaces.StrictModeWorkaround; import org.chromium.weblayer_private.interfaces.StrictModeWorkaround;
...@@ -35,6 +36,8 @@ import java.util.List; ...@@ -35,6 +36,8 @@ import java.util.List;
@JNINamespace("weblayer") @JNINamespace("weblayer")
public class BrowserImpl extends IBrowser.Stub { public class BrowserImpl extends IBrowser.Stub {
private static final ObserverList<Observer> sLifecycleObservers = new ObserverList<Observer>(); private static final ObserverList<Observer> sLifecycleObservers = new ObserverList<Observer>();
private final ObserverList<VisibleSecurityStateObserver> mVisibleSecurityStateObservers =
new ObserverList<VisibleSecurityStateObserver>();
// Key used to save the crypto key in instance state. // Key used to save the crypto key in instance state.
public static final String SAVED_STATE_SESSION_SERVICE_CRYPTO_KEY = public static final String SAVED_STATE_SESSION_SERVICE_CRYPTO_KEY =
...@@ -52,6 +55,7 @@ public class BrowserImpl extends IBrowser.Stub { ...@@ -52,6 +55,7 @@ public class BrowserImpl extends IBrowser.Stub {
private IBrowserClient mClient; private IBrowserClient mClient;
private LocaleChangedBroadcastReceiver mLocaleReceiver; private LocaleChangedBroadcastReceiver mLocaleReceiver;
private boolean mInDestroy; private boolean mInDestroy;
private final UrlBarControllerImpl mUrlBarController;
// Created in the constructor from saved state and used in setClient(). // Created in the constructor from saved state and used in setClient().
private PersistenceInfo mPersistenceInfo; private PersistenceInfo mPersistenceInfo;
...@@ -80,6 +84,19 @@ public class BrowserImpl extends IBrowser.Stub { ...@@ -80,6 +84,19 @@ public class BrowserImpl extends IBrowser.Stub {
sLifecycleObservers.removeObserver(observer); sLifecycleObservers.removeObserver(observer);
} }
/**
* Allows observing of visible security state of the active tab.
*/
public static interface VisibleSecurityStateObserver {
public void onVisibleSecurityStateOfActiveTabChanged();
}
public void addVisibleSecurityStateObserver(VisibleSecurityStateObserver observer) {
mVisibleSecurityStateObservers.addObserver(observer);
}
public void removeVisibleSecurityStateObserver(VisibleSecurityStateObserver observer) {
mVisibleSecurityStateObservers.removeObserver(observer);
}
public BrowserImpl(ProfileImpl profile, String persistenceId, Bundle savedInstanceState, public BrowserImpl(ProfileImpl profile, String persistenceId, Bundle savedInstanceState,
Context context, FragmentWindowAndroid windowAndroid) { Context context, FragmentWindowAndroid windowAndroid) {
mProfile = profile; mProfile = profile;
...@@ -95,8 +112,8 @@ public class BrowserImpl extends IBrowser.Stub { ...@@ -95,8 +112,8 @@ public class BrowserImpl extends IBrowser.Stub {
: null; : null;
createAttachmentState(context, windowAndroid); createAttachmentState(context, windowAndroid);
mNativeBrowser = BrowserImplJni.get().createBrowser(profile.getNativeProfile(), this); mNativeBrowser = BrowserImplJni.get().createBrowser(profile.getNativeProfile(), this);
mUrlBarController = new UrlBarControllerImpl(this);
for (Observer observer : sLifecycleObservers) { for (Observer observer : sLifecycleObservers) {
observer.onBrowserCreated(); observer.onBrowserCreated();
...@@ -242,6 +259,13 @@ public class BrowserImpl extends IBrowser.Stub { ...@@ -242,6 +259,13 @@ public class BrowserImpl extends IBrowser.Stub {
// destroyed, or switching to a different fragment. // destroyed, or switching to a different fragment.
} }
@CalledByNative
private void onVisibleSecurityStateOfActiveTabChanged() {
for (VisibleSecurityStateObserver observer : mVisibleSecurityStateObservers) {
observer.onVisibleSecurityStateOfActiveTabChanged();
}
}
@Override @Override
public boolean setActiveTab(ITab controller) { public boolean setActiveTab(ITab controller) {
StrictModeWorkaround.apply(); StrictModeWorkaround.apply();
...@@ -307,6 +331,12 @@ public class BrowserImpl extends IBrowser.Stub { ...@@ -307,6 +331,12 @@ public class BrowserImpl extends IBrowser.Stub {
tab.destroy(); tab.destroy();
} }
@Override
public IUrlBarController getUrlBarController() {
StrictModeWorkaround.apply();
return mUrlBarController;
}
public View getFragmentView() { public View getFragmentView() {
return getViewController().getView(); return getViewController().getView();
} }
...@@ -323,6 +353,7 @@ public class BrowserImpl extends IBrowser.Stub { ...@@ -323,6 +353,7 @@ public class BrowserImpl extends IBrowser.Stub {
observer.onBrowserDestroyed(); observer.onBrowserDestroyed();
} }
BrowserImplJni.get().deleteBrowser(mNativeBrowser); BrowserImplJni.get().deleteBrowser(mNativeBrowser);
mUrlBarController.destroy();
} }
private void destroyAttachmentState() { private void destroyAttachmentState() {
...@@ -338,6 +369,8 @@ public class BrowserImpl extends IBrowser.Stub { ...@@ -338,6 +369,8 @@ public class BrowserImpl extends IBrowser.Stub {
mWindowAndroid.destroy(); mWindowAndroid.destroy();
mWindowAndroid = null; mWindowAndroid = null;
} }
mVisibleSecurityStateObservers.clear();
} }
private void updateAllTabsAndSetActive() { private void updateAllTabsAndSetActive() {
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.weblayer_private;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.widget.TextView;
import org.chromium.weblayer_private.interfaces.IObjectWrapper;
import org.chromium.weblayer_private.interfaces.IUrlBarController;
import org.chromium.weblayer_private.interfaces.ObjectWrapper;
import org.chromium.weblayer_private.interfaces.StrictModeWorkaround;
/**
* Implementation of {@link IUrlBarController}.
*/
public class UrlBarControllerImpl extends IUrlBarController.Stub {
// To be kept in sync with the constants in UrlBarOptions.java
public static final String URL_TEXT_SIZE = "UrlTextSize";
public static final float DEFAULT_TEXT_SIZE = 10.0F;
public static final float MINIMUM_TEXT_SIZE = 5.0F;
private BrowserImpl mBrowserImpl;
public UrlBarControllerImpl(BrowserImpl browserImpl) {
mBrowserImpl = browserImpl;
}
void destroy() {
mBrowserImpl = null;
}
@Override
public IObjectWrapper /* View */ createUrlBarView(Bundle options) {
StrictModeWorkaround.apply();
if (mBrowserImpl == null) {
throw new IllegalStateException("UrlBarView cannot be created without a valid Browser");
}
Context context = mBrowserImpl.getContext();
if (context == null) throw new IllegalStateException("BrowserFragment not attached yet.");
UrlBarView urlBarView = new UrlBarView(context, options);
return ObjectWrapper.wrap(urlBarView);
}
private class UrlBarView extends TextView implements BrowserImpl.VisibleSecurityStateObserver {
private float mTextSize;
public UrlBarView(@NonNull Context context, Bundle options) {
super(context);
updateView();
mTextSize = options.getFloat(URL_TEXT_SIZE, DEFAULT_TEXT_SIZE);
setTextSize(TypedValue.COMPLEX_UNIT_SP, Math.max(MINIMUM_TEXT_SIZE, mTextSize));
}
// BrowserImpl.VisibleSecurityStateObserver
@Override
public void onVisibleSecurityStateOfActiveTabChanged() {
updateView();
}
@Override
protected void onAttachedToWindow() {
if (mBrowserImpl != null) {
mBrowserImpl.addVisibleSecurityStateObserver(this);
updateView();
}
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
if (mBrowserImpl != null) mBrowserImpl.removeVisibleSecurityStateObserver(this);
super.onDetachedFromWindow();
}
private void updateView() {
// TODO(crbug.com/1025607): Add a way to get a formatted URL based
// on mOptions.
if (mBrowserImpl == null) return;
setText(mBrowserImpl.getActiveTab().getWebContents().getVisibleUrl());
}
}
}
\ No newline at end of file
...@@ -6,8 +6,9 @@ package org.chromium.weblayer_private.interfaces; ...@@ -6,8 +6,9 @@ package org.chromium.weblayer_private.interfaces;
import org.chromium.weblayer_private.interfaces.IBrowserClient; import org.chromium.weblayer_private.interfaces.IBrowserClient;
import org.chromium.weblayer_private.interfaces.IObjectWrapper; import org.chromium.weblayer_private.interfaces.IObjectWrapper;
import org.chromium.weblayer_private.interfaces.ITab;
import org.chromium.weblayer_private.interfaces.IProfile; import org.chromium.weblayer_private.interfaces.IProfile;
import org.chromium.weblayer_private.interfaces.ITab;
import org.chromium.weblayer_private.interfaces.IUrlBarController;
import java.util.List; import java.util.List;
...@@ -30,4 +31,5 @@ interface IBrowser { ...@@ -30,4 +31,5 @@ interface IBrowser {
void addTab(in ITab tab) = 7; void addTab(in ITab tab) = 7;
void destroyTab(in ITab tab) = 8; void destroyTab(in ITab tab) = 8;
IUrlBarController getUrlBarController() = 9;
} }
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.weblayer_private.interfaces;
import org.chromium.weblayer_private.interfaces.IObjectWrapper;
interface IUrlBarController {
IObjectWrapper /* View */ createUrlBarView(in Bundle options) = 0;
}
...@@ -568,6 +568,13 @@ void TabImpl::OnFindResultAvailable(content::WebContents* web_contents) { ...@@ -568,6 +568,13 @@ void TabImpl::OnFindResultAvailable(content::WebContents* web_contents) {
#endif #endif
} }
void TabImpl::DidChangeVisibleSecurityState() {
if (browser_) {
if (browser_->GetActiveTab() == this)
browser_->VisibleSecurityStateOfActiveTabChanged();
}
}
void TabImpl::OnExitFullscreen() { void TabImpl::OnExitFullscreen() {
// If |processing_enter_fullscreen_| is true, it means the callback is being // If |processing_enter_fullscreen_| is true, it means the callback is being
// called while processing EnterFullscreenModeForTab(). WebContents doesn't // called while processing EnterFullscreenModeForTab(). WebContents doesn't
......
...@@ -188,6 +188,7 @@ class TabImpl : public Tab, ...@@ -188,6 +188,7 @@ class TabImpl : public Tab,
void DidFinishNavigation( void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override; content::NavigationHandle* navigation_handle) override;
void RenderProcessGone(base::TerminationStatus status) override; void RenderProcessGone(base::TerminationStatus status) override;
void DidChangeVisibleSecurityState() override;
// find_in_page::FindResultObserver: // find_in_page::FindResultObserver:
void OnFindResultAvailable(content::WebContents* web_contents) override; void OnFindResultAvailable(content::WebContents* web_contents) override;
......
...@@ -66,6 +66,8 @@ class Browser { ...@@ -66,6 +66,8 @@ class Browser {
virtual void AddObserver(BrowserObserver* observer) = 0; virtual void AddObserver(BrowserObserver* observer) = 0;
virtual void RemoveObserver(BrowserObserver* observer) = 0; virtual void RemoveObserver(BrowserObserver* observer) = 0;
virtual void VisibleSecurityStateOfActiveTabChanged() = 0;
}; };
} // namespace weblayer } // namespace weblayer
......
...@@ -55,6 +55,8 @@ android_library("java") { ...@@ -55,6 +55,8 @@ android_library("java") {
"org/chromium/weblayer/TabListCallback.java", "org/chromium/weblayer/TabListCallback.java",
"org/chromium/weblayer/ThreadCheck.java", "org/chromium/weblayer/ThreadCheck.java",
"org/chromium/weblayer/UnsupportedVersionException.java", "org/chromium/weblayer/UnsupportedVersionException.java",
"org/chromium/weblayer/UrlBarController.java",
"org/chromium/weblayer/UrlBarOptions.java",
"org/chromium/weblayer/WebLayer.java", "org/chromium/weblayer/WebLayer.java",
"org/chromium/weblayer/WebLayerFileProvider.java", "org/chromium/weblayer/WebLayerFileProvider.java",
_version_constants_java_file, _version_constants_java_file,
......
...@@ -30,12 +30,19 @@ import java.util.List; ...@@ -30,12 +30,19 @@ import java.util.List;
public final class Browser { public final class Browser {
private final IBrowser mImpl; private final IBrowser mImpl;
private final ObserverList<TabListCallback> mTabListCallbacks; private final ObserverList<TabListCallback> mTabListCallbacks;
private final UrlBarController mUrlBarController;
Browser(IBrowser impl) { Browser(IBrowser impl) {
mImpl = impl; mImpl = impl;
mTabListCallbacks = new ObserverList<TabListCallback>(); mTabListCallbacks = new ObserverList<TabListCallback>();
try { try {
mImpl.setClient(new BrowserClientImpl()); mImpl.setClient(new BrowserClientImpl());
if (WebLayer.getSupportedMajorVersionInternal() >= 81) {
mUrlBarController = new UrlBarController(mImpl.getUrlBarController());
} else {
mUrlBarController = null;
}
} catch (RemoteException e) { } catch (RemoteException e) {
throw new APICallException(e); throw new APICallException(e);
} }
...@@ -219,6 +226,19 @@ public final class Browser { ...@@ -219,6 +226,19 @@ public final class Browser {
} }
} }
/**
* Returns the UrlBarController.
* @since 81
*/
@NonNull
public UrlBarController getUrlBarController() {
ThreadCheck.ensureOnUiThread();
if (WebLayer.getSupportedMajorVersionInternal() < 81) {
throw new UnsupportedOperationException();
}
return mUrlBarController;
}
private final class BrowserClientImpl extends IBrowserClient.Stub { private final class BrowserClientImpl extends IBrowserClient.Stub {
@Override @Override
public void onActiveTabChanged(int activeTabId) { public void onActiveTabChanged(int activeTabId) {
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.weblayer;
import android.os.RemoteException;
import android.view.View;
import org.chromium.weblayer_private.interfaces.APICallException;
import org.chromium.weblayer_private.interfaces.IUrlBarController;
import org.chromium.weblayer_private.interfaces.ObjectWrapper;
/**
* UrlBarController enables creation of URL bar views and retrieval of information about them.
*/
public final class UrlBarController {
private final IUrlBarController mImpl;
UrlBarController(IUrlBarController urlBarController) {
mImpl = urlBarController;
}
/**
* Creates a URL bar view based on the options provided.
* @param options The options provided to tweak the URL bar display.
* @since 81
*/
public View createUrlBarView(UrlBarOptions options) {
ThreadCheck.ensureOnUiThread();
if (WebLayer.getSupportedMajorVersionInternal() < 81) {
throw new UnsupportedOperationException();
}
try {
return ObjectWrapper.unwrap(mImpl.createUrlBarView(options.getBundle()), View.class);
} catch (RemoteException exception) {
throw new APICallException(exception);
}
}
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.weblayer;
import android.os.Bundle;
/**
* Class containing options to tweak the URL bar.
*/
public final class UrlBarOptions {
// Keep in sync with the constants in UrlBarControllerImpl.java
private static final String URL_TEXT_SIZE = "UrlTextSize";
public static Builder builder() {
return new Builder();
}
private Bundle mOptions;
/**
* A Builder class to help create UrlBarOptions.
*/
public static final class Builder {
private Bundle mOptions;
private Builder() {
mOptions = new Bundle();
}
Bundle getBundle() {
return mOptions;
}
/**
* Sets the text size of the URL bar.
*
* @param textSize The desired size of the URL bar text in scalable pixels.
* The default is 10.0F and the minimum allowed size is 5.0F.
*/
public Builder setTextSizeSP(float textSize) {
mOptions.putFloat(URL_TEXT_SIZE, textSize);
return this;
}
/**
* Builds a UrlBarOptions object.
*/
public UrlBarOptions build() {
return new UrlBarOptions(this);
}
}
private UrlBarOptions(Builder builder) {
mOptions = builder.getBundle();
}
/**
* Gets the URL bar options as a Bundle.
*/
Bundle getBundle() {
return mOptions;
}
/**
* Gets the text size of the URL bar text in scalable pixels.
*/
public float getTextSizeSP() {
return mOptions.getFloat(URL_TEXT_SIZE);
}
}
...@@ -11,24 +11,24 @@ ...@@ -11,24 +11,24 @@
tools:ignore="LabelFor,ContentDescription" tools:ignore="LabelFor,ContentDescription"
android:background="#FFc9c9c9"> android:background="#FFc9c9c9">
<EditText <FrameLayout
android:id="@+id/url_view" android:id="@+id/url_view_container"
android:inputType="textUri" android:layout_width="wrap_content"
android:selectAllOnFocus="true"
android:imeOptions="actionGo"
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_toStartOf="@+id/menu_button" android:layout_marginStart="5dp"
android:layout_alignParentStart="true" /> android:layout_alignParentStart="true"
android:layout_gravity="center_vertical"
android:layout_toStartOf="@+id/menu_button" />
<ImageButton <ImageButton
android:id="@id/menu_button" android:id="@+id/menu_button"
android:src="@android:drawable/ic_menu_more" android:src="@android:drawable/ic_menu_more"
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_alignParentEnd="true" /> android:layout_alignParentEnd="true"
android:layout_alignBottom="@id/url_view_container" />
<ProgressBar <ProgressBar
android:id="@+id/progress_bar" android:id="@+id/progress_bar"
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
android:visibility="invisible" android:visibility="invisible"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignBottom="@id/url_view" android:layout_alignBottom="@id/url_view_container"
android:layout_marginTop="0dp" android:layout_marginTop="0dp"
android:layout_marginBottom="-10px" android:layout_marginBottom="-10px"
android:layout_marginLeft="0dp" android:layout_marginLeft="0dp"
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
package org.chromium.weblayer.shell; package org.chromium.weblayer.shell;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
...@@ -13,22 +12,19 @@ import android.support.v4.app.FragmentActivity; ...@@ -13,22 +12,19 @@ import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup.LayoutParams; import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.ValueCallback; import android.webkit.ValueCallback;
import android.widget.EditText; import android.widget.FrameLayout;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.PopupMenu; import android.widget.PopupMenu;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import org.chromium.weblayer.Browser; import org.chromium.weblayer.Browser;
import org.chromium.weblayer.DownloadCallback; import org.chromium.weblayer.DownloadCallback;
...@@ -42,9 +38,8 @@ import org.chromium.weblayer.NewTabCallback; ...@@ -42,9 +38,8 @@ import org.chromium.weblayer.NewTabCallback;
import org.chromium.weblayer.NewTabType; import org.chromium.weblayer.NewTabType;
import org.chromium.weblayer.Profile; import org.chromium.weblayer.Profile;
import org.chromium.weblayer.Tab; import org.chromium.weblayer.Tab;
import org.chromium.weblayer.TabCallback;
import org.chromium.weblayer.TabListCallback;
import org.chromium.weblayer.UnsupportedVersionException; import org.chromium.weblayer.UnsupportedVersionException;
import org.chromium.weblayer.UrlBarOptions;
import org.chromium.weblayer.WebLayer; import org.chromium.weblayer.WebLayer;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -59,13 +54,12 @@ public class WebLayerShellActivity extends FragmentActivity { ...@@ -59,13 +54,12 @@ public class WebLayerShellActivity extends FragmentActivity {
private Profile mProfile; private Profile mProfile;
private Browser mBrowser; private Browser mBrowser;
private EditText mUrlView;
private ImageButton mMenuButton; private ImageButton mMenuButton;
private FrameLayout mUrlViewContainer;
private ProgressBar mLoadProgressBar; private ProgressBar mLoadProgressBar;
private View mMainView; private View mMainView;
private int mMainViewId; private int mMainViewId;
private View mTopContentsContainer; private View mTopContentsContainer;
private TabListCallback mTabListCallback;
private List<Tab> mPreviousTabList = new ArrayList<>(); private List<Tab> mPreviousTabList = new ArrayList<>();
private Runnable mExitFullscreenRunnable; private Runnable mExitFullscreenRunnable;
...@@ -92,27 +86,7 @@ public class WebLayerShellActivity extends FragmentActivity { ...@@ -92,27 +86,7 @@ public class WebLayerShellActivity extends FragmentActivity {
mTopContentsContainer = mTopContentsContainer =
LayoutInflater.from(this).inflate(R.layout.shell_browser_controls, null); LayoutInflater.from(this).inflate(R.layout.shell_browser_controls, null);
mUrlViewContainer = mTopContentsContainer.findViewById(R.id.url_view_container);
mUrlView = mTopContentsContainer.findViewById(R.id.url_view);
// Make the view transparent. Note that setBackgroundColor() applies a color filter to the
// background drawable rather than replacing it, and thus does not affect layout.
mUrlView.setBackgroundColor(0);
mUrlView.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId != EditorInfo.IME_ACTION_GO)
&& (event == null || event.getKeyCode() != KeyEvent.KEYCODE_ENTER
|| event.getAction() != KeyEvent.ACTION_DOWN)) {
return false;
}
loadUrl(mUrlView.getText().toString());
mUrlView.clearFocus();
InputMethodManager imm =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mUrlView.getWindowToken(), 0);
return true;
}
});
mMenuButton = mTopContentsContainer.findViewById(R.id.menu_button); mMenuButton = mTopContentsContainer.findViewById(R.id.menu_button);
mMenuButton.setOnClickListener(new View.OnClickListener() { mMenuButton.setOnClickListener(new View.OnClickListener() {
...@@ -162,10 +136,7 @@ public class WebLayerShellActivity extends FragmentActivity { ...@@ -162,10 +136,7 @@ public class WebLayerShellActivity extends FragmentActivity {
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
if (mTabListCallback != null) { mUrlViewContainer.removeAllViews();
mBrowser.unregisterTabListCallback(mTabListCallback);
mTabListCallback = null;
}
} }
private void onWebLayerReady(WebLayer webLayer, Bundle savedInstanceState) { private void onWebLayerReady(WebLayer webLayer, Bundle savedInstanceState) {
...@@ -181,35 +152,24 @@ public class WebLayerShellActivity extends FragmentActivity { ...@@ -181,35 +152,24 @@ public class WebLayerShellActivity extends FragmentActivity {
// when the shell is rotated in the foreground). // when the shell is rotated in the foreground).
fragment.setRetainInstance(true); fragment.setRetainInstance(true);
mBrowser = Browser.fromFragment(fragment); mBrowser = Browser.fromFragment(fragment);
mTabListCallback = new TabListCallback() {
@Override
public void onActiveTabChanged(Tab activeTab) {
String currentDisplayUrl = getCurrentDisplayUrl();
if (currentDisplayUrl != null) {
mUrlView.setText(currentDisplayUrl);
}
}
};
mBrowser.registerTabListCallback(mTabListCallback);
setTabCallbacks(mBrowser.getActiveTab(), fragment); setTabCallbacks(mBrowser.getActiveTab(), fragment);
mProfile = mBrowser.getProfile(); mProfile = mBrowser.getProfile();
mBrowser.setTopView(mTopContentsContainer); mBrowser.setTopView(mTopContentsContainer);
UrlBarOptions urlBarOptions = UrlBarOptions.builder().setTextSizeSP(12.0F).build();
View urlView = mBrowser.getUrlBarController().createUrlBarView(urlBarOptions);
// If there is already a url loaded in the current tab just display it in the top bar; mUrlViewContainer.addView(urlView,
// otherwise load the startup url. new RelativeLayout.LayoutParams(
String currentDisplayUrl = getCurrentDisplayUrl(); LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
if (getCurrentDisplayUrl() != null) {
if (currentDisplayUrl != null) { return;
mUrlView.setText(currentDisplayUrl); }
String startupUrl = getUrlFromIntent(getIntent());
} else { if (TextUtils.isEmpty(startupUrl)) {
String startupUrl = getUrlFromIntent(getIntent()); startupUrl = "https://google.com";
if (TextUtils.isEmpty(startupUrl)) {
startupUrl = "https://google.com";
}
loadUrl(startupUrl);
} }
loadUrl(startupUrl);
} }
/* Returns the Url for the current tab as a String, or null if there is no /* Returns the Url for the current tab as a String, or null if there is no
...@@ -278,12 +238,6 @@ public class WebLayerShellActivity extends FragmentActivity { ...@@ -278,12 +238,6 @@ public class WebLayerShellActivity extends FragmentActivity {
} }
} }
}); });
tab.registerTabCallback(new TabCallback() {
@Override
public void onVisibleUriChanged(Uri uri) {
mUrlView.setText(uri.toString());
}
});
tab.getNavigationController().registerNavigationCallback(new NavigationCallback() { tab.getNavigationController().registerNavigationCallback(new NavigationCallback() {
@Override @Override
public void onNavigationStarted(Navigation navigation) { public void onNavigationStarted(Navigation navigation) {
...@@ -358,7 +312,6 @@ public class WebLayerShellActivity extends FragmentActivity { ...@@ -358,7 +312,6 @@ public class WebLayerShellActivity extends FragmentActivity {
public void loadUrl(String url) { public void loadUrl(String url) {
mBrowser.getActiveTab().getNavigationController().navigate(Uri.parse(sanitizeUrl(url))); mBrowser.getActiveTab().getNavigationController().navigate(Uri.parse(sanitizeUrl(url)));
mUrlView.clearFocus();
} }
private static String getUrlFromIntent(Intent intent) { private static String getUrlFromIntent(Intent intent) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment