Messages: Replace (Safe)AsyncTask
* AsyncTask is deprecated * Executors and Handlers can achieve the same thing and are not deprecated Change-Id: I5271bb73b848ce885eeaf5632c1da27853c56c4a
This commit is contained in:
@@ -21,6 +21,8 @@ import android.content.res.Resources;
|
||||
import android.graphics.Rect;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.Editable;
|
||||
import android.text.Html;
|
||||
import android.text.InputFilter;
|
||||
@@ -70,13 +72,14 @@ import com.android.messaging.util.ContentType;
|
||||
import com.android.messaging.util.LogUtil;
|
||||
import com.android.messaging.util.MediaUtil;
|
||||
import com.android.messaging.util.PhoneUtils;
|
||||
import com.android.messaging.util.SafeAsyncTask;
|
||||
import com.android.messaging.util.UiUtils;
|
||||
import com.android.messaging.util.UriUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* This view contains the UI required to generate and send messages.
|
||||
@@ -596,10 +599,11 @@ public class ComposeMessageView extends LinearLayout
|
||||
mConversationDataModel.getData().getParticipantsLoaded();
|
||||
}
|
||||
|
||||
private static class AsyncUpdateMessageBodySizeTask
|
||||
extends SafeAsyncTask<List<MessagePartData>, Void, Long> {
|
||||
private static class AsyncUpdateMessageBodySizeTask {
|
||||
|
||||
private final Context mContext;
|
||||
private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();
|
||||
private final Handler mHandler = new Handler(Looper.getMainLooper());
|
||||
private final TextView mSizeTextView;
|
||||
|
||||
public AsyncUpdateMessageBodySizeTask(final Context context, final TextView tv) {
|
||||
@@ -607,20 +611,23 @@ public class ComposeMessageView extends LinearLayout
|
||||
mSizeTextView = tv;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Long doInBackgroundTimed(final List<MessagePartData>... params) {
|
||||
final List<MessagePartData> attachments = params[0];
|
||||
long totalSize = 0;
|
||||
for (final MessagePartData attachment : attachments) {
|
||||
final Uri contentUri = attachment.getContentUri();
|
||||
if (contentUri != null) {
|
||||
totalSize += UriUtil.getContentSize(attachment.getContentUri());
|
||||
protected void execute(final List<MessagePartData> attachments) {
|
||||
mExecutor.execute(() -> {
|
||||
long totalSize = 0;
|
||||
for (final MessagePartData attachment : attachments) {
|
||||
final Uri contentUri = attachment.getContentUri();
|
||||
if (contentUri != null) {
|
||||
totalSize += UriUtil.getContentSize(attachment.getContentUri());
|
||||
}
|
||||
}
|
||||
}
|
||||
return totalSize;
|
||||
|
||||
final long size = totalSize;
|
||||
mHandler.post(() -> {
|
||||
onPostExecute(size);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Long size) {
|
||||
if (mSizeTextView != null) {
|
||||
mSizeTextView.setText(Formatter.formatFileSize(mContext, size));
|
||||
@@ -656,7 +663,7 @@ public class ComposeMessageView extends LinearLayout
|
||||
if (hasAttachmentsChanged) {
|
||||
// Calculate message attachments size and show it.
|
||||
new AsyncUpdateMessageBodySizeTask(getContext(), mMessageBodySize)
|
||||
.executeOnThreadPool(attachments, null, null);
|
||||
.execute(attachments);
|
||||
} else {
|
||||
// No update. Just show previous size.
|
||||
mMessageBodySize.setVisibility(View.VISIBLE);
|
||||
|
||||
@@ -34,6 +34,7 @@ import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Parcelable;
|
||||
import android.telephony.PhoneNumberUtils;
|
||||
import android.text.TextUtils;
|
||||
@@ -105,7 +106,6 @@ import com.android.messaging.util.ImeUtil;
|
||||
import com.android.messaging.util.LogUtil;
|
||||
import com.android.messaging.util.OsUtil;
|
||||
import com.android.messaging.util.PhoneUtils;
|
||||
import com.android.messaging.util.SafeAsyncTask;
|
||||
import com.android.messaging.util.TextUtil;
|
||||
import com.android.messaging.util.UiUtils;
|
||||
import com.android.messaging.util.UriUtil;
|
||||
@@ -113,6 +113,8 @@ import com.android.messaging.util.UriUtil;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* Shows a list of messages/parts comprising a conversation.
|
||||
@@ -312,7 +314,7 @@ public class ConversationFragment extends Fragment implements ConversationDataLi
|
||||
part.getContentType());
|
||||
}
|
||||
if (saveAttachmentTask.getAttachmentCount() > 0) {
|
||||
saveAttachmentTask.executeOnThreadPool();
|
||||
saveAttachmentTask.execute();
|
||||
mHost.dismissActionMode();
|
||||
}
|
||||
return true;
|
||||
@@ -1266,8 +1268,10 @@ public class ConversationFragment extends Fragment implements ConversationDataLi
|
||||
}
|
||||
}
|
||||
|
||||
public static class SaveAttachmentTask extends SafeAsyncTask<Void, Void, Void> {
|
||||
public static class SaveAttachmentTask {
|
||||
private final Context mContext;
|
||||
private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();
|
||||
private final Handler mHandler = new Handler(Looper.getMainLooper());
|
||||
private final List<AttachmentToSave> mAttachmentsToSave = new ArrayList<>();
|
||||
|
||||
public SaveAttachmentTask(final Context context, final Uri contentUri,
|
||||
@@ -1288,8 +1292,14 @@ public class ConversationFragment extends Fragment implements ConversationDataLi
|
||||
return mAttachmentsToSave.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackgroundTimed(final Void... arg) {
|
||||
public void execute() {
|
||||
mExecutor.execute(() -> {
|
||||
onExecute();
|
||||
mHandler.post(this::onPostExecute);
|
||||
});
|
||||
}
|
||||
|
||||
protected void onExecute() {
|
||||
final File appDir = new File(Environment.getExternalStoragePublicDirectory(
|
||||
Environment.DIRECTORY_PICTURES),
|
||||
mContext.getResources().getString(R.string.app_name));
|
||||
@@ -1301,11 +1311,9 @@ public class ConversationFragment extends Fragment implements ConversationDataLi
|
||||
attachment.persistedUri = UriUtil.persistContent(attachment.uri,
|
||||
isImageOrVideo ? appDir : downloadDir, attachment.contentType);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(final Void result) {
|
||||
protected void onPostExecute() {
|
||||
int failCount = 0;
|
||||
int imageCount = 0;
|
||||
int videoCount = 0;
|
||||
|
||||
Reference in New Issue
Block a user