TalkViewActivity.java :  » Client » com-android-moms-client » com » moms » android » client » activity » Android Open Source

Android Open Source » Client » com android moms client 
com android moms client » com » moms » android » client » activity » TalkViewActivity.java
package com.moms.android.client.activity;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpException;
import org.json.JSONException;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.utils.MomsObject;
import android.utils.MomsPreference;
import android.utils.Utils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.moms.android.client.R;
import com.moms.android.client.context.Global;
import com.moms.android.client.context.ReplyContext;
import com.moms.android.client.context.TalkContext;
import com.moms.android.client.exception.AuthorizeException;
import com.moms.android.client.funtion.MomsFunc;
import com.moms.android.client.net.Moms4j;
import com.moms.android.client.net.MomsReply;

public class TalkViewActivity extends ListActivity {
  public static final String      DEBUG_TAG            = "__TalkViewActivity__";

  public static final int        CODE_TOAST            = 0x00000015;
  public static final int        CODE_PROGRESSBAR_GONE      = 0x00000016;
  public static final int        CODE_PROGRESSBAR_VISIBLE    = 0x00000017;
  public static final int        CODE_UPDATE_TALK_CONTEXT    = 0x00000018;
  public static final int        CODE_NETWORK_UNREACHABLE    = 0x00000020;
  public static final int        CODE_TALK_DELETED        = 0x00000021;
  public static final int        CODE_UPDATE_REPLY        = 0x00000022;
  public static final int        CODE_NO_ACCESS_SDCARD      = 0x00000023;
  public static final int        CODE_WRONG_AUTHORIZATION_TOKEN  = 0x00000024;
  public static final int        CODE_ENABLE_LISTVIEW      = 0x00000025;
  public static final int        CODE_DISABLE_LISTVIEW      = 0x00000026;
  
  private final String        UPDATE_ALL            = "1";
  private final String        UPDATE_REPLY          = "0";
  
  private Button            mButtonPostReplyTalk      = null;
  private Button            mButtonLikeToggle        = null;
  private Button            mButtonFavToggle        = null;
  private Button            mButtonDeleteTalk        = null;
  private Button            mButtonMeari          = null;
  
  private String            mLog              = null;

  private byte[]            mUploadedPhotoBytes        = null;

  private TalkContextAdapter      mAdapter            = null;

  private TalkContext          mTalkContext          = null;

  private ArrayList<ReplyContext>    mReplyContexts          = null;

  private Moms4j            mMomsData            = null;

  private MomsPreference        mMomsPreference          = null;

  private int              mReplyNum            = 0;

  private ProgressBar          mProgressBar          = null;

  private Intent            mResultIntent          = null;

  private UpdateTalkContextsAsyncTask  mUpdateTalkContextsAsyncTask  = null;

  private Bitmap            b                = null;
  
  Handler mMessageHandler = new Handler()  {
    @Override
    public void handleMessage(Message msg) {
      if(msg.what == CODE_UPDATE_TALK_CONTEXT) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            mAdapter = new TalkContextAdapter(TalkViewActivity.this, mTalkContext);
            setListAdapter(mAdapter);
          }
        });
      }
      else if(msg.what == CODE_PROGRESSBAR_GONE) {
        mProgressBar.setVisibility(View.GONE);
      } 
      else if(msg.what == CODE_PROGRESSBAR_VISIBLE) {
        mProgressBar.setVisibility(View.VISIBLE);
      } 
      else if(msg.what == CODE_TOAST) {
        Toast.makeText(TalkViewActivity.this, mLog, Toast.LENGTH_SHORT).show();
      }
      else if(msg.what == CODE_NETWORK_UNREACHABLE) {
        Toast.makeText(TalkViewActivity.this, "   ", Toast.LENGTH_SHORT).show();
      }
      else if(msg.what == CODE_TALK_DELETED) {
        Toast.makeText(TalkViewActivity.this, " ", Toast.LENGTH_SHORT).show();
        mResultIntent.putExtra("talk_number", mTalkContext.getTalkNumber());
        setResult(Global.ACTIVITY_RESULT_TALK_DELETE, mResultIntent);
        finish();
      }
      else if(msg.what == CODE_UPDATE_REPLY) {
        int result = updateReplyContextsFromWeb();
        if(result == -10) {
          Toast.makeText(TalkViewActivity.this, "   ", Toast.LENGTH_SHORT).show();
        } else if(result == -1)  {
          Toast.makeText(TalkViewActivity.this, "      .  ", Toast.LENGTH_SHORT).show();
        }
      }
      else if(msg.what == CODE_NO_ACCESS_SDCARD) {
        Toast.makeText(TalkViewActivity.this, "   ", Toast.LENGTH_SHORT).show();
      }
      else if(msg.what == CODE_WRONG_AUTHORIZATION_TOKEN ) {
        Toast.makeText(TalkViewActivity.this, "      .  ", Toast.LENGTH_SHORT).show();
      }
      else if(msg.what == CODE_ENABLE_LISTVIEW) {
        getListView().setEnabled(true);
      }
      else if(msg.what == CODE_DISABLE_LISTVIEW) {
        getListView().setEnabled(false);
      }
    }
  }; 
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, 1, 1, " ").setIcon(R.drawable.ic_menu_moms);
    menu.add(Menu.NONE, 2, 2, "").setIcon(R.drawable.ic_menu_reload);
    menu.add(Menu.NONE, 3, 3, "").setIcon(R.drawable.ic_menu_search);
    menu.add(Menu.NONE, 4, 4, "").setIcon(R.drawable.ic_menu_logout);
    
    return super.onCreateOptionsMenu(menu);
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
      case 1: 
        break;
      case 2: 
        sendMessage(mMessageHandler, CODE_DISABLE_LISTVIEW, 0);
        UpdateReplyContextsAsyncTask(UPDATE_ALL);
        break;
      case 3: 
        break;
      case 4: 
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("  ?").setCancelable(false).setPositiveButton("", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id)  {
            setResult(Global.ACTIVITY_RESULT_LOGOUT);
            finish();
            Toast.makeText(TalkViewActivity.this, "", Toast.LENGTH_SHORT).show();
          }
        }).setNegativeButton("",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id)  {
              dialog.cancel();
            }
          });
        AlertDialog dialog = builder.create();
        dialog.setTitle("");
        dialog.show();
        break;
    }
    
    return super.onOptionsItemSelected(item);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data)
  {
    if(resultCode == Global.ACTIVITY_RESULT_TALK_CONTEXT_UPDATE)
    {
      UpdateReplyContextsAsyncTask(UPDATE_ALL);

      setResult(Global.ACTIVITY_RESULT_TALK_CONTEXT_UPDATE);
    }
    else if(resultCode == Global.ACTIVITY_RESULT_POST_REPLY_OK) //   ,    . 
    {
      mTalkContext.setReplyCount(mTalkContext.getReplyCount() + 1);
      
      mResultIntent.putExtra("reply", true);
      setResult(Global.ACTIVITY_RESULT_TALK_CONTEXT_UPDATE, mResultIntent);
      
      sendMessage(mMessageHandler, CODE_UPDATE_REPLY, 0);
      
      sendMessage(mMessageHandler, CODE_UPDATE_TALK_CONTEXT, 0);
    }
    else if(resultCode == Global.ACTIVITY_RESULT_POST_MEARI_OK) //     
    {
      mTalkContext.setMeariCount(mTalkContext.getMeariCount() + 1);
      mButtonMeari.setEnabled(false);
      mButtonMeari.setBackgroundResource(R.drawable.ic_smenu_meari_default_no);
      
//      mResultIntent.putExtra("meari", true);
//      setResult(Global.ACTIVITY_RESULT_TALK_LIST_UPDATE, mResultIntent);
      
      sendMessage(mMessageHandler, CODE_UPDATE_TALK_CONTEXT, 0);
    }
    else if(resultCode == Global.ACTIVITY_RESULT_GOTO_HOME)
    {
      setResult(Global.ACTIVITY_RESULT_GOTO_HOME);
      
      finish();
    }
    
    super.onActivityResult(requestCode, resultCode, data);
  }
  
    private void sendMessage(Handler handler, int code, int delaymillis) 
    {
      Message message = handler.obtainMessage();
      message.what = code;
      
      handler.sendMessageDelayed(message, (long)delaymillis);
    }

  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
    Utils.info(DEBUG_TAG, "onCreate()");
    
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    
    setContentView(R.layout.talk_view_activity_layout);
    
    mMomsPreference = new MomsPreference(this);
    
    mProgressBar = (ProgressBar) findViewById(R.id.progress_small);
    
    mReplyContexts = new ArrayList<ReplyContext>();
    
    mResultIntent = new Intent();
    
    mMomsData = new Moms4j(this);
    
    b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_photo_frame);
    
    initView();
    initViewListener();

    //   
    String talkNumber = getIntent().getStringExtra("talk_number");
    String writerId = getIntent().getStringExtra("writer_id"); 
    if(talkNumber != null && writerId != null)
    {
      UpdateReplyContextsAsyncTask("UPDATE_TALK", talkNumber, writerId);
    }
    else
    {
      //   
      Bundle bundle = getIntent().getBundleExtra("bundle");
      
      mTalkContext = (TalkContext) bundle.getParcelable("talk_context");
      
      updateTalkContextStatus();
      
      sendMessage(mMessageHandler, CODE_UPDATE_TALK_CONTEXT, 0);
    
      UpdateReplyContextsAsyncTask(UPDATE_REPLY);
    }

    super.onCreate(savedInstanceState);
  }
  
  private void UpdateReplyContextsAsyncTask(String... flag)
  {
    if(mUpdateTalkContextsAsyncTask != null)
      mUpdateTalkContextsAsyncTask.cancel(true);
    
    mUpdateTalkContextsAsyncTask = new UpdateTalkContextsAsyncTask();
    
    mUpdateTalkContextsAsyncTask.execute(flag);
  }
  
  @Override
    protected void onDestroy() 
  {
      Log.e(DEBUG_TAG, "onDestroy()");
      
      super.onDestroy();
    }
  
  @Override
  protected void onPause()
  {
    if(mUpdateTalkContextsAsyncTask != null )
    {
      mUpdateTalkContextsAsyncTask.cancel(true);
    }
    
    super.onPause();
  }
    
    @Override
    protected void onStop()
    {
      Utils.info(DEBUG_TAG, "onStop()");
      
      super.onStop();
    }
    
    @Override
    protected void onResume() 
    {
      Utils.info(DEBUG_TAG, "onResume()");
      
      super.onResume();
    }
    
    @Override
    protected void onStart() 
    {
      
      Utils.info(DEBUG_TAG, "onStart()");
      
      super.onStart();
    }
    
    @Override
    protected void onRestart() 
    {
      Utils.info(DEBUG_TAG, "onRestart()");
  
      super.onRestart();
    }
  
  private void initView() 
  {
    mButtonPostReplyTalk = (Button) findViewById(R.id.btn_post_reply_talk_in_talk_activity);
    mButtonLikeToggle = (Button) findViewById(R.id.btn_like_smenu_in_talk_activity);
    mButtonFavToggle = (Button) findViewById(R.id.btn_fav_smenu_in_talk_activity);
    mButtonDeleteTalk = (Button) findViewById(R.id.btn_delete_smenu_in_talk_activity);
    mButtonMeari = (Button) findViewById(R.id.btn_meari_in_talk_activity);
  }

  private void initViewListener()
  {
    //  
    mButtonDeleteTalk.setOnClickListener(new OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        AlertDialog.Builder builder = new AlertDialog.Builder(TalkViewActivity.this);
        builder.setMessage("  ? \n     ").setCancelable(false).setPositiveButton("",
            new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
            
            if(Utils.checkNetworkStatus(TalkViewActivity.this))
            {
              String loginId = mMomsPreference.getMomsLoginId();
              
              int result = MomsFunc.deleteTalk(mMomsData, loginId, mTalkContext.getTalkNumber());
              if(result == 1)
              {
                mResultIntent.putExtra("talk_number", mTalkContext.getTalkNumber());
                setResult(Global.ACTIVITY_RESULT_TALK_DELETE, mResultIntent);
                
                finish();
              }
              else if(result == -10)
              {
                sendMessage(mMessageHandler, CODE_WRONG_AUTHORIZATION_TOKEN, 0);
              }
              else if(result == -1)
              {
                sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
              }
            }
            else
            {
              sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
            }
            
          }}).setNegativeButton("",
              
          new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
          }});
        
        AlertDialog dialog = builder.create();
        dialog.setTitle(" ");
        dialog.show();
      }
    });
    
    //    
    mButtonFavToggle.setOnClickListener(new OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        if(Utils.checkNetworkStatus(TalkViewActivity.this))
        {
          String loginId = mMomsPreference.getMomsLoginId();
          
          //      ..
          if(mTalkContext.isCapture() == 0) 
          {
            //   
            mButtonFavToggle.setEnabled(false);
            
            //    
            int result = MomsFunc.setFav(mMomsData, loginId, mTalkContext.getTalkNumber());
            if(result == 1)
            {
              v.setBackgroundResource(R.drawable.ic_smenu_fav_click);
              
              mTalkContext.setCaptureCount(mTalkContext.getCaptureCount() + 1);
              mTalkContext.setCapture(1);
              
              mResultIntent.putExtra("capture", true);
              setResult(Global.ACTIVITY_RESULT_TALK_CONTEXT_UPDATE);
              
              sendMessage(mMessageHandler, CODE_UPDATE_TALK_CONTEXT, 0);
            }
            else if(result == -10)
            {
              sendMessage(mMessageHandler, CODE_WRONG_AUTHORIZATION_TOKEN, 0);
            }
            else if(result == -1)
            {
              sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
            }
            
            //  
            mButtonFavToggle.setEnabled(true);
          } 
          else //      
          {
            //    
            mButtonFavToggle.setEnabled(false);
            
            //   
            int result = MomsFunc.setUnfav(mMomsData, loginId, mTalkContext.getTalkNumber());
            if(result == 1)
            {
              v.setBackgroundResource(R.drawable.ic_smenu_fav_default);
              
              mTalkContext.setCaptureCount(mTalkContext.getCaptureCount() - 1);
              mTalkContext.setCapture(0);
              
              mResultIntent.putExtra("capture", false);
              setResult(Global.ACTIVITY_RESULT_TALK_CONTEXT_UPDATE);
              
              sendMessage(mMessageHandler, CODE_UPDATE_TALK_CONTEXT, 0);
            }
            else if(result == -10)
            {
              sendMessage(mMessageHandler, CODE_WRONG_AUTHORIZATION_TOKEN, 0);
            }
            else if(result == -1)
            {
              sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
            }
            
            //  
            mButtonFavToggle.setEnabled(true);
          }
        }
        else
        {
          sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
        }
      }
    });
    
    // 
    mButtonLikeToggle.setOnClickListener(new OnClickListener() 
    {
      @Override
      public void onClick(View v) 
      {
        if(Utils.checkNetworkStatus(TalkViewActivity.this))
        {
          String loginId = mMomsPreference.getMomsLoginId();
          
          if (mTalkContext.getILikeIt() > 0) 
          {
            int result = MomsFunc.setUnLike(mMomsData, loginId, mTalkContext.getTalkNumber());
            if(result == 1)
            {
              mResultIntent.putExtra("like", true);
              
              setResult(Global.ACTIVITY_RESULT_TALK_CONTEXT_UPDATE);
              
              mTalkContext.setLikeCount(mTalkContext.getLikeCount() -1);
              mTalkContext.setILikeIt(0);
              
              sendMessage(mMessageHandler, CODE_UPDATE_TALK_CONTEXT, 0);
              
              v.setBackgroundResource(R.drawable.ic_smenu_good_default);
            }
            else if(result == -10)
            {
              sendMessage(mMessageHandler, CODE_WRONG_AUTHORIZATION_TOKEN, 0);
            }
            else if(result == -1)
            {
              sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
            }
          } 
          else if (mTalkContext.getILikeIt() <= 0) 
          {
            int result = MomsFunc.setLike(mMomsData, loginId, mTalkContext.getTalkNumber());
            if(result == 1)
            {
              mResultIntent.putExtra("like", false);
              
              setResult(Global.ACTIVITY_RESULT_TALK_CONTEXT_UPDATE);
              
              mTalkContext.setLikeCount(mTalkContext.getLikeCount() + 1);
              mTalkContext.setILikeIt(1);
              
              sendMessage(mMessageHandler, CODE_UPDATE_TALK_CONTEXT, 0);
              
              v.setBackgroundResource(R.drawable.ic_smenu_good_click);
            }
            else if(result == -10)
            {
              sendMessage(mMessageHandler, CODE_WRONG_AUTHORIZATION_TOKEN, 0);
            }
            else if(result == -1)
            {
              sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
            }
          }
          
//          updateTalkContextWithReplysThread();
        }
        else
        {
          sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
        }
      }
    });
    
    //    
    ((Button) findViewById(R.id.btn_post_reply_smenu_in_talk_activity)).setOnClickListener(new OnClickListener() 
    {
      @Override
      public void onClick(View v) 
      {
        if(Utils.checkNetworkStatus(TalkViewActivity.this))
        {
          Intent i = new Intent();
          i.setClass(TalkViewActivity.this, PostReplyTalkActivity.class);
          i.putExtra("talk_number", mTalkContext.getTalkNumber());
          
          startActivityForResult(i, Global.ACTIVITY_REQUEST_CHANGE_TALK);          
        }
        else
        {
          sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
        }
      }
    });
    
    //   
    mButtonPostReplyTalk.setOnClickListener(new OnClickListener() 
    {
      @Override
      public void onClick(View v) 
      {
        if(Utils.checkNetworkStatus(TalkViewActivity.this))
        {
          Intent i = new Intent();
          i.setClass(TalkViewActivity.this, PostReplyTalkActivity.class);
          i.putExtra("talk_number", mTalkContext.getTalkNumber());
          
          startActivityForResult(i, Global.ACTIVITY_REQUEST_CHANGE_TALK);          
        }
        else
        {
          sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
        }
      }
    });
    
    //  
    mButtonMeari.setOnClickListener(new OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        if(Utils.checkNetworkStatus(TalkViewActivity.this))
        {
          Intent i = new Intent();
          i.setClass(TalkViewActivity.this, PostMeariActivity.class);
          i.putExtra("talk_number", mTalkContext.getTalkNumber());
          i.putExtra("user_id", mTalkContext.getId());

          startActivityForResult(i, Global.ACTIVITY_REQUEST_CHANGE_TALK);
        }
        else
        {
          sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
        }
      }
    });
    
    ((Button) findViewById(R.id.btn_go_home_in_talk_view_activity)).setOnClickListener(new OnClickListener() 
    {
      @Override
      public void onClick(View v) 
      {
        setResult(Global.ACTIVITY_RESULT_GOTO_HOME);
        
        finish();
      }
    });
  }
  
  private void updateTalkContextStatus()
  {
    //   
    if (mTalkContext.getILikeIt() == 0) 
    {
      mButtonLikeToggle.setBackgroundResource(R.drawable.ic_smenu_good_default);
    } 
    else 
    {
      mButtonLikeToggle.setBackgroundResource(R.drawable.ic_smenu_good_click);
    }
    
    //       
    if (mMomsPreference.getMomsLoginId().equals(mTalkContext.getId()) == false) 
    {
      mButtonDeleteTalk.setEnabled(false);
      mButtonDeleteTalk.setBackgroundResource(R.drawable.ic_smenu_delete_no);
    }
    
    //      
    if(mTalkContext.getId().equals(mMomsPreference.getMomsLoginId()) == true) 
    {
      mButtonLikeToggle.setEnabled(false);
      mButtonLikeToggle.setBackgroundResource(R.drawable.ic_smenu_good_no);
      
      mButtonMeari.setEnabled(false);
      mButtonMeari.setBackgroundResource(R.drawable.ic_smenu_meari_default_no);
    }
    
    //    
    if(mTalkContext.getIsMeari() == 1) 
    {
      mButtonMeari.setEnabled(false);
      mButtonMeari.setBackgroundResource(R.drawable.ic_smenu_meari_default_no);
    }
    
    //    
    if(mTalkContext.isCapture() == 1) 
    {
      if( mTalkContext.getCaptureCount() != -1)
      {
        mButtonFavToggle.setEnabled(true);
        mButtonFavToggle.setBackgroundResource(R.drawable.ic_smenu_fav_click);          
      }
      else
      {
        mButtonFavToggle.setEnabled(false);
        mButtonFavToggle.setBackgroundResource(R.drawable.ic_smenu_fav_no);
      }
    }
  }

  private int updateReplyContextsFromWeb()
  {
    int result = -1;
    
    try 
    {
      mReplyContexts.clear();
      
      List<MomsReply> replys = null;
      
      replys = mMomsData.getReplysMore(mTalkContext.getTalkNumber());
      if(replys == null)
        return 0;

      for (MomsReply reply : replys)
      {
        ReplyContext replyContext = new ReplyContext();
        replyContext.setId(reply.getReplyId());
        replyContext.setReplyNo(reply.getReplyNumber());
        replyContext.setContext(reply.getReplyContents());
        replyContext.setName(reply.getReplyName());
        replyContext.setProfilePhoto(Utils.toBitmap(reply.getReplyProfileImage()));
        replyContext.setTime(reply.getReplyTimeStamp());
        
        mReplyContexts.add(replyContext);
      }
      
      result = 1;
    }
    catch (HttpException e) 
    {
      result = -1;
    } 
    catch (IOException e) 
    {
      result = -1;
    } 
    catch (JSONException e) 
    {
      String errmsg = e.getMessage();
      if(errmsg.equals("No value for reply")); 
        result = -1;
    }
    catch(IllegalStateException e)
    {
      result = -1;
    } 
    catch (AuthorizeException e) 
    {
      result = -10;
    }
    
    return result;
  }
  
  class TalkContextAdapter extends BaseAdapter {
    TalkContext talkContext = null;
    
    public TalkContextAdapter(Context context, TalkContext talkContext) {
      this.talkContext = talkContext;
    }

    @Override
    public int getCount()
    {
      //  +    1 .  talkContext replay count  reply
      // count   .
      return mReplyContexts.size() + 1;
    }

    @Override
    public Object getItem(int position)
    {
      if (position == 0)
      {
        return talkContext;
      } else
      {
        return mReplyContexts.get(position - 1);
      }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
      View view = null;
      
      // 
      if(position == 0) {
        if(view == null) {
          LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          view = li.inflate(R.layout.talk_view_list_row, null);
//          view.setTag(new Integer(position));
        }
        
        ImageView profileImage = (ImageView) view.findViewById(R.id.imageview_talkview_user_profile_image);
        TextView userName = (TextView) view.findViewById(R.id.tv_user_name_in_talk_activity);
        TextView talk = (TextView) view.findViewById(R.id.tv_context_in_talk_activity);
        TextView time = (TextView) view.findViewById(R.id.tv_time_in_talkview_list_row);
        TextView replyCount = (TextView) view.findViewById(R.id.tv_alarm_reply_in_talk_activity);
        TextView meariCount = (TextView) view.findViewById(R.id.tv_alarm_meari_in_talk_activity);
        TextView likeCount = (TextView) view.findViewById(R.id.tv_alarm_like_in_talk_activity);
        TextView favoriteCount = (TextView) view.findViewById(R.id.tv_alarm_favorite_in_talk_activity);
        TextView themeTalk = (TextView) view.findViewById(R.id.tv_theme_talk_context_in_talk_view_list_row);
        TextView hello_name = (TextView) view.findViewById(R.id.tv_hello_name_in_talk_view_list_row);
        LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.ll_image_in_talk_list_row);
        LinearLayout movie = (LinearLayout) view.findViewById(R.id.ll_no_movie_in_talk_view_list_row);
        LinearLayout hello_layout = (LinearLayout) view.findViewById(R.id.ll_hello_talk_in_talk_view_list_row);
          
          //     
          if(mTalkContext.getIsMeari() == 1) {
            view.findViewById(R.id.ll_meari_in_talk_view_list_row).setVisibility(View.VISIBLE);
            
            ImageView meari_icon = (ImageView) view.findViewById(R.id.iv_meari_icon_in_talk_view_list_row);
            
            TextView meari_name = (TextView) view.findViewById(R.id.tv_meari_name_in_talk_view_list_row);
              TextView meari_context = (TextView) view.findViewById(R.id.tv_meari_context_in_talk_view_list_row);
              ImageView meari_image = (ImageView) view.findViewById(R.id.iv_meari_image_in_talk_view_list_row);
              
          if (meari_icon != null) {
            meari_icon.setVisibility(View.VISIBLE);
          }

          if (meari_name != null) {
            meari_name.setText(mTalkContext.getMeariName());
          }

          if (meari_context != null) {
            meari_context.setText(mTalkContext.getMeariContext());
          }
            
            if(meari_image != null) {
              if(mTalkContext.getMeariImage() != null) {
                ((LinearLayout) view.findViewById(R.id.ll_backgroud_of_meari_image_in_talk_view_list_row)).setVisibility(View.VISIBLE);
                meari_image.setImageBitmap(Utils.toBitmap(mTalkContext.getMeariImage()));
              }
            }
          }
          
          // 
          if(themeTalk != null) {
            if(talkContext.getIsTheme() == 1) {
              themeTalk.setVisibility(View.VISIBLE);
              themeTalk.setText("Q: "+ talkContext.getTheme());
            }
          }
        
          //    
        if(linearLayout != null) {
          mUploadedPhotoBytes = mTalkContext.getUploadedPhotos();      
          if(mUploadedPhotoBytes != null) {
            linearLayout.setPadding(10, 10, 5, 10);
            
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.rightMargin = 5;
            
            ImageView imageView = new ImageView(TalkViewActivity.this);
            imageView.setLayoutParams(params);
            
            Bitmap bitmap = Utils.toBitmap(mUploadedPhotoBytes);
            
            imageView.setImageBitmap(bitmap);
            
            linearLayout.addView(imageView);
            
            if(bitmap == null) {
              Utils.error(DEBUG_TAG, "   .");
            } else {
              Bitmap c = Bitmap.createScaledBitmap(b, bitmap.getWidth(), bitmap.getHeight(), true);
              Drawable d = new BitmapDrawable(c);
              linearLayout.setBackgroundDrawable(d);
              linearLayout.setVisibility(View.VISIBLE);
              
              imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v)  {
                  if (Utils.checkNetworkStatus(TalkViewActivity.this)) {
                    TalkContext talkContext = (TalkContext) getIntent().getBundleExtra("bundle").getParcelable("talk_context");
                    String imageDir = "http://www.moms.kr";
                    URL profile_image_url = null;
                    String[] values = null;
                    try {
                      String filename = talkContext.getLargeImageURL();
                      profile_image_url = new URL(imageDir + filename);
                      values = filename.split("/");
                    } catch (MalformedURLException e) {
                    }

                    Intent i = new Intent();
                    i.setClass(TalkViewActivity.this, BigPhotoActivity.class);
                    i.putExtra("url", profile_image_url);
                    i.putExtra("filename", values[values.length - 1] + ".full");
                    
                    startActivityForResult(i, 1);
                  } else {
                    sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
                  }
                }
              });
            }
          }
        }
        
        if(movie != null) 
        {
          if(talkContext.hasMovie() == 1) 
          {
            movie.setVisibility(View.VISIBLE);
          }
        }
        
        //  
        if (profileImage != null) 
        {
          if (talkContext.getUserProfilePhoto() != null) 
          {
            profileImage.setImageBitmap(Utils.toBitmap(talkContext.getUserProfilePhoto()));
          }
          
          profileImage.setOnClickListener(new OnClickListener() 
          {
            @Override
            public void onClick(View v)
            {
              if(Utils.checkNetworkStatus(TalkViewActivity.this))
              {
                Intent i = new Intent();
                i.setClass(TalkViewActivity.this, ProfileActivity.class);
                i.putExtra("id", talkContext.getWriterId());
                
                startActivityForResult(i, 1);                
              }
              else
              {
                sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
              }
            }
          });
        }
        
        if(hello_layout != null) 
        {
          if(talkContext.getWriterName().equals(talkContext.getName()) == false) 
          {
            hello_layout.setVisibility(View.VISIBLE);
            
            if(hello_name != null) 
            {
              userName.setText(talkContext.getWriterName());
              hello_name.setText(mTalkContext.getName());
            } 
          } 
          else
          {
            if (userName != null)
            {
              userName.setText(talkContext.getWriterName());
            }
          }
        }

        if (talk != null) 
        {
          talk.setText(talkContext.getContext());
        }

        if (time != null) 
        {
          String timeString = Utils.timeTopassedTime(System.currentTimeMillis()/1000, Long.parseLong(talkContext.getTime()));
          time.setText(timeString);
        }

        if (replyCount != null) 
        {
          if(talkContext.getReplyCount() != 0) 
          {
            String str = String.format(" %d  ", talkContext.getReplyCount());
            replyCount.setText(str);
            ((ImageView)view.findViewById(R.id.a)).setVisibility(View.VISIBLE);
          } 
        }

        if (meariCount != null)
        {
          if(talkContext.getMeariCount() != 0 ) 
          {
            String str = String.format(" %d  ", talkContext.getMeariCount());
            meariCount.setText(str);
            ((ImageView)view.findViewById(R.id.c)).setVisibility(View.VISIBLE);
          }
        }

        if (likeCount != null) 
        {
          if(talkContext.getLikeCount() != 0) 
          {
            String str = String.format(" %d  ", talkContext.getLikeCount());
            likeCount.setText(str);
            ((ImageView)view.findViewById(R.id.b)).setVisibility(View.VISIBLE);
          }
        }

        if (favoriteCount != null) 
        {
          if(talkContext.getCaptureCount() < 0)
          {
            favoriteCount.setVisibility(View.GONE);
          } 
          else if(talkContext.getCaptureCount() != 0)
          {
            String str = String.format(" %d  ", talkContext.getCaptureCount());
            favoriteCount.setText(str);
            ((ImageView)view.findViewById(R.id.d)).setVisibility(View.VISIBLE);  
          }
        }
      }
      else // 
      { 
        if(view == null)
        {
          LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          view = li.inflate(R.layout.talk_view_reply_list_row, null);
          Integer index = position;
          view.setTag(index);
        }

        Utils.info(DEBUG_TAG, "position=" + String.valueOf(position) + "size=" + String.valueOf(mReplyContexts.size()));
        
        final ReplyContext replyContext = mReplyContexts.get(position-1);
                
        ImageView profileImage = (ImageView) view.findViewById(R.id.iv_profile_image_in_talkview_reply_list_row);
        TextView userName = (TextView) view.findViewById(R.id.textview_talkview_reply_user_name);
        TextView talk = (TextView) view.findViewById(R.id.textview_talkview_reply_context);
        TextView time = (TextView) view.findViewById(R.id.textview_talkview_reply_time);
        ImageView trash = (ImageView) view.findViewById(R.id.iv_trash_in_talkview_raply_list_row);
  
        if (profileImage != null) 
        {
          if(replyContext.getProfilePhoto() != null) {
            profileImage.setImageBitmap(replyContext.getProfilePhoto());
          }
          
          profileImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) 
            {
              if(Utils.checkNetworkStatus(TalkViewActivity.this))
              {
                Intent i = new Intent();
                i.setClass(TalkViewActivity.this, ProfileActivity.class);
                i.putExtra("id", replyContext.getId());
                
                startActivityForResult(i, 1);                
              }
              else
              {
                sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
              }
            }
          });
        }
        
        if(trash != null) {
          if(mMomsPreference.getMomsLoginId().equals(replyContext.getId()) == false && talkContext.getWriterId().equals(mMomsPreference.getMomsLoginId())==false) {
            trash.setVisibility(View.GONE);
          } else {
            trash.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                mReplyNum = position -1;
                
                AlertDialog.Builder builder = new AlertDialog.Builder(TalkViewActivity.this);
                builder.setMessage("  ? \n     ").setCancelable(false).setPositiveButton("",
                    new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                          
                    if(Utils.checkNetworkStatus(TalkViewActivity.this))
                    {
                      String loginId = mMomsPreference.getMomsLoginId();
                      
                      int result = MomsFunc.deleteReply(mMomsData, loginId, mTalkContext.getTalkNumber(), mReplyContexts.get(mReplyNum).getReplyNo(), mTalkContext.getWriterId());
                      if(result == 1)
                      {
                        mReplyContexts.remove(mReplyNum);
                        
                        mTalkContext.setReplyCount(mTalkContext.getReplyCount() - 1);
                        
                        mAdapter.notifyDataSetChanged();
                        
                        setResult(Global.ACTIVITY_RESULT_TALK_CONTEXT_UPDATE);
                      }
                      else if(result == -10)
                      {
                        sendMessage(mMessageHandler, CODE_WRONG_AUTHORIZATION_TOKEN, 0);
                      }
                      else if(result == -1)
                      {
                        sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
                      }
                    }
                    else
                    {
                      sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
                    }

                  }}).setNegativeButton("",
                      
                  new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                  }});
                
                AlertDialog dialog = builder.create();
                dialog.setTitle(" ");
                dialog.show();
              }
            });
          }
        }
        
        if (userName != null) 
        {
          userName.setText(replyContext.getName());
        }
        
        if (talk != null) 
        {
          talk.setText(replyContext.getContext());
        }
        
        if (time != null) 
        {
          String timeStamp = Utils.timeTopassedTime(System.currentTimeMillis()/1000, Long.parseLong(replyContext.getTime()));
          
          time.setText(timeStamp);
        }
      }
      
      return view;
    }

    @Override
    public long getItemId(int position) {
      return 0;
    }
  }
  
  class UpdateTalkContextsAsyncTask extends AsyncTask<String, Integer, Integer> {
    @Override
    protected Integer doInBackground(String... params) {
      String loginId = mMomsPreference.getMomsLoginId();
      
      Integer result = 0;
      
      //    
      if(params[0].equals("UPDATE_TALK")) {
        MomsObject object = MomsFunc.getTalkContext(mMomsData, params[1], params[2], loginId);
        TalkContext talkContext = (TalkContext) object.getObject();
        if(talkContext != null) {
          mTalkContext = talkContext;
        }
        
        result = object.getResult();
        if(result == 1) {
          //  
          result = updateReplyContextsFromWeb();
        }
      } else {
//        Utils.info(mTalkContext.getId());
        
//        int n = MomsFunc.isTalkDeleted(mMomsData, loginId, mTalkContext.getTalkNumber(), mTalkContext.getWriterId());
        int n = MomsFunc.isTalkDeleted(mMomsData, loginId, mTalkContext.getTalkNumber(), mTalkContext.getId());
        if (n == 1) {//  
          return -2;
        } else if(n == 0) { //   
          // 
          if(params[0].equals(UPDATE_ALL)) {
            MomsObject object = MomsFunc.getTalkContext(mMomsData, mTalkContext.getTalkNumber(), mTalkContext.getWriterId(), loginId);
            TalkContext talkContext = (TalkContext) object.getObject(); 
            if(talkContext != null)  {
              mTalkContext = talkContext;
            }
            
            result = object.getResult();
          }
          
          //  
          result = updateReplyContextsFromWeb();
          sendMessage(mMessageHandler, CODE_ENABLE_LISTVIEW, 0);
          
        } else if(n == -1) {
          result = -1;
        } else if(n == -10) {
          result = -10;
        }
      }
            
      return result;
    }
    
    @Override
    protected void onCancelled() {
      sendMessage(mMessageHandler, CODE_ENABLE_LISTVIEW, 0);
      sendMessage(mMessageHandler, CODE_PROGRESSBAR_GONE, 0);
      Utils.info(DEBUG_TAG, "UpdateTalkContextsAsyncTask has been canclled");
      super.onCancelled();
    }
    
    @Override
    protected void onPostExecute(Integer result) {
      if(result == -2) {
        sendMessage(mMessageHandler, CODE_TALK_DELETED, 0);
      } else if(result == -1) {
        sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
      } else if(result == -10) {
        sendMessage(mMessageHandler, CODE_WRONG_AUTHORIZATION_TOKEN, 0);
      } else if(result == 1) {
        sendMessage(mMessageHandler, CODE_UPDATE_TALK_CONTEXT, 0);
      }
      
      sendMessage(mMessageHandler, CODE_PROGRESSBAR_GONE, 0);
      Utils.info(DEBUG_TAG, "UpdateTalkContextsAsyncTask has been done");
      super.onPostExecute(result);
    }
    
    @Override
    protected void onPreExecute() {
      if(Utils.checkNetworkStatus(TalkViewActivity.this) == false) {
        cancel(true);
        sendMessage(mMessageHandler, CODE_NETWORK_UNREACHABLE, 0);
      } else {
        sendMessage(mMessageHandler, CODE_PROGRESSBAR_VISIBLE, 0);        
      }
      
      super.onPreExecute();
    }
    
    @Override
    protected void onProgressUpdate(Integer... values)
    {
      super.onProgressUpdate(values);
    }
  }
}
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.