Skip to content

Commit

Permalink
Refactored code - enhancement #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed Mar 6, 2018
1 parent 91e8cee commit 501c5f2
Show file tree
Hide file tree
Showing 18 changed files with 488 additions and 762 deletions.
@@ -1,16 +1,10 @@
package javinator9889.bitcoinpools.AppUpdaterManager;

import android.app.DownloadManager;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.StrictMode;
import android.support.annotation.NonNull;
import android.util.Log;

Expand All @@ -21,8 +15,6 @@
import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.lang.reflect.Method;
import java.util.concurrent.ExecutionException;

import javinator9889.bitcoinpools.BitCoinApp;
Expand All @@ -37,59 +29,54 @@ public class CheckUpdates {
private static final String GITHUB_API_URL = "https://api.github.com/repos/";
private static String CONNECTION_URL;
private static String APP_VERSION;
private static JSONArray RETRIEVED_DATA;
private static String LATEST_VERSION;
private static String DOWNLOAD_URL = null;
private static String MORE_INFO = Constants.GOOGLE_PLAY_URL;
private static String APK_NAME;
private static boolean HTML_PAGE = false;

public CheckUpdates(@NonNull String GitHub_User, @NonNull String GitHub_Repo) {
CONNECTION_URL = GITHUB_API_URL + GitHub_User + "/" + GitHub_Repo + "/releases";
try {
APP_VERSION = BitCoinApp.getAppContext().getPackageManager().getPackageInfo(BitCoinApp.getAppContext().getPackageName(), 0).versionName;
APP_VERSION = BitCoinApp.getAppContext().getPackageManager()
.getPackageInfo(BitCoinApp.getAppContext().getPackageName(), 0)
.versionName;
} catch (PackageManager.NameNotFoundException e) {
Log.e(Constants.LOG.CTAG, Constants.LOG.NO_INFO + e.getMessage(), new PackageManager.NameNotFoundException());
Log.e(Constants.LOG.CTAG, Constants.LOG.NO_INFO + e.getMessage(),
new PackageManager.NameNotFoundException());
}
getData();
}

public void checkForUpdates(final Context dialogContext, String title, String description, String positiveText, String negativeText, String neutralText) {
if (!APP_VERSION.equals(LATEST_VERSION)) {// && (Float.parseFloat(APP_VERSION) < Float.parseFloat(LATEST_VERSION))) {
Log.d(Constants.LOG.CTAG, Constants.LOG.NEW_VERSION + APP_VERSION + " | " + LATEST_VERSION);
public void checkForUpdates(final Context dialogContext, String title, String description,
String positiveText, String negativeText, String neutralText) {
if (!APP_VERSION.equals(LATEST_VERSION)) {
Log.d(Constants.LOG.CTAG, Constants.LOG.NEW_VERSION + APP_VERSION
+ " | " + LATEST_VERSION);
MaterialDialog materialDialog;
if (!HTML_PAGE) {
Log.d(Constants.LOG.CTAG, Constants.LOG.DOW_NOTIFICATION);
materialDialog = new MaterialDialog.Builder(dialogContext)
.title(title)
.content(description)
//.positiveText(positiveText)
.negativeText(negativeText)
.neutralText(positiveText)
.cancelable(false)
.onAny(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
public void onClick(@NonNull MaterialDialog dialog,
@NonNull DialogAction which) {
switch (which) {
case POSITIVE:
DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(DOWNLOAD_URL));
downloadRequest.setTitle(APK_NAME);
downloadRequest.allowScanningByMediaScanner();
downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadRequest.setVisibleInDownloadsUi(true);
downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, APK_NAME);
final DownloadManager manager = (DownloadManager) dialogContext.getSystemService(Context.DOWNLOAD_SERVICE);
assert manager != null;
manager.enqueue(downloadRequest);
break;
case NEUTRAL:
try {
Uri webUri = Uri.parse("market://details?id=com.fastaccess.github");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, webUri);
Uri webUri = Uri.parse(
"market://details?id=javinator9889.bitcoinpools");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW,
webUri);
dialogContext.startActivity(launchBrowser);
} catch (ActivityNotFoundException e) {
Uri webUri = Uri.parse(MORE_INFO);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, webUri);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW,
webUri);
dialogContext.startActivity(launchBrowser);
}
break;
Expand Down Expand Up @@ -134,28 +121,34 @@ private void getData() {
NetworkConnection connection = new NetworkConnection();
connection.execute(CONNECTION_URL);
try {
RETRIEVED_DATA = connection.get();
JSONArray RETRIEVED_DATA = connection.get();
int jsonLength = RETRIEVED_DATA.length();
boolean noPreRelease = false;
for (int i = 0; (i < jsonLength) && !noPreRelease; ++i) {
if (!RETRIEVED_DATA.getJSONObject(i).getBoolean("prerelease")) {
noPreRelease = true;
LATEST_VERSION = RETRIEVED_DATA.getJSONObject(i).getString("tag_name");
boolean apkFound = false;
for (int j = 0; (j < RETRIEVED_DATA.getJSONObject(i).getJSONArray("assets").length()) && !apkFound; ++j) {
if (!(RETRIEVED_DATA.getJSONObject(i).getJSONArray("assets").isNull(j) && (RETRIEVED_DATA.getJSONObject(i).getJSONArray("assets").getJSONObject(j).getString("name").contains(".apk")))) {
for (int j = 0; (j < RETRIEVED_DATA.getJSONObject(i).getJSONArray("assets")
.length()) && !apkFound; ++j)
{
if (!(RETRIEVED_DATA.getJSONObject(i).getJSONArray("assets").isNull(j)
&& (RETRIEVED_DATA.getJSONObject(i).getJSONArray("assets")
.getJSONObject(j).getString("name").contains(".apk"))))
{
apkFound = true;
DOWNLOAD_URL = RETRIEVED_DATA.getJSONObject(i).getJSONArray("assets").getJSONObject(0).getString("browser_download_url");
APK_NAME = RETRIEVED_DATA.getJSONObject(i).getJSONArray("assets").getJSONObject(j).getString("name");
DOWNLOAD_URL = RETRIEVED_DATA.getJSONObject(i)
.getJSONArray("assets").getJSONObject(0)
.getString("browser_download_url");
}
}
if (DOWNLOAD_URL == null) {
HTML_PAGE = true;
}
//MORE_INFO = RETRIEVED_DATA.getJSONObject(i).getString("html_url");
}
}
} catch (InterruptedException | ExecutionException | JSONException | NullPointerException e) {
} catch (InterruptedException | ExecutionException
| JSONException | NullPointerException e) {
Crashlytics.logException(e);
Log.e(Constants.LOG.CTAG, Constants.LOG.NO_INFO + e.getMessage());
}
Expand Down
@@ -1,6 +1,7 @@
package javinator9889.bitcoinpools.AppUpdaterManager;

import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.util.Log;

import org.json.JSONArray;
Expand Down Expand Up @@ -34,6 +35,7 @@ protected JSONArray doInBackground(String... url) {
return null;
}

@NonNull
private String readAll(Reader httpsReader) throws IOException {
StringBuilder response = new StringBuilder();
int valueRead;
Expand All @@ -43,9 +45,11 @@ private String readAll(Reader httpsReader) throws IOException {
return response.toString();
}

@NonNull
private JSONArray readJSONFromURL(String url) throws IOException, JSONException {
try (InputStream JSONStream = new URL(url).openStream()) {
BufferedReader br = new BufferedReader(new InputStreamReader(JSONStream, Charset.forName("UTF-8")));
BufferedReader br = new BufferedReader(
new InputStreamReader(JSONStream, Charset.forName("UTF-8")));
String JSONText = readAll(br);
return new JSONArray(JSONText);
}
Expand Down
Expand Up @@ -6,20 +6,12 @@
import android.os.Message;
import android.util.Log;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;

import javax.net.ssl.HttpsURLConnection;

import javinator9889.bitcoinpools.BitCoinApp;
import javinator9889.bitcoinpools.CacheManaging;
import javinator9889.bitcoinpools.Constants;
Expand All @@ -39,12 +31,9 @@ public class CacheJobSchedulerService extends JobService {
private Handler jobHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
System.out.println("Handling message");
if (!jobCancelled) {
try {
System.out.println("Updating cache");
updateCache();
System.out.println("Cache updated");
jobWorking = false;
jobFinished((JobParameters) msg.obj, false);
return false;
Expand Down Expand Up @@ -78,7 +67,8 @@ public boolean onStopJob(JobParameters params) {
}

private void updateCache() {
String date = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.US).format(Calendar.getInstance().getTime());
String date = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.US)
.format(Calendar.getInstance().getTime());
CacheManaging cache = CacheManaging.newInstance(BitCoinApp.getAppContext());
try {
cache.setupFile();
Expand All @@ -89,32 +79,11 @@ private void updateCache() {
newValuesToSave.put("date", date);
for (String key : valuesObtained.keySet()) {
newValuesToSave.put(key, String.valueOf(valuesObtained.get(key)));
System.out.println(key + " | " + valuesObtained.get(key));
}
cache.writeCache(newValuesToSave);
} catch (Exception e) {
e.printStackTrace();
throw new NullPointerException("Impossible to obtain values");
}
}

/*private JSONObject getHTTPSRequest(String requestUrl) throws Exception {
Thread httpsRequestThread = new Thread(new Runnable() {
@Override
public void run() {
StringBuilder response = new StringBuilder();
URL urlObject = new URL(requestUrl);
HttpsURLConnection connection = (HttpsURLConnection) urlObject.openConnection();
connection.setRequestMethod("GET");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
response.append(line);
}
bufferedReader.close();
return new JSONObject(response.toString());
}
})
}*/
}

0 comments on commit 501c5f2

Please sign in to comment.