Creating .keystore from .cert and .key


A quick not for creating .keystore using a set of certificates including root, intermediate and domain certs.

First create a .p12 using private key and domain certificate.

openssl pkcs12 -export -in <domain_cert> -inkey
<private_key> -certfile <domain_cert> -name "<name>" -out
<p12_name>.p12

Create keystore file using

keytool -importkeystore -destkeystore <keystore_name>.keystore -srckeystore
<p12_name>.p12 -srcstoretype pkcs12 -alias <alias_name>

Then import the other certificates using

keytool -import -trustcacerts -alias root -file <cert_file> -keystore <keystore_name>.keystore

Limits (Mathamatics)


[Warning: These are more like my notes which I make public so that if at all there are some stupid people like me, these would come handy]

Mathematical definition:
A limit is the value that a function or sequence “approaches” as the input or index approaches some value.[1]

Limit of function

Mathematical Representation:

lim  ƒ(x) = L
x->c

This says that L is the value of function ƒ(x) when x reaches to value c.

There is one more definition which is kind of logical evidence to this is L is the limit of function ƒ(x) with ε as a small positive real number then value of ƒ(x) lies in (L-ε,L+ε) or ƒ(x) – L < ε

Reallife examples:
I found this amazing question

Adding the most upvoted example here

The reading of your speedometer (e.g., 85 km/h) is a limit in the real world. Maybe you think speed is speed, why not 85 km/h. But in fact your speed is changing continuously during time, and the only “solid”, i.e., “limitless” data you have is that it took you exactly 2 hours to drive the 150 km from A to B. The figure your speedometer gives you is at each instant t0 of your travel the limit

     v(t0):= lim    (t0)−s(t0−Δt)/Δt
            Δt→0s

where s(t) denotes the distance travelled up to time t.

And the most simple to understand (for me)

If I keep tossing a coin as long as it takes, how likely am I to never toss a head?

Possibility of getting a head is (1/2) if I toss the coin once.

Now if I toss N times P(N) = (1/2)^N
Now this probability would be zero if N is ∞.

Mathematical representation of the question is

P(N) = (1/2)^N find N for which P(N) = 0

and Answer to the given question is

lim  P(N)
N->∞

setOnItemClickListener() is not working…and its’ because your layout structure is wrong


This is gonna be a quick post. I spent like last 45 mins solving a very dumb issue, related to setOnItemClickListener of a custom class extending GridView.

Layout of GridView item:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="@dimen/fragment_application_grid_item_width"
                android:layout_height="@dimen/fragment_application_grid_item_height"
                android:background="@drawable/fragment_application_list_item_background_drawable"
                android:orientation="vertical">

        

<!-- Some Elements -->

        <View
            android:id="@+id/application_price_indicator"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="0.05"
            android:background="@drawable/fragment_application_grid_item_free_indicator"/>

        <ImageButton
            android:id="@+id/add_to_list_menu"
            android:layout_width="0dp"
            android:layout_height="@dimen/fragment_application_grid_item_menu_dimen"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:src="@drawable/fragment_application_grid_item_add_to_list_icon"/>

        <ImageButton
            android:id="@+id/install_app_icon"
            android:layout_width="0dp"
            android:layout_height="@dimen/fragment_application_grid_item_menu_dimen"
            android:background="@android:color/transparent"
            android:layout_weight="1"
            android:src="@drawable/fragment_application_grid_item_install_icon"/>

        <ImageButton
            android:id="@+id/share_app_icon"
            android:layout_width="0dp"
            android:layout_height="@dimen/fragment_application_grid_item_menu_dimen"
            android:background="@android:color/transparent"
            android:layout_weight="1"
            android:src="@drawable/fragment_application_grid_item_share_icon"/>
        </LinearLayout>
    </RelativeLayout>

The issue here is, ImageButton is the part of this view. So whenever onClick event happens, Android system will try to find onClickListener related to ImageButtons in the view. So There are only two solutions to this :

  1. Either change ImageButton to ImageView
  2. Override onTouch of ImageButton and return False.

Why BATTERY_STATS permission is not gonna work in KITKAT


I am working on an internal project which requires battery stats. A quick search would guide you to use android.permission.BATTERY_STATS, and you would be able to access battery stats directly. But you would notice that this code wont work in KITKAT.

Why ?

If you are coding in Android you must be familiar with the concept of permission group. Every of the android.permission.* is associated with a permissions group. And each of these permission groups have a protection level associated with them. There are six types of the protection levels.

Constant Value Description
normal A lower-risk permission that gives an application access to isolated
application-level features, with minimal risk to other applications,
the system, or the user. The system automatically grants this type
of permission to a requesting application at installation, without
asking for the user’s explicit approval (though the user always
has the option to review these permissions before installing).
dangerous 1 A higher-risk permission that would give a requesting application
access to private user data or control over the device that can
negatively impact the user. Because this type of permission
introduces potential risk, the system may not automatically
grant it to the requesting application. For example, any dangerous
permissions requested by an application may be displayed to the
user and require confirmation before proceeding, or some other
approach may be taken to avoid the user automatically allowing
the use of such facilities.
signature 2 A permission that the system is to grant only if the requesting
application is signed with the same certificate as the application
that declared the permission. If the certificates match, the system
automatically grants the permission without notifying the user or
asking for the user’s explicit approval.
signatureOrSystem 3 A permission that the system is to grant only to packages in the
Android system image or that are signed with the same
certificates. Please avoid using this option, as the
signature protection level should be sufficient for most needs and
works regardless of exactly where applications are installed. This
permission is used for certain special situations where multiple
vendors have applications built in to a system image which need
to share specific features explicitly because they are being built
together.
system 0x10 Additional flag from base permission type: this permission can also
be granted to any applications installed on the system image.
Please avoid using this option, as the
signature protection level should be sufficient for most needs and
works regardless of exactly where applications are installed. This
permission flag is used for certain special situations where multiple
vendors have applications built in to a system image which need
to share specific features explicitly because they are being built
together.
development 0x20 Additional flag from base permission type: this permission can also
(optionally) be granted to development applications.

Earlier BATTERY_STATS was having protection level as dangerous but from kitkat they have changed it to signature/system so unless your app is running as system app or your app is signed with same certificate as system apps, your app wont be able to access battery stats !!

Have a good day coders :)

Consumer Producer implementation with golang


Today I invested my whole day in evaluating GoLang for one of our component implementations. Since the first document, I got impressed by it’’’s construct. Till I reached the ultimate problem solution for our case, I fell in love with this new language. It gives you the power of OS level constructs keeping a lot of overheads hidden from you. I was playing with a lot of simple problems while reading the documentation and here I am sharing one of those implementation and will discuss how simple it is to implement pretty complex problems with GoLang.

Classic Consumer-Producer problem

You can find some classic implemetations here.

GoLang implementation:

package main
import (
	"fmt"
	"strconv" // added for giving proper name to job
	"time"    // added for delaying consecutive inserts into job queue
)
func main() {
	jobQueue := make(chan string, 200)
	done := make(chan bool,1)
	// consumer
	go func() {
		for {
			job := <-jobQueue
			fmt.Println("Got job:", job)
		}
		done <- true
	}()
	// producer
	go func() {
		i := int64(0)
		for {
			jobQueue &lt;- strconv.FormatInt(i, 10)
			time.Sleep(time.Second * 5) // to wait for 5 seconds before adding one more job
			i++
		}
		done <- true
	}()
	<-done
}

Components

Channels
Channels are the basic inter go-routine communication model. If you are a Java developer you can consider it as array blocking queue, which is mostly used for inter-thread communication.

Go routine
Go routines are the lightweight Thread implementation for Go.

You can learn Go with some amazing examples here. Initial impressions of Go are pretty good. Will share more info as we discover it during our implementations.