use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class BoxLayout method layoutContainer.
/**
* {@inheritDoc}
*/
public void layoutContainer(Container parent) {
Style ps = parent.getStyle();
int width = parent.getLayoutWidth() - parent.getSideGap() - ps.getHorizontalPadding();
int height = parent.getLayoutHeight() - parent.getBottomGap() - ps.getVerticalPadding();
int x = ps.getPaddingLeft(parent.isRTL());
int y = ps.getPaddingTop();
int numOfcomponents = parent.getComponentCount();
boolean rtl = parent.isRTL();
if (rtl) {
x += parent.getSideGap();
}
int initX = x;
for (int i = 0; i < numOfcomponents; i++) {
Component cmp = parent.getComponentAt(i);
Style stl = cmp.getStyle();
switch(axis) {
case Y_AXIS:
case Y_AXIS_BOTTOM_LAST:
int cmpBottom = height;
cmp.setWidth(width - stl.getHorizontalMargins());
int cmpH = cmp.getPreferredH();
y += stl.getMarginTop();
if (y - ps.getPaddingTop() >= cmpBottom && !parent.isScrollableY()) {
cmpH = 0;
} else if (y + cmpH - ps.getPaddingTop() > cmpBottom) {
if (!parent.isScrollableY()) {
cmpH = cmpBottom - y - stl.getMarginBottom();
}
}
cmp.setHeight(cmpH);
cmp.setX(x + stl.getMarginLeft(parent.isRTL()));
cmp.setY(y);
y += cmp.getHeight() + stl.getMarginBottom();
break;
case X_AXIS_NO_GROW:
{
int cmpRight = width;
height = Math.min(getPreferredSize(parent).getHeight(), height);
int cmpW = cmp.getPreferredW();
x += stl.getMarginLeftNoRTL();
if (x >= cmpRight && !parent.isScrollableX()) {
cmpW = 0;
} else {
if (x + cmpW - ps.getPaddingLeftNoRTL() > cmpRight) {
cmpW = cmpRight - x - stl.getMarginRightNoRTL();
}
}
cmp.setWidth(cmpW);
cmp.setHeight(height - stl.getMarginTop() - stl.getMarginBottom());
if (rtl) {
cmp.setX(width + initX - (x - initX) - cmpW);
} else {
cmp.setX(x);
}
cmp.setY(y + stl.getMarginTop());
x += cmp.getWidth() + stl.getMarginRightNoRTL();
break;
}
default:
int cmpRight = width;
int cmpW = cmp.getPreferredW();
x += stl.getMarginLeftNoRTL();
if (x >= cmpRight && !parent.isScrollableX()) {
cmpW = 0;
} else {
if (x + cmpW - ps.getPaddingLeftNoRTL() > cmpRight) {
cmpW = cmpRight - x - stl.getMarginRightNoRTL();
}
}
cmp.setWidth(cmpW);
cmp.setHeight(height - stl.getVerticalMargins());
if (rtl) {
cmp.setX(width + initX - (x - initX) - cmpW);
} else {
cmp.setX(x);
}
cmp.setY(y + stl.getMarginTop());
x += cmp.getWidth() + stl.getMarginRightNoRTL();
break;
}
}
if (axis == Y_AXIS_BOTTOM_LAST && numOfcomponents > 0) {
if (parent instanceof Form) {
parent = ((Form) parent).getContentPane();
}
Component cmp = parent.getComponentAt(numOfcomponents - 1);
if (cmp.getY() + cmp.getHeight() < height) {
cmp.setY(height - cmp.getHeight());
}
}
if (axis == Y_AXIS) {
if (numOfcomponents > 0) {
int containerBottomInner = parent.getLayoutHeight() - parent.getStyle().getPaddingBottom();
Component lastCmp = parent.getComponentAt(numOfcomponents - 1);
int lastCmpBottomOuter = lastCmp.getY() + lastCmp.getHeight() + lastCmp.getStyle().getMarginBottom();
int dy = 0;
switch(align) {
case Component.CENTER:
{
dy = (containerBottomInner - lastCmpBottomOuter) / 2;
break;
}
case Component.BOTTOM:
{
dy = (containerBottomInner - lastCmpBottomOuter);
break;
}
}
if (dy > 0) {
for (int i = 0; i < numOfcomponents; i++) {
Component cmp = parent.getComponentAt(i);
cmp.setY(cmp.getY() + dy);
}
}
}
} else if (axis == X_AXIS) {
if (numOfcomponents > 0) {
if (rtl) {
int containerLeftInner = parent.getStyle().getPaddingLeftNoRTL();
Component lastCmp = parent.getComponentAt(numOfcomponents - 1);
int lastCmpLeftOuter = lastCmp.getX() - lastCmp.getStyle().getMarginLeftNoRTL();
int dx = 0;
switch(align) {
case Component.CENTER:
{
dx = (lastCmpLeftOuter - containerLeftInner) / 2;
break;
}
case Component.RIGHT:
{
dx = (lastCmpLeftOuter - containerLeftInner);
break;
}
}
if (dx > 0) {
for (int i = 0; i < numOfcomponents; i++) {
Component cmp = parent.getComponentAt(i);
cmp.setX(cmp.getX() - dx);
}
}
} else {
int containerRightInner = parent.getLayoutWidth() - parent.getStyle().getPaddingRightNoRTL();
Component lastCmp = parent.getComponentAt(numOfcomponents - 1);
int lastCmpRightOuter = lastCmp.getX() + lastCmp.getWidth() + lastCmp.getStyle().getMarginRightNoRTL();
int dx = 0;
switch(align) {
case Component.CENTER:
{
dx = (containerRightInner - lastCmpRightOuter) / 2;
break;
}
case Component.RIGHT:
{
dx = (containerRightInner - lastCmpRightOuter);
break;
}
}
if (dx > 0) {
for (int i = 0; i < numOfcomponents; i++) {
Component cmp = parent.getComponentAt(i);
cmp.setX(cmp.getX() + dx);
}
}
}
}
}
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class AndroidImplementation method createMediaException.
public static MediaException createMediaException(int extra) {
MediaErrorType type;
String message;
switch(extra) {
case MediaPlayer.MEDIA_ERROR_IO:
type = MediaErrorType.Network;
message = "IO error";
break;
case MediaPlayer.MEDIA_ERROR_MALFORMED:
type = MediaErrorType.Decode;
message = "Media was malformed";
break;
case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
type = MediaErrorType.SrcNotSupported;
message = "Not valie for progressive playback";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
type = MediaErrorType.Network;
message = "Server died";
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
type = MediaErrorType.Network;
message = "Timed out";
break;
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
type = MediaErrorType.Network;
message = "Unknown error";
break;
case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
type = MediaErrorType.SrcNotSupported;
message = "Unsupported media";
break;
default:
type = MediaErrorType.Network;
message = "Unknown error";
}
return new MediaException(type, message);
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class AndroidImplementation method createImage.
@Override
public Object createImage(String path) throws IOException {
int IMAGE_MAX_SIZE = getDisplayHeight();
if (exists(path)) {
Bitmap b = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inPreferredConfig = Bitmap.Config.ARGB_8888;
InputStream fis = createFileInputStream(path);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
if (sampleSizeOverride != -1) {
o2.inSampleSize = sampleSizeOverride;
} else {
String sampleSize = Display.getInstance().getProperty("android.sampleSize", null);
if (sampleSize != null) {
o2.inSampleSize = Integer.parseInt(sampleSize);
} else {
o2.inSampleSize = scale;
}
}
o2.inPurgeable = true;
o2.inInputShareable = true;
fis = createFileInputStream(path);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
// fix rotation
ExifInterface exif = new ExifInterface(removeFilePrefix(path));
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
angle = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
angle = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
angle = 270;
break;
}
if (sampleSizeOverride < 0 && angle != 0) {
Matrix mat = new Matrix();
mat.postRotate(angle);
Bitmap correctBmp = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), mat, true);
b.recycle();
b = correctBmp;
}
} catch (IOException e) {
}
return b;
} else {
InputStream in = this.getResourceAsStream(getClass(), path);
if (in == null) {
throw new IOException("Resource not found. " + path);
}
try {
return this.createImage(in);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception ignored) {
;
}
}
}
}
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class AndroidImplementation method captureVideo.
@Override
public void captureVideo(VideoCaptureConstraints cnst, ActionListener response) {
if (getActivity() == null) {
throw new RuntimeException("Cannot capture video in background mode");
}
if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to take a video")) {
return;
}
if (getRequestedPermissions().contains(Manifest.permission.CAMERA)) {
// See https://github.com/codenameone/CodenameOne/issues/2409#issuecomment-391696058
if (!checkForPermission(Manifest.permission.CAMERA, "This is required to take a video")) {
return;
}
}
callback = new EventDispatcher();
callback.addListener(response);
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
if (cnst != null) {
switch(cnst.getQuality()) {
case VideoCaptureConstraints.QUALITY_LOW:
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
break;
case VideoCaptureConstraints.QUALITY_HIGH:
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
break;
}
if (cnst.getMaxFileSize() > 0) {
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, cnst.getMaxFileSize());
}
if (cnst.getMaxLength() > 0) {
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, cnst.getMaxLength());
}
}
File newFile = getOutputMediaFile(true);
newFile.getParentFile().mkdirs();
newFile.getParentFile().setWritable(true, false);
Uri videoUri = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", newFile);
Storage.getInstance().writeObject("videoUri", newFile.getAbsolutePath());
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, videoUri);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (Build.VERSION.SDK_INT < 21) {
List<ResolveInfo> resInfoList = getContext().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
getContext().grantUriPermission(packageName, videoUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
this.getActivity().startActivityForResult(intent, CAPTURE_VIDEO);
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class CodenameOneView method onTouchEvent.
// private boolean nativePeerGrabbedPointer = false;
public boolean onTouchEvent(MotionEvent event) {
if (this.implementation.getCurrentForm() == null) {
/**
* make sure a form has been set before we can send events to the
* EDT. if we send events before the form has been set we might
* deadlock!
*/
return true;
}
int[] x = null;
int[] y = null;
int size = event.getPointerCount();
if (size > 1) {
x = new int[size];
y = new int[size];
for (int i = 0; i < size; i++) {
x[i] = (int) event.getX(i);
y[i] = (int) event.getY(i);
}
}
/*
if (!cn1GrabbedPointer) {
if (x == null) {
Component componentAt = this.implementation.getCurrentForm().getComponentAt((int)event.getX(), (int)event.getY());
if (componentAt != null && (componentAt instanceof PeerComponent)) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//nativePeerGrabbedPointer = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//nativePeerGrabbedPointer = false;
}
return false;
}
} else {
Component componentAt = this.implementation.getCurrentForm().getComponentAt((int)x[0], (int)y[0]);
if (componentAt != null && (componentAt instanceof PeerComponent)) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
nativePeerGrabbedPointer = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
nativePeerGrabbedPointer = false;
}
return false;
}
}
}
*/
// if (nativePeerGrabbedPointer) {
// return false;
// }
Component componentAt;
try {
if (x == null) {
componentAt = this.implementation.getCurrentForm().getComponentAt((int) event.getX(), (int) event.getY());
} else {
componentAt = this.implementation.getCurrentForm().getComponentAt((int) x[0], (int) y[0]);
}
} catch (Throwable t) {
// Since this is is an EDT violation, we may get an exception
// Just consume it
componentAt = null;
}
boolean isPeer = (componentAt instanceof PeerComponent);
boolean consumeEvent = !isPeer || cn1GrabbedPointer;
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (x == null) {
this.implementation.pointerPressed((int) event.getX(), (int) event.getY());
} else {
this.implementation.pointerPressed(x, y);
}
if (!isPeer)
cn1GrabbedPointer = true;
break;
case MotionEvent.ACTION_UP:
if (x == null) {
this.implementation.pointerReleased((int) event.getX(), (int) event.getY());
} else {
this.implementation.pointerReleased(x, y);
}
cn1GrabbedPointer = false;
break;
case MotionEvent.ACTION_CANCEL:
cn1GrabbedPointer = false;
break;
case MotionEvent.ACTION_MOVE:
if (x == null) {
this.implementation.pointerDragged((int) event.getX(), (int) event.getY());
} else {
this.implementation.pointerDragged(x, y);
}
break;
}
return consumeEvent;
}
Aggregations