Skip to content

Commit

Permalink
HTTP API login discovery simplified
Browse files Browse the repository at this point in the history
Updated mdns service type strings
Decreased min SDK version to 16
  • Loading branch information
Alberto Geniola committed Jun 26, 2021
1 parent 583ff86 commit 784a69a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
7 changes: 5 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {

defaultConfig {
applicationId "com.albertogeniola.merossconf"
minSdkVersion 23
minSdkVersion 16
targetSdkVersion 30
versionCode 3
versionName "0.0.1-alpha2"
Expand All @@ -21,6 +21,9 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
}

}

Expand All @@ -44,7 +47,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'

implementation project(path: ':merosslib')
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'androidx.work:work-runtime:2.5.0'
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">

<!-- android:theme="@style/AppTheme.NoActionBar" -->
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar" />
android:theme="@style/Theme.MaterialComponents.NoActionBar"
/>
<activity
android:name=".PairActivity"
android:screenOrientation="portrait"
Expand Down
13 changes: 2 additions & 11 deletions app/src/main/java/com/albertogeniola/merossconf/AndroidUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.util.DisplayMetrics;

import androidx.core.app.ActivityCompat;
import androidx.core.location.LocationManagerCompat;

import com.albertogeniola.merosslib.model.Cipher;
import com.albertogeniola.merosslib.model.Encryption;
Expand All @@ -24,17 +25,7 @@
public class AndroidUtils {
public static boolean isLocationEnabled(Context context)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// This is new method provided in API 28
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return lm.isLocationEnabled();
} else {
// This is Deprecated in API 28
int mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
Settings.Secure.LOCATION_MODE_OFF);
return (mode != Settings.Secure.LOCATION_MODE_OFF);

}
return LocationManagerCompat.isLocationEnabled((LocationManager) context.getSystemService(Context.LOCATION_SERVICE));
}

public static String getConnectedWifi(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

public class LoginFragment extends Fragment {
// Constants
private static final String SERVICE_TYPE = "_meross-local-api._tcp.";
private static final String SERVICE_TYPE = "_meross-api._tcp.";
private static final String TAG = "Login";

// Instance attributes
Expand Down Expand Up @@ -146,11 +146,15 @@ private void startApiDiscovery() {

@Override
public void onDestroy() {
super.onDestroy();
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}

if (mDiscoveryInProgress)
mNsdManager.stopServiceDiscovery(mDiscoveryListener);

super.onDestroy();
}

private void configureUi(final boolean uiEnabled, final boolean discoveryInProgress, @Nullable final String hostnameValue, @Nullable final String message) {
Expand Down Expand Up @@ -189,7 +193,6 @@ public void run() {
@Override
public void onResume() {
super.onResume();
mDiscoveryInProgress = false;
startApiDiscovery();
}

Expand Down Expand Up @@ -271,20 +274,25 @@ public void onDiscoveryStarted(String serviceType) {
@Override
public void onServiceFound(NsdServiceInfo service) {
Log.d(TAG, "Service discovery success" + service);
/*
if (mDiscoveryInProgress)
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
*/
mNsdManager.resolveService(service, mResolveListener);
}

@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
configureUi(true, false, null, "Discovery failed");
mNsdManager.stopServiceDiscovery(this);
}

@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
mNsdManager.stopServiceDiscovery(this);
}

@Override
Expand All @@ -296,8 +304,6 @@ public void onDiscoveryStopped(String serviceType) {
@Override
public void onServiceLost(NsdServiceInfo serviceInfo) {
Log.e(TAG, "service lost: " + serviceInfo.getServiceType());
if (mDiscoveryInProgress)
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
}
};

Expand All @@ -321,6 +327,8 @@ private class TimeoutTask extends TimerTask {
public void run() {
configureUi(true, false, null, "No Local HTTP service found.");
mTimer = null;
if (mDiscoveryInProgress)
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
public class ConfigureWifiFragment extends AbstractWifiFragment {
private static final String TAG = "ConfigureWifiFragment";
private NsdManager mNsdManager;
private static final String SERVICE_TYPE = "_meross-local-mqtt._tcp.";
private static final String SERVICE_TYPE = "_meross-mqtt._tcp.";

private Handler mUiHandler;
private PairActivityViewModel pairActivityViewModel;
Expand Down

0 comments on commit 784a69a

Please sign in to comment.