Android Open Source - dissertation-project Data Whole






From Project

Back to project page dissertation-project.

License

The source code is released under:

MIT License

If you think the Android project dissertation-project 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 com.fyp.resilience.database.model;
//from  ww  w. j a v  a2 s.  c o m
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.net.Uri;
import android.util.Log;

import com.fyp.resilience.Flags;
import com.fyp.resilience.event.WholeModified;
import com.fyp.resilience.interfaces.Messagable;
import com.fyp.resilience.interfaces.Partialable;
import com.fyp.resilience.proto.ProtoBuffSpecification.DataWholeMessage;
import com.fyp.resilience.util.Utils;
import com.fyp.resilience.widerst.model.DataWholePartial;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import de.greenrobot.event.EventBus;

@DatabaseTable(tableName = "data_whole")
public class DataWhole implements Partialable<DataWholePartial>, Messagable<DataWholeMessage> {

    private static final String TAG = DataWhole.class.getSimpleName();

    /**
     * @param {@link Context}
     * @param {@link Uri}
     * @return {@link DataWhole}
     */
    public static DataWhole getOwnedDataWhole(final Context context, final Uri uri, long pieceSize) {

        final String fileUri = Utils.getFilePathFromUri(context, uri);

        if (null == fileUri) {
            return null;
        }

        /* Create a temporary File to obtain its length in bytes */
        final File dataFile = new File(fileUri);

        if (Flags.DEBUG) {
            Log.d(TAG, "PieceSize is " + pieceSize);
        }

        /* Calculate the pieces based on the Fixed PIECE_SIZE */
        final int pieces = (int) Math.ceil((double) dataFile.length() / (double) pieceSize);

        /* Calculate the size of the last piece */
        final long lastPieceSize = dataFile.length() - ((pieces - 1) * pieceSize);

        if (Flags.DEBUG) {
            Log.d(TAG, "Last PieceSize is " + lastPieceSize);
        }

        /* Create the server ID */
        final String serverId = Utils.getDeviceInfo(context).getServerRegistrationId();
        final Calendar currTime = Calendar.getInstance();

        final DataWhole dataWhole = new DataWhole(
                serverId + currTime.getTimeInMillis(),
                uri.toString(),
                pieces,
                dataFile.getName(),
                context.getContentResolver().getType(uri),
                new Date(),
                true);

        final List<DataPiece> pieceList = new ArrayList<DataPiece>();
        for (int i = 1; i <= pieces; i++) {

            if (i == pieces) {
                pieceSize = lastPieceSize;
            }

            pieceList.add(DataPiece.getDataPiece(i, dataWhole, pieceSize));
        }

        dataWhole.mPieces = pieceList;

        return dataWhole;
    }

    /**
     * @param context
     * @param wholeMessage
     * @return
     */
    public static DataWhole getUnownedDataWhole(final Context context, final DataWholeMessage wholeMessage) {

        return new DataWhole(
                wholeMessage.getKey(),
                null,
                wholeMessage.getNoPieces(),
                wholeMessage.getFileName(),
                null,
                new Date(),
                false);
    }

    /* State constants */
    public static final int STATE_COMPLETED = 0;
    public static final int STATE_IN_PROGRESS = 1;
    public static final int STATE_DOWNLOADING = 2;
    public static final int STATE_NONE = 3;

    /* Column name constants */
    static final String COL_KEY = "key";
    static final String COL_URI = "uri";
    static final String COL_NO_PIECES = "piece_number";
    static final String COL_FILE_NAME = "file_name";
    static final String COL_ADDED_ON = "added_on";
    static final String COL_OVERALL_STATE = "state";
    static final String COL_MIME_TYPE = "mime_type";
    static final String COL_AVAILABLE = "available";
    static final String COL_OWNED = "owned";

    @DatabaseField(columnName = COL_KEY, id = true)
    private String mKey;

    @DatabaseField(columnName = COL_URI)
    private String mUri;

    @DatabaseField(columnName = COL_NO_PIECES)
    private int mNoPieces;

    @DatabaseField(columnName = COL_FILE_NAME)
    private String mFileName;

    @DatabaseField(columnName = COL_ADDED_ON)
    private Date mTimeAdded;

    @DatabaseField(columnName = COL_OVERALL_STATE)
    private int mState;

    @DatabaseField(columnName = COL_MIME_TYPE)
    private String mMimeType;

    @DatabaseField(columnName = COL_OWNED)
    private boolean mOwned;

    @DatabaseField(columnName = COL_AVAILABLE)
    private boolean mAvailable;

    private List<DataPiece> mPieces;

    /* Instance-specific fields */
    private File mFile;

    DataWhole() {
        /* Ormlite requires a constructor with no arguments */
    }

    private DataWhole(final String key, final String uri, final int noPieces, final String fileName,
            final String mimeType,
            final Date date, final boolean owned) {
        mKey = key;
        mUri = uri;
        mNoPieces = noPieces;
        mFileName = fileName;
        mMimeType = mimeType;
        mTimeAdded = date;
        mState = STATE_NONE;
        mAvailable = false;
        mOwned = owned;
        mPieces = new ArrayList<DataPiece>();
    }

    public String getKey() {
        return mKey;
    }

    public boolean isAvailable() {
        return mAvailable;
    }

    public boolean isOwned() {
        return mOwned;
    }

    public String getUriString() {
        return mUri;
    }

    public void setUriString(final String uri) {
        mUri = uri;
    }

    public int getNoPieces() {
        return mNoPieces;
    }

    public List<DataPiece> getPieces() {
        if (null != mPieces) {
            synchronized (mPieces) {
                return new ArrayList<DataPiece>(mPieces);
            }
        }
        return null;
    }

    public synchronized void addPiece(final DataPiece dataPiece) {
        if (null == mPieces) {
            mPieces = new ArrayList<DataPiece>();
        }
        mPieces.add(dataPiece);
    }

    public void setPieces(final List<DataPiece> pieces) {
        if (pieces != null) {
            for (DataPiece dataPiece : pieces) {
                dataPiece.setParent(this);
                if (dataPiece.getState() == DataPiece.STATE_IN_PROGRESS) {
                    dataPiece.setState(DataPiece.STATE_NONE);
                }
            }
        }
        mPieces = pieces;
    }

    public Date getTimeAdded() {
        return mTimeAdded;
    }

    public String getFileName() {
        return mFileName;
    }

    public File getFile(final Context context) {
        if (null == mFile && null != mUri) {
            String filePath = Utils.getFilePathFromUri(context, Uri.parse(mUri));
            if (null != filePath) {
                mFile = new File(filePath);
            }
        }

        if (null != mFile) {
            return mFile.exists() ? mFile : null;
        }

        return null;
    }

    public int getState() {
        return mState;
    }

    public void setState(final int state) {
        mState = state;
        postEvent(new WholeModified());
    }

    public void setAvailability(final boolean available) {
        mAvailable = available;
    }

    private void postEvent(final Object event) {
        EventBus.getDefault().post(event);
    }

    @Override
    public String toString() {
        return mKey;
    }

    @Override
    public int hashCode() {
        return mKey.hashCode();
    }

    @Override
    public boolean equals(final Object object) {
        return mKey != null && mKey.contentEquals(((DataWhole) object).getKey());
    }

    @Override
    public DataWholePartial toPartial() {
        return new DataWholePartial()
                .setFileName(mFileName)
                .setMimeType(mMimeType)
                .setKey(mKey)
                .setNumOfPieces(mNoPieces);
    }

    @Override
    public DataWholeMessage toMessage() {
        return DataWholeMessage.newBuilder()
                .setKey(mKey)
                .setNoPieces(mNoPieces)
                .setFileName(mFileName)
                .build();
    }
}




Java Source Code List

com.fyp.resilience.Constants.java
com.fyp.resilience.Flags.java
com.fyp.resilience.GCMIntentService.java
com.fyp.resilience.PreferenceConstants.java
com.fyp.resilience.ResilienceApplication.java
com.fyp.resilience.ResilienceController.java
com.fyp.resilience.activity.LicenceActivity.java
com.fyp.resilience.activity.ResilienceActivity.java
com.fyp.resilience.activity.SettingsActivity.java
com.fyp.resilience.adapter.ClientListAdapter.java
com.fyp.resilience.adapter.ConnectionListAdapter.java
com.fyp.resilience.adapter.FileListAdapter.java
com.fyp.resilience.connection.Connectable.java
com.fyp.resilience.connection.ServerDownloadConnectable.java
com.fyp.resilience.connection.ServerUploadConnectable.java
com.fyp.resilience.connection.UploadConnectable.java
com.fyp.resilience.connection.WifiDownloadConnectable.java
com.fyp.resilience.connection.WifiUploadConnectable.java
com.fyp.resilience.database.ResilienceDbHelper.java
com.fyp.resilience.database.ResilienceDbManager.java
com.fyp.resilience.database.model.DataPiece.java
com.fyp.resilience.database.model.DataWhole.java
com.fyp.resilience.event.ClientListChanged.java
com.fyp.resilience.event.ClientModified.java
com.fyp.resilience.event.ConnectionProgressChange.java
com.fyp.resilience.event.ConnectionStateChange.java
com.fyp.resilience.event.ConnectionsModified.java
com.fyp.resilience.event.PieceStateChange.java
com.fyp.resilience.event.ServerRegistrationChanged.java
com.fyp.resilience.event.ServerUploadFinished.java
com.fyp.resilience.event.WholeModified.java
com.fyp.resilience.event.WifiDownloadFinished.java
com.fyp.resilience.event.WifiUploadFinished.java
com.fyp.resilience.fragment.ClientsFragment.java
com.fyp.resilience.fragment.ConnectionsFragment.java
com.fyp.resilience.fragment.FilesFragment.java
com.fyp.resilience.interfaces.Messagable.java
com.fyp.resilience.interfaces.Partialable.java
com.fyp.resilience.proto.ProtoBuffSpecification.java
com.fyp.resilience.receiver.AbstractConnectivityBroadcastReceiver.java
com.fyp.resilience.receiver.BootReceiver.java
com.fyp.resilience.receiver.ConnectivityBroadcastReceiver.java
com.fyp.resilience.receiver.WiFiDirectBroadcastReceiver.java
com.fyp.resilience.register.RegisterRequestInitializer.java
com.fyp.resilience.register.RegisterRequest.java
com.fyp.resilience.register.RegisterScopes.java
com.fyp.resilience.register.Register.java
com.fyp.resilience.register.model.DeviceInfo.java
com.fyp.resilience.service.PieceUploadService.java
com.fyp.resilience.stream.PiecedRandomAccessFile.java
com.fyp.resilience.swarm.helper.NsdHelper.java
com.fyp.resilience.swarm.helper.SwarmHelperInterface.java
com.fyp.resilience.swarm.helper.WifiDirectSdHelper.java
com.fyp.resilience.swarm.model.SwarmClient.java
com.fyp.resilience.thread.ResilienceRunnable.java
com.fyp.resilience.thread.ResilienceThreadFactory.java
com.fyp.resilience.util.ConnectionUtils.java
com.fyp.resilience.util.Utils.java
com.fyp.resilience.view.ClientView.java
com.fyp.resilience.view.ConnectionView.java
com.fyp.resilience.view.FileView.java
com.fyp.resilience.view.PieceProgressIndicator.java
com.fyp.resilience.widerst.WiderstRequestInitializer.java
com.fyp.resilience.widerst.WiderstRequest.java
com.fyp.resilience.widerst.WiderstScopes.java
com.fyp.resilience.widerst.Widerst.java
com.fyp.resilience.widerst.model.DataPiecePartial.java
com.fyp.resilience.widerst.model.DataWholePartial.java
com.fyp.resilience.widerst.model.PostResponse.java
com.fyp.widerst.Constants.java
com.fyp.widerst.WiderstObjectifyService.java
com.fyp.widerst.backend.FileJoinerBackend.java
com.fyp.widerst.cron.CronJobServlet.java
com.fyp.widerst.endpoint.DataPieceEndpoint.java
com.fyp.widerst.endpoint.DeviceInfoEndpoint.java
com.fyp.widerst.entity.DataPiece.java
com.fyp.widerst.entity.DataWhole.java
com.fyp.widerst.entity.DeviceInfo.java
com.fyp.widerst.handler.BlobstoreUploadHandler.java
com.fyp.widerst.partial.DataPiecePartial.java
com.fyp.widerst.partial.DataWholePartial.java
com.fyp.widerst.response.PostResponse.java
com.fyp.widerst.servlet.WholeFileServer.java
com.fyp.widerst.util.DbHelper.java
com.fyp.widerst.util.GcmHelper.java