Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-26025] TiAPI: Improve Ti.Filesystem.File parity #10019

Merged
merged 12 commits into from
May 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2011-2012 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2011-2018 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand All @@ -11,6 +11,7 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -303,14 +304,14 @@ public String resolve()
// clang-format off
@Kroll.method
@Kroll.getProperty
public double getSize()
public long getSize()
// clang-format on
{
return tbf.size();
}

@Kroll.method
public double spaceAvailable()
public long spaceAvailable()
{
return tbf.spaceAvailable();
}
Expand Down Expand Up @@ -354,17 +355,35 @@ public void writeLine(String data) throws IOException
}

@Kroll.method
public double createTimestamp()
public long createTimestamp()
{
Log.w(
TAG,
"createTimestamp() has been deprecated in 7.2.0 in favor of createdAt() to avoid platform-differences for return type between iOS and Android. createdAt() will return a Date object on all platforms.");
return tbf.createTimestamp();
}

@Kroll.method
public double modificationTimestamp()
public long modificationTimestamp()
{
Log.w(
TAG,
"modificationTimestamp() has been deprecated in 7.2.0 in favor of modifiedAt() to avoid platform-differences for return type between iOS and Android. modifiedAt() will return a Date object on all platforms.");
return tbf.modificationTimestamp();
}

@Kroll.method
public Date createdAt()
{
return tbf.createdAt();
}

@Kroll.method
public Date modifiedAt()
{
return tbf.modifiedAt();
}

@Kroll.method
public FileStreamProxy open(int mode) throws IOException
{
Expand All @@ -374,6 +393,19 @@ public FileStreamProxy open(int mode) throws IOException
return new FileStreamProxy(this);
}

@Kroll.method
public boolean append(Object[] args) throws IOException
{
if (args == null || args.length == 0) {
return false;
}
// delegate to #write()
Object[] newArgs = new Object[2];
newArgs[0] = args[0];
newArgs[1] = Boolean.TRUE;
return write(newArgs);
}

public InputStream getInputStream() throws IOException
{
return getBaseFile().getInputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Date;
import java.util.List;

import org.appcelerator.kroll.common.Log;
Expand Down Expand Up @@ -215,10 +216,15 @@ public boolean createShortcut()
return false;
}

public double createTimestamp()
public long createTimestamp()
{
logNotSupported("createTimestamp");
return 0;
return 0L;
}

public Date createdAt()
{
return new Date(createTimestamp());
}

public boolean deleteDirectory(boolean recursive)
Expand Down Expand Up @@ -269,10 +275,15 @@ public TiBaseFile getParent()
return null;
}

public double modificationTimestamp()
public long modificationTimestamp()
{
logNotSupported("modificationTimestamp");
return 0;
return 0L;
}

public Date modifiedAt()
{
return new Date(modificationTimestamp());
}

public boolean move(String destination) throws IOException
Expand Down Expand Up @@ -380,13 +391,13 @@ public boolean setWriteable()
public long size()
{
logNotSupported("size");
return 0;
return 0L;
}

public double spaceAvailable()
public long spaceAvailable()
{
logNotSupported("spaceAvailable");
return 0;
return 0L;
}

public void unzip(String destination)
Expand Down
28 changes: 21 additions & 7 deletions android/titanium/src/java/org/appcelerator/titanium/io/TiFile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2018 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand All @@ -20,17 +20,20 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiBlob;

import android.net.Uri;
import android.os.Build;
import android.os.StatFs;

/**
* An extension of {@link TiBaseFile}, used for representing a file on the device's true file system.
* An extension of {@link TiBaseFile}, used for representing a file on the device's true file system.
* This differentiates it from TiResourceFile, which represents a file inside the application's resource bundle.
*/
public class TiFile extends TiBaseFile
Expand Down Expand Up @@ -193,13 +196,21 @@ public boolean exists()
}

@Override
public double createTimestamp()
public long createTimestamp()
{
return file.lastModified();
if (Build.VERSION.SDK_INT >= 26) {
try {
BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
return attr.creationTime().toMillis();
} catch (Throwable t) {
// ignore, fall back to modification timestamp
}
}
return modificationTimestamp();
}

@Override
public double modificationTimestamp()
public long modificationTimestamp()
{
return file.lastModified();
}
Expand Down Expand Up @@ -246,10 +257,13 @@ public long size()

@SuppressWarnings("deprecation")
@Override
public double spaceAvailable()
public long spaceAvailable()
{
StatFs stat = new StatFs(file.getPath());
return (double) stat.getAvailableBlocks() * (double) stat.getBlockSize();
if (Build.VERSION.SDK_INT >= 18) {
return stat.getAvailableBytes();
}
return (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
}

/**
Expand Down