Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit f406ecd

Browse files
committed
android code cleanup
1 parent e0b9266 commit f406ecd

File tree

10 files changed

+120
-147
lines changed

10 files changed

+120
-147
lines changed

android/app/src/main/java/com/microsoft/codepush/react/CodePush.java

Lines changed: 68 additions & 79 deletions
Large diffs are not rendered by default.

android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class CodePushDialog extends ReactContextBaseJavaModule{
1313

14-
Activity mainActivity;
14+
private Activity mainActivity;
1515

1616
public CodePushDialog(ReactApplicationContext reactContext, Activity mainActivity) {
1717
super(reactContext);

android/app/src/main/java/com/microsoft/codepush/react/CodePushInstallMode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public enum CodePushInstallMode {
66
ON_NEXT_RESUME(2);
77

88
private final int value;
9-
private CodePushInstallMode(int value) {
9+
CodePushInstallMode(int value) {
1010
this.value = value;
1111
}
1212
public int getValue() {

android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.microsoft.codepush.react;
22

3-
import android.content.Context;
4-
53
import com.facebook.react.bridge.ReadableMap;
64
import com.facebook.react.bridge.WritableMap;
75
import com.facebook.react.bridge.WritableNativeMap;
@@ -21,39 +19,39 @@
2119

2220
public class CodePushPackage {
2321

24-
public final String CODE_PUSH_FOLDER_PREFIX = "CodePush";
25-
public final String CURRENT_PACKAGE_KEY = "currentPackage";
26-
public final String DIFF_MANIFEST_FILE_NAME = "hotcodepush.json";
27-
public final int DOWNLOAD_BUFFER_SIZE = 1024 * 256;
28-
public final String DOWNLOAD_FILE_NAME = "download.zip";
29-
public final String DOWNLOAD_URL_KEY = "downloadUrl";
30-
public final String PACKAGE_FILE_NAME = "app.json";
31-
public final String PACKAGE_HASH_KEY = "packageHash";
32-
public final String PREVIOUS_PACKAGE_KEY = "previousPackage";
33-
public final String RELATIVE_BUNDLE_PATH_KEY = "bundlePath";
34-
public final String STATUS_FILE = "codepush.json";
35-
public final String UNZIPPED_FOLDER_NAME = "unzipped";
36-
public final String UPDATE_BUNDLE_FILE_NAME = "app.jsbundle";
22+
private final String CODE_PUSH_FOLDER_PREFIX = "CodePush";
23+
private final String CURRENT_PACKAGE_KEY = "currentPackage";
24+
private final String DIFF_MANIFEST_FILE_NAME = "hotcodepush.json";
25+
private final int DOWNLOAD_BUFFER_SIZE = 1024 * 256;
26+
private final String DOWNLOAD_FILE_NAME = "download.zip";
27+
private final String DOWNLOAD_URL_KEY = "downloadUrl";
28+
private final String PACKAGE_FILE_NAME = "app.json";
29+
private final String PACKAGE_HASH_KEY = "packageHash";
30+
private final String PREVIOUS_PACKAGE_KEY = "previousPackage";
31+
private final String RELATIVE_BUNDLE_PATH_KEY = "bundlePath";
32+
private final String STATUS_FILE = "codepush.json";
33+
private final String UNZIPPED_FOLDER_NAME = "unzipped";
34+
private final String UPDATE_BUNDLE_FILE_NAME = "app.jsbundle";
3735

3836
private String documentsDirectory;
3937

4038
public CodePushPackage(String documentsDirectory) {
4139
this.documentsDirectory = documentsDirectory;
4240
}
4341

44-
public String getDownloadFilePath() {
42+
private String getDownloadFilePath() {
4543
return CodePushUtils.appendPathComponent(getCodePushPath(), DOWNLOAD_FILE_NAME);
4644
}
4745

48-
public String getUnzippedFolderPath() {
46+
private String getUnzippedFolderPath() {
4947
return CodePushUtils.appendPathComponent(getCodePushPath(), UNZIPPED_FOLDER_NAME);
5048
}
5149

52-
public String getDocumentsDirectory() {
50+
private String getDocumentsDirectory() {
5351
return documentsDirectory;
5452
}
5553

56-
public String getCodePushPath() {
54+
private String getCodePushPath() {
5755
String codePushPath = CodePushUtils.appendPathComponent(getDocumentsDirectory(), CODE_PUSH_FOLDER_PREFIX);
5856
if (CodePush.isUsingTestConfiguration()) {
5957
codePushPath = CodePushUtils.appendPathComponent(codePushPath, "TestPackages");
@@ -62,7 +60,7 @@ public String getCodePushPath() {
6260
return codePushPath;
6361
}
6462

65-
public String getStatusFilePath() {
63+
private String getStatusFilePath() {
6664
return CodePushUtils.appendPathComponent(getCodePushPath(), STATUS_FILE);
6765
}
6866

@@ -151,7 +149,7 @@ public WritableMap getPackage(String packageHash) {
151149
}
152150
}
153151

154-
public void downloadPackage(Context applicationContext, ReadableMap updatePackage, String expectedBundleFileName,
152+
public void downloadPackage(ReadableMap updatePackage, String expectedBundleFileName,
155153
DownloadProgressCallback progressCallback) throws IOException {
156154
String newUpdateHash = CodePushUtils.tryGetString(updatePackage, PACKAGE_HASH_KEY);
157155
String newUpdateFolderPath = getPackageFolderPath(newUpdateHash);
@@ -163,7 +161,6 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
163161
}
164162

165163
String downloadUrlString = CodePushUtils.tryGetString(updatePackage, DOWNLOAD_URL_KEY);
166-
URL downloadUrl = null;
167164
HttpURLConnection connection = null;
168165
BufferedInputStream bin = null;
169166
FileOutputStream fos = null;
@@ -173,7 +170,7 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
173170

174171
// Download the file while checking if it is a zip and notifying client of progress.
175172
try {
176-
downloadUrl = new URL(downloadUrlString);
173+
URL downloadUrl = new URL(downloadUrlString);
177174
connection = (HttpURLConnection) (downloadUrl.openConnection());
178175

179176
long totalBytes = connection.getContentLength();
@@ -206,7 +203,10 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
206203
progressCallback.call(new DownloadProgress(totalBytes, receivedBytes));
207204
}
208205

209-
assert totalBytes == receivedBytes;
206+
if (totalBytes != receivedBytes) {
207+
throw new CodePushUnknownException("Received " + receivedBytes + " bytes, expected " + totalBytes);
208+
}
209+
210210
isZip = ByteBuffer.wrap(header).getInt() == 0x504b0304;
211211
} catch (MalformedURLException e) {
212212
throw new CodePushMalformedDataException(downloadUrlString, e);
@@ -266,7 +266,7 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
266266
" in update package.", e);
267267
}
268268

269-
updatePackage = CodePushUtils.convertJsonObjectToWriteable(updatePackageJSON);
269+
updatePackage = CodePushUtils.convertJsonObjectToWritable(updatePackageJSON);
270270
}
271271
} else {
272272
// File is a jsbundle, move it to a folder with the packageHash as its name
@@ -277,7 +277,7 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
277277
CodePushUtils.writeReadableMapToFile(updatePackage, newUpdateMetadataPath);
278278
}
279279

280-
public void installPackage(ReadableMap updatePackage, boolean removePendingUpdate) throws IOException {
280+
public void installPackage(ReadableMap updatePackage, boolean removePendingUpdate) {
281281
String packageHash = CodePushUtils.tryGetString(updatePackage, PACKAGE_HASH_KEY);
282282
WritableMap info = getCurrentPackageInfo();
283283
if (removePendingUpdate) {

android/app/src/main/java/com/microsoft/codepush/react/CodePushUnknownException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.microsoft.codepush.react;
22

3-
public class CodePushUnknownException extends RuntimeException {
3+
class CodePushUnknownException extends RuntimeException {
44

55
public CodePushUnknownException(String message, Throwable cause) {
66
super(message, cause);

android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static String getHashForBinaryContents(Activity mainActivity, boolean isD
115115
}
116116

117117
public static void verifyHashForDiffUpdate(String folderPath, String expectedHash) {
118-
ArrayList<String> updateContentsManifest = new ArrayList<String>();
118+
ArrayList<String> updateContentsManifest = new ArrayList<>();
119119
addContentsOfFolderToManifest(folderPath, "", updateContentsManifest);
120120
Collections.sort(updateContentsManifest);
121121
JSONArray updateContentsJSONArray = new JSONArray();

android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,22 @@
1515
import org.json.JSONException;
1616
import org.json.JSONObject;
1717

18-
import java.io.BufferedInputStream;
1918
import java.io.BufferedReader;
2019
import java.io.File;
21-
import java.io.FileInputStream;
22-
import java.io.FileOutputStream;
2320
import java.io.IOException;
2421
import java.io.InputStream;
2522
import java.io.InputStreamReader;
26-
import java.io.PrintWriter;
2723
import java.util.Iterator;
28-
import java.util.zip.ZipEntry;
29-
import java.util.zip.ZipInputStream;
3024

3125
public class CodePushUtils {
3226

33-
public static final String CODE_PUSH_TAG = "CodePush";
3427
public static final String REACT_NATIVE_LOG_TAG = "ReactNative";
3528

3629
public static String appendPathComponent(String basePath, String appendPathComponent) {
3730
return new File(basePath, appendPathComponent).getAbsolutePath();
3831
}
3932

40-
public static WritableArray convertJsonArrayToWriteable(JSONArray jsonArr) {
33+
public static WritableArray convertJsonArrayToWritable(JSONArray jsonArr) {
4134
WritableArray arr = Arguments.createArray();
4235
for (int i=0; i<jsonArr.length(); i++) {
4336
Object obj = null;
@@ -49,9 +42,9 @@ public static WritableArray convertJsonArrayToWriteable(JSONArray jsonArr) {
4942
}
5043

5144
if (obj instanceof JSONObject)
52-
arr.pushMap(convertJsonObjectToWriteable((JSONObject) obj));
45+
arr.pushMap(convertJsonObjectToWritable((JSONObject) obj));
5346
else if (obj instanceof JSONArray)
54-
arr.pushArray(convertJsonArrayToWriteable((JSONArray) obj));
47+
arr.pushArray(convertJsonArrayToWritable((JSONArray) obj));
5548
else if (obj instanceof String)
5649
arr.pushString((String) obj);
5750
else if (obj instanceof Double)
@@ -69,7 +62,7 @@ else if (obj == null)
6962
return arr;
7063
}
7164

72-
public static WritableMap convertJsonObjectToWriteable(JSONObject jsonObj) {
65+
public static WritableMap convertJsonObjectToWritable(JSONObject jsonObj) {
7366
WritableMap map = Arguments.createMap();
7467
Iterator<String> it = jsonObj.keys();
7568
while(it.hasNext()){
@@ -83,9 +76,9 @@ public static WritableMap convertJsonObjectToWriteable(JSONObject jsonObj) {
8376
}
8477

8578
if (obj instanceof JSONObject)
86-
map.putMap(key, convertJsonObjectToWriteable((JSONObject) obj));
79+
map.putMap(key, convertJsonObjectToWritable((JSONObject) obj));
8780
else if (obj instanceof JSONArray)
88-
map.putArray(key, convertJsonArrayToWriteable((JSONArray) obj));
81+
map.putArray(key, convertJsonArrayToWritable((JSONArray) obj));
8982
else if (obj instanceof String)
9083
map.putString(key, (String) obj);
9184
else if (obj instanceof Double)
@@ -105,7 +98,7 @@ else if (obj == null)
10598

10699
public static WritableMap convertReadableMapToWritableMap(ReadableMap map) {
107100
JSONObject mapJSON = convertReadableToJsonObject(map);
108-
return convertJsonObjectToWriteable(mapJSON);
101+
return convertJsonObjectToWritable(mapJSON);
109102
}
110103

111104
public static JSONArray convertReadableToJsonArray(ReadableArray arr) {
@@ -204,16 +197,13 @@ public static String getStringFromInputStream(InputStream inputStream) throws IO
204197
}
205198

206199
public static WritableMap getWritableMapFromFile(String filePath) throws IOException {
207-
208200
String content = FileUtils.readFileToString(filePath);
209-
JSONObject json = null;
210201
try {
211-
json = new JSONObject(content);
212-
return convertJsonObjectToWriteable(json);
202+
JSONObject json = new JSONObject(content);
203+
return convertJsonObjectToWritable(json);
213204
} catch (JSONException jsonException) {
214205
throw new CodePushMalformedDataException(filePath, jsonException);
215206
}
216-
217207
}
218208

219209
public static void log(String message) {

android/app/src/main/java/com/microsoft/codepush/react/DownloadProgress.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import com.facebook.react.bridge.WritableMap;
44
import com.facebook.react.bridge.WritableNativeMap;
55

6-
public class DownloadProgress {
7-
public long totalBytes;
8-
public long receivedBytes;
6+
class DownloadProgress {
7+
private long totalBytes;
8+
private long receivedBytes;
99

1010
public DownloadProgress (long totalBytes, long receivedBytes){
1111
this.totalBytes = totalBytes;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.microsoft.codepush.react;
22

3-
public interface DownloadProgressCallback {
3+
interface DownloadProgressCallback {
44
void call(DownloadProgress downloadProgress);
55
}

android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
import java.io.InputStreamReader;
1010
import java.io.PrintWriter;
1111
import java.util.zip.ZipEntry;
12-
import java.util.zip.ZipFile;
1312
import java.util.zip.ZipInputStream;
1413

1514
public class FileUtils {
1615

17-
public static final int WRITE_BUFFER_SIZE = 1024 * 8;
16+
private static final int WRITE_BUFFER_SIZE = 1024 * 8;
1817

1918
public static void copyDirectoryContents(String sourceDirectoryPath, String destinationDirectoryPath) throws IOException {
2019
File sourceDir = new File(sourceDirectoryPath);
@@ -55,21 +54,16 @@ public static void copyDirectoryContents(String sourceDirectoryPath, String dest
5554
}
5655
}
5756

58-
public static boolean createFolderAtPath(String filePath) {
59-
File file = new File(filePath);
60-
return file.mkdir();
61-
}
62-
6357
public static void deleteDirectory(File directory) {
6458
if (directory.exists()) {
6559
File[] files = directory.listFiles();
6660
if (files != null) {
67-
for (int i=0; i<files.length; i++) {
68-
if(files[i].isDirectory()) {
69-
deleteDirectory(files[i]);
61+
for (File file : files) {
62+
if(file.isDirectory()) {
63+
deleteDirectory(file);
7064
}
7165
else {
72-
files[i].delete();
66+
file.delete();
7367
}
7468
}
7569
}
@@ -88,12 +82,12 @@ public static void deleteFileAtPathSilently(String path) {
8882
public static void deleteFileOrFolderSilently(File file) {
8983
if (file.isDirectory()) {
9084
File[] files = file.listFiles();
91-
for (int i = 0; i < files.length; i++) {
92-
if (files[i].isDirectory()) {
93-
deleteFileOrFolderSilently(files[i]);
85+
for (File fileEntry : files) {
86+
if (fileEntry.isDirectory()) {
87+
deleteFileOrFolderSilently(fileEntry);
9488
} else {
9589
if (!file.delete()) {
96-
files[i].delete();
90+
fileEntry.delete();
9791
}
9892
}
9993
}

0 commit comments

Comments
 (0)