Skip to content

Commit

Permalink
chore: remove App plugin (#3676)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Oct 19, 2020
1 parent 8576b0f commit c3e821e
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 563 deletions.
89 changes: 89 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,89 @@
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;

@Nullable
private BackButtonListener backButtonListener;

@Nullable
private AppRestoredListener appRestoredListener;

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 fireStatusChange(boolean isActive) {
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().fireStatusChange(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().fireStatusChange(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

0 comments on commit c3e821e

Please sign in to comment.