2023-01-22 15:22:59 +01:00
|
|
|
package app.fedilab.android.mastodon.activities;
|
2022-04-27 15:20:42 +02:00
|
|
|
/* Copyright 2021 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of Fedilab
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
|
2022-06-21 17:09:34 +02:00
|
|
|
import static app.fedilab.android.BaseMainActivity.currentAccount;
|
2023-07-14 11:11:27 +02:00
|
|
|
import static app.fedilab.android.BaseMainActivity.currentInstance;
|
2022-06-14 19:17:35 +02:00
|
|
|
|
2022-12-09 18:07:44 +01:00
|
|
|
import android.content.Intent;
|
2022-04-27 15:20:42 +02:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2022-11-08 09:52:20 +01:00
|
|
|
import android.util.TypedValue;
|
2022-04-27 15:20:42 +02:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.appcompat.app.ActionBar;
|
|
|
|
import androidx.fragment.app.Fragment;
|
2022-06-14 19:17:35 +02:00
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
2022-04-27 15:20:42 +02:00
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
2022-12-24 10:32:12 +01:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2022-12-10 10:38:01 +01:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2022-06-30 14:44:52 +02:00
|
|
|
import app.fedilab.android.BaseMainActivity;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.R;
|
2023-01-22 15:22:59 +01:00
|
|
|
import app.fedilab.android.activities.MainActivity;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.databinding.ActivityConversationBinding;
|
2023-01-22 15:22:59 +01:00
|
|
|
import app.fedilab.android.mastodon.client.entities.api.Status;
|
|
|
|
import app.fedilab.android.mastodon.client.entities.app.StatusCache;
|
|
|
|
import app.fedilab.android.mastodon.exception.DBException;
|
|
|
|
import app.fedilab.android.mastodon.helper.Helper;
|
|
|
|
import app.fedilab.android.mastodon.helper.MastodonHelper;
|
|
|
|
import app.fedilab.android.mastodon.ui.drawer.StatusAdapter;
|
|
|
|
import app.fedilab.android.mastodon.ui.fragment.timeline.FragmentMastodonContext;
|
|
|
|
import app.fedilab.android.mastodon.viewmodel.mastodon.StatusesVM;
|
2022-12-09 18:07:44 +01:00
|
|
|
import es.dmoral.toasty.Toasty;
|
2022-04-27 15:20:42 +02:00
|
|
|
|
2022-12-09 18:07:44 +01:00
|
|
|
public class ContextActivity extends BaseActivity implements FragmentMastodonContext.FirstMessage {
|
2022-04-27 15:20:42 +02:00
|
|
|
|
|
|
|
public static boolean expand;
|
|
|
|
public static boolean displayCW;
|
2022-04-27 18:15:32 +02:00
|
|
|
public static Resources.Theme theme;
|
2022-04-27 15:20:42 +02:00
|
|
|
Fragment currentFragment;
|
2022-12-09 18:07:44 +01:00
|
|
|
private Status firstMessage;
|
|
|
|
private String remote_instance;
|
2023-07-14 11:11:27 +02:00
|
|
|
private Status focusedStatus;
|
2023-08-25 14:14:53 +02:00
|
|
|
private String focusedStatusURI;
|
2023-07-14 11:11:27 +02:00
|
|
|
private boolean checkRemotely;
|
2022-04-27 15:20:42 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2022-11-28 10:37:57 +01:00
|
|
|
|
2022-12-09 18:07:44 +01:00
|
|
|
ActivityConversationBinding binding = ActivityConversationBinding.inflate(getLayoutInflater());
|
2022-04-27 15:20:42 +02:00
|
|
|
setContentView(binding.getRoot());
|
|
|
|
setSupportActionBar(binding.toolbar);
|
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
//Remove title
|
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setDisplayShowTitleEnabled(false);
|
|
|
|
}
|
|
|
|
binding.title.setText(R.string.context_conversation);
|
2022-11-08 09:52:20 +01:00
|
|
|
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
float scale = sharedpreferences.getFloat(getString(R.string.SET_FONT_SCALE), 1.1f);
|
|
|
|
binding.title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale);
|
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
if (getSupportActionBar() != null) {
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
getSupportActionBar().setDisplayShowHomeEnabled(true);
|
|
|
|
}
|
|
|
|
Bundle b = getIntent().getExtras();
|
|
|
|
displayCW = sharedpreferences.getBoolean(getString(R.string.SET_EXPAND_CW), false);
|
2023-07-14 11:11:27 +02:00
|
|
|
focusedStatus = null; // or other values
|
2022-12-09 18:07:44 +01:00
|
|
|
if (b != null) {
|
2022-04-27 15:20:42 +02:00
|
|
|
focusedStatus = (Status) b.getSerializable(Helper.ARG_STATUS);
|
2022-12-09 18:07:44 +01:00
|
|
|
remote_instance = b.getString(Helper.ARG_REMOTE_INSTANCE, null);
|
2023-08-25 14:14:53 +02:00
|
|
|
focusedStatusURI = b.getString(Helper.ARG_FOCUSED_STATUS_URI, null);
|
|
|
|
|
2022-12-09 18:07:44 +01:00
|
|
|
}
|
2022-07-10 10:41:07 +02:00
|
|
|
if (focusedStatus == null || currentAccount == null || currentAccount.mastodon_account == null) {
|
2022-04-27 15:20:42 +02:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2023-08-25 14:14:53 +02:00
|
|
|
if (focusedStatusURI == null && remote_instance == null) {
|
|
|
|
focusedStatusURI = focusedStatus.uri;
|
|
|
|
}
|
2022-06-21 17:09:34 +02:00
|
|
|
MastodonHelper.loadPPMastodon(binding.profilePicture, currentAccount.mastodon_account);
|
2023-07-14 11:11:27 +02:00
|
|
|
|
|
|
|
checkRemotely = sharedpreferences.getBoolean(getString(R.string.SET_CONVERSATION_REMOTELY), false);
|
|
|
|
if (!checkRemotely) {
|
|
|
|
loadLocalConversation();
|
|
|
|
} else {
|
|
|
|
loadRemotelyConversation(true);
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-22 11:00:12 +01:00
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(@NonNull Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
outState.clear();
|
|
|
|
}
|
|
|
|
|
2023-07-14 11:11:27 +02:00
|
|
|
private void loadLocalConversation() {
|
2022-04-27 15:20:42 +02:00
|
|
|
Bundle bundle = new Bundle();
|
2022-07-16 09:21:43 +02:00
|
|
|
bundle.putSerializable(Helper.ARG_STATUS, focusedStatus);
|
2022-12-09 18:07:44 +01:00
|
|
|
bundle.putString(Helper.ARG_REMOTE_INSTANCE, remote_instance);
|
|
|
|
FragmentMastodonContext fragmentMastodonContext = new FragmentMastodonContext();
|
|
|
|
fragmentMastodonContext.firstMessage = this;
|
|
|
|
currentFragment = Helper.addFragment(getSupportFragmentManager(), R.id.nav_host_fragment_content_main, fragmentMastodonContext, bundle, null, null);
|
2023-07-14 11:11:27 +02:00
|
|
|
//Update the status
|
2022-12-09 18:07:44 +01:00
|
|
|
if (remote_instance == null) {
|
|
|
|
StatusesVM timelinesVM = new ViewModelProvider(ContextActivity.this).get(StatusesVM.class);
|
|
|
|
timelinesVM.getStatus(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, focusedStatus.id).observe(ContextActivity.this, status -> {
|
|
|
|
if (status != null) {
|
|
|
|
StatusCache statusCache = new StatusCache();
|
|
|
|
statusCache.instance = BaseMainActivity.currentInstance;
|
|
|
|
statusCache.user_id = BaseMainActivity.currentUserID;
|
|
|
|
statusCache.status = status;
|
|
|
|
statusCache.status_id = status.id;
|
|
|
|
//Update cache
|
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
|
|
|
new StatusCache(getApplication()).updateIfExists(statusCache);
|
|
|
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
|
|
//Update UI
|
2023-01-22 15:22:59 +01:00
|
|
|
Runnable myRunnable = () -> StatusAdapter.sendAction(ContextActivity.this, Helper.ARG_STATUS_ACTION, status, null);
|
2022-12-09 18:07:44 +01:00
|
|
|
mainHandler.post(myRunnable);
|
|
|
|
} catch (DBException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(@NonNull Menu menu) {
|
|
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
|
getMenuInflater().inflate(R.menu.menu_context, menu);
|
|
|
|
MenuItem itemExpand = menu.findItem(R.id.action_expand);
|
|
|
|
if (expand) {
|
|
|
|
itemExpand.setIcon(R.drawable.ic_baseline_expand_less_24);
|
|
|
|
} else {
|
|
|
|
itemExpand.setIcon(R.drawable.ic_baseline_expand_more_24);
|
|
|
|
}
|
|
|
|
MenuItem itemDisplayCW = menu.findItem(R.id.action_show_cw);
|
|
|
|
if (displayCW) {
|
|
|
|
itemDisplayCW.setIcon(R.drawable.ic_baseline_remove_red_eye_24);
|
|
|
|
} else {
|
|
|
|
itemDisplayCW.setIcon(R.drawable.ic_outline_remove_red_eye_24);
|
|
|
|
}
|
2022-12-09 18:07:44 +01:00
|
|
|
MenuItem action_remote = menu.findItem(R.id.action_remote);
|
2023-07-14 11:11:27 +02:00
|
|
|
if (remote_instance != null || checkRemotely) {
|
2022-12-09 18:07:44 +01:00
|
|
|
action_remote.setVisible(false);
|
2022-12-10 10:38:01 +01:00
|
|
|
} else {
|
2022-12-10 10:57:28 +01:00
|
|
|
if (firstMessage != null && !firstMessage.visibility.equalsIgnoreCase("direct") && !firstMessage.visibility.equalsIgnoreCase("private")) {
|
|
|
|
Pattern pattern = Helper.statusIdInUrl;
|
|
|
|
Matcher matcher = pattern.matcher(firstMessage.uri);
|
|
|
|
action_remote.setVisible(matcher.find());
|
|
|
|
} else {
|
|
|
|
action_remote.setVisible(false);
|
|
|
|
}
|
2022-12-09 18:07:44 +01:00
|
|
|
}
|
2022-04-27 15:20:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCurrentFragment(FragmentMastodonContext fragmentMastodonContext) {
|
|
|
|
currentFragment = fragmentMastodonContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
if (item.getItemId() == android.R.id.home) {
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
} else if (item.getItemId() == R.id.action_expand) {
|
|
|
|
expand = !expand;
|
|
|
|
if (currentFragment != null && currentFragment instanceof FragmentMastodonContext) {
|
|
|
|
((FragmentMastodonContext) currentFragment).redraw();
|
|
|
|
}
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
} else if (item.getItemId() == R.id.action_show_cw) {
|
|
|
|
displayCW = !displayCW;
|
|
|
|
if (currentFragment != null && currentFragment instanceof FragmentMastodonContext) {
|
|
|
|
((FragmentMastodonContext) currentFragment).refresh();
|
|
|
|
}
|
|
|
|
invalidateOptionsMenu();
|
2022-12-09 18:07:44 +01:00
|
|
|
} else if (item.getItemId() == R.id.action_remote) {
|
2023-07-14 11:11:27 +02:00
|
|
|
loadRemotelyConversation(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-09 18:07:44 +01:00
|
|
|
|
2023-07-14 11:11:27 +02:00
|
|
|
private void loadRemotelyConversation(boolean fallback) {
|
|
|
|
if (fallback) {
|
|
|
|
StatusesVM statusesVM;
|
|
|
|
statusesVM = new ViewModelProvider(this).get(StatusesVM.class);
|
|
|
|
statusesVM.getContext(currentInstance, null, focusedStatus.id)
|
|
|
|
.observe(this, result -> {
|
|
|
|
if (result != null && result.ancestors != null && result.ancestors.size() > 0) {
|
|
|
|
firstMessage = result.ancestors.get(0);
|
|
|
|
String instance = null;
|
|
|
|
try {
|
|
|
|
URL url = new URL(firstMessage.uri);
|
|
|
|
instance = url.getHost();
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
if (instance == null) {
|
|
|
|
loadLocalConversation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Pattern pattern = Helper.statusIdInUrl;
|
|
|
|
Matcher matcher = pattern.matcher(firstMessage.uri);
|
|
|
|
String remoteId = null;
|
|
|
|
if (matcher.find()) {
|
|
|
|
remoteId = matcher.group(1);
|
|
|
|
}
|
|
|
|
if (remoteId == null) {
|
|
|
|
loadLocalConversation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String finalInstance = instance;
|
|
|
|
statusesVM.getStatus(instance, null, remoteId).observe(ContextActivity.this, status -> {
|
|
|
|
if (status != null) {
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
bundle.putSerializable(Helper.ARG_STATUS, status);
|
|
|
|
bundle.putString(Helper.ARG_REMOTE_INSTANCE, finalInstance);
|
2023-08-25 14:14:53 +02:00
|
|
|
bundle.putString(Helper.ARG_FOCUSED_STATUS_URI, focusedStatusURI);
|
2023-07-14 11:11:27 +02:00
|
|
|
FragmentMastodonContext fragmentMastodonContext = new FragmentMastodonContext();
|
|
|
|
fragmentMastodonContext.firstMessage = ContextActivity.this;
|
|
|
|
currentFragment = Helper.addFragment(getSupportFragmentManager(), R.id.nav_host_fragment_content_main, fragmentMastodonContext, bundle, null, null);
|
|
|
|
} else {
|
|
|
|
loadLocalConversation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
loadLocalConversation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2022-12-09 18:07:44 +01:00
|
|
|
if (firstMessage == null) {
|
|
|
|
Toasty.warning(ContextActivity.this, getString(R.string.toast_try_later), Toasty.LENGTH_SHORT).show();
|
2023-07-14 11:11:27 +02:00
|
|
|
return;
|
2022-12-09 18:07:44 +01:00
|
|
|
}
|
|
|
|
if (firstMessage.account.acct != null) {
|
2022-12-24 10:32:12 +01:00
|
|
|
String instance = null;
|
|
|
|
try {
|
|
|
|
URL url = new URL(firstMessage.uri);
|
|
|
|
instance = url.getHost();
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
if (instance == null) {
|
|
|
|
Toasty.info(ContextActivity.this, getString(R.string.toast_error_fetch_message), Toasty.LENGTH_SHORT).show();
|
2023-07-14 11:11:27 +02:00
|
|
|
return;
|
2022-12-24 10:32:12 +01:00
|
|
|
}
|
|
|
|
if (instance.equalsIgnoreCase(MainActivity.currentInstance)) {
|
2022-12-09 18:07:44 +01:00
|
|
|
Toasty.info(ContextActivity.this, getString(R.string.toast_on_your_instance), Toasty.LENGTH_SHORT).show();
|
2023-07-14 11:11:27 +02:00
|
|
|
return;
|
2022-12-09 18:07:44 +01:00
|
|
|
}
|
2022-12-10 10:38:01 +01:00
|
|
|
Pattern pattern = Helper.statusIdInUrl;
|
|
|
|
Matcher matcher = pattern.matcher(firstMessage.uri);
|
|
|
|
String remoteId = null;
|
|
|
|
if (matcher.find()) {
|
|
|
|
remoteId = matcher.group(1);
|
|
|
|
}
|
|
|
|
if (remoteId != null) {
|
|
|
|
StatusesVM statusesVM = new ViewModelProvider(ContextActivity.this).get(StatusesVM.class);
|
2022-12-24 10:32:12 +01:00
|
|
|
String finalInstance = instance;
|
2022-12-10 10:38:01 +01:00
|
|
|
statusesVM.getStatus(instance, null, remoteId).observe(ContextActivity.this, status -> {
|
2022-12-09 18:07:44 +01:00
|
|
|
if (status != null) {
|
|
|
|
Intent intentContext = new Intent(ContextActivity.this, ContextActivity.class);
|
|
|
|
intentContext.putExtra(Helper.ARG_STATUS, status);
|
2023-08-25 14:14:53 +02:00
|
|
|
intentContext.putExtra(Helper.ARG_FOCUSED_STATUS_URI, focusedStatusURI);
|
2022-12-24 10:32:12 +01:00
|
|
|
intentContext.putExtra(Helper.ARG_REMOTE_INSTANCE, finalInstance);
|
2022-12-09 18:07:44 +01:00
|
|
|
intentContext.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
startActivity(intentContext);
|
|
|
|
} else {
|
|
|
|
Toasty.warning(ContextActivity.this, getString(R.string.toast_error_fetch_message), Toasty.LENGTH_SHORT).show();
|
|
|
|
}
|
2022-12-10 10:38:01 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Toasty.warning(ContextActivity.this, getString(R.string.toast_error_fetch_message), Toasty.LENGTH_SHORT).show();
|
|
|
|
}
|
2022-12-09 18:07:44 +01:00
|
|
|
} else {
|
|
|
|
Toasty.warning(ContextActivity.this, getString(R.string.toast_error_fetch_message), Toasty.LENGTH_SHORT).show();
|
|
|
|
}
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 18:07:44 +01:00
|
|
|
@Override
|
|
|
|
public void get(Status status) {
|
|
|
|
firstMessage = status;
|
2022-12-10 10:38:01 +01:00
|
|
|
invalidateOptionsMenu();
|
2022-12-09 18:07:44 +01:00
|
|
|
}
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|