Wednesday, January 11, 2012

Android - Extending ListView and Instantiate with XML and AlertDialogs for itemSelect

Was doing a bit of coding and decided to demonstrate a way to create your own class which extends ListView. I wanted to manage specific code in regards to this listview and didnt want the code to be all over the place. Anywho, here is the source of a quick ListView showing names.

When you click on the items an AlertDialog box appears indicating what you clicked on.


NOTE: the XML files below should go in res/layout folder of your project.


Main.xml Make sure to make change to package name!


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

 <com.daish.viewtest.TestListView
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"/>

</LinearLayout>

row.xml This layout defines how each row is going to look like. This has a picture and name


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp" >
    </TextView>

</LinearLayout>

MainActivity (beginning of program)


import android.app.Activity;
import android.os.Bundle;

public class ViewTestActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

TestListView Class extending ListView


package com.daish.viewtest;

import android.app.AlertDialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TestListView extends ListView
{
 private ArrayAdapter<String> test;
 String[] testItems = {"Larry", "John", "Gary"};
 
 //If built programmatically
 public TestListView(Context context)
 {
  super(context);
  init();
 }
 //This example uses this method since being built from XML
 public TestListView(Context context, AttributeSet attrs)
 {
  super(context, attrs);
  init();
 }
 
 //Build from XML layout 
 public TestListView(Context context, AttributeSet attrs, int defStyle)
 {
  super(context, attrs, defStyle);
  init();
 }
 
 public void init()
 {
  test = new ArrayAdapter<String>(getContext(),R.layout.row, R.id.label , testItems);
  setAdapter(test);
  setOnItemClickListener(new ListSelection());
 }
 
 private class ListSelection implements OnItemClickListener
 {

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
    long id)
  {
   AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
   builder.setMessage("You pressed item #" + (position+1));
   builder.setPositiveButton("OK", null);
   builder.show();
  }
  
 }
}


The code above should give you this:

Friday, January 6, 2012

Recovering Cisco IP Phone 7940 / 7960

These Cisco IP phones are a pain to setup. I was given the task to setup 6 Cisco IP phones. Out of the batch 2 did not boot up to the point where we could access the settings. On the Cisco 7940 we received TFTP timeout o / A and just remained there. The other was a Cisco 7960 and gave an error Application Protocol Invalid. I was able to recover the phones by doing the following:

CAUTION: The instructions configure phone to work as SIP Phones not Skinny SCCP.

You will need the following:

  1. TFTpd32 http://tftpd32.jounin.net/
  2. Cisco IP Phone firmware (I used P0S3-8-12-00) http://radiotwenterand.nl/~graver/cisco/SIP-7960/P0S3-8-12-00.zip
  3. Cisco IP Phone configuration files http://www.minded.ca/default/wp-content/uploads/2009/12/Cisco-Config-Files.zip. If you do not know how to setup config files, check out the site http://www.minded.ca/2009-12-16/configure-cisco-ip-phones-with-asterisk/
  4. Switch
  5. Computer

Instructions:

  1. Download TFTpd32 at http://tftpd32.jounin.net/ and firmware and configuration files.
  2. Grab a computer and install TFTpd32. Unzip firmware and configuration files to a folder. To be safe, setup configuration files to have SIP[Phone MAC Address].cnf and SEP[Phone MAC Address].cnf.xml (Check website above if you don't know how to setup configs)
  3. Disconnect computer from network and plug into switch (Switch should only have computer and phone. Leave Cisco Phone powered off while you setup TFTpd32)
  4. Set the computer IP address to 192.168.1.1 MASK 255.255.255.0. Bring up TFTpd32 and go to settings.
    • Under Global Tab, only enable TFTP Server and DHCP Server.
    • Click on TFTP tab. Set Base Directory to the location of the folder where Cisco IP firmware and configuration files are. Set TFTP security to none.
    • Click on DHCP tab and set IP pool starting address to 192.168.1.2. Set pool to 5 or something like that. Default router put 192.168.1.1 mask 255.255.255.0 Additional option 66 in small box and ip 192.168.1.1 in the box next to it. Uncheck Ping Address before assignation and Persistant leases.
  5. Hit OK and Restart TFTpd32.



  6. While your TFTP is running, power on the Cisco IP phone. If all goes well, TFTpd32 will log the connection and show the files it requests.



  7. The phones will show upgrading firmware.



  8. Phones should now boot up completely once installation is complete.