Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checks for 'ethernet' connectivity #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public void onReceive(Context context, Intent intent) {
if(GidderCommons.isSshServiceRunning(context)) {
context.stopService(new Intent(C.action.START_SSH_SERVER));
Log.i(TAG, "Broadcast - stop service!");
} else if (!GidderCommons.isWifiReady(context)) {
Toast.makeText(context, "WiFi is NOT connected!", Toast.LENGTH_SHORT).show();
Log.i(TAG, "Broadcast failed - wifi is NOT connected!");
} else if (!GidderCommons.isNetworkReady(context)) {
Toast.makeText(context, "Network is NOT ready!", Toast.LENGTH_SHORT).show();
Log.i(TAG, "Broadcast failed - network is NOT ready!");
} else {
context.startService(new Intent(C.action.START_SSH_SERVER));
Log.i(TAG, "Broadcast - start service!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,11 @@ public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();

if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
if (GidderCommons.isWifiReady(context)) {
wifiStatusTextView.setText("WiFi connected to");
wifiSSIDTextView.setText(GidderCommons.getWifiSSID(context));
wirelessImageView.setImageResource(R.drawable.ic_wireless_enabled);
startStopButton.setBackgroundResource(R.drawable.blue_btn_selector);
} else {
wifiStatusTextView.setText("WiFi is NOT connected");
wifiSSIDTextView.setText("");
wirelessImageView.setImageResource(R.drawable.ic_wireless_disabled);
startStopButton.setBackgroundResource(R.drawable.white_btn_selector);
}
showWifiStatus(context);
}
}
};

private BroadcastReceiver sshdBroadcastReceiver = new BroadcastReceiver() {

@Override
Expand All @@ -83,8 +73,7 @@ public void onReceive(Context context, Intent intent) {
animationSet.addAnimation(fadeInAnimation);
animationSet.setInterpolator(AnimationUtils.loadInterpolator(HomeActivity.this, android.R.anim.overshoot_interpolator));

homeServerInfoTextView.setText(GidderCommons.getCurrentWifiIpAddress(HomeActivity.this) + ":" +
prefs.getString(PrefsConstants.SSH_PORT.getKey(), PrefsConstants.SSH_PORT.getDefaultValue()));
homeServerInfoTextView.setText(GidderCommons.getCurrentServerAddress(HomeActivity.this,prefs));

homeServerInfoTextView.startAnimation(animationSet);
homeServerInfoTextView.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -134,19 +123,9 @@ protected void initComponents(Bundle savedInstanceState) {
wirelessImageView = (ImageView) findViewById(R.id.homeWirelessImage);
wifiStatusTextView = (TextView) findViewById(R.id.homeWifiStatus);
wifiSSIDTextView = (TextView) findViewById(R.id.homeWifiSSID);

if(GidderCommons.isWifiReady(this)) {
wifiStatusTextView.setText("WiFi connected to");
wifiSSIDTextView.setText(GidderCommons.getWifiSSID(this));
wirelessImageView.setImageResource(R.drawable.ic_wireless_enabled);
startStopButton.setBackgroundResource(R.drawable.blue_btn_selector);
} else {
wifiStatusTextView.setText("WiFi is NOT connected");
wifiSSIDTextView.setText("");
wirelessImageView.setImageResource(R.drawable.ic_wireless_disabled);
startStopButton.setBackgroundResource(R.drawable.white_btn_selector);
}


showWifiStatus(this);

homeServerInfoTextView = (TextView) findViewById(R.id.homeServerInfoTextView);

if(isSshServiceRunning) {
Expand Down Expand Up @@ -250,7 +229,7 @@ public void onClick(View v) {

Intent intent = new Intent(C.action.START_SSH_SERVER);
if(!isSshServiceRunning) {
if (!GidderCommons.isWifiReady(HomeActivity.this)) {
if (!GidderCommons.isNetworkReady(HomeActivity.this)) {
return;
}

Expand All @@ -260,5 +239,24 @@ public void onClick(View v) {
}
}
}


private void showWifiStatus(Context context) {
if (GidderCommons.isWifiReady(context)) {
wifiStatusTextView.setText("WiFi connected to");
wifiSSIDTextView.setText(GidderCommons.getWifiSSID(context));
wirelessImageView.setImageResource(R.drawable.ic_wireless_enabled);
startStopButton.setBackgroundResource(R.drawable.blue_btn_selector);
} else if (GidderCommons.isEthernetConnected(context)) {
wifiStatusTextView.setText("Ethernet connected");
wifiSSIDTextView.setText("");
wirelessImageView.setImageResource(R.drawable.ic_wireless_enabled);
startStopButton.setBackgroundResource(R.drawable.blue_btn_selector);
} else {
wifiStatusTextView.setText("WiFi is NOT connected");
wifiSSIDTextView.setText("");
wirelessImageView.setImageResource(R.drawable.ic_wireless_disabled);
startStopButton.setBackgroundResource(R.drawable.white_btn_selector);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
Expand All @@ -26,6 +27,11 @@
import android.util.Log;
import android.view.WindowManager;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public abstract class GidderCommons {
private final static int SSH_STARTED_NOTIFICATION_ID = 1;

Expand Down Expand Up @@ -75,6 +81,17 @@ public static boolean isWifiReady(Context context) {
}
}

public static boolean isNetworkReady(Context context) {
return isWifiReady(context) || isEthernetConnected(context);
}

public static boolean isEthernetConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
//NetworkInfo info = connectivityManager.getActiveNetworkInfo();
return info != null && info.isConnected();
}

public static boolean isWifiConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
Expand Down Expand Up @@ -124,13 +141,55 @@ public static byte[] reverse(byte[] array) {
return reversedArray;
}

public static String getCurrentAddress(Context context) {
String addr = getCurrentWifiIpAddress(context);
if (addr == null) {
return getFirstNonLoopbackAddress();
}
return addr;
}

public static String getFirstNonLoopbackAddress() {
String result = "";
try {

for(Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();interfaces.hasMoreElements();) {
NetworkInterface ni = interfaces.nextElement();
for (Enumeration<InetAddress> iaddress = ni.getInetAddresses(); iaddress.hasMoreElements(); ) {
InetAddress ia = iaddress.nextElement();
byte[] addr = ia.getAddress();
Log.i("Commons", "interface address:" + ia.toString() + " host:" + ia.getHostAddress()+ " loopback:"+ia.isLoopbackAddress()+
" addr:"+formatIpAddress(addr));
if (!ia.isLoopbackAddress() && addr != null && addr.length == 4) {
result = formatIpAddress(addr);
}
}
}

} catch (Exception e) {
Log.e("Commons", "getFirstNonLoopbackAddress: '" + e.getMessage()+ "'",e);
}
return result;
}

public static String getCurrentWifiIpAddress(Context context) {
WifiManager myWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
int ipAddress = myWifiInfo.getIpAddress();
if (ipAddress != 0) {
return formatIpAddress(ipAddress);
} else {
return null;
}
}

private static String formatIpAddress(int ipAddress) {
byte[] addr = GidderCommons.convertIntToInet4Addr(ipAddress);
return formatIpAddress(addr);
}

private static String formatIpAddress(byte[] addr) {
StringBuffer addressBuffer = new StringBuffer();
for (byte b : addr) {
if (!(addressBuffer.length() == 0)) {
Expand All @@ -142,6 +201,10 @@ public static String getCurrentWifiIpAddress(Context context) {
return addressBuffer.toString();
}

public static String getCurrentServerAddress(Context context,SharedPreferences prefs) {
return getCurrentAddress(context) + ':' + prefs.getString(PrefsConstants.SSH_PORT.getKey(), PrefsConstants.SSH_PORT.getDefaultValue());
}

public static String generateSha1(String data) {
byte[] dataBytes = data.getBytes();

Expand Down Expand Up @@ -187,16 +250,14 @@ public static void makeStatusBarNotification(Context context) {

PendingIntent contentIntent = PendingIntent.getActivity(context, 1, notificationIntent, 0);

String currentIpAddress = GidderCommons.getCurrentWifiIpAddress(context);
String sshPort = PreferenceManager.getDefaultSharedPreferences(context)
.getString(PrefsConstants.SSH_PORT.getKey(), PrefsConstants.SSH_PORT.getDefaultValue());

String currentAddress = GidderCommons.getCurrentServerAddress(context, PreferenceManager.getDefaultSharedPreferences(context));

Notification notification = new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_SOUND)
.setTicker("SSH server started!")
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_stat_notification)
.setContentText(currentIpAddress + ":" + sshPort)
.setContentText(currentAddress)
.setContentTitle("SSH server is running")
.setOngoing(true)
.setWhen(System.currentTimeMillis())
Expand Down