Skip to content

Commit c05fcf2

Browse files
committed
Implemented functionality to add Media from inside of Albums
1 parent 4f268d4 commit c05fcf2

7 files changed

Lines changed: 153 additions & 37 deletions

File tree

app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,14 @@ private void onCopyFileOperationFinish(CopyFileOperation operation, RemoteOperat
20152015

20162016
private void onCopyAlbumFileOperationFinish(CopyFileToAlbumOperation operation, RemoteOperationResult result) {
20172017
if (result.isSuccess()) {
2018-
DisplayUtils.showSnackMessage(this, "File added successfully");
2018+
// when item added from inside of Album
2019+
Fragment fragment = getSupportFragmentManager().findFragmentByTag(AlbumItemsFragment.Companion.getTAG());
2020+
if (fragment instanceof AlbumItemsFragment albumItemsFragment) {
2021+
albumItemsFragment.refreshData();
2022+
} else {
2023+
// files added directly from Media tab
2024+
DisplayUtils.showSnackMessage(this, "File added successfully");
2025+
}
20192026
Log_OC.e(TAG, "Files copied successfully");
20202027
} else {
20212028
try {

app/src/main/java/com/owncloud/android/ui/fragment/ExtendedListFragment.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ open class ExtendedListFragment :
165165
}
166166

167167
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
168-
val item = menu.findItem(R.id.action_search)
168+
// NMC Customization: while picking Media files from Gallery Fragment through AlbumPickerActivity
169+
// there will be no search option so it we have to return it
170+
val item = menu.findItem(R.id.action_search) ?: return
169171
searchView = MenuItemCompat.getActionView(item) as SearchView?
170172
viewThemeUtils.androidx.themeToolbarSearchView(searchView!!)
171173
closeButton = searchView?.findViewById<ImageView>(androidx.appcompat.R.id.search_close_btn)

app/src/main/java/com/owncloud/android/ui/fragment/GalleryFragment.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.content.res.Configuration;
1717
import android.os.AsyncTask;
1818
import android.os.Bundle;
19+
import android.text.TextUtils;
1920
import android.view.LayoutInflater;
2021
import android.view.Menu;
2122
import android.view.MenuInflater;
@@ -80,7 +81,10 @@ public class GalleryFragment extends OCFileListFragment implements GalleryFragme
8081
private final static int maxColumnSizeLandscape = 5;
8182
private final static int maxColumnSizePortrait = 2;
8283
private int columnSize;
84+
85+
// NMC: required for Albums
8386
private Set<OCFile> checkedFiles;
87+
private boolean isFromAlbum; // when opened from Albums to add items
8488

8589
protected void setPhotoSearchQueryRunning(boolean value) {
8690
this.photoSearchQueryRunning = value;
@@ -109,6 +113,10 @@ public void onCreate(Bundle savedInstanceState) {
109113
}
110114

111115
registerRefreshSearchEventReceiver();
116+
117+
if (getArguments() != null) {
118+
isFromAlbum = getArguments().getBoolean(AlbumsPickerActivity.Companion.getEXTRA_FROM_ALBUM(), false);
119+
}
112120
}
113121

114122
private void registerRefreshSearchEventReceiver() {
@@ -449,28 +457,41 @@ public void markAsFavorite(String remotePath, boolean favorite) {
449457
if (Activity.RESULT_OK == intentResult.getResultCode() && intentResult.getData() != null) {
450458
String albumName = intentResult.getData().getStringExtra(AlbumsFragment.ARG_SELECTED_ALBUM_NAME);
451459
Log_OC.e(TAG, "Selected album name: " + albumName);
452-
453-
connectivityService.isNetworkAndServerAvailable(result -> {
454-
if (result) {
455-
if (checkedFiles == null || checkedFiles.isEmpty()) {
456-
return;
457-
}
458-
final ArrayList<String> paths = new ArrayList<>(checkedFiles.size());
459-
for (OCFile file : checkedFiles) {
460-
paths.add(file.getRemotePath());
461-
}
462-
mContainerActivity.getFileOperationsHelper().albumCopyFiles(paths, albumName);
463-
checkedFiles = null;
464-
} else {
465-
DisplayUtils.showSnackMessage(requireActivity(), getString(R.string.offline_mode));
466-
}
467-
});
460+
addFilesToAlbum(albumName);
468461
}
469462
});
470463

471464
public void addImagesToAlbum(Set<OCFile> checkedFiles) {
472465
this.checkedFiles = checkedFiles;
466+
if (isFromAlbum) {
467+
addFilesToAlbum(null);
468+
} else {
469+
activityResult.launch(AlbumsPickerActivity.Companion.intentForPickingAlbum(requireActivity()));
470+
}
471+
}
473472

474-
activityResult.launch(AlbumsPickerActivity.Companion.intentForPickingAlbum(requireActivity()));
473+
private void addFilesToAlbum(@Nullable String albumName) {
474+
connectivityService.isNetworkAndServerAvailable(result -> {
475+
if (result) {
476+
if (checkedFiles == null || checkedFiles.isEmpty()) {
477+
return;
478+
}
479+
final ArrayList<String> paths = new ArrayList<>(checkedFiles.size());
480+
for (OCFile file : checkedFiles) {
481+
paths.add(file.getRemotePath());
482+
}
483+
checkedFiles = null;
484+
if (!TextUtils.isEmpty(albumName)) {
485+
mContainerActivity.getFileOperationsHelper().albumCopyFiles(paths, albumName);
486+
} else {
487+
Intent resultIntent = new Intent();
488+
resultIntent.putStringArrayListExtra(AlbumsPickerActivity.Companion.getEXTRA_MEDIA_FILES_PATH(), paths);
489+
requireActivity().setResult(Activity.RESULT_OK, resultIntent);
490+
requireActivity().finish();
491+
}
492+
} else {
493+
DisplayUtils.showSnackMessage(requireActivity(), getString(R.string.offline_mode));
494+
}
495+
});
475496
}
476497
}

app/src/main/java/com/owncloud/android/ui/fragment/albums/AlbumItemsFragment.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
package com.owncloud.android.ui.fragment.albums
99

10+
import android.app.Activity
1011
import android.content.Context
12+
import android.content.Intent
1113
import android.content.res.Configuration
1214
import android.os.Bundle
1315
import android.os.Handler
@@ -20,6 +22,9 @@ import android.view.MenuItem
2022
import android.view.View
2123
import android.view.ViewGroup
2224
import android.widget.AbsListView
25+
import androidx.activity.result.ActivityResult
26+
import androidx.activity.result.ActivityResultLauncher
27+
import androidx.activity.result.contract.ActivityResultContracts
2328
import androidx.annotation.DrawableRes
2429
import androidx.annotation.IdRes
2530
import androidx.annotation.StringRes
@@ -60,13 +65,15 @@ import com.owncloud.android.ui.adapter.GalleryAdapter
6065
import com.owncloud.android.ui.dialog.CreateAlbumDialogFragment
6166
import com.owncloud.android.ui.events.FavoriteEvent
6267
import com.owncloud.android.ui.fragment.FileFragment
68+
import com.owncloud.android.ui.fragment.albums.AlbumsPickerActivity.Companion.intentForPickingMediaFiles
6369
import com.owncloud.android.ui.interfaces.OCFileListFragmentInterface
6470
import com.owncloud.android.ui.preview.PreviewImageFragment
6571
import com.owncloud.android.ui.preview.PreviewMediaActivity
6672
import com.owncloud.android.utils.DisplayUtils
6773
import com.owncloud.android.utils.ErrorMessageAdapter
6874
import com.owncloud.android.utils.theme.ViewThemeUtils
6975
import kotlinx.coroutines.Dispatchers
76+
import kotlinx.coroutines.delay
7077
import kotlinx.coroutines.launch
7178
import kotlinx.coroutines.withContext
7279
import org.greenrobot.eventbus.EventBus
@@ -181,6 +188,7 @@ class AlbumItemsFragment : Fragment(), OCFileListFragmentInterface, Injectable {
181188

182189
R.id.action_add_more_photos -> {
183190
// open Gallery fragment as selection then add items to current album
191+
openGalleryToAddMedia()
184192
true
185193
}
186194

@@ -888,4 +896,34 @@ class AlbumItemsFragment : Fragment(), OCFileListFragmentInterface, Injectable {
888896
adapter?.clearCheckedItems()
889897
}
890898
}
899+
900+
private val activityResult: ActivityResultLauncher<Intent> = registerForActivityResult(
901+
ActivityResultContracts.StartActivityForResult()
902+
) { intentResult: ActivityResult ->
903+
if (Activity.RESULT_OK == intentResult.resultCode) {
904+
intentResult.data?.let {
905+
val paths = it.getStringArrayListExtra(AlbumsPickerActivity.EXTRA_MEDIA_FILES_PATH)
906+
paths?.let { p ->
907+
addMediaToAlbum(p.toMutableList())
908+
}
909+
}
910+
}
911+
}
912+
913+
private fun openGalleryToAddMedia() {
914+
activityResult.launch(intentForPickingMediaFiles(requireActivity()))
915+
}
916+
917+
private fun addMediaToAlbum(filePaths: MutableList<String>) {
918+
viewLifecycleOwner.lifecycleScope.launch {
919+
// short delay to let other transactions finish
920+
// else showLoadingDialog will throw exception
921+
delay(100)
922+
mContainerActivity?.fileOperationsHelper?.albumCopyFiles(filePaths, albumName)
923+
}
924+
}
925+
926+
fun refreshData(){
927+
fetchAndSetData()
928+
}
891929
}

app/src/main/java/com/owncloud/android/ui/fragment/albums/AlbumsFragment.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ class AlbumsFragment : Fragment(), AlbumFragmentInterface, Injectable {
349349
companion object {
350350
val TAG: String = AlbumsFragment::class.java.simpleName
351351
private const val ARG_IS_SELECTION_MODE = "is_selection_mode"
352-
const val SELECT_ALBUM_REQ_KEY = "select_album_req_key"
353352
const val ARG_SELECTED_ALBUM_NAME = "selected_album_name"
354353

355354
fun newInstance(isSelectionMode: Boolean = false): AlbumsFragment {

app/src/main/java/com/owncloud/android/ui/fragment/albums/AlbumsPickerActivity.kt

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ import com.owncloud.android.datamodel.OCFile
1919
import com.owncloud.android.lib.common.operations.RemoteOperation
2020
import com.owncloud.android.lib.common.operations.RemoteOperationResult
2121
import com.owncloud.android.lib.common.utils.Log_OC
22+
import com.owncloud.android.lib.resources.files.SearchRemoteOperation
2223
import com.owncloud.android.operations.albums.CreateNewAlbumOperation
2324
import com.owncloud.android.ui.activity.FileActivity
25+
import com.owncloud.android.ui.activity.FolderPickerActivity.Companion.TAG_LIST_OF_FOLDERS
26+
import com.owncloud.android.ui.activity.OnEnforceableRefreshListener
27+
import com.owncloud.android.ui.events.SearchEvent
2428
import com.owncloud.android.ui.fragment.FileFragment
29+
import com.owncloud.android.ui.fragment.GalleryFragment
30+
import com.owncloud.android.ui.fragment.OCFileListFragment
2531
import com.owncloud.android.utils.DisplayUtils
2632
import com.owncloud.android.utils.ErrorMessageAdapter
2733

28-
class AlbumsPickerActivity : FileActivity(), FileFragment.ContainerActivity, Injectable {
34+
class AlbumsPickerActivity : FileActivity(), FileFragment.ContainerActivity, OnEnforceableRefreshListener, Injectable {
2935

3036
private var captionText: String? = null
3137

@@ -64,27 +70,51 @@ class AlbumsPickerActivity : FileActivity(), FileFragment.ContainerActivity, Inj
6470

6571
private fun setupAction() {
6672
action = intent.getStringExtra(EXTRA_ACTION)
67-
6873
setupUIForChooseButton()
6974
}
7075

7176
private fun setupUIForChooseButton() {
72-
captionText = resources.getText(R.string.folder_picker_choose_caption_text).toString()
77+
if (action == CHOOSE_ALBUM) {
78+
captionText = resources.getText(R.string.album_picker_toolbar_title).toString()
79+
} else if (action == CHOOSE_MEDIA_FILES) {
80+
captionText = resources.getText(R.string.media_picker_toolbar_title).toString()
81+
}
7382

7483
folderPickerBinding.folderPickerBtnCopy.visibility = View.GONE
7584
folderPickerBinding.folderPickerBtnMove.visibility = View.GONE
7685
folderPickerBinding.folderPickerBtnChoose.visibility = View.GONE
86+
folderPickerBinding.folderPickerBtnCancel.visibility = View.GONE
7787
folderPickerBinding.chooseButtonSpacer.visibility = View.GONE
7888
folderPickerBinding.moveOrCopyButtonSpacer.visibility = View.GONE
7989
}
8090

81-
protected fun createFragments() {
91+
private fun createFragments() {
92+
if (action == CHOOSE_ALBUM) {
93+
val transaction = supportFragmentManager.beginTransaction()
94+
transaction.add(
95+
R.id.fragment_container,
96+
AlbumsFragment.newInstance(isSelectionMode = true),
97+
AlbumsFragment.TAG
98+
)
99+
transaction.commit()
100+
} else if (action == CHOOSE_MEDIA_FILES) {
101+
createGalleryFragment()
102+
}
103+
}
104+
105+
private fun createGalleryFragment() {
106+
val photoFragment = GalleryFragment()
107+
val bundle = Bundle()
108+
bundle.putParcelable(OCFileListFragment.SEARCH_EVENT, SearchEvent("image/%", SearchRemoteOperation.SearchType.PHOTO_SEARCH))
109+
bundle.putBoolean(EXTRA_FROM_ALBUM, true)
110+
photoFragment.arguments = bundle
111+
82112
val transaction = supportFragmentManager.beginTransaction()
83-
transaction.add(R.id.fragment_container, AlbumsFragment.newInstance(isSelectionMode = true), AlbumsFragment.TAG)
113+
transaction.add(R.id.fragment_container, photoFragment, TAG_LIST_OF_FOLDERS)
84114
transaction.commit()
85115
}
86116

87-
protected val listOfFilesFragment: AlbumsFragment?
117+
private val listOfFilesFragment: AlbumsFragment?
88118
get() {
89119
val listOfFiles = supportFragmentManager.findFragmentByTag(AlbumsFragment.TAG)
90120

@@ -125,9 +155,28 @@ class AlbumsPickerActivity : FileActivity(), FileFragment.ContainerActivity, Inj
125155
}
126156
}
127157

158+
override fun showDetails(file: OCFile?) {
159+
// not used at the moment
160+
}
161+
162+
override fun showDetails(file: OCFile?, activeTab: Int) {
163+
// not used at the moment
164+
}
165+
166+
override fun onBrowsedDownTo(folder: OCFile?) {
167+
// not used at the moment
168+
}
169+
170+
override fun onTransferStateChanged(file: OCFile?, downloading: Boolean, uploading: Boolean) {
171+
// not used at the moment
172+
}
173+
128174
companion object {
129175
private val EXTRA_ACTION = AlbumsPickerActivity::class.java.canonicalName?.plus(".EXTRA_ACTION")
130176
private val CHOOSE_ALBUM = AlbumsPickerActivity::class.java.canonicalName?.plus(".CHOOSE_ALBUM")
177+
private val CHOOSE_MEDIA_FILES = AlbumsPickerActivity::class.java.canonicalName?.plus(".CHOOSE_MEDIA_FILES")
178+
val EXTRA_FROM_ALBUM = AlbumsPickerActivity::class.java.canonicalName?.plus(".EXTRA_FROM_ALBUM")
179+
val EXTRA_MEDIA_FILES_PATH = AlbumsPickerActivity::class.java.canonicalName?.plus(".EXTRA_MEDIA_FILES_PATH")
131180

132181
private val TAG = AlbumsPickerActivity::class.java.simpleName
133182

@@ -136,21 +185,19 @@ class AlbumsPickerActivity : FileActivity(), FileFragment.ContainerActivity, Inj
136185
putExtra(EXTRA_ACTION, CHOOSE_ALBUM)
137186
}
138187
}
139-
}
140-
141-
override fun showDetails(file: OCFile?) {
142-
// not used at the moment
143-
}
144188

145-
override fun showDetails(file: OCFile?, activeTab: Int) {
146-
// not used at the moment
189+
fun intentForPickingMediaFiles(context: FragmentActivity): Intent {
190+
return Intent(context, AlbumsPickerActivity::class.java).apply {
191+
putExtra(EXTRA_ACTION, CHOOSE_MEDIA_FILES)
192+
}
193+
}
147194
}
148195

149-
override fun onBrowsedDownTo(folder: OCFile?) {
150-
// not used at the moment
196+
override fun onRefresh(enforced: Boolean) {
197+
// do nothing
151198
}
152199

153-
override fun onTransferStateChanged(file: OCFile?, downloading: Boolean, uploading: Boolean) {
154-
// not used at the moment
200+
override fun onRefresh() {
201+
// do nothing
155202
}
156203
}

app/src/main/res/values/nmc_album_strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919
<string name="album_delete">Delete Album</string>
2020
<string name="album_delete_failed_message">Failed to delete few of the files.</string>
2121
<string name="album_already_exists">Album already exists</string>
22+
<string name="album_picker_toolbar_title">Pick Album</string>
23+
<string name="media_picker_toolbar_title">Pick Media Files</string>
2224
</resources>

0 commit comments

Comments
 (0)