package com.mobed.ssn;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ssnView extends Activity
{
private boolean isBinded = false;
/*Debugging*/
private static String LOG_SMARTNET = "SSN-Main-ACTIVITY";
/* ACTIVTY TYPES & STATUS */
public static final int ACTIVITY_START = 1;
public static final int ACTIVITY_RESUME = 2;
public static final int ACTIVITY_PAUSE = 3;
public static final int ACTIVITY_STOP = 4;
public static int STATUS = 0;
private TextView motionType, motionInfo, landmarkUUID, landmarkTime, landmarkLatitude, landmarkLongitude;
private ListView progressListView;
private SmartNetBroadcastReceiver receiver = new SmartNetBroadcastReceiver();
private IntentFilter contextFilter = new IntentFilter(Common.LANDMARK_FILTER);
private ArrayAdapter<String> progressArrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
motionType = (TextView) findViewById(R.id.motionType);
motionInfo = (TextView) findViewById(R.id.motionInfo);
landmarkUUID = (TextView) findViewById(R.id.landmarkUUID);
landmarkTime = (TextView) findViewById(R.id.landmarkTime);
landmarkLatitude = (TextView) findViewById(R.id.landmarkLatitude);
landmarkLongitude = (TextView) findViewById(R.id.landmarkLongitude);
progressArrayAdapter = new ArrayAdapter<String>(this, R.layout.landmark_theme);
progressListView = (ListView) findViewById(R.id.CurrentProgress);
progressListView.setCacheColorHint(00000000);
progressListView.setDivider(null);
progressListView.setAdapter(progressArrayAdapter);
motionType.setText("Unknwon");
// start SSN Service
startService(new Intent(this, ssnService.class));
}
@Override
public void onStart()
{
Log.i(LOG_SMARTNET, "onStart");
registerReceiver(receiver, contextFilter);
STATUS = ACTIVITY_START;
super.onStart();
}
public class SmartNetBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
int IntentContent = intent.getIntExtra("IntentContent",0);
switch(IntentContent)
{
case Common.INTENT_MOTION:
long motionDuration = intent.getLongExtra("MotionDuration", 0);
String motionDurationString = Common.longToTime(motionDuration);
//Log.i(LOG_SMARTNET, motionDurationString);
switch(intent.getIntExtra("MotionType", 0))
{
case Common.ACTIVITY_MOVE:
motionType.setText("MOVING");
motionInfo.setText(motionDurationString);
break;
case Common.ACTIVITY_STAY:
motionType.setText("STATIONARY");
motionInfo.setText(motionDurationString);
break;
}
break;
case Common.INTENT_CURRENT_LANDMARK:
Log.i(LOG_SMARTNET,"-----------New LandmarkBroadcast Received from Service-------------");
String currentLandmarkUUID = intent.getStringExtra("CurrentLandmarkUUID");
double currentLandmarkLatitude = intent.getDoubleExtra("CurrentLandmarkLatitude", 0.0);
double currentLandmarkLongitude = intent.getDoubleExtra("CurrentLandmarkLongitude", 0.0);
String currentLandmarkTime = intent.getStringExtra("CurrentLandmarkUpdateTime");
int currentLandmarkVisited = intent.getIntExtra("CurrentLandmarkVisited", 0);
String uuid;
if (currentLandmarkUUID != null)
uuid = currentLandmarkUUID;
else
uuid = "Unknown";
landmarkUUID.setText(uuid.toUpperCase());
landmarkTime.setText(currentLandmarkTime + " / " + currentLandmarkVisited);
String lat = Double.toString(currentLandmarkLatitude);
if (lat.length() > 10)
lat=lat.substring(0, 9);
String lng = Double.toString(currentLandmarkLongitude);
if (lng.length() > 10)
lng = lng.substring(0,9);
landmarkLatitude.setText(lat);
landmarkLongitude.setText(lng );
break;
case Common.INTENT_CURRENT_PROGRESS:
Log.i(LOG_SMARTNET,"+++++++++++++++++New Progress Received+++++++++++++++++++++");
if(intent.getStringExtra("ProgressString").equals("Start"))
progressArrayAdapter.clear();
else
progressArrayAdapter.add(intent.getStringExtra("ProgressString"));
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu mainMenu)
{
Log.v(LOG_SMARTNET, "Create Menu");
mainMenu.clear();
MenuItem subitem;
mainMenu.setQwertyMode(true);
subitem = mainMenu.add(0, Common.MENU_LANDMARKS, 0 ,"Landmarks");
subitem.setIcon(android.R.drawable.ic_menu_agenda);
subitem = mainMenu.add(0, Common.MENU_NEIGHBORS, 0 ,"Neighbors");
subitem.setIcon(R.drawable.ic_menu_allfriends);
subitem = mainMenu.add(0, Common.MENU_SETTINGS, 0 ,"Settings");
subitem.setIcon(android.R.drawable.ic_menu_preferences);
subitem = mainMenu.add(0, Common.MENU_HELP, 0, "Help");
subitem.setIcon(android.R.drawable.ic_menu_help);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
Log.v(LOG_SMARTNET, "Select Menu Item");
switch (menuItem.getItemId())
{
case Common.MENU_NEIGHBORS:
startActivity(new Intent(this,NeighborListActivity.class));
return true;
case Common.MENU_SETTINGS:
startActivity(new Intent(this, SettingsActivity.class));
return true;
case Common.MENU_HELP:
//startActivity(new Intent(this, helpActivity.class));
return true;
case Common.MENU_LANDMARKS:
startActivity(new Intent(this,LandmarkListActivity.class));
return true;
}
return false;
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
Log.i(LOG_SMARTNET, "onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}
@Override
public void onResume() {
Log.i(LOG_SMARTNET, "onResume");
STATUS = ACTIVITY_RESUME;
bindService();
super.onResume();
}
@Override
public void onPause() {
Log.i(LOG_SMARTNET, "onPause");
STATUS = ACTIVITY_PAUSE;
super.onPause();
}
private ServiceConnection serviceConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
Log.i(LOG_SMARTNET, "Service bound");
RemoteSSNService remoteConnectionToService = (RemoteSSNService)service;
try
{
Log.i(LOG_SMARTNET, "Remote method: updateLandmark2ssnView() called!!!");
remoteConnectionToService.updateLandmark2ssnView();
}
catch(RemoteException e)
{
Log.i(LOG_SMARTNET, "Remote Exception happenned");
throw new RuntimeException(e);
}
}
@Override
public void onServiceDisconnected(ComponentName arg0)
{
Log.i(LOG_SMARTNET, "Service Unbound");
}
};
private void bindService()
{
Intent i = new Intent(this, ssnService.class);
bindService(i, serviceConnection, Context.BIND_AUTO_CREATE);
isBinded = true;
}
private void unbindService()
{
if(isBinded)
{
unbindService(serviceConnection);
isBinded = false;
}
}
@Override
public void onRestart()
{
Log.i(LOG_SMARTNET, "onRestart");
bindService();
super.onRestart();
}
@Override
public void onStop() {
Log.i(LOG_SMARTNET, "onStop");
unregisterReceiver(receiver);
STATUS = ACTIVITY_STOP;
super.onStop();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
Log.i(LOG_SMARTNET, "onSaveInstanceState");
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onDestroy() {
Log.i(LOG_SMARTNET, "onDestroy");
unbindService();
super.onDestroy();
}
}
|