2020-08-17 13:49:51 +00:00
|
|
|
package awais.instagrabber.fragments.directmessages;
|
2020-07-01 17:08:28 +00:00
|
|
|
|
2020-08-15 21:25:57 +00:00
|
|
|
import android.app.Activity;
|
2020-09-12 08:51:50 +00:00
|
|
|
import android.content.Context;
|
2020-08-18 20:47:43 +00:00
|
|
|
import android.content.DialogInterface;
|
2020-07-01 17:08:28 +00:00
|
|
|
import android.content.Intent;
|
2020-11-04 14:58:06 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2020-08-18 15:15:13 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
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-17 13:49:51 +00:00
|
|
|
import android.os.Handler;
|
2020-07-01 17:08:28 +00:00
|
|
|
import android.util.Log;
|
2020-08-17 13:49:51 +00:00
|
|
|
import android.view.LayoutInflater;
|
2020-08-30 06:47:04 +00:00
|
|
|
import android.view.Menu;
|
2020-09-05 07:16:11 +00:00
|
|
|
import android.view.MenuInflater;
|
2020-08-30 06:47:04 +00:00
|
|
|
import android.view.MenuItem;
|
2020-07-02 03:20:54 +00:00
|
|
|
import android.view.View;
|
2020-08-17 13:49:51 +00:00
|
|
|
import android.view.ViewGroup;
|
2020-11-18 16:12:45 +00:00
|
|
|
import android.view.ViewParent;
|
2020-08-18 20:47:43 +00:00
|
|
|
import android.widget.ArrayAdapter;
|
2020-07-01 17:08:28 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2020-08-17 13:49:51 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2020-07-01 17:08:28 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2020-08-30 06:47:04 +00:00
|
|
|
import androidx.appcompat.app.ActionBar;
|
2020-08-18 20:47:43 +00:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
2020-08-30 06:47:04 +00:00
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
2020-11-04 14:58:06 +00:00
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
|
|
import androidx.core.content.ContextCompat;
|
2020-08-17 13:49:51 +00:00
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
|
import androidx.lifecycle.ViewModel;
|
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
2020-08-22 21:03:55 +00:00
|
|
|
import androidx.navigation.NavDirections;
|
|
|
|
import androidx.navigation.fragment.NavHostFragment;
|
2020-07-01 17:08:28 +00:00
|
|
|
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-19 16:51:41 +00:00
|
|
|
import java.io.DataOutputStream;
|
2020-08-18 15:15:13 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2020-08-16 09:21:09 +00:00
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.HttpURLConnection;
|
2020-08-19 16:51:41 +00:00
|
|
|
import java.net.URL;
|
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-08-17 13:49:51 +00:00
|
|
|
import java.util.LinkedList;
|
2020-07-01 17:08:28 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import awais.instagrabber.R;
|
2020-08-20 13:05:23 +00:00
|
|
|
import awais.instagrabber.adapters.DirectMessageItemsAdapter;
|
2020-08-16 09:21:09 +00:00
|
|
|
import awais.instagrabber.asyncs.ImageUploader;
|
2020-11-04 16:26:29 +00:00
|
|
|
import awais.instagrabber.asyncs.PostFetcher;
|
2020-08-17 13:49:51 +00:00
|
|
|
import awais.instagrabber.asyncs.direct_messages.DirectMessageInboxThreadFetcher;
|
2020-08-16 09:21:09 +00:00
|
|
|
import awais.instagrabber.asyncs.direct_messages.DirectThreadBroadcaster;
|
2020-07-01 17:08:28 +00:00
|
|
|
import awais.instagrabber.customviews.helpers.RecyclerLazyLoader;
|
2020-08-17 13:49:51 +00:00
|
|
|
import awais.instagrabber.databinding.FragmentDirectMessagesThreadBinding;
|
2020-11-04 16:26:29 +00:00
|
|
|
import awais.instagrabber.fragments.PostViewV2Fragment;
|
2020-07-01 17:08:28 +00:00
|
|
|
import awais.instagrabber.interfaces.FetchListener;
|
2020-08-17 13:49:51 +00:00
|
|
|
import awais.instagrabber.interfaces.MentionClickListener;
|
2020-08-16 09:21:09 +00:00
|
|
|
import awais.instagrabber.models.ImageUploadOptions;
|
2020-07-01 17:08:28 +00:00
|
|
|
import awais.instagrabber.models.ProfileModel;
|
|
|
|
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-08-29 19:55:40 +00:00
|
|
|
import awais.instagrabber.models.enums.MediaItemType;
|
2020-07-01 17:08:28 +00:00
|
|
|
import awais.instagrabber.models.enums.UserInboxDirection;
|
|
|
|
import awais.instagrabber.utils.Constants;
|
2020-09-10 11:44:25 +00:00
|
|
|
import awais.instagrabber.utils.CookieUtils;
|
|
|
|
import awais.instagrabber.utils.DownloadUtils;
|
|
|
|
import awais.instagrabber.utils.TextUtils;
|
2020-07-01 17:08:28 +00:00
|
|
|
import awais.instagrabber.utils.Utils;
|
|
|
|
|
2020-08-17 13:49:51 +00:00
|
|
|
public class DirectMessageThreadFragment extends Fragment {
|
|
|
|
private static final String TAG = "DirectMessagesThreadFmt";
|
2020-08-15 21:25:57 +00:00
|
|
|
private static final int PICK_IMAGE = 100;
|
2020-11-04 14:58:06 +00:00
|
|
|
private static final int STORAGE_PERM_REQUEST_CODE = 8020;
|
2020-08-15 21:25:57 +00:00
|
|
|
|
2020-08-30 06:47:04 +00:00
|
|
|
private AppCompatActivity fragmentActivity;
|
2020-09-05 07:16:11 +00:00
|
|
|
private String threadId;
|
|
|
|
private String threadTitle;
|
|
|
|
private String cursor;
|
|
|
|
private String lastMessage;
|
2020-08-18 20:47:43 +00:00
|
|
|
private final String cookie = Utils.settingsHelper.getString(Constants.COOKIE);
|
2020-09-10 11:44:25 +00:00
|
|
|
private final String myId = CookieUtils.getUserIdFromCookie(cookie);
|
2020-08-17 13:49:51 +00:00
|
|
|
private FragmentDirectMessagesThreadBinding binding;
|
|
|
|
private DirectItemModelListViewModel listViewModel;
|
2020-08-18 20:47:43 +00:00
|
|
|
private DirectItemModel directItemModel;
|
2020-08-17 13:49:51 +00:00
|
|
|
private RecyclerView messageList;
|
2020-09-02 18:11:28 +00:00
|
|
|
private boolean hasDeletedSomething;
|
2020-08-17 13:49:51 +00:00
|
|
|
private boolean hasOlder = true;
|
2020-09-01 17:37:00 +00:00
|
|
|
public static boolean hasSentSomething;
|
2020-08-15 21:02:46 +00:00
|
|
|
|
2020-08-15 20:38:46 +00:00
|
|
|
private final ProfileModel myProfileHolder = ProfileModel.getDefaultProfileModel();
|
2020-08-17 13:49:51 +00:00
|
|
|
private final List<ProfileModel> users = new ArrayList<>();
|
|
|
|
private final List<ProfileModel> leftUsers = new ArrayList<>();
|
2020-08-18 20:47:43 +00:00
|
|
|
private ArrayAdapter<String> dialogAdapter;
|
2020-08-17 13:49:51 +00:00
|
|
|
|
|
|
|
private final View.OnClickListener clickListener = v -> {
|
|
|
|
if (v == binding.commentSend) {
|
|
|
|
final String text = binding.commentText.getText().toString();
|
2020-09-10 11:44:25 +00:00
|
|
|
if (TextUtils.isEmpty(text)) {
|
2020-09-12 08:51:50 +00:00
|
|
|
final Context context = getContext();
|
|
|
|
if (context == null) return;
|
|
|
|
Toast.makeText(context, R.string.comment_send_empty_comment, Toast.LENGTH_SHORT).show();
|
2020-08-17 13:49:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-18 20:47:43 +00:00
|
|
|
sendText(text, null, false);
|
2020-08-17 13:49:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (v == binding.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-07-01 17:08:28 +00:00
|
|
|
private final FetchListener<InboxThreadModel> fetchListener = new FetchListener<InboxThreadModel>() {
|
|
|
|
@Override
|
|
|
|
public void doBefore() {
|
2020-08-17 13:49:51 +00:00
|
|
|
binding.swipeRefreshLayout.setRefreshing(true);
|
2020-07-01 17:08:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResult(final InboxThreadModel result) {
|
2020-09-12 08:51:50 +00:00
|
|
|
if (result == null && ("MINCURSOR".equals(cursor) || "MAXCURSOR".equals(cursor) || TextUtils.isEmpty(cursor))) {
|
|
|
|
final Context context = getContext();
|
|
|
|
if (context == null) return;
|
|
|
|
Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
2020-07-01 17:08:28 +00:00
|
|
|
|
|
|
|
if (result != null) {
|
2020-08-17 13:49:51 +00:00
|
|
|
cursor = result.getOldestCursor();
|
|
|
|
hasOlder = result.hasOlder();
|
|
|
|
if ("MINCURSOR".equals(cursor) || "MAXCURSOR".equals(cursor)) {
|
|
|
|
cursor = 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-17 13:49:51 +00:00
|
|
|
List<DirectItemModel> list = listViewModel.getList().getValue();
|
|
|
|
final List<DirectItemModel> newList = Arrays.asList(result.getItems());
|
|
|
|
list = list != null ? new LinkedList<>(list) : new LinkedList<>();
|
2020-08-19 16:51:41 +00:00
|
|
|
if (hasSentSomething || hasDeletedSomething) {
|
2020-08-17 13:49:51 +00:00
|
|
|
list = newList;
|
|
|
|
final Handler handler = new Handler();
|
2020-08-19 16:51:41 +00:00
|
|
|
if (hasSentSomething) handler.postDelayed(() -> {
|
2020-08-17 13:49:51 +00:00
|
|
|
if (messageList != null) {
|
|
|
|
messageList.smoothScrollToPosition(0);
|
|
|
|
}
|
|
|
|
}, 200);
|
2020-08-19 16:51:41 +00:00
|
|
|
hasSentSomething = false;
|
|
|
|
hasDeletedSomething = false;
|
2020-08-17 13:49:51 +00:00
|
|
|
} else {
|
|
|
|
list.addAll(newList);
|
|
|
|
}
|
|
|
|
listViewModel.getList().postValue(list);
|
2020-09-01 17:37:00 +00:00
|
|
|
|
|
|
|
lastMessage = result.getNewestCursor();
|
|
|
|
|
|
|
|
if (Utils.settingsHelper.getBoolean(Constants.DM_MARK_AS_SEEN)) new ThreadAction().execute("seen", lastMessage);
|
2020-07-01 17:08:28 +00:00
|
|
|
}
|
2020-08-17 13:49:51 +00:00
|
|
|
binding.swipeRefreshLayout.setRefreshing(false);
|
2020-08-15 21:25:57 +00:00
|
|
|
}
|
2020-08-15 21:02:46 +00:00
|
|
|
};
|
2020-11-04 14:58:06 +00:00
|
|
|
private ConstraintLayout root;
|
2020-09-05 07:16:11 +00:00
|
|
|
private boolean shouldRefresh = true;
|
2020-11-04 14:58:06 +00:00
|
|
|
private DirectItemModel.DirectItemMediaModel downloadMediaItem;
|
2020-07-01 17:08:28 +00:00
|
|
|
|
|
|
|
@Override
|
2020-08-17 13:49:51 +00:00
|
|
|
public void onCreate(@Nullable final Bundle savedInstanceState) {
|
2020-07-01 17:08:28 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
2020-08-30 06:47:04 +00:00
|
|
|
fragmentActivity = (AppCompatActivity) requireActivity();
|
|
|
|
setHasOptionsMenu(true);
|
2020-08-17 13:49:51 +00:00
|
|
|
}
|
2020-07-01 17:08:28 +00:00
|
|
|
|
2020-08-17 13:49:51 +00:00
|
|
|
@Override
|
|
|
|
public View onCreateView(@NonNull final LayoutInflater inflater,
|
|
|
|
final ViewGroup container,
|
|
|
|
final Bundle savedInstanceState) {
|
2020-09-05 07:16:11 +00:00
|
|
|
if (root != null) {
|
|
|
|
shouldRefresh = false;
|
2020-08-17 13:49:51 +00:00
|
|
|
return root;
|
2020-07-01 17:08:28 +00:00
|
|
|
}
|
2020-09-05 07:16:11 +00:00
|
|
|
binding = FragmentDirectMessagesThreadBinding.inflate(inflater, container, false);
|
|
|
|
root = binding.getRoot();
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
|
|
|
|
if (!shouldRefresh) return;
|
|
|
|
init();
|
|
|
|
shouldRefresh = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void init() {
|
|
|
|
listViewModel = new ViewModelProvider(fragmentActivity).get(DirectItemModelListViewModel.class);
|
|
|
|
if (getArguments() == null) return;
|
2020-08-22 21:03:55 +00:00
|
|
|
if (!DirectMessageThreadFragmentArgs.fromBundle(getArguments()).getThreadId().equals(threadId)) {
|
|
|
|
listViewModel.empty();
|
|
|
|
threadId = DirectMessageThreadFragmentArgs.fromBundle(getArguments()).getThreadId();
|
|
|
|
}
|
|
|
|
threadTitle = DirectMessageThreadFragmentArgs.fromBundle(getArguments()).getTitle();
|
2020-08-30 06:47:04 +00:00
|
|
|
final ActionBar actionBar = fragmentActivity.getSupportActionBar();
|
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setTitle(threadTitle);
|
|
|
|
}
|
2020-08-17 13:49:51 +00:00
|
|
|
binding.swipeRefreshLayout.setEnabled(false);
|
|
|
|
messageList = binding.messageList;
|
|
|
|
messageList.setHasFixedSize(true);
|
|
|
|
binding.commentSend.setOnClickListener(clickListener);
|
|
|
|
binding.image.setOnClickListener(clickListener);
|
2020-09-12 08:51:50 +00:00
|
|
|
final Context context = getContext();
|
|
|
|
if (context == null) return;
|
|
|
|
final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
|
2020-08-17 13:49:51 +00:00
|
|
|
layoutManager.setReverseLayout(true);
|
|
|
|
messageList.setLayoutManager(layoutManager);
|
|
|
|
messageList.addOnScrollListener(new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> {
|
2020-09-10 11:44:25 +00:00
|
|
|
if (TextUtils.isEmpty(cursor) || !hasOlder) {
|
2020-08-17 13:49:51 +00:00
|
|
|
return;
|
2020-07-01 17:08:28 +00:00
|
|
|
}
|
2020-09-05 07:16:11 +00:00
|
|
|
new DirectMessageInboxThreadFetcher(threadId, UserInboxDirection.OLDER, cursor, fetchListener)
|
|
|
|
.execute(); // serial because we don't want messages to be randomly ordered
|
2020-07-01 17:08:28 +00:00
|
|
|
}));
|
2020-08-20 13:05:23 +00:00
|
|
|
final DialogInterface.OnClickListener onDialogListener = (dialogInterface, which) -> {
|
2020-08-20 14:04:17 +00:00
|
|
|
if (which == 0) {
|
2020-07-02 03:20:54 +00:00
|
|
|
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:
|
2020-08-30 01:34:14 +00:00
|
|
|
case CLIP:
|
2020-11-01 19:43:07 +00:00
|
|
|
case FELIX_SHARE:
|
2020-11-04 16:26:29 +00:00
|
|
|
final String shortCode = directItemModel.getMediaModel().getCode();
|
|
|
|
new PostFetcher(shortCode, feedModel -> {
|
|
|
|
final PostViewV2Fragment fragment = PostViewV2Fragment
|
|
|
|
.builder(feedModel)
|
|
|
|
.build();
|
|
|
|
fragment.show(getChildFragmentManager(), "post_view");
|
|
|
|
}).execute();
|
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);
|
2020-09-12 08:51:50 +00:00
|
|
|
linkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
2020-07-03 00:27:54 +00:00
|
|
|
linkIntent.setData(Uri.parse(directItemModel.getLinkModel().getLinkContext().getLinkUrl()));
|
|
|
|
startActivity(linkIntent);
|
|
|
|
break;
|
2020-07-15 00:35:41 +00:00
|
|
|
case TEXT:
|
|
|
|
case REEL_SHARE:
|
2020-09-12 08:51:50 +00:00
|
|
|
Utils.copyText(context, directItemModel.getText());
|
|
|
|
Toast.makeText(context, 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-11-04 14:58:06 +00:00
|
|
|
downloadItem(context);
|
2020-07-03 00:27:54 +00:00
|
|
|
break;
|
|
|
|
case STORY_SHARE:
|
2020-08-05 18:54:49 +00:00
|
|
|
if (directItemModel.getReelShare() != null) {
|
2020-08-29 08:01:42 +00:00
|
|
|
// StoryModel sm = new StoryModel(
|
|
|
|
// directItemModel.getReelShare().getReelId(),
|
|
|
|
// directItemModel.getReelShare().getMedia().getVideoUrl(),
|
|
|
|
// directItemModel.getReelShare().getMedia().getMediaType(),
|
|
|
|
// directItemModel.getTimestamp(),
|
|
|
|
// directItemModel.getReelShare().getReelOwnerName(),
|
|
|
|
// String.valueOf(directItemModel.getReelShare().getReelOwnerId()),
|
|
|
|
// false
|
|
|
|
// );
|
|
|
|
// sm.setVideoUrl(directItemModel.getReelShare().getMedia().getVideoUrl());
|
|
|
|
// StoryModel[] sms = {sm};
|
2020-09-12 08:51:50 +00:00
|
|
|
// startActivity(new Intent(getContext(), StoryViewer.class)
|
2020-08-29 08:01:42 +00:00
|
|
|
// .putExtra(Constants.EXTRAS_USERNAME, directItemModel.getReelShare().getReelOwnerName())
|
|
|
|
// .putExtra(Constants.EXTRAS_STORIES, sms)
|
|
|
|
// );
|
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-09-05 07:16:11 +00:00
|
|
|
Log.d(TAG, "unsupported type " + itemType);
|
2020-07-02 03:20:54 +00:00
|
|
|
}
|
2020-08-30 06:47:04 +00:00
|
|
|
} else if (which == 1) {
|
2020-08-18 20:47:43 +00:00
|
|
|
sendText(null, directItemModel.getItemId(), directItemModel.isLiked());
|
2020-09-12 08:51:50 +00:00
|
|
|
} else if (which == 2) {
|
2020-11-04 14:58:06 +00:00
|
|
|
if (directItemModel == null) {
|
2020-09-12 08:51:50 +00:00
|
|
|
Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
|
2020-11-04 14:58:06 +00:00
|
|
|
} else if (String.valueOf(directItemModel.getUserId()).equals(myId)) {
|
2020-09-02 17:31:09 +00:00
|
|
|
new ThreadAction().execute("delete", directItemModel.getItemId());
|
2020-11-04 14:58:06 +00:00
|
|
|
} else {
|
|
|
|
searchUsername(getUser(directItemModel.getUserId()).getUsername());
|
|
|
|
}
|
2020-08-18 20:47:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
final View.OnClickListener onClickListener = v -> {
|
|
|
|
Object tag = v.getTag();
|
|
|
|
if (tag instanceof ProfileModel) {
|
|
|
|
searchUsername(((ProfileModel) tag).getUsername());
|
2020-08-20 13:05:23 +00:00
|
|
|
} else if (tag instanceof DirectItemModel) {
|
2020-08-18 20:47:43 +00:00
|
|
|
directItemModel = (DirectItemModel) tag;
|
|
|
|
final DirectItemType itemType = directItemModel.getItemType();
|
|
|
|
int firstOption = R.string.dms_inbox_raven_message_unknown;
|
|
|
|
String[] dialogList;
|
|
|
|
|
|
|
|
switch (itemType) {
|
|
|
|
case MEDIA_SHARE:
|
2020-08-30 01:34:14 +00:00
|
|
|
case CLIP:
|
2020-11-01 19:43:07 +00:00
|
|
|
case FELIX_SHARE:
|
2020-08-18 20:47:43 +00:00
|
|
|
firstOption = R.string.view_post;
|
|
|
|
break;
|
|
|
|
case LINK:
|
|
|
|
firstOption = R.string.dms_inbox_open_link;
|
|
|
|
break;
|
|
|
|
case TEXT:
|
|
|
|
case REEL_SHARE:
|
|
|
|
firstOption = R.string.dms_inbox_copy_text;
|
|
|
|
break;
|
|
|
|
case RAVEN_MEDIA:
|
|
|
|
case MEDIA:
|
|
|
|
firstOption = R.string.dms_inbox_download;
|
|
|
|
break;
|
|
|
|
case STORY_SHARE:
|
|
|
|
if (directItemModel.getReelShare() != null) {
|
|
|
|
firstOption = R.string.show_stories;
|
|
|
|
} else if (directItemModel.getText() != null && directItemModel.getText().toString().contains("@")) {
|
|
|
|
firstOption = R.string.open_profile;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PLACEHOLDER:
|
|
|
|
if (directItemModel.getText().toString().contains("@"))
|
|
|
|
firstOption = R.string.open_profile;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dialogList = new String[]{
|
|
|
|
getString(firstOption),
|
|
|
|
getString(directItemModel.isLiked() ? R.string.dms_inbox_unlike : R.string.dms_inbox_like),
|
|
|
|
getString(String.valueOf(directItemModel.getUserId()).equals(myId) ? R.string.dms_inbox_unsend : R.string.dms_inbox_author)
|
|
|
|
};
|
|
|
|
|
2020-09-12 08:51:50 +00:00
|
|
|
dialogAdapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, dialogList);
|
2020-08-18 20:47:43 +00:00
|
|
|
|
2020-09-12 08:51:50 +00:00
|
|
|
new AlertDialog.Builder(context)
|
2020-08-18 20:47:43 +00:00
|
|
|
.setAdapter(dialogAdapter, onDialogListener)
|
|
|
|
.show();
|
|
|
|
}
|
2020-08-17 13:49:51 +00:00
|
|
|
};
|
2020-08-30 12:51:47 +00:00
|
|
|
final MentionClickListener mentionClickListener = (view, text, isHashtag, isLocation) -> searchUsername(text);
|
2020-08-20 13:05:23 +00:00
|
|
|
final DirectMessageItemsAdapter adapter = new DirectMessageItemsAdapter(users, leftUsers, onClickListener, mentionClickListener);
|
2020-08-17 13:49:51 +00:00
|
|
|
messageList.setAdapter(adapter);
|
|
|
|
listViewModel.getList().observe(fragmentActivity, adapter::submitList);
|
2020-08-22 21:03:55 +00:00
|
|
|
if (listViewModel.isEmpty()) {
|
2020-09-05 07:16:11 +00:00
|
|
|
new DirectMessageInboxThreadFetcher(threadId, UserInboxDirection.OLDER, null, fetchListener)
|
|
|
|
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 14:58:06 +00:00
|
|
|
private void downloadItem(final Context context) {
|
|
|
|
final ProfileModel user = getUser(directItemModel.getUserId());
|
|
|
|
final DirectItemModel.DirectItemMediaModel selectedItem = directItemModel.getItemType() == DirectItemType.MEDIA
|
|
|
|
? directItemModel.getMediaModel()
|
|
|
|
: directItemModel.getRavenMediaModel().getMedia();
|
|
|
|
final String url = selectedItem.getMediaType() == MediaItemType.MEDIA_TYPE_VIDEO
|
|
|
|
? selectedItem.getVideoUrl()
|
|
|
|
: selectedItem.getThumbUrl();
|
|
|
|
if (url == null) {
|
|
|
|
Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
|
|
|
|
} else {
|
|
|
|
if (ContextCompat.checkSelfPermission(context, DownloadUtils.PERMS[0]) == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
DownloadUtils.dmDownload(context, user.getUsername(), selectedItem.getId(), url);
|
|
|
|
} else {
|
|
|
|
requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE);
|
|
|
|
}
|
|
|
|
Toast.makeText(context, R.string.downloader_downloading_media, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 07:16:11 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
if (hasSentSomething) {
|
|
|
|
new DirectMessageInboxThreadFetcher(threadId, UserInboxDirection.OLDER, null, fetchListener)
|
|
|
|
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
|
|
|
final ActionBar actionBar = fragmentActivity.getSupportActionBar();
|
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setTitle(threadTitle);
|
2020-08-22 21:03:55 +00:00
|
|
|
}
|
2020-07-01 17:08:28 +00:00
|
|
|
}
|
2020-07-02 03:20:54 +00:00
|
|
|
|
2020-08-30 06:47:04 +00:00
|
|
|
@Override
|
|
|
|
public void onPrepareOptionsMenu(@NonNull final Menu menu) {
|
|
|
|
}
|
|
|
|
|
2020-09-05 07:16:11 +00:00
|
|
|
@Override
|
|
|
|
public void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater) {
|
|
|
|
inflater.inflate(R.menu.dm_thread_menu, menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
|
|
|
|
final int itemId = item.getItemId();
|
|
|
|
switch (itemId) {
|
|
|
|
case R.id.info:
|
|
|
|
final NavDirections action = DirectMessageThreadFragmentDirections.actionDMThreadFragmentToDMSettingsFragment(threadId, threadTitle);
|
|
|
|
NavHostFragment.findNavController(this).navigate(action);
|
|
|
|
return true;
|
|
|
|
case R.id.mark_as_seen:
|
|
|
|
new ThreadAction().execute("seen", lastMessage);
|
|
|
|
item.setVisible(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2020-08-15 21:25:57 +00:00
|
|
|
@Override
|
2020-08-17 13:49:51 +00:00
|
|
|
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
2020-08-15 21:25:57 +00:00
|
|
|
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-08-17 13:49:51 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2020-10-24 16:21:54 +00:00
|
|
|
if (listViewModel != null) listViewModel.getList().postValue(Collections.emptyList());
|
2020-07-02 03:20:54 +00:00
|
|
|
}
|
2020-08-16 09:21:09 +00:00
|
|
|
|
2020-11-04 14:58:06 +00:00
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
final boolean granted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
|
|
|
|
final Context context = getContext();
|
|
|
|
if (context == null) return;
|
|
|
|
if (requestCode == STORAGE_PERM_REQUEST_CODE && granted) {
|
|
|
|
downloadItem(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 20:47:43 +00:00
|
|
|
private void sendText(final String text, final String itemId, final boolean delete) {
|
|
|
|
DirectThreadBroadcaster.TextBroadcastOptions textOptions = null;
|
|
|
|
DirectThreadBroadcaster.ReactionBroadcastOptions reactionOptions = null;
|
|
|
|
if (text != null) {
|
|
|
|
try {
|
|
|
|
textOptions = new DirectThreadBroadcaster.TextBroadcastOptions(text);
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
Log.e(TAG, "Error", e);
|
|
|
|
return;
|
|
|
|
}
|
2020-08-20 13:05:23 +00:00
|
|
|
} else {
|
2020-08-18 20:47:43 +00:00
|
|
|
reactionOptions = new DirectThreadBroadcaster.ReactionBroadcastOptions(itemId, delete);
|
2020-08-16 09:21:09 +00:00
|
|
|
}
|
2020-08-18 20:47:43 +00:00
|
|
|
broadcast(text != null ? textOptions : reactionOptions, result -> {
|
2020-08-16 09:21:09 +00:00
|
|
|
if (result == null || result.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
2020-09-12 08:51:50 +00:00
|
|
|
final Context context = getContext();
|
|
|
|
if (context == null) return;
|
|
|
|
Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
|
2020-08-16 09:21:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-18 20:47:43 +00:00
|
|
|
if (text != null) {
|
|
|
|
binding.commentText.setText("");
|
2020-08-20 13:05:23 +00:00
|
|
|
} else {
|
2020-11-18 16:12:45 +00:00
|
|
|
final View viewWithTag = binding.messageList.findViewWithTag(directItemModel);
|
|
|
|
if (viewWithTag != null) {
|
|
|
|
final ViewParent dim = viewWithTag.getParent();
|
|
|
|
if (dim instanceof View) {
|
|
|
|
final View dimView = (View) dim;
|
|
|
|
final View likedContainer = dimView.findViewById(R.id.liked_container);
|
|
|
|
if (likedContainer != null) {
|
|
|
|
likedContainer.setVisibility(delete ? View.GONE : View.VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 13:05:23 +00:00
|
|
|
}
|
2020-08-18 20:47:43 +00:00
|
|
|
directItemModel.setLiked();
|
|
|
|
}
|
2020-08-29 20:42:08 +00:00
|
|
|
DirectMessageInboxFragment.refreshPlease = true;
|
2020-08-17 13:49:51 +00:00
|
|
|
hasSentSomething = true;
|
2020-09-05 07:16:11 +00:00
|
|
|
new DirectMessageInboxThreadFetcher(threadId, UserInboxDirection.OLDER, null, fetchListener)
|
|
|
|
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2020-08-16 09:21:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendImage(final Uri imageUri) {
|
2020-09-12 08:51:50 +00:00
|
|
|
final Context context = getContext();
|
|
|
|
if (context == null) return;
|
|
|
|
try (InputStream inputStream = context.getContentResolver().openInputStream(imageUri)) {
|
2020-08-18 15:15:13 +00:00
|
|
|
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
|
2020-09-12 08:51:50 +00:00
|
|
|
Toast.makeText(context, R.string.uploading, Toast.LENGTH_SHORT).show();
|
2020-08-18 15:15:13 +00:00
|
|
|
// Upload Image
|
|
|
|
final ImageUploader imageUploader = new ImageUploader();
|
|
|
|
imageUploader.setOnTaskCompleteListener(response -> {
|
|
|
|
if (response == null || response.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
2020-09-12 08:51:50 +00:00
|
|
|
Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
|
2020-08-18 15:15:13 +00:00
|
|
|
if (response != null && response.getResponse() != null) {
|
|
|
|
Log.e(TAG, response.getResponse().toString());
|
|
|
|
}
|
|
|
|
return;
|
2020-08-18 13:22:16 +00:00
|
|
|
}
|
2020-08-18 15:15:13 +00:00
|
|
|
final JSONObject responseJson = response.getResponse();
|
|
|
|
try {
|
|
|
|
final String uploadId = responseJson.getString("upload_id");
|
|
|
|
// Broadcast
|
|
|
|
final DirectThreadBroadcaster.ImageBroadcastOptions options = new DirectThreadBroadcaster.ImageBroadcastOptions(true, uploadId);
|
|
|
|
hasSentSomething = true;
|
2020-09-05 07:16:11 +00:00
|
|
|
broadcast(options,
|
|
|
|
broadcastResponse -> new DirectMessageInboxThreadFetcher(threadId, UserInboxDirection.OLDER, null, fetchListener)
|
|
|
|
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR));
|
2020-08-18 15:15:13 +00:00
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(TAG, "Error parsing json response", e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
final ImageUploadOptions options = ImageUploadOptions.builder(bitmap).build();
|
|
|
|
imageUploader.execute(options);
|
2020-08-20 13:05:23 +00:00
|
|
|
} catch (IOException e) {
|
2020-09-12 08:51:50 +00:00
|
|
|
Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
|
2020-08-18 15:15:13 +00:00
|
|
|
Log.e(TAG, "Error opening file", e);
|
|
|
|
}
|
2020-08-16 09:21:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 07:16:11 +00:00
|
|
|
private void broadcast(final DirectThreadBroadcaster.BroadcastOptions broadcastOptions,
|
|
|
|
final DirectThreadBroadcaster.OnBroadcastCompleteListener listener) {
|
2020-08-16 09:21:09 +00:00
|
|
|
final DirectThreadBroadcaster broadcaster = new DirectThreadBroadcaster(threadId);
|
|
|
|
broadcaster.setOnTaskCompleteListener(listener);
|
|
|
|
broadcaster.execute(broadcastOptions);
|
|
|
|
}
|
2020-08-17 13:49:51 +00:00
|
|
|
|
|
|
|
@NonNull
|
|
|
|
private ProfileModel getUser(final long userId) {
|
|
|
|
ProfileModel result = myProfileHolder;
|
|
|
|
for (final ProfileModel user : users) {
|
|
|
|
if (Long.toString(userId).equals(user.getId())) result = user;
|
|
|
|
}
|
|
|
|
for (final ProfileModel leftUser : leftUsers) {
|
|
|
|
if (Long.toString(userId).equals(leftUser.getId())) result = leftUser;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void searchUsername(final String text) {
|
2020-11-18 16:00:33 +00:00
|
|
|
final Bundle bundle = new Bundle();
|
|
|
|
bundle.putString("username", "@" + text);
|
|
|
|
NavHostFragment.findNavController(this).navigate(R.id.action_global_profileFragment, bundle);
|
2020-08-17 13:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class DirectItemModelListViewModel extends ViewModel {
|
|
|
|
private MutableLiveData<List<DirectItemModel>> list;
|
2020-08-22 21:03:55 +00:00
|
|
|
private boolean isEmpty;
|
2020-08-17 13:49:51 +00:00
|
|
|
|
|
|
|
public MutableLiveData<List<DirectItemModel>> getList() {
|
|
|
|
if (list == null) {
|
|
|
|
list = new MutableLiveData<>();
|
2020-08-22 21:03:55 +00:00
|
|
|
isEmpty = true;
|
2020-08-30 06:47:04 +00:00
|
|
|
} else isEmpty = false;
|
2020-08-17 13:49:51 +00:00
|
|
|
return list;
|
|
|
|
}
|
2020-08-22 21:03:55 +00:00
|
|
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
return isEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void empty() {
|
|
|
|
list = null;
|
|
|
|
isEmpty = true;
|
|
|
|
}
|
2020-08-17 13:49:51 +00:00
|
|
|
}
|
2020-08-19 16:51:41 +00:00
|
|
|
|
2020-09-01 17:37:00 +00:00
|
|
|
class ThreadAction extends AsyncTask<String, Void, Void> {
|
|
|
|
String action, argument;
|
|
|
|
|
|
|
|
protected Void doInBackground(String... rawAction) {
|
|
|
|
action = rawAction[0];
|
|
|
|
argument = rawAction[1];
|
2020-09-02 17:31:09 +00:00
|
|
|
final String url = "https://i.instagram.com/api/v1/direct_v2/threads/" + threadId + "/items/" + argument + "/" + action + "/";
|
2020-08-19 16:51:41 +00:00
|
|
|
try {
|
|
|
|
String urlParameters = "_csrftoken=" + cookie.split("csrftoken=")[1].split(";")[0]
|
2020-08-30 06:47:04 +00:00
|
|
|
+ "&_uuid=" + Utils.settingsHelper.getString(Constants.DEVICE_UUID);
|
2020-08-19 16:51:41 +00:00
|
|
|
final HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
|
|
|
|
urlConnection.setRequestMethod("POST");
|
|
|
|
urlConnection.setUseCaches(false);
|
|
|
|
urlConnection.setRequestProperty("User-Agent", Constants.I_USER_AGENT);
|
|
|
|
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
urlConnection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
|
|
|
|
urlConnection.setDoOutput(true);
|
|
|
|
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
|
|
|
|
wr.writeBytes(urlParameters);
|
|
|
|
wr.flush();
|
|
|
|
wr.close();
|
|
|
|
urlConnection.connect();
|
|
|
|
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
2020-09-07 13:44:56 +00:00
|
|
|
if (action.equals("delete")) hasDeletedSomething = true;
|
|
|
|
else if (action.equals("seen")) DirectMessageInboxFragment.refreshPlease = true;
|
2020-08-19 16:51:41 +00:00
|
|
|
}
|
|
|
|
urlConnection.disconnect();
|
|
|
|
} catch (Throwable ex) {
|
2020-09-01 17:37:00 +00:00
|
|
|
Log.e("austin_debug", action + ": " + ex);
|
2020-08-19 16:51:41 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void result) {
|
|
|
|
if (hasDeletedSomething) {
|
|
|
|
directItemModel = null;
|
2020-09-05 07:16:11 +00:00
|
|
|
new DirectMessageInboxThreadFetcher(threadId, UserInboxDirection.OLDER, null, fetchListener)
|
|
|
|
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2020-08-19 16:51:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 13:49:51 +00:00
|
|
|
}
|