Android Open Source - no-cloud-share Share Item






From Project

Back to project page no-cloud-share.

License

The source code is released under:

GNU General Public License

If you think the Android project no-cloud-share listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.ub0r.android.nocloudshare.model;
/*from   ww w  .  ja  v  a2 s  . com*/
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import java.util.UUID;

import de.ub0r.android.logg0r.Log;

/**
 * @author flx
 */
public class ShareItem {

    private static final String TAG = "ShareItem";

    private static final long EXTEND_PERIOD = 15 * 60 * 1000;

    public static final int THUMBNAIL_SIZE = 512;

    private final String mHash;

    private final Uri mUri;

    private final String mContent;

    private String mMimeType;

    private String mName;

    private final long mCreation;

    private long mExpiration;

    public ShareItem(final String name, final String content, final String mimeType) {
        if (content == null) {
            throw new NullPointerException("content must not be null");
        }
        mHash = genHash();
        mName = name;
        mUri = null;
        mContent = content;
        mMimeType = mimeType;
        mCreation = System.currentTimeMillis();
    }

    public ShareItem(final Uri uri, final String mimeType) {
        if (uri == null) {
            throw new NullPointerException("uri must not be null");
        }
        if (!"file".equals(uri.getScheme())
                && !"content".equals(uri.getScheme())) {
            throw new IllegalArgumentException("uri must be a local uri");
        }

        mHash = genHash();
        mName = uri.getLastPathSegment();
        mUri = uri;
        mContent = null;
        mMimeType = mimeType;
        mCreation = System.currentTimeMillis();
    }

    private String genHash() {
        return UUID.randomUUID().toString();
    }

    public String getHash() {
        return mHash;
    }

    public void setName(final String name) {
        mName = name;
    }

    public String getName() {
        return mName;
    }

    public Uri getUri() {
        return mUri;
    }

    public boolean hasContent() {
        return mContent != null;
    }

    public String getContent() {
        return mContent;
    }

    public String getMimeType() {
        return mMimeType;
    }

    public void setMimeType(final String type) {
        mMimeType = type;
    }

    public String getExternalPath() {
        return "/" + mHash + "/" + getName();
    }

    public long getCreation() {
        return mCreation;
    }

    public long getExpiration() {
        return mExpiration;
    }

    public void setExpireIn(final long expiration) {
        mExpiration = System.currentTimeMillis() + expiration;
    }

    public void extend() {
        mExpiration = Math.max(System.currentTimeMillis(), mExpiration) + EXTEND_PERIOD;
    }

    public void expire() {
        mExpiration = System.currentTimeMillis() - 1L;
    }

    public boolean isExpired() {
        return mExpiration < System.currentTimeMillis();
    }

    public void setInfos(final Context context) {
        if (mUri == null) {
            return;
        }
        String[] proj = {MediaStore.MediaColumns.DISPLAY_NAME,
                MediaStore.MediaColumns.TITLE,
                MediaStore.MediaColumns.MIME_TYPE};
        Cursor c = context.getContentResolver().query(mUri, proj, null, null, null);
        if (c != null && c.moveToFirst()) {
            String name = c.getString(0);
            if (name == null) {
                name = c.getString(1);
            }
            if (name != null) {
                setName(name);
            }
            String mimeType = c.getString(2);
            if (mimeType != null) {
                setMimeType(mimeType);
            }
        }
        if (c != null) {
            c.close();
        }
    }

    public String getThumbnailName() {
        if (mUri == null || mMimeType == null || !mMimeType.startsWith("image/")) {
            Log.d(TAG, "mUri: ", mUri);
            Log.d(TAG, "mMimeType: ", mMimeType);
            return null;
        }
        return "thumb_" + mHash;
    }

    @Override
    public String toString() {
        return getClass().getName() + ": " + mName;
    }

    @Override
    public boolean equals(final Object o) {
        if (o == null || !(o instanceof ShareItem)) {
            return false;
        }
        ShareItem i = (ShareItem) o;
        return mUri != null && mUri.equals(i.getUri())
                || mContent != null && mContent.equals(i.getContent());
    }
}




Java Source Code List

de.ub0r.android.nocloudshare.AboutActivity.java
de.ub0r.android.nocloudshare.HttpService.java
de.ub0r.android.nocloudshare.IntroActivity.java
de.ub0r.android.nocloudshare.SettingsActivity.java
de.ub0r.android.nocloudshare.ShareActivity.java
de.ub0r.android.nocloudshare.ShareFragment.java
de.ub0r.android.nocloudshare.ShareListActivity.java
de.ub0r.android.nocloudshare.http.BitmapLruCache.java
de.ub0r.android.nocloudshare.http.Httpd.java
de.ub0r.android.nocloudshare.model.GsonFactory.java
de.ub0r.android.nocloudshare.model.ShareItemContainer.java
de.ub0r.android.nocloudshare.model.ShareItem.java
de.ub0r.android.nocloudshare.views.CheckableRelativeLayout.java
de.ub0r.android.nocloudshare.views.MeasuringRelativeLayout.java
fi.iki.elonen.NanoHTTPD.java