20-CS-6053 Network Security Spring 2017
Android Samples

Secret Key, Public Key, Hash Algorithms, IPSec, Kerberos, Authentication, more

Several Examples of Android Code

Note: These are taken from numerous examples that can be found on the internet. Unfortunately, I cannot remember all of these sources but they are easy to find with a google search.

A popup: A simple app that creates a new LinearLayout for the popup and populates it with a TextView and a Button.
     See PopActivity.java source
See AndroidManifest.xml manifest
     See popup_layout.xml for the layout.
     Layout is determined by LinearLayout class
Inheritied from LayoutParms class
LinearLayout constructor takes the Context it is associated with.
See Context class.
See Activity class.
See LinearLayout page for alternative xml annotations.
addView comes from ViewGroup class.

Check network state: simply check the network state
      See NetActivity.java source
See AndroidManifest.xml manifest
See network_layout.xml for the layout.
Uses the ConnectivityManager class.
See getSystemService(...) in the java code
It is a method of the Context class which is subclassed by Activity
In the "Contants" section note TYPE_MOBILE - click on it
It gets used in getNetworkInfo of the ConnectivityManager class
Click on NetworkInfo - look at getDetailedState - look at list of states
Look at AndroidManifest.xml for permissions
Look at the possible states in Manifest.permissions.
Here is how you would create your own permission:
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.me.app.myapp" >
      <permission android:name="com.me.app.myapp.permission.DEADLY_ACTIVITY"
          android:protectionLevel="dangerous" />

Check network state: but leave permissions out of AndroidManifest.xml
      Try the app - press the button
Do a dmesg to see what SEAndroid is up to

Motivation for a background service: using a service in the same thread as the activity
causes performance problems.
      See TryAActivity.java source
See AndroidManifest.xml manifest
See trya_layout.xml for the layout.
Note that the app is blank for 30 seconds

Background Service: the service is to echo a string with the current time and day.
The app prints the string to a EditText widget.
      See SrvActivity.java source
See Servicer.java source
See AndroidManifest.xml manifest
See srv_layout.xml for the layout.
Look at onClick in SrvActivity:
    A new Intent is constructed
see Intent class
Note the constructor: (Context, Class) = (implementer, service class)
   (example of explicit intent)
Note putExtra(String name, String value):
   name must include package prefix
   value is the data to be sent to the service
Note: startService is a method of the Context class
Note the Servicer class is not called directly
Look at onCreate in SrvActivity:
    Registers an IntentFilter and a BroadcastReceiver.
Note constructor IntentFilter(String): the string is an action name
See CATEGORY_DEFAULT for an explanation
See instantiation of the BroadcastReceiver
See registration of filter with receiver
Look at GetResponse in SrvActivity:
    Note: onReceive picks up the intent that is sent by the Servicer
Note: gets the string from the Servicer, puts it in the EditView
Look at the Servicer:
    Note: this class extends the IntentService class
Note: setAction, setCategory (must match with that of the requestor)
Note: putExtra: key plus a string
Note: sendBroadcast
Look at the AndroidManifest.xml:
    Look at the <service /> section
Note the "true" for export
Note the "permission" required

Binder example: for binding a service to an Activity
      See BinderActivity.java source
See BoundService.java source
See AndroidManifest.xml manifest
See binder_layout.xml for the layout.
Look at onCreate in the BinderActivity class
    bindService is a method of the Context class
The connection (middle arg to bindService) is a ServiceConnection
Note: onServiceConnected in ServiceConnection: an IBinder is sent back
   and the service is pulled from the IBinder and saved
Type of saved object is LocalBinder defined in BoundService
Look at BoundService:
    LocalBinder provides a way to get the service
With the service, the getCurrentTime method can be invoked
Look at the AndroidManifest.xml:
    Note the service is provided