Files
FairEmail/app/src/main/java/eu/faircode/email/ActivityBase.java

201 lines
6.8 KiB
Java
Raw Normal View History

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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +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-02 13:33:06 +00:00
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2018-12-08 08:59:28 +01:00
import android.Manifest;
import android.content.Context;
2018-11-16 20:11:01 +01:00
import android.content.Intent;
2018-08-02 13:33:06 +00:00
import android.content.SharedPreferences;
2018-08-11 16:13:22 +00:00
import android.content.res.Configuration;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
2018-11-16 20:11:01 +01:00
import androidx.annotation.Nullable;
2018-08-08 06:55:47 +00:00
import androidx.appcompat.app.AppCompatActivity;
2019-04-17 19:26:39 +02:00
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
2019-03-15 13:54:25 +00:00
import androidx.preference.PreferenceManager;
2018-08-08 06:55:47 +00:00
2019-05-11 22:10:49 +02:00
import com.bugsnag.android.BreadcrumbType;
import com.bugsnag.android.Bugsnag;
import java.util.ArrayList;
import java.util.Arrays;
2019-05-11 22:10:49 +02:00
import java.util.HashMap;
import java.util.List;
2019-05-11 22:10:49 +02:00
import java.util.Map;
2018-08-04 15:37:42 +00:00
abstract class ActivityBase extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
2019-06-18 21:41:15 +02:00
private Context originalContext;
2018-12-08 08:59:28 +01:00
private boolean contacts;
2019-04-17 19:26:39 +02:00
private List<IBackPressedListener> backPressedListeners = new ArrayList<>();
2018-12-08 08:59:28 +01:00
@Override
protected void attachBaseContext(Context base) {
2019-06-18 21:41:15 +02:00
originalContext = base;
super.attachBaseContext(ApplicationEx.getLocalizedContext(base));
}
2019-06-18 21:41:15 +02:00
Context getOriginalContext() {
return originalContext;
}
2018-08-02 13:33:06 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
2018-12-24 12:27:45 +00:00
Log.i("Create " + this.getClass().getName() + " version=" + BuildConfig.VERSION_NAME);
2019-07-10 11:10:32 +02:00
Intent intent = getIntent();
if (intent != null) {
Log.i(intent.toString());
Log.logBundle(intent.getExtras());
}
2018-11-03 10:53:37 +00:00
2019-02-07 09:02:40 +00:00
this.contacts = hasPermission(Manifest.permission.READ_CONTACTS);
2018-12-08 08:59:28 +01:00
2018-08-02 13:33:06 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
2019-01-22 15:12:20 +00:00
String theme = prefs.getString("theme", null);
if ("system".equals(theme)) {
int uiMode = getResources().getConfiguration().uiMode;
Log.i("UI mode=" + uiMode);
if ((uiMode & Configuration.UI_MODE_NIGHT_YES) != 0)
2019-06-14 12:36:18 +02:00
setTheme(R.style.AppThemeBlack);
}
2019-01-22 15:12:20 +00:00
if ("dark".equals(theme))
setTheme(R.style.AppThemeDark);
else if ("black".equals(theme))
setTheme(R.style.AppThemeBlack);
2018-11-03 10:53:37 +00:00
2018-09-17 06:15:54 +00:00
prefs.registerOnSharedPreferenceChangeListener(this);
2018-11-03 10:53:37 +00:00
2018-08-02 13:33:06 +00:00
super.onCreate(savedInstanceState);
}
2018-08-04 15:37:42 +00:00
2019-04-11 09:47:49 +02:00
@Override
protected void onSaveInstanceState(Bundle outState) {
int before = Helper.getSize(outState);
super.onSaveInstanceState(outState);
int after = Helper.getSize(outState);
Log.i("Saved instance " + this + " size=" + before + "/" + after);
2019-05-11 22:10:49 +02: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 22:06:13 +02:00
Bugsnag.leaveBreadcrumb("onSaveInstanceState", BreadcrumbType.LOG, crumb);
2019-05-11 22:10:49 +02:00
2019-04-11 09:47:49 +02:00
for (String key : outState.keySet())
2019-04-11 19:30:39 +02:00
Log.i("Saved " + this + " " + key + "=" + outState.get(key));
2019-04-11 09:47:49 +02:00
}
2018-08-11 16:13:22 +00:00
@Override
protected void onResume() {
2018-12-24 12:27:45 +00:00
Log.i("Resume " + this.getClass().getName());
2018-12-08 08:59:28 +01:00
2019-02-07 09:02:40 +00:00
boolean contacts = hasPermission(Manifest.permission.READ_CONTACTS);
2018-12-08 08:59:28 +01:00
if (!this.getClass().equals(ActivitySetup.class) && this.contacts != contacts) {
2018-12-24 12:27:45 +00:00
Log.i("Contacts permission=" + contacts);
2018-12-08 08:59:28 +01:00
finish();
startActivity(getIntent());
2019-07-10 17:58:26 +02:00
} else if (!this.getClass().equals(ActivityMain.class) && Helper.shouldAuthenticate(this)) {
finish();
startActivity(new Intent(this, ActivityMain.class));
2018-12-08 08:59:28 +01:00
}
2018-08-11 16:13:22 +00:00
super.onResume();
}
@Override
protected void onPause() {
2018-12-24 12:27:45 +00:00
Log.i("Pause " + this.getClass().getName());
2018-08-11 16:13:22 +00:00
super.onPause();
2019-07-10 17:58:26 +02:00
if (!this.getClass().equals(ActivityMain.class) && Helper.shouldAuthenticate(this))
finish();
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.getClass().getName());
2018-08-11 16:13:22 +00:00
super.onConfigurationChanged(newConfig);
}
2018-08-04 15:37:42 +00:00
@Override
protected void onDestroy() {
2018-12-24 12:27:45 +00:00
Log.i("Destroy " + this.getClass().getName());
2018-08-04 15:37:42 +00:00
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
2018-11-16 20:11:01 +01:00
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
2018-12-24 12:27:45 +00:00
Log.i("Result class=" + this.getClass().getSimpleName() +
" request=" + requestCode + " result=" + resultCode);
Log.logExtras(data);
2018-11-16 20:11:01 +01:00
super.onActivityResult(requestCode, resultCode, data);
}
2018-08-04 15:37:42 +00:00
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
2018-12-24 12:27:45 +00:00
Log.i("Preference " + key + "=" + prefs.getAll().get(key));
2018-10-24 07:36:43 +00:00
if ("theme".equals(key)) {
finish();
2018-11-03 10:53:37 +00:00
if (this.getClass().equals(ActivitySetup.class))
startActivity(getIntent());
2019-01-21 18:12:22 +00:00
} else if (!this.getClass().equals(ActivitySetup.class) &&
Arrays.asList(FragmentOptions.OPTIONS_RESTART).contains(key))
2018-08-21 18:00:06 +00:00
finish();
2018-08-04 15:37:42 +00:00
}
2018-08-19 05:25:49 +00:00
2019-02-07 09:02:40 +00:00
public boolean hasPermission(String name) {
return Helper.hasPermission(this, name);
}
2019-04-17 19:26:39 +02:00
void addBackPressedListener(final IBackPressedListener listener, LifecycleOwner owner) {
2019-04-29 20:24:33 +02:00
Log.i("Adding back listener=" + listener);
backPressedListeners.add(listener);
2019-04-17 19:26:39 +02:00
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
Log.i("Removing back listener=" + listener);
backPressedListeners.remove(listener);
}
});
2018-08-19 05:25:49 +00:00
}
@Override
public void onBackPressed() {
2019-06-23 16:05:36 +02:00
if (backHandled())
return;
super.onBackPressed();
}
protected boolean backHandled() {
2018-08-19 05:25:49 +00:00
for (IBackPressedListener listener : backPressedListeners)
if (listener.onBackPressed())
2019-06-23 16:05:36 +02:00
return true;
return false;
2018-08-19 05:25:49 +00:00
}
public interface IBackPressedListener {
boolean onBackPressed();
}
2018-08-02 13:33:06 +00:00
}