DublinBuzz - WordPress.com

shrewdnessmodernMobile - sans fil

14 déc. 2013 (il y a 3 années et 8 mois)

73 vue(s)

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.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.SharedPreferences;

import android.graphics.Color;

im
port 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;


/**



* Whatever is thought to be the la
st available location



*/


private Location currentLocation = null;


/**



* Typically, we want GPS provider, but might get Network instead



*/


private String provider = "";




/**



* Location updating handling



*/


private LocationListener 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;




/**



* Sets up the ac
tivity



*/


@Override


public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.main);


app = DublinBuzzer.getInstance();


}




/**



* onStart() runs when screen redisp
layed. Always 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();



setupLocationProvid
er();




refreshView();


}



/**



* Sets up the providing of location information for the user



*/


private void setupLocationProvider() {



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



if (provider==null)



{




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




b.setTextColor(Color.DKGRAY);




b.setEnabled(false);



}



else



{




currentLocation = app.loc.getLastKnownLocation(provider);




if (currentLocation != null)




{





for (
Sight s : app.sights)






s.refreshDistanceFromCurrentLocation(currentLocation);




}



}



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



// periodically. We can then refresh distance too.



updater = new L
ocationListener() {




public void onLocationChanged (Location location){





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



*/


priva
te void refreshView() {



changeButtonColours(app.preferredButton);



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



{




viewable = getFavourites();




displaySights(null);



}



else



{




viewable = getDisplaySights(app.sigh
ts);




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





ShowAToZ(findViewById(R.id.AToZButton));




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





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




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





Show
Pop(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 provi
der criteria


Criteria locationProviderCriteria = new Criteria();


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 compare A Comparator used to sort the sights


*/


public void displaySights (Comparator<Sight> compare)


{


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


if (comp
are != null)



Collections.sort(viewable, compare);




TextView order = (TextView)findViewById(R.id.grouped);


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



TableLayout table = (TableLayou
t)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.setBackgroundColor(Color.WHITE);




v.setTextSize(16);




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




{





int dis
tance = (int)s.getLatestDistance();





if (distance >= 1000)





{






float kms = distance ; // turn meters into kms






kms /= 1000 ;






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






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





}





else





{






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





}




}




else





v.setText(s.getName());




v.setClickable(true);




v.setOnClickListener(new OnClickList
ener() {





@Override





public void onClick(View arg0) {






Intent launch = 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);


}



/**


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


* @param 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++] = getString(SightCategory.getDescription(s));




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



builder.setTitle(R.string.chooseType);



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




public void onClick(DialogInterface dialog, int item) {





app.preferredFilter = SightCategory.getCategory(item);




}



});



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


public void onClick(DialogInterface 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();



changeButt
onColours(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.PopularityButton);



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;





return 0;




}



});


}



/**


* User clicked distance button


* Sorts by distance, nearest to furthest.


* @param v


*/


public void ShowDistance (Vie
w v)


{



viewable = getDisplaySights(app.sights);



changeButtonColours(R.id.DistanceButton);


displaySights (new Comparator<Sight>(){


public int compare(Sight p1, Sight p2) {


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



return 1;


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



return
-
1;


return 0;





}


});


}



/**



* Shows all sights in alphabet
ical order



* @param v



*/


public void ShowAToZ (View v)


{



viewable = 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 = ne
w ArrayList<Sight>();



SharedPreferences favs = this.getSharedPreferences("MyPrefs",
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 specific category only


* @param sights


*/


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


{



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



for (Sigh
t s : sights)



{




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





displaySights.add(s);



}



return displaySights;


}



/**



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



* And sa
ves choice of user into preferredButton



* @param pressed



*/


private void changeButtonColours (int pressed)


{



app.preferredButton = pressed;



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



bb.setTextColor(Color.WHITE);



if (pressed == R.id.A
ToZButton)




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);


}


}