Skip to content

Instantly share code, notes, and snippets.

public class ColorUtils {
private static final double LM_RED_COEFFICIENT = 0.2126;
private static final double LM_GREEN_COEFFICIENT = 0.7152;
private static final double LM_BLUE_COEFFICIENT = 0.0722;
public static int calculateRelativeLuminance(int color) {
int red = (int) (Color.red(color) * LM_RED_COEFFICIENT);
int green = (int) (Color.green(color) * LM_GREEN_COEFFICIENT);
int blue = (int) (Color.blue(color) * LM_BLUE_COEFFICIENT);
return red + green + blue;
@jfsurban
jfsurban / wm_enumarate_evailable_connections.cpp
Created November 12, 2013 09:33
Enumerate available connections on WM5 device - ConnMgrQueryDetailedStatus
CONNMGR_CONNECTION_DETAILED_STATUS *pConnMgrDet;
HRESULT hResult;
DWORD dwBufferSize=0;/
hResult=ConnMgrQueryDetailedStatus(pConnMgrDet,&dwBufferSize);
if(hResult==(HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)))
{
pConnMgrDet=(CONNMGR_CONNECTION_DETAILED_STATUS *)new BYTE[dwBufferSize];
hResult=ConnMgrQueryDetailedStatus(pConnMgrDet,&dwBufferSize);
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms
Read 4K randomly from SSD* 150,000 ns 0.15 ms
@jfsurban
jfsurban / AsyncTask_NonUI_Fragment.txt
Created July 10, 2013 10:49
Android - Background worker using non ui Fragment + AsyncTask - http://stackoverflow.com/a/12303649/718
I think you will enjoy my extremely comprehensive and working example!
Rotation works, and the dialog survives.
You can cancel the task and dialog by pressing the back button (if you want this behaviour).
It uses fragments.
The layout of the fragment underneath the activity changes properly when the device rotates.
There is a complete source code download and a precompiled APK so you can see if the behaviour is what you want.
Edit
As requested by Brad Larson I have reproduced most of the linked solution below. Also since I posted it I have been pointed to AsyncTaskLoader. I'm not sure it is totally applicable to the same problems, but you should check it out anyway.
If your service is going to be part of you application then you are making it way more complex than it needs to be. Since you have a simple use case of getting some data from a RESTful Web Service, you should look into ResultReceiver and IntentService.
This Service + ResultReceiver pattern works by starting or binding to the service with startService() when you want to do some action. You can specify the operation to perform and pass in your ResultReceiver (the activity) through the extras in the Intent.
In the service you implement onHandleIntent to do the operation that is specified in the Intent. When the operation is completed you use the passed in ResultReceiver to send a message back to the Activity at which point onReceiveResult will be called.
So for example, you want to pull some data from your Web Service.
You create the intent and call startService.
The operation in the service starts and it sends the activity a message saying it started
@jfsurban
jfsurban / check_running_service.java
Created June 28, 2013 01:49
Check if a Service is running in Android - http://stackoverflow.com/a/5921190/718
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (MyService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@jfsurban
jfsurban / android tips
Created June 10, 2013 03:21
Android tips & tricks
- do not use null on LayoutInflater.inflate
- use FrameLayout or LinearLayout instead of RelativeLayout as the latter takes at least 2 passes on layout
- use ViewHolder pattern
@jfsurban
jfsurban / intent_available.java
Last active December 17, 2015 22:39
Indicates whether the specified action can be used as an intent - #android http://www.curious-creature.org/2008/12/15/android-can-i-use-this-intent/
/**
* Indicates whether the specified action can be used as an intent. This
* method queries the package manager for installed packages that can
* respond to an intent with the specified action. If no suitable package is
* found, this method returns false.
*
* @param context The application's environment.
* @param action The Intent action to check for availability.
*
package com.company.app.utils;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
int dipValue = 200; // we whant to convert this dip value to pix
Resources r = getResources();
float pix = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, r.getDisplayMetrics());