2019-07-01 14:27:22 +00:00
|
|
|
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/>.
|
|
|
|
|
2022-01-01 08:46:36 +00:00
|
|
|
Copyright 2018-2022 by Marcel Bokhorst (M66B)
|
2019-10-20 10:15:13 +00:00
|
|
|
*/
|
|
|
|
|
2019-10-05 14:09:52 +00:00
|
|
|
import android.content.ActivityNotFoundException;
|
2019-07-01 14:27:22 +00:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
2019-08-21 12:01:00 +00:00
|
|
|
import android.os.Bundle;
|
2019-07-01 14:27:22 +00:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2019-08-04 07:37:46 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2019-07-01 14:27:22 +00:00
|
|
|
import androidx.fragment.app.DialogFragment;
|
|
|
|
import androidx.fragment.app.Fragment;
|
2019-08-04 07:37:46 +00:00
|
|
|
import androidx.fragment.app.FragmentManager;
|
2020-09-18 17:49:38 +00:00
|
|
|
import androidx.fragment.app.FragmentResultListener;
|
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;
|
2019-07-01 14:27:22 +00:00
|
|
|
|
|
|
|
import static android.app.Activity.RESULT_CANCELED;
|
2021-12-27 07:40:22 +00:00
|
|
|
import static android.app.Activity.RESULT_OK;
|
2019-07-01 14:27:22 +00:00
|
|
|
|
2019-09-11 19:43:27 +00:00
|
|
|
public class FragmentDialogBase extends DialogFragment {
|
2021-12-27 07:40:22 +00:00
|
|
|
private boolean hasResult = 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
|
|
|
|
2020-09-18 17:49:38 +00:00
|
|
|
public String getRequestKey() {
|
2021-12-26 19:39:58 +00:00
|
|
|
return Helper.getRequestKey(this);
|
2020-09-18 17:49:38 +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");
|
|
|
|
}
|
|
|
|
|
2021-12-26 08:13:33 +00:00
|
|
|
String requestKey = getRequestKey();
|
|
|
|
if (!BuildConfig.PLAY_STORE_RELEASE)
|
|
|
|
EntityLog.log(getContext(), "Listing key=" + requestKey);
|
|
|
|
getParentFragmentManager().setFragmentResultListener(requestKey, this, new FragmentResultListener() {
|
2020-09-18 17:49:38 +00:00
|
|
|
@Override
|
|
|
|
public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
|
2020-10-02 10:16:25 +00:00
|
|
|
try {
|
2020-10-02 12:50:06 +00:00
|
|
|
result.setClassLoader(ApplicationEx.class.getClassLoader());
|
2020-10-02 10:16:25 +00:00
|
|
|
int requestCode = result.getInt("requestCode");
|
|
|
|
int resultCode = result.getInt("resultCode");
|
|
|
|
|
|
|
|
Intent data = new Intent();
|
|
|
|
data.putExtra("args", result);
|
|
|
|
onActivityResult(requestCode, resultCode, data);
|
|
|
|
} catch (Throwable ex) {
|
2021-12-26 08:13:33 +00:00
|
|
|
Log.e(ex);
|
2020-10-02 10:16:25 +00:00
|
|
|
}
|
2020-09-18 17:49:38 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 14:27:22 +00:00
|
|
|
@Override
|
|
|
|
public void onDismiss(@NonNull DialogInterface dialog) {
|
|
|
|
sendResult(RESULT_CANCELED);
|
2019-07-01 19:22:57 +00:00
|
|
|
super.onDismiss(dialog);
|
2019-07-01 14:27:22 +00:00
|
|
|
}
|
|
|
|
|
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-18 17:49:38 +00:00
|
|
|
if (fragment instanceof FragmentBase)
|
|
|
|
targetRequestKey = ((FragmentBase) fragment).getRequestKey();
|
|
|
|
else if (fragment instanceof FragmentDialogBase)
|
|
|
|
targetRequestKey = ((FragmentDialogBase) fragment).getRequestKey();
|
|
|
|
else {
|
|
|
|
Log.e("setTargetFragment=" + fragment.getClass().getName());
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
2021-12-26 08:13:33 +00:00
|
|
|
|
2021-12-27 07:40:22 +00:00
|
|
|
hasResult = false;
|
2020-05-18 16:34:49 +00:00
|
|
|
targetRequestCode = requestCode;
|
2019-08-21 12:01:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 14:22:42 +00:00
|
|
|
protected void sendResult(int resultCode) {
|
2021-12-26 08:13:33 +00:00
|
|
|
EntityLog.log(getContext(), "Sending key=" + targetRequestKey +
|
|
|
|
" request=" + targetRequestCode +
|
|
|
|
" result=" + resultCode +
|
2021-12-27 07:40:22 +00:00
|
|
|
" has=" + hasResult);
|
2021-12-26 08:13:33 +00:00
|
|
|
|
2021-12-27 07:40:22 +00:00
|
|
|
if (!hasResult || resultCode == RESULT_OK) {
|
|
|
|
hasResult = true;
|
2021-12-26 08:13:33 +00:00
|
|
|
|
2020-09-17 14:22:42 +00:00
|
|
|
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
|
|
|
}
|
2019-07-01 14:27:22 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-11 05:41:20 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void startActivity(Intent intent) {
|
2019-10-05 14:09:52 +00:00
|
|
|
try {
|
|
|
|
super.startActivity(intent);
|
2022-01-16 07:43:56 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Helper.reportNoViewer(getContext(), intent, ex);
|
2019-10-05 14:09:52 +00:00
|
|
|
}
|
2019-07-11 05:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void startActivityForResult(Intent intent, int requestCode) {
|
2019-10-05 14:09:52 +00:00
|
|
|
try {
|
|
|
|
super.startActivityForResult(intent, requestCode);
|
2022-01-16 07:43:56 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Helper.reportNoViewer(getContext(), intent, ex);
|
2019-10-05 14:09:52 +00:00
|
|
|
}
|
2019-07-11 05:41:20 +00:00
|
|
|
}
|
2019-07-01 14:27:22 +00:00
|
|
|
}
|