Wednesday, March 15, 2017

Android XML Layout (most used options).

  1. Everything you see is a view.
  2. View types: TextView, ImageView, Button etc.
  3. Every view has attributes="values" syntax.
  4. Pixel / dot / px  - smallest controllable element of the screen.
  5. dp dip (density|device independent pixel) - unit of measurement based on a coordinate system so every device using dp then can convert it to the actual (physical) pixel size (default on Android device 1dp=1/160 of inch=0.15875mm)
  6. sp is the same as dp but used for representing text
  7. Make touch targets 48dp at least

General (to all views and view-groups) attributes

#hex-colors can be found on https://material.io/guidelines/style
# /color.html#color-color-palette
android:background="#hex-color"

#android:layout_
#measure can be like:
# "75dp" or
# "wrap_content" - only space needed for displaying view content (i.e. text) # or
# "match_parent" - all space available inside the parent
android:layout_width="measure"
android:layout_height="measure"

#to use comments inside xml
<!-- this is comment -->

TextView

<TextView
  Attribute-Values goes here
            />

Atributes-values

android:text="Hello World"
android:textColor="#hex-color"

#you can use `textSize` with dedicated font size or use
#`textAppearance` to make your app matching phone-user preferences:
# "?android:textAppearanceLarge" or "?android:textAppearanceMedium" or "android:textAppearanceSmall" android:textSize="24sp"
android:textAppearance="?android:textAppearanceMedium"

ImageView

<ImageView
  Attribute-Values goes here
            />

Atributes-values

#`@drawable` is directory name for Android Studio where all images must be downloaded
# `pic` is the name of the picture in the `drawable` directory
android:src="@drawable/pic"

#how a picture must be presented on the screen:
#`center` if a picture is larger than a screen we'll see only central part of it
#`centerCrop` aligns to the center ov the view and crop (cut off) to the #actual size of the view
android:scaleType="center"

ViewGroups

ViewGroup is the view containing other views, so ViewGroup is the parent view and views inside it are children and all views inside the same ViewGroup are siblings. There are two types of the View Groups: LinearLayout and RelativeLayout

LinearLayout

LinearLayout arranges children only either vertical (children are below each other) or horizontal (children goes one by one next to each other horizontally).

<LinearLayout
     #`horizontal` or `vertical`
     android:orientation="vertical" >
     Child-view-1
     Child-view-2
     ....
</LinearLayout>

If you want to arrange children proportionally, basing on their weight, use android:layout_weight.

For example we have 3 views with corresponding weights: 1st - weight 1, 2nd - weight 2 and 3rd weight 3, then total measure of the parent will be 1+2+3, so parent's 100% measure will be split into 6 parts. If parent is 180dp and 180dp/6=30dp so 1 part equals 30dp, then 1st view will be 1part - 30dp, 2nd - 2 parts - 60dp and the 3rd - 3 parts = 90dp (30dp+60dp+90dp=180dp).

Examples:
  1. horizontal orientation all children with the same width, you have several choices but I use this one:
    1. For all children do:
    2. android:layout_width="wrap_content"
    3. android:layout_height="match_parent"
    4. android:layout_weight="1" 
  2. vertical orientation all children with the same height, you have several choices but I use this one:
    1. For all children do:
    2. android:layout_width="match_parent"
    3. android:layout_height="wrap_content"
    4. android:layout_weight="1"
But if your view is too big, then you can use measure in dp or if using `wrap_content` it can lead you to the situation when all other views will disappear. In all other situations as I understand when you use layout_weight - "wrap_content"="0dp"

RelativeLayout

In RelativeLAyout views are arranged relative to parent or siblings:
  1. relative to parent (I use regex to write shorter):
    1. default position of the child is the top left corner of the parent
    2. android:layout_alignParent(Top|Bottom|Left|Right)="(true|false)"
    3. android:layout_center(Vertical|Horizontal)="(true|false)"
  2. relative to sibling (I use regex to write shorter):
    1.  to arrange relative to siblings you first need to name views using IDs: android:id="@+id/desired_name_here"
    2. and then use relation to this sibling with:
    3. android:layout_to(Left|Right)of="@id/given_name_here"
    4. android:layout_(above|below)="@id/given_name_here"

Padding & Margin

You can use padding or margin to add white space to the edges of the ViewGroup or View. Padding enlarges the View itself adding padding to the width and height (content of the view will remain in the centre of the View) and margin is used inside ViewGroup adding frame to the child View. But user of the app will see both padding and margin as the same thing if you don't use different background color for both ViewGroup and View.
#to add padding to all sides of a View:
android_padding="8dp"  
#or specifying side (content of the View will be moved from that side to the other side if padding is not the same into opposite directions):
android_padding(Left|Right|Top|Bottom)="8dp"


Thursday, March 9, 2017

Linux minicom pexpect-ing and piping with subprocess Popen & PIPE

I have E1 rack device (parabel ELF), which sometimes serving improper in such situations I always connected to the ELF and restarted it via minicom (this device is connecting using USB - and USB to serial ttyUSB). I tried with Python pexpect:

[root@localhost ~]# vi elf_restart.py
#!/usr/bin/python

from subprocess import PIPE,Popen
import pexpect
import time

#piping (`PIPE`) `dmesg` to find USB Serial Device
#it will be in line like:
#usb 1-1.4: FTDI USB Serial Device converter now attached to ttyUSB1
proc1 = Popen(["/bin/dmesg"], stdout=PIPE)
#using output of proc1 as input for proc 2 to use `grep ttyUSB`
proc2 = Popen(["/bin/grep", "ttyUSB"], stdin=proc1.stdout, stdout=PIPE)
#using output of proc2 as input for proc 3 - showing one last line
proc3 = Popen(["/usr/bin/tail", "-n", "1"], stdin=proc2.stdout, stdout=PIPE)
#splitting line from the `tail` output to the `words`
dmesg = (proc3.communicate()[0].split(" "))
#taking last word (containing ttyUSB)
usb_console = dmesg[-1].strip()
print usb_console
#will use `-s` option to enter setup-mode
connection_string = 'minicom -s'
#connecting to serial emulator and starting logging to the file `elf_pexpect.log`
elf = pexpect.spawn(connection_string)
elf.logfile = open("elf_pexpect.log", "w")
#wait for minicom for 3 seconds
time.sleep(3)
#press down arrow 2 times to select `Serial port setup`
for press in range(2):
 elf.send('j')
#send `Enter`
elf.sendcontrol('m')
#enter `A - Serial Device`
elf.send('a')
#backspace 5 times to erase `modem` word
for press in range(5):
 elf.send('\10')
#inputing actial USB Serial Device name & pressing `Enter`
elf.send(usb_console)
elf.sendcontrol('m')
#enteing ` E -    Bps/Par/Bits`
elf.send('e')
#we select `D:  38400`
elf.send('d')
#press `Enter`
elf.sendcontrol('m')
#make `F - Hardware Flow Control` uqual `No` - default is `Yes`
elf.send('f')
elf.sendcontrol('m')
#move to the `Exit` and exit setup-mode
for press in range(5):
 elf.send('j')
elf.sendcontrol('m')
#wait for the device for 5 seconds
time.sleep(5)
#here are device specific commands to restart device,
#you can use commands appropriate for your device
for press in range(2):
 elf.send('0')
elf.send('9')
#after restarting wait for the device for 5 seconds
time.sleep(5)
#3 lines to exit minicom
elf.sendcontrol('a')
elf.send('q')
elf.sendcontrol('m')
#if something went wrong you can use below command
#for manually interacting device
#elf.interact()

Tuesday, March 7, 2017

Sorting and Counting output in Linux shell

Sometimes we need to make some operations on commands output in Linux. Very intensive operation on output I do is sorting and counting. For the sake of an example - every time I setup a new equipment I make backup and save it in `back` directory, with the file-names like:
ls -1 back  |  head -n3
101-back-2017-02-21
102-back-2017-03-01
106-back-2017-02-21
Where numbers at the starting of the names of the backups are branch codes.
When I want to learn how many equipment was installed I can simply do:
ls back | wc -l
98
But this is total number of all configured equipment. what if I want to find how much equipment I setup every month?
Let's go:
  1. we need to list all files in `back` directory along with their creation times:
    1. ll back/ 
  2. now we need to select only month (6 column) and print it:
    1. ll back/ | awk '{print $6}'
  3. now we need to sort output alphabetically:
    1. ll back/ | awk '{print $6}' | sort
  4. we needed sorting because uniq command filtering matching adjacent lines and then making operations according to specified options:
    1. ll back/ | awk '{print $6}' | sort | uniq

      1. Feb
      2. Mar
    2. we'll use `-c` which makes `uniq` count all occurrences of unique values  (Mar and Feb in our case) in the output:
      1. ll back/ | awk '{print $6}' | sort | uniq -c
        1. 1
        2. 60 Feb
        3. 38 Mar
      2. You see line which count is `1` this is because of the `ll` commands `total` first line, to avoid this you can use:
        1.  ll back/ | grep -v total | awk '{print $6}' | sort | uniq -c
  5. If you need to view results per day of the month additional use 7th column (use "-" to see `-` as month and day of the month delimiter):
    1. Sorting using default `sort` settings
    2. ll back/ | grep -v total | awk '{print $6 "-" $7}' | sort | uniq -c
      1. 1 Feb-20
      2. 59 Feb-21
      3. 23 Mar-1
      4. 9 Mar-2
      5. 6 Mar-7
    3. Soring using day of the month and numeric sorting with `-` delimiter (to sort in reverse order, use `-r` option > '-k2nr' instead of `-k2n`):
    4. ll back/ | grep -v total | awk '{print $6 "-" $7}' | sort -t"-" -k2n | uniq -c
      1. 23 Mar-1
      2. 9 Mar-2
      3. 6 Mar-7
      4. 1 Feb-20
      5. 59 Feb-21
     


Wednesday, March 1, 2017

Make Apache use SSL


openssl genrsa -out ca.key 2048
openssl req -new -key ca.key -out ca.csr
openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt
cp ca.crt /etc/pki/tcl/certs/ca.crt
cp ca.key /etc/pki/tcl/private/ca.key
cp ca.csr /etc/pki/tls/private/ca.csr
vi +/SSLCertificateFile /etc/httpd/conf.f/ssl.conf and change:
SSLCertificateFile /etc/pki/tlc/certs/ca.crt
SSLCertificateFile /etc/pki/tlc/private/ca.key
Now restart your web-server and use https