-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9587ed6
commit ecd0538
Showing
9 changed files
with
257 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
app/src/main/java/com/espressif/espblufi/task/BlufiAppReleaseTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package com.espressif.espblufi.task; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Locale; | ||
|
||
/** | ||
* Released APKs' name format: EspBluFi-{version name}-{version code}.apk | ||
*/ | ||
public class BlufiAppReleaseTask { | ||
private static final String LATEST_RELEASE_URL = "https://api.github.com/repos/EspressifApp/EspBlufiForAndroid/releases/latest"; | ||
|
||
private static final String APK_SUFFIX = ".apk"; | ||
|
||
private static final String KEY_ASSETS = "assets"; | ||
private static final String KEY_ASSET_NAME = "name"; | ||
private static final String KEY_SIZE = "size"; | ||
private static final String KEY_DOWNLOAD_URL = "browser_download_url"; | ||
private static final String KEY_BODY = "body"; | ||
|
||
public ReleaseInfo requestLatestRelease() { | ||
HttpURLConnection connection = null; | ||
try { | ||
URL url = new URL(LATEST_RELEASE_URL); | ||
connection = (HttpURLConnection) url.openConnection(); | ||
int code = connection.getResponseCode(); | ||
String message = connection.getResponseMessage(); | ||
System.out.println("Code = " + code); | ||
if (code != HttpURLConnection.HTTP_OK) { | ||
ReleaseInfo info = new ReleaseInfo(); | ||
info.versionCode = -1; | ||
info.versionName = message; | ||
return info; | ||
} | ||
|
||
InputStream is = connection.getInputStream(); | ||
ByteArrayOutputStream contentArray = new ByteArrayOutputStream(); | ||
for (int read = is.read(); read != -1; read = is.read()) { | ||
contentArray.write(read); | ||
} | ||
|
||
JSONObject releaseJSON = new JSONObject(new String(contentArray.toByteArray())); | ||
return parseRelease(releaseJSON); | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (connection != null) { | ||
connection.disconnect(); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private ReleaseInfo parseRelease(JSONObject releaseJSON) { | ||
try { | ||
String notes = releaseJSON.getString(KEY_BODY); | ||
JSONArray assetArray = releaseJSON.getJSONArray(KEY_ASSETS); | ||
for (int i = 0; i < assetArray.length(); i++) { | ||
JSONObject assetJSON = assetArray.getJSONObject(i); | ||
String assetName = assetJSON.getString(KEY_ASSET_NAME); | ||
if (assetName.endsWith(APK_SUFFIX)) { | ||
String apkName = assetName.substring(0, assetName.length() - APK_SUFFIX.length()); | ||
String[] apkNameSplits = apkName.split("-"); | ||
String versionName = apkNameSplits[1]; | ||
int versionCode = Integer.parseInt(apkNameSplits[2]); | ||
|
||
long apkSize = assetJSON.getLong(KEY_SIZE); | ||
String downloadUrl = assetJSON.getString(KEY_DOWNLOAD_URL); | ||
|
||
ReleaseInfo result = new ReleaseInfo(); | ||
result.versionCode = versionCode; | ||
result.versionName = versionName; | ||
result.apkSize = apkSize; | ||
result.downloadUrl = downloadUrl; | ||
result.notes = notes; | ||
return result; | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static class ReleaseInfo { | ||
String versionName; | ||
int versionCode; | ||
String downloadUrl; | ||
long apkSize; | ||
String notes; | ||
|
||
public int getVersionCode() { | ||
return versionCode; | ||
} | ||
|
||
public String getVersionName() { | ||
return versionName; | ||
} | ||
|
||
public String getDownloadUrl() { | ||
return downloadUrl; | ||
} | ||
|
||
public long getApkSize() { | ||
return apkSize; | ||
} | ||
|
||
public String getNotes() { | ||
return notes; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public String toString() { | ||
return String.format(Locale.ENGLISH, | ||
"VersionName=%s, VersionCode=%d, DownloadUrl=%s, APKSize=%d, notes=%s", | ||
versionName, versionCode, downloadUrl, apkSize, notes); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.