1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-29 11:15:51 +00:00

Moved setup check to parent fragment

This commit is contained in:
M66B 2020-04-15 11:13:37 +02:00
parent f23cef1279
commit c66b24fa35
2 changed files with 102 additions and 110 deletions

View file

@ -19,16 +19,26 @@ package eu.faircode.email;
Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/
import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
@ -123,6 +133,23 @@ public class FragmentOptions extends FragmentBase {
pager.setAdapter(adapter);
addKeyPressedListener(new ActivityBase.IKeyPressedListener() {
@Override
public boolean onKeyPressed(KeyEvent event) {
return false;
}
@Override
public boolean onBackPressed() {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
onExit();
return true;
} else
return false;
}
});
return view;
}
@ -179,7 +206,79 @@ public class FragmentOptions extends FragmentBase {
getActivity().getIntent().removeExtra("tab");
}
boolean isVisible(int position) {
return (pager.getCurrentItem() == position);
@Override
protected void finish() {
onExit();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
case ActivitySetup.REQUEST_STILL:
if (resultCode == Activity.RESULT_OK)
pager.setCurrentItem(0);
else
super.finish();
break;
}
} catch (Throwable ex) {
Log.e(ex);
}
}
private void onExit() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean setup_reminder = prefs.getBoolean("setup_reminder", true);
boolean hasPermissions = hasPermission(Manifest.permission.READ_CONTACTS);
Boolean isIgnoring = Helper.isIgnoringOptimizations(getContext());
if (!setup_reminder ||
(hasPermissions && (isIgnoring == null || isIgnoring)))
super.finish();
else {
FragmentDialogStill fragment = new FragmentDialogStill();
fragment.setTargetFragment(this, ActivitySetup.REQUEST_STILL);
fragment.show(getParentFragmentManager(), "setup:still");
}
}
public static class FragmentDialogStill extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_setup, null);
CheckBox cbNotAgain = dview.findViewById(R.id.cbNotAgain);
Group grp3 = dview.findViewById(R.id.grp3);
Group grp4 = dview.findViewById(R.id.grp4);
cbNotAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putBoolean("setup_reminder", !isChecked).apply();
}
});
boolean hasPermissions = Helper.hasPermission(getContext(), Manifest.permission.READ_CONTACTS);
Boolean isIgnoring = Helper.isIgnoringOptimizations(getContext());
grp3.setVisibility(hasPermissions ? View.GONE : View.VISIBLE);
grp4.setVisibility(isIgnoring == null || isIgnoring ? View.GONE : View.VISIBLE);
return new AlertDialog.Builder(getContext())
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sendResult(Activity.RESULT_OK);
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
}
}

View file

@ -20,7 +20,6 @@ package eu.faircode.email;
*/
import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
@ -33,17 +32,13 @@ import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
@ -53,7 +48,6 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.constraintlayout.widget.Group;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Observer;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
@ -285,7 +279,7 @@ public class FragmentSetup extends FragmentBase {
btnInbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onExit();
((FragmentBase) getParentFragment()).finish();
}
});
@ -344,23 +338,6 @@ public class FragmentSetup extends FragmentBase {
}
}.execute(this, new Bundle(), "outbox:create");
addKeyPressedListener(new ActivityBase.IKeyPressedListener() {
@Override
public boolean onKeyPressed(KeyEvent event) {
return false;
}
@Override
public boolean onBackPressed() {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED) &&
((FragmentOptions) getParentFragment()).isVisible(0)) {
onExit();
return true;
} else
return false;
}
});
return view;
}
@ -460,54 +437,6 @@ public class FragmentSetup extends FragmentBase {
}
}
private void onExit() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean setup_reminder = prefs.getBoolean("setup_reminder", true);
boolean hasPermissions = hasPermission(Manifest.permission.READ_CONTACTS);
Boolean isIgnoring = Helper.isIgnoringOptimizations(getContext());
if (!setup_reminder ||
(hasPermissions && (isIgnoring == null || isIgnoring)))
((FragmentBase) getParentFragment()).finish();
else {
FragmentDialogStill fragment = new FragmentDialogStill();
fragment.setTargetFragment(FragmentSetup.this, ActivitySetup.REQUEST_STILL);
fragment.show(getParentFragmentManager(), "setup:still");
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == ActivitySetup.REQUEST_STILL)
if (resultCode == Activity.RESULT_OK) {
boolean hasPermissions = hasPermission(Manifest.permission.READ_CONTACTS);
Boolean isIgnoring = Helper.isIgnoringOptimizations(getContext());
final int top;
if (!hasPermissions)
top = view.findViewById(R.id.three).getTop();
else if (isIgnoring != null && !isIgnoring)
top = view.findViewById(R.id.four).getTop();
else
top = 0;
new Handler().post(new Runnable() {
@Override
public void run() {
view.scrollTo(0, top);
}
});
} else
((FragmentBase) getParentFragment()).finish();
} catch (Throwable ex) {
Log.e(ex);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
for (int i = 0; i < permissions.length; i++)
@ -547,40 +476,4 @@ public class FragmentSetup extends FragmentBase {
.create();
}
}
public static class FragmentDialogStill extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_setup, null);
CheckBox cbNotAgain = dview.findViewById(R.id.cbNotAgain);
Group grp3 = dview.findViewById(R.id.grp3);
Group grp4 = dview.findViewById(R.id.grp4);
cbNotAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putBoolean("setup_reminder", !isChecked).apply();
}
});
boolean hasPermissions = Helper.hasPermission(getContext(), Manifest.permission.READ_CONTACTS);
Boolean isIgnoring = Helper.isIgnoringOptimizations(getContext());
grp3.setVisibility(hasPermissions ? View.GONE : View.VISIBLE);
grp4.setVisibility(isIgnoring == null || isIgnoring ? View.GONE : View.VISIBLE);
return new AlertDialog.Builder(getContext())
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sendResult(Activity.RESULT_OK);
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
}
}