Skip to content

Commit

Permalink
refactor(android): require AppCompatActivity in Bridge (#3883)
Browse files Browse the repository at this point in the history
Co-authored-by: Carl Poole <[email protected]>
  • Loading branch information
imhoffd and carlpoole authored Dec 1, 2020
1 parent b417278 commit b883af4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
11 changes: 6 additions & 5 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.getcapacitor.android.R;
import com.getcapacitor.annotation.CapacitorPlugin;
Expand Down Expand Up @@ -79,7 +80,7 @@ public class Bridge {
private CapConfig config;

// A reference to the main activity for the app
private final Activity context;
private final AppCompatActivity context;
private WebViewLocalServer localServer;
private String localUrl;
private String appUrl;
Expand Down Expand Up @@ -129,7 +130,7 @@ public class Bridge {
*/
@Deprecated
public Bridge(
Activity context,
AppCompatActivity context,
WebView webView,
List<Class<? extends Plugin>> initialPlugins,
MockCordovaInterfaceImpl cordovaInterface,
Expand Down Expand Up @@ -324,7 +325,7 @@ public Context getContext() {
* Get the activity for the app
* @return
*/
public Activity getActivity() {
public AppCompatActivity getActivity() {
return this.context;
}

Expand Down Expand Up @@ -1096,11 +1097,11 @@ public static class Builder {
private Bundle instanceState = null;
private JSONObject config = new JSONObject();
private List<Class<? extends Plugin>> plugins = new ArrayList<>();
private Activity activity = null;
private AppCompatActivity activity = null;
private Context context = null;
private WebView webView = null;

protected Builder setActivity(Activity activity) {
protected Builder setActivity(AppCompatActivity activity) {
this.activity = activity;
this.context = activity.getApplicationContext();
this.webView = activity.findViewById(R.id.webview);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.getcapacitor.android.R;
import java.util.ArrayList;
Expand Down Expand Up @@ -62,7 +63,12 @@ protected void load(Bundle savedInstanceState) {
startDir = getArguments().getString(ARG_START_DIR);
}

bridge = new Bridge.Builder().setActivity(getActivity()).setInstanceState(savedInstanceState).setPlugins(initialPlugins).create();
bridge =
new Bridge.Builder()
.setActivity((AppCompatActivity) getActivity())
.setInstanceState(savedInstanceState)
.setPlugins(initialPlugins)
.create();

if (startDir != null) {
bridge.setServerAssetPath(startDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Context getContext() {
* @return the Activity for the current app
*/
public AppCompatActivity getActivity() {
return (AppCompatActivity) this.bridge.getActivity();
return this.bridge.getActivity();
}

/**
Expand Down

0 comments on commit b883af4

Please sign in to comment.