MediaProvider.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.util.Log;
  29. import com.google.android.material.snackbar.Snackbar;
  30. import com.owncloud.android.MainApp;
  31. import com.owncloud.android.R;
  32. import com.owncloud.android.utils.PermissionUtil;
  33. import com.owncloud.android.utils.ThemeUtils;
  34. import java.io.File;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. import javax.annotation.Nullable;
  38. /**
  39. * Media queries to gain access to media lists for the device.
  40. */
  41. public final class MediaProvider {
  42. private static final String TAG = MediaProvider.class.getSimpleName();
  43. // fixed query parameters
  44. private static final Uri IMAGES_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 IMAGES_FILE_SELECTION = MediaStore.Images.Media.BUCKET_ID + "=";
  47. private static final String[] IMAGES_FOLDER_PROJECTION = {"Distinct " + MediaStore.Images.Media.BUCKET_ID,
  48. MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
  49. private static final String IMAGES_FOLDER_SORT_ORDER = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " ASC";
  50. private static final String[] VIDEOS_FOLDER_PROJECTION = {"Distinct " + MediaStore.Video.Media.BUCKET_ID,
  51. MediaStore.Video.Media.BUCKET_DISPLAY_NAME};
  52. private MediaProvider() {
  53. // utility class -> private constructor
  54. }
  55. /**
  56. * Getting All Images Paths.
  57. *
  58. * @param contentResolver the content resolver
  59. * @param itemLimit the number of media items (usually images) to be returned per media folder.
  60. * @return list with media folders
  61. */
  62. public static List<MediaFolder> getImageFolders(ContentResolver contentResolver, int itemLimit,
  63. @Nullable final Activity activity, boolean getWithoutActivity) {
  64. // check permissions
  65. checkPermissions(activity);
  66. // query media/image folders
  67. Cursor cursorFolders = null;
  68. if ((activity != null && PermissionUtil.checkSelfPermission(activity.getApplicationContext(),
  69. Manifest.permission.WRITE_EXTERNAL_STORAGE)) || getWithoutActivity) {
  70. cursorFolders = contentResolver.query(IMAGES_MEDIA_URI, IMAGES_FOLDER_PROJECTION, null, null,
  71. IMAGES_FOLDER_SORT_ORDER);
  72. }
  73. List<MediaFolder> mediaFolders = new ArrayList<>();
  74. String dataPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder();
  75. if (cursorFolders != null) {
  76. String folderName;
  77. String fileSortOrder = MediaStore.Images.Media.DATE_TAKEN + " DESC LIMIT " + itemLimit;
  78. Cursor cursorImages;
  79. while (cursorFolders.moveToNext()) {
  80. String folderId = cursorFolders.getString(cursorFolders.getColumnIndex(MediaStore.Images.Media
  81. .BUCKET_ID));
  82. MediaFolder mediaFolder = new MediaFolder();
  83. folderName = cursorFolders.getString(cursorFolders.getColumnIndex(
  84. MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
  85. mediaFolder.type = MediaFolderType.IMAGE;
  86. mediaFolder.folderName = folderName;
  87. mediaFolder.filePaths = new ArrayList<>();
  88. // query images
  89. cursorImages = contentResolver.query(
  90. IMAGES_MEDIA_URI,
  91. FILE_PROJECTION,
  92. IMAGES_FILE_SELECTION + folderId,
  93. null,
  94. fileSortOrder
  95. );
  96. Log.d(TAG, "Reading images for " + mediaFolder.folderName);
  97. if (cursorImages != null) {
  98. String filePath;
  99. while (cursorImages.moveToNext()) {
  100. filePath = cursorImages.getString(cursorImages.getColumnIndexOrThrow(
  101. MediaStore.MediaColumns.DATA));
  102. // check if valid path and file exists
  103. if (filePath != null && filePath.lastIndexOf('/') > 0 && new File(filePath).exists()) {
  104. mediaFolder.filePaths.add(filePath);
  105. mediaFolder.absolutePath = filePath.substring(0, filePath.lastIndexOf('/'));
  106. }
  107. }
  108. cursorImages.close();
  109. // only do further work if folder is not within the Nextcloud app itself
  110. if (mediaFolder.absolutePath != null && !mediaFolder.absolutePath.startsWith(dataPath)) {
  111. // count images
  112. Cursor count = contentResolver.query(
  113. IMAGES_MEDIA_URI,
  114. FILE_PROJECTION,
  115. IMAGES_FILE_SELECTION + folderId,
  116. null,
  117. null);
  118. if (count != null) {
  119. mediaFolder.numberOfFiles = count.getCount();
  120. count.close();
  121. }
  122. mediaFolders.add(mediaFolder);
  123. }
  124. }
  125. }
  126. cursorFolders.close();
  127. }
  128. return mediaFolders;
  129. }
  130. private static void checkPermissions(@Nullable Activity activity) {
  131. if (activity != null &&
  132. !PermissionUtil.checkSelfPermission(activity.getApplicationContext(),
  133. Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
  134. // Check if we should show an explanation
  135. if (PermissionUtil.shouldShowRequestPermissionRationale(activity,
  136. Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
  137. // Show explanation to the user and then request permission
  138. Snackbar snackbar = Snackbar.make(activity.findViewById(R.id.ListLayout),
  139. R.string.permission_storage_access, Snackbar.LENGTH_INDEFINITE)
  140. .setAction(R.string.common_ok, v -> PermissionUtil.requestWriteExternalStoreagePermission(activity));
  141. ThemeUtils.colorSnackbar(activity.getApplicationContext(), snackbar);
  142. snackbar.show();
  143. } else {
  144. // No explanation needed, request the permission.
  145. PermissionUtil.requestWriteExternalStoreagePermission(activity);
  146. }
  147. }
  148. }
  149. public static List<MediaFolder> getVideoFolders(ContentResolver contentResolver, int itemLimit,
  150. @Nullable final Activity activity, boolean getWithoutActivity) {
  151. // check permissions
  152. checkPermissions(activity);
  153. // query media/image folders
  154. Cursor cursorFolders = null;
  155. if ((activity != null && PermissionUtil.checkSelfPermission(activity.getApplicationContext(),
  156. Manifest.permission.WRITE_EXTERNAL_STORAGE)) || getWithoutActivity) {
  157. cursorFolders = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, VIDEOS_FOLDER_PROJECTION,
  158. null, null, null);
  159. }
  160. List<MediaFolder> mediaFolders = new ArrayList<>();
  161. String dataPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder();
  162. if (cursorFolders != null) {
  163. String folderName;
  164. String fileSortOrder = MediaStore.Video.Media.DATE_TAKEN + " DESC LIMIT " + itemLimit;
  165. Cursor cursorVideos;
  166. while (cursorFolders.moveToNext()) {
  167. String folderId = cursorFolders.getString(cursorFolders.getColumnIndex(MediaStore.Video.Media
  168. .BUCKET_ID));
  169. MediaFolder mediaFolder = new MediaFolder();
  170. folderName = cursorFolders.getString(cursorFolders.getColumnIndex(
  171. MediaStore.Video.Media.BUCKET_DISPLAY_NAME));
  172. mediaFolder.type = MediaFolderType.VIDEO;
  173. mediaFolder.folderName = folderName;
  174. mediaFolder.filePaths = new ArrayList<>();
  175. // query videos
  176. cursorVideos = contentResolver.query(
  177. MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  178. FILE_PROJECTION,
  179. MediaStore.Video.Media.BUCKET_ID + "=" + folderId,
  180. null,
  181. fileSortOrder);
  182. Log.d(TAG, "Reading videos for " + mediaFolder.folderName);
  183. if (cursorVideos != null) {
  184. String filePath;
  185. while (cursorVideos.moveToNext()) {
  186. filePath = cursorVideos.getString(cursorVideos.getColumnIndexOrThrow(
  187. MediaStore.MediaColumns.DATA));
  188. if (filePath != null) {
  189. mediaFolder.filePaths.add(filePath);
  190. mediaFolder.absolutePath = filePath.substring(0, filePath.lastIndexOf('/'));
  191. }
  192. }
  193. cursorVideos.close();
  194. // only do further work if folder is not within the Nextcloud app itself
  195. if (mediaFolder.absolutePath != null && !mediaFolder.absolutePath.startsWith(dataPath)) {
  196. // count images
  197. Cursor count = contentResolver.query(
  198. MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  199. FILE_PROJECTION,
  200. MediaStore.Video.Media.BUCKET_ID + "=" + folderId,
  201. null,
  202. null);
  203. if (count != null) {
  204. mediaFolder.numberOfFiles = count.getCount();
  205. count.close();
  206. }
  207. mediaFolders.add(mediaFolder);
  208. }
  209. }
  210. }
  211. cursorFolders.close();
  212. }
  213. return mediaFolders;
  214. }
  215. }