@Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   mAdapter = new ListViewAdapter();
   mAdapter.setOnItemClickListener(this);
   mAppInfos = DataSource.getInstance().getData();
   for (AppInfo info : mAppInfos) {
     DownloadInfo downloadInfo = DownloadManager.getInstance().getDownloadProgress(info.getUrl());
     if (downloadInfo != null) {
       info.setProgress(downloadInfo.getProgress());
       info.setDownloadPerSize(
           getDownloadPerSize(downloadInfo.getFinished(), downloadInfo.getLength()));
       info.setStatus(AppInfo.STATUS_PAUSE);
     }
   }
 }
  @Override
  public void onItemClick(View v, final int position, final AppInfo appInfo) {

    if (appInfo.getStatus() == AppInfo.STATUS_DOWNLOADING
        || appInfo.getStatus() == AppInfo.STATUS_CONNECTING) {
      if (isCurrentListViewItemVisible(position)) {
        DownloadManager.getInstance().pause(appInfo.getUrl());
      }
      return;
    } else if (appInfo.getStatus() == AppInfo.STATUS_COMPLETE) {
      if (isCurrentListViewItemVisible(position)) {
        Utils.installApp(getActivity(), new File(dir, appInfo.getName() + ".apk"));
      }
      return;
    } else if (appInfo.getStatus() == AppInfo.STATUS_INSTALLED) {
      if (isCurrentListViewItemVisible(position)) {
        Utils.unInstallApp(getActivity(), appInfo.getPackageName());
      }
    } else {
      download(position, appInfo);
    }
  }
  private void download(final int position, final AppInfo appInfo) {
    DownloadManager.getInstance()
        .download(
            appInfo.getName() + ".apk",
            appInfo.getUrl(),
            dir,
            new CallBack() {

              @Override
              public void onDownloadStart() {
                appInfo.setStatus(AppInfo.STATUS_CONNECTING);
                if (isCurrentListViewItemVisible(position)) {
                  ListViewAdapter.ViewHolder holder = getViewHolder(position);
                  holder.tvStatus.setText(appInfo.getStatusText());
                  holder.btnDownload.setText(appInfo.getButtonText());
                }
              }

              @Override
              public void onConnected(long total, boolean isRangeSupport) {
                appInfo.setStatus(AppInfo.STATUS_DOWNLOADING);
                if (isCurrentListViewItemVisible(position)) {
                  ListViewAdapter.ViewHolder holder = getViewHolder(position);
                  holder.tvStatus.setText(appInfo.getStatusText());
                  holder.btnDownload.setText(appInfo.getButtonText());
                }
              }

              @Override
              public void onProgress(long finished, long total, int progress) {
                String downloadPerSize = getDownloadPerSize(finished, total);
                appInfo.setProgress(progress);
                appInfo.setDownloadPerSize(downloadPerSize);
                appInfo.setStatus(AppInfo.STATUS_DOWNLOADING);
                if (isCurrentListViewItemVisible(position)) {
                  ListViewAdapter.ViewHolder holder = getViewHolder(position);
                  holder.tvDownloadPerSize.setText(downloadPerSize);
                  holder.progressBar.setProgress(progress);
                  holder.tvStatus.setText(appInfo.getStatusText());
                  holder.btnDownload.setText(appInfo.getButtonText());
                }
              }

              @Override
              public void onComplete() {
                appInfo.setStatus(AppInfo.STATUS_COMPLETE);
                File apk = new File(dir, appInfo.getName() + ".apk");
                if (apk.isFile() && apk.exists()) {
                  String packageName = Utils.getApkFilePackage(getActivity(), apk);
                  appInfo.setPackageName(packageName);
                  if (Utils.isAppInstalled(getActivity(), packageName)) {
                    appInfo.setStatus(AppInfo.STATUS_INSTALLED);
                  }
                }

                if (isCurrentListViewItemVisible(position)) {
                  ListViewAdapter.ViewHolder holder = getViewHolder(position);
                  holder.tvStatus.setText(appInfo.getStatusText());
                  holder.btnDownload.setText(appInfo.getButtonText());
                }
              }

              @Override
              public void onDownloadPause() {
                appInfo.setStatus(AppInfo.STATUS_PAUSE);
                if (isCurrentListViewItemVisible(position)) {
                  ListViewAdapter.ViewHolder holder = getViewHolder(position);
                  holder.tvStatus.setText(appInfo.getStatusText());
                  holder.btnDownload.setText(appInfo.getButtonText());
                }
              }

              @Override
              public void onDownloadCancel() {
                appInfo.setStatus(AppInfo.STATUS_NOT_DOWNLOAD);
                appInfo.setDownloadPerSize("");
                if (isCurrentListViewItemVisible(position)) {
                  ListViewAdapter.ViewHolder holder = getViewHolder(position);
                  holder.tvStatus.setText(appInfo.getStatusText());
                  holder.tvDownloadPerSize.setText("");
                  holder.btnDownload.setText(appInfo.getButtonText());
                }
              }

              @Override
              public void onFailure(DownloadException e) {
                appInfo.setStatus(AppInfo.STATUS_DOWNLOAD_ERROR);
                appInfo.setDownloadPerSize("");
                if (isCurrentListViewItemVisible(position)) {
                  ListViewAdapter.ViewHolder holder = getViewHolder(position);
                  holder.tvStatus.setText(appInfo.getStatusText());
                  holder.tvDownloadPerSize.setText("");
                  holder.btnDownload.setText(appInfo.getButtonText());
                }
                e.printStackTrace();
              }
            });
  }