Programming and Exciting Things

Style Dialog in Android

Published on 14.06.2014

The one of the easiest ways to create custom styled Dialog in Android is to hide title and set xml layout in content. For example:

final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_select_action_new);
dialog.show();
requestWindowFeature
method is need to be set before
setContentView
And in
dialog_select_action_new.xml
file we have freedom to make any modifications, just like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f7fa"
    android:minWidth="350dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#97c7dc"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/add_event_title"
            android:textColor="#ffffff"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f0f7fa"
        android:orientation="vertical"
        android:padding="10dp" >

        <Button
            android:id="@+id/dialog_select_fun_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_add_fun"
            android:drawableLeft="@drawable/fun_icon"
            android:drawablePadding="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="@string/add_event_fun_btn"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/dialog_select_ill_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/btn_add_fun"
            android:drawableLeft="@drawable/baby_health_icon"
            android:drawablePadding="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="@string/add_event_fun_ill"
            android:textColor="#ffffff" />
    </LinearLayout>
</LinearLayout>

Final result is:

ps: The big plus from that is on android 2.x devices dialog looks like android 4.x version.

Autoloading Pagent keys on startup

Published on 13.06.2014

I have some private keys which I use to connect to few remote servers. In Windows managing SSH keys with Pagent is easy but loading a few keys everytime after reboot is annoying task. For luck Pagent has ability to load multiple ssh keys when it start up. The easiest method is to make shortcut in Startup program directory to Pagent with some additional params. Just like this:
Pagent shortcut
The goal is to edit "Target" field with path to our private keys, ex:

Target: D:\SSH\pageant.exe d:\SSH\private.ppk d:\SSH\deployment_keys\private.ppk
.
Also we can start Pagent from windows -> start -> run dialog with same params, ex:
D:\SSH\pageant.exe d:\SSH\private.ppk d:\SSH\0config\private.ppk

Export / Backup mongo db to cloud storage

Published on 12.06.2014

I'm using Digital Ocean for my experiments and place to host some sites, they provide very good service for ridiculous price but SSD space is limited and for backup purposes I'm using cloud service providers like pCloud. Here are a sample command for backuping Mongo databases to pCloud service.

cd /tmp/ && rm -rf mongo_dump && mkdir mongo_dump && mongodump --out /tmp/mongo_dump/ && tar -cvzf mongo_production_eureka.tar.gz mongo_dump/ && rm -r mongo_dump/ && curl -F "[email protected]" -F "password=password" -F "folderid=0" -F "filename=mongo1.tar.gz" -F "[email protected]/tmp/mongo_production_eureka.tar.gz" https://api.pcloud.com/uploadfile && rm mongo_production_eureka.tar.gz

For backup only:
cd /tmp/ && rm -rf mongo_dump && mkdir mongo_dump && mongodump --out /tmp/mongo_dump/ && tar -cvzf mongo_production_eureka.tar.gz mongo_dump/ && rm -r mongo_dump/

Our sys admin helped me to make this variant as bash script:
#!/bin/bash
set -e

dumpdir=mongodb.$$.$RANDOM
trap "rm -rf /tmp/$dumpdir /tmp/$dumpdir.tar.gz" exit SIGHUP SIGINT SIGTERM
cd /tmp 
rm -rf $dumpdir 
mkdir $dumpdir
mongodump --out /tmp/$dumpdir 
tar czf $dumpdir.tar.gz mongo_dump/ 
curl -F "[email protected]" -F "password=pas1" -F "folderid=0" -F "filename=mongo1.tar.gz" -F "[email protected]/tmp/$dumpdir.tar.gz" https://api.pcloud.com/uploadfile

More information about pCloud can be viewed in their website http://pcloud.com and Developer documentation
ps: pCloud have versioning of file and for that I'm using one name for all backups.

Nginx precompress resources for saving cpu

Published on 08.06.2014

Nginx have one great module "HttpGzipStaticModule", according to documentation on nginx website his job is to

Before serving a file from disk to a gzip-enabled client, this module will look for a precompressed file in the same location that ends in ".gz". The purpose is to avoid compressing the same file each time it is requested.

So the basic idea is to avoid CPU overhead of deflating on each request. Alson this will give us ability to use maximum compression of resources.
For using this module we must add something like that
location ~* \.(js|css|json)$ {
	gzip_static on;
}
in our config file in section
server
.
In my opinion compressing with gzip image files like (jpg/png/gif) are stupid because they already compressed. Gziping them will slow down the browser. My advice is to use "convert" from imagemagick package to stripping them on deploying.
Here are some example for gzip css/js/json files in deployment process.
find -L "/projects/***********/root/" -regextype posix-extended \( -type f -or -type l \) -and  -regex '.*\.(css|js|json)' -and -size +1k | xargs -n1 -I "{}" sh -c 'gzip -n -c -9 {} > {}.gz && touch {} {}.gz'

Disable skype ads

Published on 05.06.2014

If you want to disable this annoying ads in new version of Skype you can add this line

127.0.0.1       apps.skype.com

in your host file, which are usualy located in
%systemroot%\system32\drivers\etc\hosts