Android收集下载
来源:长沙计算机培训学校 发布日期:2016-05-21
//for temp purpose to download return new File(sUpdateTempDir, name + "_" + "temp"); else { return new File(sUpdateTempDir, name + "_" + version); private static final class UpdateHandler extends Handler { private long mAutoUpdateTimer; private long FAIL_RETRY_TIMER = 5 * 60 * 1000; /* default 5 minutes if failed */ public UpdateHandler(Looper looper) { super(looper); private boolean getUpdateInfo(Message msg) throws Exception { String jsonText = ""; mAutoUpdateTimer = FAIL_RETRY_TIMER; cleanPendingMessages(); try { if(DEBUG) Log.v(TAG, "start to get server update info..."); String updateURL = WonderDB.DB_UPDATE2_PHP; if( Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN) { updateURL += "os=41"; else { updateURL += "os=42"; jsonText = Network.getWonderTVText(updateURL); if (jsonText == null || jsonText.length() == 0){ onServerUpdateInfo(null); for(UpdateItem item : sUpdateItems) { if(item.name.equals(NAME_APK)) continue; if( !getItemFolder(item.name).exists()){ mAutoUpdateTimer = 10 * 1000; //check later after 10s if any item not exist break; Log.w(TAG, "server update info return empty, check after " + (mAutoUpdateTimer/1000)); return false; jsonText = String.format("[%1$s]", jsonText); final JSONArray array = new JSONArray(jsonText); if (array.length() == 0) { Log.w(TAG, "json decode from server php wrong:" + jsonText); onServerUpdateInfo(null); return false; final JSONObject obj = array.getJSONObject(0); sUpdateInfo.updateFromJson(obj); if(DEBUG) Log.v(TAG, "server update info:" + sUpdateInfo); onServerUpdateInfo(sUpdateInfo); //check the updateToken if( !Network.getWonderTVId().matches(sUpdateInfo.updateToken)){ Log.v(TAG, "No token to update:" + Network.getWonderTVId() + "," + sUpdateInfo.updateToken); mAutoUpdateTimer = sUpdateInfo.busywaittime; return true; for(int i = 0; i sUpdateItems.size(); i++) { UpdateItem item = sUpdateItems.get(i); //start modify by tangzefang patch for upgrade if(NAME_APK.equals(item.name)) item.localVersion = LOCAL_VERSION; else item.localVersion = Util.getPropertyKeyLong(item.name, 0); //end modify by tangzefang item.serverVersion = Long.parseLong(obj.getString(item.name)); Log.d(TAG, item.name + " local version " + item.localVersion + ",server version " + item.serverVersion); if ( item.serverVersion item.localVersion || /* localVersion uses old time stamp, force a update then */ item.localVersion 1000000 ) { //check if we really need to download it or not Log.d(TAG, item.name + " need to update"); File f = getUnZipFolder(item.name, item.serverVersion); if( f.exists()) { //item has been downloaded last time, just update is OK. Log.d(TAG, item.name + " ver " + item.serverVersion + " exists, update now"); Util.setPropertyKeyValue(item.name, String.valueOf(item.serverVersion)); item.unzipFolder = f; onUpdateAvaliable(item, sUpdateInfo); else { File cur = getUnZipFolder(item.name, item.localVersion); cleanUnusedFolder(item.name, cur); //start to download this item now. Message itemMsg = sUpdateHandler.obtainMessage(MSG_UPDATE_CATALOG + i, item); onUpdateNeed(item, true); sUpdateHandler.sendMessage(itemMsg); Log.d(TAG, item.name + " start to download ver " + item.serverVersion); } else { Log.d(TAG, item.name + " no need to update."); onUpdateNeed(item, false); //good. talk to server later. mAutoUpdateTimer = sUpdateInfo.busywaittime; return true; catch(Exception e){ Log.d(TAG, "Exception when getupdateinfo", e); onServerUpdateInfo(null); for(UpdateItem item : sUpdateItems) { if(item.name.equals(NAME_APK)) continue; if( !getItemFolder(item.name).exists()){ mAutoUpdateTimer = 10 * 1000; //check later after 10s if any item not exist Log.w(TAG, item.name + " not exist, check after " + (mAutoUpdateTimer/1000) + "s"); break; return false; boolean executeZipUpdate(UpdateItem item) throws Exception { HttpURLConnection c = null; FileOutputStream fos = null; try { String urlPath; if( item.name.equals(NAME_APK)){ if(Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN) urlPath = SERV_UPDATE_FOLDER + item.name + "41_" + item.serverVersion + CAB_EXT; else urlPath = SERV_UPDATE_FOLDER + item.name + "42_" + item.serverVersion + CAB_EXT; else { urlPath = SERV_UPDATE_FOLDER + item.name + "_" + item.serverVersion + CAB_EXT; onDownloadProgress(item, 0); if( sUpdateInfo.serverRedirect.length() 1) { Log.v(TAG, item.name + " download update from redirect server:" + sUpdateInfo.serverRedirect); c = Network.createConnection(sUpdateInfo.serverRedirect, urlPath); else { Log.v(TAG, item.name + " download update from direct server"); c = Network.getWonderTVConnection(urlPath); if (c == null) { Log.w(TAG, item.name + " can't create connection " + sUpdateInfo.serverRedirect); onDownloadProgress(item, -1); return false; final File zipFile = new File(sUpdateTempDir, item.name + CAB_EXT); //modify by tangzefang File tempUnZipFolder = getUnZipFolder(item.name, -1); Log.d(TAG, item.name + " reading update file from " + urlPath + "..."); InputStream inputStream = new BufferedInputStream(c.getInputStream()); int readLen = 0; final byte[] buf = new byte[8192]; fos = new FileOutputStream(zipFile);