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

313 lines
11 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.ActivityNotFoundException;
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;
2019-12-08 10:48:49 +01:00
import android.os.Build;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
2019-07-11 08:10:33 +02:00
import android.os.PowerManager;
2019-10-04 16:57:40 +02:00
import android.view.MenuItem;
2019-12-08 10:48:49 +01:00
import android.view.View;
import android.view.Window;
2019-10-05 16:09:52 +02:00
import android.view.WindowManager;
import android.widget.Toast;
2018-08-02 13:33:06 +00:00
2019-10-04 16:57:40 +02:00
import androidx.annotation.NonNull;
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
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-11-15 14:27:28 +01:00
boolean secure = prefs.getBoolean("secure", false);
if (secure)
2019-10-05 16:09:52 +02:00
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
2019-07-11 08:04:11 +02:00
if (!this.getClass().equals(ActivityMain.class)) {
2019-08-16 12:14:20 +02:00
String theme = prefs.getString("theme", "light");
if ("dark".equals(theme))
setTheme(R.style.AppThemeDark);
else if ("black".equals(theme))
setTheme(R.style.AppThemeBlack);
2019-09-09 09:31:55 +02:00
else if ("grey_light".equals(theme))
setTheme(R.style.AppThemeGreyLight);
else if ("grey_dark".equals(theme))
setTheme(R.style.AppThemeGreyDark);
2019-08-16 12:14:20 +02:00
else if ("system".equals(theme)) {
2019-07-11 08:04:11 +02:00
int uiMode = getResources().getConfiguration().uiMode;
Log.i("UI mode=" + uiMode);
if ((uiMode & Configuration.UI_MODE_NIGHT_YES) != 0)
setTheme(R.style.AppThemeBlack);
2019-09-09 11:40:14 +02:00
} else if ("grey_system".equals(theme)) {
int uiMode = getResources().getConfiguration().uiMode;
Log.i("UI mode=" + uiMode);
if ((uiMode & Configuration.UI_MODE_NIGHT_YES) != 0)
setTheme(R.style.AppThemeGreyDark);
else
setTheme(R.style.AppThemeGreyLight);
2019-07-11 08:04:11 +02:00
}
2019-12-08 10:48:49 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
boolean dark = Helper.isDarkTheme(this);
Window window = getWindow();
View view = window.getDecorView();
int flags = view.getSystemUiVisibility();
if (dark)
flags &= ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
view.setSystemUiVisibility(flags);
}
}
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
checkAuthentication();
2019-10-14 09:11:17 +02: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);
2019-12-07 17:02:42 +01:00
Log.d("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-08-12 13:07:14 +02:00
Log.breadcrumb("onSaveInstanceState", crumb);
2019-05-11 22:10:49 +02:00
2019-04-11 09:47:49 +02:00
for (String key : outState.keySet())
2019-12-07 17:02:42 +01:00
Log.d("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() {
2019-12-07 17:02:42 +01:00
Log.d("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());
} else
checkAuthentication();
2018-12-08 08:59:28 +01:00
2018-08-11 16:13:22 +00:00
super.onResume();
}
@Override
protected void onPause() {
2019-12-07 17:02:42 +01:00
Log.d("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))
finishAndRemoveTask();
2018-08-11 16:13:22 +00:00
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
2019-12-07 17:02:42 +01:00
Log.d("Config " + this.getClass().getName());
2018-08-11 16:13:22 +00:00
super.onConfigurationChanged(newConfig);
}
@Override
public void onUserInteraction() {
2019-12-07 17:02:42 +01:00
Log.d("User interaction");
if (!this.getClass().equals(ActivityMain.class) && Helper.shouldAuthenticate(this)) {
finishAndRemoveTask();
2019-11-01 18:26:11 +01:00
Intent main = new Intent(this, ActivityMain.class);
main.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(main);
}
}
@Override
protected void onUserLeaveHint() {
2019-12-07 17:02:42 +01:00
Log.d("User leaving");
}
2019-07-11 08:10:33 +02:00
@Override
protected void onStop() {
super.onStop();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
2019-07-11 08:10:33 +02:00
if (pm != null && !pm.isInteractive()) {
Log.i("Stop with screen off");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean biometrics = prefs.getBoolean("biometrics", false);
if (biometrics) {
Helper.clearAuthentication(this);
finish();
}
2019-07-11 08:10:33 +02:00
}
}
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) {
2019-10-08 10:48:31 +02:00
String action = (data == null ? null : data.getAction());
2018-12-24 12:27:45 +00:00
Log.i("Result class=" + this.getClass().getSimpleName() +
2019-10-08 10:48:31 +02:00
" action=" + action + " request=" + requestCode + " result=" + resultCode);
Log.logExtras(data);
2018-11-16 20:11:01 +01:00
super.onActivityResult(requestCode, resultCode, data);
}
private void checkAuthentication() {
if (!this.getClass().equals(ActivityMain.class) && Helper.shouldAuthenticate(this)) {
Intent intent = getIntent();
finishAndRemoveTask();
2019-11-01 18:26:11 +01:00
Intent main = new Intent(this, ActivityMain.class)
.putExtra("intent", intent);
main.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(main);
}
}
@Override
public void startActivity(Intent intent) {
try {
super.startActivity(intent);
} catch (ActivityNotFoundException ex) {
Log.e(ex);
ToastEx.makeText(this, getString(R.string.title_no_viewer, intent.getAction()), Toast.LENGTH_LONG).show();
}
}
@Override
public void startActivityForResult(Intent intent, int requestCode) {
try {
super.startActivityForResult(intent, requestCode);
} catch (ActivityNotFoundException ex) {
Log.e(ex);
ToastEx.makeText(this, getString(R.string.title_no_viewer, intent.getAction()), Toast.LENGTH_LONG).show();
}
}
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-12-07 17:02:42 +01:00
Log.d("Adding back listener=" + listener);
2019-04-29 20:24:33 +02:00
backPressedListeners.add(listener);
2019-04-17 19:26:39 +02:00
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
2019-12-07 17:02:42 +01:00
Log.d("Removing back listener=" + listener);
2019-04-17 19:26:39 +02:00
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();
}
2019-10-04 16:57:40 +02:00
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
2019-06-23 16:05:36 +02:00
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
}