ultrasonic-app-subsonic-and.../ultrasonic/src/main/java/org/moire/ultrasonic/util/TabActivityBackgroundTask.java

90 lines
1.6 KiB
Java
Raw Normal View History

2015-07-26 18:15:07 +02:00
package org.moire.ultrasonic.util;
2012-02-26 21:25:13 +01:00
import android.app.Activity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
2012-02-26 21:25:13 +01:00
/**
* @author Sindre Mehus
* @version $Id$
*/
public abstract class TabActivityBackgroundTask<T> extends BackgroundTask<T>
{
private final boolean changeProgress;
private final SwipeRefreshLayout swipe;
2021-02-08 20:24:20 +01:00
private final CancellationToken cancel;
public TabActivityBackgroundTask(Activity activity, boolean changeProgress,
SwipeRefreshLayout swipe, CancellationToken cancel)
{
super(activity);
this.changeProgress = changeProgress;
this.swipe = swipe;
this.cancel = cancel;
}
2012-02-26 21:25:13 +01:00
@Override
public void execute()
{
if (changeProgress)
{
if (swipe != null) swipe.setRefreshing(true);
}
2012-02-26 21:25:13 +01:00
new Thread()
{
@Override
public void run()
{
try
{
final T result = doInBackground();
if (cancel.isCancellationRequested())
{
return;
}
2012-02-26 21:25:13 +01:00
getHandler().post(new Runnable()
{
@Override
public void run()
{
if (changeProgress)
{
if (swipe != null) swipe.setRefreshing(false);
}
2012-02-26 21:25:13 +01:00
done(result);
}
});
}
catch (final Throwable t)
{
if (cancel.isCancellationRequested())
{
return;
}
getHandler().post(new Runnable()
{
@Override
public void run()
{
if (changeProgress)
{
if (swipe != null) swipe.setRefreshing(false);
}
2012-02-26 21:25:13 +01:00
error(t);
}
});
}
}
}.start();
}
@Override
public void updateProgress(final String message)
{
}
2012-02-26 21:25:13 +01:00
}