DublinBuzz - WordPress.com

wellofflimpetMobile - Wireless

Dec 14, 2013 (3 years and 5 months ago)

64 views

package com.tony.buzz;


import java.text.DecimalFormat;

import java.text.NumberFormat;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;


import android.app.Activity;

import android.app.AlertDialo
g;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.SharedPreferences;

import android.graphics.Color;

import android.location.*;

import android.os.Bundle;

import android.view.View
;

import android.view.View.OnClickListener;

import android.widget.*;


/**


* The start activity for Dublin Buzz. Shows main list of sights ordered and filtered.


*


* @author Tony Scannell


* @version 1.0


*/

public class DublinBuzz extends Activity {


/
**



* The list of currently viewable sights, decided by user preferences and changed constantly



*/


private List<Sight> viewable = null;


private Location currentLocation = null;


private String provider = "";




// Location updating handling


private L
ocationListener updater = null ;


private static final long timeBetweenUpdates = 10000; // milliseconds between updates


private static final float distanceBeforeUpdates = 10; // meters moved before needing updates


private static DublinBuzzer app = null;




@Override


public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.main);


app = DublinBuzzer.getInstance();


}




/**



* onStart() runs when screen redisplayed. Alwa
ys refresh the best provider, to see if a new one available.



* Registers for location updates.



* Displays the sights in preferred order of user.



*/


@Override


public void onStart ()


{



super.onStart();



setupLocationProvider();




refreshView();


}



/**



*



*/


private void setupLocationProvider() {



provider = app.loc.getBestProvider(getLocationProviderCriteria(), true);



if (provider==null)



{




Button b = (Button)findViewById(R.id.DistanceButton);




b.setTe
xtColor(Color.DKGRAY);




b.setEnabled(false);



}



else



{




currentLocation = app.loc.getLastKnownLocation(provider);




if (currentLocation != null)




{





for (Sight s : app.sights)






s.refreshDistanceFromCurrentLocation(cu
rrentLocation);




}



}



// We have a location provider, so now create and add a listener for updating



// periodically. We can then refresh distance too.



updater = new LocationListener() {




public void onLocationChanged (Location loc
ation){





currentLocation = location ;





for (Sight s : app.sights)






s.refreshDistanceFromCurrentLocation(location);





if (app.preferredButton==R.id.DistanceButton)






ShowDistance(null);





else






displaySights(null);




}




public void onProviderDisabled(String provider)




{

}




public void onProviderEnabled(String provider)




{

}




public void onStatusChanged(String provider, int status, Bundle extras){}



};



if (provider !=null)



{





app.loc.requestLocationUpdates(provider, timeBetweenUpdates, distanceBeforeUpdates,
updater);



}



}



/**



* Refreshes the current view after a significant change



*/


private void refreshView() {




changeButtonColours(app.preferredButton);



if (app.preferredButton == R.id.FavouriteButton)



{




viewable = getFavourites();




displaySights(null);



}



else



{




viewable = getDisplaySights(app.sights);




if (app.preferredButton==R.id.AToZButton)





ShowAToZ(find
ViewById(R.id.AToZButton));




if (app.preferredButton==R.id.DistanceButton)





ShowDistance(findViewById(R.id.DistanceButton));




if (app.preferredButton==R.id.PopularityButton)





ShowPop(findViewById(R.id.PopularityButton));



}


}



/**



* A set

of criteria for getting location
-
based provider.



* We ideally want accuracy (GPS) for the distance measurements to be meaningful.



* Altitude and bearing information not required.



* This set of criteria should return GPS positioning, when available
, or the network if not.



*



* @return Set of criteria for choosing a location provider



*/


private Criteria getLocationProviderCriteria()


{


// Sets up standard location provider criteria


Criteria locationProviderCriteria = new Criter
ia();


locationProviderCriteria.setAccuracy(Criteria.ACCURACY_FINE);


locationProviderCriteria.setAltitudeRequired(false);


locationProviderCriteria.setBearingRequired(false);


locationProviderCriteria.setCostAllowed(true);



locationProviderCriteria.setPowerRequirement(Criteria.POWER_HIGH);


return locationProviderCriteria;


}






/**


* Displays all sights according to ordering and filtering preferences


* @param display


*/


public void displayS
ights (Comparator<Sight> compare)


{


// The order this list by ordering preferences, passed via the comparator, if specified


if (compare != null)



Collections.sort(viewable, compare);




TextView order = (TextView)f
indViewById(R.id.grouped);


order.setText(getString(SightCategory.getDescription(app.preferredFilter)));



TableLayout table = (TableLayout)findViewById(R.id.table);


table.removeAllViews();


// Add the sights to the table view


for (final Sight s: viewable)


{




TableRow r = new TableRow(this);




r.setPadding(1, 2, 1, 2);




final Button v = new Button(this);




final Context c = this;




v.setPadding(1, 2, 1, 2);




v.setTextColor(Color.BLUE);




v.setB
ackgroundColor(Color.WHITE);




v.setTextSize(16);




if (app.loc!=null && provider!=null && (int)s.getLatestDistance()!=0)




{





int distance = (int)s.getLatestDistance();





if (distance >= 1000)





{






float kms = distance ;

// turn meters into kms






kms = distance ;






kms /= 1000 ;






NumberFormat df = new DecimalFormat("#0.00");






v.setText(s.getName() + " (" + df.format(kms) + "km)");





}





else





{






v.setText(s.getName(
) + " (" + (int)s.getLatestDistance() + "m)" );





}




}




else





v.setText(s.getName());




v.setClickable(true);




v.setOnClickListener(new OnClickListener() {





@Override





public void onClick(View arg0) {






Intent laun
ch = new Intent(c, SightViewer.class);






launch.putExtra("Sight", s);






startActivity(launch);






}});








r.addView(v);




table.addView(r);



}


}



/**


* Removes location listener when not displaying


*/


@Override


public void onPause ()


{



super.onPause();



if (app.loc != null && updater != null)




app.loc.removeUpdates(updater);


}



/**


* Displays a dialog with all categories for user to choose a filter on sights


* @p
aram The button for selecting type


*/


public void ShowType (View v)


{



final CharSequence[] items = new CharSequence[SightCategory.values().length];



int i = 0;



for (SightCategory s: SightCategory.values())




items[i++] = getS
tring(SightCategory.getDescription(s));




AlertDialog.Builder builder = new AlertDialog.Builder(this);



builder.setTitle(R.string.chooseType);



builder.setSingleChoiceItems(items,
-
1, new DialogInterface.OnClickListener() {




public void

onClick(DialogInterface dialog, int item) {





app.preferredFilter = SightCategory.getCategory(item);




}



});



builder.setNegativeButton(R.string.ok, new DialogInterface.OnClickListener() {


public void onClick(DialogInte
rface dialog, int id) {


dialog.cancel();





refreshView();


}



});



builder.setCancelable(true);



AlertDialog alert = builder.create();



alert.show();


}



/**


* User clicked the Favourites button
.


* The viewable sights are just the registered favourites


* @param v


*/


public void ShowFavs (View v)


{



viewable = getFavourites();



changeButtonColours(R.id.FavouriteButton);



displaySights (null);


}



/**



* User clicked popularity button.


* Sorts the list by popularity criteria (hard
-
coded into every sight)


* @param v


*/


public void ShowPop (View v)


{



viewable = getDisplaySights(app.sights);



changeButtonColours(R.id.Popular
ityButton);



displaySights (new Comparator<Sight>() {




public int compare(Sight p1, Sight p2) {





if (p1.getPopularity() > p2.getPopularity())






return 1;





if (p1.getPopularity() < p2.getPopularity())






return
-
1;





ret
urn 0;




}



});


}



/**


* User clicked distance button


* Sorts by distance, nearest to furthest.


* @param v


*/


public void ShowDistance (View v)


{



viewable = getDisplaySights(app.sights);



changeButtonColou
rs(R.id.DistanceButton);


displaySights (new Comparator<Sight>(){


public int compare(Sight p1, Sight p2) {


if (p1.getLatestDistance() > p2.getLatestDistance())



return 1;


if (p1.getLatestDi
stance() < p2.getLatestDistance())



return
-
1;


return 0;





}


});


}



/**



* Shows all sights in alphabetical order



* @param v



*/


public void ShowAToZ (View v)


{



viewa
ble = getDisplaySights(app.sights);



changeButtonColours(R.id.AToZButton);


displaySights(new Comparator<Sight>(){


public int compare(Sight p1, Sight p2) {


return (p1.getName().compareTo(p2.getName()));


}


});


}



/**


* Gets the current list of user favourites


* @return


*/


private List<Sight> getFavourites()


{



List<Sight> favourites = new ArrayList<Sight>();



SharedPreferences favs = this.getSharedPreferences("M
yPrefs", MODE_WORLD_READABLE);



for (Sight s: app.sights)



{




int result = favs.getInt(s.getName(), 0);




if (result==1)





favourites.add(s);



}



return favourites;


}




/**


* Filters down the sights to a specif
ic category only


* @param sights


*/


private List<Sight> getDisplaySights(List<Sight> sights)


{



List<Sight> displaySights = new ArrayList<Sight>();



for (Sight s : sights)



{




if (app.preferredFilter == SightCategory.ALL || app.p
referredFilter == s.getType())





displaySights.add(s);



}



return displaySights;


}



/**



* Colours text on buttons to indicate which one was pressed.



* And saves choice of user into preferredButton



* @param pressed



*/


private void ch
angeButtonColours (int pressed)


{



app.preferredButton = pressed;



Button bb = (Button)findViewById(R.id.AToZButton);



bb.setTextColor(Color.WHITE);



if (pressed == R.id.AToZButton)




bb.setTextColor(Color.GREEN);



if (app.loc != null && provider !=

null)



{




bb = (Button)findViewById(R.id.DistanceButton);




bb.setTextColor(Color.WHITE);




if (pressed == R.id.DistanceButton)





bb.setTextColor(Color.GREEN);



}



bb = (Button)findViewById(R.id.FavouriteButton);



bb.setTextColor(Color.WHITE);



if (pressed == R.id.FavouriteButton)




bb.setTextColor(Color.GREEN);



bb = (Button)findViewById(R.id.PopularityButton);



bb.setTextColor(Color.WHITE);



if (pressed == R.id.PopularityButton)




bb.setTextColor(Color.GREEN);


}


}