Skip to content

Commit f725633

Browse files
committed
Merge branch 'task/improvements_and_fixes' into 'master'
Improvements and minor fixes See merge request app-frameworks/esp-rainmaker-android!38
2 parents 0571819 + 341c48a commit f725633

23 files changed

+773
-377
lines changed

app/build.gradle

+13-7
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ android {
125125
}
126126
}
127127

128-
buildToolsVersion '28.0.3'
128+
buildToolsVersion '29.0.3'
129+
130+
compileOptions {
131+
sourceCompatibility JavaVersion.VERSION_1_8
132+
targetCompatibility JavaVersion.VERSION_1_8
133+
}
129134
}
130135

131136
protobuf {
@@ -152,18 +157,18 @@ protobuf {
152157
dependencies {
153158

154159
implementation fileTree(include: ['*.jar'], dir: 'libs')
155-
implementation 'androidx.appcompat:appcompat:1.2.0'
156-
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
160+
implementation 'androidx.appcompat:appcompat:1.3.1'
161+
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
157162
implementation 'com.google.android.material:material:1.1.0'
158-
implementation 'androidx.recyclerview:recyclerview:1.1.0'
163+
implementation 'androidx.recyclerview:recyclerview:1.2.1'
159164
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
160165
implementation 'com.larswerkman:HoloColorPicker:1.5'
161166

162167
implementation 'com.google.protobuf:protobuf-lite:3.0.1'
163168
implementation 'com.google.crypto.tink:tink-android:1.1.0'
164169

165170
implementation 'com.wang.avi:library:2.1.3'
166-
implementation 'org.greenrobot:eventbus:3.1.1'
171+
implementation 'org.greenrobot:eventbus:3.2.0'
167172
implementation 'com.auth0.android:jwtdecode:1.4.0'
168173
implementation 'com.google.code.gson:gson:2.8.6'
169174
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
@@ -175,13 +180,14 @@ dependencies {
175180
implementation 'com.budiyev.android:code-scanner:2.1.0'
176181
implementation 'com.github.espressif:esp-idf-provisioning-android:lib-2.0.9'
177182
implementation 'com.github.warkiz.tickseekbar:tickseekbar:0.1.4'
183+
implementation 'com.github.abdularis:TapHoldUpButton:0.1.2'
178184

179185
// Room dependencies
180186
def room_version = "2.3.0"
181187
implementation "android.arch.persistence.room:runtime:$room_version"
182188
annotationProcessor "android.arch.persistence.room:compiler:$room_version"
183189

184190
testImplementation 'junit:junit:4.13.2'
185-
androidTestImplementation 'androidx.test:runner:1.3.0'
186-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
191+
androidTestImplementation 'androidx.test:runner:1.4.0'
192+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
187193
}

app/src/main/AndroidManifest.xml

-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
1515
<uses-permission android:name="android.permission.BLUETOOTH" />
1616
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
17-
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
1817

1918
<supports-screens
2019
android:anyDensity="true"
@@ -38,7 +37,6 @@
3837

3938
<activity
4039
android:name="com.espressif.ui.activities.SplashActivity"
41-
android:screenOrientation="portrait"
4240
android:theme="@android:style/Theme.NoDisplay">
4341

4442
<intent-filter>

app/src/main/java/com/espressif/AppConstants.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public enum UpdateEventType {
4343
EVENT_DEVICE_REMOVED,
4444
EVENT_ADD_DEVICE_TIME_OUT,
4545
EVENT_DEVICE_STATUS_UPDATE,
46-
EVENT_STATE_CHANGE_UPDATE
46+
EVENT_STATE_CHANGE_UPDATE,
47+
EVENT_LOCAL_DEVICE_UPDATE
4748
}
4849

4950
public static final String CURRENT_VERSION = "v1";
@@ -91,6 +92,8 @@ public enum UpdateEventType {
9192
public static final String UI_TYPE_HUE_SLIDER = "esp.ui.hue-slider";
9293
public static final String UI_TYPE_HUE_CIRCLE = "esp.ui.hue-circle";
9394
public static final String UI_TYPE_DROP_DOWN = "esp.ui.dropdown";
95+
public static final String UI_TYPE_HIDDEN = "esp.ui.hidden";
96+
public static final String UI_TYPE_TRIGGER = "esp.ui.trigger";
9497

9598
// ESP Device Types
9699
public static final String ESP_DEVICE_SWITCH = "esp.device.switch";

app/src/main/java/com/espressif/EspApplication.java

+138
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
import android.content.Intent;
2020
import android.content.SharedPreferences;
2121
import android.os.Bundle;
22+
import android.text.TextUtils;
2223
import android.util.Log;
2324

2425
import com.espressif.cloudapi.ApiManager;
2526
import com.espressif.cloudapi.ApiResponseListener;
2627
import com.espressif.db.EspDatabase;
28+
import com.espressif.mdns.mDNSApiManager;
2729
import com.espressif.mdns.mDNSDevice;
30+
import com.espressif.mdns.mDNSManager;
2831
import com.espressif.provisioning.ESPProvisionManager;
2932
import com.espressif.rainmaker.BuildConfig;
3033
import com.espressif.ui.activities.MainActivity;
@@ -34,6 +37,8 @@
3437
import com.espressif.ui.models.UpdateEvent;
3538

3639
import org.greenrobot.eventbus.EventBus;
40+
import org.json.JSONException;
41+
import org.json.JSONObject;
3742

3843
import java.util.HashMap;
3944

@@ -50,6 +55,7 @@ public class EspApplication extends Application {
5055

5156
private SharedPreferences appPreferences;
5257
private ApiManager apiManager;
58+
private mDNSManager mdnsManager;
5359

5460
public enum AppState {
5561
NO_USER_LOGIN,
@@ -71,6 +77,9 @@ public void onCreate() {
7177
appPreferences = getSharedPreferences(AppConstants.ESP_PREFERENCES, Context.MODE_PRIVATE);
7278
apiManager = ApiManager.getInstance(this);
7379
ESPProvisionManager.getInstance(this);
80+
if (BuildConfig.isLocalControlSupported) {
81+
mdnsManager = mDNSManager.getInstance(getApplicationContext(), AppConstants.MDNS_SERVICE_TYPE, listener);
82+
}
7483
}
7584

7685
public AppState getAppState() {
@@ -81,6 +90,9 @@ public void changeAppState(AppState newState, Bundle extras) {
8190

8291
switch (newState) {
8392
case GETTING_DATA:
93+
if (BuildConfig.isLocalControlSupported) {
94+
mdnsManager.initializeNsd();
95+
}
8496
case REFRESH_DATA:
8597
if (!appState.equals(newState)) {
8698
appState = newState;
@@ -96,6 +108,7 @@ public void changeAppState(AppState newState, Bundle extras) {
96108
updateEvent.setData(extras);
97109
}
98110
EventBus.getDefault().post(updateEvent);
111+
startLocalDeviceDiscovery();
99112
break;
100113

101114
case NO_USER_LOGIN:
@@ -111,6 +124,7 @@ public void changeAppState(AppState newState, Bundle extras) {
111124
case NO_INTERNET:
112125
appState = newState;
113126
EventBus.getDefault().post(new UpdateEvent(AppConstants.UpdateEventType.EVENT_STATE_CHANGE_UPDATE));
127+
startLocalDeviceDiscovery();
114128
break;
115129
}
116130
}
@@ -168,6 +182,15 @@ public void refreshData() {
168182
}
169183
}
170184

185+
public void loginSuccess() {
186+
EspDatabase.getInstance(this).getNodeDao().deleteAll();
187+
EspDatabase.getInstance(this).getGroupDao().deleteAll();
188+
nodeMap.clear();
189+
scheduleMap.clear();
190+
mDNSDeviceMap.clear();
191+
groupMap.clear();
192+
}
193+
171194
public void logout() {
172195
// Do logout and clear all data
173196
if (!ApiManager.isOAuthLogin) {
@@ -208,4 +231,119 @@ public void onNetworkFailure(Exception exception) {
208231
Log.e(TAG, "Deleted all things from local storage.");
209232
changeAppState(AppState.NO_USER_LOGIN, null);
210233
}
234+
235+
private void startLocalDeviceDiscovery() {
236+
if (BuildConfig.isLocalControlSupported) {
237+
if (nodeMap.size() > 0) {
238+
mdnsManager.discoverServices();
239+
}
240+
}
241+
}
242+
243+
public void stopLocalDeviceDiscovery() {
244+
if (BuildConfig.isLocalControlSupported) {
245+
mdnsManager.stopDiscovery();
246+
}
247+
}
248+
249+
mDNSManager.mDNSEvenListener listener = new mDNSManager.mDNSEvenListener() {
250+
251+
@Override
252+
public void deviceFound(final mDNSDevice dnsDevice) {
253+
254+
Log.e(TAG, "Device Found on Local Network");
255+
final String url = "http://" + dnsDevice.getIpAddr() + ":" + dnsDevice.getPort();
256+
final mDNSApiManager dnsMsgHelper = new mDNSApiManager(getApplicationContext());
257+
258+
dnsMsgHelper.getPropertyCount(url, AppConstants.LOCAL_CONTROL_PATH, new ApiResponseListener() {
259+
260+
@Override
261+
public void onSuccess(Bundle data) {
262+
263+
if (data != null) {
264+
265+
int count = data.getInt(AppConstants.KEY_PROPERTY_COUNT, 0);
266+
dnsDevice.setPropertyCount(count);
267+
268+
dnsMsgHelper.getPropertyValues(url, AppConstants.LOCAL_CONTROL_PATH, count, new ApiResponseListener() {
269+
270+
@Override
271+
public void onSuccess(Bundle data) {
272+
273+
if (data != null) {
274+
275+
String configData = data.getString(AppConstants.KEY_CONFIG);
276+
String paramsData = data.getString(AppConstants.KEY_PARAMS);
277+
278+
Log.d(TAG, "Config data : " + configData);
279+
Log.d(TAG, "Params data : " + paramsData);
280+
281+
if (!TextUtils.isEmpty(configData)) {
282+
283+
JSONObject configJson = null;
284+
try {
285+
configJson = new JSONObject(configData);
286+
} catch (JSONException e) {
287+
e.printStackTrace();
288+
}
289+
290+
String nodeId = configJson.optString(AppConstants.KEY_NODE_ID);
291+
EspNode node = nodeMap.get(nodeId);
292+
boolean isDeviceFound = false;
293+
if (node != null) {
294+
isDeviceFound = true;
295+
}
296+
EspNode localNode = JsonDataParser.setNodeConfig(node, configJson);
297+
298+
if (node != null) {
299+
Log.e(TAG, "Found node " + localNode.getNodeId() + " on local network.");
300+
isDeviceFound = true;
301+
localNode.setAvailableLocally(true);
302+
localNode.setIpAddress(dnsDevice.getIpAddr());
303+
localNode.setPort(dnsDevice.getPort());
304+
localNode.setOnline(true);
305+
mDNSDeviceMap.put(localNode.getNodeId(), dnsDevice);
306+
}
307+
308+
if (!TextUtils.isEmpty(paramsData) && isDeviceFound) {
309+
310+
JSONObject paramsJson = null;
311+
try {
312+
paramsJson = new JSONObject(paramsData);
313+
} catch (JSONException e) {
314+
e.printStackTrace();
315+
}
316+
JsonDataParser.setAllParams(EspApplication.this, localNode, paramsJson);
317+
nodeMap.put(localNode.getNodeId(), localNode);
318+
EventBus.getDefault().post(new UpdateEvent(AppConstants.UpdateEventType.EVENT_LOCAL_DEVICE_UPDATE));
319+
}
320+
}
321+
}
322+
}
323+
324+
@Override
325+
public void onResponseFailure(Exception exception) {
326+
// Nothing to do
327+
}
328+
329+
@Override
330+
public void onNetworkFailure(Exception exception) {
331+
// Nothing to do
332+
}
333+
});
334+
}
335+
}
336+
337+
@Override
338+
public void onResponseFailure(Exception exception) {
339+
// Nothing to do
340+
}
341+
342+
@Override
343+
public void onNetworkFailure(Exception exception) {
344+
// Nothing to do
345+
}
346+
});
347+
}
348+
};
211349
}

app/src/main/java/com/espressif/JsonDataParser.java

+5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public static void setDeviceParamValue(JSONObject deviceJson, Device device, Par
7676
boolean value = deviceJson.optBoolean(paramName);
7777
param.setSwitchStatus(value);
7878

79+
} else if (AppConstants.UI_TYPE_TRIGGER.equalsIgnoreCase(param.getUiType())) {
80+
81+
boolean value = deviceJson.optBoolean(paramName);
82+
param.setSwitchStatus(value);
83+
7984
} else if (AppConstants.UI_TYPE_DROP_DOWN.equalsIgnoreCase(param.getUiType())) {
8085

8186
String labelValue = "";

app/src/main/java/com/espressif/cloudapi/ApiManager.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,6 @@ public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
133133
refreshToken = jsonObject.getString("refreshtoken");
134134
isOAuthLogin = false;
135135

136-
EspDatabase.getInstance(espApp).getNodeDao().deleteAll();
137-
EspDatabase.getInstance(espApp).getGroupDao().deleteAll();
138-
espApp.nodeMap.clear();
139-
espApp.scheduleMap.clear();
140-
espApp.mDNSDeviceMap.clear();
141-
espApp.groupMap.clear();
142-
143136
SharedPreferences.Editor editor = sharedPreferences.edit();
144137
editor.putString(AppConstants.KEY_EMAIL, userName);
145138
editor.putString(AppConstants.KEY_ID_TOKEN, idToken);
@@ -149,6 +142,7 @@ public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
149142
editor.apply();
150143

151144
getTokenAndUserId();
145+
espApp.loginSuccess();
152146
listener.onSuccess(null);
153147
} else {
154148
String jsonErrResponse = response.errorBody().string();
@@ -1619,7 +1613,6 @@ public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
16191613

16201614
String jsonResponse = response.body().string();
16211615
Log.d(TAG, "onResponse Success : " + jsonResponse);
1622-
JSONObject jsonObject = new JSONObject(jsonResponse);
16231616
listener.onSuccess(null);
16241617

16251618
} else {
@@ -1630,9 +1623,6 @@ public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
16301623
String jsonErrResponse = response.errorBody().string();
16311624
processError(jsonErrResponse, listener, "Failed to update param value");
16321625
}
1633-
} catch (JSONException e) {
1634-
e.printStackTrace();
1635-
listener.onResponseFailure(e);
16361626
} catch (IOException e) {
16371627
e.printStackTrace();
16381628
listener.onResponseFailure(e);

app/src/main/java/com/espressif/mdns/mDNSApiManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ public void onSuccess(Bundle data) {
9494

9595
if (!TextUtils.isEmpty(paramsData) && isDeviceFound) {
9696

97-
espApp.nodeMap.put(node.getNodeId(), localNode);
9897
JSONObject paramsJson = null;
9998
try {
10099
paramsJson = new JSONObject(paramsData);
101100
} catch (JSONException e) {
102101
e.printStackTrace();
103102
}
104103
JsonDataParser.setAllParams(espApp, localNode, paramsJson);
104+
espApp.nodeMap.put(node.getNodeId(), localNode);
105105
listener.onSuccess(null);
106106
}
107107
}

app/src/main/java/com/espressif/mdns/mDNSManager.java

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ private mDNSManager(Context context, String serviceType, mDNSEvenListener listen
7272

7373
// Initialize Listeners
7474
public void initializeNsd() {
75+
Log.d(TAG, "Initialize Network service discovery");
7576
// Initialize only resolve listener
7677
initializeResolveListener();
7778
}

0 commit comments

Comments
 (0)