-
Notifications
You must be signed in to change notification settings - Fork 47
feat: Implement fullscreen WebView for vshare with theme auto-adaptation #153
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
Merged
+200
−1
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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
173 changes: 173 additions & 0 deletions
173
app/src/main/java/me/ghui/v2er/module/vshare/VshareWebActivity.java
This file contains hidden or 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,173 @@ | ||
| package me.ghui.v2er.module.vshare; | ||
|
|
||
| import android.annotation.SuppressLint; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.graphics.Bitmap; | ||
| import android.os.Bundle; | ||
| import android.view.View; | ||
| import android.webkit.WebChromeClient; | ||
| import android.webkit.WebResourceRequest; | ||
| import android.webkit.WebSettings; | ||
| import android.webkit.WebView; | ||
| import android.webkit.WebViewClient; | ||
| import android.widget.ProgressBar; | ||
|
|
||
| import androidx.appcompat.app.AppCompatActivity; | ||
|
|
||
| import butterknife.BindView; | ||
| import butterknife.ButterKnife; | ||
| import me.ghui.v2er.R; | ||
| import me.ghui.v2er.util.DarkModelUtils; | ||
|
|
||
| /** | ||
| * Fullscreen WebView Activity for displaying vshare page | ||
| * with automatic theme adaptation | ||
| */ | ||
| public class VshareWebActivity extends AppCompatActivity { | ||
|
|
||
| private static final String VSHARE_BASE_URL = "https://v2er.app/vshare"; | ||
|
|
||
| @BindView(R.id.webview) | ||
| WebView mWebView; | ||
|
|
||
| @BindView(R.id.progress_bar) | ||
| ProgressBar mProgressBar; | ||
|
|
||
| public static void open(Context context) { | ||
| Intent intent = new Intent(context, VshareWebActivity.class); | ||
| context.startActivity(intent); | ||
| } | ||
|
|
||
| @Override | ||
| protected void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
|
|
||
| // Hide action bar if present | ||
| if (getSupportActionBar() != null) { | ||
| getSupportActionBar().hide(); | ||
| } | ||
|
|
||
| // Set SystemUI flags to match MainActivity's edge-to-edge behavior | ||
| View decorView = getWindow().getDecorView(); | ||
| int systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | ||
| | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | ||
| | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN; | ||
|
|
||
| // Set status bar icon color based on theme | ||
| if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { | ||
| if (!DarkModelUtils.isDarkMode()) { | ||
| // Light mode: use dark status bar icons | ||
| systemUiVisibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; | ||
| } | ||
| } | ||
|
|
||
| decorView.setSystemUiVisibility(systemUiVisibility); | ||
|
|
||
| setContentView(R.layout.activity_vshare_web); | ||
| ButterKnife.bind(this); | ||
|
|
||
| setupWebView(); | ||
|
|
||
| // Compute URL with theme parameter based on current app theme | ||
| String url = VSHARE_BASE_URL; | ||
| if (DarkModelUtils.isDarkMode()) { | ||
| url += "?theme=dark"; | ||
| } else { | ||
| url += "?theme=light"; | ||
| } | ||
| mWebView.loadUrl(url); | ||
| } | ||
|
|
||
| @SuppressLint("SetJavaScriptEnabled") | ||
| private void setupWebView() { | ||
| WebSettings settings = mWebView.getSettings(); | ||
|
|
||
| // Enable JavaScript | ||
| settings.setJavaScriptEnabled(true); | ||
|
|
||
| // Enable DOM storage | ||
| settings.setDomStorageEnabled(true); | ||
|
|
||
| // Disable file and content access for security | ||
| settings.setAllowFileAccess(false); | ||
| settings.setAllowContentAccess(false); | ||
|
|
||
| // Enable Safe Browsing if API >= 26 | ||
| if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | ||
| settings.setSafeBrowsingEnabled(true); | ||
| } | ||
|
|
||
| // Enable responsive layout | ||
| settings.setUseWideViewPort(true); | ||
| settings.setLoadWithOverviewMode(true); | ||
|
|
||
| // Enable zoom | ||
| settings.setSupportZoom(true); | ||
| settings.setBuiltInZoomControls(true); | ||
| settings.setDisplayZoomControls(false); | ||
|
|
||
| // Set cache mode | ||
| settings.setCacheMode(WebSettings.LOAD_DEFAULT); | ||
|
|
||
| // Set WebViewClient to handle navigation | ||
| mWebView.setWebViewClient(new WebViewClient() { | ||
| @Override | ||
| public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { | ||
| // Let the WebView handle the navigation | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void onPageStarted(WebView view, String url, Bitmap favicon) { | ||
| super.onPageStarted(view, url, favicon); | ||
| mProgressBar.setVisibility(View.VISIBLE); | ||
| } | ||
|
|
||
| @Override | ||
| public void onPageFinished(WebView view, String url) { | ||
| super.onPageFinished(view, url); | ||
|
|
||
| // Inject CSS to ensure proper theme is applied | ||
| String theme = DarkModelUtils.isDarkMode() ? "dark" : "light"; | ||
| String js = "javascript:(function() { " + | ||
| "document.documentElement.setAttribute('data-theme', '" + theme + "'); " + | ||
| "})()"; | ||
| mWebView.loadUrl(js); | ||
| } | ||
| }); | ||
|
|
||
| // Set WebChromeClient for progress updates | ||
| mWebView.setWebChromeClient(new WebChromeClient() { | ||
| @Override | ||
| public void onProgressChanged(WebView view, int newProgress) { | ||
| mProgressBar.setProgress(newProgress); | ||
| if (newProgress == 100) { | ||
| mProgressBar.setVisibility(View.GONE); | ||
| } | ||
| } | ||
|
Comment on lines
+141
to
+148
|
||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void onBackPressed() { | ||
| // Handle back navigation in WebView | ||
| if (mWebView.canGoBack()) { | ||
| mWebView.goBack(); | ||
| } else { | ||
| super.onBackPressed(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void onDestroy() { | ||
| if (mWebView != null) { | ||
| mWebView.clearHistory(); | ||
| mWebView.clearCache(false); | ||
| mWebView.loadUrl("about:blank"); | ||
| mWebView.pauseTimers(); | ||
| mWebView.destroy(); | ||
| } | ||
| super.onDestroy(); | ||
| } | ||
| } | ||
This file contains hidden or 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,21 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:background="@android:color/transparent"> | ||
|
|
||
| <WebView | ||
| android:id="@+id/webview" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" /> | ||
|
|
||
| <ProgressBar | ||
| android:id="@+id/progress_bar" | ||
| style="?android:attr/progressBarStyleHorizontal" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="3dp" | ||
| android:layout_gravity="top" | ||
| android:progressTint="@color/colorAccent" | ||
| android:visibility="gone" /> | ||
|
|
||
| </FrameLayout> |
Oops, something went wrong.
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.
JavaScript + DOM storage are enabled for remote content without tightening other surfaces; consider explicitly disabling file and content access (settings.setAllowFileAccess(false); settings.setAllowContentAccess(false);) and enabling Safe Browsing (if API >= 26) to reduce attack surface.