Skip to content
This repository was archived by the owner on Dec 17, 2020. It is now read-only.

Commit 1520b8c

Browse files
committed
Merge branch 'develop'
2 parents 65e4164 + b994ef6 commit 1520b8c

File tree

14 files changed

+98
-37
lines changed

14 files changed

+98
-37
lines changed

DOCUMENTATION.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,15 @@ public class MyBaseActivity extends SomeActivity implements TaskManagerOwner {
229229

230230
@Override
231231
protected void onStop() {
232-
proxy.onStop();
232+
proxy.onStop(); // Call before super!
233233
super.onStop();
234234
}
235+
236+
@Override
237+
protected void onDestroy() {
238+
proxy.onDestroy(); // Call before super!
239+
super.onDestroy();
240+
}
235241

236242
@Override
237243
public TaskManager getTaskManager() {

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ Android-Retainable-Tasks is available on jcenter just like many other Android/Ja
5353
```groovy
5454
dependencies {
5555
// The library dependency
56-
compile 'org.neotech.library:android-retainable-tasks:1.0.0-rc-1'
56+
compile 'org.neotech.library:android-retainable-tasks:1.0.0-rc-2'
5757
5858
// Needed if you want to use annotations
59-
annotationProcessor 'org.neotech.library:android-retainable-tasks-compiler:1.0.0-rc-1'
59+
annotationProcessor 'org.neotech.library:android-retainable-tasks-compiler:1.0.0-rc-2'
6060
}
6161
```
6262

annotations-processor/src/main/java/org/neotech/library/retainabletasks/AnnotationsProcessor.java

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,7 @@ private JavaFile createJavaFile(TypeElement forTypeElement, TaskBindingContainer
157157
callbackImplementation.addMethod(createTaskCallbackMethod("onPreExecute", methods.getElementForPreExecute()));
158158
callbackImplementation.addMethod(createTaskCallbackMethod("onPostExecute", methods.getElementForPostExecute()));
159159
callbackImplementation.addMethod(createTaskCallbackMethod("onCanceled", methods.getElementForCancel()));
160-
161-
MethodSpec.Builder progressUpdateMethod = MethodSpec.methodBuilder("onProgressUpdate")
162-
.addAnnotation(Override.class)
163-
.addModifiers(Modifier.PUBLIC)
164-
.addParameter(CLASS_TASK, "task")
165-
.addParameter(TypeName.OBJECT, "object");
166-
167-
if(methods.getElementForProgress() != null) {
168-
progressUpdateMethod.addStatement("target.$L(task, object)", methods.getElementForProgress().getSimpleName());
169-
}
170-
callbackImplementation.addMethod(progressUpdateMethod.build());
171-
160+
callbackImplementation.addMethod(createTaskCallbackMethodForProgress(methods.getElementForProgress()));
172161

173162
if(methods.getElementForAttach() != null) {
174163
final boolean onlyCallOnReAttach = methods.getElementForAttach().getAnnotation(TaskAttach.class).onlyCallOnReAttach();
@@ -179,7 +168,21 @@ private JavaFile createJavaFile(TypeElement forTypeElement, TaskBindingContainer
179168
if(parameters.size() == 0) {
180169
getListenerForMethod.addStatement("target.$L()", methods.getElementForAttach().getSimpleName());
181170
} else {
182-
getListenerForMethod.addStatement("target.$L(task)", methods.getElementForAttach().getSimpleName());
171+
// One parameter, check it and call the method.
172+
final VariableElement parameter = parameters.get(0);
173+
final TypeMirror taskType = processingEnv.getTypeUtils().erasure(processingEnv.getElementUtils().getTypeElement(CLASS_TASK.reflectionName()).asType());
174+
175+
if (!processingEnv.getTypeUtils().isAssignable(parameter.asType(), taskType)) {
176+
// Parameter not an instance of Task
177+
error(parameter, "Type of parameter '%s' is not an instance of '%s'!", parameter.getSimpleName(), taskType);
178+
} else {
179+
// Check if the class to cast too is accessible.
180+
final Element requiredElement = processingEnv.getTypeUtils().asElement(parameter.asType());
181+
if (!requiredElement.getModifiers().contains(Modifier.PUBLIC) && !requiredElement.getModifiers().contains(Modifier.PROTECTED)) {
182+
error(parameter, "Type of parameter '%s' is not public or protected accessible! This prevents Android-Retainable-Tasks from casting '%s' to '%s'.\nTo fix this either the type of the parameter or make the class accessible by adding the public or protected modifier!", parameter.getSimpleName(), taskType, parameter.asType().toString());
183+
}
184+
getListenerForMethod.addStatement("target.$L(($T) task)", methods.getElementForAttach().getSimpleName(), parameter.asType());
185+
}
183186
}
184187
if(onlyCallOnReAttach){
185188
getListenerForMethod.endControlFlow();
@@ -210,6 +213,47 @@ private JavaFile createJavaFile(TypeElement forTypeElement, TaskBindingContainer
210213
.build();
211214
}
212215

216+
private MethodSpec createTaskCallbackMethodForProgress(@Nullable Element progressElement){
217+
MethodSpec.Builder progressUpdateMethod = MethodSpec.methodBuilder("onProgressUpdate")
218+
.addAnnotation(Override.class)
219+
.addModifiers(Modifier.PUBLIC)
220+
.addParameter(CLASS_TASK, "task")
221+
.addParameter(TypeName.OBJECT, "object");
222+
223+
if(progressElement != null) {
224+
final List<? extends VariableElement> parameters = ((ExecutableElement) progressElement).getParameters();
225+
if(parameters.size() == 0) {
226+
progressUpdateMethod.addStatement("target.$L()", progressElement.getSimpleName());
227+
} else if(parameters.size() >= 1 && parameters.size() <= 2){
228+
// One or two parameters, check it and call the method.
229+
final VariableElement parameterTask = parameters.get(0);
230+
final TypeMirror taskType = processingEnv.getTypeUtils().erasure(processingEnv.getElementUtils().getTypeElement(CLASS_TASK.reflectionName()).asType());
231+
232+
if (!processingEnv.getTypeUtils().isAssignable(parameterTask.asType(), taskType)) {
233+
// Parameter not an instance of Task
234+
error(parameterTask, "Type of parameter '%s' is not an instance of '%s'!", parameterTask.getSimpleName(), taskType);
235+
return progressUpdateMethod.build();
236+
}
237+
// Check if the class to cast to is accessible.
238+
final Element requiredElement = processingEnv.getTypeUtils().asElement(parameterTask.asType());
239+
if (!requiredElement.getModifiers().contains(Modifier.PUBLIC) && !requiredElement.getModifiers().contains(Modifier.PROTECTED)) {
240+
error(parameterTask, "Type of parameter '%s' is not public or protected accessible! This prevents Android-Retainable-Tasks from casting '%s' to '%s'.\nTo fix this either the type of the parameter or make the class accessible by adding the public or protected modifier!", parameterTask.getSimpleName(), taskType, parameterTask.asType().toString());
241+
return progressUpdateMethod.build();
242+
}
243+
244+
if(parameters.size() == 2){
245+
progressUpdateMethod.addStatement("target.$L(($T) task, object)", progressElement.getSimpleName(), parameterTask.asType());
246+
} else {
247+
progressUpdateMethod.addStatement("target.$L(($T) task)", progressElement.getSimpleName(), parameterTask.asType());
248+
}
249+
250+
} else {
251+
progressUpdateMethod.addComment("No annotated method found for $L", progressElement.getSimpleName());
252+
}
253+
}
254+
return progressUpdateMethod.build();
255+
}
256+
213257
private MethodSpec createTaskCallbackMethod(String methodName, @Nullable Element methodToCall){
214258
// Create the implementation of the onPreExecute method.
215259
final MethodSpec.Builder onPreExecuteMethod = MethodSpec.methodBuilder(methodName)

demo/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 25
4+
compileSdkVersion 26
55
buildToolsVersion '25.0.3'
66

77
defaultConfig {
88
applicationId "org.neotech.app.retainabletasksdemo"
99
minSdkVersion 14
10-
targetSdkVersion 25
10+
targetSdkVersion 26
1111
versionCode 1
1212
versionName "1.0"
1313
}
@@ -23,10 +23,10 @@ dependencies {
2323
compile project(':library')
2424
compile project(":annotations")
2525
annotationProcessor project(":annotations-processor")
26-
//compile 'org.neotech.library:android-retainable-tasks:1.0.0-rc-1'
26+
//compile 'org.neotech.library:android-retainable-tasks:1.0.0-rc-2'
2727

28-
compile 'com.android.support:appcompat-v7:25.3.1'
29-
compile 'com.android.support:design:25.3.1'
28+
compile 'com.android.support:appcompat-v7:26.0.2'
29+
compile 'com.android.support:design:26.0.2'
3030

3131
//LeakCanary is used to detect memory leaks in debug builds.
3232
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'

demo/src/main/java/org/neotech/app/retainabletasksdemo/activity/Main.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.neotech.app.retainabletasksdemo.R;
2222
import org.neotech.library.retainabletasks.Task;
2323
import org.neotech.library.retainabletasks.TaskAttach;
24+
import org.neotech.library.retainabletasks.TaskManager;
2425
import org.neotech.library.retainabletasks.TaskPostExecute;
2526
import org.neotech.library.retainabletasks.providers.TaskActivityCompat;
2627

@@ -40,6 +41,8 @@ protected void onCreate(Bundle savedInstanceState) {
4041
super.onCreate(savedInstanceState);
4142
setContentView(R.layout.activity_main);
4243

44+
getSupportActionBar().setSubtitle(getString(R.string.library_version, TaskManager.getVersionName()));
45+
4346
vSwitcher = (ViewSwitcher) findViewById(R.id.switcher);
4447
list = (ListView) findViewById(android.R.id.list);
4548

demo/src/main/java/org/neotech/app/retainabletasksdemo/activity/ProgressTaskHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ public void execute(){
3434
}
3535

3636
@TaskAttach(TASK_PROGRESS)
37-
public void onAttach(Task task){
37+
public void onAttach(SimpleTask task){
3838
// Task attaches, make sure to show the progress dialog and update the progress if needed.
3939
progressDialog = ProgressDialog.showIfNotShowing(demoActivityAnnotations.getSupportFragmentManager(), DIALOG_PROGRESS);
4040
if(task.getLastKnownProgress() != null) {
41-
progressDialog.setProgress((Integer) task.getLastKnownProgress());
41+
progressDialog.setProgress(task.getLastKnownProgress());
4242
}
4343
}
4444

4545
@TaskProgress(TASK_PROGRESS)
46-
public void onProgress(Task<?, ?> task, Object progress){
47-
progressDialog.setProgress((int) progress);
46+
public void onProgress(SimpleTask task){
47+
progressDialog.setProgress(task.getLastKnownProgress());
4848
}
4949

5050
// Now this is cool, we can have a single method handle both the normal onPostExecute and the

demo/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
<string name="menu_github">View on GitHub</string>
3434

35+
<string name="library_version">Library version: %1$s</string>
3536

3637
<string name="demo_fragments_title">Fragment TaskManager</string>
3738
<string name="demo_fragments_description">"<![CDATA[This demonstration shows how individual Fragments use the <code>TaskManager</code> class to start and manage <code>Tasks</code>. Each Fragment extends from the <code>TaskFragmentCompat</code> class, which means that each Fragment has it's own <code>TaskManager</code> instance. <code>Task</code> results are only delivered to the Fragment if it is currently attached to the Activity.]]>"</string>
@@ -45,7 +46,6 @@
4546
<string name="demo_annotations_title">Annotations example</string>
4647
<string name="demo_annotations_description">"<![CDATA[This demonstration shows an Activity which extends <code>TaskActivityCompat</code> and uses Annotations to receive <code>Task</code> lifecycle events like <code>onPostExecute</code>.]]>"</string>
4748

48-
4949
<string name="demo_serial_title">Serial execution</string>
5050
<string name="demo_serial_description">"<![CDATA[By default <code>Tasks</code> are executed using a parallel Executor. This demonstration shows how <code>Tasks</code> can be executed in serial using the <code>TaskManager.execute(Task, Callback, Executor)</code> method.]]>"</string>
5151

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# org.gradle.parallel=true
1919

2020
GROUP=org.neotech.library
21-
VERSION_NAME=1.0.0-rc-1
21+
VERSION_NAME=1.0.0-rc-2
2222

2323
BINTRAY_REPO=maven
2424
BINTRAY_LICENCE=Apache-2.0

library/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ group = GROUP
1111
version = VERSION_NAME
1212

1313
android {
14-
compileSdkVersion 25
14+
compileSdkVersion 26
1515
buildToolsVersion '25.0.3'
1616

1717
defaultConfig {
1818
minSdkVersion 14
19-
targetSdkVersion 25
19+
targetSdkVersion 26
2020
versionName = VERSION_NAME
2121
}
2222
buildTypes {
@@ -38,12 +38,12 @@ dependencies {
3838

3939
// Android Architecture Lifecycle library to support using the TaskManagerLifeCycleProxy in
4040
// combination with LifecycleOwner.getLifecycle().addObserver().
41-
compile 'android.arch.lifecycle:extensions:1.0.0-alpha3'
42-
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha3"
41+
compile 'android.arch.lifecycle:extensions:1.0.0-alpha9'
42+
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha9"
4343

4444
// Support library for the support library specific TaskManagerOwners (TaskActivityCompat).
45-
compile 'com.android.support:appcompat-v7:25.3.1'
46-
javadoc 'com.android.support:appcompat-v7:25.3.1'
45+
compile 'com.android.support:appcompat-v7:26.0.2'
46+
javadoc 'com.android.support:appcompat-v7:26.0.2'
4747

4848
testCompile 'junit:junit:4.12'
4949
}

library/src/main/java/org/neotech/library/retainabletasks/TaskManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,12 @@ public static TaskManager getGlobalTaskManager(){
284284
return globalInstance;
285285
}
286286
}
287+
288+
/**
289+
* Returns the version of the library.
290+
* @return the version name of the library.
291+
*/
292+
public static String getVersionName(){
293+
return BuildConfig.VERSION_NAME;
294+
}
287295
}

0 commit comments

Comments
 (0)