Android Open Source - mitlocate Friend Activity






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;
// w  w w. j  a  va 2 s  . c  om
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import edu.mit.locate.MITHttpsClient;
import edu.mit.locate.R;

public class FriendActivity extends FragmentActivity implements OnClickListener {

  private SharedPreferences prefs;
  private TextView mFriendName, mFriendInfo, mToggleSharingButton, mRemoveFriendButton;
  private final String TAG = "FriendActivity";
  private boolean sharing;
  private String fid, name, location, age;
  
  private ProgressDialog pd;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

//    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    setContentView(R.layout.activity_friend);
    prefs = getSharedPreferences("MITLOCATE", 0);

    mFriendName = (TextView) findViewById(R.id.friendName);
    mFriendInfo = (TextView) findViewById(R.id.friendInfo);

    mToggleSharingButton = (TextView) findViewById(R.id.toggleSharingButton);
    mRemoveFriendButton = (TextView) findViewById(R.id.removeFriendButton);

    Intent i = getIntent();
    fid = i.getStringExtra("id");
    name = i.getStringExtra("name");
    location = i.getStringExtra("location");
    age = i.getStringExtra("age");
    sharing = i.getBooleanExtra("sharing", false);
    
    refreshStrings();

    mToggleSharingButton.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View v) {
        pd = ProgressDialog.show(FriendActivity.this, "Saving", "Please wait...");
        JSONObject params = new JSONObject();
        try {
          params.put("UUID", prefs.getString("DeviceID", null));
          params.put((sharing?"un":"")+"share", fid);
        } catch (JSONException e) {
          e.printStackTrace();
        }
        new MITHttpsClient("update-friend", FriendActivity.this, true){
          @Override
          protected void onPostExecute(JSONObject response){
            if(response!=null && response.optBoolean("success")){
              sharing = !sharing;
              refreshStrings();
            } else {
              Log.e(TAG,"Fail: "+response);
              Toast.makeText(FriendActivity.this, "Couldn't save changes.", Toast.LENGTH_SHORT).show();
            }
            pd.dismiss();
          }
        }.execute(params);
      }
    });

    mRemoveFriendButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        new AlertDialog.Builder(FriendActivity.this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle("Confirm")
        .setMessage(Html.fromHtml("Are you sure you want to remove <b>"+name+"</b>? Your location will no longer be shared with this person unless you enable the public sharing mode."))
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int id) {}

        })
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int id) {
            pd = ProgressDialog.show(FriendActivity.this, "Removing", "Please wait...");
            JSONObject params = new JSONObject();
            try {
              params.put("UUID", prefs.getString("DeviceID", null));
              params.put("remove", fid);
            } catch (JSONException e) {
              e.printStackTrace();
            }
            new MITHttpsClient("update-friend", FriendActivity.this, true){
              @Override
              protected void onPostExecute(JSONObject response){
                if(response!=null && response.optBoolean("success")){
                  Toast.makeText(FriendActivity.this,"Removed "+name,Toast.LENGTH_SHORT).show();
                  finish();
                } else {
                  Log.e(TAG,"Fail: "+response);
                  Toast.makeText(FriendActivity.this, "Couldn't remove friend.", Toast.LENGTH_SHORT).show();
                }
                pd.dismiss();
              }
            }.execute(params);
          }
        }).create().show();
      }
    });
  }
  
  private void refreshStrings(){
    final int sharingMode = prefs.getInt("SharingMode", 1);
    
    mToggleSharingButton.setText(sharing?"Stop sharing location":"Start sharing location");
    
    String shareText;
    switch(sharingMode){
    case 0: shareText = "Your location is <b>shared</b> with this person because your sharing mode is public. " +
        "If you switch to limited mode, your location will <b>" + (sharing?"still":"no longer") + " be shared</b> with this person.";break;
    case 2: shareText = "Your location is <b>not shared</b> with this person because your sharing mode is private. " +
        "If you switch to limited mode, your location will <b>" + (sharing?"then":"still not") + " be shared</b> with this person.";break;
    default: shareText = "Your location is<b>" + (sharing?"":" not") + " shared</b> with this person.";break;
    }
    
    mFriendName.setText(name);
    mFriendInfo.setText(Html.fromHtml("<b>" + location + "</b><br>" + (age.equals("")?"":"<i>as of " + age + "</i>") + 
        "<br><br>"+shareText));
  }

  @Override
  public void onClick(View v){}
}




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