Skip to content

Commit f07abfc

Browse files
upgrade gradle and build tools (#181)
* upgrade gradle and build tools * cleanup lint errors * update to include android annotations
1 parent 044f507 commit f07abfc

File tree

8 files changed

+65
-57
lines changed

8 files changed

+65
-57
lines changed

android-sdk/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ dependencies {
6565
compile ("com.optimizely.ab:core-api:$java_core_ver") {
6666
exclude group: 'com.google.code.findbugs'
6767
}
68-
provided "com.android.support:support-annotations:$support_annotations_ver"
68+
compile "com.android.support:support-annotations:$support_annotations_ver"
6969

7070
testCompile "junit:junit:$junit_ver"
7171
testCompile "org.mockito:mockito-core:$mockito_ver"

build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ buildscript {
3131
}
3232
repositories {
3333
jcenter()
34+
google()
3435
}
3536
dependencies {
36-
classpath 'com.android.tools.build:gradle:2.3.3'
37+
classpath 'com.android.tools.build:gradle:3.0.1'
3738

3839
// NOTE: Do not place your application dependencies here; they belong
3940
// in the individual module build.gradle files
@@ -43,12 +44,13 @@ buildscript {
4344
allprojects {
4445
repositories {
4546
jcenter()
47+
google()
4648
}
4749
}
4850

4951
ext {
5052
compile_sdk_version = 26
51-
build_tools_version = "26.0.1"
53+
build_tools_version = "27.0.0"
5254
min_sdk_version = 10
5355
target_sdk_version = 26
5456
java_core_ver = "2.0.0-beta4"

datafile-handler/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ android {
5353

5454
dependencies {
5555
compile project(':shared')
56-
provided "com.android.support:support-annotations:$support_annotations_ver"
56+
compile "com.android.support:support-annotations:$support_annotations_ver"
5757

5858
testCompile "junit:junit:$junit_ver"
5959
testCompile "org.mockito:mockito-core:$mockito_ver"

event-handler/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ android {
4949
sourceCompatibility JavaVersion.VERSION_1_7
5050
targetCompatibility JavaVersion.VERSION_1_7
5151
}
52+
buildToolsVersion '27.0.0'
5253
}
5354

5455
dependencies {
5556
compile project(':shared')
56-
provided "com.android.support:support-annotations:$support_annotations_ver"
57+
compile "com.android.support:support-annotations:$support_annotations_ver"
5758

5859
testCompile "junit:junit:$junit_ver"
5960
testCompile "org.mockito:mockito-core:$mockito_ver"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Tue Apr 04 22:41:06 CEST 2017
1+
#Wed Feb 07 11:56:04 PST 2018
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

shared/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ android {
4949
sourceCompatibility JavaVersion.VERSION_1_7
5050
targetCompatibility JavaVersion.VERSION_1_7
5151
}
52+
buildToolsVersion '27.0.0'
5253
}
5354

5455
dependencies {

shared/src/main/java/com/optimizely/ab/android/shared/JobWorkService.java

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ public class JobWorkService extends JobService {
5353
/**
5454
* This is a task to dequeue and process work in the background.
5555
*/
56-
final class CommandProcessor extends AsyncTask<Void, Void, Void> {
56+
static final class CommandProcessor extends AsyncTask<Void, Void, Void> {
5757
private final JobParameters mParams;
58-
CommandProcessor(JobParameters params) {
59-
mParams = params;
58+
private final Logger logger;
59+
private final Context context;
60+
61+
CommandProcessor(JobParameters params, Context context, Logger logger) {
62+
mParams = params; this.logger = logger; this.context = context;
6063
}
6164
@Override
6265
protected Void doInBackground(Void... params) {
@@ -89,7 +92,7 @@ protected Void doInBackground(Void... params) {
8992
callOnHandleIntent(intentService, work.getIntent());
9093
completeWork(mParams, work);
9194
} else {
92-
Handler mainHandler = new Handler(getApplicationContext().getMainLooper());
95+
Handler mainHandler = new Handler(context.getApplicationContext().getMainLooper());
9396
final Service mainService = (Service)service;
9497
final JobWorkItem workItem = work;
9598
final Intent manServiceIntent = work.getIntent();
@@ -121,6 +124,49 @@ public void run() {
121124
}
122125
return null;
123126
}
127+
private void setContext(Service service) {
128+
callMethod(ContextWrapper.class, service, "attachBaseContext", new Class[] { Context.class }, context.getApplicationContext());
129+
}
130+
131+
private void callOnStartCommand(Service service, Intent intent) {
132+
callMethod(Service.class, service, "onStartCommand", new Class[] { Intent.class, int.class, int.class}, intent, 0, 1);
133+
}
134+
135+
private void callOnHandleIntent(IntentService intentService, Intent intent) {
136+
callMethod(IntentService.class, intentService, "onHandleIntent", new Class[] { Intent.class }, intent);
137+
}
138+
139+
private void callMethod(Class clazz, Object object, String methodName, Class[] parameterTypes, Object... parameters ) {
140+
try {
141+
Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
142+
method.setAccessible(true);
143+
method.invoke(object, parameters);
144+
145+
} catch (NoSuchMethodException e) {
146+
logger.error("Error calling method " + methodName, e);
147+
} catch (InvocationTargetException e) {
148+
logger.error("Error calling method " + methodName, e);
149+
} catch (IllegalAccessException e) {
150+
logger.error("Error calling method " + methodName, e);
151+
}
152+
153+
}
154+
155+
private void completeWork(JobParameters jobParameters, JobWorkItem jobWorkItem) {
156+
Intent intent = jobWorkItem.getIntent();
157+
if (intent != null && intent.hasExtra(INTENT_EXTRA_JWS_PERIODIC)) {
158+
logger.info("Periodic work item completed ");
159+
jobParameters.completeWork(jobWorkItem);
160+
//reschedule(jobWorkItem);
161+
}
162+
else {
163+
logger.info("work item completed");
164+
jobParameters.completeWork(jobWorkItem);
165+
166+
}
167+
168+
}
169+
124170
}
125171
@Override
126172
public void onCreate() {
@@ -134,7 +180,7 @@ public void onDestroy() {
134180
@Override
135181
public boolean onStartJob(JobParameters params) {
136182
// Start task to pull work out of the queue and process it.
137-
mCurProcessor = new CommandProcessor(params);
183+
mCurProcessor = new CommandProcessor(params, getApplicationContext(), this.logger);
138184
mCurProcessor.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
139185
// Allow the job to continue running while we process work.
140186
return true;
@@ -150,49 +196,6 @@ public boolean onStopJob(JobParameters params) {
150196
return true;
151197
}
152198

153-
private void setContext(Service service) {
154-
callMethod(ContextWrapper.class, service, "attachBaseContext", new Class[] { Context.class }, getApplicationContext());
155-
}
156-
157-
private void callOnStartCommand(Service service, Intent intent) {
158-
callMethod(Service.class, service, "onStartCommand", new Class[] { Intent.class, int.class, int.class}, intent, 0, 1);
159-
}
160-
161-
private void callOnHandleIntent(IntentService intentService, Intent intent) {
162-
callMethod(IntentService.class, intentService, "onHandleIntent", new Class[] { Intent.class }, intent);
163-
}
164-
165-
private void callMethod(Class clazz, Object object, String methodName, Class[] parameterTypes, Object... parameters ) {
166-
try {
167-
Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
168-
method.setAccessible(true);
169-
method.invoke(object, parameters);
170-
171-
} catch (NoSuchMethodException e) {
172-
logger.error("Error calling method " + methodName, e);
173-
} catch (InvocationTargetException e) {
174-
logger.error("Error calling method " + methodName, e);
175-
} catch (IllegalAccessException e) {
176-
logger.error("Error calling method " + methodName, e);
177-
}
178-
179-
}
180-
181-
private void completeWork(JobParameters jobParameters, JobWorkItem jobWorkItem) {
182-
Intent intent = jobWorkItem.getIntent();
183-
if (intent != null && intent.hasExtra(INTENT_EXTRA_JWS_PERIODIC)) {
184-
logger.info("Periodic work item completed ");
185-
jobParameters.completeWork(jobWorkItem);
186-
//reschedule(jobWorkItem);
187-
}
188-
else {
189-
logger.info("work item completed");
190-
jobParameters.completeWork(jobWorkItem);
191-
192-
}
193-
194-
}
195-
196199
private void reschedule(JobWorkItem item) {
197200
ServiceScheduler.PendingIntentFactory pendingIntentFactory = new ServiceScheduler
198201
.PendingIntentFactory(getApplicationContext());

shared/src/main/java/com/optimizely/ab/android/shared/ServiceScheduler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
import android.support.annotation.RequiresApi;
3131

3232
import org.slf4j.Logger;
33-
import org.slf4j.LoggerFactory;
33+
34+
import static android.app.job.JobScheduler.RESULT_SUCCESS;
3435

3536
/**
3637
* Schedules {@link android.app.Service}es to run.
@@ -129,7 +130,7 @@ private void setRepeating(long interval, PendingIntent pendingIntent, Intent int
129130

130131
builder.setExtras(persistableBundle);
131132

132-
if (jobScheduler.schedule(builder.build()) <= 0) {
133+
if (jobScheduler.schedule(builder.build()) != RESULT_SUCCESS) {
133134
logger.error("ServiceScheduler", "Some error while scheduling the job");
134135
}
135136

0 commit comments

Comments
 (0)