1 package com.bumptech.glide.signature; 2 3 import com.bumptech.glide.load.Key; 4 5 import java.io.UnsupportedEncodingException; 6 import java.nio.ByteBuffer; 7 import java.security.MessageDigest; 8 9 /** 10 * A unique signature based on metadata data from the media store that detects common changes to media store files like 11 * edits, rotations, and temporary file replacement. 12 */ 13 public class MediaStoreSignature implements Key { 14 private final String mimeType; 15 private final long dateModified; 16 private final int orientation; 17 18 /** 19 * Constructor for {@link com.bumptech.glide.signature.MediaStoreSignature}. 20 * 21 * @param mimeType The mime type of the media store media. Ok to default to empty string "". See 22 * {@link android.provider.MediaStore.Images.ImageColumns#MIME_TYPE} or 23 * {@link android.provider.MediaStore.Video.VideoColumns#MIME_TYPE}. 24 * @param dateModified The date modified time of the media store media. Ok to default to 0. See 25 * {@link android.provider.MediaStore.Images.ImageColumns#DATE_MODIFIED} or 26 * {@link android.provider.MediaStore.Video.VideoColumns#DATE_MODIFIED}. 27 * @param orientation The orientation of the media store media. Ok to default to 0. See 28 * {@link android.provider.MediaStore.Images.ImageColumns#ORIENTATION}. 29 */ 30 public MediaStoreSignature(String mimeType, long dateModified, int orientation) { 31 this.mimeType = mimeType; 32 this.dateModified = dateModified; 33 this.orientation = orientation; 34 } 35 36 @Override 37 public boolean equals(Object o) { 38 if (this == o) { 39 return true; 40 } 41 if (o == null || getClass() != o.getClass()) { 42 return false; 43 } 44 45 MediaStoreSignature that = (MediaStoreSignature) o; 46 47 if (dateModified != that.dateModified) { 48 return false; 49 } 50 if (orientation != that.orientation) { 51 return false; 52 } 53 if (mimeType != null ? !mimeType.equals(that.mimeType) : that.mimeType != null) { 54 return false; 55 } 56 57 return true; 58 } 59 60 @Override 61 public int hashCode() { 62 int result = mimeType != null ? mimeType.hashCode() : 0; 63 result = 31 * result + (int) (dateModified ^ (dateModified >>> 32)); 64 result = 31 * result + orientation; 65 return result; 66 } 67 68 @Override 69 public void updateDiskCacheKey(MessageDigest messageDigest) throws UnsupportedEncodingException { 70 byte[] data = ByteBuffer.allocate(12) 71 .putLong(dateModified) 72 .putInt(orientation) 73 .array(); 74 messageDigest.update(data); 75 messageDigest.update(mimeType.getBytes(STRING_CHARSET_NAME)); 76 } 77 } 78