MediaProvider.java 11 KB

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