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.
|
|
|
|
|
|
|
|
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-09-04 18:06:22 +00:00
|
|
|
import android.annotation.TargetApi;
|
2018-08-12 07:51:33 +00:00
|
|
|
import android.content.Context;
|
2018-09-04 18:06:22 +00:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
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;
|
2018-09-07 12:34:54 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2018-09-04 18:06:22 +00:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.os.Bundle;
|
2018-09-04 18:06:22 +00:00
|
|
|
import android.os.PowerManager;
|
2018-08-04 15:37:42 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2018-09-04 18:06:22 +00:00
|
|
|
import android.provider.Settings;
|
|
|
|
import android.util.Log;
|
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.CompoundButton;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.widget.TextView;
|
2018-08-11 16:50:30 +00:00
|
|
|
import android.widget.Toast;
|
2018-09-07 12:34:54 +00:00
|
|
|
import android.widget.ToggleButton;
|
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;
|
2018-09-04 18:06:22 +00:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
2018-08-08 06:55:47 +00:00
|
|
|
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 TextView tvAccountDone;
|
2018-08-04 15:13:19 +00:00
|
|
|
|
|
|
|
private Button btnIdentity;
|
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-09-04 18:06:22 +00:00
|
|
|
private Button btnDoze;
|
|
|
|
private TextView tvDozeDone;
|
|
|
|
|
|
|
|
private Button btnData;
|
|
|
|
|
2018-09-07 12:34:54 +00:00
|
|
|
private ToggleButton tbDarkTheme;
|
2018-08-16 18:19:09 +00:00
|
|
|
|
|
|
|
private Button btnOptions;
|
2018-08-05 18:46:36 +00:00
|
|
|
|
2018-09-07 12:34:54 +00:00
|
|
|
private Drawable check;
|
|
|
|
|
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-09-07 12:34:54 +00:00
|
|
|
check = getResources().getDrawable(R.drawable.baseline_check_24, getContext().getTheme());
|
|
|
|
|
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);
|
|
|
|
tvAccountDone = view.findViewById(R.id.tvAccountDone);
|
2018-08-04 15:13:19 +00:00
|
|
|
|
|
|
|
btnIdentity = view.findViewById(R.id.btnIdentity);
|
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-09-04 18:06:22 +00:00
|
|
|
btnDoze = view.findViewById(R.id.btnDoze);
|
|
|
|
tvDozeDone = view.findViewById(R.id.tvDozeDone);
|
|
|
|
|
|
|
|
btnData = view.findViewById(R.id.btnData);
|
|
|
|
|
2018-09-07 12:34:54 +00:00
|
|
|
tbDarkTheme = view.findViewById(R.id.tbDarkTheme);
|
2018-08-16 18:19:09 +00:00
|
|
|
btnOptions = view.findViewById(R.id.btnOptions);
|
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-09-04 18:06:22 +00:00
|
|
|
btnDoze.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
new AlertDialog.Builder(getContext())
|
|
|
|
.setMessage(R.string.title_setup_doze_instructions)
|
|
|
|
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
try {
|
|
|
|
startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS));
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.create()
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
btnData.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
@TargetApi(Build.VERSION_CODES.N)
|
|
|
|
public void onClick(View v) {
|
|
|
|
try {
|
|
|
|
startActivity(new Intent(Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS,
|
|
|
|
Uri.parse("package:" + BuildConfig.APPLICATION_ID)));
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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);
|
2018-09-07 12:34:54 +00:00
|
|
|
tbDarkTheme.setTag(dark);
|
|
|
|
tbDarkTheme.setChecked(dark);
|
|
|
|
tbDarkTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
2018-08-04 15:37:42 +00:00
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton button, boolean checked) {
|
|
|
|
if (checked != (Boolean) button.getTag()) {
|
|
|
|
button.setTag(checked);
|
2018-09-07 12:34:54 +00:00
|
|
|
tbDarkTheme.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-16 18:19:09 +00:00
|
|
|
btnOptions.setOnClickListener(new View.OnClickListener() {
|
2018-08-06 12:29:14 +00:00
|
|
|
@Override
|
2018-08-16 18:19:09 +00:00
|
|
|
public void onClick(View view) {
|
|
|
|
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
|
|
|
|
fragmentTransaction.replace(R.id.content_frame, new FragmentOptions()).addToBackStack("options");
|
|
|
|
fragmentTransaction.commit();
|
2018-08-06 12:29:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
// Initialize
|
|
|
|
|
2018-09-04 18:06:22 +00:00
|
|
|
tvAccountDone.setText(null);
|
2018-09-07 12:34:54 +00:00
|
|
|
tvAccountDone.setCompoundDrawables(null, null, null, null);
|
|
|
|
|
2018-09-04 12:32:23 +00:00
|
|
|
btnIdentity.setEnabled(false);
|
2018-09-04 18:06:22 +00:00
|
|
|
tvIdentityDone.setText(null);
|
2018-09-07 12:34:54 +00:00
|
|
|
tvIdentityDone.setCompoundDrawables(null, null, null, null);
|
|
|
|
|
2018-09-04 18:06:22 +00:00
|
|
|
tvPermissionsDone.setText(null);
|
2018-09-07 12:34:54 +00:00
|
|
|
tvPermissionsDone.setCompoundDrawables(null, null, null, null);
|
|
|
|
|
2018-09-04 18:06:22 +00:00
|
|
|
btnDoze.setEnabled(false);
|
|
|
|
tvDozeDone.setText(null);
|
2018-09-07 12:34:54 +00:00
|
|
|
tvDozeDone.setCompoundDrawables(null, null, null, null);
|
|
|
|
|
2018-09-04 18:06:22 +00:00
|
|
|
btnData.setVisibility(View.GONE);
|
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
|
2018-08-12 07:51:33 +00:00
|
|
|
new SimpleTask<Void>() {
|
2018-08-03 16:21:18 +00:00
|
|
|
@Override
|
2018-08-12 16:14:20 +00:00
|
|
|
protected Void onLoad(Context context, Bundle args) {
|
2018-08-12 07:51:33 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2018-08-12 12:47:52 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
EntityFolder outbox = db.folder().getOutbox();
|
|
|
|
if (outbox == null) {
|
|
|
|
outbox = new EntityFolder();
|
|
|
|
outbox.name = "OUTBOX";
|
|
|
|
outbox.type = EntityFolder.OUTBOX;
|
|
|
|
outbox.synchronize = false;
|
|
|
|
outbox.after = 0;
|
|
|
|
outbox.id = db.folder().insertFolder(outbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2018-08-03 16:21:18 +00:00
|
|
|
}
|
2018-08-12 12:47:52 +00:00
|
|
|
|
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
|
2018-08-12 07:51:33 +00:00
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2018-08-11 16:50:30 +00:00
|
|
|
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
2018-08-12 07:51:33 +00:00
|
|
|
}.load(this, 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) {
|
2018-09-07 12:34:54 +00:00
|
|
|
boolean done = (accounts != null && accounts.size() > 0);
|
|
|
|
btnIdentity.setEnabled(done);
|
|
|
|
tvAccountDone.setText(done ? R.string.title_setup_done : R.string.title_setup_to_do);
|
|
|
|
tvAccountDone.setCompoundDrawablesWithIntrinsicBounds(done ? check : null, null, null, null);
|
2018-08-08 05:52:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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) {
|
2018-09-07 12:34:54 +00:00
|
|
|
boolean done = (identities != null && identities.size() > 0);
|
|
|
|
tvIdentityDone.setText(done ? R.string.title_setup_done : R.string.title_setup_to_do);
|
|
|
|
tvIdentityDone.setCompoundDrawablesWithIntrinsicBounds(done ? check : null, null, null, null);
|
2018-08-08 05:52:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-04 18:06:22 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
PowerManager pm = getContext().getSystemService(PowerManager.class);
|
|
|
|
boolean ignoring = pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID);
|
|
|
|
btnDoze.setEnabled(!ignoring);
|
|
|
|
tvDozeDone.setText(ignoring ? R.string.title_setup_done : R.string.title_setup_to_do);
|
2018-09-07 12:34:54 +00:00
|
|
|
tvDozeDone.setCompoundDrawablesWithIntrinsicBounds(ignoring ? check : null, null, null, null);
|
2018-09-04 18:06:22 +00:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
ConnectivityManager cm = getContext().getSystemService(ConnectivityManager.class);
|
|
|
|
boolean saving = (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED);
|
|
|
|
btnData.setVisibility(saving ? View.VISIBLE : View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2018-08-15 11:34:21 +00:00
|
|
|
tvPermissionsDone.setText(has ? R.string.title_setup_done : R.string.title_setup_to_do);
|
2018-09-07 12:34:54 +00:00
|
|
|
tvPermissionsDone.setCompoundDrawablesWithIntrinsicBounds(has ? check : null, null, null, null);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|