Thursday 26 March 2015

HB Blog 65: Broadcast Receiver For Booting Process, Call Reciever And Sms Reciever In Android.

A BroadcastReceiver is nothing but a event capture mechanism. It can be system specific or custom intents as well. The system specific events such as event while booting, call receiving, sms receiving, battery status, etc. Programmatically, we add receiver tag in manifest with action specified for the events that is to be captured such as,
1
2
3
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.PHONE_STATE" />
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />

 We need to provide required permissions as well. Then, we receive the call to BroadcastReceiver life cycle method onReceive (Context context, Intent intent) from the class which extends BroadcastReceiver and is provided as receiver name to the receiver action in receiver tag.

Refer the below link for complete sample code:-
Download Sample Code
Download Apk File
Have a look on few code snippets,
//BootReceiver.java
1
2
3
4
5
6
7
8
public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent arg1) {
        Toast.makeText(context, "BootReceiver", Toast.LENGTH_SHORT).show();
        System.out.println("BootReceiver");
    }
}
//CallReceiver.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class CallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Toast.makeText(context, "RecieveCall", Toast.LENGTH_SHORT).show();

            // TELEPHONY MANAGER class object to register one listner
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            //Create Listner
            MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
            // Register listener for LISTEN_CALL_STATE
            telephonyManager.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        } catch (Exception e) {
            Log.e("Phone Receive Error", " " + e);
        }
    }

    /**
     * MyPhoneListener for New Phone Call Event. Incomming Number
     */
    private class MyPhoneStateListener extends PhoneStateListener {

        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == 1) {
                String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
                System.out.println("msg: "+msg);
            }
        }
    }
}
//SMSReceiver.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 public class SMSReceiver extends BroadcastReceiver {
                    String TAG = "SMSReceiver";

                    @Override
                    public void onReceive(Context context, Intent intent) {
                        String msg_from = null, msgBody = null;
                        if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
                            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
                            SmsMessage[] msgs = null;
                            if (bundle != null) {
                                //---retrieve the SMS message received---
                                try {
                                    Object[] pdus = (Object[]) bundle.get("pdus");
                                    msgs = new SmsMessage[pdus.length];
                                    for (int i = 0; i < msgs.length; i++) {
                                        msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                                        msg_from = msgs[i].getOriginatingAddress();
                                        msgBody = msgs[i].getMessageBody();
                                        Toast.makeText(context, "SMSReceiver msg_from " + msg_from + " msgBody " + msgBody, Toast.LENGTH_SHORT).show();
                                    }
                                } catch (Exception e) {
                                e.printStackTrace();
                    }
              }
          }
      }
  }
//AndroidManifest.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.broadcastreciever">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- SMS Recevier -->
        <receiver android:name="recievers.SMSReceiver">
            <intent-filter android:priority="2147483647">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

        <!-- Boot Recevier -->
        <receiver
            android:name="recievers.BootReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

        <!-- Call Recevier -->
        <receiver android:name="recievers.CallReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

    </application>
</manifest>

Monday 23 March 2015

HB Blog 64: REPL (Read-Eval-Print-Loop) Environment In iOS Playground File.

A read–eval–print loop (REPL), also known as an interactive toplevel or language shell, is a simple, interactive computer programming environment that takes single user inputs (i.e. single expressions), evaluates them, and returns the result to the user; a program written in a REPL environment is executed piecewise.
In a REPL, the user enters one or more expressions (rather than an entire compilation unit) and the REPL evaluates them and display the results.
    The read function accepts an expression from the user, and parses it into a data structure in memory. For instance, the user may enter the s-expression (+ 1 2 3), which is parsed into a linked list containing four data elements.
    The eval function takes this internal data structure and evaluates it. In Lisp, evaluating an s-expression beginning with the name of a function means calling that function on the arguments that make up the rest of the expression. So the function + is called on the arguments 1 2 3, yielding the result 6.
    The print function takes the result yielded by eval, and prints it out to the user. If it is a complex expression, it may be pretty-printed to make it easier to understand. In this example, though, the number 6 does not need much formatting to print.

Most recent example is Playground file. Without requiring you to compile and run a complete project, a playground provides quick feedback for the results of your coding experiments. You just need to select a .playground file in the project navigator in xcode, or create a .playground file by choosing File > New > File and selecting the Playground source template for your ios platform. Enter the code into the .playground file, and examine the results in the sidebar. The quick look option opens assistant editor which will open with a timeline view, containing a graph of these values over time.

Below  is the exapmle for fibonacci series in playground in swift language on iOS platform.

Monday 16 March 2015

HB Blog 63: Virtualization Defined.

Virtualization, in computing, refers to the act of creating a virtual (rather than actual) version of something, including but not limited to a virtual computer hardware platform, operating system (OS), storage device, or computer network resources. Following are the various levels of virtualization:

Hardware virtualization or platform virtualization refers to the creation of a virtual machine that acts like a real computer with an operating system.
Different types of hardware virtualization include:
    Full virtualization - It is almost a complete simulation of the actual hardware to allow software, which typically consists of a guest operating system, to run unmodified.
    Partial virtualization - It is some but not all of the target environment is simulated. Some guest programs, therefore, may need modifications to run in this virtual environment.
    Paravirtualization - It is a hardware environment is not simulated; however, the guest programs are executed in their own isolated domains, as if they are running on a separate system. Guest programs need to be specifically modified to run in this environment.

Desktop virtualization is the concept of separating the logical desktop from the physical machine.
One form of desktop virtualization, virtual desktop infrastructure (VDI), can be thought of as a more advanced form of hardware virtualization. 

Software virtualization allows a single host computer to create and run one or more virtual environments.
Different types of software virtualization include:
    Operating system-level virtualization -  It is hosting of multiple virtualized environments within a single OS instance.
    Application virtualization and workspace virtualization - It is the hosting of individual applications in an environment separated from the underlying OS. Application virtualization is closely associated with the concept of portable applications.
    Service virtualization -  It is an emulating the behavior of dependent (e.g., third-party, evolving, or not implemented) system components that are needed to exercise an application under test (AUT) for development or testing purposes. Rather than virtualizing entire components, it virtualizes only specific slices of dependent behavior critical to the execution of development and testing tasks.

Memory virtualization decouples volatile random access memory (RAM) resources from individual systems in the data center, and then aggregates those resources into a virtualized memory pool available to any computer in the cluster.

Storage virtualization is the process of completely abstracting logical storage from physical storage.
Different examples of storage virtualization include:
Distributed file system - It is any file system that allows access to files from multiple hosts sharing via a computer network.
Virtual file system -  It is an abstraction layer on top of a more concrete file system, allowing client applications to access different types of concrete file systems in a uniform way.
Storage hypervisor - It is the software that manages storage virtualization and combines physical storage resources into one or more flexible pools of logical storage.
Virtual disk drive -  It is a computer program that emulates a disk drive such as a hard disk drive or optical disk drive (see comparison of disc image software).

Data virtualization is the presentation of data as an abstract layer, independent of underlying database systems, structures and storage. Database virtualization is a the best example of data virtualization.

Network virtualization is creation of a virtualized network addressing space within or across network subnets.
Virtual private network (VPN), a network protocol that replaces the actual wire or other physical media in a network with an abstract layer, allowing a network to be created over the Internet.

Monday 9 March 2015

HB Blog 62: Firewall And Its Types.

Firewall is a hardware or software that protects from intrusions from attackers. It examine all the data packets passing through them to see if they neet the rules defined by the ACL(Access Control List) made by the administration of the network. Firewall also maintain a log of important activities in the network. The log can be configure as per required. It also filters contents on the basis of address, protocol, packet attributes and state.

The types of firewall are as follows:-
Packet filtering firewall
Circuit level gateway firewall
Application level gateway firewall
Stateful multilayer inspection firewall
Packet filtering firewall:-
Packet filtering firewalls are deployed on routers which connect the Internet network to Internet. It can be only be implemented on network layer of osi model. It works on the basis of rules defines by Access Control Lists. They check all the packets and screens them against the rules as per the ACL. If the packet is not match the criteria then that packet is dropped and logs are updated. The ACL are created on the basis of address, protocol, packet attributes and state.
Circuit level gateway firewall:-
Circuit level gateways are deployed at the session layer of OSIodel and they monitor sessions like TCP three way and handshake to see whether a requested connection is legitimate or not. Major screening happens before the connection is established. Information sent to a computer is secure and appears to be sent from the gateway.
Application level gateway firewall:-
Application level gateways works on the application layer of OSI model and provide protection for specific Application layer protocol. It only works for configured protocols. It can also be configured as Caching servers which in turn increase the network performance and makes it easier to log traffic.
Stateful multilayer inspection firewall:-
Stateful multilayer inspection firewall is combination of all firewalls. They can filter packets at network layer using ACL, check for legitimate sessions on the session layer and thry also evaluate packets on the application layer. They can work on a transparent mode allowing direct connections between the client and the server. It can also implement algorithms and complex security models which are protocol specific, making the connection and data transfer more secure.

Monday 2 March 2015

HB Blog 61: How To Publish Android Application On Google Play.

In this post, I will show how to publish android application on Google play.
Publishing is the general process that makes your Android applications available to users. Before you publish your apps on Google Play and distribute them to users, you need to get the apps ready, test them, and prepare your promotional materials as well as go thorough few forms and signups.
Preparing your application for release is a multi-step process that involves the following tasks:
1)Building and signing a release version of your application:-
Firstly, build your application which includes standard of designed user interface, standard coding  development and android permissions as per requirement, testing phase, etc. Few pre - build process such as remove log calls, remove the android:debuggable attribute from your manifest file,  test all services, add analytics or error logs if required, provide values for the android:versionCode and android:versionName attributes, then building and signing a release version of your application. As an output signed .apk file is generated.

2) Register for a Publisher Account:-
Visit the Google Play Developer Console. Go through the signup process which includes basic information about your developer identity - name, email address, etc., add up promotional materials, screenshots, description, etc. Finally read and accept the Developer Distribution Agreement by paying a $25 USD registration fee.

3)Uploading your signed apk file:-
Now, upload your first signed apk. It looks so simple but the real job starts now. After uploading your apk do keep in mind that the application will be used by number of users all over the countries you have selected in registration form, so do test the application after it is live on Google play and verify if all things are on rigth path.

Happy publishing... :)