Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
v0.0.1-beta; add checkUpdate support
Browse files Browse the repository at this point in the history
Signed-off-by: Jerry Chen <jerryc05@qq.com>
  • Loading branch information
Jerry Chen committed Jul 9, 2019
1 parent 7642ed4 commit b10fe6a
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "0.0.1-alpha"
versionName "0.0.1-beta"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
resConfigs "xxhdpi"
}
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 @@ -5,6 +5,7 @@
tools:ignore="GoogleAppIndexingWarning">

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature
android:name="android.hardware.camera.front"
android:required="true" />
Expand Down
71 changes: 60 additions & 11 deletions app/src/main/java/jerryc05/unlockme/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraDevice.StateCallback;
import android.hardware.camera2.CameraManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
Expand All @@ -24,6 +25,7 @@
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;

import jerryc05.unlockme.helpers.URLConnectionBuilder;
import jerryc05.unlockme.receivers.MyDAReceiver;

@SuppressWarnings("NullableProblems")
Expand All @@ -50,18 +52,24 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.activity_main_button_takePhoto)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
new Thread(new Runnable() {
@Override
public void run() {
checkUpdate();
findViewById(R.id.activity_main_button_takePhoto)
.setOnClickListener(new View.OnClickListener() {
@Override
public void run() {
takePhoto();
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
takePhoto();
}
}).start();
}
}).start();
}
});
});
}
}).start();
}

@Override
Expand Down Expand Up @@ -122,6 +130,47 @@ public void onRequestPermissionsResult(int requestCode,
}
}

protected void checkUpdate() {
final String
keyword = "/jerryc05/UnlockMe/tree",
URL = "https://www.github.com/jerryc05/UnlockMe/releases";
try (URLConnectionBuilder connectionBuilder = URLConnectionBuilder
.get(URL)
.setConnectTimeout(1000)
.setReadTimeout(1000)
.connect()) {
String result = connectionBuilder.getResult();
result = result.substring(result.indexOf(keyword) +
keyword.length() + 2);
final String latest = result.substring(0, result.indexOf('"'));

if (!latest.equals(BuildConfig.VERSION_NAME))
runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(MainActivity.this)
.setTitle("New Version Available")
.setMessage("Do you want to upgrade from\n" +
BuildConfig.VERSION_NAME + " to " + latest + '?')
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,
int i) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(URL + "/tag/v" + latest)));
}
})
.setNegativeButton("NO", null)
.show();
}
});

} catch (Exception e) {
AlertExceptionToUI(e);
}
}

void requestDeviceAdmin() {
if (requestDeviceAdminLock != null) {
requestDeviceAdminLock.unlock();
Expand Down Expand Up @@ -304,7 +353,7 @@ private void AlertExceptionToUI(Exception e,
@Override
public void run() {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Fatal Error")
.setTitle("Crash Report")
.setMessage(e.toString())
.setCancelable(false)
.setPositiveButton("OK", onClickListener)
Expand Down
174 changes: 174 additions & 0 deletions app/src/main/java/jerryc05/unlockme/helpers/URLConnectionBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package jerryc05.unlockme.helpers;

import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

import javax.net.ssl.HttpsURLConnection;

import jerryc05.unlockme.BuildConfig;

/**
* A builder for URLConnection class.
*
* @author \
* \ d88b d88888b d8888b. d8888b. db db .o88b. .d88b. ooooo
* \ `8P' 88' 88 `8D 88 `8D `8b d8' d8P Y8 .8P 88. 8P~~~~
* \ 88 88ooooo 88oobY' 88oobY' `8bd8' 8P 88 d'88 dP
* \ 88 88~~~~~ 88`8b 88`8b 88 8b 88 d' 88 V8888b.
* \ db. 88 88. 88 `88. 88 `88. 88 Y8b d8 `88 d8' `8D
* \ Y8888P Y88888P 88 YD 88 YD YP `Y88P' `Y88P' 88oobY'
* @see java.net.URLConnection
* @see java.net.HttpURLConnection
* @see javax.net.ssl.HttpsURLConnection
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public class URLConnectionBuilder implements AutoCloseable {

private final static String
TAG = URLConnectionBuilder.class.getName();

public final static String
METHOD_GET = "GET",
METHOD_POST = "POST",
METHOD_HEAD = "HEAD",
METHOD_OPTIONS = "OPTIONS",
METHOD_PUT = "PUT",
METHOD_DELETE = "DELETE",
METHOD_TRACE = "TRACE";

@Override
public void close() {
disconnect();
}

private boolean isHTTP;
private URLConnection urlConnection;

private URLConnectionBuilder(String _baseURL) throws IOException {
_baseURL = _baseURL.trim();
if (!_baseURL.startsWith("http://") && !_baseURL.startsWith("https://"))
throw new UnsupportedOperationException(
"${baseURL} prefix not recognized: " + _baseURL);

urlConnection = new URL(_baseURL).openConnection();
urlConnection.setConnectTimeout(5 * 1000);
urlConnection.setReadTimeout(5 * 1000);
isHTTP = _baseURL.charAt(4) == ':';
}

public static URLConnectionBuilder get(String _baseURL) throws IOException {
return new URLConnectionBuilder(_baseURL);
}

public static URLConnectionBuilder post(String _baseURL) throws IOException {
return new URLConnectionBuilder(_baseURL).setRequestMethod(METHOD_POST);
}

public URLConnectionBuilder connect() throws IOException {
checkNullUrlConnection("run");
(isHTTP ? urlConnection
: (HttpsURLConnection) urlConnection).connect();
return this;
}

public String getResult(String charset) throws IOException {
try {
String result;
{
InputStream inputStream = (isHTTP
? urlConnection
: (HttpsURLConnection) urlConnection).getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;

while ((length = inputStream.read(buffer)) != -1)
outputStream.write(buffer, 0, length);

result = outputStream.toString(charset);
inputStream.close();
}

if (BuildConfig.DEBUG)
Log.d(TAG, "connect: Response code = " + (isHTTP
? (HttpURLConnection) urlConnection
: (HttpsURLConnection) urlConnection)
.getResponseCode()
+ "\n================ Respond content ================\n"
+ result);

return result;

} catch (Exception e) {
Log.e(TAG, "connect: ", e);
throw e;
}
}

public String getResult() throws IOException {
return getResult(StandardCharsets.UTF_8.name());
}

public void disconnect() {
checkNullUrlConnection("disconnect");
(isHTTP ? (HttpURLConnection) urlConnection
: (HttpsURLConnection) urlConnection)
.disconnect();

if (BuildConfig.DEBUG)
Log.d(TAG, "disconnect: " + urlConnection.getURL());
}

public URLConnectionBuilder setRequestMethod(String _requestMethod) {
try {
(isHTTP ? (HttpURLConnection) urlConnection
: (HttpsURLConnection) urlConnection)
.setRequestMethod(_requestMethod);
} catch (java.net.ProtocolException e) {
Log.e(TAG, "setRequestMethod: ", e);
}
return this;
}

public URLConnectionBuilder setConnectTimeout(int _connectTimeout) {
urlConnection.setConnectTimeout(_connectTimeout);
return this;
}

public URLConnectionBuilder setReadTimeout(int _readTimeout) {
urlConnection.setReadTimeout(_readTimeout);
return this;
}

public URLConnectionBuilder setUseCache(boolean _useCache) {
urlConnection.setUseCaches(_useCache);
return this;
}

public URLConnectionBuilder setRequestProperty(String key, String value) {
urlConnection.setRequestProperty(key, value);
return this;
}

public URLConnection getUrlConnection() {
checkNullUrlConnection("get");
return urlConnection;
}

public URLConnection _getUrlConnection() {
return urlConnection;
}

private void checkNullUrlConnection(String action) {
if (urlConnection == null)
throw new UnsupportedOperationException(
"Cannot " + action + " null instance decodeURL ${urlConnection}!");
}
}

0 comments on commit b10fe6a

Please sign in to comment.