MediaProvider.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * Copyright (C) 2016 Andy Scherzinger
  6. * Copyright (C) 2016 Nextcloud
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.datamodel;
  22. import android.Manifest;
  23. import android.app.Activity;
  24. import android.content.ContentResolver;
  25. import android.database.Cursor;
  26. import android.net.Uri;
  27. import android.provider.MediaStore;
  28. import android.support.design.widget.Snackbar;
  29. import android.util.Log;
  30. import android.view.View;
  31. import com.owncloud.android.MainApp;
  32. import com.owncloud.android.R;
  33. import com.owncloud.android.utils.PermissionUtil;
  34. import com.owncloud.android.utils.ThemeUtils;
  35. import java.io.File;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. /**
  39. * Media queries to gain access to media lists for the device.
  40. */
  41. public class MediaProvider {
  42. private static final String TAG = MediaProvider.class.getSimpleName();
  43. // fixed query parameters
  44. private static final Uri MEDIA_URI = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  45. private static final String[] FILE_PROJECTION = new String[]{MediaStore.MediaColumns.DATA};
  46. private static final String FILE_SELECTION = MediaStore.Images.Media.BUCKET_ID + "=";
  47. private static final String[] FOLDER_PROJECTION = { "Distinct " + MediaStore.Images.Media.BUCKET_ID,
  48. MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
  49. private static final String FOLDER_SORT_ORDER = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " ASC";
  50. /**
  51. * Getting All Images Paths.
  52. *
  53. * @param contentResolver the content resolver
  54. * @param itemLimit the number of media items (usually images) to be returned per media folder.
  55. * @return list with media folders
  56. */
  57. public static List<MediaFolder> getMediaFolders(ContentResolver contentResolver, int itemLimit,
  58. final Activity activity) {
  59. // check permissions
  60. if (!PermissionUtil.checkSelfPermission(activity.getApplicationContext(),
  61. Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
  62. // Check if we should show an explanation
  63. if (PermissionUtil.shouldShowRequestPermissionRationale(activity,
  64. Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
  65. // Show explanation to the user and then request permission
  66. Snackbar snackbar = Snackbar.make(activity.findViewById(R.id.ListLayout),
  67. R.string.permission_storage_access, Snackbar.LENGTH_INDEFINITE)
  68. .setAction(R.string.common_ok, new View.OnClickListener() {
  69. @Override
  70. public void onClick(View v) {
  71. PermissionUtil.requestWriteExternalStoreagePermission(activity);
  72. }
  73. });
  74. ThemeUtils.colorSnackbar(activity.getApplicationContext(), snackbar);
  75. snackbar.show();
  76. } else {
  77. // No explanation needed, request the permission.
  78. PermissionUtil.requestWriteExternalStoreagePermission(activity);
  79. }
  80. }
  81. // query media/image folders
  82. Cursor cursorFolders = null;
  83. if (PermissionUtil.checkSelfPermission(activity.getApplicationContext(),
  84. Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
  85. cursorFolders = contentResolver.query(MEDIA_URI, FOLDER_PROJECTION, null, null, FOLDER_SORT_ORDER);
  86. }
  87. List<MediaFolder> mediaFolders = new ArrayList<>();
  88. String dataPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder();
  89. if (cursorFolders != null) {
  90. String folderName;
  91. String fileSortOrder = MediaStore.Images.Media.DATE_TAKEN + " DESC LIMIT " + itemLimit;
  92. Cursor cursorImages;
  93. while (cursorFolders.moveToNext()) {
  94. String folderId = cursorFolders.getString(cursorFolders.getColumnIndex(MediaStore.Images.Media
  95. .BUCKET_ID));
  96. MediaFolder mediaFolder = new MediaFolder();
  97. folderName = cursorFolders.getString(cursorFolders.getColumnIndex(
  98. MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
  99. mediaFolder.folderName = folderName;
  100. mediaFolder.filePaths = new ArrayList<>();
  101. // query images
  102. cursorImages = contentResolver.query(MEDIA_URI, FILE_PROJECTION, FILE_SELECTION + folderId, null,
  103. fileSortOrder);
  104. Log.d(TAG, "Reading images for " + mediaFolder.folderName);
  105. if (cursorImages != null) {
  106. String filePath;
  107. int failedImages = 0;
  108. while (cursorImages.moveToNext()) {
  109. filePath = cursorImages.getString(cursorImages.getColumnIndexOrThrow(
  110. MediaStore.MediaColumns.DATA));
  111. if (filePath != null) {
  112. mediaFolder.filePaths.add(filePath);
  113. mediaFolder.absolutePath = filePath.substring(0, filePath.lastIndexOf("/"));
  114. } else {
  115. failedImages++;
  116. }
  117. }
  118. cursorImages.close();
  119. // only do further work if folder is not within the Nextcloud app itself
  120. if (!mediaFolder.absolutePath.startsWith(dataPath)) {
  121. // count images
  122. Cursor count = contentResolver.query(
  123. MEDIA_URI,
  124. FILE_PROJECTION,
  125. FILE_SELECTION + folderId,
  126. null,
  127. null);
  128. if (count != null) {
  129. mediaFolder.numberOfFiles = count.getCount() - failedImages;
  130. count.close();
  131. }
  132. mediaFolders.add(mediaFolder);
  133. }
  134. }
  135. }
  136. cursorFolders.close();
  137. }
  138. return mediaFolders;
  139. }
  140. }