| // Copyright 2018 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package io.flutter.plugins.googlemaps; |
| |
| import android.app.Activity; |
| import android.app.Application; |
| import android.os.Bundle; |
| import io.flutter.plugin.common.PluginRegistry.Registrar; |
| import java.util.concurrent.atomic.AtomicInteger; |
| |
| /** |
| * Plugin for controlling a set of GoogleMap views to be shown as overlays on top of the Flutter |
| * view. The overlay should be hidden during transformations or while Flutter is rendering on top of |
| * the map. A Texture drawn using GoogleMap bitmap snapshots can then be shown instead of the |
| * overlay. |
| */ |
| public class GoogleMapsPlugin implements Application.ActivityLifecycleCallbacks { |
| static final int CREATED = 1; |
| static final int STARTED = 2; |
| static final int RESUMED = 3; |
| static final int PAUSED = 4; |
| static final int STOPPED = 5; |
| static final int DESTROYED = 6; |
| private final AtomicInteger state = new AtomicInteger(0); |
| private final int registrarActivityHashCode; |
| |
| public static void registerWith(Registrar registrar) { |
| if (registrar.activity() == null) { |
| // When a background flutter view tries to register the plugin, the registrar has no activity. |
| // We stop the registration process as this plugin is foreground only. |
| return; |
| } |
| final GoogleMapsPlugin plugin = new GoogleMapsPlugin(registrar); |
| registrar.activity().getApplication().registerActivityLifecycleCallbacks(plugin); |
| registrar |
| .platformViewRegistry() |
| .registerViewFactory( |
| "plugins.flutter.io/google_maps", new GoogleMapFactory(plugin.state, registrar)); |
| } |
| |
| @Override |
| public void onActivityCreated(Activity activity, Bundle savedInstanceState) { |
| if (activity.hashCode() != registrarActivityHashCode) { |
| return; |
| } |
| state.set(CREATED); |
| } |
| |
| @Override |
| public void onActivityStarted(Activity activity) { |
| if (activity.hashCode() != registrarActivityHashCode) { |
| return; |
| } |
| state.set(STARTED); |
| } |
| |
| @Override |
| public void onActivityResumed(Activity activity) { |
| if (activity.hashCode() != registrarActivityHashCode) { |
| return; |
| } |
| state.set(RESUMED); |
| } |
| |
| @Override |
| public void onActivityPaused(Activity activity) { |
| if (activity.hashCode() != registrarActivityHashCode) { |
| return; |
| } |
| state.set(PAUSED); |
| } |
| |
| @Override |
| public void onActivityStopped(Activity activity) { |
| if (activity.hashCode() != registrarActivityHashCode) { |
| return; |
| } |
| state.set(STOPPED); |
| } |
| |
| @Override |
| public void onActivitySaveInstanceState(Activity activity, Bundle outState) {} |
| |
| @Override |
| public void onActivityDestroyed(Activity activity) { |
| if (activity.hashCode() != registrarActivityHashCode) { |
| return; |
| } |
| activity.getApplication().unregisterActivityLifecycleCallbacks(this); |
| state.set(DESTROYED); |
| } |
| |
| private GoogleMapsPlugin(Registrar registrar) { |
| this.registrarActivityHashCode = registrar.activity().hashCode(); |
| } |
| } |