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

189 lines
5.9 KiB
Java
Raw Normal View History

package eu.faircode.email;
2019-10-20 10:15:13 +00:00
/*
This file is part of FairEmail.
FairEmail 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.
FairEmail 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 FairEmail. If not, see <http://www.gnu.org/licenses/>.
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2019-10-20 10:15:13 +00:00
*/
2019-10-05 14:09:52 +00:00
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
2019-08-21 12:01:00 +00:00
import android.os.Bundle;
2019-10-05 14:09:52 +00:00
import android.widget.Toast;
import androidx.annotation.NonNull;
2019-08-04 07:37:46 +00:00
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
2019-08-04 07:37:46 +00:00
import androidx.fragment.app.FragmentManager;
2019-08-21 12:01:00 +00:00
import androidx.lifecycle.Lifecycle;
2019-09-11 12:03:59 +00:00
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import static android.app.Activity.RESULT_CANCELED;
2019-09-11 19:43:27 +00:00
public class FragmentDialogBase extends DialogFragment {
2019-07-01 14:46:40 +00:00
private boolean once = false;
2019-09-11 12:03:59 +00:00
private LifecycleOwner owner;
private LifecycleRegistry registry;
2020-09-17 14:22:42 +00:00
private String targetRequestKey;
2020-05-18 16:34:49 +00:00
private int targetRequestCode;
2019-07-01 14:46:40 +00:00
2019-08-21 12:01:00 +00:00
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2020-09-17 14:22:42 +00:00
2019-09-11 12:03:59 +00:00
owner = new LifecycleOwner() {
@NonNull
@Override
public Lifecycle getLifecycle() {
return registry;
}
};
registry = new LifecycleRegistry(owner);
registry.setCurrentState(Lifecycle.State.CREATED);
2020-09-17 14:22:42 +00:00
if (savedInstanceState != null) {
targetRequestKey = savedInstanceState.getString("fair:key");
targetRequestCode = savedInstanceState.getInt("fair:code");
}
2019-08-21 12:01:00 +00:00
Log.i("Create " + this);
}
2020-09-17 14:22:42 +00:00
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putString("fair:key", targetRequestKey);
outState.putInt("fair:code", targetRequestCode);
super.onSaveInstanceState(outState);
}
2019-08-21 12:01:00 +00:00
@Override
public void onResume() {
2019-09-11 12:03:59 +00:00
registry.setCurrentState(Lifecycle.State.RESUMED);
2019-08-21 12:01:00 +00:00
super.onResume();
2019-12-07 17:30:43 +00:00
Log.d("Resume " + this);
2019-08-21 12:01:00 +00:00
}
@Override
public void onPause() {
2019-09-11 12:03:59 +00:00
registry.setCurrentState(Lifecycle.State.STARTED);
2019-08-21 12:01:00 +00:00
super.onPause();
2019-12-07 17:30:43 +00:00
Log.d("Pause " + this);
2019-08-21 12:01:00 +00:00
}
@Override
public void onDestroy() {
2019-09-11 12:03:59 +00:00
registry.setCurrentState(Lifecycle.State.DESTROYED);
2019-08-21 12:01:00 +00:00
super.onDestroy();
Log.i("Destroy " + this);
}
2019-07-05 18:54:07 +00:00
@Override
public void onStart() {
2019-09-11 12:03:59 +00:00
registry.setCurrentState(Lifecycle.State.STARTED);
2019-07-05 18:54:07 +00:00
try {
super.onStart();
} catch (Throwable ex) {
Log.e(ex);
}
2019-12-07 17:30:43 +00:00
Log.d("Start " + this);
2019-09-11 12:03:59 +00:00
}
@Override
public void onStop() {
registry.setCurrentState(Lifecycle.State.CREATED);
super.onStop();
2019-12-07 17:30:43 +00:00
Log.d("Stop " + this);
2019-09-11 12:03:59 +00:00
}
2019-09-11 17:32:17 +00:00
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
2019-10-08 08:48:31 +00:00
String action = (data == null ? null : data.getAction());
2019-09-11 17:32:17 +00:00
Log.i("Result class=" + this.getClass().getSimpleName() +
2019-10-08 08:48:31 +00:00
" action=" + action + " request=" + requestCode + " result=" + resultCode);
2019-09-11 17:32:17 +00:00
Log.logExtras(data);
super.onActivityResult(requestCode, resultCode, data);
}
2019-09-11 12:03:59 +00:00
@NonNull
@Override
public LifecycleOwner getViewLifecycleOwner() {
return owner;
2019-07-05 18:54:07 +00:00
}
2019-08-04 07:37:46 +00:00
@Override
public void show(@NonNull FragmentManager manager, @Nullable String tag) {
try {
super.show(manager, tag);
2019-08-04 18:00:20 +00:00
} catch (Throwable ex) {
// IllegalStateException Can not perform this action after onSaveInstanceState
2019-08-04 07:37:46 +00:00
// Should not happen, but still happened in AdapterMessage.onOpenLink
2019-08-04 18:00:20 +00:00
Log.e(ex);
2019-08-04 07:37:46 +00:00
}
}
@Override
public void onDismiss(@NonNull DialogInterface dialog) {
sendResult(RESULT_CANCELED);
2019-07-01 19:22:57 +00:00
super.onDismiss(dialog);
}
2019-08-21 12:01:00 +00:00
@Override
2020-09-17 14:22:42 +00:00
@SuppressWarnings("deprecation")
2019-08-21 12:01:00 +00:00
public void setTargetFragment(@Nullable Fragment fragment, int requestCode) {
2020-09-17 20:40:38 +00:00
targetRequestKey = ((FragmentBase) fragment).getRequestKey();
2020-05-18 16:34:49 +00:00
targetRequestCode = requestCode;
Log.i("Set target " + this + " " + fragment + " request=" + requestCode);
2019-08-21 12:01:00 +00:00
}
2020-09-17 14:22:42 +00:00
protected void sendResult(int resultCode) {
2019-07-01 14:46:40 +00:00
if (!once) {
once = true;
2020-09-17 14:22:42 +00:00
Log.i("Dialog key=" + targetRequestKey + " result=" + resultCode);
if (targetRequestKey != null) {
Bundle args = getArguments();
2020-09-17 15:24:34 +00:00
if (args == null) // onDismiss
args = new Bundle();
2020-09-17 14:22:42 +00:00
args.putInt("requestCode", targetRequestCode);
args.putInt("resultCode", resultCode);
getParentFragmentManager().setFragmentResult(targetRequestKey, args);
2019-07-01 14:46:40 +00:00
}
}
}
@Override
public void startActivity(Intent intent) {
2019-10-05 14:09:52 +00:00
try {
super.startActivity(intent);
} catch (ActivityNotFoundException ex) {
2020-06-14 16:34:13 +00:00
Log.w(ex);
2020-06-21 13:01:44 +00:00
ToastEx.makeText(getContext(), getString(R.string.title_no_viewer, intent), Toast.LENGTH_LONG).show();
2019-10-05 14:09:52 +00:00
}
}
@Override
public void startActivityForResult(Intent intent, int requestCode) {
2019-10-05 14:09:52 +00:00
try {
super.startActivityForResult(intent, requestCode);
} catch (ActivityNotFoundException ex) {
2020-06-14 16:34:13 +00:00
Log.w(ex);
2020-06-21 13:01:44 +00:00
ToastEx.makeText(getContext(), getString(R.string.title_no_viewer, intent), Toast.LENGTH_LONG).show();
2019-10-05 14:09:52 +00:00
}
}
}