-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[camerax] Add LifecycleOwner
Proxy
#3837
Changes from 6 commits
ae4fa27
a2807ce
cfc5b09
3afd604
c38daf5
f19a142
b89c334
ae82048
4a93dbd
7df2c54
7aeecac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
package io.flutter.plugins.camerax; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import androidx.annotation.NonNull; | ||
import androidx.lifecycle.LifecycleOwner; | ||
|
@@ -79,9 +80,17 @@ public void onAttachedToActivity(@NonNull ActivityPluginBinding activityPluginBi | |
pluginBinding.getApplicationContext(), | ||
pluginBinding.getTextureRegistry()); | ||
updateContext(pluginBinding.getApplicationContext()); | ||
processCameraProviderHostApi.setLifecycleOwner( | ||
(LifecycleOwner) activityPluginBinding.getActivity()); | ||
systemServicesHostApi.setActivity(activityPluginBinding.getActivity()); | ||
|
||
Activity activity = activityPluginBinding.getActivity(); | ||
|
||
if (activity instanceof LifecycleOwner) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, now that I think about it. This probably isn't guaranteed in an Add-to-app scenario. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I think that's why the cast wasn't safe. |
||
processCameraProviderHostApi.setLifecycleOwner((LifecycleOwner) activity); | ||
} else { | ||
ProxyLifecycleProvider proxyLifecycleProvider = new ProxyLifecycleProvider(activity); | ||
processCameraProviderHostApi.setLifecycleOwner(proxyLifecycleProvider); | ||
} | ||
|
||
systemServicesHostApi.setActivity(activity); | ||
systemServicesHostApi.setPermissionsRegistry( | ||
activityPluginBinding::addRequestPermissionsResultListener); | ||
} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non blocking: Considering this is at least the second plugin to need this style code consider either including the class in the framework or publishing it as its own package. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will include this consideration in my umbrella issue for plugin cleanup. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2013 The Flutter 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.camerax; | ||
|
||
import android.app.Activity; | ||
import android.app.Application.ActivityLifecycleCallbacks; | ||
import android.os.Bundle; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.VisibleForTesting; | ||
import androidx.lifecycle.Lifecycle; | ||
import androidx.lifecycle.Lifecycle.Event; | ||
import androidx.lifecycle.LifecycleOwner; | ||
import androidx.lifecycle.LifecycleRegistry; | ||
|
||
/** | ||
* This class provides a custom {@link LifecycleOwner} for the activity driven by {@link | ||
* ActivityLifecycleCallbacks}. | ||
* | ||
* <p>This is used in the case where a direct {@link LifecycleOwner} is not available. | ||
*/ | ||
public class ProxyLifecycleProvider implements ActivityLifecycleCallbacks, LifecycleOwner { | ||
|
||
@VisibleForTesting @NonNull public LifecycleRegistry lifecycle = new LifecycleRegistry(this); | ||
private final int registrarActivityHashCode; | ||
|
||
ProxyLifecycleProvider(@NonNull Activity activity) { | ||
this.registrarActivityHashCode = activity.hashCode(); | ||
activity.getApplication().registerActivityLifecycleCallbacks(this); | ||
} | ||
|
||
@Override | ||
public void onActivityCreated(@NonNull Activity activity, @NonNull Bundle savedInstanceState) { | ||
if (activity.hashCode() != registrarActivityHashCode) { | ||
return; | ||
} | ||
lifecycle.handleLifecycleEvent(Event.ON_CREATE); | ||
} | ||
|
||
@Override | ||
public void onActivityStarted(@NonNull Activity activity) { | ||
if (activity.hashCode() != registrarActivityHashCode) { | ||
return; | ||
} | ||
lifecycle.handleLifecycleEvent(Event.ON_START); | ||
} | ||
|
||
@Override | ||
public void onActivityResumed(@NonNull Activity activity) { | ||
if (activity.hashCode() != registrarActivityHashCode) { | ||
return; | ||
} | ||
lifecycle.handleLifecycleEvent(Event.ON_RESUME); | ||
} | ||
|
||
@Override | ||
public void onActivityPaused(@NonNull Activity activity) { | ||
if (activity.hashCode() != registrarActivityHashCode) { | ||
return; | ||
} | ||
lifecycle.handleLifecycleEvent(Event.ON_PAUSE); | ||
} | ||
|
||
@Override | ||
public void onActivityStopped(@NonNull Activity activity) { | ||
if (activity.hashCode() != registrarActivityHashCode) { | ||
return; | ||
} | ||
lifecycle.handleLifecycleEvent(Event.ON_STOP); | ||
} | ||
|
||
@Override | ||
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {} | ||
|
||
@Override | ||
public void onActivityDestroyed(@NonNull Activity activity) { | ||
if (activity.hashCode() != registrarActivityHashCode) { | ||
return; | ||
} | ||
activity.getApplication().unregisterActivityLifecycleCallbacks(this); | ||
lifecycle.handleLifecycleEvent(Event.ON_DESTROY); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Lifecycle getLifecycle() { | ||
return lifecycle; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2013 The Flutter 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.camerax; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import android.app.Activity; | ||
import android.app.Application; | ||
import android.os.Bundle; | ||
import androidx.lifecycle.Lifecycle.Event; | ||
import androidx.lifecycle.LifecycleRegistry; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
|
||
public class ProxyLifecycleProviderTest { | ||
@Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); | ||
|
||
@Mock Activity activity; | ||
@Mock Application application; | ||
@Mock LifecycleRegistry mockLifecycleRegistry; | ||
|
||
private final int testHashCode = 27; | ||
|
||
@Before | ||
public void setUp() { | ||
when(activity.getApplication()).thenReturn(application); | ||
} | ||
|
||
@Test | ||
public void onActivityCreated_handlesOnCreateEvent() { | ||
ProxyLifecycleProvider proxyLifecycleProvider = new ProxyLifecycleProvider(activity); | ||
Bundle mockBundle = mock(Bundle.class); | ||
|
||
proxyLifecycleProvider.lifecycle = mockLifecycleRegistry; | ||
|
||
proxyLifecycleProvider.onActivityCreated(activity, mockBundle); | ||
|
||
verify(mockLifecycleRegistry).handleLifecycleEvent(Event.ON_CREATE); | ||
} | ||
|
||
@Test | ||
public void onActivityStarted_handlesOnActivityStartedEvent() { | ||
ProxyLifecycleProvider proxyLifecycleProvider = new ProxyLifecycleProvider(activity); | ||
proxyLifecycleProvider.lifecycle = mockLifecycleRegistry; | ||
|
||
proxyLifecycleProvider.onActivityStarted(activity); | ||
|
||
verify(mockLifecycleRegistry).handleLifecycleEvent(Event.ON_START); | ||
} | ||
|
||
@Test | ||
public void onActivityResumed_handlesOnActivityResumedEvent() { | ||
ProxyLifecycleProvider proxyLifecycleProvider = new ProxyLifecycleProvider(activity); | ||
proxyLifecycleProvider.lifecycle = mockLifecycleRegistry; | ||
|
||
proxyLifecycleProvider.onActivityResumed(activity); | ||
|
||
verify(mockLifecycleRegistry).handleLifecycleEvent(Event.ON_RESUME); | ||
} | ||
|
||
@Test | ||
public void onActivityPaused_handlesOnActivityPausedEvent() { | ||
ProxyLifecycleProvider proxyLifecycleProvider = new ProxyLifecycleProvider(activity); | ||
proxyLifecycleProvider.lifecycle = mockLifecycleRegistry; | ||
|
||
proxyLifecycleProvider.onActivityPaused(activity); | ||
|
||
verify(mockLifecycleRegistry).handleLifecycleEvent(Event.ON_PAUSE); | ||
} | ||
|
||
@Test | ||
public void onActivityStopped_handlesOnActivityStoppedEvent() { | ||
ProxyLifecycleProvider proxyLifecycleProvider = new ProxyLifecycleProvider(activity); | ||
proxyLifecycleProvider.lifecycle = mockLifecycleRegistry; | ||
|
||
proxyLifecycleProvider.onActivityStopped(activity); | ||
|
||
verify(mockLifecycleRegistry).handleLifecycleEvent(Event.ON_STOP); | ||
} | ||
|
||
@Test | ||
public void onActivityDestroyed_handlesOnActivityDestroyed() { | ||
ProxyLifecycleProvider proxyLifecycleProvider = new ProxyLifecycleProvider(activity); | ||
proxyLifecycleProvider.lifecycle = mockLifecycleRegistry; | ||
|
||
proxyLifecycleProvider.onActivityDestroyed(activity); | ||
|
||
verify(mockLifecycleRegistry).handleLifecycleEvent(Event.ON_DESTROY); | ||
} | ||
|
||
@Test | ||
public void onActivitySaveInstanceState_doesNotHandleLifecycleEvvent() { | ||
ProxyLifecycleProvider proxyLifecycleProvider = new ProxyLifecycleProvider(activity); | ||
Bundle mockBundle = mock(Bundle.class); | ||
|
||
proxyLifecycleProvider.lifecycle = mockLifecycleRegistry; | ||
|
||
proxyLifecycleProvider.onActivitySaveInstanceState(activity, mockBundle); | ||
|
||
verifyNoInteractions(mockLifecycleRegistry); | ||
} | ||
|
||
@Test | ||
public void getLifecycle_returnsExpectedLifecycle() { | ||
ProxyLifecycleProvider proxyLifecycleProvider = new ProxyLifecycleProvider(activity); | ||
|
||
proxyLifecycleProvider.lifecycle = mockLifecycleRegistry; | ||
|
||
assertEquals(proxyLifecycleProvider.getLifecycle(), mockLifecycleRegistry); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,18 @@ | |
import 'package:camera_android_camerax/src/camera.dart'; | ||
import 'package:camera_android_camerax/src/instance_manager.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/annotations.dart'; | ||
|
||
import 'camera_test.mocks.dart'; | ||
import 'test_camerax_library.g.dart'; | ||
|
||
@GenerateMocks(<Type>[TestInstanceManagerHostApi]) | ||
void main() { | ||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
// Mocks the call to clear the native InstanceManager. | ||
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated fix for failing Dart test. |
||
|
||
group('Camera', () { | ||
test('flutterApiCreateTest', () { | ||
final InstanceManager instanceManager = InstanceManager( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Mocks generated by Mockito 5.4.0 from annotations | ||
// in camera_android_camerax/test/camera_test.dart. | ||
// Do not manually edit this file. | ||
|
||
// ignore_for_file: no_leading_underscores_for_library_prefixes | ||
import 'package:mockito/mockito.dart' as _i1; | ||
|
||
import 'test_camerax_library.g.dart' as _i2; | ||
|
||
// ignore_for_file: type=lint | ||
// ignore_for_file: avoid_redundant_argument_values | ||
// ignore_for_file: avoid_setters_without_getters | ||
// ignore_for_file: comment_references | ||
// ignore_for_file: implementation_imports | ||
// ignore_for_file: invalid_use_of_visible_for_testing_member | ||
// ignore_for_file: prefer_const_constructors | ||
// ignore_for_file: unnecessary_parenthesis | ||
// ignore_for_file: camel_case_types | ||
// ignore_for_file: subtype_of_sealed_class | ||
|
||
/// A class which mocks [TestInstanceManagerHostApi]. | ||
/// | ||
/// See the documentation for Mockito's code generation for more information. | ||
class MockTestInstanceManagerHostApi extends _i1.Mock | ||
implements _i2.TestInstanceManagerHostApi { | ||
MockTestInstanceManagerHostApi() { | ||
_i1.throwOnMissingStub(this); | ||
} | ||
|
||
@override | ||
void clear() => super.noSuchMethod( | ||
Invocation.method( | ||
#clear, | ||
[], | ||
), | ||
returnValueForMissingStub: null, | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should add a test for this file to make sure the fallback works.