• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
onCreate(Bundle savedInstanceState)44     public void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         Log.d(TAG, "Creating chat activity");
47         setContentView(R.layout.main);
48         mStatusView = (TextView) findViewById(R.id.status);
49 
50         mUpdateHandler = new Handler() {
51                 @Override
52             public void handleMessage(Message msg) {
53                 String chatLine = msg.getData().getString("msg");
54                 addChatLine(chatLine);
55             }
56         };
57 
58     }
59 
clickAdvertise(View v)60     public void clickAdvertise(View v) {
61         // Register service
62         if(mConnection.getLocalPort() > -1) {
63             mNsdHelper.registerService(mConnection.getLocalPort());
64         } else {
65             Log.d(TAG, "ServerSocket isn't bound.");
66         }
67     }
68 
clickDiscover(View v)69     public void clickDiscover(View v) {
70         mNsdHelper.discoverServices();
71     }
72 
clickConnect(View v)73     public void clickConnect(View v) {
74         NsdServiceInfo service = mNsdHelper.getChosenServiceInfo();
75         if (service != null) {
76             Log.d(TAG, "Connecting.");
77             mConnection.connectToServer(service.getHost(),
78                     service.getPort());
79         } else {
80             Log.d(TAG, "No service to connect to!");
81         }
82     }
83 
clickSend(View v)84     public void clickSend(View v) {
85         EditText messageView = (EditText) this.findViewById(R.id.chatInput);
86         if (messageView != null) {
87             String messageString = messageView.getText().toString();
88             if (!messageString.isEmpty()) {
89                 mConnection.sendMessage(messageString);
90             }
91             messageView.setText("");
92         }
93     }
94 
addChatLine(String line)95     public void addChatLine(String line) {
96         mStatusView.append("\n" + line);
97     }
98 
99     @Override
onStart()100     protected void onStart() {
101         Log.d(TAG, "Starting.");
102         mConnection = new ChatConnection(mUpdateHandler);
103 
104         mNsdHelper = new NsdHelper(this);
105         mNsdHelper.initializeNsd();
106         super.onStart();
107     }
108 
109 
110     @Override
onPause()111     protected void onPause() {
112         Log.d(TAG, "Pausing.");
113         if (mNsdHelper != null) {
114             mNsdHelper.stopDiscovery();
115         }
116         super.onPause();
117     }
118 
119     @Override
onResume()120     protected void onResume() {
121         Log.d(TAG, "Resuming.");
122         super.onResume();
123         if (mNsdHelper != null) {
124             mNsdHelper.discoverServices();
125         }
126     }
127 
128 
129     // For KitKat and earlier releases, it is necessary to remove the
130     // service registration when the application is stopped.  There's
131     // no guarantee that the onDestroy() method will be called (we're
132     // killable after onStop() returns) and the NSD service won't remove
133     // the registration for us if we're killed.
134 
135     // In L and later, NsdService will automatically unregister us when
136     // our connection goes away when we're killed, so this step is
137     // optional (but recommended).
138 
139     @Override
onStop()140     protected void onStop() {
141         Log.d(TAG, "Being stopped.");
142         mNsdHelper.tearDown();
143         mConnection.tearDown();
144         mNsdHelper = null;
145         mConnection = null;
146         super.onStop();
147     }
148 
149     @Override
onDestroy()150     protected void onDestroy() {
151         Log.d(TAG, "Being destroyed.");
152         super.onDestroy();
153     }
154 }
155