четверг, 18 июля 2013 г.

Android: warning "SetJavaScriptEnabled"

Используя WebView есть риск получить варнинг setJavaScriptEnabled.

        WebView webView = (WebView) findViewById(R.id.web_view);
        webView.getSettings().setJavaScriptEnabled(true);

Это предупреждение о потенциальной XSS уязвимости.
Для решения этой проблемы можно отключить JS (by default значение равно false), или, если вы уверены в том, что эксплуатация XSS уязвимостей невозможна (например, вы отображаете свой HTML контент), можно "задавить" предупреждение аннотацией на класс или метод:

        @SuppressLint("SetJavaScriptEnabled")

Или комментарием для IDE (проверено в IntellijIdea/AndroidStudio):

        //noinspection AndroidLintSetJavaScriptEnabled
        webView.getSettings().setJavaScriptEnabled(true);

четверг, 25 апреля 2013 г.

Enable TRIM on OSX


The first step makes a backup of the original IOAHCIBlockStorage file called IOAHCIBlockStorage.original. You will be prompted to enter in your system password when using the "sudo" command, since you are modifying system files. Copy and paste the code into the terminal window, a successful or uneventful response is a new blank terminal line.
sudo cp /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage.original
Next the code patches the IOAHCIBlockStorage file, removing the requirements that the SSD be made by Apple. Copy and paste this code just like the previous command, this step should complete in very little time (seconds or less than a second).
sudo perl -pi -e 's|(\x52\x6F\x74\x61\x74\x69\x6F\x6E\x61\x6C\x00{1,20})[^\x00]{9}(\x00{1,20}\x51)|$1\x00\x00\x00\x00\x00\x00\x00\x00\x00$2|sg' /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage
The last step loads the newly patched file into the kernel. On a few systems this particular command takes a bit to complete. Expect to see the terminal window wait for 10-15 seconds before a new line appears.
sudo kextcache -system-prelinked-kernel
sudo kextcache -system-caches
Now reboot your system and verify that TRIM is enabled through the System Information on the Serial-ATA device listing. As you can see on the system we performed this modification on, TRIM support is now enabled.
If you want to disable TRIM or completely reverse any changes performed with this guide, two options are available.
The first reverses the changes.
sudo perl -pi -e 's|(\x52\x6F\x74\x61\x74\x69\x6F\x6E\x61\x6C\x00).{9}(\x00\x51)|$1\x41\x50\x50\x4C\x45\x20\x53\x53\x44$2|sg' /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage
sudo kextcache -system-prelinked-kernel
sudo kextcache -system-caches
The second utilizes the backup file created and restores it over the modified one.
sudo cp /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage.original /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage
Overall the process to enable TRIM on an OSX Lion system is rather painless, with the only requirement being that a SSD that supports TRIM is installed. If you happened to update your notebook or desktop to an SSD, enabling TRIM is a must for the best long term sustained performance.

© http://www.storagereview.com/how_to_enable_trim_with_nonapple_ssd ©

среда, 17 апреля 2013 г.

OSX: Cleanup "open with..."

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user

среда, 20 февраля 2013 г.

Android: How to get current screen orientation?


private int getScreenOrientation() {
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        int orientation;
        // if the device's natural orientation is portrait:
        if ((rotation == Surface.ROTATION_0
                || rotation == Surface.ROTATION_180) && height > width ||
                (rotation == Surface.ROTATION_90
                        || rotation == Surface.ROTATION_270) && width > height) {
            switch (rotation) {
                case Surface.ROTATION_0:
                case Surface.ROTATION_180:
                    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                    break;
                case Surface.ROTATION_90:
                case Surface.ROTATION_270:
                    orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                    break;
                default:
                    Utils.logDebug(TAG, "Unknown screen orientation. Defaulting to portrait.");
                    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                    break;
            }
        } else {
            // if the device's natural orientation is landscape or if the device
            // is square:
            switch (rotation) {
                case Surface.ROTATION_0:
                case Surface.ROTATION_180:
                    orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                    break;
                case Surface.ROTATION_90:
                case Surface.ROTATION_270:
                    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                    break;
                default:
                    Utils.logDebug(TAG, "Unknown screen orientation. Defaulting to portrait.");
                    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                    break;
            }
        }
 
        return orientation;
    }

четверг, 17 января 2013 г.

LCH Color Model. Photoshop blend modes: Hue, Saturation, Color

Wikipedia:

  • The Hue blend mode preserves the luma and chroma of the bottom layer, while adopting the hue of the top layer.
  • The Saturation blend mode preserves the luma and hue of the bottom layer, while adopting the chroma of the top layer.
  • The Color blend mode preserves the luma of the bottom layer, while adopting the hue and chroma of the top layer.


Good. But what is "Luma", "chroma", "hue"? This is CIE LCH color model.

Converting from RGB:
RGB --> XYZ --> LAB --> LCH and back.


#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))

typedef int uint8_t;

typedef struct { //Android RGBA format #AARRGGBB
    uint8_t red;
    uint8_t green;
    uint8_t blue;
    uint8_t alpha;
} argb;

#define WHITEPOINT_X 0.950456
#define WHITEPOINT_Y 1.0
#define WHITEPOINT_Z 1.088754
#define MIN3(A,B,C) (((A) <= (B)) ? min(A,C) : min(B,C))

#define INVGAMMACORRECTION(t) (((t) <= 0.0404482362771076)? ((t)/12.92) : pow(((t) + 0.055)/1.055, 2.4))
#define GAMMACORRECTION(t) (((t) <= 0.0031306684425005883) ? (12.92*(t)) : (1.055*pow((t), 0.416666666666666667) - 0.055))
#define LABF(t) ((t >= 8.85645167903563082e-3) ? pow(t,0.333333333333333) : (841.0/108.0)*(t) + (4.0/29.0))
#define LABINVF(t) ((t >= 0.206896551724137931) ? ((t)*(t)*(t)) : (108.0/841.0)*((t) - (4.0/29.0)))

#define MIN3(A,B,C) (((A) <= (B)) ? min(A,C) : min(B,C))
#define MAX3(A,B,C) (((A) >= (B)) ? max(A,C) : max(B,C))

static void Rgb2Xyz(double *X, double *Y, double *Z, double R, double G, double B)
{
R = INVGAMMACORRECTION(R);
G = INVGAMMACORRECTION(G);
B = INVGAMMACORRECTION(B);
*X = (double)(0.4123955889674142161*R + 0.3575834307637148171*G + 0.1804926473817015735*B);
*Y = (double)(0.2125862307855955516*R + 0.7151703037034108499*G + 0.07220049864333622685*B);
*Z = (double)(0.01929721549174694484*R + 0.1191838645808485318*G + 0.9504971251315797660*B);
}

static void Xyz2Lab(double *L, double *a, double *b, double X, double Y, double Z)
{
X /= WHITEPOINT_X;
Y /= WHITEPOINT_Y;
Z /= WHITEPOINT_Z;
X = LABF(X);
Y = LABF(Y);
Z = LABF(Z);
*L = 116*Y - 16;
*a = 500*(X - Y);
*b = 200*(Y - Z);
}

static void Xyz2Lch(double *L, double *C, double *H, double X, double Y, double Z)
{
double a, b;
Xyz2Lab(L, &a, &b, X, Y, Z);
*C = sqrt(a*a + b*b);
*H = atan2(b, a)*180.0/M_PI;
if(*H < 0)
*H += 360;
}

static void Rgb2Lch(double *L, double *C, double *H, double R, double G, double B)
{
double X, Y, Z;
Rgb2Xyz(&X, &Y, &Z, R, G, B);
Xyz2Lch(L, C, H, X, Y, Z);
}

void Lab2Xyz(double *X, double *Y, double *Z, double L, double a, double b)
{
L = (L + 16)/116;
a = L + a/500;
b = L - b/200;
*X = WHITEPOINT_X*LABINVF(a);
*Y = WHITEPOINT_Y*LABINVF(L);
*Z = WHITEPOINT_Z*LABINVF(b);
}


static void Lch2Xyz(double *X, double *Y, double *Z, double L, double C, double H)
{
double a = C * cos(H*(M_PI/180.0));
double b = C * sin(H*(M_PI/180.0));
Lab2Xyz(X, Y, Z, L, a, b);
}

static void Xyz2Rgb(double *R, double *G, double *B, double X, double Y, double Z)
{
double R_1, B_1, G_1, Min;
R_1 = (double)( 3.2406*X - 1.5372*Y - 0.4986*Z);
G_1 = (double)(-0.9689*X + 1.8758*Y + 0.0415*Z);
B_1 = (double)( 0.0557*X - 0.2040*Y + 1.0570*Z);
Min = MIN3(R_1, G_1, B_1);
    
if(Min < 0) {
R_1 -= Min;
G_1 -= Min;
B_1 -= Min;
}
    
*R = GAMMACORRECTION(R_1);
*G = GAMMACORRECTION(G_1);
*B = GAMMACORRECTION(B_1);
    
    *R = max(*R, 0);
    *R = min(*R, 1);
    *G = max(*G, 0);
    *G = min(*G, 1);
    *B = max(*B, 0);
    *B = min(*B, 1);
}


static void Lch2Rgb(double *R, double *G, double *B, double L, double C, double H)
{
double X, Y, Z;
Lch2Xyz(&X, &Y, &Z, L, C, H);
Xyz2Rgb(R, G, B, X, Y, Z);
}

note!

You can use HSV/HSL transformation instead the RGBtoLCH. But!

LCH color model (Photoshop)


HSV/HSL color model (GIMP)