package cornell.cs2046.facebookevent; import java.io.IOException; import java.util.List; import java.util.Locale; import org.apache.http.message.BasicNameValuePair; import org.json.JSONObject; 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.Bitmap; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.SystemClock; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import cornell.cs2046.facebookevent.ImgDownloadTask.ImgResultProcessor; /** * Displays some details about the event. */ public class FacebookEventActivity extends Activity { public static final String EVENT_URL = "https://graph.facebook.com/143981188983163"; private String accessToken; private SharedPreferences mPrefs; private String mLocation; private void updateLocation(Location loc) { if (loc == null) { return; } Geocoder gcd = new Geocoder(FacebookEventActivity.this, Locale.getDefault()); List
addresses = null; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); } catch (IOException e) { e.printStackTrace(); } if (addresses != null && addresses.size() > 0) { mLocation = addresses.get(0).getLocality(); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set a location listener to fetch location in background. LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); updateLocation(manager.getLastKnownLocation(LocationManager.GPS_PROVIDER)); LocationListener listener = new LocationListener() { @Override public void onLocationChanged(Location loc) { updateLocation(loc); } @Override public void onProviderDisabled(String arg0) {} @Override public void onProviderEnabled(String arg0) {} @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) {} }; manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); mPrefs = getSharedPreferences(AuthActivity.PREFS_FILE, Context.MODE_PRIVATE); accessToken = Uri.encode(mPrefs.getString(AuthActivity.PREF_AUTH_CODE, null)); if (accessToken == null) { // Obtain an initial access token. startActivityForResult(new Intent(this, AuthActivity.class), 0); } else { new APIFetcherTask(this, new APIFetcherTask.APIResultProcessor() { @Override public void processResult(JSONObject result) { if (result == null) { TextView err = new TextView(FacebookEventActivity.this); err.setText("Error accessing Facebook API: Connection failed."); setContentView(err); } else { JSONObject obj = result.optJSONObject("error"); if (obj != null) { if ("OAuthException".equals(obj.optString("type"))) { // Authentication error - call AuthActivity. startActivityForResult(new Intent(FacebookEventActivity.this, AuthActivity.class), 0); } else { // Unknown error. TextView err = new TextView(FacebookEventActivity.this); err.setText("Unknown error accessing Facebook API."); setContentView(err); } } else { // Fetch successful - populate data. populateData(result); } } } }).execute(EVENT_URL + "?access_token=" + accessToken); } } @Override public void onActivityResult(int requestCode, int returnCode, Intent i) { if (returnCode == RESULT_CANCELED) { // Authentication failed, show error. TextView err = new TextView(this); err.setText("Error authenticating with Facebook API."); setContentView(err); return; } // Authentication was successful - retry fetching/populating data. accessToken = Uri.encode(mPrefs.getString(AuthActivity.PREF_AUTH_CODE, null)); new APIFetcherTask(this, new APIFetcherTask.APIResultProcessor() { @Override public void processResult(JSONObject result) { if (result == null) { TextView err = new TextView(FacebookEventActivity.this); err.setText("Error accessing Facebook API: Connection failed."); setContentView(err); } else { JSONObject obj = result.optJSONObject("error"); if (obj != null) { // Unknown error. TextView err = new TextView(FacebookEventActivity.this); err.setText("Unknown error accessing Facebook API."); setContentView(err); } else { // Fetch successful - populate data. populateData(result); } } } }).execute(EVENT_URL + "?access_token=" + accessToken); } public void populateData(JSONObject obj) { setContentView(R.layout.info_layout); ((TextView) findViewById(R.id.eventTitle)).setText(obj.optString("name")); ((TextView) findViewById(R.id.eventDescription)).setText("Description: " + obj.optString("description")); // Fetch the profile image - delay 3 seconds to give authentication time to propagate. new Handler().postAtTime(new Runnable() { @Override public void run() { task.execute(EVENT_URL + "/picture?access_token=" + accessToken + "&type=large"); } }, SystemClock.uptimeMillis() + 1000*3); fetchNewsPost(); ((Button) findViewById(R.id.postMessage)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder alert = new AlertDialog.Builder(FacebookEventActivity.this); alert.setTitle("Enter message:"); final EditText input = new EditText(FacebookEventActivity.this); alert.setView(input); alert.setPositiveButton("Submit", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { new APIPusherTask(FacebookEventActivity.this, EVENT_URL + "/feed", new APIPusherTask.APIResultProcessor() { @Override public void processResult() { fetchNewsPost(); } }).execute(new BasicNameValuePair("access_token", accessToken), new BasicNameValuePair("message", input.getText().toString())); } }); alert.setNegativeButton("Cancel", null); alert.show(); } }); } /** * Fetch the latest news post and display it on the screen. */ public void fetchNewsPost() { final TextView latest = (TextView) findViewById(R.id.latestPost); new APIFetcherTask(this, new APIFetcherTask.APIResultProcessor() { @Override public void processResult(JSONObject result) { if (result == null) { latest.setText("Error accessing Facebook API: Connection failed."); } else { JSONObject obj = result.optJSONObject("error"); if (obj != null) { // Unknown error. latest.setText("Unknown error accessing Facebook API."); } else { // Fetch successful - populate data. StringBuilder msg = new StringBuilder("Last Post: "); try { JSONObject post = result.getJSONArray("data").getJSONObject(0); msg.append(post.getString("message")); msg.append(" (Posted by: "); msg.append(post.getJSONObject("from").getString("name")); msg.append(")"); } catch (Exception e) { msg.append("Error fetching message."); e.printStackTrace(); } latest.setText(msg); } } } }).execute(EVENT_URL + "/feed?access_token=" + accessToken); } }