megalodon/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java

171 lines
6.3 KiB
Java
Raw Normal View History

2022-01-14 13:02:10 +01:00
package org.joinmastodon.android;
import android.Manifest;
2022-03-10 16:48:24 +01:00
import android.app.Fragment;
2022-04-03 22:54:31 +02:00
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
2022-01-14 13:02:10 +01:00
import android.os.Bundle;
2022-04-03 22:54:31 +02:00
import android.util.Log;
2022-01-14 13:02:10 +01:00
2022-04-03 22:54:31 +02:00
import org.joinmastodon.android.api.ObjectValidationException;
2022-03-10 16:48:24 +01:00
import org.joinmastodon.android.api.session.AccountSession;
2022-01-14 13:02:10 +01:00
import org.joinmastodon.android.api.session.AccountSessionManager;
2022-05-15 18:14:24 +02:00
import org.joinmastodon.android.fragments.ComposeFragment;
2022-01-14 13:02:10 +01:00
import org.joinmastodon.android.fragments.HomeFragment;
2022-04-03 22:54:31 +02:00
import org.joinmastodon.android.fragments.ProfileFragment;
import org.joinmastodon.android.fragments.ThreadFragment;
2022-03-10 16:48:24 +01:00
import org.joinmastodon.android.fragments.onboarding.AccountActivationFragment;
2022-12-16 14:21:54 +01:00
import org.joinmastodon.android.fragments.onboarding.CustomWelcomeFragment;
2022-04-03 22:54:31 +02:00
import org.joinmastodon.android.model.Notification;
2022-04-06 02:11:15 +02:00
import org.joinmastodon.android.ui.utils.UiUtils;
2022-10-31 07:26:17 +01:00
import org.joinmastodon.android.updater.GithubSelfUpdater;
2022-04-03 22:54:31 +02:00
import org.parceler.Parcels;
2022-01-14 13:02:10 +01:00
import androidx.annotation.Nullable;
import me.grishka.appkit.FragmentStackActivity;
public class MainActivity extends FragmentStackActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
2022-04-06 02:11:15 +02:00
UiUtils.setUserPreferredTheme(this);
2022-01-14 13:02:10 +01:00
super.onCreate(savedInstanceState);
if(savedInstanceState==null){
if(AccountSessionManager.getInstance().getLoggedInAccounts().isEmpty()){
2022-12-16 14:21:54 +01:00
showFragmentClearingBackStack(new CustomWelcomeFragment());
2022-01-14 13:02:10 +01:00
}else{
AccountSessionManager.getInstance().maybeUpdateLocalInfo();
2022-04-03 22:54:31 +02:00
AccountSession session;
2022-01-14 13:02:10 +01:00
Bundle args=new Bundle();
2022-04-03 22:54:31 +02:00
Intent intent=getIntent();
boolean fromNotification = intent.getBooleanExtra("fromNotification", false);
boolean hasNotification = intent.hasExtra("notification");
if(fromNotification){
2022-04-03 22:54:31 +02:00
String accountID=intent.getStringExtra("accountID");
try{
session=AccountSessionManager.getInstance().getAccount(accountID);
if(!hasNotification) args.putString("tab", "notifications");
2022-04-03 22:54:31 +02:00
}catch(IllegalStateException x){
session=AccountSessionManager.getInstance().getLastActiveAccount();
}
}else{
session=AccountSessionManager.getInstance().getLastActiveAccount();
}
2022-03-10 16:48:24 +01:00
args.putString("account", session.getID());
Fragment fragment=session.activated ? new HomeFragment() : new AccountActivationFragment();
2022-01-14 13:02:10 +01:00
fragment.setArguments(args);
if(fromNotification && hasNotification){
2022-04-03 22:54:31 +02:00
Notification notification=Parcels.unwrap(intent.getParcelableExtra("notification"));
showFragmentForNotification(notification, session.getID());
} else if (intent.getBooleanExtra("compose", false)){
2022-05-15 18:14:24 +02:00
showCompose();
} else {
showFragmentClearingBackStack(fragment);
maybeRequestNotificationsPermission();
2022-04-03 22:54:31 +02:00
}
2022-01-14 13:02:10 +01:00
}
}
2022-02-19 01:04:04 +01:00
if(GithubSelfUpdater.needSelfUpdating()){
2022-10-31 07:26:17 +01:00
GithubSelfUpdater.getInstance().maybeCheckForUpdates();
2022-02-19 01:04:04 +01:00
}
2022-01-14 13:02:10 +01:00
}
2022-04-03 22:54:31 +02:00
@Override
protected void onNewIntent(Intent intent){
super.onNewIntent(intent);
if(intent.getBooleanExtra("fromNotification", false)){
String accountID=intent.getStringExtra("accountID");
AccountSession accountSession;
try{
accountSession=AccountSessionManager.getInstance().getAccount(accountID);
}catch(IllegalStateException x){
return;
}
if(intent.hasExtra("notification")){
Notification notification=Parcels.unwrap(intent.getParcelableExtra("notification"));
showFragmentForNotification(notification, accountID);
}else{
AccountSessionManager.getInstance().setLastActiveAccountID(accountID);
Bundle args=new Bundle();
args.putString("account", accountID);
args.putString("tab", "notifications");
Fragment fragment=new HomeFragment();
fragment.setArguments(args);
showFragmentClearingBackStack(fragment);
}
2022-05-15 18:14:24 +02:00
}else if(intent.getBooleanExtra("compose", false)){
showCompose();
2022-10-31 07:26:17 +01:00
}/*else if(intent.hasExtra(PackageInstaller.EXTRA_STATUS) && GithubSelfUpdater.needSelfUpdating()){
GithubSelfUpdater.getInstance().handleIntentFromInstaller(intent, this);
}*/
2022-04-03 22:54:31 +02:00
}
private void showFragmentForNotification(Notification notification, String accountID){
Fragment fragment;
Bundle args=new Bundle();
args.putString("account", accountID);
args.putBoolean("_can_go_back", true);
try{
notification.postprocess();
}catch(ObjectValidationException x){
Log.w("MainActivity", x);
return;
}
if(notification.status!=null){
fragment=new ThreadFragment();
args.putParcelable("status", Parcels.wrap(notification.status));
}else{
fragment=new ProfileFragment();
args.putParcelable("profileAccount", Parcels.wrap(notification.account));
}
fragment.setArguments(args);
showFragment(fragment);
}
2022-05-15 18:14:24 +02:00
private void showCompose(){
AccountSession session=AccountSessionManager.getInstance().getLastActiveAccount();
if(session==null || !session.activated)
return;
ComposeFragment compose=new ComposeFragment();
Bundle composeArgs=new Bundle();
composeArgs.putString("account", session.getID());
compose.setArguments(composeArgs);
showFragment(compose);
}
private void maybeRequestNotificationsPermission(){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.TIRAMISU && checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS)!=PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, 100);
}
}
/**
* when opening app through a notification: if (thread) fragment "can go back", clear back stack
* and show home fragment. upstream's implementation doesn't require this as it opens home first
* and then immediately switches to the notification's ThreadFragment. this causes a black
* screen in megalodon, for some reason, so i'm working around this that way.
*/
@Override
public void onBackPressed() {
Fragment currentFragment = getFragmentManager().findFragmentById(
(fragmentContainers.get(fragmentContainers.size() - 1)).getId()
);
Bundle currentArgs = currentFragment.getArguments();
if (this.fragmentContainers.size() == 1
&& currentArgs != null
&& currentArgs.getBoolean("_can_go_back", false)
&& currentArgs.containsKey("account")) {
Bundle args = new Bundle();
args.putString("account", currentArgs.getString("account"));
args.putString("tab", "notifications");
Fragment fragment=new HomeFragment();
fragment.setArguments(args);
showFragmentClearingBackStack(fragment);
} else {
super.onBackPressed();
}
}
2022-01-14 13:02:10 +01:00
}