/
BridgeActivity.java
241 lines (202 loc) · 7.74 KB
/
BridgeActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package com.getcapacitor;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.getcapacitor.android.R;
import java.util.ArrayList;
import java.util.List;
public class BridgeActivity extends AppCompatActivity {
protected Bridge bridge;
protected boolean keepRunning = true;
private CapConfig config;
private int activityDepth = 0;
private List<Class<? extends Plugin>> initialPlugins = new ArrayList<>();
private final Bridge.Builder bridgeBuilder = new Bridge.Builder(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bridgeBuilder.setInstanceState(savedInstanceState);
getApplication().setTheme(getResources().getIdentifier("AppTheme_NoActionBar", "style", getPackageName()));
setTheme(getResources().getIdentifier("AppTheme_NoActionBar", "style", getPackageName()));
setTheme(R.style.AppTheme_NoActionBar);
setContentView(R.layout.bridge_layout_main);
}
/**
* Initializes the Capacitor Bridge with the Activity.
* @deprecated It is preferred not to call this method. If it is not called, the bridge is
* initialized automatically. If you need to add additional plugins during initialization,
* use {@link #registerPlugin(Class)} or {@link #registerPlugins(List)}.
*
* @param plugins A list of plugins to initialize with Capacitor
*/
@Deprecated
protected void init(Bundle savedInstanceState, List<Class<? extends Plugin>> plugins) {
this.init(savedInstanceState, plugins, null);
}
/**
* Initializes the Capacitor Bridge with the Activity.
* @deprecated It is preferred not to call this method. If it is not called, the bridge is
* initialized automatically. If you need to add additional plugins during initialization,
* use {@link #registerPlugin(Class)} or {@link #registerPlugins(List)}.
*
* @param plugins A list of plugins to initialize with Capacitor
* @param config An instance of a Capacitor Configuration to use. If null, will load from file
*/
@Deprecated
protected void init(Bundle savedInstanceState, List<Class<? extends Plugin>> plugins, CapConfig config) {
this.initialPlugins = plugins;
this.config = config;
this.load();
}
/**
* @deprecated This method should not be called manually.
*/
@Deprecated
protected void load(Bundle savedInstanceState) {
this.load();
}
private void load() {
Logger.debug("Starting BridgeActivity");
bridge = bridgeBuilder.addPlugins(initialPlugins).setConfig(config).create();
this.keepRunning = bridge.shouldKeepRunning();
this.onNewIntent(getIntent());
}
public void registerPlugin(Class<? extends Plugin> plugin) {
bridgeBuilder.addPlugin(plugin);
}
public void registerPlugins(List<Class<? extends Plugin>> plugins) {
bridgeBuilder.addPlugins(plugins);
}
public Bridge getBridge() {
return this.bridge;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
bridge.saveInstanceState(outState);
}
@Override
public void onStart() {
super.onStart();
// Preferred behavior: init() was not called, so we construct the bridge with auto-loaded plugins.
if (bridge == null) {
PluginManager loader = new PluginManager(getAssets());
try {
bridgeBuilder.addPlugins(loader.loadPluginClasses());
} catch (PluginLoadException ex) {
Logger.error("Error loading plugins.", ex);
}
this.load();
}
activityDepth++;
this.bridge.onStart();
Logger.debug("App started");
}
@Override
public void onRestart() {
super.onRestart();
this.bridge.onRestart();
Logger.debug("App restarted");
}
@Override
public void onResume() {
super.onResume();
bridge.getApp().fireStatusChange(true);
this.bridge.onResume();
Logger.debug("App resumed");
}
@Override
public void onPause() {
super.onPause();
this.bridge.onPause();
Logger.debug("App paused");
}
@Override
public void onStop() {
super.onStop();
activityDepth = Math.max(0, activityDepth - 1);
if (activityDepth == 0) {
bridge.getApp().fireStatusChange(false);
}
this.bridge.onStop();
Logger.debug("App stopped");
}
@Override
public void onDestroy() {
super.onDestroy();
this.bridge.onDestroy();
Logger.debug("App destroyed");
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
this.bridge.onDetachedFromWindow();
}
/**
* Handles permission request results.
*
* Capacitor is backwards compatible such that plugins using legacy permission request codes
* may coexist with plugins using the AndroidX Activity v1.2 permission callback flow introduced
* in Capacitor 3.0.
*
* In this method, plugins are checked first for ownership of the legacy permission request code.
* If the {@link Bridge#onRequestPermissionsResult(int, String[], int[])} method indicates it has
* handled the permission, then the permission callback will be considered complete. Otherwise,
* the permission will be handled using the AndroidX Activity flow.
*
* @param requestCode the request code associated with the permission request
* @param permissions the Android permission strings requested
* @param grantResults the status result of the permission request
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (this.bridge == null) {
return;
}
if (!bridge.onRequestPermissionsResult(requestCode, permissions, grantResults)) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
/**
* Handles activity results.
*
* Capacitor is backwards compatible such that plugins using legacy activity result codes
* may coexist with plugins using the AndroidX Activity v1.2 activity callback flow introduced
* in Capacitor 3.0.
*
* In this method, plugins are checked first for ownership of the legacy request code. If the
* {@link Bridge#onActivityResult(int, int, Intent)} method indicates it has handled the activity
* result, then the callback will be considered complete. Otherwise, the result will be handled
* using the AndroidX Activiy flow.
*
* @param requestCode the request code associated with the activity result
* @param resultCode the result code
* @param data any data included with the activity result
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (this.bridge == null) {
return;
}
if (!bridge.onActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (this.bridge == null || intent == null) {
return;
}
this.bridge.onNewIntent(intent);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.bridge == null) {
return;
}
this.bridge.onConfigurationChanged(newConfig);
}
}