Skip to content
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

Fix Prevent Widget Malfunction and App Crash After Extension Removal #217

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.json.JSONObject;
import java.util.List;
import java.util.ArrayList;
import org.chromium.chrome.browser.ntp.NewTabPageLayout;
import android.content.Context;
import android.widget.Toast;
import org.chromium.base.ContextUtils;
Expand All @@ -17,6 +18,12 @@
import org.jni_zero.NativeMethods;

public class Extensions {
private static NewTabPageLayout newTabPageLayout;

public static void setNewTabPageLayout(NewTabPageLayout layout) {
newTabPageLayout = layout;
}

public static ArrayList<ExtensionInfo> getExtensionsInfo() {
String jsonString = ExtensionsJni.get().getExtensionsInfo();
ArrayList<ExtensionInfo> result = new ArrayList<>();
Expand Down Expand Up @@ -69,7 +76,13 @@ public static void installExtension(String url) {
}

public static void uninstallExtension(String extensionId) {
Log.d("Extensions", "Uninstalling extension: " + extensionId);
ExtensionsJni.get().uninstallExtension(extensionId);
if (newTabPageLayout != null) {
Log.d("Extensions", "Reloading new tab page!!");
newTabPageLayout.reload();
Log.d("Extensions", "Reloaded new tab page!!");
}
}

@NativeMethods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,19 @@ public void initialize(
manager.addDestructionObserver(NewTabPageLayout.this::onDestroy);
mInitialized = true;

Log.d(TAG, "NewTabPageLayout initialized to the Extensions object!!");
Extensions.setNewTabPageLayout(this);
Log.d(TAG, "NewTabPageLayout set to the Extensions object!!");

TraceEvent.end(TAG + ".initialize()");
}

public void reload() {
// TODO(crbug.com/41487877): Add handler in Magic Stack and dispatcher.
Log.d(TAG, "Reload the NewTabPageLayout!!");
Log.d(TAG, "Detaching and Reattaching the NewTabPageLayout!!");
this.onDetachedFromWindow();
this.onAttachedToWindow();
}

/**
Expand Down Expand Up @@ -1411,107 +1419,142 @@ public Callback<Logo> getOnLogoAvailableCallback() {
}

private View createWebView(int i) {
Log.d(TAG, "EXTS: " + Extensions.getExtensionsInfo().get(i).toString());

NestedScrollView scrollView = new NestedScrollView(getContext());
scrollView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));

// Create a FrameLayout to wrap the GridView
FrameLayout viewWrapper = new FrameLayout(getContext());
FrameLayout.LayoutParams wrapperParams = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
try {
Log.d(TAG, "Creating WebView for extension index: " + i);
Log.d(TAG, "EXTS: " + Extensions.getExtensionsInfo().get(i).toString());

NestedScrollView scrollView = new NestedScrollView(getContext());
scrollView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));

// Create a FrameLayout to wrap the GridView
FrameLayout viewWrapper = new FrameLayout(getContext());
FrameLayout.LayoutParams wrapperParams = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);

// Set margins for the wrapper (adjust these values as needed)
int margin = dpToPx(32); // Convert 16dp to pixels
wrapperParams.setMargins(
0,
-margin,
0,
margin
);
viewWrapper.setLayoutParams(wrapperParams);

Profile profile = ProfileManager.getLastUsedRegularProfile();
WebContents webContents = WebContentsFactory.createWebContents(profile, true, false);
ContentView contentView = ContentView.createContentView(getContext(), null, webContents);
webContents.setDelegates(
VersionInfo.getProductVersion(),
ViewAndroidDelegate.createBasicDelegate(contentView),
contentView,
mWindowAndroid,
WebContents.createDefaultInternalsHolder());

Log.d(TAG, "contentview " + contentView.toString());
// viewWrapper.addView(contentView);

// Set margins for the wrapper (adjust these values as needed)
int margin = dpToPx(32); // Convert 16dp to pixels
wrapperParams.setMargins(
0,
-margin,
0,
margin
);
viewWrapper.setLayoutParams(wrapperParams);

Profile profile = ProfileManager.getLastUsedRegularProfile();
WebContents webContents = WebContentsFactory.createWebContents(profile, true, false);
ContentView contentView = ContentView.createContentView(getContext(), null, webContents);
webContents.setDelegates(
VersionInfo.getProductVersion(),
ViewAndroidDelegate.createBasicDelegate(contentView),
contentView,
mWindowAndroid,
WebContents.createDefaultInternalsHolder());

Log.d(TAG, "contentview " + contentView.toString());
// viewWrapper.addView(contentView);

IntentRequestTracker intentRequestTracker = mWindowAndroid.getIntentRequestTracker();
ThinWebView thinWebView = ThinWebViewFactory.create(
getContext(), new ThinWebViewConstraints(), intentRequestTracker);
thinWebView.attachWebContents(webContents, contentView, null);
float borderRadius = dpToPx(24); // You can adjust this value as needed
thinWebView.getView().setClipToOutline(true);
thinWebView.getView().setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), borderRadius);
}
});


String widgetUrl = Extensions.getExtensionsInfo().get(i).getWidgetUrl();
webContents.getNavigationController().loadUrl(
new LoadUrlParams(widgetUrl));

// Create a FrameLayout to hold both the WebView and the overlay
FrameLayout container = new FrameLayout(getContext());
container.setLayoutParams(new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));

View view = thinWebView.getView();
view.setTag(webContents);

mCurrentWebContents = webContents;
// setupFloatingBackButton();
container.addView(view);
IntentRequestTracker intentRequestTracker = mWindowAndroid.getIntentRequestTracker();
ThinWebView thinWebView = ThinWebViewFactory.create(
getContext(), new ThinWebViewConstraints(), intentRequestTracker);
thinWebView.attachWebContents(webContents, contentView, null);
float borderRadius = dpToPx(24); // You can adjust this value as needed
thinWebView.getView().setClipToOutline(true);
thinWebView.getView().setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), borderRadius);
}
});

// Add a transparent overlay View
View overlay = new View(getContext());
overlay.setLayoutParams(new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
overlay.setBackgroundColor(Color.TRANSPARENT);
overlay.setOnClickListener(v -> {
ExtensionInfo extension = Extensions.getExtensionsInfo().get(i);
String extensionId = extension.getId();
OpenExtensionsById.openExtensionById(extensionId);
});
container.addView(overlay);

return container;
String widgetUrl = Extensions.getExtensionsInfo().get(i).getWidgetUrl();
Log.d(TAG, "Loading widget URL: " + widgetUrl);
webContents.getNavigationController().loadUrl(
new LoadUrlParams(widgetUrl));

// Create a FrameLayout to hold both the WebView and the overlay
FrameLayout container = new FrameLayout(getContext());
container.setLayoutParams(new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));

View view = thinWebView.getView();
view.setTag(webContents);

mCurrentWebContents = webContents;
// setupFloatingBackButton();
container.addView(view);

// Add a transparent overlay View
View overlay = new View(getContext());
overlay.setLayoutParams(new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
overlay.setBackgroundColor(Color.TRANSPARENT);
overlay.setOnClickListener(v -> {
try {
ExtensionInfo extension = Extensions.getExtensionsInfo().get(i);
String extensionId = extension.getId();
Log.d(TAG, "Opening extension with ID: " + extensionId);
OpenExtensionsById.openExtensionById(extensionId);
} catch (Exception e) {
Log.e(TAG, "Error opening extension", e);
cleanupWebContents();
}
});
container.addView(overlay);

return container;
} catch (Exception e) {
Log.e(TAG, "Error creating WebView", e);
Log.e(TAG, "Cleaning up WebContents");
cleanupWebContents();
return null;
}
}

private void initializeExtensionWebView(int index) {
LinearLayout webViewContainer = findViewById(R.id.ntp_web_view_container);
webViewContainer.setVisibility(View.VISIBLE);
FrameLayout webViewFrame = findViewById(R.id.ntp_web_view);
webViewFrame.removeAllViews();
View webView = createWebView(index);
webViewFrame.addView(webView);
try {
Log.d(TAG, "Initializing Extension WebView for index: " + index);
LinearLayout webViewContainer = findViewById(R.id.ntp_web_view_container);
webViewContainer.setVisibility(View.VISIBLE);
FrameLayout webViewFrame = findViewById(R.id.ntp_web_view);
webViewFrame.removeAllViews();
View webView = createWebView(index);
webViewFrame.addView(webView);
} catch (Exception e) {
Log.e(TAG, "Error initializing Extension WebView", e);
cleanupWebContents();
}
}

private void setupExtensionWebViewClick() {
FrameLayout webViewFrame = findViewById(R.id.ntp_web_view);
if (webViewFrame != null) {
webViewFrame.setOnClickListener(v -> {
// Get the extension ID from the Extensions list
ExtensionInfo extensions = Extensions.getExtensionsInfo().get(1);
String extensionId = extensions.getId();
OpenExtensionsById.openExtensionById(extensionId);
});
try {
FrameLayout webViewFrame = findViewById(R.id.ntp_web_view);
if (webViewFrame != null) {
webViewFrame.setOnClickListener(v -> {
try {
// Get the extension ID from the Extensions list
ExtensionInfo extensions = Extensions.getExtensionsInfo().get(1);
String extensionId = extensions.getId();
OpenExtensionsById.openExtensionById(extensionId);
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "Failed to get extension ID: Index out of bounds", e);
cleanupWebContents();
} catch (Exception e) {
Log.e(TAG, "An unexpected error occurred while opening extension", e);
cleanupWebContents();
}
});
}
} catch (Exception e) {
Log.e(TAG, "Error setting up extension WebView click listener", e);
Log.e(TAG, "Cleaning up WebContents");
cleanupWebContents();
}
}

Expand All @@ -1523,15 +1566,21 @@ private int dpToPx(int dp) {
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
Log.d(TAG, "onDetachedFromWindow called");
cleanupWebContents();
}

private void cleanupWebContents() {
public void cleanupWebContents() {
Log.d(TAG, "cleanupWebContents called");
if (mCurrentWebContents != null) {
Log.d(TAG, "Destroying current WebContents");
mCurrentWebContents.destroy();
mCurrentWebContents = null;
}
LinearLayout webViewContainer = findViewById(R.id.ntp_web_view_container);
webViewContainer.setVisibility(View.GONE);
if (webViewContainer != null) {
Log.d(TAG, "Hiding webViewContainer");
webViewContainer.setVisibility(View.GONE);
}
}
}
}
Loading