Android Open Source - android-qq Main Activity






From Project

Back to project page android-qq.

License

The source code is released under:

Apache License

If you think the Android project android-qq 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 com.myandroid.activity;
/*w  w w  .ja  v a2  s .co  m*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.myandroid.message.R;
import com.myandroid.util.Audio;
import com.myandroid.util.FileTcpServer;
import com.myandroid.util.Msg;
import com.myandroid.util.Tools;
import com.myandroid.util.User;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

  //????
  AlertDialog revTalkDialog=null;
  public boolean isPaused=false;
  ListView listView=null;
  public List<User> userList = null;
  public List<Map<String, Object>> adapterList = null;
  Tools tools=null;
  SimpleAdapter adapter=null;
  Msg m=null;
  ProgressDialog proDia = null;
  Double fileSize=0.0;
  TextView titlename=null;
  SettingDialog settingDialog=null;
  ImageView myicon=null;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);// ?????????
    setContentView(R.layout.activity_main);
    //?????????
    Tools.State=Tools.MAINACTIVITY;//??
    Tools.mainA=this;
    init();
    tools=new Tools(this,Tools.ACTIVITY_MAIN);
    //??????(???????)
    reBroad();
    Tools.out("???");
    // ????????? ?????????????
    tools.receiveMsg();
    // ????
    tools.startCheck();
    // ????????
    Tools.audio.start();
  }
  // ?????????
  public void init()
  {
    myicon=(ImageView)findViewById(R.id.friend_list_myImg);
    listView = (ListView) super.findViewById(R.id.listView);
    // ????????
    listView.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
          long arg3) {
        //???chatActivity
        Intent it = new Intent(MainActivity.this, ChatActivity.class);
        it.putExtra("person", userList.get(arg2));
        MainActivity.this.startActivityForResult(it, 1);
        // ????????????????????
        for (int i = 0; i < adapterList.size(); i++) {
          if (adapterList.get(i).get("ip").equals("IP:"+userList.get(arg2).getIp())) { // ????
            adapterList.get(i).put("UnReadMsgCount", "");
            tools.Tips(Tools.FLUSH, null);
            Tools.out("????");
            Tools.out("???chart");
          }
        }
      }
    });
    // ????????
    userList = new ArrayList<User>();
    Tools.me = new User(Build.MODEL,Tools.getLocalHostIp(),0,System.currentTimeMillis());
    userList.add(Tools.me.getCopy());
    adapterList = new ArrayList<Map<String, Object>>();
    Map map = new HashMap<String, Object>();
    map.put("headicon", Tools.headIconIds[Tools.me.getHeadIconPos()]);
    map.put("name", Tools.me.getName());
    map.put("ip", " IP:"+Tools.me.getIp());
    map.put("UnReadMsgCount", "");
    myicon.setImageResource(Tools.headIconIds[Tools.me.getHeadIconPos()]);
    adapterList.add(map);
    //?????view??????
    adapter = new SimpleAdapter(this, adapterList, R.layout.item,
        new String[] {"headicon","name", "ip", "UnReadMsgCount" }, new int[] {
            R.id.headicon,R.id.name, R.id.ip, R.id.UnReadMsgCount });
    listView.setAdapter(adapter);
    titlename=(TextView)findViewById(R.id.friend_list_myName);
    titlename.setText(Tools.me.getName());
    proDia = new ProgressDialog(this);
    proDia.setTitle("???????");// ??????
    proDia.setMessage("???");// ??????????
    proDia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// ????????
    proDia.setMax(100);// ???????????
    proDia.setProgress(10);// ?????
    
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.chart, menu);
    menu.add(0, 1, 1, "??????");
        //menu.add(0, 2, 2, "???????");
        return super.onCreateOptionsMenu(menu);
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == 1){
            //??????
          if(null==settingDialog)settingDialog = new SettingDialog(this);
        settingDialog.show();
        }
        return true;
    }
  public Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      switch (msg.what) {
      case Tools.CMD_UPDATEINFORMATION:
        //??????????
        reBroad();
        myicon.setImageResource(Tools.headIconIds[Tools.me.getHeadIconPos()]);
        break;
      case Tools.FLUSH:
        adapter.notifyDataSetChanged();
        break;
      case Tools.ADDUSER:
        adapterList.add((Map)msg.obj);
        adapter.notifyDataSetChanged();
        break;
      case Tools.SHOW:
        Toast.makeText(MainActivity.this, (String) msg.obj,
            Toast.LENGTH_SHORT).show();
        break;
      case Tools.DESTROYUSER:
        int i=(Integer) msg.obj;
        userList.remove(i);
        adapterList.remove(i);
        adapter.notifyDataSetChanged();
      case Tools.REFLESHCOUNT:
        String ip=msg.obj.toString();
        Tools.out("??????"+ip);
        for (int k = 0; k < adapterList.size(); k++) {
          if (adapterList.get(k).get("ip").equals("IP:"+ip)) 
          { // ????
            if(Tools.msgContainer.get(ip)==null)
            {
              adapterList.get(k).put("UnReadMsgCount", "");  
            }
            else {
              adapterList.get(k).put("UnReadMsgCount", "???:"+Tools.msgContainer.get(ip).size());  
              Tools.out("?????:"+Tools.msgContainer.get(ip));
            }
          }
        }
        adapter.notifyDataSetChanged();
        break;
      case Tools.CMD_STARTTALK:
        //????????
        receiveCall((Msg)msg.obj);
        break;
      case Tools.CMD_STOPTALK:
        //????????
        if(revTalkDialog!=null)
          revTalkDialog.dismiss();
        break;
      case Tools.CMD_FINISHMEDIA:
        //??????????????????????????????????
        Msg m0=(Msg)msg.obj;
        String ip0=m0.getSendUserIp();
        Tools.out("????????"+ip0);
        for (int k = 0; k < adapterList.size(); k++) {
          if (adapterList.get(k).get("ip").equals("IP:"+ip0)) 
          { // ????
            if(Tools.msgContainer.get(ip0)==null)
            {
              adapterList.get(k).put("UnReadMsgCount", "");  
            }
            else {
              adapterList.get(k).put("UnReadMsgCount", "???:"+Tools.msgContainer.get(ip0).size());  
              Tools.out("?????:"+Tools.msgContainer.get(ip0));
            }
          }
        }
        adapter.notifyDataSetChanged();
        break;
      case Tools.CMD_FILEREQUEST:
        //???????
        receiveFile((Msg)msg.obj);
        break;
      case Tools.FILE_JINDU:
        String[] pi = ((String) msg.obj).split(Tools.sign);
        fileSize = Double.parseDouble(pi[2]);
        proDia.setTitle(pi[0]);// ??????
        proDia.setMessage(pi[1] + " ?????"
            + FileAcitivity.getFormatSize(fileSize));// ??????????
        proDia.onStart();
        proDia.show();
        break;
      case Tools.PROGRESS_FLUSH:
        int i0 = (int) ((Tools.sendProgress / (fileSize)) * 100);
        proDia.setProgress(i0);
        break;
      case Tools.PROGRESS_COL:// ????????
        proDia.dismiss();
        break;
      }
    }
  };
  // ??????????????
  private void receiveFile(Msg mes)
  {
    this.m=mes;
    String str=mes.getBody().toString();
    new AlertDialog.Builder(MainActivity.this)
    .setTitle("???????????" + str.split(Tools.sign)[0] +" ?????"+FileAcitivity.getFormatSize(Double.parseDouble(str.split(Tools.sign)[1])))
    .setIcon(android.R.drawable.ic_dialog_info)
    .setPositiveButton("????", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        // ??????? ??????????? ????tcp ?????? ???????
        FileTcpServer ts = new FileTcpServer(MainActivity.this);
        ts.start();
        Tools.sendProgress = 0;
        Message m1 = new Message();
        m1.what = Tools.FILE_JINDU;
        m1.obj = "???????" + Tools.sign + "????????" + Tools.newfileName
            + Tools.sign + Tools.newfileSize;
        handler.sendMessage(m1);
        fileProgress();// ?????????????
        // ??????? ?????????????
        Msg msg=new Msg(0,Tools.me.getName(), Tools.me.getIp(), m.getSendUser(), m.getSendUserIp(),Tools.CMD_FILEACCEPT, null);
        tools.sendMsg(msg);
        return;
      }
    })
    .setNegativeButton("???", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        // ?????? ?????????????
        Msg msg=new Msg(0,Tools.me.getName(), Tools.me.getIp(), m.getSendUser(), m.getSendUserIp(),Tools.CMD_FILEREFUSE, null);
        tools.sendMsg(msg);
        return;
      }
    }).show();
  }
  // ????????????
  public void fileProgress() {
    new Thread() {
      public void run() {
        while (Tools.sendProgress != -1) {
          Message m = new Message();
          m.what = Tools.PROGRESS_FLUSH;
          handler.sendMessage(m);
          try {
            Thread.sleep(1000);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        // ????????
        Message m1 = new Message();
        m1.what = Tools.PROGRESS_COL;
        handler.sendMessage(m1);
      }
    }.start();
  }
  // ????????????
  private void receiveCall(Msg mes)
  {
    if(!Tools.stoptalk){
      this.m=mes;
      AlertDialog.Builder  builder = new AlertDialog.Builder(this);
      builder.setTitle("????:"+m.getSendUser());
      builder.setMessage(null);
      builder.setIcon(null);
      View vi = getLayoutInflater().inflate(R.layout.request_talk_layout, null);
      builder.setView(vi);
      revTalkDialog = builder.show();
      revTalkDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface arg0) {
          //???????????????
          Msg msg=new Msg();
          msg.setSendUser(Tools.me.getName());//?????????????????
          msg.setSendUserIp(Tools.me.getIp());
          msg.setReceiveUserIp(m.getSendUserIp());
          msg.setMsgType(Tools.CMD_STOPTALK);
          msg.setPackId(Tools.getTimel());
          tools.sendMsg(msg);
        }
      });
      Button talkOkBtn = (Button)vi.findViewById(R.id.receive_talk_okbtn);
      talkOkBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View okBtn) {
          if(!Tools.stoptalk){//???????????????????????????????????????
            Msg msg=new Msg();
            msg.setSendUser(Tools.me.getName());//?????????????????
            msg.setSendUserIp(Tools.me.getIp());
            msg.setReceiveUserIp(m.getSendUserIp());
            msg.setMsgType(Tools.CMD_ACCEPTTALK);
            msg.setPackId(Tools.getTimel());
            tools.sendMsg(msg);
            okBtn.setEnabled(false);
            //??????????????????????
            User person=new User(m.getSendUser(),m.getSendUserIp() );
            Tools.audio.audioSend(person);
          }
        }
      });
      Button talkCancelBtn = (Button)vi.findViewById(R.id.receive_talk_cancel);
      talkCancelBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View cancelBtn) {
          revTalkDialog.dismiss();
          
        }
      });
    }
  }
  @Override
    protected void onResume() {
      super.onResume();
      isPaused = false;
      Tools.out("Resume");
      reBroad();
    Tools.State=Tools.MAINACTIVITY;
    }
    @Override
    protected void onPause() {
      super.onPause();
      isPaused = false;
      Tools.out("PAUSE");
    }
    @Override
    protected void onDestroy() {
      super.onDestroy();
      isPaused=true;
      Tools.out("Destroy");
    }    
    //?????
    public void reBroad()
    {
      //??????(???????)
    Msg msg=new Msg();
    msg.setSendUser(Tools.me.getName());//?????????????????
    msg.setHeadIconPos(Tools.me.getHeadIconPos());
    msg.setSendUserIp(Tools.me.getIp());
    msg.setReceiveUserIp(Tools.getBroadCastIP());
    msg.setMsgType(Tools.CMD_ONLINE);//??????????
    msg.setPackId(Tools.getTimel());
    // ???????????
    tools.sendMsg(msg);
    }
}




Java Source Code List

com.myandroid.activity.ChatActivity.java
com.myandroid.activity.FileAcitivity.java
com.myandroid.activity.MainActivity.java
com.myandroid.activity.MediaDialog.java
com.myandroid.activity.SettingDialog.java
com.myandroid.util.Audio.java
com.myandroid.util.FileTcpClient.java
com.myandroid.util.FileTcpServer.java
com.myandroid.util.MediaTcpClient.java
com.myandroid.util.MediaTcpServer.java
com.myandroid.util.Media.java
com.myandroid.util.Msg.java
com.myandroid.util.Tools.java
com.myandroid.util.User.java