3939import com .owncloud .android .R ;
4040import com .owncloud .android .domain .files .model .OCFile ;
4141import com .owncloud .android .domain .files .usecases .DisableThumbnailsForFileUseCase ;
42+ import com .owncloud .android .domain .files .usecases .GetWebDavUrlForSpaceUseCase ;
43+ import com .owncloud .android .domain .spaces .model .SpaceSpecial ;
4244import com .owncloud .android .lib .common .OwnCloudAccount ;
4345import com .owncloud .android .lib .common .OwnCloudClient ;
4446import com .owncloud .android .lib .common .SingleSessionManager ;
@@ -74,7 +76,8 @@ public class ThumbnailsCacheManager {
7476 private static final int mCompressQuality = 70 ;
7577 private static OwnCloudClient mClient = null ;
7678
77- private static final String PREVIEW_URI = "%s/remote.php/dav/files/%s%s?x=%d&y=%d&c=%s&preview=1" ;
79+ private static final String PREVIEW_URI = "%s%s?x=%d&y=%d&c=%s&preview=1" ;
80+ private static final String SPACE_SPECIAL_URI = "%s?scalingup=0&a=1&x=%d&y=%d&c=%s&preview=1" ;
7881
7982 public static Bitmap mDefaultImg =
8083 BitmapFactory .decodeResource (
@@ -186,6 +189,8 @@ protected Bitmap doInBackground(Object... params) {
186189 thumbnail = doOCFileInBackground ();
187190 } else if (mFile instanceof File ) {
188191 thumbnail = doFileInBackground ();
192+ } else if (mFile instanceof SpaceSpecial ) {
193+ thumbnail = doSpaceImageInBackground ();
189194 //} else { do nothing
190195 }
191196
@@ -210,6 +215,8 @@ protected void onPostExecute(Bitmap bitmap) {
210215 tagId = String .valueOf (((OCFile ) mFile ).getId ());
211216 } else if (mFile instanceof File ) {
212217 tagId = String .valueOf (mFile .hashCode ());
218+ } else if (mFile instanceof SpaceSpecial ) {
219+ tagId = ((SpaceSpecial ) mFile ).getId ();
213220 }
214221 if (String .valueOf (imageView .getTag ()).equals (tagId )) {
215222 imageView .setImageBitmap (bitmap );
@@ -252,10 +259,18 @@ private int getThumbnailDimension() {
252259 }
253260
254261 private String getPreviewUrl (OCFile ocFile , Account account ) {
262+ String baseUrl = mClient .getBaseUri () + "/remote.php/dav/files/" + account .name .split ("@" )[0 ];
263+
264+ if (ocFile .getSpaceId () != null ) {
265+ Lazy <GetWebDavUrlForSpaceUseCase > getWebDavUrlForSpaceUseCaseLazy = inject (GetWebDavUrlForSpaceUseCase .class );
266+ baseUrl = getWebDavUrlForSpaceUseCaseLazy .getValue ().execute (
267+ new GetWebDavUrlForSpaceUseCase .Params (ocFile .getOwner (), ocFile .getSpaceId ())
268+ );
269+
270+ }
255271 return String .format (Locale .ROOT ,
256272 PREVIEW_URI ,
257- mClient .getBaseUri (),
258- account .name .split ("@" )[0 ],
273+ baseUrl ,
259274 Uri .encode (ocFile .getRemotePath (), "/" ),
260275 getThumbnailDimension (),
261276 getThumbnailDimension (),
@@ -349,6 +364,64 @@ private Bitmap doFileInBackground() {
349364 return thumbnail ;
350365 }
351366
367+ private String getSpaceSpecialUri (SpaceSpecial spaceSpecial ) {
368+ // Converts dp to pixel
369+ Resources r = MainApp .Companion .getAppContext ().getResources ();
370+ Integer spacesThumbnailSize = Math .round (r .getDimension (R .dimen .spaces_thumbnail_height )) * 2 ;
371+ return String .format (Locale .ROOT ,
372+ SPACE_SPECIAL_URI ,
373+ spaceSpecial .getWebDavUrl (),
374+ spacesThumbnailSize ,
375+ spacesThumbnailSize ,
376+ spaceSpecial .getETag ());
377+ }
378+
379+ private Bitmap doSpaceImageInBackground () {
380+ SpaceSpecial spaceSpecial = (SpaceSpecial ) mFile ;
381+
382+ final String imageKey = spaceSpecial .getId ();
383+
384+ // Check disk cache in background thread
385+ Bitmap thumbnail = getBitmapFromDiskCache (imageKey );
386+
387+ // Not found in disk cache
388+ if (thumbnail == null ) {
389+ int px = getThumbnailDimension ();
390+
391+ // Download thumbnail from server
392+ if (mClient != null ) {
393+ GetMethod get ;
394+ try {
395+ String uri = getSpaceSpecialUri (spaceSpecial );
396+ Timber .d ("URI: %s" , uri );
397+ get = new GetMethod (new URL (uri ));
398+ int status = mClient .executeHttpMethod (get );
399+ if (status == HttpConstants .HTTP_OK ) {
400+ InputStream inputStream = get .getResponseBodyAsStream ();
401+ Bitmap bitmap = BitmapFactory .decodeStream (inputStream );
402+ thumbnail = ThumbnailUtils .extractThumbnail (bitmap , px , px );
403+
404+ // Handle PNG
405+ if (spaceSpecial .getFile ().getMimeType ().equalsIgnoreCase ("image/png" )) {
406+ thumbnail = handlePNG (thumbnail , px );
407+ }
408+
409+ // Add thumbnail to cache
410+ if (thumbnail != null ) {
411+ addBitmapToCache (imageKey , thumbnail );
412+ }
413+ } else {
414+ mClient .exhaustResponse (get .getResponseBodyAsStream ());
415+ }
416+ } catch (Exception e ) {
417+ Timber .e (e );
418+ }
419+ }
420+ }
421+
422+ return thumbnail ;
423+
424+ }
352425 }
353426
354427 public static boolean cancelPotentialThumbnailWork (Object file , ImageView imageView ) {
0 commit comments