package com.mobilinga.video_vr;
import android.app.IntentService;
import android.content.Intent;
import android.os.Environment;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class DownloadService extends IntentService {
public static final int UPDATE_PROGRESS = 8344;
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraVideo");
private String CHANNEL_ID = "ID";
private int notificationId = 55;
public DownloadService() {
super("DownloadService");
Log.i("devoloTest", "DownloadService");
}
public static void unzip(File zipFile, File targetDirectory) throws IOException {
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(new FileInputStream(zipFile)));
try {
ZipEntry ze;
int count;
byte[] buffer = new byte[8192];
while ((ze = zis.getNextEntry()) != null) {
File file = new File(targetDirectory, ze.getName());
File dir = ze.isDirectory() ? file : file.getParentFile();
if (!dir.isDirectory() && !dir.mkdirs())
throw new FileNotFoundException("Failed to ensure directory: " +
dir.getAbsolutePath());
if (ze.isDirectory())
continue;
FileOutputStream fout = new FileOutputStream(file);
try {
while ((count = zis.read(buffer)) != -1)
fout.write(buffer, 0, count);
} finally {
fout.close();
}
/* if time should be restored as well
long time = ze.getTime();
if (time > 0)
file.setLastModified(time);
*/
}
} finally {
zis.close();
}
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i("devoloTest", "onHandleIntent");
String urlToDownload = intent.getStringExtra("url");
Log.i("devoloTest", " " + urlToDownload);
long fileLength = 0;
try {
URL url = new URL(urlToDownload);
Log.i("Test", "URL is " + urlToDownload);
URLConnection connection = url.openConnection();
connection.setRequestProperty("Accept-Encoding", "identity");
connection.connect();
SettingManager settingManager = new SettingManager(this);
// download the file
InputStream input = new BufferedInputStream(connection.getInputStream());
File file = new File(mediaStorageDir.getPath() + File.separator + settingManager.getVideoType() + File.separator + "v2.zip");
Log.i("Test", "URL is " + file.getPath());
OutputStream output = new FileOutputStream(file);
// this will be useful so that you can show a typical 0-100% progress bar
fileLength = connection.getContentLength();
Log.i("Test", "FileLength is" + fileLength);
byte data[] = new byte[32768];
long total = 0;
settingManager.setLoadingStatusByCurrentSettings(SettingManager.LoadingStatus.LOADING);
int count;
while ((count = input.read(data)) != -1) {
total += count;
Log.i("Test", "count is" + count);
showProgressNotification((int) total / (1024 * 1024), (int) (fileLength / (1024 * 1024)));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
File outputDirectory = new File(mediaStorageDir, settingManager.getVideoType());
if (!outputDirectory.exists())
outputDirectory.mkdirs();
showUnzipNotification();
unzip(file, outputDirectory);
file.delete();
settingManager.setLoadingStatusByCurrentSettings(SettingManager.LoadingStatus.DONE);
showDoneNotification();
} catch (IOException e) {
e.printStackTrace();
}
}
private void showProgressNotification(int total, int fileLength) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Loading a video")
.setContentText(total + "/" + fileLength + " MB")
.setAutoCancel(false)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setProgress(total, fileLength, false)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, mBuilder.build());
}
private void showUnzipNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Loading a video")
.setContentText("File is unzipping")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, mBuilder.build());
}
private void showDoneNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentText("Loading a video is done")
.setContentTitle("Done")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, mBuilder.build());
}
}
04-12 14:56:31.463 15920-15920 / com.mobilinga.mob360 I / devoloTest:DownloadService 04-12 14:56:31.469 15920-25109 / com.mobilinga.mob360 I / devoloTest:onHandleIntent 04-12 14:56:31.470 15920-25109 / com.mobilinga.mob360 I / devoloTest:https://myFile.zip 04-12 14:56:31.470 15920-25109 / com.mobilinga.mob360 I / Test:URL为https://myFile.zip 04-12 14:56:31.856 15920-25109 / com.mobilinga.mob360 I / Test:URL为/storage/emulated/0/Pictures/MyCameraVideo/480_en/v2.zip
似乎装载没有开始。
网址有效,并授予权限。