@@ -85,25 +85,53 @@ public void onActivityDestroyed(Activity activity) {
8585 @ Override
8686 public void onActivityStopped (Activity activity ) {
8787 if (thisActivity == activity ) {
88- delegate .saveStateBeforeResult ();
88+ activityState . getDelegate () .saveStateBeforeResult ();
8989 }
9090 }
9191 }
9292
93- private class ActivityState {
93+ private class ActivityBindingState {
94+ private final Application application ;
9495 private final Activity activity ;
96+ private final ImagePickerDelegate delegate ;
97+ private final MethodChannel channel ;
9598 private final LifeCycleObserver observer ;
9699
97- ActivityState (final Activity activity ) {
100+ ActivityBindingState (
101+ final Application application , final Activity activity , final BinaryMessenger messenger ) {
102+ this .application = application ;
98103 this .activity = activity ;
104+
105+ channel = new MethodChannel (messenger , CHANNEL );
106+ delegate = constructDelegate (activity );
99107 observer = new LifeCycleObserver (activity );
100108 }
101109
102- final Activity getActivity () {
110+ ActivityBindingState (final Activity activity , final ImagePickerDelegate delegate ) {
111+ this .activity = activity ;
112+ this .delegate = delegate ;
113+ application = null ;
114+ channel = null ;
115+ observer = null ;
116+ }
117+
118+ Application getApplication () {
119+ return application ;
120+ }
121+
122+ Activity getActivity () {
103123 return activity ;
104124 }
105125
106- final LifeCycleObserver getObserver () {
126+ ImagePickerDelegate getDelegate () {
127+ return delegate ;
128+ }
129+
130+ MethodChannel getChannel () {
131+ return channel ;
132+ }
133+
134+ LifeCycleObserver getObserver () {
107135 return observer ;
108136 }
109137 }
@@ -119,14 +147,11 @@ final LifeCycleObserver getObserver() {
119147 private static final int SOURCE_CAMERA = 0 ;
120148 private static final int SOURCE_GALLERY = 1 ;
121149
122- private MethodChannel channel ;
123- private ImagePickerDelegate delegate ;
150+ private ActivityBindingState activityState ;
124151 private FlutterPluginBinding pluginBinding ;
125152 private ActivityPluginBinding activityBinding ;
126- private Application application ;
127153 // This is null when not using v2 embedding;
128154 private Lifecycle lifecycle ;
129- private ActivityState activityState ;
130155
131156 @ SuppressWarnings ("deprecation" )
132157 public static void registerWith (io .flutter .plugin .common .PluginRegistry .Registrar registrar ) {
@@ -154,8 +179,7 @@ public ImagePickerPlugin() {}
154179
155180 @ VisibleForTesting
156181 ImagePickerPlugin (final ImagePickerDelegate delegate , final Activity activity ) {
157- this .delegate = delegate ;
158- activityState = new ActivityState (activity );
182+ activityState = new ActivityBindingState (activity , delegate );
159183 }
160184
161185 @ Override
@@ -200,36 +224,36 @@ private void setup(
200224 final Activity activity ,
201225 final PluginRegistry .Registrar registrar ,
202226 final ActivityPluginBinding activityBinding ) {
203- this .application = application ;
204- delegate = constructDelegate (activity );
205- activityState = new ActivityState (activity );
206- channel = new MethodChannel (messenger , CHANNEL );
207- channel .setMethodCallHandler (this );
227+ activityState = new ActivityBindingState (application , activity , messenger );
228+ activityState .getChannel ().setMethodCallHandler (this );
208229 if (registrar != null ) {
209230 // V1 embedding setup for activity listeners.
210- application .registerActivityLifecycleCallbacks (activityState .getObserver ());
211- registrar .addActivityResultListener (delegate );
212- registrar .addRequestPermissionsResultListener (delegate );
231+ activityState
232+ .getApplication ()
233+ .registerActivityLifecycleCallbacks (activityState .getObserver ());
234+ registrar .addActivityResultListener (activityState .getDelegate ());
235+ registrar .addRequestPermissionsResultListener (activityState .getDelegate ());
213236 } else {
214237 // V2 embedding setup for activity listeners.
215- activityBinding .addActivityResultListener (delegate );
216- activityBinding .addRequestPermissionsResultListener (delegate );
238+ activityBinding .addActivityResultListener (activityState . getDelegate () );
239+ activityBinding .addRequestPermissionsResultListener (activityState . getDelegate () );
217240 lifecycle = FlutterLifecycleAdapter .getActivityLifecycle (activityBinding );
218241 lifecycle .addObserver (activityState .getObserver ());
219242 }
220243 }
221244
222245 private void tearDown () {
223- activityBinding .removeActivityResultListener (delegate );
224- activityBinding .removeRequestPermissionsResultListener (delegate );
246+ activityBinding .removeActivityResultListener (activityState . getDelegate () );
247+ activityBinding .removeRequestPermissionsResultListener (activityState . getDelegate () );
225248 activityBinding = null ;
226- lifecycle .removeObserver (activityState .getObserver ());
227- lifecycle = null ;
228- delegate = null ;
229- channel .setMethodCallHandler (null );
230- channel = null ;
231- application .unregisterActivityLifecycleCallbacks (activityState .getObserver ());
232- application = null ;
249+ if (lifecycle != null ) {
250+ lifecycle .removeObserver (activityState .getObserver ());
251+ lifecycle = null ;
252+ }
253+ activityState .getChannel ().setMethodCallHandler (null );
254+ activityState
255+ .getApplication ()
256+ .unregisterActivityLifecycleCallbacks (activityState .getObserver ());
233257 activityState = null ;
234258 }
235259
@@ -244,7 +268,7 @@ final ImagePickerDelegate constructDelegate(final Activity setupActivity) {
244268 }
245269
246270 @ VisibleForTesting
247- final ActivityState getActivityState () {
271+ final ActivityBindingState getActivityBindingState () {
248272 return activityState ;
249273 }
250274
@@ -309,40 +333,40 @@ public void onMethodCall(MethodCall call, MethodChannel.Result rawResult) {
309333 } else {
310334 device = CameraDevice .REAR ;
311335 }
312- delegate .setCameraDevice (device );
336+ activityState . getDelegate () .setCameraDevice (device );
313337 }
314338 switch (call .method ) {
315339 case METHOD_CALL_IMAGE :
316340 imageSource = call .argument ("source" );
317341 switch (imageSource ) {
318342 case SOURCE_GALLERY :
319- delegate .chooseImageFromGallery (call , result );
343+ activityState . getDelegate () .chooseImageFromGallery (call , result );
320344 break ;
321345 case SOURCE_CAMERA :
322- delegate .takeImageWithCamera (call , result );
346+ activityState . getDelegate () .takeImageWithCamera (call , result );
323347 break ;
324348 default :
325349 throw new IllegalArgumentException ("Invalid image source: " + imageSource );
326350 }
327351 break ;
328352 case METHOD_CALL_MULTI_IMAGE :
329- delegate .chooseMultiImageFromGallery (call , result );
353+ activityState . getDelegate () .chooseMultiImageFromGallery (call , result );
330354 break ;
331355 case METHOD_CALL_VIDEO :
332356 imageSource = call .argument ("source" );
333357 switch (imageSource ) {
334358 case SOURCE_GALLERY :
335- delegate .chooseVideoFromGallery (call , result );
359+ activityState . getDelegate () .chooseVideoFromGallery (call , result );
336360 break ;
337361 case SOURCE_CAMERA :
338- delegate .takeVideoWithCamera (call , result );
362+ activityState . getDelegate () .takeVideoWithCamera (call , result );
339363 break ;
340364 default :
341365 throw new IllegalArgumentException ("Invalid video source: " + imageSource );
342366 }
343367 break ;
344368 case METHOD_CALL_RETRIEVE :
345- delegate .retrieveLostImage (result );
369+ activityState . getDelegate () .retrieveLostImage (result );
346370 break ;
347371 default :
348372 throw new IllegalArgumentException ("Unknown method " + call .method );
0 commit comments