use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.
the class SpanLabelTest2980 method start.
public void start() {
if (current != null) {
current.show();
return;
}
Layout lay = new FlowLayout();
// Layout lay = BoxLayout.y();
Form hi = new Form("Gallery Test", lay);
hi.add(new SpanLabel("Tap the following button to open the gallery. You should be able to select multiple images and videos."));
Button btn = new Button("Run Test");
SpanLabel files = new SpanLabel();
SpanLabel result = new SpanLabel();
btn.addActionListener(l -> {
try {
SpanLabelTests2980 test = new SpanLabelTests2980();
test.runTest();
result.setText("Test Passed");
} catch (Throwable t) {
result.setText("Failed: " + t.getMessage());
}
hi.revalidateWithAnimationSafety();
});
hi.addAll(btn, files, result);
hi.show();
}
use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.
the class GoogleLoginSample method doLogin.
void doLogin(Login lg, UserData data, boolean forceLogin) {
if (!forceLogin) {
if (lg.isUserLoggedIn()) {
showContactsForm(data);
return;
}
// if the user already logged in previously and we have a token
String t = Preferences.get(tokenPrefix + "token", (String) null);
if (t != null) {
// we check the expiration of the token which we previously stored as System time
long tokenExpires = Preferences.get(tokenPrefix + "tokenExpires", (long) -1);
if (tokenExpires < 0 || tokenExpires > System.currentTimeMillis()) {
// we are still logged in
showContactsForm(data);
return;
}
}
}
lg.setCallback(new LoginCallback() {
@Override
public void loginFailed(String errorMessage) {
Dialog.show("Error Logging In", "There was an error logging in: " + errorMessage, "OK", null);
}
@Override
public void loginSuccessful() {
// when login is successful we fetch the full data
data.fetchData(lg.getAccessToken().getToken(), () -> {
// we store the values of result into local variables
uniqueId = data.getId();
fullName = data.getName();
imageURL = data.getImage();
// we then store the data into local cached storage so they will be around when we run the app next time
Preferences.set("fullName", fullName);
Preferences.set("uniqueId", uniqueId);
Preferences.set("imageURL", imageURL);
Preferences.set(tokenPrefix + "token", lg.getAccessToken().getToken());
// token expiration is in seconds from the current time, we convert it to a System.currentTimeMillis value so we can
// reference it in the future to check expiration
Preferences.set(tokenPrefix + "tokenExpires", tokenExpirationInMillis(lg.getAccessToken()));
showContactsForm(data);
});
}
});
lg.doLogin();
}
use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.
the class SimpleDateFormatTest method start.
public void start() {
if (current != null) {
current.show();
return;
}
final Form hi = new Form("Hi World", BoxLayout.y());
TextField dateStringIn = new TextField();
TextField dateFormat = new TextField();
Label shortMonth = new Label();
Label longMonth = new Label();
Label shortDate = new Label();
Label longDate = new Label();
Label dateTime = new Label();
Label formattedString = new Label();
Container result = new Container(BoxLayout.y());
Button parse = new Button("Parse Date");
Container resultWrapper = new Container(BoxLayout.y());
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm a");
dateFormatter.format(new Date());
parse.addActionListener(evt -> {
try {
SimpleDateFormat inputFormat = new SimpleDateFormat(dateFormat.getText());
Date dt = dateStringIn.getText().length() == 0 ? new Date() : inputFormat.parse(dateStringIn.getText());
SimpleDateFormat shortMonthFormat = new SimpleDateFormat("MMM");
shortMonth.setText(shortMonthFormat.format(dt));
SimpleDateFormat longMonthFormat = new SimpleDateFormat("MMMM");
longMonth.setText(longMonthFormat.format(dt));
longDate.setText(L10NManager.getInstance().formatDateLongStyle(dt));
shortDate.setText(L10NManager.getInstance().formatDateShortStyle(dt));
dateTime.setText(L10NManager.getInstance().formatDateTime(dt));
formattedString.setText(inputFormat.format(dt));
resultWrapper.removeAll();
resultWrapper.add(result);
hi.revalidateWithAnimationSafety();
} catch (ParseException ex) {
Log.e(ex);
ToastBar.showErrorMessage("Parse failed: " + ex.getMessage());
}
});
result.addAll(new Label("Short month name:"), shortMonth, new Label("Long month name:"), longMonth, new Label("Short Date:"), shortDate, new Label("Long Date:"), longDate, new Label("Time: "), dateTime, new Label("Formatted String:"), formattedString);
hi.addAll(new Label("Date:"), dateStringIn, new Label("Date Format:"), dateFormat, parse, resultWrapper);
hi.show();
}
use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.
the class TestComponent2 method testSimpleDateFormat.
public void testSimpleDateFormat() {
try {
SimpleDateFormat format = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
format.parse("mon, 20-nov-2017 19:49:58 gmt");
format = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss z");
format.parse("mon 20-nov-2017 19:49:58 gmt");
format = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss z");
java.util.Date dt = format.parse("tue 21-nov-2017 17:04:27 gmt");
if (Display.getInstance().isSimulator()) {
java.text.SimpleDateFormat format0 = new java.text.SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss z", java.util.Locale.US);
java.util.Date dt0 = format0.parse("tue 21-nov-2017 17:04:27 gmt");
TestUtils.assertEqual(dt0, dt, "SimpleDateFormat gives different result than java.text version on tue 21-nov-2017 17:04:27 gmt with format \"EEE, dd-MMM-yyyy HH:mm:ss z\"");
}
TestUtils.assertBool(dt.getTime() / 1000L == 1511283867L, "Incorrect date for simple date format parse");
format = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
com.codename1.l10n.ParseException pex = null;
try {
format.parse("tue 21-nov-2017 17:04:27 gmt");
} catch (com.codename1.l10n.ParseException pex2) {
pex = pex2;
}
TestUtils.assertTrue(pex != null, "Parsing date with wrong format should give parse exception");
format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
dt = format.parse("sun, 22 nov 2037 13:20:46 -0000");
// 2142508846
// Log.p("Difference = "+(dt.getTime() - 2142508846000L));
TestUtils.assertEqual(dt.getTime() / 1000L, 2142508846L, "Failed to parse RFC822 datetime. " + dt);
String dateStr = "sun 22 nov 2037 13:20:46 -0000";
format = new SimpleDateFormat("EEE dd MMM yyyy HH:mm:ss Z");
dt = format.parse("sun 22 nov 2037 13:20:46 -0000");
TestUtils.assertEqual(dt.getTime() / 1000L, 2142508846L, "Failed to parse RFC822 datetime no comma. " + dt);
} catch (Throwable t) {
Log.e(t);
throw new RuntimeException("Failed to parse date mon 20-nov-2017 19:49:58 gmt: " + t.getMessage());
}
}
use of com.codename1.rad.processing.Result in project CodenameOne by codenameone.
the class JavaSEPort method getInAppPurchase.
public Purchase getInAppPurchase() {
return new Purchase() {
private Vector purchases;
@Override
public Product[] getProducts(String[] skus) {
return null;
}
@Override
public boolean isItemListingSupported() {
return false;
}
@Override
public boolean isManagedPaymentSupported() {
return managedPurchaseSupported;
}
@Override
public boolean isManualPaymentSupported() {
return manualPurchaseSupported;
}
@Override
public boolean isRefundable(String sku) {
if (!refundSupported) {
return false;
}
int val = JOptionPane.showConfirmDialog(window, "Is " + sku + " refundable?", "Purchase", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
return val == JOptionPane.YES_OPTION;
}
private Vector getPurchases() {
if (purchases == null) {
purchases = (Vector) Storage.getInstance().readObject("CN1InAppPurchases");
if (purchases == null) {
purchases = new Vector();
}
}
return purchases;
}
private void savePurchases() {
if (purchases != null) {
Storage.getInstance().writeObject("CN1InAppPurchases", purchases);
}
}
@Override
public boolean isSubscriptionSupported() {
return subscriptionSupported;
}
@Override
public boolean isUnsubscribeSupported() {
return subscriptionSupported;
}
@Override
public String pay(final double amount, final String currency) {
try {
if (Display.getInstance().isEdt()) {
final String[] response = new String[1];
Display.getInstance().invokeAndBlock(new Runnable() {
public void run() {
response[0] = pay(amount, currency);
}
});
return response[0];
}
if (!manualPurchaseSupported) {
throw new RuntimeException("Manual payment isn't supported check the isManualPaymentSupported() method!");
}
final String[] result = new String[1];
final boolean[] completed = new boolean[] { false };
SwingUtilities.invokeLater(new Runnable() {
public void run() {
int res = JOptionPane.showConfirmDialog(window, "A payment of " + amount + " was made\nDo you wish to accept it?", "Payment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (res == JOptionPane.YES_OPTION) {
result[0] = UUID.randomUUID().toString();
}
completed[0] = true;
}
});
Display.getInstance().invokeAndBlock(new Runnable() {
public void run() {
while (!completed[0]) {
try {
Thread.sleep(20);
} catch (InterruptedException err) {
}
}
}
});
if (getPurchaseCallback() != null) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (result[0] != null) {
getPurchaseCallback().paymentSucceeded(result[0], amount, currency);
} else {
getPurchaseCallback().paymentFailed(UUID.randomUUID().toString(), null);
}
}
});
}
return result[0];
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
public void purchase(final String sku) {
if (managedPurchaseSupported) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final int res = JOptionPane.showConfirmDialog(window, "An in-app purchase of " + sku + " was made\nDo you wish to accept it?", "Payment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (res == JOptionPane.YES_OPTION) {
com.codename1.payment.Purchase.postReceipt(Receipt.STORE_CODE_SIMULATOR, sku, "cn1-iap-sim-" + UUID.randomUUID().toString(), System.currentTimeMillis(), "");
getPurchaseCallback().itemPurchased(sku);
getPurchases().addElement(sku);
savePurchases();
} else {
getPurchaseCallback().itemPurchaseError(sku, "Purchase failed");
}
}
});
}
});
} else {
throw new RuntimeException("In app purchase isn't supported on this platform!");
}
}
@Override
public void refund(final String sku) {
if (refundSupported) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
getPurchaseCallback().itemRefunded(sku);
getPurchases().removeElement(sku);
savePurchases();
}
});
}
}
@Override
public void subscribe(final String sku) {
if (getReceiptStore() != null) {
purchase(sku);
return;
}
if (subscriptionSupported) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final int res = JOptionPane.showConfirmDialog(window, "An in-app subscription to " + sku + " was made\nDo you wish to accept it?", "Payment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (res == JOptionPane.YES_OPTION) {
getPurchaseCallback().subscriptionStarted(sku);
getPurchases().addElement(sku);
savePurchases();
} else {
getPurchaseCallback().itemPurchaseError(sku, "Subscription failed");
}
}
});
}
});
}
}
@Override
public void unsubscribe(final String sku) {
if (subscriptionSupported) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final int res = JOptionPane.showConfirmDialog(window, "In-app unsubscription request for " + sku + " was made\nDo you wish to accept it?", "Payment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (res == JOptionPane.YES_OPTION) {
getPurchaseCallback().subscriptionCanceled(sku);
getPurchases().removeElement(sku);
savePurchases();
} else {
getPurchaseCallback().itemPurchaseError(sku, "Error in unsubscribe");
}
}
});
}
});
}
}
@Override
public boolean wasPurchased(String sku) {
return getPurchases().contains(sku);
}
@Override
public String getStoreCode() {
return Receipt.STORE_CODE_SIMULATOR;
}
};
}
Aggregations