diff options
| author | 2019-08-20 15:21:03 +0000 | |
|---|---|---|
| committer | 2019-08-20 15:21:03 +0000 | |
| commit | bb021e3b1be613f44c1ae056a3ca8bdea5c2bcfe (patch) | |
| tree | d12c7547ef0cca6f946969ec29bfd38b924fca62 | |
| parent | 95dbff5e7817b1b7e36c7d518b4818be5c23dc32 (diff) | |
| parent | 69bbb72be807f42eb3d9a51e3136cc2ab68708a0 (diff) | |
Merge "Add API to create GestureLibrary from FileDescriptor"
| -rw-r--r-- | api/current.txt | 1 | ||||
| -rw-r--r-- | core/java/android/gesture/GestureLibraries.java | 85 |
2 files changed, 61 insertions, 25 deletions
diff --git a/api/current.txt b/api/current.txt index 593710314a0f..a0d272189000 100644 --- a/api/current.txt +++ b/api/current.txt @@ -13460,6 +13460,7 @@ package android.gesture { public final class GestureLibraries { method public static android.gesture.GestureLibrary fromFile(String); method public static android.gesture.GestureLibrary fromFile(java.io.File); + method @NonNull public static android.gesture.GestureLibrary fromFileDescriptor(@NonNull android.os.ParcelFileDescriptor); method public static android.gesture.GestureLibrary fromPrivateFile(android.content.Context, String); method public static android.gesture.GestureLibrary fromRawResource(android.content.Context, @RawRes int); } diff --git a/core/java/android/gesture/GestureLibraries.java b/core/java/android/gesture/GestureLibraries.java index 611d9abb3b9f..5e31ce63d7c9 100644 --- a/core/java/android/gesture/GestureLibraries.java +++ b/core/java/android/gesture/GestureLibraries.java @@ -16,14 +16,16 @@ package android.gesture; +import android.annotation.NonNull; import android.annotation.RawRes; +import android.os.ParcelFileDescriptor; import android.util.Log; import static android.gesture.GestureConstants.*; import android.content.Context; import java.io.File; +import java.io.FileDescriptor; import java.io.FileOutputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.FileInputStream; import java.io.InputStream; @@ -41,6 +43,11 @@ public final class GestureLibraries { return new FileGestureLibrary(path); } + @NonNull + public static GestureLibrary fromFileDescriptor(@NonNull ParcelFileDescriptor pfd) { + return new FileGestureLibrary(pfd.getFileDescriptor()); + } + public static GestureLibrary fromPrivateFile(Context context, String name) { return fromFile(context.getFileStreamPath(name)); } @@ -50,55 +57,83 @@ public final class GestureLibraries { } private static class FileGestureLibrary extends GestureLibrary { + // Either a file or an fd is used private final File mPath; + private final FileDescriptor mFd; public FileGestureLibrary(File path) { mPath = path; + mFd = null; } + public FileGestureLibrary(FileDescriptor fd) { + mPath = null; + mFd = fd; + } + + /** + * <p>If this GestureLibrary was created using a FileDescriptor, + * this method will always return false. + */ @Override public boolean isReadOnly() { - return !mPath.canWrite(); + if (mPath != null) { + return !mPath.canWrite(); + } + return false; } public boolean save() { if (!mStore.hasChanged()) return true; + boolean result = false; - final File file = mPath; + if (mPath != null) { + final File file = mPath; - final File parentFile = file.getParentFile(); - if (!parentFile.exists()) { - if (!parentFile.mkdirs()) { - return false; + final File parentFile = file.getParentFile(); + if (!parentFile.exists()) { + if (!parentFile.mkdirs()) { + return false; + } } - } - boolean result = false; - try { - //noinspection ResultOfMethodCallIgnored - file.createNewFile(); - mStore.save(new FileOutputStream(file), true); - result = true; - } catch (FileNotFoundException e) { - Log.d(LOG_TAG, "Could not save the gesture library in " + mPath, e); - } catch (IOException e) { - Log.d(LOG_TAG, "Could not save the gesture library in " + mPath, e); + try { + //noinspection ResultOfMethodCallIgnored + file.createNewFile(); + mStore.save(new FileOutputStream(file), true); + result = true; + } catch (IOException e) { + Log.d(LOG_TAG, "Could not save the gesture library in " + mPath, e); + } + } else { + try { + mStore.save(new FileOutputStream(mFd), true); + result = true; + } catch (IOException e) { + Log.d(LOG_TAG, "Could not save the gesture library", e); + } } - return result; } public boolean load() { boolean result = false; - final File file = mPath; - if (file.exists() && file.canRead()) { + if (mPath != null) { + final File file = mPath; + if (file.exists() && file.canRead()) { + try { + mStore.load(new FileInputStream(file), true); + result = true; + } catch (IOException e) { + Log.d(LOG_TAG, "Could not load the gesture library from " + mPath, e); + } + } + } else { try { - mStore.load(new FileInputStream(file), true); + mStore.load(new FileInputStream(mFd), true); result = true; - } catch (FileNotFoundException e) { - Log.d(LOG_TAG, "Could not load the gesture library from " + mPath, e); } catch (IOException e) { - Log.d(LOG_TAG, "Could not load the gesture library from " + mPath, e); + Log.d(LOG_TAG, "Could not load the gesture library", e); } } |