Skip to content

Commit 2d36dbd

Browse files
author
Sebastian Roth
committed
Split into separate MethodCallHandler class and resolve issue
1 parent e1e05bc commit 2d36dbd

File tree

2 files changed

+130
-96
lines changed

2 files changed

+130
-96
lines changed
Lines changed: 31 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,76 @@
11
package com.baseflow.googleapiavailability;
22

3-
import android.app.Activity;
43
import android.content.Context;
5-
6-
import androidx.annotation.IntDef;
7-
8-
import com.google.android.gms.common.ConnectionResult;
9-
import com.google.android.gms.common.GoogleApiAvailability;
10-
11-
import java.lang.annotation.Retention;
12-
import java.lang.annotation.RetentionPolicy;
13-
4+
import androidx.annotation.NonNull;
145
import io.flutter.embedding.engine.plugins.FlutterPlugin;
156
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
167
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
178
import io.flutter.plugin.common.BinaryMessenger;
18-
import io.flutter.plugin.common.MethodCall;
199
import io.flutter.plugin.common.MethodChannel;
20-
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
21-
import io.flutter.plugin.common.MethodChannel.Result;
2210
import io.flutter.plugin.common.PluginRegistry.Registrar;
11+
import io.flutter.plugin.common.PluginRegistry.ViewDestroyListener;
12+
import io.flutter.view.FlutterNativeView;
2313

2414
/**
2515
* GoogleApiAvailabilityPlugin
2616
*/
27-
public class GoogleApiAvailabilityPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware {
28-
private static final int REQUEST_GOOGLE_PLAY_SERVICES = 1000;
29-
30-
//GOOGLE_PLAY_SERVICES_AVAILABILITY
31-
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SUCCESS = 0;
32-
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_MISSING = 1;
33-
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_UPDATING = 2;
34-
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_VERSION_UPDATE_REQUIRED = 3;
35-
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_DISABLED = 4;
36-
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_INVALID = 5;
37-
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_NOT_AVAILABLE_ON_PLATFORM = 6;
38-
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_UNKNOWN = 7;
17+
public class GoogleApiAvailabilityPlugin implements FlutterPlugin, ActivityAware {
3918

40-
@Retention(RetentionPolicy.SOURCE)
41-
@IntDef({
42-
GOOGLE_PLAY_SERVICES_AVAILABILITY_SUCCESS,
43-
GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_MISSING,
44-
GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_UPDATING,
45-
GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_VERSION_UPDATE_REQUIRED,
46-
GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_DISABLED,
47-
GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_INVALID,
48-
GOOGLE_PLAY_SERVICES_AVAILABILITY_NOT_AVAILABLE_ON_PLATFORM,
49-
GOOGLE_PLAY_SERVICES_AVAILABILITY_UNKNOWN,
50-
})
51-
private @interface GooglePlayServicesAvailability {
52-
}
53-
54-
private Context context;
55-
56-
57-
public void setContext(Context newContext) {
58-
context = newContext;
59-
}
19+
private MethodChannel channel;
20+
private MethodCallHandlerImpl methodCallHandler;
6021

6122
@Override
6223
public void onAttachedToActivity(ActivityPluginBinding binding) {
63-
setContext(binding.getActivity());
24+
methodCallHandler.setActivity(binding.getActivity());
6425
}
6526

6627
@Override
67-
public void onDetachedFromActivityForConfigChanges() {
68-
setContext(null);
28+
public void onDetachedFromActivity() {
29+
methodCallHandler.setActivity(null);
6930
}
7031

7132
@Override
72-
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
73-
setContext(binding.getActivity());
33+
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
34+
methodCallHandler.setActivity(binding.getActivity());
7435
}
7536

7637
@Override
77-
public void onDetachedFromActivity() {
78-
setContext(null);
38+
public void onDetachedFromActivityForConfigChanges() {
39+
methodCallHandler.setActivity(null);
7940
}
8041

8142
@Override
8243
public void onAttachedToEngine(FlutterPluginBinding binding) {
83-
this.registerPlugin(null, binding.getBinaryMessenger());
44+
registerPlugin(null, binding.getBinaryMessenger());
8445
}
8546

8647
@Override
87-
public void onDetachedFromEngine(FlutterPluginBinding binding) {}
88-
89-
private void registerPlugin(Context context, BinaryMessenger messenger) {
90-
final MethodChannel channel = new MethodChannel(messenger, "flutter.baseflow.com/google_api_availability/methods");
91-
if (context != null) setContext(context);
92-
channel.setMethodCallHandler(this);
48+
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
49+
unregisterPlugin();
9350
}
9451

9552
public static void registerWith(Registrar registrar) {
9653
final GoogleApiAvailabilityPlugin plugin = new GoogleApiAvailabilityPlugin();
9754
plugin.registerPlugin(registrar.context(), registrar.messenger());
98-
}
55+
plugin.methodCallHandler.setActivity(registrar.activity());
9956

100-
@Override
101-
public void onMethodCall(MethodCall call, Result result) {
102-
if (call.method.equals("checkPlayServicesAvailability")) {
103-
final Boolean showDialog = call.argument("showDialog");
104-
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
105-
final int connectionResult = googleApiAvailability.isGooglePlayServicesAvailable(context);
106-
107-
108-
if (context instanceof Activity) {
109-
Activity activity = (Activity) context;
110-
if (showDialog != null && showDialog) {
111-
googleApiAvailability.showErrorDialogFragment(activity, connectionResult, REQUEST_GOOGLE_PLAY_SERVICES);
112-
}
57+
registrar.addViewDestroyListener(new ViewDestroyListener() {
58+
@Override
59+
public boolean onViewDestroy(FlutterNativeView view) {
60+
plugin.unregisterPlugin();
61+
return false;
11362
}
63+
});
64+
}
11465

115-
final int availability = toPlayServiceAvailability(connectionResult);
116-
result.success(availability);
117-
} else {
118-
result.notImplemented();
119-
}
66+
private void registerPlugin(Context context, BinaryMessenger messenger) {
67+
methodCallHandler = new MethodCallHandlerImpl(context);
68+
channel = new MethodChannel(messenger, "flutter.baseflow.com/google_api_availability/methods");
69+
channel.setMethodCallHandler(methodCallHandler);
12070
}
12171

122-
@GooglePlayServicesAvailability
123-
private int toPlayServiceAvailability(int connectionResult) {
124-
switch (connectionResult) {
125-
case ConnectionResult.SUCCESS:
126-
return GOOGLE_PLAY_SERVICES_AVAILABILITY_SUCCESS;
127-
case ConnectionResult.SERVICE_MISSING:
128-
return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_MISSING;
129-
case ConnectionResult.SERVICE_UPDATING:
130-
return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_UPDATING;
131-
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
132-
return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_VERSION_UPDATE_REQUIRED;
133-
case ConnectionResult.SERVICE_DISABLED:
134-
return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_DISABLED;
135-
case ConnectionResult.SERVICE_INVALID:
136-
return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_INVALID;
137-
default:
138-
return GOOGLE_PLAY_SERVICES_AVAILABILITY_UNKNOWN;
139-
}
72+
private void unregisterPlugin() {
73+
channel.setMethodCallHandler(null);
74+
channel = null;
14075
}
14176
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.baseflow.googleapiavailability;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import androidx.annotation.IntDef;
6+
import androidx.annotation.NonNull;
7+
import androidx.annotation.Nullable;
8+
import com.google.android.gms.common.ConnectionResult;
9+
import com.google.android.gms.common.GoogleApiAvailability;
10+
import io.flutter.plugin.common.MethodCall;
11+
import io.flutter.plugin.common.MethodChannel;
12+
import io.flutter.plugin.common.MethodChannel.Result;
13+
import java.lang.annotation.Retention;
14+
import java.lang.annotation.RetentionPolicy;
15+
16+
public class MethodCallHandlerImpl implements MethodChannel.MethodCallHandler {
17+
18+
public MethodCallHandlerImpl(Context applicationContext) {
19+
this.applicationContext = applicationContext;
20+
}
21+
22+
private final Context applicationContext;
23+
24+
@Nullable
25+
private Activity activity;
26+
27+
public void setActivity(@Nullable Activity activity) {
28+
this.activity = activity;
29+
}
30+
31+
private static final int REQUEST_GOOGLE_PLAY_SERVICES = 1000;
32+
33+
//GOOGLE_PLAY_SERVICES_AVAILABILITY
34+
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SUCCESS = 0;
35+
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_MISSING = 1;
36+
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_UPDATING = 2;
37+
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_VERSION_UPDATE_REQUIRED = 3;
38+
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_DISABLED = 4;
39+
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_INVALID = 5;
40+
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_NOT_AVAILABLE_ON_PLATFORM = 6;
41+
private static final int GOOGLE_PLAY_SERVICES_AVAILABILITY_UNKNOWN = 7;
42+
43+
@Retention(RetentionPolicy.SOURCE)
44+
@IntDef({
45+
GOOGLE_PLAY_SERVICES_AVAILABILITY_SUCCESS,
46+
GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_MISSING,
47+
GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_UPDATING,
48+
GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_VERSION_UPDATE_REQUIRED,
49+
GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_DISABLED,
50+
GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_INVALID,
51+
GOOGLE_PLAY_SERVICES_AVAILABILITY_NOT_AVAILABLE_ON_PLATFORM,
52+
GOOGLE_PLAY_SERVICES_AVAILABILITY_UNKNOWN,
53+
})
54+
private @interface GooglePlayServicesAvailability {
55+
56+
}
57+
58+
@Override
59+
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
60+
if (call.method.equals("checkPlayServicesAvailability")) {
61+
final Boolean showDialog = call.argument("showDialog");
62+
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
63+
final int connectionResult = googleApiAvailability
64+
.isGooglePlayServicesAvailable(applicationContext);
65+
66+
if (activity != null) {
67+
if (showDialog != null && showDialog) {
68+
googleApiAvailability
69+
.showErrorDialogFragment(activity, connectionResult, REQUEST_GOOGLE_PLAY_SERVICES);
70+
}
71+
}
72+
73+
final int availability = toPlayServiceAvailability(connectionResult);
74+
result.success(availability);
75+
} else {
76+
result.notImplemented();
77+
}
78+
}
79+
80+
@GooglePlayServicesAvailability
81+
private int toPlayServiceAvailability(int connectionResult) {
82+
switch (connectionResult) {
83+
case ConnectionResult.SUCCESS:
84+
return GOOGLE_PLAY_SERVICES_AVAILABILITY_SUCCESS;
85+
case ConnectionResult.SERVICE_MISSING:
86+
return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_MISSING;
87+
case ConnectionResult.SERVICE_UPDATING:
88+
return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_UPDATING;
89+
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
90+
return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_VERSION_UPDATE_REQUIRED;
91+
case ConnectionResult.SERVICE_DISABLED:
92+
return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_DISABLED;
93+
case ConnectionResult.SERVICE_INVALID:
94+
return GOOGLE_PLAY_SERVICES_AVAILABILITY_SERVICE_INVALID;
95+
default:
96+
return GOOGLE_PLAY_SERVICES_AVAILABILITY_UNKNOWN;
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)