自动更新

This commit is contained in:
seven332 committed 2014-03-23 10:14:47 +08:00
1 parent 7254d9d7a7
commit 471962e664
6 files changed
+3330

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
# cache for current jar dependency. DO NOT EDIT.
# format is <lastModified> <length> <SHA-1> <path>
# Encoding is UTF-8
+147
View File
@@ -0,0 +1,147 @@
package com.hippo.ehviewer;
import java.io.File;
import java.net.MalformedURLException;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import com.hippo.ehviewer.dialog.DialogBuilder;
import com.hippo.ehviewer.network.Downloader;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.EhClient;
import com.hippo.ehviewer.view.AlertButton;
public class UpdateHelper {
private static final String TAG = "UpdateHelper";
private Activity mActivity;
private String updateFileName;
private OnCheckUpdateListener listener;
public UpdateHelper(Activity activity) {
mActivity = activity;
}
public void checkUpdate() {
EhClient.checkUpdate(new EhClient.OnCheckUpdateListener() {
@Override
public void onSuccess(String pageContext) {
String[] items = pageContext.split("\n");
if (items.length > 3) {
if (listener != null)
listener.onSuccess(pageContext);
String newVer = items[0];
final String url = EhClient.UPDATE_URL + items[1];
final String name = url.substring(url.lastIndexOf('/')+1);
updateFileName = name;
String size = items[2];
String info = items[3];
AlertDialog dialog = new DialogBuilder(mActivity).setTitle(R.string.update)
.setMessage(String.format(mActivity.getString(R.string.update_message), newVer, size, info))
.setNegativeButton(android.R.string.cancel, new View.OnClickListener() {
@Override
public void onClick(View v) {
((AlertButton)v).dialog.dismiss();
}
}).setPositiveButton(android.R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View v) {
((AlertButton)v).dialog.dismiss();
Downloader d = new Downloader();
try {
d.resetData(Config.getDownloadPath(), name, url);
d.setOnDownloadListener(new UpdateListener());
new Thread(d).start();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}).create();
if (!mActivity.isFinishing())
dialog.show();
} else if(pageContext.equals("none")) {
if (listener != null)
listener.onNoUpdate();
} else if(pageContext.equals("error")){
if (listener != null)
listener.onFailure(R.string.em_request_error);
} else {
if (listener != null)
listener.onFailure(R.string.em_host_error);
}
}
@Override
public void onFailure(int eMesgId) {
Log.d(TAG, mActivity.getString(eMesgId));
if (listener != null)
listener.onFailure(eMesgId);
}
});
}
public UpdateHelper SetOnCheckUpdateListener(OnCheckUpdateListener listener) {
this.listener = listener;
return this;
}
public interface OnCheckUpdateListener {
public void onSuccess(String pageContext);
public void onNoUpdate();
public void onFailure(int eMesgId);
}
private class UpdateListener implements Downloader.OnDownloadListener {
private NotificationManager mNotifyManager;
private NotificationCompat.Builder mBuilder;
public UpdateListener() {
mNotifyManager = (NotificationManager)
mActivity.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(mActivity.getApplicationContext());
mBuilder.setSmallIcon(R.drawable.ic_launcher);
}
@Override
public void onDownloadStart(int totalSize) {
mBuilder.setContentTitle("正在下载更新")
.setContentText(null)
.setProgress(0, 0, true).setOngoing(true);
mNotifyManager.notify(233, mBuilder.build());
}
@Override
public void onDownloadStatusUpdate(
int downloadSize, int totalSize) {
mBuilder.setContentTitle("正在下载更新")
.setContentText(String.format("%.2f / %.2f KB", downloadSize/1024.0f, totalSize/1024.0f))
.setProgress(totalSize, downloadSize, false).setOngoing(true);
mNotifyManager.notify(233, mBuilder.build());
}
@Override
public void onDownloadOver(boolean ok, int eMesgId) {
if (ok) {
mNotifyManager.cancel(233);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Config.getDownloadPath(), updateFileName)),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mActivity.getApplicationContext().startActivity(intent);
} else {
mBuilder.setContentTitle("下载更新失败")
.setContentText(null)
.setProgress(0, 0, false).setOngoing(false);
}
}
}
}
@@ -0,0 +1,46 @@
package com.hippo.ehviewer.view;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
public class GetPaddingRelativeLayout extends RelativeLayout {
private static final String TAG = "GetPaddingRelativeLayout";
private View mView1;
private View mView2;
public GetPaddingRelativeLayout(Context context) {
super(context);
}
public GetPaddingRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GetPaddingRelativeLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public void setView(View view1, View view2) {
mView1 = view1;
mView2 = view2;
}
@Override
protected boolean fitSystemWindows(Rect insets) {
boolean re = super.fitSystemWindows(insets);
int pt = this.getPaddingTop();
int pb = this.getPaddingBottom();
if (mView1 != null)
mView1.setPadding(mView1.getPaddingLeft(), pt, mView1.getPaddingRight(), pb);
if (mView2 != null)
mView2.setPadding(mView2.getPaddingLeft(), pt, mView2.getPaddingRight(), pb);
return re;
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff