Commit 8d6670dc authored by Vitaliy Vashchenko's avatar Vitaliy Vashchenko

#612 Added Android camera intent call.

parent b4b4a4a5
......@@ -21,8 +21,6 @@ import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Build;
......@@ -34,11 +32,9 @@ import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import java.io.File;
import java.io.FileOutputStream;
import cz.nic.tablexia.Tablexia;
import cz.nic.tablexia.android.camera.AndroidCamera2QRCodeScanner;
import cz.nic.tablexia.android.camera.AndroidCameraOpener;
import cz.nic.tablexia.debug.BuildConfig;
import cz.nic.tablexia.debug.R;
import cz.nic.tablexia.loader.application.ApplicationAvatarManager;
......@@ -54,13 +50,15 @@ public class AndroidLauncher extends AndroidApplication {
private static final int MULTI_SAMPLING_2X = 2;
private static final boolean HAS_SOFT_BACK_BUTTON = false;
private Tablexia tablexia;
private AndroidGalleryOpener opener;
private Tablexia tablexia;
private AndroidGalleryOpener galleryOpener;
private AndroidCameraOpener cameraOpener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.opener = new AndroidGalleryOpener(this);
this.galleryOpener = new AndroidGalleryOpener(this);
this.cameraOpener = new AndroidCameraOpener(this);
// workaround for launching app from market or installation dialog
if (!isTaskRoot()) {
......@@ -81,7 +79,8 @@ public class AndroidLauncher extends AndroidApplication {
getResources().getConfiguration().locale,
SQL_CONNECTION_TYPE,
new AndroidConnectionManager(getContext()),
opener,
galleryOpener,
cameraOpener,
qrCodeScanner,
getResources().getString(R.string.sentry_dsn),
HAS_SOFT_BACK_BUTTON,
......@@ -99,10 +98,20 @@ public class AndroidLauncher extends AndroidApplication {
//Creates pixmap in correct resolution. Check ApplicationAvatarManager.AVATAR_WIDTH and AVATAR_HEIGHT for more info.
Pixmap resultPixmap = ApplicationAvatarManager.getInstance().createAvatarPixmap(new FileHandle(imagePath));
opener.setPixmap(resultPixmap, true);
galleryOpener.setPixmap(resultPixmap, true);
}
else {
galleryOpener.setPixmap(null, false);
}
}else if (requestCode == AndroidCameraOpener.TAKE_PHOTO_CODE){
if(resultCode == RESULT_OK) {
String imagePath = getPath(cameraOpener.getPhotoPath());
Pixmap resultPixmap = ApplicationAvatarManager.getInstance().createAvatarPixmap(new FileHandle(imagePath));
cameraOpener.deleteTemporaryFile();
cameraOpener.onPhotoTaken(resultPixmap, true);
}
else {
opener.setPixmap(null, false);
cameraOpener.onPhotoTaken(null, false);
}
}
}
......
/*
* Copyright (C) 2017 CZ.NIC, z.s.p.o. (http://www.nic.cz/)
*
* 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/>.
*/
package cz.nic.tablexia.android.camera;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.MediaStore;
import java.io.File;
import java.io.IOException;
import cz.nic.tablexia.android.AndroidLauncher;
import cz.nic.tablexia.util.CameraOpener;
import cz.nic.tablexia.util.Log;
public class AndroidCameraOpener extends CameraOpener {
public static final int TAKE_PHOTO_CODE = 80085;
AndroidLauncher androidLauncher;
Uri photoPath;
private static final String AVATAR_FILE_NAME_PREFIX = "avatar";
private static final String AVATAR_FILE_NAME_SUFFIX = null;
public AndroidCameraOpener(AndroidLauncher androidLauncher) {
this.androidLauncher = androidLauncher;
}
public Uri getPhotoPath() {
return photoPath;
}
@Override
public void onCameraOpen() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(androidLauncher.getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = File.createTempFile(AVATAR_FILE_NAME_PREFIX, AVATAR_FILE_NAME_SUFFIX, androidLauncher.getFilesDir());
} catch (IOException ex) {
Log.err(AndroidCameraOpener.class, "Cant create temporary file to save avatar.");
}
if (photoFile != null) {
photoPath = Uri.fromFile(photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);
androidLauncher.startActivityForResult(takePictureIntent, TAKE_PHOTO_CODE);
}
}
}
@Override
public void onCameraClose() {
}
@Override
public boolean cameraAccessible() {
return androidLauncher.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
}
@Override
public void deleteTemporaryFile() {
if (photoPath != null){
File temp = new File(photoPath.getPath());
if (temp.exists()) temp.delete();
photoPath = null;
}
}
}
......@@ -48,6 +48,7 @@ import cz.nic.tablexia.menu.MenuController;
import cz.nic.tablexia.model.DatabaseManager;
import cz.nic.tablexia.screen.AbstractTablexiaScreen;
import cz.nic.tablexia.screen.loader.IConnectionManager;
import cz.nic.tablexia.util.CameraOpener;
import cz.nic.tablexia.util.GalleryOpener;
import cz.nic.tablexia.util.Log;
import cz.nic.tablexia.util.Utility;
......@@ -93,6 +94,7 @@ public class Tablexia extends TablexiaApplication {
private static IConnectionManager connectionManager;
private static GalleryOpener galleryOpener;
private static CameraOpener cameraOpener;
private static QRCodeScanner qrCodeScanner;
private final SQLConnectionType sqlConnectionType;
......@@ -127,12 +129,13 @@ public class Tablexia extends TablexiaApplication {
}
}
public Tablexia(String buildType, Locale systemLocale, SQLConnectionType sqlConnectionType, IConnectionManager connManager, GalleryOpener galleryOpener, QRCodeScanner qrCodeScanner, String sentryDSN, boolean hasSoftBackButton, boolean reset, String hwSerial) {
public Tablexia(String buildType, Locale systemLocale, SQLConnectionType sqlConnectionType, IConnectionManager connManager, GalleryOpener galleryOpener, CameraOpener cameraOpener, QRCodeScanner qrCodeScanner, String sentryDSN, boolean hasSoftBackButton, boolean reset, String hwSerial) {
this.reset = reset;
this.connectionManager = validateConnectionManager(connManager);
this.qrCodeScanner = qrCodeScanner;
this.sqlConnectionType = sqlConnectionType;
this.galleryOpener = galleryOpener;
this.cameraOpener = cameraOpener;
TablexiaSettings.init(buildType, systemLocale, hasSoftBackButton, hwSerial);
......@@ -471,6 +474,10 @@ public class Tablexia extends TablexiaApplication {
return galleryOpener;
}
public static CameraOpener getCameraOpener() {
return cameraOpener;
}
public static boolean hasQRCodeScanner() {
return qrCodeScanner != null;
}
......
......@@ -75,7 +75,7 @@ import cz.nic.tablexia.screen.createuser.form.FormActorsLayout;
import cz.nic.tablexia.screen.createuser.form.SignatureInputPane;
import cz.nic.tablexia.screen.createuser.form.SignaturePane;
import cz.nic.tablexia.shared.model.User;
import cz.nic.tablexia.util.GalleryOpener;
import cz.nic.tablexia.util.CameraOpener;
import cz.nic.tablexia.shared.model.definitions.GenderDefinition;
import cz.nic.tablexia.util.Log;
import cz.nic.tablexia.util.Point;
......@@ -576,17 +576,16 @@ public class FormScreen extends AbstractTablexiaScreen<Void> {
public void clicked(InputEvent event, float x, float y) {
event.stop();
choosingCustomAvatar = true;
//Adds listener, which is waiting for user to choose an image...
Tablexia.getGalleryOpener().setImageChosenListener(new GalleryOpener.ImageChosenListener() {
Tablexia.getCameraOpener().setOnPhotoTakenListener(new CameraOpener.OnPhotoTakenListener() {
@Override
public void onImageChosen(final Pixmap pixmap, boolean success) {
if(!success) {
public void onPhotoTaken(final Pixmap pixmap, boolean success) {
if (!success) {
choosingCustomAvatar = false;
return;
}
if(choosingCustomAvatar) {
if (choosingCustomAvatar) {
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
......@@ -597,9 +596,8 @@ public class FormScreen extends AbstractTablexiaScreen<Void> {
}
}
});
//Requests GalleryOpener to open image picker... Calls listener from above when finished
Tablexia.getGalleryOpener().openGallery();
Tablexia.getCameraOpener().openCamera();
}
});
}
......
/*
* Copyright (C) 2017 CZ.NIC, z.s.p.o. (http://www.nic.cz/)
*
* 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/>.
*/
package cz.nic.tablexia.util;
import com.badlogic.gdx.graphics.Pixmap;
public abstract class CameraOpener {
OnPhotoTakenListener onPhotoTakenListener;
public interface OnPhotoTakenListener{
void onPhotoTaken(Pixmap pixmap, boolean success);
}
public void openCamera(){
onCameraOpen();
}
public void closeCamera(){
onCameraClose();
}
public void setOnPhotoTakenListener(OnPhotoTakenListener onPhotoTakenListener) {
this.onPhotoTakenListener = onPhotoTakenListener;
}
abstract public void onCameraOpen();
abstract public void onCameraClose();
abstract public boolean cameraAccessible();
abstract public void deleteTemporaryFile();
public void onPhotoTaken(Pixmap pixmap, boolean success){
if (onPhotoTakenListener != null){
onPhotoTakenListener.onPhotoTaken(pixmap, success);
}
}
}
/*
* Copyright (C) 2017 CZ.NIC, z.s.p.o. (http://www.nic.cz/)
*
* 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/>.
*/
package cz.nic.tablexia.desktop;
import cz.nic.tablexia.util.CameraOpener;
public class DesktopCameraOpener extends CameraOpener {
@Override
public void onCameraOpen() {
}
@Override
public void onCameraClose() {
}
@Override
public boolean cameraAccessible() {
return false;
}
@Override
public void deleteTemporaryFile() {
}
}
......@@ -85,7 +85,7 @@ public class DesktopLauncher {
config.addIcon(DESKTOP_ICON_128, Files.FileType.Internal);
}
new LwjglApplication(new Tablexia(buildType, Locale.getDefault(), SQL_CONNECTION_TYPE, new DesktopConnectionManager(), new DesktopGalleryOpener(), null, null, HAS_SOFT_BACK_BUTTON, true, null), config);
new LwjglApplication(new Tablexia(buildType, Locale.getDefault(), SQL_CONNECTION_TYPE, new DesktopConnectionManager(), new DesktopGalleryOpener(), new DesktopCameraOpener(), null, null, HAS_SOFT_BACK_BUTTON, true, null), config);
}
private static String loadAttributeFromManifest(String attributeName, String defaultValue) {
......
/*
* Copyright (C) 2017 CZ.NIC, z.s.p.o. (http://www.nic.cz/)
*
* 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/>.
*/
package cz.nic.tablexia;
import cz.nic.tablexia.util.CameraOpener;
public class IOSCameraOpener extends CameraOpener{
@Override
public void onCameraOpen() {
// TODO: 17.1.17 add iOS implementation
}
@Override
public void onCameraClose() {
}
@Override
public boolean cameraAccessible() {
return true;
}
@Override
public void deleteTemporaryFile() {
}
}
......@@ -95,6 +95,7 @@ public class IOSLauncher extends IOSApplication.Delegate {
SQL_CONNECTION_TYPE,
new IOSConnectionManager(),
new IOSGalleryOpener(),
new IOSCameraOpener(),
new IOSQRCodeScanner(),
sentryDSN,
HAS_SOFT_BACK_BUTTON,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment