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

550 lines
24 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
2020-01-05 18:32:53 +01:00
Copyright 2018-2020 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;
2020-03-03 16:44:57 +01:00
import android.content.ComponentName;
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;
2020-01-30 20:52:18 +01:00
import android.view.KeyEvent;
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;
2020-03-21 09:16:49 +01:00
private boolean visible;
2018-12-08 08:59:28 +01:00
private boolean contacts;
2020-01-30 20:52:18 +01:00
private List<IKeyPressedListener> keyPressedListeners = 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");
2020-04-03 10:59:43 +02:00
// https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#configuration_changes
2019-12-26 16:15:46 +01:00
int uiMode = getResources().getConfiguration().uiMode;
2020-04-03 10:59:43 +02:00
boolean night = ((uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES);
2019-12-28 16:58:46 +01:00
Log.i("theme=" + theme + " UI mode=" + uiMode + " night=" + night);
2019-12-26 16:15:46 +01:00
switch (theme) {
2019-12-30 14:29:16 +01:00
// Light
2019-12-26 16:15:46 +01:00
case "light":
case "blue_orange_light":
setTheme(R.style.AppThemeBlueOrangeLight);
break;
2019-12-27 18:51:00 +01:00
case "orange_blue_light":
setTheme(R.style.AppThemeOrangeBlueLight);
break;
2019-12-26 16:15:46 +01:00
case "yellow_purple_light":
setTheme(R.style.AppThemeYellowPurpleLight);
break;
2019-12-27 18:51:00 +01:00
case "purple_yellow_light":
setTheme(R.style.AppThemePurpleYellowLight);
break;
2019-12-26 21:03:38 +01:00
case "red_green_light":
setTheme(R.style.AppThemeRedGreenLight);
2019-12-26 16:15:46 +01:00
break;
2019-12-27 18:51:00 +01:00
case "green_red_light":
setTheme(R.style.AppThemeGreenRedLight);
2019-12-26 16:15:46 +01:00
break;
2019-12-30 14:29:16 +01:00
// Dark
2019-12-26 16:15:46 +01:00
case "dark":
case "blue_orange_dark":
setTheme(R.style.AppThemeBlueOrangeDark);
break;
2019-12-27 18:51:00 +01:00
case "orange_blue_dark":
setTheme(R.style.AppThemeOrangeBlueDark);
break;
2019-12-26 16:15:46 +01:00
case "yellow_purple_dark":
setTheme(R.style.AppThemeYellowPurpleDark);
break;
2019-12-27 18:51:00 +01:00
case "purple_yellow_dark":
setTheme(R.style.AppThemePurpleYellowDark);
break;
2019-12-26 21:03:38 +01:00
case "red_green_dark":
setTheme(R.style.AppThemeRedGreenDark);
2019-12-26 16:15:46 +01:00
break;
2019-12-27 18:51:00 +01:00
case "green_red_dark":
setTheme(R.style.AppThemeGreenRedDark);
break;
2019-12-30 14:29:16 +01:00
// Black
case "blue_orange_black":
setTheme(R.style.AppThemeBlueOrangeBlack);
break;
case "orange_blue_black":
setTheme(R.style.AppThemeOrangeBlueBlack);
break;
case "yellow_purple_black":
setTheme(R.style.AppThemeYellowPurpleBlack);
break;
case "purple_yellow_black":
setTheme(R.style.AppThemePurpleYellowBlack);
break;
case "red_green_black":
setTheme(R.style.AppThemeRedGreenBlack);
break;
case "green_red_black":
setTheme(R.style.AppThemeGreenRedBlack);
break;
// Grey
2019-12-27 18:51:00 +01:00
case "grey_light":
setTheme(R.style.AppThemeGreySteelBlueLight);
break;
2019-12-26 16:15:46 +01:00
case "grey_dark":
setTheme(R.style.AppThemeGreySteelBlueDark);
break;
2019-12-28 14:45:10 +01:00
2019-12-30 15:21:08 +01:00
// Black
case "black":
setTheme(R.style.AppThemeBlack);
break;
2019-12-30 14:29:16 +01:00
// System
2019-12-26 16:15:46 +01:00
case "system":
2019-12-28 14:45:10 +01:00
case "blue_orange_system":
2019-12-28 16:58:46 +01:00
setTheme(night
? R.style.AppThemeBlueOrangeDark : R.style.AppThemeBlueOrangeLight);
2019-12-26 16:15:46 +01:00
break;
2020-04-03 12:04:50 +02:00
case "orange_blue_system":
setTheme(night
? R.style.AppThemeOrangeBlueDark : R.style.AppThemeOrangeBlueLight);
break;
2019-12-28 14:45:10 +01:00
case "yellow_purple_system":
2019-12-28 16:58:46 +01:00
setTheme(night
? R.style.AppThemeYellowPurpleDark : R.style.AppThemeYellowPurpleLight);
2019-12-28 14:45:10 +01:00
break;
2020-04-03 12:04:50 +02:00
case "purple_yellow_system":
setTheme(night
? R.style.AppThemePurpleYellowDark : R.style.AppThemePurpleYellowLight);
break;
2019-12-28 14:45:10 +01:00
case "red_green_system":
2019-12-28 16:58:46 +01:00
setTheme(night
? R.style.AppThemeRedGreenDark : R.style.AppThemeRedGreenLight);
2019-12-28 14:45:10 +01:00
break;
2020-04-03 12:04:50 +02:00
case "green_red_system":
setTheme(night
? R.style.AppThemeGreenRedDark : R.style.AppThemeGreenRedLight);
break;
2019-12-26 16:15:46 +01:00
case "grey_system":
2019-12-28 16:58:46 +01:00
setTheme(night
? R.style.AppThemeGreySteelBlueDark : R.style.AppThemeGreySteelBlueLight);
2019-12-26 16:15:46 +01:00
break;
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
boolean navbar_colorize = prefs.getBoolean("navbar_colorize", false);
if (navbar_colorize) {
Window window = getWindow();
if (window != null)
window.setNavigationBarColor(Helper.resolveColor(this, R.attr.colorPrimaryDark));
}
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
2020-03-21 09:16:49 +01:00
visible = true;
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
2020-04-21 08:07:36 +02:00
try {
super.onResume();
} catch (Throwable ex) {
Log.w(ex);
/*
java.lang.RuntimeException: Unable to resume activity {eu.faircode.email/eu.faircode.email.ActivityView}: java.lang.IllegalArgumentException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4025)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4057)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1959)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7100)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
Caused by: java.lang.IllegalArgumentException
at android.os.Parcel.createException(Parcel.java:1970)
at android.os.Parcel.readException(Parcel.java:1934)
at android.os.Parcel.readException(Parcel.java:1884)
at android.app.IActivityManager$Stub$Proxy.isTopOfTask(IActivityManager.java:7835)
at android.app.Activity.isTopOfTask(Activity.java:6544)
at android.app.Activity.onResume(Activity.java:1404)
at androidx.fragment.app.FragmentActivity.onResume(SourceFile:458)
at eu.faircode.email.ActivityBase.onResume(SourceFile:263)
at eu.faircode.email.ActivityBilling.onResume(SourceFile:125)
at eu.faircode.email.ActivityView.onResume(SourceFile:588)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1416)
at android.app.Activity.performResume(Activity.java:7612)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4017)
... 11 more
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActivityManagerService.isTopOfTask(ActivityManagerService.java:18313)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2028)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:4166)
at android.os.Binder.execTransact(Binder.java:739)
*/
}
2018-08-11 16:13:22 +00:00
}
@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
2020-03-21 09:16:49 +01:00
visible = false;
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);
2020-03-27 14:35:13 +01:00
if (data != null)
Log.i("data=" + data.getData());
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) {
2020-06-14 18:34:13 +02:00
Log.w(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) {
2020-06-14 18:34:13 +02:00
Log.w(ex);
ToastEx.makeText(this, getString(R.string.title_no_viewer, intent.getAction()), Toast.LENGTH_LONG).show();
}
}
2020-03-03 16:44:57 +01:00
@Override
public ComponentName startService(Intent service) {
try {
return super.startService(service);
} catch (Throwable ex) {
Log.w(ex);
/*
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=clear:0 cmp=eu.faircode.email/.ServiceUI }: app is in background uid UidRecord{cb19b35 u0a286 TRNB idle change:uncached procs:1 proclist:1344, seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1666)
at android.app.ContextImpl.startService(ContextImpl.java:1611)
at android.content.ContextWrapper.startService(ContextWrapper.java:677)
at android.content.ContextWrapper.startService(ContextWrapper.java:677)
at eu.faircode.email.ActivityView.checkIntent(SourceFile:873)
at eu.faircode.email.ActivityView.onResume(SourceFile:595)
*/
return null;
}
}
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());
2020-03-21 09:16:49 +01:00
} else if (!this.getClass().equals(ActivitySetup.class) && !visible &&
2019-01-21 18:12:22 +00:00
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);
}
2020-01-30 20:52:18 +01:00
void addKeyPressedListener(final IKeyPressedListener listener, LifecycleOwner owner) {
2019-12-07 17:02:42 +01:00
Log.d("Adding back listener=" + listener);
2020-01-30 20:52:18 +01:00
keyPressedListeners.add(listener);
2019-04-29 20:24:33 +02:00
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);
2020-01-30 20:52:18 +01:00
keyPressedListeners.remove(listener);
2019-04-17 19:26:39 +02:00
}
});
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();
}
2020-01-30 20:52:18 +01:00
public boolean dispatchKeyEvent(KeyEvent event) {
for (IKeyPressedListener listener : keyPressedListeners)
2020-02-20 16:46:51 +01:00
if (listener.onKeyPressed(event))
2020-01-30 20:52:18 +01:00
return true;
2020-03-03 16:07:15 +01:00
try {
return super.dispatchKeyEvent(event);
} catch (Throwable ex) {
Log.w(ex);
/*
java.lang.IllegalArgumentException
at com.android.internal.util.Preconditions.checkArgument(Preconditions.java:33)
at android.widget.SelectionActionModeHelper$TextClassificationHelper.init(SelectionActionModeHelper.java:974)
at android.widget.SelectionActionModeHelper.resetTextClassificationHelper(SelectionActionModeHelper.java:462)
at android.widget.SelectionActionModeHelper.resetTextClassificationHelper(SelectionActionModeHelper.java:470)
at android.widget.SelectionActionModeHelper.startSelectionActionModeAsync(SelectionActionModeHelper.java:118)
at android.widget.Editor.startSelectionActionModeAsync(Editor.java:2131)
at android.widget.Editor.refreshTextActionMode(Editor.java:2076)
at android.widget.TextView.spanChange(TextView.java:9903)
at android.widget.TextView$ChangeWatcher.onSpanChanged(TextView.java:12534)
at android.text.SpannableStringBuilder.sendSpanChanged(SpannableStringBuilder.java:1303)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:750)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:674)
at android.text.Selection.extendSelection(Selection.java:174)
at android.text.Selection.setSelectionAndMemory(Selection.java:256)
at android.text.Selection.extendUp(Selection.java:357)
at android.text.method.ArrowKeyMovementMethod.up(ArrowKeyMovementMethod.java:92)
at android.text.method.BaseMovementMethod.handleMovementKey(BaseMovementMethod.java:189)
at android.text.method.ArrowKeyMovementMethod.handleMovementKey(ArrowKeyMovementMethod.java:65)
at android.text.method.BaseMovementMethod.onKeyDown(BaseMovementMethod.java:42)
at android.widget.TextView.doKeyDown(TextView.java:7691)
at android.widget.TextView.onKeyDown(TextView.java:7442)
at android.view.KeyEvent.dispatch(KeyEvent.java:2692)
at android.view.View.dispatchKeyEvent(View.java:12471)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:389)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:451)
at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1830)
at android.app.Activity.dispatchKeyEvent(Activity.java:3385)
at androidx.core.app.ComponentActivity.superDispatchKeyEvent(SourceFile:122)
at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(SourceFile:84)
at androidx.core.app.ComponentActivity.dispatchKeyEvent(SourceFile:140)
at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(SourceFile:559)
*/
return false;
}
2020-01-30 20:52:18 +01:00
}
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() {
2020-01-30 20:52:18 +01:00
for (IKeyPressedListener listener : keyPressedListeners)
2018-08-19 05:25:49 +00:00
if (listener.onBackPressed())
2019-06-23 16:05:36 +02:00
return true;
return false;
2018-08-19 05:25:49 +00:00
}
2020-06-13 21:29:39 +02:00
@Override
public boolean shouldUpRecreateTask(Intent targetIntent) {
2020-06-14 18:34:13 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
2020-06-13 21:29:39 +02:00
ComponentName cn = targetIntent.getComponent();
if (cn != null && BuildConfig.APPLICATION_ID.equals(cn.getPackageName()))
return false;
}
return super.shouldUpRecreateTask(targetIntent);
}
2020-01-30 20:52:18 +01:00
public interface IKeyPressedListener {
2020-02-20 16:46:51 +01:00
boolean onKeyPressed(KeyEvent event);
2020-01-30 20:52:18 +01:00
2018-08-19 05:25:49 +00:00
boolean onBackPressed();
}
2018-08-02 13:33:06 +00:00
}