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

127 lines
4.3 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
Copyright 2018 by Marcel Bokhorst (M66B)
*/
2018-11-16 19:11:01 +00: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;
import android.preference.PreferenceManager;
2018-08-04 15:37:42 +00:00
import android.util.Log;
2018-08-02 13:33:06 +00:00
2018-08-19 05:25:49 +00:00
import java.util.ArrayList;
import java.util.List;
2018-11-16 19:11:01 +00:00
import androidx.annotation.Nullable;
2018-08-08 06:55:47 +00:00
import androidx.appcompat.app.AppCompatActivity;
2018-08-04 15:37:42 +00:00
abstract class ActivityBase extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
2018-08-02 13:33:06 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
2018-08-23 11:21:40 +00:00
Log.i(Helper.TAG, "Create " + this.getClass().getName() + " version=" + BuildConfig.VERSION_NAME);
2018-11-03 10:53:37 +00:00
2018-08-02 13:33:06 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
2018-11-03 10:53:37 +00:00
if (Helper.isPro(this)) {
String theme = prefs.getString("theme", null);
if ("dark".equals(theme))
setTheme(R.style.AppThemeDark);
else if ("black".equals(theme))
setTheme(R.style.AppThemeBlack);
}
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
2018-08-11 16:13:22 +00:00
@Override
protected void onResume() {
Log.i(Helper.TAG, "Resume " + this.getClass().getName());
super.onResume();
}
@Override
protected void onPause() {
Log.i(Helper.TAG, "Pause " + this.getClass().getName());
super.onPause();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.i(Helper.TAG, "Config " + this.getClass().getName());
super.onConfigurationChanged(newConfig);
}
2018-08-04 15:37:42 +00:00
@Override
protected void onDestroy() {
Log.i(Helper.TAG, "Destroy " + this.getClass().getName());
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
2018-11-16 19:11:01 +00:00
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
Log.i(Helper.TAG, "Result class=" + this.getClass().getSimpleName() +
" request=" + requestCode + " result=" + resultCode + " data=" + data);
super.onActivityResult(requestCode, resultCode, data);
}
2018-08-04 15:37:42 +00:00
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Log.i(Helper.TAG, "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());
2018-11-03 16:34:35 +00:00
} else if (!this.getClass().equals(ActivitySetup.class) &&
2018-11-04 12:42:41 +00:00
("compact".equals(key) ||
"avatars".equals(key) ||
"identicons".equals(key) ||
"preview".equals(key) ||
"actionbar".equals(key) ||
"confirm".equals(key) ||
2018-11-04 12:42:41 +00:00
"debug".equals(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
private List<IBackPressedListener> backPressedListeners = new ArrayList<>();
public void addBackPressedListener(IBackPressedListener listener) {
backPressedListeners.add(listener);
}
2018-11-13 14:14:14 +00:00
public void removeBackPressedListener(IBackPressedListener listener) {
backPressedListeners.remove(listener);
}
2018-08-19 05:25:49 +00:00
@Override
public void onBackPressed() {
for (IBackPressedListener listener : backPressedListeners)
if (listener.onBackPressed())
return;
super.onBackPressed();
}
public interface IBackPressedListener {
boolean onBackPressed();
}
2018-08-02 13:33:06 +00:00
}