Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
XuXiangJun committed Jul 1, 2019
1 parent 9587ed6 commit ecd0538
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.espressif.espblufi"
minSdkVersion 18
targetSdkVersion 28
versionCode 18
versionName "1.4.1"
versionCode 19
versionName "1.4.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".app.BlufiApp"
Expand Down
136 changes: 136 additions & 0 deletions app/src/main/java/com/espressif/espblufi/task/BlufiAppReleaseTask.java
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);
}
}
}
78 changes: 78 additions & 0 deletions app/src/main/java/com/espressif/espblufi/ui/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package com.espressif.espblufi.ui;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;

import android.preference.PreferenceScreen;
import android.text.TextUtils;

import com.espressif.espblufi.R;
import com.espressif.espblufi.app.BaseActivity;
import com.espressif.espblufi.app.BlufiApp;
import com.espressif.espblufi.constants.BlufiConstants;
import com.espressif.espblufi.constants.SettingsConstants;
import com.espressif.espblufi.task.BlufiAppReleaseTask;

import java.util.concurrent.atomic.AtomicReference;

import blufi.espressif.BlufiClient;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import libs.espressif.app.AppUtil;

public class SettingsActivity extends BaseActivity {

Expand All @@ -42,6 +54,9 @@ public static class SettingsFragment extends PreferenceFragment implements Prefe
private EditTextPreference mMtuPref;
private EditTextPreference mBlePrefixPref;

private Preference mVersionCheckPref;
private volatile BlufiAppReleaseTask.ReleaseInfo mAppLatestRelease;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -70,6 +85,8 @@ public void onCreate(Bundle savedInstanceState) {
mBlePrefixPref.setOnPreferenceChangeListener(this);
String blePrefix = (String) mApp.settingsGet(KEY_BLE_PREFIX, BlufiConstants.BLUFI_PREFIX);
mBlePrefixPref.setSummary(blePrefix);

mVersionCheckPref = findPreference(getString(R.string.settings_upgrade_check_key));
}

public String getVersionName() {
Expand All @@ -84,6 +101,21 @@ public String getVersionName() {
return version;
}

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference == mVersionCheckPref) {
if (mAppLatestRelease == null) {
mVersionCheckPref.setEnabled(false);
checkAppLatestRelease();
} else {
downloadLatestRelease();
}
return true;
}

return super.onPreferenceTreeClick(preferenceScreen, preference);
}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference == mMtuPref) {
Expand All @@ -107,5 +139,51 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {

return false;
}

private void checkAppLatestRelease() {
Observable.just(new BlufiAppReleaseTask())
.subscribeOn(Schedulers.io())
.map(task -> new AtomicReference<>(task.requestLatestRelease()))
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(reference -> {
mVersionCheckPref.setEnabled(true);

mAppLatestRelease = null;
BlufiAppReleaseTask.ReleaseInfo latestRelease = reference.get();
if (latestRelease == null) {
mVersionCheckPref.setSummary(R.string.settings_upgrade_check_failed);
return;
} else if (latestRelease.getVersionCode() < 0) {
mVersionCheckPref.setSummary(R.string.settings_upgrade_check_not_found);
return;
}

int currentVersion = AppUtil.getVersionCode(getActivity());
int latestVersion = latestRelease.getVersionCode();
if (latestVersion > currentVersion) {
mVersionCheckPref.setSummary(R.string.settings_upgrade_check_disciver_new);
mAppLatestRelease = latestRelease;

new AlertDialog.Builder(getActivity())
.setTitle(R.string.settings_upgrade_dialog_title)
.setMessage(R.string.settings_upgrade_dialog_message)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(R.string.settings_upgrade_dialog_upgrade,
(dialog, which) -> downloadLatestRelease())
.show();
} else {
mVersionCheckPref.setSummary(R.string.settings_upgrade_check_current_latest);
}
})
.subscribe();

}

private void downloadLatestRelease() {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(mAppLatestRelease.getDownloadUrl());
intent.setData(uri);
startActivity(intent);
}
} // Fragment end
} // Activity end
19 changes: 14 additions & 5 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">EspBlufi</string>
<string name="app_name">EspBluFi</string>

<string name="string_unknown">未知</string>

<string name="main_title">EspBlufi</string>
<string name="main_title">EspBluFi</string>
<string name="main_bt_disable_msg">蓝牙不可用</string>
<string name="main_location_disable_msg">位置信息(GPS)不可用</string>
<string name="main_menu_settings">设置</string>
Expand Down Expand Up @@ -50,7 +50,7 @@

<string name="settings_title">设置</string>
<string name="settings_category_blufi_key">setting_category_blufi</string>
<string name="settings_category_blufi_title">Blufi</string>
<string name="settings_category_blufi_title">BluFi</string>
<string name="settings_mtu_length_key">settings_key_mtu_length</string>
<string name="settings_mtu_length_title">设置 mtu 长度</string>
<string name="settings_mtu_length_hint">设置一个大于 %1$d 的数</string>
Expand All @@ -61,5 +61,14 @@
<string name="settings_version_key">settings_key_version</string>
<string name="settings_version_title">APP 版本</string>
<string name="settings_blufi_version_key">settings_key_blufi_version</string>
<string name="settings_blufi_version_title">Blufi 库版本</string>
</resources>
<string name="settings_blufi_version_title">BluFi 库版本</string>
<string name="settings_upgrade_check_key">settings_upgrade_check</string>
<string name="settings_upgrade_check_title">检查更新</string>
<string name="settings_upgrade_check_failed">获取最新版本信息失败</string>
<string name="settings_upgrade_check_not_found">没有找到更新版本</string>
<string name="settings_upgrade_check_current_latest">当前APP已经是最新版本</string>
<string name="settings_upgrade_check_disciver_new">发现新版本APP, 点击开始下载</string>
<string name="settings_upgrade_dialog_title">发现新版本</string>
<string name="settings_upgrade_dialog_message">点击[升级]下载APK</string>
<string name="settings_upgrade_dialog_upgrade">升级</string>
</resources>
19 changes: 14 additions & 5 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<resources>
<string name="app_name">EspBlufi</string>
<string name="app_name">EspBluFi</string>

<string name="string_unknown">Unknown</string>

<string name="main_title">EspBlufi</string>
<string name="main_title">EspBluFi</string>
<string name="main_bt_disable_msg">Bluetooth is disable</string>
<string name="main_location_disable_msg">Location is disable</string>
<string name="main_menu_settings">Settings</string>
Expand All @@ -15,7 +15,7 @@
<string name="blufi_function_security">Security</string>
<string name="blufi_function_security_msg">Negotiate security with device</string>
<string name="blufi_function_version">Version</string>
<string name="blufi_function_version_msg">Get the device blufi version</string>
<string name="blufi_function_version_msg">Get the device BluFi version</string>
<string name="blufi_function_device_status">Status</string>
<string name="blufi_function_device_status_msg">Get the device status</string>
<string name="blufi_function_configure">Configure</string>
Expand Down Expand Up @@ -49,7 +49,7 @@

<string name="settings_title">Settings</string>
<string name="settings_category_blufi_key">setting_category_blufi</string>
<string name="settings_category_blufi_title">Blufi</string>
<string name="settings_category_blufi_title">BluFi</string>
<string name="settings_mtu_length_key">settings_key_mtu_length</string>
<string name="settings_mtu_length_title">Set mtu length</string>
<string name="settings_mtu_length_hint">Enter a number great than %1$d</string>
Expand All @@ -60,5 +60,14 @@
<string name="settings_version_key">settings_key_version</string>
<string name="settings_version_title">APP Version</string>
<string name="settings_blufi_version_key">settings_key_blufi_version</string>
<string name="settings_blufi_version_title">Blufi lib version</string>
<string name="settings_blufi_version_title">BluFi lib version</string>
<string name="settings_upgrade_check_key">settings_upgrade_check</string>
<string name="settings_upgrade_check_title">Check APP latest version</string>
<string name="settings_upgrade_check_failed">Get latest release information failed</string>
<string name="settings_upgrade_check_not_found">Not found new release</string>
<string name="settings_upgrade_check_current_latest">Current APP is latest</string>
<string name="settings_upgrade_check_disciver_new">Discover new version APP, Click to download</string>
<string name="settings_upgrade_dialog_title">Discover new version</string>
<string name="settings_upgrade_dialog_message">Click [Upgrade] to download APK</string>
<string name="settings_upgrade_dialog_upgrade">Upgrade</string>
</resources>
5 changes: 4 additions & 1 deletion app/src/main/res/xml/blufi_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
<Preference
android:key="@string/settings_blufi_version_key"
android:title="@string/settings_blufi_version_title"/>
<Preference
android:key="@string/settings_upgrade_check_key"
android:title="@string/settings_upgrade_check_title"/>
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>
5 changes: 4 additions & 1 deletion log/updatelog-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

# Update Log

# v1.4.1
## v1.4.2
- Add app upgrade option in Settings

## v1.4.1
- Add error code in BluFi callback function,see class BlufiCallback

## v1.4.0
Expand Down
Loading

0 comments on commit ecd0538

Please sign in to comment.