/* 
 * Copyright 2014 OpenMarket Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.matrix.androidsdk.rest.model;

import android.net.Uri;

import java.io.File;

public class ImageMessage extends Message {
    public ImageInfo info;
    public ImageInfo thumbnailInfo;
    public String url;
    public String thumbnailUrl;

    public ImageMessage() {
        msgtype = MSGTYPE_IMAGE;
    }

    public boolean isLocalContent() {
        return (null != url) && (url.startsWith("file://"));
    }

    public String getMimeType() {
        if (null != info) {
            return info.mimetype;
        } else {
            return null;
        }
    }

    /**
     * Checks if the media Urls are still valid.
     * The media Urls could define a file path.
     * They could have been deleted after a media cache cleaning.
     */
    public void checkMediaUrls() {
        if ((thumbnailUrl != null) && thumbnailUrl.startsWith("file://")) {
            try {
                File file = new File(Uri.parse(thumbnailUrl).getPath());

                if (!file.exists()) {
                    thumbnailUrl = null;
                }
            } catch (Exception e) {

            }
        }

        if ((url != null) && url.startsWith("file://")) {
            try {
                File file = new File(Uri.parse(url).getPath());

                if (!file.exists()) {
                    url = null;
                }
            } catch (Exception e) {

            }
        }
    }
}

@@ -638,16 +638,52 @@
 model;%0A%0A
+import android.media.ExifInterface;%0A
 import a
@@ -698,16 +698,16 @@
 et.Uri;%0A
-
 %0Aimport 
@@ -1050,32 +1050,104 @@
 e://%22));%0A    %7D%0A%0A
+    /**%0A     * @return The image mimetype. null is not defined.%0A     */%0A
     public Strin
@@ -1239,24 +1239,24 @@
    %7D else %7B%0A
-
             
@@ -1277,32 +1277,638 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * @return the rotation angle. Integer.MAX_VALUE if not defined.%0A     */%0A    public int getRotation() %7B%0A        if ((null != info) && (null != info.rotation)) %7B%0A            return info.rotation;%0A        %7D else %7B%0A            return Integer.MAX_VALUE;%0A        %7D%0A    %7D%0A%0A    /**%0A     * @return the image orientation. ExifInterface.ORIENTATION_UNDEFINED if not defined.%0A     */%0A    public int getOrientation() %7B%0A        if ((null != info) && (null != info.orientation)) %7B%0A            return info.orientation;%0A        %7D else %7B%0A            return ExifInterface.ORIENTATION_UNDEFINED;%0A        %7D%0A    %7D%0A%0A
     /**%0A     * C

Add some getters