FairEmail/app/src/main/java/eu/faircode/email/ActivitySetup.java

132 lines
4.9 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
Copyright 2018 by Marcel Bokhorst (M66B)
*/
2018-08-04 15:46:22 +00:00
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
2018-08-04 16:54:57 +00:00
import android.view.MenuItem;
2018-08-02 13:33:06 +00:00
2018-08-14 08:31:43 +00:00
import java.util.List;
2018-08-08 06:55:47 +00:00
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
2018-08-17 05:34:46 +00:00
import androidx.lifecycle.Lifecycle;
2018-08-14 08:31:43 +00:00
import androidx.lifecycle.Observer;
2018-08-08 06:55:47 +00:00
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
2018-09-07 08:44:48 +00:00
public class ActivitySetup extends ActivityBilling implements FragmentManager.OnBackStackChangedListener {
private boolean hasAccount;
2018-08-14 08:31:43 +00:00
2018-08-27 14:31:45 +00:00
static final int REQUEST_PERMISSION = 1;
static final int REQUEST_CHOOSE_ACCOUNT = 2;
static final int REQUEST_SOUND = 3;
2018-09-14 11:33:22 +00:00
static final int REQUEST_EXPORT = 4;
static final int REQUEST_IMPORT = 5;
static final int REQUEST_ERROR = 6;
2018-11-19 13:14:02 +00:00
2018-08-04 15:46:22 +00:00
static final String ACTION_EDIT_ACCOUNT = BuildConfig.APPLICATION_ID + ".EDIT_ACCOUNT";
static final String ACTION_EDIT_IDENTITY = BuildConfig.APPLICATION_ID + ".EDIT_IDENTITY";
2018-08-02 13:33:06 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2018-08-04 16:54:57 +00:00
2018-08-02 13:33:06 +00:00
getSupportFragmentManager().addOnBackStackChangedListener(this);
if (getSupportFragmentManager().getFragments().size() == 0) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentSetup()).addToBackStack("setup");
fragmentTransaction.commit();
}
2018-08-14 08:31:43 +00:00
2018-12-16 16:39:15 +00:00
DB.getInstance(this).account().liveAccounts(true).observe(this, new Observer<List<TupleAccountEx>>() {
2018-08-14 08:31:43 +00:00
@Override
2018-12-16 16:39:15 +00:00
public void onChanged(List<TupleAccountEx> accounts) {
2018-08-14 08:31:43 +00:00
hasAccount = (accounts != null && accounts.size() > 0);
}
});
2018-08-02 13:33:06 +00:00
}
2018-08-04 15:46:22 +00:00
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
IntentFilter iff = new IntentFilter();
iff.addAction(ACTION_EDIT_ACCOUNT);
iff.addAction(ACTION_EDIT_IDENTITY);
lbm.registerReceiver(receiver, iff);
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
lbm.unregisterReceiver(receiver);
}
2018-08-04 16:54:57 +00:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
2018-08-17 05:34:46 +00:00
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
getSupportFragmentManager().popBackStack();
2018-08-04 16:54:57 +00:00
return true;
}
return super.onOptionsItemSelected(item);
}
2018-08-02 13:33:06 +00:00
@Override
public void onBackStackChanged() {
2018-08-14 08:31:43 +00:00
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
if (hasAccount)
startActivity(new Intent(this, ActivityView.class));
2018-08-02 13:33:06 +00:00
finish();
2018-08-14 08:31:43 +00:00
}
2018-08-02 13:33:06 +00:00
}
2018-08-04 15:46:22 +00:00
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION_EDIT_ACCOUNT.equals(intent.getAction())) {
FragmentAccount fragment = new FragmentAccount();
fragment.setArguments(intent.getExtras());
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("account");
fragmentTransaction.commit();
} else if (ACTION_EDIT_IDENTITY.equals(intent.getAction())) {
FragmentIdentity fragment = new FragmentIdentity();
fragment.setArguments(intent.getExtras());
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("identity");
fragmentTransaction.commit();
}
}
};
2018-08-02 13:33:06 +00:00
}