use of com.codename1.io.Log in project CodenameOne by codenameone.
the class Log method logThrowable.
/**
* Logs an exception to the log, by default print is called with the exception
* details, on supported devices the stack trace is also physically written to
* the log
* @param t
*/
protected void logThrowable(Throwable t) {
if (t == null) {
p("Exception logging invoked with null exception...");
return;
}
print("Exception: " + t.getClass().getName() + " - " + t.getMessage(), ERROR);
Thread thr = Thread.currentThread();
if (thr instanceof CodenameOneThread && ((CodenameOneThread) thr).hasStackFrame()) {
print(((CodenameOneThread) thr).getStack(t), ERROR);
}
t.printStackTrace();
try {
synchronized (this) {
Writer w = getWriter();
Util.getImplementation().printStackTraceToStream(t, w);
w.flush();
}
} catch (IOException err) {
err.printStackTrace();
}
}
use of com.codename1.io.Log in project CodenameOne by codenameone.
the class AndroidImplementation method fixAttachmentPath.
private String fixAttachmentPath(String attachment) {
if (attachment.contains(getAppHomePath())) {
FileSystemStorage fs = FileSystemStorage.getInstance();
final char sep = fs.getFileSystemSeparator();
String fileName = attachment.substring(attachment.lastIndexOf(sep) + 1);
String[] roots = FileSystemStorage.getInstance().getRoots();
// iOS doesn't have an SD card
String root = roots[0];
for (int i = 0; i < roots.length; i++) {
// media_rw is a protected system lib
if (FileSystemStorage.getInstance().getRootType(roots[i]) == FileSystemStorage.ROOT_TYPE_SDCARD && !roots[i].contains("media_rw")) {
root = roots[i];
break;
}
}
// might happen if only the media_rw is of type ROOT_TYPE_SDCARD
if (root.contains("media_rw")) {
// try again without checking the root type
for (int i = 0; i < roots.length; i++) {
// media_rw is a protected system lib
if (!roots[i].contains("media_rw")) {
root = roots[i];
break;
}
}
}
String fileUri = root + sep + "tmp" + sep + fileName;
FileSystemStorage.getInstance().mkdir(root + sep + "tmp");
try {
InputStream is = FileSystemStorage.getInstance().openInputStream(attachment);
OutputStream os = FileSystemStorage.getInstance().openOutputStream(fileUri);
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > -1) {
os.write(buf, 0, len);
}
is.close();
os.close();
} catch (IOException ex) {
Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
}
attachment = fileUri;
}
if (attachment.indexOf(":") < 0) {
attachment = "file://" + attachment;
}
return attachment;
}
use of com.codename1.io.Log in project CodenameOne by codenameone.
the class CodenameOneActivity method getProducts.
Product[] getProducts(String[] skus, boolean fromCacheOnly) {
if (inventory != null) {
ArrayList pList = new ArrayList<Product>();
ArrayList moreskusList = new ArrayList<Product>();
for (int i = 0; i < skus.length; i++) {
String sku = skus[i];
if (inventory.hasDetails(sku)) {
SkuDetails details = inventory.getSkuDetails(sku);
Product p = new Product();
p.setSku(sku);
p.setDescription(details.getDescription());
p.setDisplayName(details.getTitle());
p.setLocalizedPrice(details.getPrice());
pList.add(p);
} else {
moreskusList.add(sku);
}
}
// if the inventory does not all the requestes sku make an update.
if (moreskusList.size() > 0 && !fromCacheOnly) {
try {
inventory = mHelper.queryInventory(true, moreskusList);
return getProducts(skus, true);
} catch (IabException ex) {
Logger.getLogger(CodenameOneActivity.class.getName()).log(Level.SEVERE, null, ex);
}
}
Product[] products = new Product[pList.size()];
products = (Product[]) pList.toArray(products);
return products;
}
return null;
}
use of com.codename1.io.Log in project CodenameOne by codenameone.
the class LocalNotificationPublisher method createAndroidNotification.
private Notification createAndroidNotification(Context context, LocalNotification localNotif, PendingIntent content) {
Context ctx = context;
int smallIcon = ctx.getResources().getIdentifier("ic_stat_notify", "drawable", ctx.getApplicationInfo().packageName);
int icon = ctx.getResources().getIdentifier("icon", "drawable", ctx.getApplicationInfo().packageName);
if (smallIcon == 0) {
smallIcon = icon;
} else {
icon = smallIcon;
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setContentTitle(localNotif.getAlertTitle());
builder.setContentText(localNotif.getAlertBody());
builder.setAutoCancel(true);
if (localNotif.getBadgeNumber() > 0) {
builder.setNumber(localNotif.getBadgeNumber());
}
String image = localNotif.getAlertImage();
if (image != null && image.length() > 0) {
if (image.startsWith("/")) {
image = image.substring(1);
}
InputStream in;
try {
in = context.getAssets().open(image);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap im = BitmapFactory.decodeStream(in, null, opts);
builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(im));
} catch (IOException ex) {
Logger.getLogger(LocalNotificationPublisher.class.getName()).log(Level.SEVERE, null, ex);
}
}
builder.setSmallIcon(smallIcon);
builder.setContentIntent(content);
String sound = localNotif.getAlertSound();
if (sound != null && sound.length() > 0) {
sound = sound.toLowerCase();
builder.setSound(android.net.Uri.parse("android.resource://" + ctx.getApplicationInfo().packageName + "/raw" + sound.substring(0, sound.indexOf("."))));
}
Notification n = builder.build();
n.icon = icon;
if (sound == null || sound.length() == 0) {
n.defaults |= Notification.DEFAULT_SOUND;
}
return n;
}
use of com.codename1.io.Log in project CodenameOne by codenameone.
the class StubLocationManager method addGeoFencing.
@Override
public void addGeoFencing(final Class GeofenceListenerClass, Geofence gf) {
if (gf.getId() != null) {
String id = gf.getId();
int index = -1;
for (Geofence f : geoFences) {
if (id.equals(f.getId())) {
index = geoFences.indexOf(f);
break;
}
}
if (index >= 0) {
geoFences.remove(index);
}
if (gf.getRadius() < 0) {
throw new IllegalArgumentException("Attempt to add geofence with negative radius");
}
if (gf.getRadius() < 100) {
Log.p("Adding Geofence with a radius of " + gf.getRadius() + " metres. On an actual device, the effective radius will vary. Typical Android and iOS devices have a minimum geofence radius of approximately 100m");
}
long expires = gf.getExpiration();
geoFences.add(gf);
if (geofenceTimer == null) {
geofenceTimer = new java.util.Timer();
geofenceTask = new TimerTask() {
public void run() {
Display.getInstance().callSerially(new Runnable() {
public void run() {
Location loc;
try {
loc = getCurrentLocation();
if (JavaSEPort.locSimulation == null) {
loc.setLongitude(loc.getLongitude() + 0.001);
loc.setLatitude(loc.getLatitude() + +0.001);
} else {
loc.setLongitude(JavaSEPort.locSimulation.getLongitude());
loc.setLatitude(JavaSEPort.locSimulation.getLatitude());
}
// Do exits first
for (final Geofence f : geoFences) {
if (!isInRegion(loc, f) && insideFences.contains(f.getId())) {
insideFences.remove(f.getId());
try {
final GeofenceListener l = (GeofenceListener) GeofenceListenerClass.newInstance();
new Thread() {
public void run() {
// In a separate thread to simulate that
// this might not happen on EDT
l.onExit(f.getId());
}
}.start();
} catch (Throwable t) {
Log.e(t);
}
}
}
// Do entrances next
for (final Geofence f : geoFences) {
if (isInRegion(loc, f) && !insideFences.contains(f.getId())) {
insideFences.add(f.getId());
try {
final GeofenceListener l = (GeofenceListener) GeofenceListenerClass.newInstance();
new Thread() {
public void run() {
// In a separate thread to simulate that
// this might not happen on EDT
l.onEntered(f.getId());
}
}.start();
} catch (Throwable t) {
Log.e(t);
}
}
}
} catch (IOException ex) {
Logger.getLogger(StubLocationManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
};
geofenceTimer.schedule(geofenceTask, new Date(System.currentTimeMillis() + 10000L), 10000L);
}
} else {
Log.p("Attempt to add Geofence with null ID", Log.WARNING);
}
}
Aggregations