Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android: extract resources to external storage on first launch #15396

Merged
merged 1 commit into from Feb 9, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -3,10 +3,16 @@
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.System;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


public class MainActivity extends android.app.NativeActivity {
@@ -37,6 +43,13 @@ private void set_url(String url) {

@Override
public void onCreate(Bundle savedInstanceState) {
if (needsToExtractAssets()) {
try {
extractAssets();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
if (intent.getAction().equals(Intent.ACTION_VIEW)) {
@@ -62,4 +75,42 @@ protected void onStop() {
android.os.Process.killProcess(pid);
System.exit(0);
}

private boolean needsToExtractAssets() {
// todo: also force a reextract when the resources are updated.
return !(new File("/sdcard/servo").exists());
}

/**
* extracts assets/ in the APK to /sdcard/servo.
*/
private void extractAssets() throws IOException {
ZipFile zipFile = null;
File targetDir = new File("/sdcard/servo");
try {
zipFile = new ZipFile(this.getApplicationInfo().sourceDir);
for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); ) {
ZipEntry entry = e.nextElement();
if (entry.isDirectory() || !entry.getName().startsWith("assets/")) {
continue;
}
File targetFile = new File(targetDir, entry.getName().substring("assets/".length()));
targetFile.getParentFile().mkdirs();
byte[] tempBuffer = new byte[(int)entry.getSize()];
BufferedInputStream is = null;
FileOutputStream os = null;
try {
is = new BufferedInputStream(zipFile.getInputStream(entry));
os = new FileOutputStream(targetFile);
is.read(tempBuffer);
os.write(tempBuffer);
} finally {
if (is != null) is.close();
if (os != null) os.close();
}
}
} finally {
if (zipFile != null) zipFile.close();
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.