MediaProvider.java 11 KB

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