@Override public void run() { // 判断SD卡是否存在,并且是否具有读写权限 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { mDownloadDialog.dismiss(); Message msg = new Message(); msg.obj = mContext.getString(R.string.sdcard_not_available); msg.what = TOAST_MESSAGE; mHandler.sendMessage(msg); return; } // 获得存储卡的路径 mSavePath = com.coco.theme.themebox.util.PathTool.getDownloadDir(); HttpURLConnection conn = null; InputStream is = null; FileOutputStream fos = null; try { URL url = new URL(urlString); // 创建连接 conn = (HttpURLConnection) url.openConnection(); conn.connect(); // 获取文件大小 int length = conn.getContentLength(); // 创建输入流 is = conn.getInputStream(); File file = new File(mSavePath); // 判断文件目录是否存在 if (!file.exists()) { file.mkdir(); } File apkFile = new File(mSavePath, mContext.getPackageName() + ".apk"); fos = new FileOutputStream(apkFile); int count = 0; // 缓存 byte buf[] = new byte[1024]; long sizeChangeTimeMillis = 0; // 写入到文件中 do { int numread = is.read(buf); count += numread; // 计算进度条位置 progress = (int) (((float) count / length) * 100); // 更新进度 if (numread <= 0) { // 下载完成 mHandler.sendEmptyMessage(DOWNLOAD_FINISH); break; } else { long currentTimeMillis = System.currentTimeMillis(); if (currentTimeMillis - sizeChangeTimeMillis < 0 || currentTimeMillis - sizeChangeTimeMillis > 1000) { Message msg = new Message(); msg.what = DOWNLOAD; msg.arg1 = progress; mHandler.sendMessage(msg); sizeChangeTimeMillis = currentTimeMillis; } } // 写入文件 fos.write(buf, 0, numread); } while (!cancelUpdate); // 点击取消就停止下载. } catch (MalformedURLException e) { e.printStackTrace(); Message msg = new Message(); msg.obj = mContext.getString(R.string.internet_unusual); msg.what = TOAST_MESSAGE; mHandler.sendMessage(msg); stopSelf(); } catch (IOException e) { e.printStackTrace(); Message msg = new Message(); msg.obj = mContext.getString(R.string.internet_unusual); msg.what = TOAST_MESSAGE; mHandler.sendMessage(msg); stopSelf(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (conn != null) { conn.disconnect(); } } }
@Override protected void onCreate(Bundle savedInstanceState) { ActivityManager.pushActivity(this); super.onCreate(savedInstanceState); mContext = this; // added by zhenNan.ye begin String sdpath = getSDPath(); if (sdpath != null) { PATH_ENABLE_LOG = sdpath + File.separator + "enablelog.log"; File dir = new File(PATH_ENABLE_LOG); if (dir.exists()) { Log.setEnableLog(true); } } // added by zhenNan.ye end if (!com.coco.theme.themebox.StaticClass.isAllowDownload(mContext) && com.coco.theme.themebox.util.FunctionConfig .isDownToInternal()) { com.coco.theme.themebox.StaticClass.canDownToInternal = true; } PathTool.makeDirApp(); // teapotXu_20130304: add start // set a flag that indicates whether the ThemeSelectIcon launched the // ThemeBox or Launcher app. String pkgNameFromThemeBox = getIntent().getStringExtra("FROM_PACKAGE"); if (pkgNameFromThemeBox != null && pkgNameFromThemeBox.length() > 16 && pkgNameFromThemeBox.substring(0, 16).equals( "com.coco.themes.")) { b_theme_icon_start_launcher = true; return; } // teapotXu_20130304: add end downModule = new DownModule(this); themedownModule = new ThemeDownModule(this); if (!com.coco.theme.themebox.util.FunctionConfig.isLockVisible() && !com.coco.lock2.lockbox.StaticClass.isLockBoxInstalled(this)) { setContentView(R.layout.man_tab_nolock); } else { setContentView(R.layout.main_tab_lock); } tabHost = (TabHost) findViewById(R.id.tabhost); tabHost.setup(); // 添加主题页面 tabTheme = new TabThemeFactory(MainActivity.this, themedownModule); final View indicatorTheme = View.inflate(MainActivity.this, R.layout.indicator_theme, null); tabHost.addTab(tabHost.newTabSpec(TAG_THEME) .setIndicator(indicatorTheme).setContent(tabTheme)); if (com.coco.theme.themebox.util.FunctionConfig.isLockVisible() || com.coco.lock2.lockbox.StaticClass.isLockBoxInstalled(this)) { int Screen_W = getWindowManager().getDefaultDisplay().getWidth(); int Screen_H = getWindowManager().getDefaultDisplay().getHeight(); // 添加锁屏页面 tabLock = new TabLockFactory(MainActivity.this, downModule, Screen_W, Screen_H); View indicatorLock = View.inflate(MainActivity.this, R.layout.indicator_lock, null); tabHost.addTab(tabHost.newTabSpec(TAG_LOCK) .setIndicator(indicatorLock).setContent(tabLock)); int tabIndex = getIntent().getIntExtra( StaticClass.EXTRA_MAIN_TAB_INDEX, 0); switch (tabIndex) { default: case 0: tabHost.setCurrentTabByTag(TAG_THEME); break; case 1: tabHost.setCurrentTabByTag(TAG_LOCK); break; } } // 友盟 进入主题盒子统计 MobclickAgent.onEvent(mContext, "StartBox"); // 友盟 新用户统计 NewUser(mContext); // 友盟 活跃用户统计 ActiveUser(mContext); }