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

183 lines
5.8 KiB
Java
Raw Normal View History

package eu.faircode.email;
2018-08-14 05:53:24 +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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-14 05:53:24 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-14 05:53:24 +00:00
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-08-14 05:53:24 +00:00
*/
2018-12-09 17:49:52 +00:00
import android.content.Context;
2019-06-30 14:55:15 +00:00
import android.content.Intent;
2018-08-11 16:13:22 +00:00
import android.content.res.Configuration;
import android.os.Bundle;
2018-08-12 17:51:57 +00:00
import android.view.LayoutInflater;
2018-08-06 08:39:28 +00:00
import android.view.View;
2018-08-12 17:51:57 +00:00
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
2019-06-30 14:55:15 +00:00
import androidx.annotation.Nullable;
2018-08-08 06:55:47 +00:00
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
2018-08-22 05:18:31 +00:00
import androidx.lifecycle.Lifecycle;
2018-08-08 06:55:47 +00:00
2019-05-11 20:10:49 +00:00
import com.bugsnag.android.BreadcrumbType;
import com.bugsnag.android.Bugsnag;
import java.util.HashMap;
import java.util.Map;
2019-01-15 17:41:55 +00:00
public class FragmentBase extends Fragment {
2018-08-06 12:05:43 +00:00
private String subtitle = " ";
2018-08-22 05:18:31 +00:00
private boolean finish = false;
protected void setSubtitle(int resid) {
setSubtitle(getString(resid));
}
protected void setSubtitle(String subtitle) {
this.subtitle = subtitle;
updateSubtitle();
}
2018-08-22 05:18:31 +00:00
protected void finish() {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
getFragmentManager().popBackStack();
else
finish = true;
}
2018-08-24 06:43:05 +00:00
@Override
public void onSaveInstanceState(Bundle outState) {
2018-12-24 12:27:45 +00:00
Log.i("Save instance " + this);
2019-04-11 07:47:49 +00:00
int before = Helper.getSize(outState);
2019-03-02 07:35:12 +00:00
outState.putString("fair:subtitle", subtitle);
2019-04-11 17:30:39 +00:00
super.onSaveInstanceState(outState);
2019-04-11 07:47:49 +00:00
int after = Helper.getSize(outState);
2019-04-11 17:30:39 +00:00
Log.i("Saved instance " + this + " size=" + before + "/" + after);
2019-05-11 20:10:49 +00:00
Map<String, String> crumb = new HashMap<>();
crumb.put("name", this.getClass().getName());
crumb.put("before", Integer.toString(before));
crumb.put("after", Integer.toString(after));
2019-05-16 20:06:13 +00:00
Bugsnag.leaveBreadcrumb("onSaveInstanceState", BreadcrumbType.LOG, crumb);
2019-05-11 20:10:49 +00:00
2019-04-11 07:47:49 +00:00
for (String key : outState.keySet())
2019-04-11 17:30:39 +00:00
Log.i("Saved " + this + " " + key + "=" + outState.get(key));
2018-08-24 06:43:05 +00:00
}
2018-08-11 16:13:22 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
2018-12-24 12:27:45 +00:00
Log.i("Create " + this + " saved=" + (savedInstanceState != null));
2018-08-11 16:13:22 +00:00
super.onCreate(savedInstanceState);
2018-08-24 06:43:05 +00:00
if (savedInstanceState != null)
2019-03-02 07:35:12 +00:00
subtitle = savedInstanceState.getString("fair:subtitle");
2018-08-11 16:13:22 +00:00
}
2018-08-12 17:51:57 +00:00
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2018-12-24 12:27:45 +00:00
Log.i("Create view " + this);
2018-08-12 17:51:57 +00:00
return super.onCreateView(inflater, container, savedInstanceState);
}
2018-08-11 16:13:22 +00:00
@Override
public void onActivityCreated(Bundle savedInstanceState) {
2018-12-24 12:27:45 +00:00
Log.i("Activity " + this + " saved=" + (savedInstanceState != null));
2018-08-11 16:13:22 +00:00
super.onActivityCreated(savedInstanceState);
}
@Override
public void onResume() {
2018-12-24 12:27:45 +00:00
Log.i("Resume " + this);
super.onResume();
updateSubtitle();
2018-08-22 05:18:31 +00:00
if (finish) {
getFragmentManager().popBackStack();
finish = false;
}
}
2018-08-11 16:13:22 +00:00
@Override
public void onPause() {
2018-12-24 12:27:45 +00:00
Log.i("Pause " + this);
2018-08-11 16:13:22 +00:00
super.onPause();
}
2019-06-30 14:55:15 +00:00
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
Log.i("Result class=" + this.getClass().getSimpleName() +
" request=" + requestCode + " result=" + resultCode);
Log.logExtras(data);
2019-06-30 14:55:15 +00:00
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDetach() {
super.onDetach();
2018-12-09 17:49:52 +00:00
InputMethodManager im = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
2018-08-06 12:05:43 +00:00
View focused = getActivity().getCurrentFocus();
if (focused != null)
im.hideSoftInputFromWindow(focused.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
2018-08-11 16:13:22 +00:00
@Override
public void onConfigurationChanged(Configuration newConfig) {
2018-12-24 12:27:45 +00:00
Log.i("Config " + this);
2018-08-11 16:13:22 +00:00
super.onConfigurationChanged(newConfig);
}
@Override
public void onDestroy() {
2018-12-24 12:27:45 +00:00
Log.i("Destroy " + this);
2018-08-11 16:13:22 +00:00
super.onDestroy();
}
2018-12-29 07:24:43 +00:00
@Override
public void setHasOptionsMenu(boolean hasMenu) {
super.setHasOptionsMenu(!isPane() && hasMenu);
}
private void updateSubtitle() {
AppCompatActivity activity = (AppCompatActivity) getActivity();
2018-12-29 07:24:43 +00:00
if (activity != null && !isPane()) {
ActionBar actionbar = activity.getSupportActionBar();
if (actionbar != null)
actionbar.setSubtitle(subtitle);
}
}
2018-12-29 07:24:43 +00:00
private boolean isPane() {
Bundle args = getArguments();
return (args != null && args.getBoolean("pane"));
}
2019-02-07 09:02:40 +00:00
boolean hasPermission(String name) {
ActivityBase activity = (ActivityBase) getActivity();
if (activity == null)
return false;
return activity.hasPermission(name);
}
2019-04-17 17:26:39 +00:00
void addBackPressedListener(ActivityBase.IBackPressedListener listener) {
((ActivityBase) getActivity()).addBackPressedListener(listener, getViewLifecycleOwner());
}
2019-04-29 18:26:29 +00:00
void addBillingListener(ActivityBilling.IBillingListener listener) {
((ActivityBilling) getActivity()).addBillingListener(listener, getViewLifecycleOwner());
}
}