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

224 lines
8.1 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
This file is part of Safe email.
Safe email 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.
NetGuard 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 NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.Manifest;
import android.content.Intent;
2018-08-04 15:37:42 +00:00
import android.content.SharedPreferences;
2018-08-02 13:33:06 +00:00
import android.content.pm.PackageManager;
import android.os.Bundle;
2018-08-04 15:37:42 +00:00
import android.preference.PreferenceManager;
2018-08-02 13:33:06 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
2018-08-04 15:37:42 +00:00
import android.widget.CheckBox;
import android.widget.CompoundButton;
2018-08-02 13:33:06 +00:00
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
2018-08-02 13:33:06 +00:00
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
public class FragmentSetup extends FragmentEx {
2018-08-02 13:33:06 +00:00
private Button btnAccount;
private ProgressBar pbAccount;
private TextView tvAccountDone;
2018-08-04 15:13:19 +00:00
private Button btnIdentity;
private ProgressBar pbIdentity;
2018-08-02 13:33:06 +00:00
private TextView tvIdentityDone;
2018-08-04 15:13:19 +00:00
private Button btnPermissions;
2018-08-02 13:33:06 +00:00
private TextView tvPermissionsDone;
private Button btnMessages;
2018-08-04 15:37:42 +00:00
private CheckBox cbDarkTheme;
2018-08-06 12:29:14 +00:00
private CheckBox cbDebug;
2018-08-05 18:46:36 +00:00
private ExecutorService executor = Executors.newCachedThreadPool();
2018-08-02 13:33:06 +00:00
private static final String[] permissions = new String[]{
Manifest.permission.READ_CONTACTS
};
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_setup);
2018-08-02 13:33:06 +00:00
View view = inflater.inflate(R.layout.fragment_setup, container, false);
// Get controls
btnAccount = view.findViewById(R.id.btnAccount);
pbAccount = view.findViewById(R.id.pbAccount);
tvAccountDone = view.findViewById(R.id.tvAccountDone);
2018-08-04 15:13:19 +00:00
btnIdentity = view.findViewById(R.id.btnIdentity);
pbIdentity = view.findViewById(R.id.pbIdentity);
2018-08-02 13:33:06 +00:00
tvIdentityDone = view.findViewById(R.id.tvIdentityDone);
2018-08-04 15:13:19 +00:00
btnPermissions = view.findViewById(R.id.btnPermissions);
2018-08-02 13:33:06 +00:00
tvPermissionsDone = view.findViewById(R.id.tvPermissionsDone);
btnMessages = view.findViewById(R.id.btnMessages);
2018-08-04 15:37:42 +00:00
cbDarkTheme = view.findViewById(R.id.cbDarkTheme);
2018-08-06 12:29:14 +00:00
cbDebug = view.findViewById(R.id.cbDebug);
2018-08-05 18:46:36 +00:00
2018-08-02 13:33:06 +00:00
// Wire controls
btnAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2018-08-06 12:29:14 +00:00
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentAccounts()).addToBackStack("accounts");
fragmentTransaction.commit();
2018-08-02 13:33:06 +00:00
}
});
btnIdentity.setOnClickListener(new View.OnClickListener() {
2018-08-05 18:46:36 +00:00
@Override
public void onClick(View view) {
2018-08-04 15:13:19 +00:00
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
2018-08-06 12:29:14 +00:00
fragmentTransaction.replace(R.id.content_frame, new FragmentIdentities()).addToBackStack("identities");
2018-08-04 15:13:19 +00:00
fragmentTransaction.commit();
}
});
2018-08-06 12:29:14 +00:00
btnPermissions.setOnClickListener(new View.OnClickListener() {
2018-08-02 13:33:06 +00:00
@Override
public void onClick(View view) {
2018-08-06 12:29:14 +00:00
requestPermissions(permissions, 1);
2018-08-02 13:33:06 +00:00
}
});
btnMessages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getContext(), ActivityView.class));
getFragmentManager().popBackStack();
}
});
2018-08-04 15:37:42 +00:00
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
2018-08-06 12:29:14 +00:00
String theme = prefs.getString("theme", "light");
2018-08-04 15:37:42 +00:00
boolean dark = "dark".equals(theme);
cbDarkTheme.setTag(dark);
cbDarkTheme.setChecked(dark);
cbDarkTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean checked) {
if (checked != (Boolean) button.getTag()) {
button.setTag(checked);
cbDarkTheme.setChecked(checked);
prefs.edit().putString("theme", checked ? "dark" : "light").apply();
2018-08-04 15:37:42 +00:00
}
}
});
cbDebug.setChecked(prefs.getBoolean("debug", false));
2018-08-06 12:29:14 +00:00
cbDebug.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("debug", checked).apply();
2018-08-06 12:29:14 +00:00
}
});
2018-08-02 13:33:06 +00:00
// Initialize
pbAccount.setVisibility(View.GONE);
pbIdentity.setVisibility(View.GONE);
tvAccountDone.setVisibility(View.INVISIBLE);
tvIdentityDone.setVisibility(View.INVISIBLE);
tvPermissionsDone.setVisibility(View.INVISIBLE);
btnMessages.setEnabled(false);
2018-08-02 13:33:06 +00:00
int[] grantResults = new int[permissions.length];
for (int i = 0; i < permissions.length; i++)
grantResults[i] = ContextCompat.checkSelfPermission(getActivity(), permissions[i]);
onRequestPermissionsResult(0, permissions, grantResults);
2018-08-06 12:29:14 +00:00
// Create outbox
executor.submit(new Runnable() {
@Override
public void run() {
DB db = DB.getInstance(getContext());
EntityFolder outbox = db.folder().getOutbox();
if (outbox == null) {
outbox = new EntityFolder();
outbox.name = "OUTBOX";
2018-08-09 07:02:41 +00:00
outbox.type = EntityFolder.OUTBOX;
outbox.synchronize = false;
outbox.after = 0;
outbox.id = db.folder().insertFolder(outbox);
}
}
});
2018-08-02 13:33:06 +00:00
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DB db = DB.getInstance(getContext());
db.account().liveAccounts(true).observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
@Override
public void onChanged(@Nullable List<EntityAccount> accounts) {
tvAccountDone.setVisibility(accounts.size() > 0 ? View.VISIBLE : View.INVISIBLE);
btnMessages.setEnabled(accounts.size() > 0);
}
});
db.identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() {
@Override
public void onChanged(@Nullable List<EntityIdentity> identities) {
tvIdentityDone.setVisibility(identities.size() > 0 ? View.VISIBLE : View.INVISIBLE);
}
});
}
2018-08-02 13:33:06 +00:00
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
boolean has = (grantResults.length > 0);
for (int result : grantResults)
if (result != PackageManager.PERMISSION_GRANTED) {
has = false;
break;
}
btnPermissions.setEnabled(!has);
tvPermissionsDone.setVisibility(has ? View.VISIBLE : View.INVISIBLE);
}
}