only copy updated assets

This commit is contained in:
badaix 2016-01-18 19:11:06 +01:00
parent caba850bec
commit f74825bed9
4 changed files with 114 additions and 19 deletions

View file

@ -32,18 +32,7 @@
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!--
<activity
android:name=".SnapcastActivity"
android:label="@string/title_activity_snapcast"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
-->
<!-- android:theme="@style/Theme.AppCompat.Light.DialogWhenLarge" -->
<activity
android:name=".ClientSettingsActivity"
android:label="@string/title_activity_client_settings"

View file

@ -0,0 +1,96 @@
package de.badaix.snapcast;
/*
* Copyright (C) 2012 The CyanogenMod Project
*
* * Licensed under the GNU GPLv2 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl-2.0.txt
*/
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
private static final String TAG = "MD5";
public static boolean checkMD5(String md5, File updateFile) {
if (TextUtils.isEmpty(md5) || updateFile == null) {
Log.e(TAG, "MD5 string empty or updateFile null");
return false;
}
String calculatedDigest = calculateMD5(updateFile);
if (calculatedDigest == null) {
Log.e(TAG, "calculatedDigest null");
return false;
}
Log.v(TAG, "Calculated digest: " + calculatedDigest);
Log.v(TAG, "Provided digest: " + md5);
return calculatedDigest.equalsIgnoreCase(md5);
}
public static String calculateMD5(File updateFile) {
InputStream is;
try {
is = new FileInputStream(updateFile);
} catch (FileNotFoundException e) {
Log.e(TAG, "Exception while getting FileInputStream", e);
return null;
}
try {
return calculateMD5(is);
} finally {
try {
is.close();
} catch (IOException e) {
Log.e(TAG, "Exception on closing MD5 input stream", e);
}
}
}
public static String calculateMD5(InputStream is) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "Exception while getting digest", e);
return null;
}
byte[] buffer = new byte[8192];
int read;
try {
while ((read = is.read(buffer)) > 0) {
digest.update(buffer, 0, read);
}
byte[] md5sum = digest.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
String output = bigInt.toString(16);
// Fill to 32 chars
output = String.format("%32s", output).replace(' ', '0');
return output;
} catch (IOException e) {
throw new RuntimeException("Unable to process file for MD5", e);
} finally {
try {
is.reset();
} catch (IOException e) {
Log.e(TAG, "Exception on closing MD5 input stream", e);
}
}
}
}

View file

@ -103,7 +103,14 @@ public class MainActivity extends AppCompatActivity implements ClientInfoItem.Cl
lvClient.setAdapter(clientInfoAdapter);
getSupportActionBar().setSubtitle("Host: no Snapserver found");
Setup.copyAssets(this, new String[]{"snapclient"});
new Thread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "copying snapclient");
Setup.copyAssets(MainActivity.this, new String[]{"snapclient"});
Log.d(TAG, "done copying snapclient");
}
}).start();
initializeDiscoveryListener();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.

View file

@ -1,6 +1,7 @@
package de.badaix.snapcast;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.util.Log;
@ -35,16 +36,18 @@ public class Setup {
InputStream in = null;
OutputStream out = null;
try {
File outFile = new File(context.getFilesDir(), filename);
// if (outFile.exists() && (outFile.length() == assetManager.openFd(filename).getLength())) {
// Log.d("Main", "Exists: " + outFile.getAbsolutePath());
// continue;
// }
Log.d("Main", "Asset: " + outFile.getAbsolutePath());
SharedPreferences prefs = context.getSharedPreferences("assets", Context.MODE_PRIVATE);
in = assetManager.open(filename);
String md5 = MD5.calculateMD5(in);
File outFile = new File(context.getFilesDir(), filename);
Log.d(TAG, "Asset: " + outFile.getAbsolutePath() + ", md5: " + md5);
if (outFile.exists() && md5.equals(prefs.getString(filename, "")))
continue;
Log.d(TAG, "Copying " + outFile.getAbsolutePath());
out = new FileOutputStream(outFile);
copyFile(in, out);
Runtime.getRuntime().exec("chmod 755 " + outFile.getAbsolutePath()).waitFor();
prefs.edit().putString(filename, md5).apply();
} catch (Exception e) {
Log.e(TAG, "Failed to copy asset file: " + filename, e);
} finally {