Skip to content

Commit fae50b6

Browse files
fkirccarlpoole
authored andcommitted
refactor(android): remove unused interaction listener on BridgeFragment (#3552)
1 parent cbab54c commit fae50b6

File tree

1 file changed

+119
-155
lines changed

1 file changed

+119
-155
lines changed

android/capacitor/src/main/java/com/getcapacitor/BridgeFragment.java

Lines changed: 119 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -3,195 +3,159 @@
33
import android.app.Activity;
44
import android.content.Context;
55
import android.content.res.TypedArray;
6-
import android.net.Uri;
76
import android.os.Bundle;
87
import android.util.AttributeSet;
98
import android.view.LayoutInflater;
109
import android.view.View;
1110
import android.view.ViewGroup;
1211
import android.webkit.WebView;
13-
1412
import androidx.fragment.app.Fragment;
15-
1613
import com.getcapacitor.android.R;
1714
import com.getcapacitor.cordova.MockCordovaInterfaceImpl;
1815
import com.getcapacitor.cordova.MockCordovaWebViewImpl;
19-
16+
import java.util.ArrayList;
17+
import java.util.List;
2018
import org.apache.cordova.ConfigXmlParser;
2119
import org.apache.cordova.CordovaPreferences;
2220
import org.apache.cordova.PluginEntry;
2321
import org.apache.cordova.PluginManager;
2422
import org.json.JSONObject;
2523

26-
import java.util.ArrayList;
27-
import java.util.List;
28-
2924
/**
3025
* A simple {@link Fragment} subclass.
31-
* Activities that contain this fragment must implement the
32-
* {@link BridgeFragment.OnFragmentInteractionListener} interface
33-
* to handle interaction events.
3426
* Use the {@link BridgeFragment#newInstance} factory method to
3527
* create an instance of this fragment.
3628
*/
3729
public class BridgeFragment extends Fragment {
38-
private static final String ARG_START_DIR = "startDir";
39-
40-
private String startDir;
41-
42-
private OnFragmentInteractionListener mListener;
43-
44-
private WebView webView;
45-
protected Bridge bridge;
46-
protected MockCordovaInterfaceImpl cordovaInterface;
47-
protected boolean keepRunning = true;
48-
private ArrayList<PluginEntry> pluginEntries;
49-
private PluginManager pluginManager;
50-
private CordovaPreferences preferences;
51-
private MockCordovaWebViewImpl mockWebView;
52-
private int activityDepth = 0;
53-
private String bridgeStartDir;
54-
55-
private String lastActivityPlugin;
56-
57-
private List<Class<? extends Plugin>> initialPlugins = new ArrayList<>();
58-
private JSONObject config = new JSONObject();
59-
60-
public BridgeFragment() {
61-
// Required empty public constructor
62-
}
63-
64-
/**
65-
* Use this factory method to create a new instance of
66-
* this fragment using the provided parameters.
67-
*
68-
* @param startDir the directory to serve content from
69-
* @return A new instance of fragment BridgeFragment.
70-
*/
71-
public static BridgeFragment newInstance(String startDir) {
72-
BridgeFragment fragment = new BridgeFragment();
73-
Bundle args = new Bundle();
74-
args.putString(ARG_START_DIR, startDir);
75-
fragment.setArguments(args);
76-
return fragment;
77-
}
78-
79-
protected void init(Bundle savedInstanceState) {
80-
loadConfig(this.getActivity().getApplicationContext(), this.getActivity());
81-
}
82-
83-
/**
84-
* Load the WebView and create the Bridge
85-
*/
86-
protected void load(Bundle savedInstanceState) {
87-
Logger.debug("Starting BridgeActivity");
88-
89-
Bundle args = getArguments();
90-
String startDir = null;
91-
92-
if (args != null) {
93-
startDir = getArguments().getString(ARG_START_DIR);
30+
private static final String ARG_START_DIR = "startDir";
31+
32+
private WebView webView;
33+
protected Bridge bridge;
34+
protected MockCordovaInterfaceImpl cordovaInterface;
35+
protected boolean keepRunning = true;
36+
private ArrayList<PluginEntry> pluginEntries;
37+
private PluginManager pluginManager;
38+
private CordovaPreferences preferences;
39+
private MockCordovaWebViewImpl mockWebView;
40+
41+
private List<Class<? extends Plugin>> initialPlugins = new ArrayList<>();
42+
private JSONObject config = new JSONObject();
43+
44+
public BridgeFragment() {
45+
// Required empty public constructor
46+
}
47+
48+
/**
49+
* Use this factory method to create a new instance of
50+
* this fragment using the provided parameters.
51+
*
52+
* @param startDir the directory to serve content from
53+
* @return A new instance of fragment BridgeFragment.
54+
*/
55+
public static BridgeFragment newInstance(String startDir) {
56+
BridgeFragment fragment = new BridgeFragment();
57+
Bundle args = new Bundle();
58+
args.putString(ARG_START_DIR, startDir);
59+
fragment.setArguments(args);
60+
return fragment;
9461
}
9562

96-
webView = getView().findViewById(R.id.webview);
97-
cordovaInterface = new MockCordovaInterfaceImpl(this.getActivity());
98-
if (savedInstanceState != null) {
99-
cordovaInterface.restoreInstanceState(savedInstanceState);
63+
protected void init(Bundle savedInstanceState) {
64+
loadConfig(this.getActivity().getApplicationContext(), this.getActivity());
10065
}
10166

102-
mockWebView = new MockCordovaWebViewImpl(getActivity().getApplicationContext());
103-
mockWebView.init(cordovaInterface, pluginEntries, preferences, webView);
67+
public void addPlugin(Class<? extends Plugin> plugin) {
68+
this.initialPlugins.add(plugin);
69+
}
70+
71+
/**
72+
* Load the WebView and create the Bridge
73+
*/
74+
protected void load(Bundle savedInstanceState) {
75+
Logger.debug("Starting BridgeActivity");
76+
77+
Bundle args = getArguments();
78+
String startDir = null;
10479

105-
pluginManager = mockWebView.getPluginManager();
106-
cordovaInterface.onCordovaInit(pluginManager);
80+
if (args != null) {
81+
startDir = getArguments().getString(ARG_START_DIR);
82+
}
10783

108-
if (preferences == null) {
109-
preferences = new CordovaPreferences();
84+
webView = getView().findViewById(R.id.webview);
85+
cordovaInterface = new MockCordovaInterfaceImpl(this.getActivity());
86+
if (savedInstanceState != null) {
87+
cordovaInterface.restoreInstanceState(savedInstanceState);
88+
}
89+
90+
mockWebView = new MockCordovaWebViewImpl(getActivity().getApplicationContext());
91+
mockWebView.init(cordovaInterface, pluginEntries, preferences, webView);
92+
93+
pluginManager = mockWebView.getPluginManager();
94+
cordovaInterface.onCordovaInit(pluginManager);
95+
96+
if (preferences == null) {
97+
preferences = new CordovaPreferences();
98+
}
99+
100+
bridge = new Bridge(this.getActivity(), webView, initialPlugins, cordovaInterface, pluginManager, preferences, config);
101+
102+
if (startDir != null) {
103+
bridge.setServerAssetPath(startDir);
104+
}
105+
106+
if (savedInstanceState != null) {
107+
bridge.restoreInstanceState(savedInstanceState);
108+
}
109+
this.keepRunning = preferences.getBoolean("KeepRunning", true);
110110
}
111111

112-
bridge = new Bridge(this.getActivity(), webView, initialPlugins, cordovaInterface, pluginManager, preferences, config);
112+
public void loadConfig(Context context, Activity activity) {
113+
ConfigXmlParser parser = new ConfigXmlParser();
114+
parser.parse(context);
115+
preferences = parser.getPreferences();
116+
preferences.setPreferencesBundle(activity.getIntent().getExtras());
117+
pluginEntries = parser.getPluginEntries();
118+
}
113119

114-
if (startDir != null) {
115-
bridge.setServerAssetPath(startDir);
120+
@Override
121+
public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
122+
super.onInflate(context, attrs, savedInstanceState);
123+
124+
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.bridge_fragment);
125+
CharSequence c = a.getString(R.styleable.bridge_fragment_start_dir);
126+
127+
if (c != null) {
128+
String startDir = c.toString();
129+
Bundle args = new Bundle();
130+
args.putString(ARG_START_DIR, startDir);
131+
setArguments(args);
132+
}
133+
}
134+
135+
@Override
136+
public void onCreate(Bundle savedInstanceState) {
137+
super.onCreate(savedInstanceState);
116138
}
117139

118-
if (savedInstanceState != null) {
119-
bridge.restoreInstanceState(savedInstanceState);
140+
@Override
141+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
142+
// Inflate the layout for this fragment
143+
return inflater.inflate(R.layout.fragment_bridge, container, false);
120144
}
121-
this.keepRunning = preferences.getBoolean("KeepRunning", true);
122-
}
123-
124-
public void loadConfig(Context context, Activity activity) {
125-
ConfigXmlParser parser = new ConfigXmlParser();
126-
parser.parse(context);
127-
preferences = parser.getPreferences();
128-
preferences.setPreferencesBundle(activity.getIntent().getExtras());
129-
pluginEntries = parser.getPluginEntries();
130-
}
131-
132-
133-
@Override
134-
public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
135-
super.onInflate(context, attrs, savedInstanceState);
136-
137-
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.bridge_fragment);
138-
CharSequence c = a.getString(R.styleable.bridge_fragment_start_dir);
139-
140-
if (c != null) {
141-
String startDir = c.toString();
142-
Bundle args = new Bundle();
143-
args.putString(ARG_START_DIR, startDir);
144-
setArguments(args);
145+
146+
@Override
147+
public void onActivityCreated(Bundle savedInstanceState) {
148+
super.onActivityCreated(savedInstanceState);
149+
this.init(savedInstanceState);
150+
this.load(savedInstanceState);
145151
}
146-
}
147-
148-
@Override
149-
public void onCreate(Bundle savedInstanceState) {
150-
super.onCreate(savedInstanceState);
151-
}
152-
153-
@Override
154-
public View onCreateView(LayoutInflater inflater, ViewGroup container,
155-
Bundle savedInstanceState) {
156-
// Inflate the layout for this fragment
157-
return inflater.inflate(R.layout.fragment_bridge, container, false);
158-
}
159-
160-
@Override
161-
public void onActivityCreated(Bundle savedInstanceState) {
162-
super.onActivityCreated(savedInstanceState);
163-
this.init(savedInstanceState);
164-
this.load(savedInstanceState);
165-
}
166-
167-
@Override
168-
public void onAttach(Context context) {
169-
super.onAttach(context);
170-
if (context instanceof OnFragmentInteractionListener) {
171-
mListener = (OnFragmentInteractionListener) context;
172-
} else {
173-
throw new RuntimeException(context.toString()
174-
+ " must implement OnFragmentInteractionListener");
152+
153+
@Override
154+
public void onDestroy() {
155+
super.onDestroy();
156+
this.bridge.onDestroy();
157+
if (this.mockWebView != null) {
158+
mockWebView.handleDestroy();
159+
}
175160
}
176-
}
177-
178-
@Override
179-
public void onDetach() {
180-
super.onDetach();
181-
mListener = null;
182-
}
183-
184-
/**
185-
* This interface must be implemented by activities that contain this
186-
* fragment to allow an interaction in this fragment to be communicated
187-
* to the activity and potentially other fragments contained in that
188-
* activity.
189-
* <p>
190-
* See the Android Training lesson <a href=
191-
* "http://developer.android.com/training/basics/fragments/communicating.html"
192-
* >Communicating with Other Fragments</a> for more information.
193-
*/
194-
public interface OnFragmentInteractionListener {
195-
void onFragmentInteraction(Uri uri);
196-
}
197161
}

0 commit comments

Comments
 (0)