Skip to content

Commit

Permalink
Updater improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Sep 26, 2016
1 parent 40eb609 commit bc6e665
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
Expand All @@ -56,6 +59,7 @@ public class UpdateActivity extends AppCompatActivity {
private static SharedPreferences prefs;
private static int versionnumber = 0;
private static int newversion = 0;
private static String lastDigest = "";
private final static int MY_PERMISSIONS_REQUEST_STORAGE_DOWNLOAD = 105;
private static boolean downloading = false;
private static final boolean debug = false;
Expand All @@ -67,6 +71,7 @@ public class UpdateActivity extends AppCompatActivity {
private static String DOWNLOAD_URL = "";
private static int FILE_SIZE = -1;
private static String MESSAGE = "";
private static String CHECKSUM = "";

public static void checkForAnUpdate(final Context context) {
if (prefs == null) prefs = PreferenceManager.getDefaultSharedPreferences(context);
Expand Down Expand Up @@ -144,7 +149,13 @@ public void run() {
} else {
MESSAGE = "";
}
Intent intent = new Intent(context, UpdateActivity.class);
if (lines.length > 4) {
CHECKSUM = lines[4];
} else {
CHECKSUM = "";
}

final Intent intent = new Intent(context, UpdateActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

Expand Down Expand Up @@ -182,12 +193,8 @@ private static String getDownloadFolder() {
private static void getVersionInformation(Context context) {
// try {
if (versionnumber == 0) {
//versionnumber = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_META_DATA).versionCode;
versionnumber = BuildConfig.buildVersion;
}
// } catch (PackageManager.NameNotFoundException e) {
// Log.e(TAG, "PackageManager.NameNotFoundException:" + e.getMessage());
// }
}

@Override
Expand Down Expand Up @@ -331,27 +338,38 @@ protected Boolean doInBackground(Void... params) {

Log.d(TAG, "Filename: " + filename);
if (response.code() == 200) {
lastDigest = "";
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {

dest_file = new File(getDownloadFolder(), filename);
try {
if (dest_file.exists())
dest_file.delete();
} catch (Exception e) {
Log.e(TAG, "Got exception deleting existing file: " + e);
}
;

outputStream = new FileOutputStream(dest_file);
inputStream = response.body().byteStream();
MessageDigest messageDigest = null;
DigestInputStream digestInputStream = null;
try {
messageDigest = MessageDigest.getInstance("SHA256");
digestInputStream = new DigestInputStream(inputStream, messageDigest);
} catch (NoSuchAlgorithmException e) {
//
}
byte[] buff = new byte[1024 * 4];
long downloaded = 0;
long target = response.body().contentLength();
if (target == -1)
target = FILE_SIZE; // get this from update server alternately
publishProgress(0L, target);
while (true) {
int last_read = inputStream.read(buff);

int last_read = (digestInputStream != null) ? digestInputStream.read(buff) : inputStream.read(buff);
if (last_read == -1) {
break;
}
Expand All @@ -362,7 +380,10 @@ protected Boolean doInBackground(Void... params) {
return false;
}
}
if (messageDigest != null)
lastDigest = JoH.bytesToHex(messageDigest.digest()).toLowerCase();
return downloaded == target;

} catch (IOException e) {
Log.e(TAG, "Download error: " + e.toString());
JoH.static_toast_long(getApplicationContext(), "Data error: ");
Expand Down Expand Up @@ -409,16 +430,26 @@ protected void onPostExecute(Boolean result) {
downloading = false;
if (result) {
if ((filename != null) && (filename.length() > 5) && (dest_file != null)) {
try {
final Intent installapk = new Intent(Intent.ACTION_VIEW);
installapk.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
installapk.setDataAndType(Uri.fromFile(dest_file), "application/vnd.android.package-archive");
startActivity(installapk);
if ((CHECKSUM.length() == 0) || (lastDigest.length() == 0) || (CHECKSUM.equals(lastDigest))) {
try {
final Intent installapk = new Intent(Intent.ACTION_VIEW);
installapk.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
installapk.setDataAndType(Uri.fromFile(dest_file), "application/vnd.android.package-archive");
startActivity(installapk);
finish();
} catch (Exception e) {
Log.e(TAG, "Got exception trying to install apk: " + e);
JoH.static_toast_long(getApplicationContext(), "Update is in your downloads folder");
}
} else {
Log.e(TAG, "Checksum doesn't match: " + lastDigest + " vs " + CHECKSUM);
try {
dest_file.delete();
} catch (Exception e) {
Log.e(TAG, "Got exception deleting corrupt file: " + e);
}
JoH.static_toast_long("File appears corrupt!");
finish();
} catch (Exception e) {
Log.e(TAG, "Got exception trying to install apk: " + e);
JoH.static_toast_long(getApplicationContext(), "Update is in your downloads folder");
prefs.edit().putBoolean(useInternalDownloaderPrefsName, false).apply(); // turn off as borked
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/pref_advanced_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
<CheckBoxPreference
android:defaultValue="false"
android:key="interpret_raw"
android:summary="If using Share, DexDrip will show values when they are normally hidden on the receiver."
android:summary="If using Share, xDrip will show values when they are normally hidden on the receiver."
android:title="Interpret Raw Values" />
<CheckBoxPreference
android:defaultValue="true"
Expand Down

0 comments on commit bc6e665

Please sign in to comment.