Skip to content

Commit 296757e

Browse files
committed
[Refactor] Enable predictive back in Android 14 onwards
Signed-off-by: Muntashir Al-Islam <muntashirakon@riseup.net>
1 parent c5119ec commit 296757e

15 files changed

Lines changed: 264 additions & 101 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,22 @@
170170
tools:ignore="UnusedAttribute">
171171
<activity
172172
android:name=".filters.FinderActivity"
173+
android:enableOnBackInvokedCallback="true"
173174
android:exported="false"
174175
android:label="@string/finder_title" />
175176
<activity
176177
android:name=".oneclickops.OneClickOpsActivity"
178+
android:enableOnBackInvokedCallback="true"
177179
android:exported="false"
178180
android:label="@string/one_click_ops" />
179181
<activity
180182
android:name=".runningapps.RunningAppsActivity"
183+
android:enableOnBackInvokedCallback="true"
181184
android:exported="true"
182185
android:label="@string/running_apps" />
183186
<activity
184187
android:name=".settings.SettingsActivity"
188+
android:enableOnBackInvokedCallback="true"
185189
android:exported="true"
186190
android:label="@string/app_settings"
187191
android:launchMode="singleTop">
@@ -202,14 +206,17 @@
202206
</activity>
203207
<activity
204208
android:name=".sharedpref.SharedPrefsActivity"
209+
android:enableOnBackInvokedCallback="true"
205210
android:exported="false"
206211
android:taskAffinity="" />
207212
<activity
208213
android:name=".usage.AppUsageActivity"
214+
android:enableOnBackInvokedCallback="true"
209215
android:exported="true" />
210216
<activity
211217
android:name=".editor.CodeEditorActivity"
212218
android:configChanges="keyboard|keyboardHidden|locale|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
219+
android:enableOnBackInvokedCallback="true"
213220
android:exported="false"
214221
android:label="@string/title_code_editor"
215222
android:taskAffinity=".editor.CodeEditorActivity"
@@ -366,6 +373,7 @@
366373

367374
<activity
368375
android:name=".scanner.ScannerActivity"
376+
android:enableOnBackInvokedCallback="true"
369377
android:exported="true"
370378
android:label="@string/scanner">
371379
<intent-filter>
@@ -391,6 +399,7 @@
391399
</activity>
392400
<activity
393401
android:name=".viewer.ExplorerActivity"
402+
android:enableOnBackInvokedCallback="false"
394403
android:enabled="true"
395404
android:exported="true"
396405
android:label="@string/explore"
@@ -425,6 +434,7 @@
425434
</activity>
426435
<activity
427436
android:name=".main.MainActivity"
437+
android:enableOnBackInvokedCallback="true"
428438
android:exported="true">
429439
<intent-filter android:label="@string/search">
430440
<action android:name="android.intent.action.VIEW" />
@@ -439,18 +449,21 @@
439449
</activity>
440450
<activity
441451
android:name=".runner.TermActivity"
452+
android:enableOnBackInvokedCallback="true"
442453
android:exported="false"
443454
android:label="@string/title_terminal_emulator"
444455
android:taskAffinity=""
445456
android:windowSoftInputMode="stateUnchanged" />
446457
<activity
447458
android:name=".accessibility.activity.LeadingActivityTrackerActivity"
459+
android:enableOnBackInvokedCallback="false"
448460
android:excludeFromRecents="true"
449461
android:exported="false"
450462
android:label="@string/title_ui_tracker"
451463
android:theme="@style/AppTheme.TransparentBackground" />
452464
<activity
453465
android:name=".misc.LabsActivity"
466+
android:enableOnBackInvokedCallback="true"
454467
android:exported="true"
455468
android:label="@string/title_labs_activity" />
456469

@@ -541,13 +554,16 @@
541554

542555
<activity
543556
android:name=".debloat.DebloaterActivity"
557+
android:enableOnBackInvokedCallback="true"
544558
android:exported="false"
545559
android:label="@string/debloater_title" />
546560
<activity
547561
android:name=".details.AppDetailsActivity"
562+
android:enableOnBackInvokedCallback="true"
548563
android:exported="false" />
549564
<activity
550565
android:name=".details.manifest.ManifestViewerActivity"
566+
android:enableOnBackInvokedCallback="true"
551567
android:exported="true"
552568
android:label="@string/manifest_viewer">
553569
<intent-filter>
@@ -573,6 +589,7 @@
573589
</activity>
574590
<activity
575591
android:name=".apk.installer.PackageInstallerActivity"
592+
android:enableOnBackInvokedCallback="false"
576593
android:excludeFromRecents="true"
577594
android:exported="true"
578595
android:label="@string/install"
@@ -654,42 +671,49 @@
654671
</activity>
655672
<activity
656673
android:name=".crypto.OpenPGPCryptoActivity"
674+
android:enableOnBackInvokedCallback="false"
657675
android:excludeFromRecents="true"
658676
android:exported="false"
659677
android:launchMode="singleInstance"
660678
android:taskAffinity=""
661679
android:theme="@style/AppTheme.TransparentBackground" />
662680
<activity
663681
android:name=".crypto.ks.KeyStoreActivity"
682+
android:enableOnBackInvokedCallback="false"
664683
android:excludeFromRecents="true"
665684
android:exported="false"
666685
android:launchMode="singleInstance"
667686
android:taskAffinity=""
668687
android:theme="@style/AppTheme.TransparentBackground" />
669688
<activity
670689
android:name=".profiles.ProfilesActivity"
690+
android:enableOnBackInvokedCallback="true"
671691
android:exported="true"
672692
android:label="@string/profiles" />
673693
<activity
674694
android:name=".profiles.AppsProfileActivity"
675695
android:exported="false" />
676696
<activity
677697
android:name=".sysconfig.SysConfigActivity"
698+
android:enableOnBackInvokedCallback="true"
678699
android:exported="false"
679700
android:label="@string/sys_config" />
680701
<activity
681702
android:name=".batchops.BatchOpsResultsActivity"
703+
android:enableOnBackInvokedCallback="true"
682704
android:excludeFromRecents="true"
683705
android:exported="false"
684706
android:launchMode="singleInstance"
685707
android:taskAffinity="" />
686708
<activity
687709
android:name=".history.ops.OpHistoryActivity"
710+
android:enableOnBackInvokedCallback="true"
688711
android:exported="true"
689712
android:icon="@drawable/ic_history"
690713
android:label="@string/op_history" />
691714
<activity
692715
android:name=".intercept.ActivityInterceptor"
716+
android:enableOnBackInvokedCallback="true"
693717
android:enabled="true"
694718
android:exported="true"
695719
android:label="@string/interceptor"
@@ -1013,40 +1037,47 @@
10131037
</activity>
10141038
<activity
10151039
android:name=".misc.HelpActivity"
1040+
android:enableOnBackInvokedCallback="true"
10161041
android:exported="true"
10171042
android:launchMode="singleTop"
10181043
android:taskAffinity="" />
10191044
<activity
10201045
android:name=".crypto.auth.AuthManagerActivity"
1046+
android:enableOnBackInvokedCallback="true"
10211047
android:exported="false"
10221048
android:label="@string/auth_manager_title" />
10231049
<activity
10241050
android:name=".crypto.auth.AuthFeatureDemultiplexer"
1051+
android:enableOnBackInvokedCallback="false"
10251052
android:exported="true" />
10261053
<activity
10271054
android:name=".details.ActivityLauncherShortcutActivity"
1055+
android:enableOnBackInvokedCallback="false"
10281056
android:excludeFromRecents="true"
1029-
android:noHistory="true"
10301057
android:exported="false"
10311058
android:launchMode="singleTask"
1059+
android:noHistory="true"
10321060
android:taskAffinity=".details.ActivityLauncherShortcutActivity"
10331061
android:theme="@style/AppTheme.TransparentBackground" />
10341062
<activity
10351063
android:name=".apk.behavior.FreezeUnfreezeActivity"
1064+
android:enableOnBackInvokedCallback="false"
10361065
android:excludeFromRecents="true"
10371066
android:exported="false"
10381067
android:launchMode="singleInstance"
10391068
android:taskAffinity=".apk.behavior.FreezeUnfreezeActivity"
10401069
android:theme="@style/AppTheme.TransparentBackground" />
10411070
<activity
10421071
android:name=".profiles.ProfileApplierActivity"
1072+
android:enableOnBackInvokedCallback="false"
10431073
android:excludeFromRecents="true"
10441074
android:exported="false"
10451075
android:launchMode="singleInstance"
10461076
android:taskAffinity=".profiles.ProfileApplierActivity"
10471077
android:theme="@style/AppTheme.TransparentBackground" />
10481078
<activity
10491079
android:name=".main.SplashActivity"
1080+
android:enableOnBackInvokedCallback="true"
10501081
android:exported="true"
10511082
android:icon="@mipmap/ic_launcher"
10521083
android:roundIcon="@mipmap/ic_launcher_round"
@@ -1061,6 +1092,7 @@
10611092
</activity>
10621093
<activity
10631094
android:name=".logcat.LogViewerActivity"
1095+
android:enableOnBackInvokedCallback="true"
10641096
android:enabled="true"
10651097
android:exported="true"
10661098
android:label="@string/log_viewer"
@@ -1103,10 +1135,12 @@
11031135
</activity>
11041136
<activity
11051137
android:name=".logcat.RecordLogDialogActivity"
1138+
android:enableOnBackInvokedCallback="false"
11061139
android:exported="true"
11071140
android:theme="@style/AppTheme.TransparentBackground" />
11081141
<activity
11091142
android:name=".fm.FmActivity"
1143+
android:enableOnBackInvokedCallback="true"
11101144
android:exported="true"
11111145
android:icon="@mipmap/ic_launcher_fm"
11121146
android:label="@string/files"
@@ -1153,11 +1187,13 @@
11531187

11541188
<activity
11551189
android:name=".fm.OpenWithActivity"
1190+
android:enableOnBackInvokedCallback="false"
11561191
android:exported="false"
11571192
android:label="@string/file_open_with"
11581193
android:theme="@style/AppTheme.TransparentBackground" />
11591194
<activity
11601195
android:name=".viewer.audio.AudioPlayerActivity"
1196+
android:enableOnBackInvokedCallback="false"
11611197
android:excludeFromRecents="true"
11621198
android:exported="true"
11631199
android:label="@string/title_audio_player"
@@ -1199,6 +1235,7 @@
11991235
android:allowTaskReparenting="true"
12001236
android:alwaysRetainTaskState="false"
12011237
android:clearTaskOnLaunch="true"
1238+
android:enableOnBackInvokedCallback="false"
12021239
android:enabled="true"
12031240
android:excludeFromRecents="true"
12041241
android:finishOnTaskLaunch="true"

app/src/main/java/io/github/muntashirakon/AppManager/debloat/DebloaterActivity.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.view.MenuItem;
1212
import android.view.View;
1313

14+
import androidx.activity.OnBackPressedCallback;
1415
import androidx.annotation.NonNull;
1516
import androidx.annotation.Nullable;
1617
import androidx.appcompat.app.ActionBar;
@@ -39,11 +40,13 @@
3940
import io.github.muntashirakon.widget.RecyclerView;
4041

4142
public class DebloaterActivity extends BaseActivity implements MultiSelectionView.OnSelectionChangeListener,
42-
MultiSelectionActionsView.OnItemSelectedListener, AdvancedSearchView.OnQueryTextListener {
43+
MultiSelectionActionsView.OnItemSelectedListener, AdvancedSearchView.OnQueryTextListener,
44+
MultiSelectionView.OnSelectionModeChangeListener {
4345
DebloaterViewModel viewModel;
4446

4547
private LinearProgressIndicator mProgressIndicator;
4648
private MultiSelectionView mMultiSelectionView;
49+
private DebloaterRecyclerViewAdapter mAdapter;
4750

4851
private final StoragePermission mStoragePermission = StoragePermission.init(this);
4952
private final BroadcastReceiver mBatchOpsBroadCastReceiver = new BroadcastReceiver() {
@@ -54,11 +57,23 @@ public void onReceive(Context context, Intent intent) {
5457
}
5558
}
5659
};
60+
private final OnBackPressedCallback mOnBackPressedCallback = new OnBackPressedCallback(false) {
61+
@Override
62+
public void handleOnBackPressed() {
63+
if (mAdapter != null && mMultiSelectionView != null && mAdapter.isInSelectionMode()) {
64+
mMultiSelectionView.cancel();
65+
return;
66+
}
67+
setEnabled(false);
68+
getOnBackPressedDispatcher().onBackPressed();
69+
}
70+
};
5771

5872
@Override
5973
protected void onAuthenticated(@Nullable Bundle savedInstanceState) {
6074
setContentView(R.layout.activity_debloater);
6175
setSupportActionBar(findViewById(R.id.toolbar));
76+
getOnBackPressedDispatcher().addCallback(this, mOnBackPressedCallback);
6277
ActionBar actionBar = getSupportActionBar();
6378
if (actionBar != null) {
6479
actionBar.setDisplayHomeAsUpEnabled(true);
@@ -72,17 +87,18 @@ protected void onAuthenticated(@Nullable Bundle savedInstanceState) {
7287

7388
RecyclerView recyclerView = findViewById(R.id.recycler_view);
7489
recyclerView.setLayoutManager(UIUtils.getGridLayoutAt450Dp(this));
75-
DebloaterRecyclerViewAdapter adapter = new DebloaterRecyclerViewAdapter(this);
76-
recyclerView.setAdapter(adapter);
90+
mAdapter = new DebloaterRecyclerViewAdapter(this);
91+
recyclerView.setAdapter(mAdapter);
7792
mMultiSelectionView = findViewById(R.id.selection_view);
78-
mMultiSelectionView.setAdapter(adapter);
93+
mMultiSelectionView.setAdapter(mAdapter);
7994
mMultiSelectionView.hide();
8095
mMultiSelectionView.setOnItemSelectedListener(this);
96+
mMultiSelectionView.setOnSelectionModeChangeListener(this);
8197
mMultiSelectionView.setOnSelectionChangeListener(this);
8298

8399
viewModel.getDebloatObjectListLiveData().observe(this, debloatObjects -> {
84100
mProgressIndicator.hide();
85-
adapter.setAdapterList(debloatObjects);
101+
mAdapter.setAdapterList(debloatObjects);
86102
});
87103
viewModel.loadPackages();
88104
}
@@ -120,6 +136,16 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
120136
return super.onOptionsItemSelected(item);
121137
}
122138

139+
@Override
140+
public void onSelectionModeEnabled() {
141+
mOnBackPressedCallback.setEnabled(true);
142+
}
143+
144+
@Override
145+
public void onSelectionModeDisabled() {
146+
mOnBackPressedCallback.setEnabled(false);
147+
}
148+
123149
@Override
124150
public boolean onSelectionChange(int selectionCount) {
125151
// TODO: 7/8/22

app/src/main/java/io/github/muntashirakon/AppManager/editor/CodeEditorActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected void onAuthenticated(@Nullable Bundle savedInstanceState) {
8989
@Override
9090
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
9191
if (item.getItemId() == android.R.id.home) {
92-
onBackPressed();
92+
getOnBackPressedDispatcher().onBackPressed();
9393
return true;
9494
}
9595
return super.onOptionsItemSelected(item);

0 commit comments

Comments
 (0)