Transform CodecDownloader to lib class

This commit is contained in:
Erwan Croze 2016-06-21 13:36:27 +02:00
parent 76074d2bc0
commit 031a83953b

View file

@ -23,6 +23,7 @@ import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.preference.CheckBoxPreference;
import java.io.File;
import java.io.FileInputStream;
@ -45,72 +46,34 @@ interface CodecDownloadListener{
void listenerUpdateMsg(int now, int max);
void listenerDownloadEnding();
void listenerDownloadFailed(String error);
void listenerPluginReloaded();
}
interface CodecDownloadAction{
void startDownload(Context context, Object obj);
}
/**
* @author Erwan Croze
*/
public class CodecDownloader implements DialogInterface.OnClickListener, CodecDownloadListener{
private Context context;
public class CodecDownloader implements CodecDownloadListener,CodecDownloadAction{
private static String fileDirection;
private static String nameLib;
private static String urlDownload;
private static String nameFileDownload;
private static String messageUpdateDownload;
private static String messageEndSetup;
private static Handler mHandler;
private static ProgressDialog progress;
private static String licenseMessage = "OpenH264 Video Codec provided by Cisco Systems, Inc.";
public CodecDownloader(Context c) {
this.context = c;
this.mHandler = new Handler(this.context.getMainLooper());
this.progress = null;
public CodecDownloader() {
nameLib = "libopenh264-1.5.so";
urlDownload = "http://ciscobinary.openh264.org/libopenh264-1.5.0-android19.so.bz2";
nameFileDownload = "libopenh264-1.5.0-android19.so.bz2";
messageUpdateDownload = "Downloading codec";
messageEndSetup = "Codec OpenH264 downloaded";
}
public void askPopUp(String msg, String no, String yes) {
if (this.context == null) return;
AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
builder.setCancelable(false);
builder.setMessage(msg).setPositiveButton(yes, this)
.setNegativeButton(no, this).show();
static public String getLicenseMessage() {
return licenseMessage;
}
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
downloadCodec();
break;
case DialogInterface.BUTTON_NEGATIVE:
// Disable H264
PayloadType h264 = null;
for (PayloadType pt : LinphoneManager.getLcIfManagerNotDestroyedOrNull().getVideoCodecs()) {
if (pt.getMime().equals("H264")) h264 = pt;
}
if (h264 == null) return;
if (LinphonePreferences.instance().isFirstLaunch()) {
try {
LinphoneManager.getLcIfManagerNotDestroyedOrNull().enablePayloadType(h264, false);
} catch (LinphoneCoreException e) {
e.printStackTrace();
}
}
break;
}
}
static public void setMessageEndSetup(String s) {
messageEndSetup = s;
}
static public void setMessageUpdateDownloadDl(String s) {
messageUpdateDownload = s;
static public void setFileDirection(String s) {
fileDirection = s;
}
static public void setNameLib(String s) {
@ -135,26 +98,26 @@ public class CodecDownloader implements DialogInterface.OnClickListener, CodecDo
}
public void downloadCodec() {
if (context == null) return;
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
try {
String path = context.getFilesDir()+"/" + nameLib;
String path = fileDirection+"/" + nameLib;
URL url = new URL(urlDownload);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.connect();
listenerDownloadStarting();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(context.getFilesDir()+"/"+nameFileDownload);
FileOutputStream fileOutputStream = new FileOutputStream(fileDirection+"/"+nameFileDownload);
int totalSize = urlConnection.getContentLength();
byte[] buffer = new byte[4096];
int bufferLength;
int total = 0;
listenerDownloadStarting();
listenerUpdateMsg(total, totalSize);
while((bufferLength = inputStream.read(buffer))>0 ){
total += bufferLength;
fileOutputStream.write(buffer, 0, bufferLength);
@ -164,7 +127,7 @@ public class CodecDownloader implements DialogInterface.OnClickListener, CodecDo
fileOutputStream.close();
inputStream.close();
FileInputStream in = new FileInputStream(context.getFilesDir()+"/"+nameFileDownload);
FileInputStream in = new FileInputStream(fileDirection+"/"+nameFileDownload);
FileOutputStream out = new FileOutputStream(path);
BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
@ -175,11 +138,8 @@ public class CodecDownloader implements DialogInterface.OnClickListener, CodecDo
out.close();
bzIn.close();
new File(context.getFilesDir()+"/"+nameFileDownload).delete();
new File(fileDirection+"/"+nameFileDownload).delete();
listenerDownloadEnding();
LinphoneCoreFactoryImpl.loadOptionalLibraryWithPath(path);
LinphoneManager.getLc().reloadMsPlugins(null);
listenerPluginReloaded();
} catch (FileNotFoundException e) {
listenerDownloadFailed(e.getMessage());
} catch (IOException e) {
@ -192,70 +152,21 @@ public class CodecDownloader implements DialogInterface.OnClickListener, CodecDo
@Override
public void listenerDownloadStarting() {
if (context == null) return;
mHandler.post(new Runnable() {
@Override
public void run() {
progress = new ProgressDialog(context);
progress.setCanceledOnTouchOutside(false);
progress.setCancelable(false);
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
});
}
@Override
public void listenerUpdateMsg(final int now, final int max) {
if (progress == null) return;
mHandler.post(new Runnable() {
@Override
public void run() {
progress.setMessage(messageUpdateDownload);
progress.setMax(max);
progress.setProgress(now);
progress.show();
}
});
}
@Override
public void listenerDownloadEnding() {
if (progress == null) return;
mHandler.post(new Runnable() {
@Override
public void run() {
progress.dismiss();
}
});
}
@Override
public void listenerDownloadFailed(final String error) {
if (progress == null) return;
mHandler.post(new Runnable() {
@Override
public void run() {
if(progress != null) progress.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(error);
builder.show();
}
});
}
@Override
public void listenerPluginReloaded() {
if (progress == null) return;
mHandler.post(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(messageEndSetup);
builder.setCancelable(false);
builder.setNeutralButton("Ok",null);
builder.show();
}
});
public void startDownload(Context context, Object obj) {
}
}