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

Commit

Permalink
Fixed memory leak and other small bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rolf-Smit committed Jun 19, 2016
1 parent 6f9aea5 commit 49c04cb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 33 deletions.
6 changes: 3 additions & 3 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ dependencies {
compile 'com.android.support:design:23.4.0'

//LeakCanary is used to detect memory leaks in debug builds.
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'

//Unit-testing
testCompile 'junit:junit:4.12'
Expand Down
2 changes: 1 addition & 1 deletion 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.3'
libraryVersion = '0.2.4'

siteUrl = 'https://github.com/NeoTech-Software/Android-Retainable-Tasks'
gitUrl = 'https://github.com/NeoTech-Software/Android-Retainable-Tasks.git'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.neotech.library.retainabletasks;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import org.neotech.library.retainabletasks.internal.BaseTaskManager;
Expand Down Expand Up @@ -151,11 +153,14 @@ public static boolean isStrictDebugModeEnabled(){
@MainThread
public static TaskManager getFragmentTaskManager(@NonNull Fragment fragment){
final String tag = getTaskManagerTag(fragment.getTag());
if(fragment.getParentFragment() != null){
return getTaskManager(TaskRetainingFragment.getInstance(fragment.getParentFragment().getFragmentManager()), tag);
} else {
return getTaskManager(TaskRetainingFragment.getInstance(fragment.getFragmentManager()), tag);

//Get the root activity
while(fragment.getParentFragment() != null){
fragment = fragment.getParentFragment();
}
final FragmentActivity root = fragment.getActivity();

return getTaskManager(TaskRetainingFragment.getInstance(root.getSupportFragmentManager()), tag);
}

/**
Expand All @@ -170,18 +175,21 @@ public static TaskManager getFragmentTaskManager(@NonNull android.app.Fragment f
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
return getFragmentTaskManagerAPI17(fragment, tag);
} else {
return getTaskManager(TaskRetainingFragment.getInstance(fragment.getFragmentManager()), tag);
return getTaskManager(TaskRetainingFragment.getInstance(fragment.getActivity().getFragmentManager()), tag);
}
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@MainThread
private static TaskManager getFragmentTaskManagerAPI17(@NonNull android.app.Fragment fragment, String tag){
if(fragment.getParentFragment() != null){
return getTaskManager(TaskRetainingFragment.getInstance(fragment.getParentFragment().getFragmentManager()), tag);
} else {
return getTaskManager(TaskRetainingFragment.getInstance(fragment.getFragmentManager()), tag);

//Get the root activity
while(fragment.getParentFragment() != null){
fragment = fragment.getParentFragment();
}
final Activity root = fragment.getActivity();

return getTaskManager(TaskRetainingFragment.getInstance(root.getFragmentManager()), tag);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
Expand All @@ -12,7 +13,7 @@
/**
* Created by Rolf on 3-3-2016.
*/
public final class TaskManagerLifeCycleProxy implements BaseTaskManager.UIStateProvider {
public final class TaskManagerLifeCycleProxy {

private BaseTaskManager fragmentTaskManager;
private final TaskManagerProvider provider;
Expand All @@ -32,23 +33,28 @@ public TaskManagerLifeCycleProxy(@NonNull TaskManagerProvider provider){
this.provider = provider;
}

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

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

@MainThread
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public TaskManager getTaskManager(){
if(fragmentTaskManager != null) {
return fragmentTaskManager;
}
if(provider instanceof FragmentActivity){
fragmentTaskManager = (BaseTaskManager) TaskManager.getActivityTaskManager(((FragmentActivity) provider).getSupportFragmentManager());
fragmentTaskManager = (BaseTaskManager) TaskManager.getActivityTaskManager(((FragmentActivity) provider).getFragmentManager());
} else if(provider instanceof Fragment){
fragmentTaskManager = (BaseTaskManager) TaskManager.getFragmentTaskManager((Fragment) provider);
} else if(provider instanceof Activity){
Expand All @@ -59,12 +65,7 @@ public TaskManager getTaskManager(){
//This should never happen as the constructor checks everything.
}
*/
fragmentTaskManager.setUIStateProvider(this);
fragmentTaskManager.setUIReady(uiReady);
return fragmentTaskManager;
}

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

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

protected boolean isUIReady = true;

@Override
public Task<?, ?> getTask(@NonNull String tag) {
Expand Down Expand Up @@ -138,7 +139,7 @@ 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);
if(uiStateProvider == null || uiStateProvider.isUserInterfaceReady() ){
if(isUIReady){
task.setCallback(new CallbackShadow(callback));
} else {
task.removeCallback();
Expand Down Expand Up @@ -230,12 +231,8 @@ private void removeFinishedTask(Task expectedTask){
tasks.remove(expectedTask.getTag());
}

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

public interface UIStateProvider {
boolean isUserInterfaceReady();
public void setUIReady(boolean isReady){
this.isUIReady = isReady;
}

private final class CallbackShadow implements Task.AdvancedCallback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public static TaskRetainingFragment getInstance(FragmentManager fragmentManager)
TaskRetainingFragmentCompat taskFragment = (TaskRetainingFragmentCompat) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
if(taskFragment == null){
taskFragment = new TaskRetainingFragmentCompat();
}
if(!taskFragment.isAdded()) {
fragmentManager.beginTransaction().add(taskFragment, FRAGMENT_TAG).commit();
}
return taskFragment.logic;
Expand All @@ -38,8 +36,6 @@ public static TaskRetainingFragment getInstance(android.app.FragmentManager frag
TaskRetainingFragmentV11 taskFragment = (TaskRetainingFragmentV11) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
if(taskFragment == null){
taskFragment = new TaskRetainingFragmentV11();
}
if(!taskFragment.isAdded()) {
fragmentManager.beginTransaction().add(taskFragment, FRAGMENT_TAG).commit();
}
return taskFragment.logic;
Expand Down

0 comments on commit 49c04cb

Please sign in to comment.