-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RNMobile] Add embed webview for Android #50440
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e0826c1
Add Gutenberg Embed WebView
jhnstn 8fbeb8c
Clean up
jhnstn 46d29ff
Rename method
jhnstn 19cdfa9
Remove commented out code
jhnstn e1391fe
Removed unused method
jhnstn 78c3d69
Remove unused menu layout
jhnstn 50b6f41
Update load and get title methods
jhnstn f5bd0b5
Merge branch 'trunk' into rnmobile/add-embed-webview
jhnstn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
...n/java/org/wordpress/mobile/ReactNativeGutenbergBridge/GutenbergEmbedWebViewActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package org.wordpress.mobile.ReactNativeGutenbergBridge; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.graphics.Bitmap; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.view.Menu; | ||
import android.view.MenuInflater; | ||
import android.view.MenuItem; | ||
import android.view.View; | ||
import android.webkit.CookieManager; | ||
import android.webkit.WebChromeClient; | ||
import android.webkit.WebSettings; | ||
import android.webkit.WebView; | ||
import android.webkit.WebViewClient; | ||
import android.widget.ProgressBar; | ||
|
||
import androidx.annotation.Nullable; | ||
import androidx.appcompat.app.ActionBar; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.appcompat.widget.Toolbar; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class GutenbergEmbedWebViewActivity extends AppCompatActivity { | ||
public static final String ARG_CONTENT = "content"; | ||
public static final String ARG_TITLE = "title"; | ||
private static final String JAVA_SCRIPT_INTERFACE_NAME = "wpwebkit"; | ||
|
||
protected WebView mWebView; | ||
|
||
private ProgressBar mProgressBar; | ||
private AtomicBoolean mIsWebPageLoaded = new AtomicBoolean(false); | ||
private final Handler mWebPageLoadedHandler = new Handler(); | ||
private final Runnable mWebPageLoadedRunnable = new Runnable() { | ||
@Override public void run() { | ||
if (!mIsWebPageLoaded.getAndSet(true)) { | ||
mProgressBar.setVisibility(View.GONE); | ||
} | ||
} | ||
}; | ||
|
||
@SuppressLint("SetJavaScriptEnabled") | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_gutenberg_embed_web_view); | ||
|
||
setupToolbar(); | ||
|
||
mWebView = findViewById(R.id.embed_web_view); | ||
|
||
mProgressBar = findViewById(R.id.progress_bar); | ||
|
||
// Set settings | ||
WebSettings settings = mWebView.getSettings(); | ||
settings.setJavaScriptEnabled(true); | ||
|
||
// Setup WebView client | ||
setupWebViewClient(); | ||
|
||
// Setup Web Chrome client | ||
mWebView.setWebChromeClient(new WebChromeClient() { | ||
@Override | ||
public void onProgressChanged(WebView view, int progress) { | ||
if (progress == 100) { | ||
mWebPageLoadedHandler.removeCallbacks(mWebPageLoadedRunnable); | ||
mWebPageLoadedHandler.postDelayed(mWebPageLoadedRunnable, 1500); | ||
} else { | ||
mIsWebPageLoaded.compareAndSet(true, false); | ||
if (mProgressBar.getVisibility() == View.GONE) { | ||
mProgressBar.setVisibility(View.VISIBLE); | ||
} | ||
mProgressBar.setProgress(progress); | ||
} | ||
} | ||
}); | ||
|
||
load(); | ||
} | ||
|
||
protected void load() { | ||
String content = getIntent().getExtras().getString(ARG_CONTENT); | ||
mWebView.loadData(content, "text/html", "UTF-8"); | ||
} | ||
|
||
private void setupToolbar() { | ||
setTitle(""); | ||
|
||
Toolbar toolbar = findViewById(R.id.toolbar); | ||
if (toolbar != null) { | ||
setSupportActionBar(toolbar); | ||
|
||
ActionBar actionBar = getSupportActionBar(); | ||
if (actionBar != null) { | ||
actionBar.setDisplayShowTitleEnabled(true); | ||
actionBar.setDisplayHomeAsUpEnabled(true); | ||
actionBar.setHomeAsUpIndicator(R.drawable.ic_close_24px); | ||
actionBar.setSubtitle(""); | ||
actionBar.setTitle(getToolbarTitle()); | ||
} | ||
} | ||
} | ||
|
||
protected String getToolbarTitle() { | ||
return getIntent().getExtras().getString(ARG_TITLE); | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(final MenuItem item) { | ||
if (mWebView == null) { | ||
return false; | ||
} | ||
|
||
int itemID = item.getItemId(); | ||
|
||
if (itemID == android.R.id.home) { | ||
finish(); | ||
} | ||
|
||
return super.onOptionsItemSelected(item); | ||
} | ||
|
||
private void setupWebViewClient() { | ||
mWebView.setWebViewClient(new WebViewClient() { | ||
@Override | ||
public void onPageStarted(WebView view, String url, Bitmap favicon) { | ||
// Center the embed with a black background; | ||
String css = "body{margin:0;background:#000;display:flex;align-items:center;}"; | ||
String js = String.format("(()=>{const c='%s';const s=document.createElement('style');s.textContent=c;document.head.append(s);})()", css); | ||
view.evaluateJavascript(js, null); | ||
super.onPageStarted(view, url, favicon); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onBackPressed() { | ||
if (mWebView.canGoBack()) { | ||
mWebView.goBack(); | ||
} else { | ||
super.onBackPressed(); | ||
} | ||
} | ||
|
||
@Override | ||
public void finish() { | ||
runOnUiThread(() -> { | ||
mWebView.removeJavascriptInterface(JAVA_SCRIPT_INTERFACE_NAME); | ||
mWebView.clearHistory(); | ||
mWebView.clearFormData(); | ||
mWebView.clearCache(true); | ||
mWebView.clearSslPreferences(); | ||
}); | ||
|
||
super.finish(); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
mWebPageLoadedHandler.removeCallbacks(mWebPageLoadedRunnable); | ||
super.onDestroy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...dge/android/react-native-bridge/src/main/res/layout/activity_gutenberg_embed_web_view.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<com.google.android.material.appbar.AppBarLayout | ||
android:id="@+id/appbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentTop="true"> | ||
|
||
<com.google.android.material.appbar.MaterialToolbar | ||
android:id="@+id/toolbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:background="@color/status_bar_color" | ||
app:titleTextColor="@android:color/white" | ||
app:contentInsetEnd="@dimen/toolbar_content_offset_end" | ||
app:contentInsetLeft="@dimen/toolbar_content_offset" | ||
app:contentInsetRight="@dimen/toolbar_content_offset_end" | ||
app:contentInsetStart="@dimen/toolbar_content_offset" /> | ||
|
||
</com.google.android.material.appbar.AppBarLayout> | ||
|
||
<WebView | ||
android:id="@+id/embed_web_view" | ||
android:layout_below="@+id/appbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:visibility="visible"/> | ||
|
||
<ProgressBar | ||
android:id="@+id/progress_bar" | ||
style="@android:style/Widget.ProgressBar.Horizontal" | ||
android:layout_width="match_parent" | ||
android:layout_height="@dimen/progress_bar_height" | ||
android:layout_alignTop="@+id/embed_web_view" | ||
android:indeterminate="false" | ||
android:progressDrawable="@drawable/progressbar_horizontal"/> | ||
|
||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fullscreen preview on iOS has a black background by default so there is no need to check the user theme preference .