Home | History | Annotate | Download | only in nsdchat
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.nsdchat;
     18 
     19 import android.app.Activity;
     20 import android.net.nsd.NsdServiceInfo;
     21 import android.os.Bundle;
     22 import android.os.Handler;
     23 import android.os.Message;
     24 import android.util.Log;
     25 import android.view.View;
     26 import android.widget.EditText;
     27 import android.widget.TextView;
     28 
     29 import com.example.android.nsdchat.NsdHelper;
     30 
     31 public class NsdChatActivity extends Activity {
     32 
     33     NsdHelper mNsdHelper;
     34 
     35     private TextView mStatusView;
     36     private Handler mUpdateHandler;
     37 
     38     public static final String TAG = "NsdChat";
     39 
     40     ChatConnection mConnection;
     41 
     42     /** Called when the activity is first created. */
     43     @Override
     44     public void onCreate(Bundle savedInstanceState) {
     45         super.onCreate(savedInstanceState);
     46         setContentView(R.layout.main);
     47         mStatusView = (TextView) findViewById(R.id.status);
     48 
     49         mUpdateHandler = new Handler() {
     50                 @Override
     51             public void handleMessage(Message msg) {
     52                 String chatLine = msg.getData().getString("msg");
     53                 addChatLine(chatLine);
     54             }
     55         };
     56 
     57         mConnection = new ChatConnection(mUpdateHandler);
     58 
     59         mNsdHelper = new NsdHelper(this);
     60         mNsdHelper.initializeNsd();
     61 
     62     }
     63 
     64     public void clickAdvertise(View v) {
     65         // Register service
     66         if(mConnection.getLocalPort() > -1) {
     67             mNsdHelper.registerService(mConnection.getLocalPort());
     68         } else {
     69             Log.d(TAG, "ServerSocket isn't bound.");
     70         }
     71     }
     72 
     73     public void clickDiscover(View v) {
     74         mNsdHelper.discoverServices();
     75     }
     76 
     77     public void clickConnect(View v) {
     78         NsdServiceInfo service = mNsdHelper.getChosenServiceInfo();
     79         if (service != null) {
     80             Log.d(TAG, "Connecting.");
     81             mConnection.connectToServer(service.getHost(),
     82                     service.getPort());
     83         } else {
     84             Log.d(TAG, "No service to connect to!");
     85         }
     86     }
     87 
     88     public void clickSend(View v) {
     89         EditText messageView = (EditText) this.findViewById(R.id.chatInput);
     90         if (messageView != null) {
     91             String messageString = messageView.getText().toString();
     92             if (!messageString.isEmpty()) {
     93                 mConnection.sendMessage(messageString);
     94             }
     95             messageView.setText("");
     96         }
     97     }
     98 
     99     public void addChatLine(String line) {
    100         mStatusView.append("\n" + line);
    101     }
    102 
    103     @Override
    104     protected void onPause() {
    105         if (mNsdHelper != null) {
    106             mNsdHelper.stopDiscovery();
    107         }
    108         super.onPause();
    109     }
    110 
    111     @Override
    112     protected void onResume() {
    113         super.onResume();
    114         if (mNsdHelper != null) {
    115             mNsdHelper.discoverServices();
    116         }
    117     }
    118 
    119     @Override
    120     protected void onDestroy() {
    121         mNsdHelper.tearDown();
    122         mConnection.tearDown();
    123         super.onDestroy();
    124     }
    125 }
    126