|
16 | 16 |
|
17 | 17 | package com.huawei.hms.flutter.site; |
18 | 18 |
|
| 19 | +import android.app.Activity; |
19 | 20 | import android.content.Context; |
20 | 21 | import android.net.Uri; |
| 22 | + |
21 | 23 | import androidx.annotation.NonNull; |
| 24 | + |
22 | 25 | import com.huawei.agconnect.config.AGConnectServicesConfig; |
23 | | -import com.huawei.hms.flutter.site.handlers.MethodCallHandlerImpl; |
| 26 | +import com.huawei.hms.flutter.site.constants.Channel; |
| 27 | +import com.huawei.hms.flutter.site.handlers.ActivityAwareMethodCallHandlerImpl; |
24 | 28 | import com.huawei.hms.site.api.SearchService; |
25 | 29 | import com.huawei.hms.site.api.SearchServiceFactory; |
26 | 30 |
|
27 | | -import com.google.gson.Gson; |
28 | | - |
29 | 31 | import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 32 | +import io.flutter.embedding.engine.plugins.activity.ActivityAware; |
| 33 | +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; |
30 | 34 | import io.flutter.plugin.common.BinaryMessenger; |
31 | 35 | import io.flutter.plugin.common.MethodChannel; |
32 | 36 | import io.flutter.plugin.common.PluginRegistry.Registrar; |
33 | 37 |
|
34 | | -public class SitePlugin implements FlutterPlugin { |
35 | | - private Gson mGson; |
36 | | - private MethodChannel mMethodChannel; |
37 | | - private SearchService mSearchService; |
38 | | - private MethodChannel.MethodCallHandler mMethodCallHandler; |
| 38 | +public class SitePlugin implements FlutterPlugin, ActivityAware { |
| 39 | + private SearchService searchService; |
| 40 | + private MethodChannel methodChannel; |
| 41 | + private ActivityAwareMethodCallHandlerImpl methodCallHandler; |
39 | 42 |
|
40 | | - public static void registerWith(Registrar registrar) { |
| 43 | + public static void registerWith(final Registrar registrar) { |
41 | 44 | final SitePlugin instance = new SitePlugin(); |
| 45 | + final Activity activity = registrar.activity(); |
| 46 | + final Context context = registrar.context(); |
| 47 | + |
| 48 | + // When context is available |
| 49 | + // searchService and methodChannel are instantiated |
42 | 50 | instance.onAttachedToEngine(registrar.context(), registrar.messenger()); |
| 51 | + |
| 52 | + // When activity is available |
| 53 | + // methodCallHandler is instantiated |
| 54 | + instance.methodCallHandler = new ActivityAwareMethodCallHandlerImpl(activity, instance.searchService, |
| 55 | + instance.getApiKey(context)); |
| 56 | + instance.methodChannel.setMethodCallHandler(instance.methodCallHandler); |
| 57 | + } |
| 58 | + |
| 59 | + private String getApiKey(Context context) { |
| 60 | + final String rawApiKey = AGConnectServicesConfig.fromContext(context).getString("client/api_key"); |
| 61 | + return Uri.encode(rawApiKey); |
| 62 | + } |
| 63 | + |
| 64 | + private void onAttachedToEngine(final Context context, final BinaryMessenger messenger) { |
| 65 | + searchService = SearchServiceFactory.create(context, getApiKey(context)); |
| 66 | + methodChannel = new MethodChannel(messenger, Channel.METHOD_SEARCH_SERVICE); |
| 67 | + } |
| 68 | + |
| 69 | + private void onDetachedFromEngine() { |
| 70 | + searchService = null; |
| 71 | + methodChannel = null; |
43 | 72 | } |
44 | 73 |
|
45 | 74 | @Override |
46 | | - public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { |
| 75 | + public void onAttachedToEngine(@NonNull final FlutterPluginBinding binding) { |
47 | 76 | onAttachedToEngine(binding.getApplicationContext(), binding.getBinaryMessenger()); |
48 | 77 | } |
49 | 78 |
|
50 | 79 | @Override |
51 | | - public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { |
52 | | - mMethodChannel.setMethodCallHandler(null); |
53 | | - mMethodCallHandler = null; |
54 | | - mMethodChannel = null; |
55 | | - mSearchService = null; |
56 | | - mGson = null; |
| 80 | + public void onDetachedFromEngine(@NonNull final FlutterPluginBinding binding) { |
| 81 | + onDetachedFromEngine(); |
57 | 82 | } |
58 | 83 |
|
59 | | - private void onAttachedToEngine(Context context, BinaryMessenger messenger) { |
60 | | - final String apiKey = AGConnectServicesConfig.fromContext(context).getString("client/api_key"); |
61 | | - final String encodedApiKey = Uri.encode(apiKey); |
62 | | - mGson = new Gson(); |
63 | | - mSearchService = SearchServiceFactory.create(context, encodedApiKey); |
64 | | - mMethodChannel = new MethodChannel(messenger, "com.huawei.hms.flutter.site/method"); |
65 | | - mMethodCallHandler = new MethodCallHandlerImpl(mSearchService, mGson); |
66 | | - mMethodChannel.setMethodCallHandler(mMethodCallHandler); |
| 84 | + @Override |
| 85 | + public void onAttachedToActivity(@NonNull final ActivityPluginBinding binding) { |
| 86 | + final Activity activity = binding.getActivity(); |
| 87 | + methodCallHandler = new ActivityAwareMethodCallHandlerImpl(activity, searchService, |
| 88 | + getApiKey(activity.getApplicationContext())); |
| 89 | + methodChannel.setMethodCallHandler(methodCallHandler); |
| 90 | + binding.addActivityResultListener(methodCallHandler); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onDetachedFromActivityForConfigChanges() { |
| 95 | + onDetachedFromActivity(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onReattachedToActivityForConfigChanges(@NonNull final ActivityPluginBinding binding) { |
| 100 | + onAttachedToActivity(binding); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onDetachedFromActivity() { |
| 105 | + methodChannel.setMethodCallHandler(null); |
| 106 | + methodCallHandler = null; |
67 | 107 | } |
68 | 108 | } |
0 commit comments