Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions DarkChatter/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="comdarkchatter.github.darkchatter">

<uses-permission android:required="true" android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -13,12 +13,21 @@
android:name=".GroupActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".ChatActivity"
android:label="@string/title_activity_chat"
android:parentActivityName=".GroupActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="comdarkchatter.github.darkchatter.GroupActivity" />
</activity>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package comdarkchatter.github.darkchatter;

import android.app.Activity;
import android.net.nsd.NsdServiceInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import comdarkchatter.github.darkchatter.NsdHelper;

public class ChatActivity extends Activity {

NsdHelper mNsdHelper;

private TextView mStatusView;
private Handler mUpdateHandler;

public static final String TAG = "NsdChat";

ChatConnection mConnection;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Creating chat activity");
setContentView(R.layout.main);
mStatusView = (TextView) findViewById(R.id.status);

mUpdateHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
String chatLine = msg.getData().getString("msg");
addChatLine(chatLine);
}
};

}

public void clickAdvertise(View v) {
// Register service
if(mConnection.getLocalPort() > -1) {
mNsdHelper.registerService(mConnection.getLocalPort());
} else {
Log.d(TAG, "ServerSocket isn't bound.");
}
}

public void clickDiscover(View v) {
mNsdHelper.discoverServices();
}

public void clickConnect(View v) {
NsdServiceInfo service = mNsdHelper.getChosenServiceInfo();
if (service != null) {
Log.d(TAG, "Connecting.");
mConnection.connectToServer(service.getHost(),
service.getPort());
} else {
Log.d(TAG, "No service to connect to!");
}
}

public void clickSend(View v) {
EditText messageView = (EditText) this.findViewById(R.id.chatInput);
if (messageView != null) {
String messageString = messageView.getText().toString();
if (!messageString.isEmpty()) {
mConnection.sendMessage(messageString);
}
messageView.setText("");
}
}

public void addChatLine(String line) {
mStatusView.append("\n" + line);
}

@Override
protected void onStart() {
Log.d(TAG, "Starting.");
mConnection = new ChatConnection(mUpdateHandler);

mNsdHelper = new NsdHelper(this);
mNsdHelper.initializeNsd();
super.onStart();
}


@Override
protected void onPause() {
Log.d(TAG, "Pausing.");
if (mNsdHelper != null) {
mNsdHelper.stopDiscovery();
}
super.onPause();
}

@Override
protected void onResume() {
Log.d(TAG, "Resuming.");
super.onResume();
if (mNsdHelper != null) {
mNsdHelper.discoverServices();
}
}


// For KitKat and earlier releases, it is necessary to remove the
// service registration when the application is stopped. There's
// no guarantee that the onDestroy() method will be called (we're
// killable after onStop() returns) and the NSD service won't remove
// the registration for us if we're killed.

// In L and later, NsdService will automatically unregister us when
// our connection goes away when we're killed, so this step is
// optional (but recommended).

@Override
protected void onStop() {
Log.d(TAG, "Being stopped.");
mNsdHelper.tearDown();
mConnection.tearDown();
mNsdHelper = null;
mConnection = null;
super.onStop();
}

@Override
protected void onDestroy() {
Log.d(TAG, "Being destroyed.");
super.onDestroy();
}
}
Loading