main.java :  » Client » com-android-moms-client » com » donghoon » Android Open Source

Android Open Source » Client » com android moms client 
com android moms client » com » donghoon » main.java
package com.donghoon;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

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

import com.facebook.android.Facebook.*;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

interface FaceLoginCallback {
  public void onCompleteLogin();
}

public class main extends Activity implements FaceLoginCallback {

  @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    initUI();
    loginFacebook();
  }

  public void onCompleteLogin() {
    try {
      postMessage("    ~ ..1");
      postMessage("    ~ ..2");
      postMessage("    ~ ..3");

      getMessage();
      refreshMessageList();
    }

    catch (Exception e) {
      e.printStackTrace();
    } catch (FacebookError e) {
      e.printStackTrace();
    }
  }

  public void refreshMessageList() {

    // handler  UI thread  
    mHandler.post(new Runnable() {
      public void run() {
        ListView listView = (ListView) findViewById(R.id.messageList);
        ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
      }
    });
  }

  private void loginFacebook() {
    //   onCompleteLogin callback
    mFacebook.authorize(this, FB_APP_ID, PERMISSIONS, new AppLoginListener(
        getBaseContext(), mFacebook, this));
  }

  private void initUI() {
    // FaceBook     
    // , (), ()
    MessageDataAdapter adapter = new MessageDataAdapter(this,
        R.layout.messagelistdata, mMsgDataList);

    ListView listView = (ListView) findViewById(R.id.messageList);
    listView.setAdapter(adapter);
  }

  private void postMessage(String message) {
    // 
    Bundle params = new Bundle();
    params.putString("method", "stream.publish");
    params.putString("message", message);

    try {
      mFacebook.request(null, params, "POST");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void getMessage() throws MalformedURLException, IOException,
      JSONException, FacebookError {
    //   
    String result = mFacebook.request("me/home");
    JSONObject jsonResult = Util.parseJson(result);
    JSONArray array = jsonResult.getJSONArray("data");

    // int total = array.length();
    int total = 4; //   4 
    for (int i = 0; i < total; i++) {
      JSONObject data = (JSONObject) array.get(i);

      String tempID = data.getJSONObject("from").getString("id");

      MessageData msg = new MessageData();

      if (data.isNull("message") == false) {
        msg.m_Name = data.getJSONObject("from").getString("name");
        msg.m_Message = data.getString("message");

        // web   
        URL aURL = new URL("https://graph.facebook.com/" + tempID
            + "/picture?type=square");
        URLConnection conn = aURL.openConnection();
        InputStream is = openConnectionCheckRedirects(conn);

        BufferedInputStream bis = new BufferedInputStream(is);
        Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
        msg.mProfileImage = bm;

        // UI    
        mMsgDataList.add(msg);
      }
    }
  }

  // redirection  web   
  private InputStream openConnectionCheckRedirects(URLConnection c)
      throws IOException {
    boolean redir;
    int redirects = 0;
    InputStream in = null;
    do {
      if (c instanceof HttpURLConnection) {
        ((HttpURLConnection) c).setInstanceFollowRedirects(false);
      }
      in = c.getInputStream();
      redir = false;
      if (c instanceof HttpURLConnection) {
        HttpURLConnection http = (HttpURLConnection) c;
        int stat = http.getResponseCode();
        if (stat >= 300 && stat <= 307 && stat != 306
            && stat != HttpURLConnection.HTTP_NOT_MODIFIED) {
          URL base = http.getURL();
          String loc = http.getHeaderField("Location");
          URL target = null;
          if (loc != null) {
            target = new URL(base, loc);
          }
          http.disconnect();
          if (target == null
              || !(target.getProtocol().equals("http") || target
                  .getProtocol().equals("https"))
              || redirects >= 5) {
            throw new SecurityException("illegal URL redirect");
          }
          redir = true;
          c = target.openConnection();
          redirects++;
        }
      }
    } while (redir);
    return in;
  }

  private Facebook mFacebook = new Facebook();
  private ArrayList<MessageData> mMsgDataList = new ArrayList<main.MessageData>();
  private Handler mHandler = new Handler();

  //  facebook app ID   ( ,   app ID  )
  private static final String FB_APP_ID = "103463489716253";

  //       
  private static String[] PERMISSIONS = new String[] { "offline_access",
      "read_stream", "publish_stream", "user_photos" };

  // ----------------------------------------------------
  // UI 
  // ----------------------------------------------------
  private class MessageData {
    Bitmap mProfileImage = null;
    String m_Name;
    String m_Message;
  }

  private class MessageDataAdapter extends ArrayAdapter<MessageData> {
    public MessageDataAdapter(Context context, int textViewResourceId,
        ArrayList<MessageData> items) {
      super(context, textViewResourceId, items);
      this.mItems = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View view = convertView;
      if (view == null) {
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.messagelistdata, null);
      }

      MessageData info = mItems.get(position);

      if (info != null) {
        ImageView image = (ImageView) view.findViewById(R.id.listImage);

        //  
        if (image != null) {
          if (info.mProfileImage == null)
            image.setImageResource(R.drawable.no_avatar);
          else
            image.setImageBitmap(info.mProfileImage);

        }

        //  
        TextView nameTextView = (TextView) view
            .findViewById(R.id.listName);

        if (nameTextView != null) {
          nameTextView.setText(info.m_Name);
        }

        //  
        TextView messageTextView = (TextView) view
            .findViewById(R.id.listMessage);

        if (messageTextView != null) {
          messageTextView.setText(info.m_Message);
        }
      }

      return view;
    }

    private ArrayList<MessageData> mItems;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.