Skip to content

Commit a22f12a

Browse files
authored
Merge pull request Baseflow#16 from Raphicci/develop
Android embedding V2
2 parents f2ebca0 + 5e32218 commit a22f12a

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

android/src/main/java/com/baseflow/googleapiavailability/GoogleApiAvailabilityPlugin.java

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
import android.app.Activity;
44
import android.content.Context;
55

6+
import androidx.annotation.IntDef;
7+
68
import com.google.android.gms.common.ConnectionResult;
79
import com.google.android.gms.common.GoogleApiAvailability;
810

911
import java.lang.annotation.Retention;
1012
import java.lang.annotation.RetentionPolicy;
1113

12-
import androidx.annotation.IntDef;
14+
import io.flutter.embedding.engine.plugins.FlutterPlugin;
15+
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
16+
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
17+
import io.flutter.plugin.common.BinaryMessenger;
1318
import io.flutter.plugin.common.MethodCall;
1419
import io.flutter.plugin.common.MethodChannel;
1520
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
@@ -19,7 +24,7 @@
1924
/**
2025
* GoogleApiAvailabilityPlugin
2126
*/
22-
public class GoogleApiAvailabilityPlugin implements MethodCallHandler {
27+
public class GoogleApiAvailabilityPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware {
2328
private static final int REQUEST_GOOGLE_PLAY_SERVICES = 1000;
2429

2530
//GOOGLE_PLAY_SERVICES_AVAILABILITY
@@ -46,15 +51,50 @@ public class GoogleApiAvailabilityPlugin implements MethodCallHandler {
4651
private @interface GooglePlayServicesAvailability {
4752
}
4853

49-
public static void registerWith(Registrar registrar) {
50-
final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter.baseflow.com/google_api_availability/methods");
51-
channel.setMethodCallHandler(new GoogleApiAvailabilityPlugin(registrar.context()));
54+
private Context context;
55+
56+
57+
public void setContext(Context newContext) {
58+
context = newContext;
59+
}
60+
61+
@Override
62+
public void onAttachedToActivity(ActivityPluginBinding binding) {
63+
setContext(binding.getActivity());
64+
}
65+
66+
@Override
67+
public void onDetachedFromActivityForConfigChanges() {
68+
setContext(null);
69+
}
70+
71+
@Override
72+
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
73+
setContext(binding.getActivity());
74+
}
75+
76+
@Override
77+
public void onDetachedFromActivity() {
78+
setContext(null);
79+
}
80+
81+
@Override
82+
public void onAttachedToEngine(FlutterPluginBinding binding) {
83+
this.registerPlugin(null, binding.getBinaryMessenger());
5284
}
5385

54-
private final Context context;
86+
@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);
93+
}
5594

56-
private GoogleApiAvailabilityPlugin(Context context) {
57-
this.context = context;
95+
public static void registerWith(Registrar registrar) {
96+
final GoogleApiAvailabilityPlugin plugin = new GoogleApiAvailabilityPlugin();
97+
plugin.registerPlugin(registrar.context(), registrar.messenger());
5898
}
5999

60100
@Override
@@ -98,4 +138,4 @@ private int toPlayServiceAvailability(int connectionResult) {
98138
return GOOGLE_PLAY_SERVICES_AVAILABILITY_UNKNOWN;
99139
}
100140
}
101-
}
141+
}

example/android/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
android:label="google_api_availability_example"
99
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
1010
<activity
11-
android:name=".MainActivity"
11+
android:name="io.flutter.embedding.android.FlutterActivity"
1212
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
1313
android:hardwareAccelerated="true"
1414
android:launchMode="singleTop"
1515
android:theme="@style/LaunchTheme"
1616
android:windowSoftInputMode="adjustResize">
17+
<meta-data android:name="flutterEmbedding" android:value="2"/>
1718
<!-- This keeps the window background of the activity showing
1819
until Flutter renders its first frame. It can be removed if
1920
there is no splash screen (such as the default splash screen

example/android/app/src/main/java/com/baseflow/googleapiavailabilityexample/MainActivity.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)