2020-08-15 21:31:55 +00:00
|
|
|
package awais.instagrabber.activities.directmessages;
|
2020-07-01 17:08:28 +00:00
|
|
|
|
2020-08-15 21:25:57 +00:00
|
|
|
import android.app.Activity;
|
2020-07-01 17:08:28 +00:00
|
|
|
import android.content.Intent;
|
2020-07-03 00:27:54 +00:00
|
|
|
import android.net.Uri;
|
2020-07-01 17:08:28 +00:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
2020-08-16 09:21:09 +00:00
|
|
|
import android.os.ParcelFileDescriptor;
|
2020-08-15 21:14:11 +00:00
|
|
|
import android.text.TextUtils;
|
2020-07-01 17:08:28 +00:00
|
|
|
import android.util.Log;
|
2020-07-02 03:20:54 +00:00
|
|
|
import android.view.View;
|
2020-07-01 17:08:28 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2020-08-16 09:21:09 +00:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2020-08-15 21:25:57 +00:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.InputStream;
|
2020-08-16 09:21:09 +00:00
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.HttpURLConnection;
|
2020-07-01 17:08:28 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
2020-07-03 00:27:54 +00:00
|
|
|
import java.util.Collections;
|
2020-07-01 17:08:28 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import awais.instagrabber.R;
|
2020-08-15 21:31:55 +00:00
|
|
|
import awais.instagrabber.activities.BaseLanguageActivity;
|
|
|
|
import awais.instagrabber.activities.PostViewer;
|
|
|
|
import awais.instagrabber.activities.ProfileViewer;
|
|
|
|
import awais.instagrabber.activities.StoryViewer;
|
2020-07-01 17:08:28 +00:00
|
|
|
import awais.instagrabber.adapters.MessageItemsAdapter;
|
2020-08-16 09:21:09 +00:00
|
|
|
import awais.instagrabber.asyncs.ImageUploader;
|
|
|
|
import awais.instagrabber.asyncs.direct_messages.DirectThreadBroadcaster;
|
|
|
|
import awais.instagrabber.asyncs.direct_messages.DirectThreadBroadcaster.BroadcastOptions;
|
|
|
|
import awais.instagrabber.asyncs.direct_messages.DirectThreadBroadcaster.ImageBroadcastOptions;
|
|
|
|
import awais.instagrabber.asyncs.direct_messages.DirectThreadBroadcaster.OnBroadcastCompleteListener;
|
|
|
|
import awais.instagrabber.asyncs.direct_messages.DirectThreadBroadcaster.TextBroadcastOptions;
|
2020-07-01 17:08:28 +00:00
|
|
|
import awais.instagrabber.asyncs.direct_messages.UserInboxFetcher;
|
|
|
|
import awais.instagrabber.customviews.helpers.RecyclerLazyLoader;
|
|
|
|
import awais.instagrabber.databinding.ActivityDmsBinding;
|
|
|
|
import awais.instagrabber.interfaces.FetchListener;
|
2020-08-16 09:21:09 +00:00
|
|
|
import awais.instagrabber.models.ImageUploadOptions;
|
2020-07-02 03:20:54 +00:00
|
|
|
import awais.instagrabber.models.PostModel;
|
2020-07-01 17:08:28 +00:00
|
|
|
import awais.instagrabber.models.ProfileModel;
|
2020-07-02 03:20:54 +00:00
|
|
|
import awais.instagrabber.models.StoryModel;
|
2020-07-01 17:08:28 +00:00
|
|
|
import awais.instagrabber.models.direct_messages.DirectItemModel;
|
|
|
|
import awais.instagrabber.models.direct_messages.InboxThreadModel;
|
2020-07-02 03:20:54 +00:00
|
|
|
import awais.instagrabber.models.enums.DirectItemType;
|
2020-07-03 00:27:54 +00:00
|
|
|
import awais.instagrabber.models.enums.DownloadMethod;
|
2020-07-01 17:08:28 +00:00
|
|
|
import awais.instagrabber.models.enums.UserInboxDirection;
|
|
|
|
import awais.instagrabber.utils.Constants;
|
|
|
|
import awais.instagrabber.utils.Utils;
|
|
|
|
|
2020-08-15 21:25:57 +00:00
|
|
|
import static android.view.View.VISIBLE;
|
|
|
|
|
2020-08-15 20:36:23 +00:00
|
|
|
public final class DirectMessageThread extends BaseLanguageActivity {
|
2020-08-15 21:25:57 +00:00
|
|
|
private static final String TAG = "DirectMessageThread";
|
|
|
|
private static final int PICK_IMAGE = 100;
|
|
|
|
|
2020-07-02 03:20:54 +00:00
|
|
|
private DirectItemModel directItemModel;
|
2020-08-15 21:08:25 +00:00
|
|
|
private String threadId;
|
2020-08-15 21:02:46 +00:00
|
|
|
private String endCursor;
|
|
|
|
private ActivityDmsBinding dmsBinding;
|
|
|
|
private MessageItemsAdapter messageItemsAdapter;
|
|
|
|
|
2020-08-15 20:38:46 +00:00
|
|
|
private final ProfileModel myProfileHolder = ProfileModel.getDefaultProfileModel();
|
2020-08-15 21:02:46 +00:00
|
|
|
private final ArrayList<ProfileModel> users = new ArrayList<>();
|
2020-08-15 21:08:25 +00:00
|
|
|
private final ArrayList<ProfileModel> leftUsers = new ArrayList<>();
|
2020-07-01 17:08:28 +00:00
|
|
|
private final ArrayList<DirectItemModel> directItemModels = new ArrayList<>();
|
|
|
|
private final FetchListener<InboxThreadModel> fetchListener = new FetchListener<InboxThreadModel>() {
|
|
|
|
@Override
|
|
|
|
public void doBefore() {
|
|
|
|
dmsBinding.swipeRefreshLayout.setRefreshing(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResult(final InboxThreadModel result) {
|
2020-07-03 00:27:54 +00:00
|
|
|
if (result == null && ("MINCURSOR".equals(endCursor) || "MAXCURSOR".equals(endCursor) || Utils.isEmpty(endCursor)))
|
2020-08-15 20:36:23 +00:00
|
|
|
Toast.makeText(DirectMessageThread.this, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
|
2020-07-01 17:08:28 +00:00
|
|
|
|
|
|
|
if (result != null) {
|
|
|
|
endCursor = result.getPrevCursor();
|
2020-08-15 21:00:30 +00:00
|
|
|
if ("MINCURSOR".equals(endCursor) || "MAXCURSOR".equals(endCursor))
|
|
|
|
endCursor = null;
|
2020-07-01 17:08:28 +00:00
|
|
|
|
|
|
|
users.clear();
|
|
|
|
users.addAll(Arrays.asList(result.getUsers()));
|
|
|
|
|
2020-08-15 21:08:25 +00:00
|
|
|
leftUsers.clear();
|
|
|
|
leftUsers.addAll(Arrays.asList(result.getLeftUsers()));
|
2020-08-05 18:54:49 +00:00
|
|
|
|
2020-08-15 21:08:25 +00:00
|
|
|
threadId = result.getThreadId();
|
2020-08-06 16:48:52 +00:00
|
|
|
dmsBinding.toolbar.toolbar.setTitle(result.getThreadTitle());
|
|
|
|
String[] users = new String[result.getUsers().length];
|
|
|
|
for (int i = 0; i < users.length; ++i) {
|
|
|
|
users[i] = result.getUsers()[i].getUsername();
|
|
|
|
}
|
2020-08-15 21:14:11 +00:00
|
|
|
dmsBinding.toolbar.toolbar.setSubtitle(TextUtils.join(", ", users));
|
2020-08-06 16:48:52 +00:00
|
|
|
|
2020-07-01 17:08:28 +00:00
|
|
|
final int oldSize = directItemModels.size();
|
|
|
|
final List<DirectItemModel> itemModels = Arrays.asList(result.getItems());
|
|
|
|
directItemModels.addAll(itemModels);
|
|
|
|
messageItemsAdapter.notifyItemRangeInserted(oldSize, itemModels.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
dmsBinding.swipeRefreshLayout.setRefreshing(false);
|
|
|
|
}
|
|
|
|
};
|
2020-08-15 21:25:57 +00:00
|
|
|
private final View.OnClickListener clickListener = v -> {
|
|
|
|
if (v == dmsBinding.commentSend) {
|
2020-08-16 09:21:09 +00:00
|
|
|
final String text = dmsBinding.commentText.getText().toString();
|
|
|
|
if (Utils.isEmpty(text)) {
|
2020-08-15 21:25:57 +00:00
|
|
|
Toast.makeText(getApplicationContext(), R.string.comment_send_empty_comment, Toast.LENGTH_SHORT).show();
|
2020-08-15 21:04:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-16 09:21:09 +00:00
|
|
|
sendText(text);
|
2020-08-15 21:25:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (v == dmsBinding.image) {
|
|
|
|
final Intent intent = new Intent();
|
|
|
|
intent.setType("image/*");
|
|
|
|
intent.setAction(Intent.ACTION_GET_CONTENT);
|
|
|
|
startActivityForResult(Intent.createChooser(intent, getString(R.string.select_picture)), PICK_IMAGE);
|
|
|
|
}
|
2020-08-15 21:02:46 +00:00
|
|
|
};
|
2020-07-01 17:08:28 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(@Nullable final Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
dmsBinding = ActivityDmsBinding.inflate(getLayoutInflater());
|
|
|
|
setContentView(dmsBinding.getRoot());
|
|
|
|
|
|
|
|
final InboxThreadModel threadModel;
|
|
|
|
final Intent intent = getIntent();
|
|
|
|
if (intent == null || !intent.hasExtra(Constants.EXTRAS_THREAD_MODEL) ||
|
|
|
|
(threadModel = (InboxThreadModel) intent.getSerializableExtra(Constants.EXTRAS_THREAD_MODEL)) == null) {
|
|
|
|
Utils.errorFinish(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dmsBinding.swipeRefreshLayout.setEnabled(false);
|
2020-08-15 21:25:57 +00:00
|
|
|
dmsBinding.commentText.setVisibility(VISIBLE);
|
|
|
|
dmsBinding.commentSend.setVisibility(VISIBLE);
|
|
|
|
dmsBinding.image.setVisibility(VISIBLE);
|
|
|
|
dmsBinding.commentSend.setOnClickListener(clickListener);
|
|
|
|
dmsBinding.image.setOnClickListener(clickListener);
|
2020-07-01 17:08:28 +00:00
|
|
|
|
|
|
|
final LinearLayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, true);
|
|
|
|
dmsBinding.rvDirectMessages.setLayoutManager(layoutManager);
|
|
|
|
|
|
|
|
dmsBinding.rvDirectMessages.addOnScrollListener(new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> {
|
|
|
|
if (!Utils.isEmpty(endCursor)) {
|
|
|
|
new UserInboxFetcher(threadModel.getThreadId(), UserInboxDirection.OLDER,
|
|
|
|
endCursor, fetchListener).execute(); // serial because we don't want messages to be randomly ordered
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2020-08-15 21:08:25 +00:00
|
|
|
messageItemsAdapter = new MessageItemsAdapter(directItemModels, users, leftUsers, v -> {
|
2020-07-02 03:20:54 +00:00
|
|
|
Object tag = v.getTag();
|
|
|
|
if (tag instanceof DirectItemModel) {
|
|
|
|
directItemModel = (DirectItemModel) tag;
|
|
|
|
final DirectItemType itemType = directItemModel.getItemType();
|
2020-07-03 00:27:54 +00:00
|
|
|
switch (itemType) {
|
2020-07-15 00:35:41 +00:00
|
|
|
case MEDIA_SHARE:
|
|
|
|
startActivity(new Intent(this, PostViewer.class)
|
2020-08-04 02:28:42 +00:00
|
|
|
.putExtra(Constants.EXTRAS_POST, new PostModel(directItemModel.getMediaModel().getCode(), false)));
|
2020-07-15 00:35:41 +00:00
|
|
|
break;
|
2020-07-03 00:27:54 +00:00
|
|
|
case LINK:
|
|
|
|
Intent linkIntent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
linkIntent.setData(Uri.parse(directItemModel.getLinkModel().getLinkContext().getLinkUrl()));
|
|
|
|
startActivity(linkIntent);
|
|
|
|
break;
|
2020-07-15 00:35:41 +00:00
|
|
|
case TEXT:
|
|
|
|
case REEL_SHARE:
|
|
|
|
Utils.copyText(v.getContext(), directItemModel.getText());
|
|
|
|
Toast.makeText(v.getContext(), R.string.clipboard_copied, Toast.LENGTH_SHORT).show();
|
2020-07-02 03:20:54 +00:00
|
|
|
break;
|
2020-07-15 00:35:41 +00:00
|
|
|
case RAVEN_MEDIA:
|
2020-07-03 00:27:54 +00:00
|
|
|
case MEDIA:
|
2020-08-15 21:14:57 +00:00
|
|
|
final ProfileModel user = getUser(directItemModel.getUserId());
|
|
|
|
if (user != null) {
|
|
|
|
Utils.dmDownload(this, user.getUsername(), DownloadMethod.DOWNLOAD_DIRECT, Collections.singletonList(itemType == DirectItemType.MEDIA ? directItemModel.getMediaModel() : directItemModel.getRavenMediaModel().getMedia()));
|
|
|
|
}
|
2020-07-03 00:27:54 +00:00
|
|
|
Toast.makeText(v.getContext(), R.string.downloader_downloading_media, Toast.LENGTH_SHORT).show();
|
|
|
|
break;
|
|
|
|
case STORY_SHARE:
|
2020-08-05 18:54:49 +00:00
|
|
|
if (directItemModel.getReelShare() != null) {
|
|
|
|
StoryModel sm = new StoryModel(
|
|
|
|
directItemModel.getReelShare().getReelId(),
|
|
|
|
directItemModel.getReelShare().getMedia().getVideoUrl(),
|
|
|
|
directItemModel.getReelShare().getMedia().getMediaType(),
|
|
|
|
directItemModel.getTimestamp(),
|
2020-08-06 16:48:52 +00:00
|
|
|
directItemModel.getReelShare().getReelOwnerName(),
|
|
|
|
String.valueOf(directItemModel.getReelShare().getReelOwnerId()),
|
|
|
|
false
|
2020-08-05 18:54:49 +00:00
|
|
|
);
|
|
|
|
sm.setVideoUrl(directItemModel.getReelShare().getMedia().getVideoUrl());
|
|
|
|
StoryModel[] sms = {sm};
|
2020-07-03 00:27:54 +00:00
|
|
|
startActivity(new Intent(this, StoryViewer.class)
|
2020-07-15 00:35:41 +00:00
|
|
|
.putExtra(Constants.EXTRAS_USERNAME, directItemModel.getReelShare().getReelOwnerName())
|
|
|
|
.putExtra(Constants.EXTRAS_STORIES, sms)
|
2020-07-03 00:27:54 +00:00
|
|
|
);
|
2020-08-15 21:00:30 +00:00
|
|
|
} else if (directItemModel.getText() != null && directItemModel.getText().toString().contains("@")) {
|
2020-08-05 18:54:49 +00:00
|
|
|
searchUsername(directItemModel.getText().toString().split("@")[1].split(" ")[0]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PLACEHOLDER:
|
|
|
|
if (directItemModel.getText().toString().contains("@"))
|
|
|
|
searchUsername(directItemModel.getText().toString().split("@")[1].split(" ")[0]);
|
2020-07-03 00:27:54 +00:00
|
|
|
break;
|
2020-07-15 00:35:41 +00:00
|
|
|
default:
|
2020-08-15 21:00:30 +00:00
|
|
|
Log.d("austin_debug", "unsupported type " + itemType);
|
2020-07-02 03:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-15 21:00:30 +00:00
|
|
|
}, (view, text, isHashtag) -> searchUsername(text));
|
2020-07-03 00:27:54 +00:00
|
|
|
|
2020-08-14 17:05:42 +00:00
|
|
|
dmsBinding.rvDirectMessages.setAdapter(messageItemsAdapter);
|
2020-07-01 17:08:28 +00:00
|
|
|
|
|
|
|
new UserInboxFetcher(threadModel.getThreadId(), UserInboxDirection.OLDER, null, fetchListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
2020-07-02 03:20:54 +00:00
|
|
|
|
2020-08-15 21:25:57 +00:00
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
2020-08-16 09:21:09 +00:00
|
|
|
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
|
2020-08-15 21:25:57 +00:00
|
|
|
if (data == null || data.getData() == null) {
|
|
|
|
Log.w(TAG, "data is null!");
|
|
|
|
return;
|
|
|
|
}
|
2020-08-16 09:21:09 +00:00
|
|
|
final Uri uri = data.getData();
|
|
|
|
sendImage(uri);
|
2020-08-15 21:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 03:20:54 +00:00
|
|
|
@Nullable
|
|
|
|
private ProfileModel getUser(final long userId) {
|
2020-08-05 18:54:49 +00:00
|
|
|
if (users != null) {
|
|
|
|
ProfileModel result = myProfileHolder;
|
|
|
|
for (final ProfileModel user : users) {
|
|
|
|
if (Long.toString(userId).equals(user.getId())) result = user;
|
|
|
|
}
|
2020-08-15 21:08:25 +00:00
|
|
|
if (leftUsers != null)
|
|
|
|
for (final ProfileModel leftUser : leftUsers) {
|
|
|
|
if (Long.toString(userId).equals(leftUser.getId())) result = leftUser;
|
2020-08-05 18:54:49 +00:00
|
|
|
}
|
|
|
|
return result;
|
2020-07-02 03:20:54 +00:00
|
|
|
}
|
2020-08-05 18:54:49 +00:00
|
|
|
return null;
|
2020-07-02 03:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void searchUsername(final String text) {
|
2020-08-14 17:05:42 +00:00
|
|
|
startActivity(new Intent(getApplicationContext(), ProfileViewer.class).putExtra(Constants.EXTRAS_USERNAME, text));
|
2020-07-02 03:20:54 +00:00
|
|
|
}
|
2020-08-16 09:21:09 +00:00
|
|
|
|
|
|
|
private void sendText(final String text) {
|
|
|
|
final TextBroadcastOptions options;
|
|
|
|
try {
|
|
|
|
options = new TextBroadcastOptions(text);
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
Log.e(TAG, "Error", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
broadcast(options, result -> {
|
|
|
|
if (result == null || result.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
|
|
|
Toast.makeText(getApplicationContext(), R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dmsBinding.commentText.setText("");
|
|
|
|
dmsBinding.commentText.clearFocus();
|
|
|
|
directItemModels.clear();
|
|
|
|
messageItemsAdapter.notifyDataSetChanged();
|
|
|
|
new UserInboxFetcher(threadId, UserInboxDirection.OLDER, null, fetchListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendImage(final Uri imageUri) {
|
|
|
|
try {
|
|
|
|
final ParcelFileDescriptor fileDescriptor = getContentResolver().openFileDescriptor(imageUri, "r");
|
|
|
|
if (fileDescriptor == null) {
|
|
|
|
Log.e(TAG, "fileDescriptor is null!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final long contentLength = fileDescriptor.getStatSize();
|
|
|
|
final InputStream inputStream = getContentResolver().openInputStream(imageUri);
|
|
|
|
// Upload Image
|
|
|
|
final ImageUploader imageUploader = new ImageUploader();
|
|
|
|
imageUploader.setOnTaskCompleteListener(response -> {
|
|
|
|
if (response == null || response.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
|
|
|
Toast.makeText(getApplicationContext(), R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
|
|
|
|
if (response != null && response.getResponse() != null) {
|
|
|
|
Log.e(TAG, response.getResponse().toString());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final JSONObject responseJson = response.getResponse();
|
|
|
|
try {
|
|
|
|
final String uploadId = responseJson.getString("upload_id");
|
|
|
|
// Broadcast
|
|
|
|
final ImageBroadcastOptions options = new ImageBroadcastOptions(true, uploadId);
|
|
|
|
broadcast(options, onBroadcastCompleteListener -> {
|
|
|
|
directItemModels.clear();
|
|
|
|
messageItemsAdapter.notifyDataSetChanged();
|
|
|
|
new UserInboxFetcher(threadId, UserInboxDirection.OLDER, null, fetchListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
});
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(TAG, "Error parsing json response", e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
final ImageUploadOptions options = ImageUploadOptions.builder(inputStream, contentLength).build();
|
|
|
|
imageUploader.execute(options);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
Log.e(TAG, "Error opening InputStream", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void broadcast(final BroadcastOptions broadcastOptions, final OnBroadcastCompleteListener listener) {
|
|
|
|
final DirectThreadBroadcaster broadcaster = new DirectThreadBroadcaster(threadId);
|
|
|
|
broadcaster.setOnTaskCompleteListener(listener);
|
|
|
|
broadcaster.execute(broadcastOptions);
|
|
|
|
}
|
2020-07-01 17:08:28 +00:00
|
|
|
}
|