-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In Camera1 we now try to split parameters into individual sub-maps of…
… size 1 in case if updating all parameters at once fails.
- Loading branch information
1 parent
82501be
commit b25fadd
Showing
9 changed files
with
325 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
fotoapparat/src/main/java/io/fotoapparat/hardware/v1/parameters/SplitParametersOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.fotoapparat.hardware.v1.parameters; | ||
|
||
import io.fotoapparat.hardware.operators.ParametersOperator; | ||
import io.fotoapparat.parameter.Parameters; | ||
|
||
/** | ||
* Instead of updating all parameters at once, updates each parameter one by one. | ||
*/ | ||
public class SplitParametersOperator implements ParametersOperator { | ||
|
||
private final ParametersOperator wrapped; | ||
|
||
public SplitParametersOperator(ParametersOperator wrapped) { | ||
this.wrapped = wrapped; | ||
} | ||
|
||
@Override | ||
public void updateParameters(Parameters parameters) { | ||
for (Parameters.Type type : parameters.storedTypes()) { | ||
wrapped.updateParameters( | ||
newParameters(type, parameters.getValue(type)) | ||
); | ||
} | ||
} | ||
|
||
private Parameters newParameters(Parameters.Type type, Object value) { | ||
Parameters parameters = new Parameters(); | ||
parameters.putValue(type, value); | ||
|
||
return parameters; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
.../main/java/io/fotoapparat/hardware/v1/parameters/SupressExceptionsParametersOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.fotoapparat.hardware.v1.parameters; | ||
|
||
import io.fotoapparat.hardware.operators.ParametersOperator; | ||
import io.fotoapparat.log.Logger; | ||
import io.fotoapparat.parameter.Parameters; | ||
|
||
/** | ||
* Tries to update parameters. If that fails, suppresses the exception and writes relevant | ||
* information to log. | ||
*/ | ||
public class SupressExceptionsParametersOperator implements ParametersOperator { | ||
|
||
private final ParametersOperator wrapped; | ||
private final Logger logger; | ||
|
||
public SupressExceptionsParametersOperator(ParametersOperator wrapped, | ||
Logger logger) { | ||
this.wrapped = wrapped; | ||
this.logger = logger; | ||
} | ||
|
||
@Override | ||
public void updateParameters(Parameters parameters) { | ||
try { | ||
wrapped.updateParameters(parameters); | ||
} catch (Exception e) { | ||
logger.log("Unable to set parameters: " + parameters); | ||
} | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...rc/main/java/io/fotoapparat/hardware/v1/parameters/SwitchOnFailureParametersOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.fotoapparat.hardware.v1.parameters; | ||
|
||
import io.fotoapparat.hardware.operators.ParametersOperator; | ||
import io.fotoapparat.parameter.Parameters; | ||
|
||
/** | ||
* Tries to execute first operator. If that fails, tries to execute the second one. | ||
*/ | ||
public class SwitchOnFailureParametersOperator implements ParametersOperator { | ||
|
||
private final ParametersOperator first; | ||
private final ParametersOperator second; | ||
|
||
public SwitchOnFailureParametersOperator(ParametersOperator first, | ||
ParametersOperator second) { | ||
this.first = first; | ||
this.second = second; | ||
} | ||
|
||
@Override | ||
public void updateParameters(Parameters parameters) { | ||
try { | ||
first.updateParameters(parameters); | ||
} catch (Exception e) { | ||
second.updateParameters(parameters); | ||
} | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...apparat/src/main/java/io/fotoapparat/hardware/v1/parameters/UnsafeParametersOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.fotoapparat.hardware.v1.parameters; | ||
|
||
import android.hardware.Camera; | ||
|
||
import io.fotoapparat.hardware.operators.ParametersOperator; | ||
import io.fotoapparat.hardware.v1.ParametersConverter; | ||
import io.fotoapparat.parameter.Parameters; | ||
|
||
/** | ||
* Updates parameters of camera, possibly throwing a {@link RuntimeException} if something goes | ||
* wrong. We can't really know why camera might reject parameters, so it should be expected. | ||
*/ | ||
@SuppressWarnings("deprecation") | ||
public class UnsafeParametersOperator implements ParametersOperator { | ||
|
||
private final Camera camera; | ||
private final ParametersConverter parametersConverter; | ||
|
||
public UnsafeParametersOperator(Camera camera, | ||
ParametersConverter parametersConverter) { | ||
this.camera = camera; | ||
this.parametersConverter = parametersConverter; | ||
} | ||
|
||
@Override | ||
public void updateParameters(Parameters parameters) { | ||
Camera.Parameters cameraParameters = parametersConverter.convert( | ||
parameters, | ||
camera.getParameters() | ||
); | ||
|
||
camera.setParameters(cameraParameters); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...arat/src/test/java/io/fotoapparat/hardware/v1/parameters/SplitParametersOperatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.fotoapparat.hardware.v1.parameters; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import io.fotoapparat.hardware.operators.ParametersOperator; | ||
import io.fotoapparat.parameter.Parameters; | ||
import io.fotoapparat.parameter.Size; | ||
|
||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class SplitParametersOperatorTest { | ||
|
||
static final Size PICTURE_SIZE = new Size(100, 100); | ||
static final Size PREVIEW_SIZE = new Size(50, 50); | ||
|
||
@Mock | ||
ParametersOperator wrapped; | ||
|
||
@InjectMocks | ||
SplitParametersOperator testee; | ||
|
||
@Test | ||
public void updateParameters() throws Exception { | ||
// Given | ||
Parameters parameters = new Parameters(); | ||
parameters.putValue(Parameters.Type.PICTURE_SIZE, PICTURE_SIZE); | ||
parameters.putValue(Parameters.Type.PREVIEW_SIZE, PREVIEW_SIZE); | ||
|
||
// When | ||
testee.updateParameters(parameters); | ||
|
||
// Then | ||
verify(wrapped).updateParameters( | ||
parametersWithJust(Parameters.Type.PICTURE_SIZE, PICTURE_SIZE) | ||
); | ||
verify(wrapped).updateParameters( | ||
parametersWithJust(Parameters.Type.PREVIEW_SIZE, PREVIEW_SIZE) | ||
); | ||
|
||
verifyNoMoreInteractions(wrapped); | ||
} | ||
|
||
private Parameters parametersWithJust(Parameters.Type type, Object value) { | ||
Parameters parameters = new Parameters(); | ||
parameters.putValue(type, value); | ||
|
||
return parameters; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...t/java/io/fotoapparat/hardware/v1/parameters/SupressExceptionsParametersOperatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.fotoapparat.hardware.v1.parameters; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import io.fotoapparat.hardware.operators.ParametersOperator; | ||
import io.fotoapparat.log.Logger; | ||
import io.fotoapparat.parameter.Parameters; | ||
|
||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class SupressExceptionsParametersOperatorTest { | ||
|
||
@Mock | ||
ParametersOperator wrapped; | ||
@Mock | ||
Logger logger; | ||
@Mock | ||
Parameters parameters; | ||
|
||
@InjectMocks | ||
SupressExceptionsParametersOperator testee; | ||
|
||
@Test | ||
public void updateParameters() throws Exception { | ||
// Given | ||
doThrow(new RuntimeException()) | ||
.when(wrapped) | ||
.updateParameters(parameters); | ||
|
||
// When | ||
testee.updateParameters(parameters); | ||
|
||
// Then | ||
verify(wrapped).updateParameters(parameters); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
...est/java/io/fotoapparat/hardware/v1/parameters/SwitchOnFailureParametersOperatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.fotoapparat.hardware.v1.parameters; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import io.fotoapparat.hardware.operators.ParametersOperator; | ||
import io.fotoapparat.parameter.Parameters; | ||
|
||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyZeroInteractions; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class SwitchOnFailureParametersOperatorTest { | ||
|
||
@Mock | ||
ParametersOperator first; | ||
@Mock | ||
ParametersOperator second; | ||
@Mock | ||
Parameters parameters; | ||
|
||
SwitchOnFailureParametersOperator testee; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
testee = new SwitchOnFailureParametersOperator( | ||
first, second | ||
); | ||
} | ||
|
||
@Test | ||
public void firstSucceeds() throws Exception { | ||
// When | ||
testee.updateParameters(parameters); | ||
|
||
// Then | ||
verify(first).updateParameters(parameters); | ||
|
||
verifyZeroInteractions(second); | ||
} | ||
|
||
@Test | ||
public void firstFails() throws Exception { | ||
// Given | ||
doThrow(new RuntimeException()) | ||
.when(first) | ||
.updateParameters(parameters); | ||
|
||
// When | ||
testee.updateParameters(parameters); | ||
|
||
// Then | ||
verify(second).updateParameters(parameters); | ||
} | ||
|
||
} |