-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RNMobile] Add embed webview for Android (#50440)
* Add Gutenberg Embed WebView * Clean up * Rename method Fix renaming --ammend * Remove commented out code * Removed unused method * Remove unused menu layout * Update load and get title methods These are no longer overriden in the main Android app --------- Co-authored-by: jhnstn <jhnstn@pm.me>
- Loading branch information
Showing
8 changed files
with
247 additions
and
1 deletion.
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