четвер, 30 травня 2013 р.

Oracle database tips&tricks

Here I am going to collect useful queries for Oracle database.

If you use many database users and need to check which of them are locked:
select username, ACCOUNT_STATUS from dba_users where username in ('DB_USER1', 'DB_USER2', 'DB_USER3', ....);
User with EXPIRED or LOCKED status can reactivated back to OPEN status by:
alter user DB_USER1 account unlock;
To prevent account expiration in future - you can set unlimited password time frame for all users with default profile:
alter profile default limit password_life_time_unlimited;

вівторок, 16 квітня 2013 р.

My new app - WallPaperGirls - Corset and spring-like ads management

Image for more interest :)


This is my new pet project that I started to develop month ago. First version include nearly 50 images and viewer application. I described here http://mvnblogbuild.blogspot.com/2013/04/android-bitmap-and-outofmemoryerror.html how I  fought with android memory manager. Here I tell you about ads management in this application. On my regular work I use spring framework. That's why I decided to create the same mechanism for ad rotation in app.
For each ad network was created separate class. Settings was stored in class annotations. For example annotation for class is looks like this:
import java.lang.annotation.*;
@Target(value=ElementType.TYPE)
@Retention(value= RetentionPolicy.RUNTIME)
public @interface AdEntityMeta {
int runFrequency() default 0;
String name();
}
Sample ad class with AdEntityMeta:
@AdEntityMeta(name = "SampleAdNetworkName")
public class SampleAdNetwork {...}
 As you see we set the name of class to SampleAdNetworkName and leave runFrequency property to default 0 value. In manager class I use reflection for processing ad network classes. It iterate by classes and check annotation existence by calling:
Class adCl = adObj.getClass();
if(adCl.isAnnotationPresent(AdEntityMeta.class)) {...}
If annotation exists then we can get parameters from it:
 AdEntityMeta adEntityMeta = adCl.getAnnotation(AdEntityMeta.class);
Initialization method marked by AdEntityInitMeta annotation. To process it I use this code:
Method[] method = adCl.getMethods();
for(Method md: method) {
if(md.isAnnotationPresent(AdEntityInitMeta.class)) {
AdEntityInitMeta adEntityInitMeta = md.getAnnotation(AdEntityInitMeta.class);
if (adEntityInitMeta.initType() == AdEntityLifecycleType.APPLICATION_INIT) {
Class[] methodParamTypes = md.getParameterTypes();
Object[] methodArgs = new Object[methodParamTypes.length];
for (int i=0; i
if (methodParamTypes[i] == Activity.class) {
methodArgs[i] = this.baseActivity;
}
}
try {
md.invoke(adObj, methodArgs);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Here program enumerates similarly all methods from class and checks method marked by AdEntityInitMeta annotation. All input method parameters are stored in methodArgs. In this code was allowed only parameter with type Activity or inherited from it.

If you are interested - here is my app: https://play.google.com/store/apps/details?id=com.blogspot.mvnblogbuild.wallpapergirls.corsets

неділя, 7 квітня 2013 р.

Android Bitmap and OutOfMemoryError

In this post I am going to collect tips and tricks to help developer avoid OutOfMemoryError.

In code below I use this function to suspend current thread for some time:

public static void sleep(long time) {
  try {
    Thread.sleep(time);
  } catch (Exception e) {}
}

First of all let's talk about bitmap loading. Imagine you must load whole image with width 10x for imageview control with width x. Control don't need full size original image for good quality. Also if you load it as is then bitmap allocate too much memory. (If image configuration is ARGB_8888 then 4 bytes were allocated for every pixel). If we load scaled image with width x this will be enough. In first step we read only image size without loading content:
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(context.getResources(),     R.drawable.sample_image, options);
Now options variable contain width and height of image. I scale image by control width. BitmapFactory.Options have inSampleSize property. This property used to return smaller image to save memory. The documentation mentions using values that are a power of 2. Result code looks like this:

int scale = 1;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(context.getResources(), resouceId, options);
int bmpWidth = options.outWidth;
if (bmpWidth > destWidth) {
  do {
    bmpWidth /= 2;
    if (bmpWidth > destWidth) {
      scale *= 2;
    }
  } while (bmpWidth > destWidth);
}
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
Bitmap image = BitmapFactory.decodeResource(context.getResources(), resouceId, options);

Here we multiply scale variable by 2 and divide image width while it greater then destWidth. We get image with minimal size and good quality for display in control with destWidth width.

Let's consider a simple situation. We need to load bitmap, process it and destroy:

Bitmap image = ... // Load image
// Process image content
image.recycle();
sleep(100);
image = null;
System.gc();
sleep(100);
recycle function free up memoty associated with image. sleep function here take time for GC.


If you display large image in activity and need to free resources after activity was closed:
@Override
public void onCreate(Bundle savedInstanceState) {
  ...
  imageView = (ImageView) findViewById(R.id.full_image_view);
  imageView.setImageBitmap(...);
  ...
}

@Override
protected void onDestroy() {
  ((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();
  sleep(100);
  super.onDestroy();
  System.gc();
  sleep(100);
}

Upd: If image quality is not important to you:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
This value provide normal image quality and any pixel is stored in 2 bytes. If inPreferredConfig is null and decoder try to pick image with ARGB_8888 then one pixel is stored in 4 bytes in memory. For large images this will increase memory usage.

понеділок, 4 березня 2013 р.

One more way to avoid Access-Control-Allow-Origin restriction for cross domain ajax requests

Some time ago I developed the application based on WebKit engine. It allows user to process page elements by injecting javascript code into original third party site source. Let's call it js tool. First version of it interacted with my site by sending ajax reqests. Everything works fine before I try to test it on secure connection to the site which we call SecureSite.

When I loaded a page from this site - my tool was not working. WebKit web inspector showed 1 error:

XMLHttpRequest cannot load http://localhost:8080/wsapi/util/uploadpagesrc. Origin https://some.secure.domain is not allowed by Access-Control-Allow-Origin.

SecureSite in his response headers sended Access-Control-Allow-Origin which block ajax request to my localhost site. In forums I found solution to use jsonp request. Here is example:

$(document).ready(function() {
 $.getJSON('http://twitter.com/users/usejquery.json?callback=?', function(json) {
 $('#twitter_followers').text(json.followers_count);
 });
});
But I need to send post requests and ability to save large data. In my js tool is used websockets. When ajax requests throws errors websockets works fine. They have only one minus - message size limitation. For cometd it was 8192:


I have not configured websocket server to increase this limit. Maybe I will do this another time