Android Open Source - mitlocate Friends Tab






From Project

Back to project page mitlocate.

License

The source code is released under:

MIT License

If you think the Android project mitlocate listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package edu.mit.locate.friends;
//from  www .j a  v a  2 s. c o  m
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import edu.mit.locate.MITHttpsClient;
import edu.mit.locate.MITLocationService;
import edu.mit.locate.R;
import edu.mit.locate.tabs.NavigationTab;

public class FriendsTab extends NavigationTab {
  private final String TAG = "FriendsTab";
  private ArrayList<Friend> friends;
  private FriendsListAdapter fla;
  private ListView lv;
  private ProgressDialog pd;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab_friends, container, false);

    LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(FriendUpdaterBR,
        new IntentFilter(MITLocationService.BROADCAST_LOCATOR));

    lv = (ListView) v.findViewById(R.id.friendsList);

    Log.w(TAG, "ocv called");
    if(friends==null){
      Log.w(TAG, "friends is null");
      friends = new ArrayList<Friend>();
      fla = new FriendsListAdapter(getActivity().getApplicationContext(), friends);
    }
    lv.setAdapter(fla);

    lv.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        final Friend friend = (Friend) lv.getItemAtPosition(position);

        Intent i = new Intent(getActivity(), FriendActivity.class);
        i.putExtra("id", friend.getID());
        i.putExtra("name", friend.getName());
        i.putExtra("location", friend.getLocation());
        i.putExtra("sharing", friend.getSharing());
        i.putExtra("age", friend.getAge());
        startActivity(i);
      }
    });

    TextView mInviteButton = (TextView) v.findViewById(R.id.addButton);
    mInviteButton.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View v) {
        final View inviteView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_invite, null);
        new AlertDialog.Builder(getActivity())
        .setView(inviteView)
        .setIcon(android.R.drawable.ic_dialog_email)
        .setTitle("Add Friends")
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int id) {}

        })
        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int id) {
            SharedPreferences prefs = getActivity().getSharedPreferences("MITLOCATE", 0);
            final String username = ((EditText) inviteView.findViewById(R.id.username)).getText().toString();
            if(username.length() > 0){
              pd = ProgressDialog.show(getActivity(), "Inviting", "Please wait...");
              JSONObject params = new JSONObject();
              try {
                params.put("UUID", prefs.getString("DeviceID", null));
                params.put("username", username);
              } catch (JSONException e) {
                e.printStackTrace();
              }

              new MITHttpsClient("invite", getActivity(), true){
                @Override
                protected void onPostExecute(JSONObject response){
                  if(response!=null){
                    Toast.makeText(getActivity(), response.optString("message"), Toast.LENGTH_SHORT).show();
                  } else {
                    Log.e(TAG,"Fail: "+response);
                    Toast.makeText(getActivity(), "Couldn't add friend.", Toast.LENGTH_SHORT).show();
                  }
                  pd.dismiss();
                }
              }.execute(params);
            } else {
              Toast.makeText(getActivity(), "Invalid username.", Toast.LENGTH_SHORT).show();
            }
          }
        }).create().show();
      }
    });

    return v;
  }

  private BroadcastReceiver FriendUpdaterBR = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      final String response = intent.getStringExtra("response");
      if(response!=null){
        try{
          JSONObject responseObject= new JSONObject(response);
          int status = responseObject.getInt("status");
          if(status==0){
            friends.clear();
            JSONArray faves = (JSONArray) responseObject.getJSONArray("faves");
            for (int i=0; i<faves.length(); i++) {
              JSONObject friend = faves.getJSONObject(i);
              Friend sr = new Friend();
              sr.setID(friend.getInt("id"));
              sr.setName(friend.getString("name"));
              sr.setLocation(friend.getString("location"));
              sr.setBuildingID(friend.optInt("bldgid"));
              sr.setBuilding(friend.optString("bldg"));
              sr.setSharing(responseObject.optJSONObject("shares").has(sr.getID()));
              sr.setAge(friend.optString("age"));
              friends.add(sr);
            }
            fla.notifyDataSetChanged();
          }
        }catch(JSONException pe){
          Log.e(TAG,"JSON parse failure: "+pe.getMessage());
        }
      }
    }
  };
}




Java Source Code List

edu.mit.locate.APEditorActivity.java
edu.mit.locate.BootLocationService.java
edu.mit.locate.LoginActivity.java
edu.mit.locate.MITHttpsClient.java
edu.mit.locate.MITLocationService.java
edu.mit.locate.MainActivity.java
edu.mit.locate.alerts.Alert.java
edu.mit.locate.alerts.AlertsListAdapter.java
edu.mit.locate.alerts.AlertsTab.java
edu.mit.locate.apeditor.APListAdapter.java
edu.mit.locate.apeditor.APScanResult.java
edu.mit.locate.friends.FriendActivity.java
edu.mit.locate.friends.Friend.java
edu.mit.locate.friends.FriendsListAdapter.java
edu.mit.locate.friends.FriendsTab.java
edu.mit.locate.tabs.IconPagerAdapter.java
edu.mit.locate.tabs.IcsLinearLayout.java
edu.mit.locate.tabs.NavigationTab.java
edu.mit.locate.tabs.PageIndicator.java
edu.mit.locate.tabs.SearchTab.java
edu.mit.locate.tabs.SettingsTab.java
edu.mit.locate.tabs.TabPageIndicator.java
edu.mit.locate.tabs.TabPagerAdapter.java