2019-01-31 05:23:49 +01:00
|
|
|
package com.topjohnwu.magisk.components;
|
2018-12-03 05:41:16 +01:00
|
|
|
|
2018-12-29 10:49:41 +01:00
|
|
|
import android.app.Service;
|
2018-12-03 05:41:16 +01:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
2018-12-29 10:49:41 +01:00
|
|
|
import android.os.IBinder;
|
|
|
|
import android.widget.Toast;
|
2018-12-03 05:41:16 +01:00
|
|
|
|
2019-03-07 09:41:24 +01:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
2018-12-13 10:35:50 +01:00
|
|
|
import com.topjohnwu.magisk.ClassMap;
|
2019-01-30 09:10:12 +01:00
|
|
|
import com.topjohnwu.magisk.Const;
|
2018-12-03 05:41:16 +01:00
|
|
|
import com.topjohnwu.magisk.FlashActivity;
|
2018-12-29 10:49:41 +01:00
|
|
|
import com.topjohnwu.magisk.R;
|
2019-01-30 09:10:12 +01:00
|
|
|
import com.topjohnwu.magisk.container.Repo;
|
2019-01-31 05:23:49 +01:00
|
|
|
import com.topjohnwu.magisk.uicomponents.ProgressNotification;
|
2019-01-30 09:10:12 +01:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
2018-12-12 11:51:45 +01:00
|
|
|
import com.topjohnwu.net.Networking;
|
2018-12-29 10:49:41 +01:00
|
|
|
import com.topjohnwu.superuser.Shell;
|
2018-12-03 05:41:16 +01:00
|
|
|
import com.topjohnwu.superuser.ShellUtils;
|
|
|
|
|
|
|
|
import java.io.BufferedOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipInputStream;
|
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
2018-12-29 10:49:41 +01:00
|
|
|
public class DownloadModuleService extends Service {
|
|
|
|
|
|
|
|
private boolean running = false;
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
if (flags == 0 && running) {
|
|
|
|
Utils.toast(R.string.dl_one_module, Toast.LENGTH_LONG);
|
|
|
|
} else {
|
|
|
|
running = true;
|
|
|
|
Shell.EXECUTOR.execute(() -> {
|
|
|
|
Repo repo = intent.getParcelableExtra("repo");
|
|
|
|
boolean install = intent.getBooleanExtra("install", false);
|
|
|
|
dlProcessInstall(repo, install);
|
|
|
|
stopSelf();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return START_REDELIVER_INTENT;
|
2018-12-03 05:41:16 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 10:49:41 +01:00
|
|
|
private void dlProcessInstall(Repo repo, boolean install) {
|
2018-12-03 05:41:16 +01:00
|
|
|
File output = new File(Const.EXTERNAL_PATH, repo.getDownloadFilename());
|
2018-12-03 07:44:13 +01:00
|
|
|
ProgressNotification progress = new ProgressNotification(output.getName());
|
2018-12-29 10:49:41 +01:00
|
|
|
startForeground(progress.hashCode(), progress.getNotification());
|
2018-12-03 05:41:16 +01:00
|
|
|
try {
|
2018-12-12 11:51:45 +01:00
|
|
|
InputStream in = Networking.get(repo.getZipUrl())
|
|
|
|
.setDownloadProgressListener(progress)
|
|
|
|
.execForInputStream().getResult();
|
|
|
|
removeTopFolder(in, new BufferedOutputStream(new FileOutputStream(output)));
|
2018-12-03 05:41:16 +01:00
|
|
|
if (install) {
|
|
|
|
progress.dismiss();
|
2018-12-29 10:49:41 +01:00
|
|
|
Intent intent = new Intent(this, ClassMap.get(FlashActivity.class));
|
2018-12-03 05:41:16 +01:00
|
|
|
intent.setData(Uri.fromFile(output))
|
|
|
|
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
|
|
.putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_ZIP);
|
2018-12-29 10:49:41 +01:00
|
|
|
startActivity(intent);
|
2018-12-03 05:41:16 +01:00
|
|
|
} else {
|
|
|
|
progress.dlDone();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
progress.dlFail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 10:49:41 +01:00
|
|
|
private void removeTopFolder(InputStream in, OutputStream out) throws IOException {
|
2018-12-03 05:41:16 +01:00
|
|
|
try (ZipInputStream zin = new ZipInputStream(in);
|
|
|
|
ZipOutputStream zout = new ZipOutputStream(out)) {
|
|
|
|
ZipEntry entry;
|
|
|
|
int off = -1;
|
|
|
|
while ((entry = zin.getNextEntry()) != null) {
|
|
|
|
if (off < 0)
|
|
|
|
off = entry.getName().indexOf('/') + 1;
|
|
|
|
String path = entry.getName().substring(off);
|
|
|
|
if (path.isEmpty())
|
|
|
|
continue;
|
|
|
|
zout.putNextEntry(new ZipEntry(path));
|
|
|
|
if (!entry.isDirectory())
|
|
|
|
ShellUtils.pump(zin, zout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|