Skip to content

Commit

Permalink
[timob-23494] Android: Support #createFile() method on Ti.Filesystem.…
Browse files Browse the repository at this point in the history
…File (#8514)

* exposed createFile method and updated test file to NOT skip createFile && deleteFile

* internal storage rather than external storage
  • Loading branch information
Frankie authored and sgtcoolguy committed Oct 31, 2016
1 parent 31fef25 commit 5eaa6d2
Show file tree
Hide file tree
Showing 4 changed files with 543 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@
*/
package org.appcelerator.titanium;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.content.ContextWrapper;
import android.os.Environment;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.io.TiFile;
import org.appcelerator.titanium.io.TiFileFactory;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiFileHelper2;
Expand Down Expand Up @@ -72,7 +77,6 @@ public TiFileProxy(String sourceUrl, String[] parts, boolean resolve)
} else {
path = TiFileHelper2.joinSegments(parts);
}

if (resolve) {
path = resolveUrl(scheme, path);
}
Expand Down Expand Up @@ -152,6 +156,15 @@ public boolean createDirectory(@Kroll.argument(optional=true) Object arg)
return tbf.createDirectory(recursive);
}

@Kroll.method
public boolean createFile()
{
Context context = TiApplication.getInstance().getApplicationContext();
ContextWrapper contextWrapper = new ContextWrapper(context);
tbf = new TiFile(new File(contextWrapper.getDir("data", Context.MODE_PRIVATE) + "/" + tbf.getNativeFile().getName()), path, getExecutable());
return tbf.createFile();
}

@Kroll.method
public boolean deleteDirectory(@Kroll.argument(optional=true) Object arg)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ public boolean createDirectory(boolean recursive) {
return false;
}

public boolean createFile() {
logNotSupported("createFile");
return false;
}

public boolean createShortcut() {
logNotSupported("createShortcut");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ public boolean createDirectory(boolean recursive)
}
}

@Override
public boolean createFile()
{
try {
if (!file.getParentFile().exists()) {
file.mkdirs();
}
if (!file.exists()) {
return file.createNewFile();
}
} catch (IOException e) {
Log.e(TAG, "Error creating new file: ", e);
}
return false;
}

private boolean deleteTree(File d) {
boolean deleted = true;

Expand Down Expand Up @@ -159,7 +175,7 @@ public boolean deleteDirectory(boolean recursive) {

return deleted;
}

/**
* Deletes this file.
* @return true if the file was successfully deleted, false otherwise.
Expand Down
Loading

0 comments on commit 5eaa6d2

Please sign in to comment.