Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ android {
defaultConfig {
applicationId = "com.nextcloud.client"
minSdk = 27
targetSdk = 35
compileSdk = 35
targetSdk = 36
compileSdk = 36

buildConfigField "boolean", "CI", ciBuild.toString()
buildConfigField "boolean", "RUNTIME_PERF_ANALYSIS", perfAnalysis.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public void testSetupToolbar() {
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
Activity activity =
ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(RESUMED).iterator().next();
if (activity instanceof WhatsNewActivity) {
activity.onBackPressed();
if (activity instanceof WhatsNewActivity whatsNewActivity) {
whatsNewActivity.getOnBackPressedDispatcher().onBackPressed();
}
});
scenario.recreate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ class FileDetailSharingFragmentIT : AbstractIT() {
val processFragment =
activity.supportFragmentManager.findFragmentByTag(FileDetailsSharingProcessFragment.TAG) as
FileDetailsSharingProcessFragment
processFragment.onBackPressed()
processFragment.activity?.onBackPressedDispatcher?.onBackPressed()
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:enableOnBackInvokedCallback="false"
android:enableOnBackInvokedCallback="true"
android:theme="@style/Theme.ownCloud.Toolbar"
android:usesCleartextTraffic="true"
tools:ignore="UnusedAttribute"
Expand Down Expand Up @@ -353,6 +353,7 @@
<activity
android:name=".ui.activity.SettingsActivity"
android:exported="false"
android:enableOnBackInvokedCallback="false"
android:theme="@style/PreferenceTheme" />
<activity
android:name=".ui.preview.PreviewImageActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class DocumentScanActivity :
true
}
android.R.id.home -> {
onBackPressed()
onBackPressedDispatcher.onBackPressed()
true
}
else -> false
Expand Down
18 changes: 13 additions & 5 deletions app/src/main/java/com/nextcloud/client/etm/EtmActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.nextcloud.client.di.Injectable
Expand Down Expand Up @@ -46,6 +47,7 @@ class EtmActivity :
onPageChanged(it)
}
)
handleOnBackPressed()
}

override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
Expand All @@ -58,11 +60,17 @@ class EtmActivity :
else -> super.onOptionsItemSelected(item)
}

@Deprecated("Deprecated in Java")
override fun onBackPressed() {
if (!vm.onBackPressed()) {
super.onBackPressed()
}
private fun handleOnBackPressed() {
onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
val handledByVm = vm.onBackPressed()

if (!handledByVm) {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
}
}
})
}

private fun onPageChanged(page: EtmMenuEntry?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class WhatsNewActivity :
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
onFinish()
isEnabled = false
onBackPressedDispatcher.onBackPressed()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-FileCopyrightText: 2025 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

Expand All @@ -10,22 +10,10 @@ package com.nextcloud.utils.extensions
import android.content.Intent
import com.owncloud.android.MainApp
import com.owncloud.android.R
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.ui.activity.DrawerActivity
import com.owncloud.android.ui.activity.FileDisplayActivity

@Suppress("ReturnCount")
fun DrawerActivity.handleBackButtonEvent(currentDir: OCFile): Boolean {
if (DrawerActivity.menuItemId == R.id.nav_all_files && currentDir.isRootDirectory) {
moveTaskToBack(true)
return true
}

val isParentDirExists = (storageManager.getFileById(currentDir.parentId) != null)
if (isParentDirExists) {
return false
}

fun DrawerActivity.navigateToAllFiles() {
DrawerActivity.menuItemId = R.id.nav_all_files
setNavigationViewItemChecked()

Expand All @@ -38,6 +26,4 @@ fun DrawerActivity.handleBackButtonEvent(currentDir: OCFile): Boolean {
}.run {
startActivity(this)
}

return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.Menu;
Expand Down Expand Up @@ -115,6 +116,7 @@

import javax.inject.Inject;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -206,6 +208,12 @@ public abstract class DrawerActivity extends ToolbarActivity
@Inject
ClientFactory clientFactory;

@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
addOnBackPressedCallback();
}

/**
* Initializes the drawer and its content. This method needs to be called after the content view has been set.
*/
Expand Down Expand Up @@ -1153,19 +1161,24 @@ public void onConfigurationChanged(@NonNull Configuration newConfig) {
}
}

@Override
public void onBackPressed() {
if (isDrawerOpen()) {
closeDrawer();
return;
}
Fragment fileDetailsSharingProcessFragment =
getSupportFragmentManager().findFragmentByTag(FileDetailsSharingProcessFragment.TAG);
if (fileDetailsSharingProcessFragment != null) {
((FileDetailsSharingProcessFragment) fileDetailsSharingProcessFragment).onBackPressed();
} else {
super.onBackPressed();
}
public void addOnBackPressedCallback() {
getOnBackPressedDispatcher().addCallback(new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
if (isDrawerOpen()) {
closeDrawer();
return;
}

final var fragment = getSupportFragmentManager().findFragmentByTag(FileDetailsSharingProcessFragment.TAG);
if (fragment instanceof FileDetailsSharingProcessFragment fileDetailsSharingProcessFragment) {
fileDetailsSharingProcessFragment.onBackPressed();
} else {
setEnabled(false);
getOnBackPressedDispatcher().onBackPressed();
}
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import android.view.MenuItem
import android.view.View
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.view.WindowManager.BadTokenException
import androidx.activity.OnBackPressedCallback
import androidx.annotation.VisibleForTesting
import androidx.appcompat.widget.SearchView
import androidx.core.view.MenuItemCompat
Expand Down Expand Up @@ -82,6 +83,7 @@ import com.nextcloud.utils.extensions.getParcelableArgument
import com.nextcloud.utils.extensions.isActive
import com.nextcloud.utils.extensions.lastFragment
import com.nextcloud.utils.extensions.logFileSize
import com.nextcloud.utils.extensions.navigateToAllFiles
import com.nextcloud.utils.fileNameValidator.FileNameValidator.checkFolderPath
import com.nextcloud.utils.view.FastScrollUtils
import com.owncloud.android.MainApp
Expand Down Expand Up @@ -155,7 +157,6 @@ import com.owncloud.android.utils.PermissionUtil.requestNotificationPermission
import com.owncloud.android.utils.PushUtils
import com.owncloud.android.utils.StringUtils
import com.owncloud.android.utils.theme.CapabilityUtils
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -278,6 +279,7 @@ class FileDisplayActivity :
initSyncBroadcastReceiver()
observeWorkerState()
startMetadataSyncForRoot()
handleBackPress()
}

private fun loadSavedInstanceState(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -935,7 +937,7 @@ class FileDisplayActivity :
) {
openDrawer()
} else {
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
} else if (itemId == R.id.action_select_all) {
val fragment = this.listOfFilesFragment
Expand Down Expand Up @@ -1136,40 +1138,59 @@ class FileDisplayActivity :
}
}

private val isRootDirectory: Boolean
get() {
val currentDir = getCurrentDir()
return (currentDir == null || currentDir.parentId == FileDataStorageManager.ROOT_PARENT_ID.toLong())
}

/*
* BackPressed priority/hierarchy:
* 1. close search view if opened
* 2. close drawer if opened
* 3. if it is OCFileListFragment and it's in Root -> (finish Activity) or it's not Root -> (browse up)
* 4. otherwise pop up the fragment and sortGroup view visibility and call super.onBackPressed()
/**
* Sets up a custom back-press handler for this activity.
*
* This callback determines how the back button behaves based on the current UI state:
* - If the search view is open, it closes it.
* - If the navigation drawer is open, it closes it.
* - If the left fragment is an [OCFileListFragment]:
* - If in the root directory, it either navigates to "All Files" or finishes the activity.
* - Otherwise, it navigates one level up.
* - Otherwise, it pops the current fragment from the back stack.
*
* ### About `isEnabled`
* `isEnabled` is a property of [OnBackPressedCallback].
* When `isEnabled = false`, this callback is **temporarily disabled**,
* allowing the system or other callbacks to handle the back press instead.
*/
@SuppressFBWarnings("ITC_INHERITANCE_TYPE_CHECKING") // TODO Apply fail fast principle
override fun onBackPressed() {
if (isSearchOpen()) {
resetSearchAction()
return
}
private fun handleBackPress() {
onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
when {
isSearchOpen() -> {
isEnabled = false
resetSearchAction()
}

if (isDrawerOpen) {
super.onBackPressed()
return
}
isDrawerOpen -> {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
}

if (this.leftFragment is OCFileListFragment) {
if (isRoot(getCurrentDir())) {
finish()
} else {
browseUp(leftFragment as OCFileListFragment)
leftFragment is OCFileListFragment -> {
val fragment = leftFragment as OCFileListFragment
if (isRoot(getCurrentDir())) {
if (fragment.shouldNavigateBackToAllFiles()) {
navigateToAllFiles()
} else {
finish()
}
} else {
browseUp(fragment)
}
}

else -> {
isEnabled = false
popBack()
}
}
}
}
} else {
popBack()
}
)
}

private fun browseUp(listOfFiles: OCFileListFragment) {
Expand Down Expand Up @@ -1210,20 +1231,20 @@ class FileDisplayActivity :

if (leftFragment is UnifiedSearchFragment) {
showSortListGroup(false)
super.onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
}

/**
* Use this method when want to pop the fragment on back press. It resets Scrolling (See
* [with true][.resetScrolling] and pop the visibility for sortListGroup (See
* [with false][.showSortListGroup]. At last call to super.onBackPressed()
* [with false][.showSortListGroup]. At last call to onBackPressedDispatcher.onBackPressed()
*/
private fun popBack() {
binding.fabMain.setImageResource(R.drawable.ic_plus)
resetScrolling(true)
showSortListGroup(false)
super.onBackPressed()
onBackPressedDispatcher.onBackPressed()
}

override fun onSaveInstanceState(outState: Bundle) {
Expand Down Expand Up @@ -1645,7 +1666,7 @@ class FileDisplayActivity :
if (uploadWasFine || file != null && file.fileExists()) {
fileDetailFragment.updateFileDetails(false, true)
} else {
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}

// Force the preview if the file is an image or text file
Expand Down
Loading
Loading