AsyncTask cancel handling

This commit is contained in:
NudeDude 2018-09-28 11:29:40 +02:00
parent 82fb6e7698
commit 3f741f774f
17 changed files with 159 additions and 64 deletions

View File

@ -137,9 +137,6 @@ public class MainActivity extends AppCompatActivity implements OnRefreshListener
protected void onStop() {
if (home != null && home.getStatus() == RUNNING) {
home.cancel(true);
timelineReload.setRefreshing(false);
trendReload.setRefreshing(false);
mentionReload.setRefreshing(false);
}
super.onStop();
}
@ -174,6 +171,7 @@ public class MainActivity extends AppCompatActivity implements OnRefreshListener
startActivity(search);
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
@ -257,24 +255,26 @@ public class MainActivity extends AppCompatActivity implements OnRefreshListener
@Override
public void onRefresh() {
if (home != null && home.getStatus() == RUNNING)
home.cancel(true);
home = new MainPage(MainActivity.this);
if (tabIndex == 0)
home.execute(MainPage.HOME, 1);
else if (tabIndex == 1)
home.execute(MainPage.TRND, 1);
else if (tabIndex == 2)
home.execute(MainPage.MENT, 1);
switch (tabIndex) {
case 0:
home.execute(MainPage.HOME, 1);
break;
case 1:
home.execute(MainPage.TRND, 1);
break;
case 2:
home.execute(MainPage.MENT, 1);
break;
}
}
@Override
public void onTabChanged(String tabId) {
if (home != null && home.getStatus() == RUNNING) {
home.cancel(true);
timelineReload.setRefreshing(false);
trendReload.setRefreshing(false);
mentionReload.setRefreshing(false);
}
animate();
tabIndex = tabhost.getCurrentTab();
invalidateOptionsMenu();

View File

@ -130,6 +130,12 @@ public class ImagePopup extends AsyncTask<String, Void, Boolean> {
}
@Override
protected void onCancelled(Boolean b) {
popup.dismiss();
}
private void setImage(Bitmap btm, ImageView mImg) {
if (btm != null) {
int height = (int) (btm.getHeight() / (btm.getWidth() / 640.0));

View File

@ -157,4 +157,18 @@ public class MainPage extends AsyncTask<Integer, Integer, Integer> {
}
}
}
@Override
protected void onCancelled(Integer i) {
if (ui.get() == null) return;
SwipeRefreshLayout timelineRefresh = ui.get().findViewById(R.id.timeline);
SwipeRefreshLayout trendRefresh = ui.get().findViewById(R.id.trends);
SwipeRefreshLayout mentionRefresh = ui.get().findViewById(R.id.mention);
timelineRefresh.setRefreshing(false);
trendRefresh.setRefreshing(false);
mentionRefresh.setRefreshing(false);
}
}

View File

@ -83,12 +83,13 @@ public class MessageLoader extends AsyncTask<Long, Void, Long> {
protected void onPostExecute(Long mode) {
if (ui.get() == null) return;
mAdapter.setData(message);
mAdapter.notifyDataSetChanged();
SwipeRefreshLayout mRefresh = ui.get().findViewById(R.id.dm_reload);
mRefresh.setRefreshing(false);
if (mode == FAIL) {
if (mode != FAIL) {
mAdapter.setData(message);
mAdapter.notifyDataSetChanged();
} else {
switch (returnCode) {
case 420:
Toast.makeText(ui.get(), R.string.rate_limit_exceeded, Toast.LENGTH_SHORT).show();
@ -104,4 +105,13 @@ public class MessageLoader extends AsyncTask<Long, Void, Long> {
}
}
}
@Override
protected void onCancelled(Long l) {
if (ui.get() == null) return;
SwipeRefreshLayout mRefresh = ui.get().findViewById(R.id.dm_reload);
mRefresh.setRefreshing(false);
}
}

View File

@ -90,4 +90,10 @@ public class MessageUpload extends AsyncTask<String, Void, Boolean> {
Toast.makeText(ui.get(), R.string.error_sending_dm, Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCancelled(Boolean b) {
popup.dismiss();
}
}

View File

@ -35,16 +35,14 @@ import twitter4j.TwitterException;
public class ProfileLoader extends AsyncTask<Long, Long, Long> {
// GET USER TWEETS
private static final long GET_USER = 1;
public static final long GET_TWEETS = 2;
public static final long GET_FAVORS = 3;
// USER ACTION
public static final long ACTION_FOLLOW = 6;
public static final long ACTION_BLOCK = 4;
public static final long ACTION_MUTE = 5;
// GET USER TWEETS
private static final long GET_USER = 1;
// INTERN FLAGS
private static final long FAILURE = -1;
@ -270,11 +268,6 @@ public class ProfileLoader extends AsyncTask<Long, Long, Long> {
protected void onPostExecute(final Long MODE) {
if (ui.get() == null) return;
SwipeRefreshLayout homeReload = ui.get().findViewById(R.id.hometweets);
SwipeRefreshLayout favReload = ui.get().findViewById(R.id.homefavorits);
homeReload.setRefreshing(false);
favReload.setRefreshing(false);
if (MODE == ACTION_FOLLOW) {
if (isFollowing)
Toast.makeText(ui.get(), R.string.followed, Toast.LENGTH_SHORT).show();
@ -294,6 +287,10 @@ public class ProfileLoader extends AsyncTask<Long, Long, Long> {
Toast.makeText(ui.get(), R.string.unmuted, Toast.LENGTH_SHORT).show();
} else if (MODE == FAILURE) {
SwipeRefreshLayout homeReload = ui.get().findViewById(R.id.hometweets);
SwipeRefreshLayout favReload = ui.get().findViewById(R.id.homefavorits);
homeReload.setRefreshing(false);
favReload.setRefreshing(false);
switch (returnCode) {
case 420:
@ -312,4 +309,16 @@ public class ProfileLoader extends AsyncTask<Long, Long, Long> {
ui.get().invalidateOptionsMenu();
}
}
@Override
protected void onCancelled(Long mode) {
if (ui.get() == null) return;
SwipeRefreshLayout homeReload = ui.get().findViewById(R.id.hometweets);
SwipeRefreshLayout favReload = ui.get().findViewById(R.id.homefavorits);
homeReload.setRefreshing(false);
favReload.setRefreshing(false);
}
}

View File

@ -249,4 +249,13 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> {
}
}
}
@Override
protected void onCancelled(Long l) {
if (ui.get() == null) return;
SwipeRefreshLayout ansReload = ui.get().findViewById(R.id.answer_reload);
ansReload.setRefreshing(false);
}
}

View File

@ -109,4 +109,10 @@ public class StatusUpload extends AsyncTask<String, Void, Boolean> {
.setNegativeButton(R.string.cancel, null).show();
}
}
@Override
protected void onCancelled(Boolean b) {
popup.dismiss();
}
}

View File

@ -99,10 +99,10 @@ public class TwitterSearch extends AsyncTask<String, Integer, Boolean> {
protected void onPostExecute(Boolean success) {
if (ui.get() == null) return;
SwipeRefreshLayout tweetReload = ui.get().findViewById(R.id.searchtweets);
tweetReload.setRefreshing(false);
if (!success) {
SwipeRefreshLayout tweetReload = ui.get().findViewById(R.id.searchtweets);
tweetReload.setRefreshing(false);
switch (returnCode) {
case 420:
Toast.makeText(ui.get(), R.string.rate_limit_exceeded, Toast.LENGTH_LONG).show();
@ -112,4 +112,13 @@ public class TwitterSearch extends AsyncTask<String, Integer, Boolean> {
}
}
}
@Override
protected void onCancelled(Boolean b) {
if (ui.get() == null) return;
SwipeRefreshLayout tweetReload = ui.get().findViewById(R.id.searchtweets);
tweetReload.setRefreshing(false);
}
}

View File

@ -1,6 +1,5 @@
package org.nuclearfog.twidda.backend;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
@ -69,10 +68,12 @@ public class UserLists extends AsyncTask<Long, Void, Boolean> {
@Override
@SuppressLint("InflateParams")
protected void onPostExecute(Boolean success) {
if (ui.get() == null) return;
SwipeRefreshLayout refresh = ui.get().findViewById(R.id.user_refresh);
refresh.setRefreshing(false);
if (success) {
usrAdp.setData(user);
usrAdp.notifyDataSetChanged();
@ -85,6 +86,13 @@ public class UserLists extends AsyncTask<Long, Void, Boolean> {
Toast.makeText(ui.get(), errorMessage, Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onCancelled(Boolean b) {
if (ui.get() == null) return;
SwipeRefreshLayout refresh = ui.get().findViewById(R.id.user_refresh);
refresh.setRefreshing(false);
}

View File

@ -14,10 +14,18 @@ import java.util.regex.Pattern;
public abstract class Tagger {
private static final String PATTERN = "[@#][^\\s@#\\.\\,]+";
private static final String PATTERN = "[@#][^\\s@#\\.\\,\\!\\?]+";
private static final int MODE = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
/**
* Make a spannable colored String with click listener
*
* @param text String that should be spannable
* @param color Text Color
* @param l click listener
* @return Spannable String
*/
public static Spannable makeText(final String text, final int color, @NonNull final OnTagClickListener l) {
SpannableStringBuilder sText = new SpannableStringBuilder(text);
Pattern p = Pattern.compile(PATTERN);
@ -42,6 +50,13 @@ public abstract class Tagger {
}
/**
* Make a spannable String without listener
*
* @param text String that should be spannable
* @param color Text Color
* @return Spannable String
*/
public static Spannable makeText(String text, int color) {
SpannableStringBuilder sText = new SpannableStringBuilder(text);
Pattern p = Pattern.compile(PATTERN);
@ -56,7 +71,15 @@ public abstract class Tagger {
}
/**
* Interface definition of Tag click listener
*/
public interface OnTagClickListener {
/**
* Called when a tag was clicked
*
* @param tag Clicked Tag
*/
void onClick(String tag);
}
}

View File

@ -76,7 +76,7 @@ public class LoginPage extends AppCompatActivity implements OnClickListener {
case R.id.clipboard:
ClipboardManager clip = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if (clip != null && clip.hasPrimaryClip()) {
if (clip != null && clip.getPrimaryClip() != null) {
String text = clip.getPrimaryClip().getItemAt(0).getText().toString();
if (text.matches("\\d++\n?")) {
pin.setText(text);

View File

@ -113,10 +113,8 @@ public class SearchPage extends AppCompatActivity implements UserAdapter.OnItemC
@Override
protected void onStop() {
if (mSearch != null && mSearch.getStatus() == RUNNING) {
if (mSearch != null && mSearch.getStatus() == RUNNING)
mSearch.cancel(true);
tweetReload.setRefreshing(false);
}
super.onStop();
}

View File

@ -115,10 +115,8 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
@Override
protected void onStop() {
if (mStat != null && mStat.getStatus() == RUNNING) {
if (mStat != null && mStat.getStatus() == RUNNING)
mStat.cancel(true);
answerReload.setRefreshing(false);
}
super.onStop();
}

View File

@ -160,7 +160,7 @@ public class TweetPopup extends AppCompatActivity implements OnClickListener {
if (imagePopup != null && imagePopup.getStatus() == RUNNING)
imagePopup.cancel(true);
imagePopup = new ImagePopup(this);
imagePopup.execute(mediaPath.toArray(new String[mediaPath.size()]));
imagePopup.execute(mediaPath.toArray(new String[0]));
break;
}
}

View File

@ -104,10 +104,8 @@ public class UserDetail extends AppCompatActivity implements OnItemClicked, OnRe
@Override
protected void onStop() {
if (uList != null && uList.getStatus() == RUNNING) {
if (uList != null && uList.getStatus() == RUNNING)
uList.cancel(true);
refresh.setRefreshing(false);
}
super.onStop();
}

View File

@ -123,11 +123,8 @@ public class UserProfile extends AppCompatActivity implements OnRefreshListener,
@Override
protected void onStop() {
if (mProfile != null && mProfile.getStatus() == RUNNING) {
if (mProfile != null && mProfile.getStatus() == RUNNING)
mProfile.cancel(true);
homeReload.setRefreshing(false);
favoriteReload.setRefreshing(false);
}
super.onStop();
}
@ -265,30 +262,34 @@ public class UserProfile extends AppCompatActivity implements OnRefreshListener,
@Override
public void onRefresh() {
if (tabIndex == 0) {
mProfile = new ProfileLoader(this);
mProfile.execute(userId, ProfileLoader.GET_TWEETS, 1L);
} else {
mProfile = new ProfileLoader(this);
mProfile.execute(userId, ProfileLoader.GET_FAVORS, 1L);
if (mProfile != null && mProfile.getStatus() == RUNNING)
mProfile.cancel(true);
mProfile = new ProfileLoader(this);
switch (tabIndex) {
case 0:
mProfile.execute(userId, ProfileLoader.GET_TWEETS, 1L);
break;
case 1:
mProfile.execute(userId, ProfileLoader.GET_FAVORS, 1L);
break;
}
}
@Override
public void onTabChanged(String tabId) {
if (mProfile != null && mProfile.getStatus() == RUNNING) {
mProfile.cancel(true);
homeReload.setRefreshing(false);
favoriteReload.setRefreshing(false);
}
animate();
tabIndex = mTab.getCurrentTab();
if (tabIndex == 0)
favoriteList.smoothScrollToPosition(0);
else
homeList.smoothScrollToPosition(0);
switch (tabIndex) {
case 0:
homeList.smoothScrollToPosition(0);
break;
case 1:
favoriteList.smoothScrollToPosition(0);
break;
}
tabIndex = mTab.getCurrentTab();
}