Android Open Source - JayJayLab-Android-Demo G P X Writer






From Project

Back to project page JayJayLab-Android-Demo.

License

The source code is released under:

Apache License

If you think the Android project JayJayLab-Android-Demo 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.jayjaylab.androiddemo.app.greyhound.util;
//from  w  w w .  j  a v  a 2  s . c  om
import android.content.Context;
import android.location.Location;
import android.support.v4.content.ContextCompat;

import com.google.inject.Inject;
import com.jayjaylab.androiddemo.util.AndroidHelper;
import com.jayjaylab.androiddemo.util.NIOHelper;

import org.joda.time.DateTime;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import roboguice.util.Ln;

/**
 * Created by jongjoo on 11/29/14.
 */
public class GPXWriter {
    final int BUFFER_SIZE = 1024;
    final long FREE_SPACE_LIMIT = 10 * 1024 * 1024;

    ByteBuffer byteBuffer;
    File currentFile;
//    RandomAccessFile currentFile;
    FileChannel fileChannel;

    public GPXWriter() {
        byteBuffer = ByteBuffer.allocate(BUFFER_SIZE);
    }

    public boolean openFile(String dirPath, String fileName) {
        Ln.d("openFile() : dirPath : %s, fileName : %s", dirPath, fileName);
        if(!AndroidHelper.isExternalStorageWritable())
            return false;

        // checks whether there's enough storage
        final long freeSpace = AndroidHelper.getFreeSpace(dirPath);
        Ln.d("openFile() : freeSpace : %dbytes", freeSpace);
        if(freeSpace <= FREE_SPACE_LIMIT) {
            return false;
        }

        try {
//            currentFile = new RandomAccessFile(dirPath + File.separator + fileName, "rw");
            currentFile = new File(dirPath, fileName);
            fileChannel = new FileOutputStream(currentFile, true).getChannel();
        } catch (Exception e) {
            Ln.e(e);
            NIOHelper.closeChannel(fileChannel);
            currentFile = null;
            return false;
        }
        Ln.d("openFile() : currentFile : %s", currentFile);

        return true;
    }

    public boolean addHeader() {
        if(currentFile == null)
            return false;

        // adds header
        if(!wrtieStringToFile(getHeader(currentFile.getName()))) {
            return false;
        }

        return true;
    }

    public String getAbsolutePath() {
        if(currentFile == null)
            return null;
        else
            return currentFile.getAbsolutePath();
    }

    protected boolean wrtieStringToFile(String text) {
        byteBuffer.clear();
        byteBuffer.put(text.getBytes());
        byteBuffer.flip();
        while(byteBuffer.hasRemaining()) {
            try {
                fileChannel.write(byteBuffer);
            } catch (Exception e) {
                Ln.e(e);
                // closes the fileChannel
                NIOHelper.closeChannel(fileChannel);
                currentFile = null;
                return false;
            }
        }

        return true;
    }

    protected String getHeader(String name) {
        StringBuilder builder = new StringBuilder();
        builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n");
        builder.append("<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"JayJayLAB\" version=\"1.1\" ");
        builder.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
        builder.append("xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n");
        builder.append("\t<trk>\n");
        builder.append("\t\t<name>" + name);
        builder.append("</name>\n\t\t<desc>(null)</desc>\n\t\t<trkseg>\n");

        return builder.toString();
    }

    protected String getFooter() {
        return "\t\t</trkseg>\n\t</trk>\n</gpx>";
    }

    public String snapshot() {
        Ln.d("snapshop()");

        StringBuilder builder = new StringBuilder();
        builder.append("currentFile : " + currentFile);

        return builder.toString();
    }

    public boolean isFileOpened() {
        Ln.d("isFileOpened()");
        if(currentFile == null)
            return false;

        if(!(currentFile.isFile() && currentFile.exists()))
            return false;

        return true;
    }

    public boolean isFileClosed() {
        Ln.d("isFileClosed()");

        if(currentFile == null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isWriting() {
        Ln.d("isWriing()");

        if(currentFile == null) {
            return false;
        } else {
            return true;
        }
    }

    public void closeFile() {
        Ln.d("closeFile()");

        if(currentFile == null)
            return;

        // adds footer
        wrtieStringToFile(getFooter());

        currentFile = null;
        NIOHelper.closeChannel(fileChannel);
    }

    public void write(Location location) {
        Ln.d("write() : location : %s", location);
        StringBuilder builder = new StringBuilder();
        builder.append("\t\t\t<trkpt lat=\"" + location.getLatitude() + "\" lon=\"" +
                location.getLongitude() + "\">\n");
        builder.append("\t\t\t\t<ele>" + location.getAltitude() + "</ele>\n");
        builder.append("\t\t\t\t<time>" + new DateTime(location.getTime()).toString() + "</time>\n");
        builder.append("\t\t\t</trkpt>\n");

        wrtieStringToFile(builder.toString());
    }
}




Java Source Code List

com.jayjaylab.androiddemo.ActivityIntroTest.java
com.jayjaylab.androiddemo.Application.java
com.jayjaylab.androiddemo.DaoMaster.java
com.jayjaylab.androiddemo.DaoSession.java
com.jayjaylab.androiddemo.PathDao.java
com.jayjaylab.androiddemo.Path.java
com.jayjaylab.androiddemo.app.greyhound.activity.ActivityMain.java
com.jayjaylab.androiddemo.app.greyhound.activity.ActivityMap.java
com.jayjaylab.androiddemo.app.greyhound.adapter.AdapterPathHistory.java
com.jayjaylab.androiddemo.app.greyhound.event.OnGPXParsingCompleteEvent.java
com.jayjaylab.androiddemo.app.greyhound.event.OnReceiveResultEvent.java
com.jayjaylab.androiddemo.app.greyhound.fragment.FragmentPathHistory.java
com.jayjaylab.androiddemo.app.greyhound.model.Path.java
com.jayjaylab.androiddemo.app.greyhound.service.ServiceRecordingPath.java
com.jayjaylab.androiddemo.app.greyhound.util.Constants.java
com.jayjaylab.androiddemo.app.greyhound.util.GPXParser.java
com.jayjaylab.androiddemo.app.greyhound.util.GPXWriter.java
com.jayjaylab.androiddemo.app.greyhound.util.MyResultReceiver.java
com.jayjaylab.androiddemo.app.greyhound.util.PreferenceHelper.java
com.jayjaylab.androiddemo.dialog.ProgressDialog.java
com.jayjaylab.androiddemo.event.OnClickEvent.java
com.jayjaylab.androiddemo.event.OnLongClickEvent.java
com.jayjaylab.androiddemo.event.ProgressBarEvent.java
com.jayjaylab.androiddemo.main.activity.ActivityBase.java
com.jayjaylab.androiddemo.main.activity.ActivityIntro.java
com.jayjaylab.androiddemo.main.activity.ActivityMain.java
com.jayjaylab.androiddemo.main.adapter.AdapterMain.java
com.jayjaylab.androiddemo.main.model.App.java
com.jayjaylab.androiddemo.util.AndroidHelper.java
com.jayjaylab.androiddemo.util.NIOHelper.java
com.jayjaylab.androiddemo.view.BubbleViewMaker.java
com.jayjaylab.androiddemo.view.CardView.java
com.jayjaylab.androiddemo.view.ImageViewThreadPool.java
com.jayjaylab.androiddemo.view.ViewThreadPool.java
de.greenrobot.daogenerator.gentest.ExampleDaoGenerator.java