Skip to content

Commit 47bf717

Browse files
close some url connections and streams. add connectivity change to test-app manifest. add a few more exception handlers.
1 parent 6d3ba62 commit 47bf717

File tree

10 files changed

+54
-15
lines changed

10 files changed

+54
-15
lines changed

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class OptimizelyClient {
4747
private final Logger logger;
4848

4949
@Nullable private Optimizely optimizely;
50-
@Nullable private Map<String, String> defaultAttributes;
50+
@NonNull private Map<String, String> defaultAttributes;
5151

5252
OptimizelyClient(@Nullable Optimizely optimizely, @NonNull Logger logger) {
5353
this.optimizely = optimizely;
@@ -86,7 +86,7 @@ protected void setDefaultAttributes(@NonNull Map<String, String> attrs) {
8686
* @return a new map of both the default attributes and attributes passed in.
8787
*/
8888
private Map<String, String> getAllAttributes(@NonNull Map<String, String> attrs) {
89-
Map<String,String> combinedMap = new HashMap<String,String>(defaultAttributes);
89+
Map<String,String> combinedMap = new HashMap<>(defaultAttributes);
9090

9191
// this essentially overrides defaultAttributes if the attrs passed in have the same key.
9292
combinedMap.putAll(attrs);

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyDefaultAttributes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static Map<String, String> buildDefaultAttributesMap(Context context, Logger log
7070
}
7171

7272

73-
Map<String, String> attrMap = new HashMap<String, String>();
73+
Map<String, String> attrMap = new HashMap<>();
7474

7575
attrMap.put(DEVICE_MODEL_KEY, androidDeviceModel);
7676
attrMap.put(SDK_VERSION_KEY, androidSdkVersionName);

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public class OptimizelyManager {
5858
LoggerFactory.getLogger(OptimizelyClient.class));
5959

6060
@NonNull private DatafileHandler datafileHandler;
61-
@NonNull private final long datafileDownloadInterval;
62-
@NonNull private final long eventDispatchInterval;
61+
private final long datafileDownloadInterval;
62+
private final long eventDispatchInterval;
6363
@Nullable private EventHandler eventHandler = null;
6464
@NonNull private ErrorHandler errorHandler;
6565
@NonNull private Logger logger;
@@ -323,6 +323,7 @@ public String getProjectId() {
323323
return projectId;
324324
}
325325

326+
@NonNull
326327
public DatafileHandler getDatafileHandler() {
327328
return datafileHandler;
328329
}
@@ -390,6 +391,7 @@ private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull Stri
390391
return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class));
391392
}
392393

394+
@NonNull
393395
@VisibleForTesting
394396
public UserProfileService getUserProfileService() {
395397
return userProfileService;

datafile-handler/src/main/java/com/optimizely/ab/android/datafile_handler/DatafileClient.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ public String execute() {
8989
return null;
9090
} finally {
9191
if (urlConnection != null) {
92-
urlConnection.disconnect();
92+
try {
93+
urlConnection.disconnect();
94+
}
95+
catch (Exception e) {
96+
logger.error("Error closing connection", e);
97+
}
9398
}
9499
}
95100
}

event-handler/src/main/java/com/optimizely/ab/android/event_handler/Event.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
* Event model
2323
*/
2424
class Event {
25-
private URL url;
26-
private String requestBody;
25+
private final URL url;
26+
private final String requestBody;
2727

2828
Event(URL url, String requestBody) {
2929
this.url = url;

event-handler/src/main/java/com/optimizely/ab/android/event_handler/EventClient.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
* Makes network requests related to events
3131
*/
3232
class EventClient {
33-
private Client client;
33+
private final Client client;
3434
// Package private and non final so it can easily be mocked for tests
35-
private Logger logger;
35+
private final Logger logger;
3636

3737
EventClient(Client client, Logger logger) {
3838
this.client = client;
@@ -43,9 +43,10 @@ boolean sendEvent(final Event event) {
4343
Client.Request<Boolean> request = new Client.Request<Boolean>() {
4444
@Override
4545
public Boolean execute() {
46+
HttpURLConnection urlConnection = null;
4647
try {
4748
logger.info("Dispatching event: {}", event);
48-
HttpURLConnection urlConnection = client.openConnection(event.getURL());
49+
urlConnection = client.openConnection(event.getURL());
4950

5051
if (urlConnection == null) {
5152
return Boolean.FALSE;
@@ -71,6 +72,20 @@ public Boolean execute() {
7172
logger.error("Unable to send event: {}", event, e);
7273
return Boolean.FALSE;
7374
}
75+
catch (Exception e) {
76+
logger.error("Unable to send event: {}", event, e);
77+
return Boolean.FALSE;
78+
}
79+
finally {
80+
if (urlConnection != null) {
81+
try {
82+
urlConnection.disconnect();
83+
}
84+
catch (Exception e) {
85+
logger.error("Unable to close connection", e);
86+
}
87+
}
88+
}
7489
}
7590
};
7691

event-handler/src/main/java/com/optimizely/ab/android/event_handler/EventDAO.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
*/
3838
class EventDAO {
3939

40-
@NonNull Logger logger;
41-
@NonNull private EventSQLiteOpenHelper dbHelper;
40+
@NonNull
41+
final Logger logger;
42+
@NonNull private final EventSQLiteOpenHelper dbHelper;
4243

4344
private EventDAO(@NonNull EventSQLiteOpenHelper dbHelper, @NonNull Logger logger) {
4445
this.dbHelper = dbHelper;

shared/src/main/java/com/optimizely/ab/android/shared/Cache.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public String load(String filename) {
108108
} catch (IOException e) {
109109
logger.warn("Unable to close file {}.", filename);
110110
}
111+
catch (Exception e) {
112+
logger.warn("Unable to close file {}.", filename);
113+
}
111114
}
112115
}
113116

@@ -135,6 +138,10 @@ public boolean save(String filename, String data) {
135138
} catch (IOException e) {
136139
logger.warn("Unable to close file {}.", filename);
137140
}
141+
catch (Exception e) {
142+
logger.warn("Unable to close file {}.", filename);
143+
144+
}
138145
}
139146
}
140147
}

shared/src/main/java/com/optimizely/ab/android/shared/Client.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,21 @@ public void saveLastModified(@NonNull URLConnection urlConnection) {
100100

101101
@Nullable
102102
public String readStream(@NonNull URLConnection urlConnection) {
103+
Scanner scanner = null;
103104
try {
104105
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
105-
Scanner s = new Scanner(in).useDelimiter("\\A");
106-
return s.hasNext() ? s.next() : "";
106+
scanner = new Scanner(in).useDelimiter("\\A");
107+
return scanner.hasNext() ? scanner.next() : "";
107108
} catch (Exception e) {
108109
logger.warn("Error reading urlConnection stream.", e);
109110
return null;
110111
}
112+
finally {
113+
if (scanner != null) {
114+
// We assume that closing the scanner will close the associated input stream.
115+
scanner.close();
116+
}
117+
}
111118
}
112119

113120
/**

test-app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<intent-filter>
6060
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
6161
<action android:name="android.intent.action.BOOT_COMPLETED" />
62+
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
6263
</intent-filter>
6364
</receiver>
6465
<!--
@@ -72,6 +73,7 @@
7273
<intent-filter>
7374
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
7475
<action android:name="android.intent.action.BOOT_COMPLETED" />
76+
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
7577
</intent-filter>
7678
</receiver>
7779

0 commit comments

Comments
 (0)