Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #144 from upstream
Browse files Browse the repository at this point in the history
Merge upstream formatting fixes, NPE bugfix
  • Loading branch information
ciarand authored Aug 31, 2017
2 parents b7dae54 + 7211cd8 commit 3d82e89
Show file tree
Hide file tree
Showing 61 changed files with 5,098 additions and 5,178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,41 @@
*/
@RunWith(AndroidJUnit4.class)
public final class EndToEndTest {
private Context appContext;
private FirebaseJobDispatcher dispatcher;
private Context appContext;
private FirebaseJobDispatcher dispatcher;

@Before public void setUp() {
appContext = InstrumentationRegistry.getTargetContext();
dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(appContext));
TestJobService.reset();
}
@Before
public void setUp() {
appContext = InstrumentationRegistry.getTargetContext();
dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(appContext));
TestJobService.reset();
}

@Test public void basicImmediateJob() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
TestJobService.setProxy(new TestJobService.JobServiceProxy() {
@Override
public boolean onStartJob(JobParameters params) {
latch.countDown();
return false;
}
@Test
public void basicImmediateJob() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
TestJobService.setProxy(
new TestJobService.JobServiceProxy() {
@Override
public boolean onStartJob(JobParameters params) {
latch.countDown();
return false;
}

@Override
public boolean onStopJob(JobParameters params) {
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
});

dispatcher.mustSchedule(
dispatcher.newJobBuilder()
.setService(TestJobService.class)
.setTrigger(Trigger.NOW)
.setTag("basic-immediate-job")
.build());
dispatcher.mustSchedule(
dispatcher
.newJobBuilder()
.setService(TestJobService.class)
.setTrigger(Trigger.NOW)
.setTag("basic-immediate-job")
.build());

assertTrue("Latch wasn't counted down as expected", latch.await(120, TimeUnit.SECONDS));
}
assertTrue("Latch wasn't counted down as expected", latch.await(120, TimeUnit.SECONDS));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,32 @@
package com.firebase.jobdispatcher;

final class BundleProtocol {
static final String PACKED_PARAM_BUNDLE_PREFIX = "com.firebase.jobdispatcher.";
static final String PACKED_PARAM_BUNDLE_PREFIX = "com.firebase.jobdispatcher.";

// PACKED_PARAM values are only read on the client side, so as long as the
// extraction process gets the same changes then it's fine.
static final String PACKED_PARAM_CONSTRAINTS = "constraints";
static final String PACKED_PARAM_LIFETIME = "persistent";
static final String PACKED_PARAM_RECURRING = "recurring";
static final String PACKED_PARAM_SERVICE = "service";
static final String PACKED_PARAM_TAG = "tag";
static final String PACKED_PARAM_EXTRAS = "extras";
static final String PACKED_PARAM_TRIGGER_TYPE = "trigger_type";
static final String PACKED_PARAM_TRIGGER_WINDOW_END = "window_end";
static final String PACKED_PARAM_TRIGGER_WINDOW_START = "window_start";
static final int TRIGGER_TYPE_EXECUTION_WINDOW = 1;
static final int TRIGGER_TYPE_IMMEDIATE = 2;
static final int TRIGGER_TYPE_CONTENT_URI = 3;
static final String PACKED_PARAM_RETRY_STRATEGY_INITIAL_BACKOFF_SECONDS =
"initial_backoff_seconds";
static final String PACKED_PARAM_RETRY_STRATEGY_MAXIMUM_BACKOFF_SECONDS =
"maximum_backoff_seconds";
static final String PACKED_PARAM_RETRY_STRATEGY_POLICY = "retry_policy";
static final String PACKED_PARAM_REPLACE_CURRENT = "replace_current";
static final String PACKED_PARAM_CONTENT_URI_FLAGS_ARRAY = "content_uri_flags_array";
static final String PACKED_PARAM_CONTENT_URI_ARRAY = "content_uri_array";
static final String PACKED_PARAM_TRIGGERED_URIS = "triggered_uris";
static final String PACKED_PARAM_OBSERVED_URI = "observed_uris";
// PACKED_PARAM values are only read on the client side, so as long as the
// extraction process gets the same changes then it's fine.
static final String PACKED_PARAM_CONSTRAINTS = "constraints";
static final String PACKED_PARAM_LIFETIME = "persistent";
static final String PACKED_PARAM_RECURRING = "recurring";
static final String PACKED_PARAM_SERVICE = "service";
static final String PACKED_PARAM_TAG = "tag";
static final String PACKED_PARAM_EXTRAS = "extras";
static final String PACKED_PARAM_TRIGGER_TYPE = "trigger_type";
static final String PACKED_PARAM_TRIGGER_WINDOW_END = "window_end";
static final String PACKED_PARAM_TRIGGER_WINDOW_START = "window_start";
static final int TRIGGER_TYPE_EXECUTION_WINDOW = 1;
static final int TRIGGER_TYPE_IMMEDIATE = 2;
static final int TRIGGER_TYPE_CONTENT_URI = 3;
static final String PACKED_PARAM_RETRY_STRATEGY_INITIAL_BACKOFF_SECONDS =
"initial_backoff_seconds";
static final String PACKED_PARAM_RETRY_STRATEGY_MAXIMUM_BACKOFF_SECONDS =
"maximum_backoff_seconds";
static final String PACKED_PARAM_RETRY_STRATEGY_POLICY = "retry_policy";
static final String PACKED_PARAM_REPLACE_CURRENT = "replace_current";
static final String PACKED_PARAM_CONTENT_URI_FLAGS_ARRAY = "content_uri_flags_array";
static final String PACKED_PARAM_CONTENT_URI_ARRAY = "content_uri_array";
static final String PACKED_PARAM_TRIGGERED_URIS = "triggered_uris";
static final String PACKED_PARAM_OBSERVED_URI = "observed_uris";

BundleProtocol() {
}
BundleProtocol() {}
}
133 changes: 65 additions & 68 deletions jobdispatcher/src/main/java/com/firebase/jobdispatcher/Constraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,83 +26,80 @@
* trigger has been activated and all constraints are satisfied.
*/
public final class Constraint {
/**
* Only run the job when an unmetered network is available.
*/
public static final int ON_UNMETERED_NETWORK = 1;
/** Only run the job when an unmetered network is available. */
public static final int ON_UNMETERED_NETWORK = 1;

/**
* Only run the job when a network connection is available. If both this and
* {@link #ON_UNMETERED_NETWORK} is provided, {@link #ON_UNMETERED_NETWORK} will take
* precedence.
*/
public static final int ON_ANY_NETWORK = 1 << 1;
/**
* Only run the job when a network connection is available. If both this and {@link
* #ON_UNMETERED_NETWORK} is provided, {@link #ON_UNMETERED_NETWORK} will take precedence.
*/
public static final int ON_ANY_NETWORK = 1 << 1;

/**
* Only run the job when the device is currently charging.
*/
public static final int DEVICE_CHARGING = 1 << 2;
/** Only run the job when the device is currently charging. */
public static final int DEVICE_CHARGING = 1 << 2;

/**
* Only run the job when the device is idle. This is ignored for devices that don't expose the
* concept of an idle state.
*/
public static final int DEVICE_IDLE = 1 << 3;
/**
* Only run the job when the device is idle. This is ignored for devices that don't expose the
* concept of an idle state.
*/
public static final int DEVICE_IDLE = 1 << 3;

@VisibleForTesting
static final int[] ALL_CONSTRAINTS = {
ON_ANY_NETWORK, ON_UNMETERED_NETWORK, DEVICE_CHARGING, DEVICE_IDLE};
@VisibleForTesting
static final int[] ALL_CONSTRAINTS = {
ON_ANY_NETWORK, ON_UNMETERED_NETWORK, DEVICE_CHARGING, DEVICE_IDLE
};

/** Constraint shouldn't ever be instantiated. */
private Constraint() {}
/** Constraint shouldn't ever be instantiated. */
private Constraint() {}

/**
* A tooling type-hint for any of the valid constraint values.
*/
@IntDef(flag = true, value = {
ON_ANY_NETWORK,
ON_UNMETERED_NETWORK,
DEVICE_CHARGING,
DEVICE_IDLE,
})
@Retention(RetentionPolicy.SOURCE)
public @interface JobConstraint {}

/**
* Compact a provided array of constraints into a single int.
*
* @see #uncompact(int)
*/
static int compact(@JobConstraint int[] constraints) {
int result = 0;
if (constraints == null) {
return result;
}
for (int c : constraints) {
result |= c;
}
return result;
/** A tooling type-hint for any of the valid constraint values. */
@IntDef(
flag = true,
value = {
ON_ANY_NETWORK,
ON_UNMETERED_NETWORK,
DEVICE_CHARGING,
DEVICE_IDLE,
}
)
@Retention(RetentionPolicy.SOURCE)
public @interface JobConstraint {}

/**
* Unpack a single int into an array of constraints.
*
* @see #compact(int[])
*/
static int[] uncompact(int compactConstraints) {
int length = 0;
for (int c : ALL_CONSTRAINTS) {
length += (compactConstraints & c) == c ? 1 : 0;
}
int[] list = new int[length];
/**
* Compact a provided array of constraints into a single int.
*
* @see #uncompact(int)
*/
static int compact(@JobConstraint int[] constraints) {
int result = 0;
if (constraints == null) {
return result;
}
for (int c : constraints) {
result |= c;
}
return result;
}

int i = 0;
for (int c : ALL_CONSTRAINTS) {
if ((compactConstraints & c) == c) {
list[i++] = c;
}
}
/**
* Unpack a single int into an array of constraints.
*
* @see #compact(int[])
*/
static int[] uncompact(int compactConstraints) {
int length = 0;
for (int c : ALL_CONSTRAINTS) {
length += (compactConstraints & c) == c ? 1 : 0;
}
int[] list = new int[length];

return list;
int i = 0;
for (int c : ALL_CONSTRAINTS) {
if ((compactConstraints & c) == c) {
list[i++] = c;
}
}

return list;
}
}
Loading

0 comments on commit 3d82e89

Please sign in to comment.