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.arch.lifecycle.Observer;
|
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.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v4.app.FragmentTransaction;
|
|
|
|
import android.support.v4.content.ContextCompat;
|
|
|
|
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;
|
2018-08-03 16:21:18 +00:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
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 btnAccountManage;
|
|
|
|
|
|
|
|
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 btnIdentityManage;
|
|
|
|
|
|
|
|
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-03 16:21:18 +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) {
|
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
|
|
|
btnAccountManage = view.findViewById(R.id.btnAccountManage);
|
|
|
|
|
|
|
|
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
|
|
|
btnIdentityManage = view.findViewById(R.id.btnIdentityManage);
|
|
|
|
|
|
|
|
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-02 13:33:06 +00:00
|
|
|
// Wire controls
|
|
|
|
|
|
|
|
btnAccount.setOnClickListener(new View.OnClickListener() {
|
|
|
|
private boolean once;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
once = false;
|
|
|
|
btnAccount.setEnabled(false);
|
|
|
|
pbAccount.setVisibility(View.VISIBLE);
|
|
|
|
|
2018-08-04 14:57:36 +00:00
|
|
|
DB.getInstance(getContext()).account().liveFirstAccount().observe(FragmentSetup.this, new Observer<EntityAccount>() {
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable EntityAccount account) {
|
|
|
|
if (!once) {
|
|
|
|
once = true;
|
2018-08-03 09:58:44 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
if (account != null)
|
|
|
|
args.putLong("id", account.id);
|
2018-08-03 09:58:44 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
FragmentAccount fragment = new FragmentAccount();
|
|
|
|
fragment.setArguments(args);
|
2018-08-03 09:58:44 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
|
|
|
|
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("account");
|
|
|
|
fragmentTransaction.commit();
|
2018-08-04 17:43:37 +00:00
|
|
|
|
|
|
|
btnAccount.setEnabled(true);
|
|
|
|
pbAccount.setVisibility(View.GONE);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-04 15:13:19 +00:00
|
|
|
btnAccountManage.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
//getFragmentManager().popBackStack();
|
|
|
|
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() {
|
|
|
|
private boolean once;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
once = false;
|
|
|
|
btnIdentity.setEnabled(false);
|
|
|
|
pbIdentity.setVisibility(View.VISIBLE);
|
|
|
|
|
2018-08-04 14:57:36 +00:00
|
|
|
DB.getInstance(getContext()).identity().liveFirstIdentity().observe(FragmentSetup.this, new Observer<EntityIdentity>() {
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable EntityIdentity identity) {
|
|
|
|
if (!once) {
|
|
|
|
once = true;
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
if (identity != null)
|
|
|
|
args.putLong("id", identity.id);
|
2018-08-03 09:58:44 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
FragmentIdentity fragment = new FragmentIdentity();
|
|
|
|
fragment.setArguments(args);
|
2018-08-03 09:58:44 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
|
|
|
|
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("identity");
|
|
|
|
fragmentTransaction.commit();
|
|
|
|
}
|
2018-08-04 17:43:37 +00:00
|
|
|
|
|
|
|
btnIdentity.setEnabled(true);
|
|
|
|
pbIdentity.setVisibility(View.GONE);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-04 15:13:19 +00:00
|
|
|
btnIdentityManage.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
//getFragmentManager().popBackStack();
|
|
|
|
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
|
|
|
|
fragmentTransaction.replace(R.id.content_frame, new FragmentIdentities()).addToBackStack("identities");
|
|
|
|
fragmentTransaction.commit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
btnPermissions.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
requestPermissions(permissions, 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-04 15:37:42 +00:00
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
|
|
|
String theme = prefs.getString("theme", "light");
|
|
|
|
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-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);
|
|
|
|
|
2018-08-03 16:21:18 +00:00
|
|
|
final DB db = DB.getInstance(getContext());
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
db.account().liveAccounts(true).observe(this, new Observer<List<EntityAccount>>() {
|
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable List<EntityAccount> accounts) {
|
|
|
|
tvAccountDone.setVisibility(accounts.size() > 0 ? View.VISIBLE : View.INVISIBLE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
db.identity().liveIdentities(true).observe(this, new Observer<List<EntityIdentity>>() {
|
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable List<EntityIdentity> identities) {
|
|
|
|
tvIdentityDone.setVisibility(identities.size() > 0 ? View.VISIBLE : 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-03 16:21:18 +00:00
|
|
|
// Creat outbox
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
EntityFolder outbox = db.folder().getOutbox();
|
|
|
|
if (outbox == null) {
|
|
|
|
outbox = new EntityFolder();
|
|
|
|
outbox.name = "OUTBOX";
|
|
|
|
outbox.type = EntityFolder.TYPE_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 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);
|
|
|
|
}
|
|
|
|
}
|