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;
|
2018-08-10 13:32:07 +00:00
|
|
|
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;
|
2018-08-11 16:50:30 +00:00
|
|
|
import android.widget.Toast;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
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;
|
|
|
|
|
2018-08-05 12:09:46 +00:00
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
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) {
|
2018-08-05 12:09:46 +00:00
|
|
|
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);
|
|
|
|
|
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-10 16:06:39 +00:00
|
|
|
btnPermissions.setEnabled(false);
|
2018-08-06 12:29:14 +00:00
|
|
|
requestPermissions(permissions, 1);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-04 15:37:42 +00:00
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
2018-08-06 12:29:14 +00:00
|
|
|
|
2018-08-08 05:52:57 +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);
|
2018-08-08 05:52:57 +00:00
|
|
|
prefs.edit().putString("theme", checked ? "dark" : "light").apply();
|
2018-08-04 15:37:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-08 05:52:57 +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) {
|
2018-08-08 05:52:57 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
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
|
2018-08-11 16:50:30 +00:00
|
|
|
new SimpleLoader<Void>() {
|
2018-08-03 16:21:18 +00:00
|
|
|
@Override
|
2018-08-11 16:50:30 +00:00
|
|
|
public Void onLoad(Bundle args) throws Throwable {
|
2018-08-08 05:52:57 +00:00
|
|
|
DB db = DB.getInstance(getContext());
|
2018-08-03 16:21:18 +00:00
|
|
|
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;
|
2018-08-03 16:21:18 +00:00
|
|
|
outbox.synchronize = false;
|
|
|
|
outbox.after = 0;
|
|
|
|
outbox.id = db.folder().insertFolder(outbox);
|
|
|
|
}
|
2018-08-11 16:50:30 +00:00
|
|
|
return null;
|
2018-08-03 16:21:18 +00:00
|
|
|
}
|
2018-08-11 16:50:30 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onException(Bundle args, Throwable ex) {
|
|
|
|
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}.load(this, ActivitySetup.LOADER_CREATE_OUTBOX, new Bundle());
|
2018-08-03 16:21:18 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2018-08-08 05:52:57 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
|
|
|
DB db = DB.getInstance(getContext());
|
|
|
|
|
2018-08-08 07:11:03 +00:00
|
|
|
db.account().liveAccounts(true).observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
|
2018-08-08 05:52:57 +00:00
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable List<EntityAccount> accounts) {
|
|
|
|
tvAccountDone.setVisibility(accounts.size() > 0 ? View.VISIBLE : View.INVISIBLE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-08 07:11:03 +00:00
|
|
|
db.identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() {
|
2018-08-08 05:52:57 +00:00
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable List<EntityIdentity> identities) {
|
|
|
|
tvIdentityDone.setVisibility(identities.size() > 0 ? View.VISIBLE : View.INVISIBLE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-11 16:50:30 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
if (tvAccountDone.getVisibility() == View.VISIBLE)
|
|
|
|
startActivity(new Intent(getContext(), ActivityView.class).putExtra("setup", true));
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|