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

chore: remove App plugin #3676

Merged
merged 6 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
85 changes: 85 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.getcapacitor;

import androidx.annotation.Nullable;

public class App {

/**
* Interface for callbacks when app status changes.
*/
public interface AppStatusChangeListener {
void onAppStatusChanged(Boolean isActive);
}

/**
* Interface for callbacks when back button is pressed.
*/
public interface BackButtonListener {
void onBackButton();
}

/**
* Interface for callbacks when app is restored with pending plugin call.
*/
public interface AppRestoredListener {
void onAppRestored(PluginResult result);
}

@Nullable
private AppStatusChangeListener statusChangeListener;

private BackButtonListener backButtonListener;
private AppRestoredListener appRestoredListener;
imhoffd marked this conversation as resolved.
Show resolved Hide resolved
private boolean isActive = false;

public boolean isActive() {
return isActive;
}

/**
* Set the object to receive callbacks.
* @param listener
*/
public void setStatusChangeListener(@Nullable AppStatusChangeListener listener) {
this.statusChangeListener = listener;
}

/**
* Set the object to receive callbacks.
* @param listener
*/
public void setBackButtonListener(@Nullable BackButtonListener listener) {
this.backButtonListener = listener;
}

/**
* Set the object to receive callbacks.
* @param listener
*/
public void setAppRestoredListener(@Nullable AppRestoredListener listener) {
this.appRestoredListener = listener;
}

protected void fireRestoredResult(PluginResult result) {
if (appRestoredListener != null) {
appRestoredListener.onAppRestored(result);
}
}

public void fireChange(boolean isActive) {
imhoffd marked this conversation as resolved.
Show resolved Hide resolved
this.isActive = isActive;
if (statusChangeListener != null) {
statusChangeListener.onAppStatusChanged(isActive);
}
}

public void fireBackButton() {
if (backButtonListener != null) {
backButtonListener.onBackButton();
}
}

public boolean hasBackButtonListeners() {
return backButtonListener != null;
}
}
34 changes: 14 additions & 20 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.getcapacitor.plugin.App;
import com.getcapacitor.plugin.Geolocation;
import com.getcapacitor.plugin.LocalNotifications;
import com.getcapacitor.plugin.PushNotifications;
Expand Down Expand Up @@ -83,6 +82,7 @@ public class Bridge {
public final CordovaInterfaceImpl cordovaInterface;
private CordovaPreferences preferences;
private BridgeWebViewClient webViewClient;
private App app;

// Our MessageHandler for sending and receiving data to the WebView
private final MessageHandler msgHandler;
Expand Down Expand Up @@ -123,6 +123,7 @@ public Bridge(
CordovaPreferences preferences,
JSONObject config
) {
this.app = new App();
this.context = context;
this.webView = webView;
this.webViewClient = new BridgeWebViewClient(this);
Expand Down Expand Up @@ -157,6 +158,10 @@ public Bridge(
this.loadWebView();
}

public App getApp() {
return app;
}

private void loadWebView() {
appUrlConfig = this.config.getString("server.url");
String[] appAllowNavigationConfig = this.config.getArray("server.allowNavigation");
Expand Down Expand Up @@ -379,7 +384,6 @@ private void initWebView() {
* Register our core Plugin APIs
*/
private void registerAllPlugins() {
this.registerPlugin(App.class);
this.registerPlugin(BackgroundTask.class);
this.registerPlugin(LocalNotifications.class);
this.registerPlugin(Geolocation.class);
Expand Down Expand Up @@ -617,12 +621,6 @@ private JSInjector getJSInjector() {
return null;
}

protected void storeDanglingPluginResult(PluginCall call, PluginResult result) {
PluginHandle appHandle = getPlugin("App");
App appPlugin = (App) appHandle.getInstance();
appPlugin.fireRestoredResult(result);
}

/**
* Restore any saved bundle state data
* @param savedInstanceState
Expand Down Expand Up @@ -805,18 +803,14 @@ public void onDestroy() {
}

public void onBackPressed() {
PluginHandle appHandle = getPlugin("App");
if (appHandle != null) {
App appPlugin = (App) appHandle.getInstance();

// If there are listeners, don't do the default action, as this means the user
// wants to override the back button
if (appPlugin.hasBackButtonListeners()) {
appPlugin.fireBackButton();
} else {
if (webView.canGoBack()) {
webView.goBack();
}
// If there are listeners, don't do the default action, as this means the user
// wants to override the back button
if (app.hasBackButtonListeners()) {
app.fireBackButton();
triggerJSEvent("backbutton", "document");
} else {
if (webView.canGoBack()) {
webView.goBack();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.getcapacitor.android.R;
import com.getcapacitor.cordova.MockCordovaInterfaceImpl;
import com.getcapacitor.cordova.MockCordovaWebViewImpl;
import com.getcapacitor.plugin.App;
import java.util.ArrayList;
import java.util.List;
import org.apache.cordova.ConfigXmlParser;
Expand Down Expand Up @@ -90,22 +89,6 @@ public Bridge getBridge() {
return this.bridge;
}

/**
* Notify the App plugin that the current state changed
* @param isActive
*/
private void fireAppStateChanged(boolean isActive) {
PluginHandle handle = bridge.getPlugin("App");
if (handle == null) {
return;
}

App appState = (App) handle.getInstance();
if (appState != null) {
appState.fireChange(isActive);
}
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Expand Down Expand Up @@ -135,7 +118,7 @@ public void onRestart() {
public void onResume() {
super.onResume();

fireAppStateChanged(true);
bridge.getApp().fireChange(true);

this.bridge.onResume();

Expand Down Expand Up @@ -163,7 +146,7 @@ public void onStop() {

activityDepth = Math.max(0, activityDepth - 1);
if (activityDepth == 0) {
fireAppStateChanged(false);
bridge.getApp().fireChange(false);
}

this.bridge.onStop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void sendResponseMessage(PluginCall call, PluginResult successResult, Plu
final WebView webView = this.webView;
webView.post(() -> webView.evaluateJavascript(runScript, null));
} else {
bridge.storeDanglingPluginResult(call, data);
bridge.getApp().fireRestoredResult(data);
}
} catch (Exception ex) {
Logger.error("sendResponseMessage: error: " + ex);
Expand Down
147 changes: 0 additions & 147 deletions android/capacitor/src/main/java/com/getcapacitor/plugin/App.java

This file was deleted.

Loading