Skip to content

Commit

Permalink
[TIMOB-26025] TiAPI: Improve Ti.Filesystem.File parity (#10019)
Browse files Browse the repository at this point in the history
* Add Ti.Filesystem.File#copy() method on iOS. Default Ti.filesystem.File#createDirectory to be recursive like other platforms (but allow passing false as arg to disable). Fix Android returning true for Ti.Filesystem.File#isDirectory() on non-existent Resource sub-dirs.

* Don't only run the filesystem tests. Update docs.

* Don't run retirejs thoruhg grunt. We already do it separately for Jenkins

* Fix invalid doc changes

* Fix Ti.Filesystem.File #createTimestamp and #modificationTimestamp to return Numbers not Dates. Deprecate the property versions. Incorporate some of Hans' updates.

* Stela more of Hans' work on handling various errors that could happen under the hood with these APIs

* Don't skip Ti.Filesystem.File.parent test on iOS

* Add Ti.Filesystem.File #createdAt() and #modifiedAt() methods that return Dates. Deprecate #modificationTimestamp() and #createTimestamp() due to inconsistent return types across platforms. Update unit tests to check new createdAt/modifiedAt methods. Add docs for new methods/deprecations. Use longs in place of doubles in Android file APIs whne it's more appropriate. Support getting creation timestamp for files on Android when API 26+. Fix spaceAvailable to use newer more accurate APIs on API level 18+.

* Add deprecation log for Ti.Filesystem.File #createTimestamp() and #modificationTimestamp() on Android.

* Add Ti.Filesystem.File#append() method on Android, update docs to show it wasn't there before.
  • Loading branch information
sgtcoolguy committed May 11, 2018
1 parent b0f4785 commit 4e4abc8
Show file tree
Hide file tree
Showing 8 changed files with 933 additions and 168 deletions.
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

0 comments on commit 4e4abc8

Please sign in to comment.