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

Commit

Permalink
Modified the behavior of executing Tasks through a life-cycle bound T…
Browse files Browse the repository at this point in the history
…askManager (Activity or Fragment TaskManager), tasks executed before onStart() or after onStop() are started but won't deliver callback until the Activity or Fragment UI is ready, just like Tasks which are retained across configuration changes.
  • Loading branch information
Rolf-Smit committed Apr 28, 2016
1 parent b78c210 commit b4474e0
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 46 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
// NOTE: Do not place your application dependencies here; they belong
Expand Down
6 changes: 3 additions & 3 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "org.neotech.app.retainabletasksdemo"
Expand All @@ -23,8 +23,8 @@ dependencies {
compile project(':library')
//compile 'org.neotech.library:android-retainable-tasks:0.1.0'

compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'

//LeakCanary is used to detect memory leaks in debug builds.
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
Expand Down
3 changes: 1 addition & 2 deletions demo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@

<activity
android:name=".activity.Main"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Spanned;
Expand All @@ -16,33 +18,36 @@
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import org.neotech.app.retainabletasksdemo.ExtendedHtml;
import org.neotech.app.retainabletasksdemo.R;
import org.neotech.library.retainabletasks.Task;
import org.neotech.library.retainabletasks.providers.TaskActivityCompat;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Rolf on 16-3-2016.
*/
public class Main extends AppCompatActivity {
public class Main extends TaskActivityCompat implements Task.Callback {

private ViewSwitcher vSwitcher;
private ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
// setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

ArrayList<Demo> demos = new ArrayList<>(1);
demos.add(new Demo(this, R.string.demo_examples_title, R.string.demo_examples_description, "org/neotech/app/retainabletasksdemo/activity/DemoActivityBasic.java", new Intent(this, DemoActivityBasic.class)));
demos.add(new Demo(this, R.string.demo_serial_title, R.string.demo_serial_description, "org/neotech/app/retainabletasksdemo/activity/DemoActivitySerial.java", new Intent(this, DemoActivitySerial.class)));
demos.add(new Demo(this, R.string.demo_fragments_title, R.string.demo_fragments_description, "org/neotech/app/retainabletasksdemo/activity/DemoActivityFragments.java", new Intent(this, DemoActivityFragments.class)));
demos.add(new Demo(this, R.string.demo_no_compat_title, R.string.demo_no_compat_description, "org/neotech/app/retainabletasksdemo/activity/DemoActivityV11.java", new Intent(this, DemoActivityV11.class)));
vSwitcher = (ViewSwitcher) findViewById(R.id.switcher);
list = (ListView) findViewById(android.R.id.list);


ListView list = (ListView) findViewById(android.R.id.list);
list.setAdapter(new DemoAdapter(demos));
if(!getTaskManager().isRunning("list-loader")) {
getTaskManager().execute(new UselessLoadingTask("list-loader", this), this);
}
}

@Override
Expand All @@ -59,6 +64,57 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

public void setListShown(boolean shown){
if(vSwitcher.getCurrentView() == list && !shown){
vSwitcher.showNext();
} else if(vSwitcher.getCurrentView() != list && shown){
vSwitcher.showNext();
}
}

@Override
public Task.Callback onPreAttach(@NonNull Task<?, ?> task) {
setListShown(false);
return this;
}

@Override
public void onPreExecute(Task<?, ?> task) {

}

@Override
public void onPostExecute(Task<?, ?> raw) {
UselessLoadingTask task = (UselessLoadingTask) raw;
list.setAdapter(new DemoAdapter(task.getResult()));
setListShown(true);
}

/**
* Task just to demonstrate the principe of starting a task before the UI is ready.
*/
private static class UselessLoadingTask extends Task<Void, ArrayList<Demo>> {

private final Context context;

public UselessLoadingTask(String tag, Context context) {
super(tag);
this.context = context.getApplicationContext();
}

@Override
protected ArrayList<Demo> doInBackground() {
SystemClock.sleep(1500);
ArrayList<Demo> demos = new ArrayList<>(4);
demos.add(new Demo(context, R.string.demo_examples_title, R.string.demo_examples_description, "org/neotech/app/retainabletasksdemo/activity/DemoActivityBasic.java", new Intent(context, DemoActivityBasic.class)));
demos.add(new Demo(context, R.string.demo_serial_title, R.string.demo_serial_description, "org/neotech/app/retainabletasksdemo/activity/DemoActivitySerial.java", new Intent(context, DemoActivitySerial.class)));
demos.add(new Demo(context, R.string.demo_fragments_title, R.string.demo_fragments_description, "org/neotech/app/retainabletasksdemo/activity/DemoActivityFragments.java", new Intent(context, DemoActivityFragments.class)));
demos.add(new Demo(context, R.string.demo_no_compat_title, R.string.demo_no_compat_description, "org/neotech/app/retainabletasksdemo/activity/DemoActivityV11.java", new Intent(context, DemoActivityV11.class)));
return demos;
}
}


private static class DemoAdapter extends BaseAdapter implements View.OnClickListener {

private static class ViewHolder {
Expand All @@ -82,6 +138,14 @@ public DemoAdapter(ArrayList<Demo> demos){
this.demos = demos;
}

public DemoAdapter(){
this.demos = new ArrayList<>();
}

public List<Demo> getItems(){
return demos;
}

@Override
public int getCount() {
return demos.size();
Expand Down
36 changes: 26 additions & 10 deletions demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"

<!--
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="org.neotech.app.retainabletasksdemo.activity.Main">
android:orientation="vertical">
<include layout="@layout/toolbar_layout" />
<ListView
-->

<ViewSwitcher
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@android:id/list" />
android:layout_height="match_parent">

<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@android:id/progress" />

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list" />

</android.support.design.widget.CoordinatorLayout>
</ViewSwitcher>
<!--
</LinearLayout>
-->


17 changes: 5 additions & 12 deletions demo/src/main/res/layout/toolbar_layout.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Oct 21 11:34:03 PDT 2015
#Thu Apr 28 17:08:46 CEST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
8 changes: 4 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ext {
artifact = 'android-retainable-tasks'

libraryDescription = 'Android-Retainable-Tasks is an easy to use mini-library for easy asynchronous background tasking with callback support to the UI. This library is based on the Android AsyncTask implementation but with support for retaining tasks and therefore surviving configuration changes (orientation).'
libraryVersion = '0.2.1'
libraryVersion = '0.2.2'

siteUrl = 'https://github.com/NeoTech-Software/Android-Retainable-Tasks'
gitUrl = 'https://github.com/NeoTech-Software/Android-Retainable-Tasks.git'
Expand All @@ -37,7 +37,7 @@ version = libraryVersion

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
buildToolsVersion "23.0.3"

defaultConfig {
minSdkVersion 8
Expand All @@ -60,8 +60,8 @@ configurations {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
javadocCompile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:appcompat-v7:23.3.0'
javadocCompile 'com.android.support:appcompat-v7:23.3.0'
}

apply from: 'publish-library.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
/**
* Created by Rolf on 3-3-2016.
*/
public final class TaskManagerLifeCycleProxy {
public final class TaskManagerLifeCycleProxy implements BaseTaskManager.UIStateProvider {

private BaseTaskManager fragmentTaskManager;
private final TaskManagerProvider provider;
private boolean uiReady = false;

public TaskManagerLifeCycleProxy(@NonNull TaskManagerProvider provider){
if(!(provider instanceof Activity || provider instanceof Fragment || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && provider instanceof android.app.Fragment))){
Expand All @@ -32,10 +33,12 @@ public TaskManagerLifeCycleProxy(@NonNull TaskManagerProvider provider){
}

public void onStart(){
uiReady = true;
((BaseTaskManager) getTaskManager()).attach(provider);
}

public void onStop(){
uiReady = false;
((BaseTaskManager) getTaskManager()).detach();
}

Expand All @@ -56,6 +59,12 @@ public TaskManager getTaskManager(){
//This should never happen as the constructor checks everything.
}
*/
fragmentTaskManager.setUIStateProvider(this);
return fragmentTaskManager;
}

@Override
public boolean isUserInterfaceReady() {
return uiReady;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class BaseTaskManager extends TaskManager {
private static final String TAG = "BaseTaskManager";

protected final HashMap<String, Task<?, ?>> tasks = new HashMap<>();
protected UIStateProvider uiStateProvider = null;

@Override
public Task<?, ?> getTask(@NonNull String tag) {
Expand Down Expand Up @@ -137,7 +138,11 @@ public <Progress, Result> void execute(@NonNull Task<Progress, Result> task, @No
throw new IllegalStateException("Task with an equal tag: '" + task.getTag() + "' has already been added and is currently running or finishing.");
}
tasks.put(task.getTag(), task);
task.setCallback(new CallbackShadow(callback));
if(uiStateProvider == null || uiStateProvider.isUserInterfaceReady() ){
task.setCallback(new CallbackShadow(callback));
} else {
task.removeCallback();
}
TaskExecutor.executeOnExecutor(task, executor);
}

Expand Down Expand Up @@ -225,6 +230,14 @@ private void removeFinishedTask(Task expectedTask){
tasks.remove(expectedTask.getTag());
}

public void setUIStateProvider(UIStateProvider uiStateProvider){
this.uiStateProvider = uiStateProvider;
}

public interface UIStateProvider {
boolean isUserInterfaceReady();
}

private final class CallbackShadow implements Task.AdvancedCallback {

private final Task.Callback callback;
Expand Down

0 comments on commit b4474e0

Please sign in to comment.