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

New Open File and File Chooser service #283

Open
wants to merge 1 commit into
base: desktop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/file-chooser/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies {
implementation project(":util")
}

ext.moduleName = 'com.gluonhq.attach.filechooser'
ext.description = 'Common API to access file features'
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (c) 2016, 2019 Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.gluonhq.attach.filechooser;

import java.io.File;
import java.util.Optional;

import com.gluonhq.attach.util.Services;

/**
* The picture service allows the developer to load a picture from the device's local file
* system or from a picture taken directly using the device's camera.
*
* <p><b>Example</b></p>
* <pre>
* {@code ImageView imageView = new ImageView();
* PicturesService.create().ifPresent(service -> {
* service.takePhoto(false).ifPresent(image -> imageView.setImage(image));
* });}</pre>
*
* <p>It also allows the developer to retrieve the original file and work with it
* as needed, for instance sharing it with the ShareService.</p>
*
* <p><b>Example</b></p>
* <pre>
* {@code ImageView imageView = new ImageView();
* PicturesService.create().ifPresent(service -> {
* service.loadImageFromGallery().ifPresent(image -> imageView.setImage(image));
* service.getImageFile().ifPresent(file ->
* ShareService.create().ifPresent(share ->
* share.share("image/jpeg", file)));
* });}</pre>
*
* <p><b>Android Configuration</b></p>
*
* <p>Create the file {@code /src/android/res/xml/file_provider_paths.xml} with
* the following content that allows access to the external storage:</p>
* <pre>
* {@code
* <?xml version="1.0" encoding="utf-8"?>
* <paths>
* <external-path name="external_files" path="." />
* </paths>
* }
* </pre>
*
* <p>The permission <code>android.permission.CAMERA</code> needs to be added as well as the permissions
* <code>android.permission.READ_EXTERNAL_STORAGE</code> and <code>android.permission.WRITE_EXTERNAL_STORAGE</code>
* to be able to read and write images. Also a {@code provider} is required:</p>
*
* Note: these modifications are handled automatically by <a href="https://docs.gluonhq.com/client">Client plugin</a> if it is used.
* <pre>
* {@code <manifest package="${application.package.name}" ...>
* <uses-permission android:name="android.permission.CAMERA"/>
* <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
* <application ...>
* ...
* <activity android:name="com.gluonhq.attach.android.PermissionRequestActivity" />
* <provider
* android:name="android.support.v4.content.FileProvider"
* android:authorities="${application.package.name}.fileprovider"
* android:exported="false"
* android:grantUriPermissions="true">
* <meta-data
* android:name="android.support.FILE_PROVIDER_PATHS"
* android:resource="@xml/file_provider_paths" />
* </provider>
* </application>
* </manifest>}
* </pre>
*
*
* <p><b>iOS Configuration</b></p>
* <p>The following keys are required:</p>
* <pre>
* {@code <key>NSCameraUsageDescription</key>
* <string>Reason to use Camera Service (iOS 10+)</string>
* <key>NSPhotoLibraryUsageDescription</key>
* <string>Reason to use Photo Library (iOS 10+)</string>
* <key>NSPhotoLibraryAddUsageDescription</key>
* <string>Reason to use Photo Library (iOS 10+)</string>}</pre>
*
* @since 3.0.0
*/
public interface FileChooserService {

/**
* Returns an instance of {@link FileChooserService}.
* @return An instance of {@link FileChooserService}.
*/
static Optional<FileChooserService> create() {
return Services.get(FileChooserService.class);
}

/**
* Retrieve an image from the device's gallery of images
* @return an Optional with the Image or empty if it failed or it was cancelled
*/
Optional<File> loadFile();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2016, 2020, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.gluonhq.attach.filechooser.impl;

import com.gluonhq.attach.filechooser.FileChooserService;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;

import java.io.File;
import java.util.Optional;
import java.util.logging.Logger;

/**
* <p>Create the file {@code /src/android/res/xml/file_provider_paths.xml} with
* the following content that allows access to the external storage or to
* a temporal cache in case the file is not saved:</p>
*
* <pre>
* {@code
* <?xml version="1.0" encoding="utf-8"?>
* <paths>
* <external-path name="external_files" path="." />
* <external-cache-path name="external_cache_files" path="." />
* </paths>
* }
* </pre>
*
* <p>The permission <code>android.permission.CAMERA</code> needs to be added as well as the permissions
* <code>android.permission.READ_EXTERNAL_STORAGE</code> and <code>android.permission.WRITE_EXTERNAL_STORAGE</code>
* to be able to read and write images. Also a {@code provider} is required:</p>
* <pre>
* {@code <manifest package="${application.package.name}" ...>
* <uses-permission android:name="android.permission.CAMERA"/>
* <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
* <application ...>
* ...
* <activity android:name="com.gluonhq.helloandroid.PermissionRequestActivity" />
* <provider
* android:name="com.gluonhq.helloandroid.FileProvider"
* android:authorities="${application.package.name}.fileprovider"
* android:exported="false"
* android:grantUriPermissions="true">
* <meta-data
* android:name="android.support.FILE_PROVIDER_PATHS"
* android:resource="@xml/file_provider_paths" />
* </provider>
* </application>
* </manifest>}
* </pre>
*/
public class AndroidFileChooserService implements FileChooserService {

private static final Logger LOG = Logger.getLogger(AndroidFileChooserService.class.getName());

static {
System.loadLibrary("filechooser");
}

private static final ObjectProperty<File> selectedFile = new SimpleObjectProperty<>();
private static ObjectProperty<File> result;


public AndroidFileChooserService() {
}

@Override
public Optional<File> loadFile() {
LOG.severe("Load image from gallery has been called.");
result = new SimpleObjectProperty<>();
selectFile();
try {
Platform.enterNestedEventLoop(result);
} catch (Exception e) {
LOG.severe("GalleryActivity: enterNestedEventLoop failed: " + e);
}

return Optional.ofNullable(result.get());

}

// native
public static native void selectFile();

// callback
public static void setResult(String filePath, int rotate) {
File file = new File(filePath);
selectedFile.set(file);
if (selectedFile.get() == null) {
result.set(selectedFile.get());
}
final File finalImage = selectedFile.get();
LOG.severe("Final File:");
LOG.severe(finalImage.getAbsolutePath());
Platform.runLater(() -> {
if (finalImage != null) {
result.set(selectedFile.get());
}
try {
Platform.exitNestedEventLoop(result, null);
} catch (Exception e) {
LOG.severe("GalleryActivity: exitNestedEventLoop failed: " + e);
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2019, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.gluonhq.attach.filechooser.impl;

import com.gluonhq.attach.filechooser.FileChooserService;

public abstract class DummyFileChooserService implements FileChooserService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2016, 2019, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.gluonhq.attach.filechooser.impl;

import com.gluonhq.attach.filechooser.FileChooserService;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.image.Image;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Base64;
import java.util.Optional;

/**
* Note:Since iOS 10 requires {@code NSCameraUsageDescription},
* {@code NSPhotoLibraryUsageDescription} and
* {@code NSPhotoLibraryAddUsageDescription} in pList.
*/
public class IOSFileChooserService implements FileChooserService {

static {
System.loadLibrary("FileChooser");
initFiles();
}

private static final ObjectProperty<File> selectedFile = new SimpleObjectProperty<>();
private static ObjectProperty<File> result;

@Override
public Optional<File> loadFile() {
result = new SimpleObjectProperty<>();
selectFile();
try {
Platform.enterNestedEventLoop(result);
} catch (Exception e) {
System.out.println("GalleryActivity: enterNestedEventLoop failed: " + e);
}
return Optional.ofNullable(result.get());
}

// native
private static native void initFiles(); // init IDs for java callbacks from native
public static native void selectFile();

// callback
// Still needs some changes/adaptions to work for all files instead of just images.
public static void setResult(String v, String filePath) {
if (v != null && !v.isEmpty()) {
try {
byte[] fileBytes = Base64.getDecoder().decode(v.replaceAll("\\s+", "").getBytes());
selectedFile.set(new File(filePath));
Image temp = new Image(new ByteArrayInputStream(fileBytes));
result.set(new File(temp.getUrl()));
} catch (Exception ex) {
System.err.println("Error setResult: " + ex);
}
}
Platform.runLater(() -> {
try {
Platform.exitNestedEventLoop(result, null);
} catch (Exception e) {
System.out.println("GalleryActivity: exitNestedEventLoop failed: " + e);
}
});
}
}
Loading