@Override
  public void onStateChanged(DownloadEntry entry) {
    // TODO optimize
    adapter.notifyDataSetChanged();

    if (entry.state == DownloadService.State.finished) {
      String fontName = getFontNameFromDownloadKey(entry.key);
      if (fontName == null) { // this download doesn't belong to font manager.
        return;
      }

      try {
        String downloadedZip = getFontDownloadDestination(fontName);
        File fontDir = FontManager.getFontDir(fontName);
        fontDir.mkdirs();

        Log.d(TAG, "Going to unzip " + downloadedZip); // $NON-NLS-1$

        ZipInputStream zis =
            new ZipInputStream(new BufferedInputStream(new FileInputStream(downloadedZip)));
        try {
          ZipEntry ze;
          while ((ze = zis.getNextEntry()) != null) {
            String zname = ze.getName();
            Log.d(TAG, "Extracting from zip: " + zname); // $NON-NLS-1$
            File extractFile = new File(fontDir, zname);
            FileOutputStream fos = new FileOutputStream(extractFile);
            try {
              byte[] buf = new byte[4096];
              int count;
              while ((count = zis.read(buf)) != -1) {
                fos.write(buf, 0, count);
              }
            } finally {
              fos.close();
            }
          }
        } finally {
          zis.close();
        }

        new File(downloadedZip).delete();
      } catch (Exception e) {
        new AlertDialog.Builder(FontManagerActivity.this)
            .setMessage(
                getString(
                    R.string.fm_error_when_extracting_font,
                    fontName,
                    e.getClass().getSimpleName() + ' ' + e.getMessage()))
            .setPositiveButton(R.string.ok, null)
            .show();
      }
    }
  }
          @Override
          public void onClick(View v) {
            FontItem item = (FontItem) v.getTag(R.id.TAG_fontItem);

            String dlkey = getFontDownloadKey(item.name);
            dls.removeEntry(dlkey);

            if (dls.getEntry(dlkey) == null) {
              new File(FontManager.getFontsPath()).mkdirs();
              dls.startDownload(
                  dlkey,
                  String.format(URL_fontData, item.name),
                  getFontDownloadDestination(item.name));
            }

            notifyDataSetChanged();
          }
 String getFontDownloadDestination(String name) {
   return new File(FontManager.getFontsPath(), "download-" + name + ".zip")
       .getAbsolutePath(); //$NON-NLS-1$ //$NON-NLS-2$
 }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View res =
          convertView != null
              ? convertView
              : getLayoutInflater().inflate(R.layout.item_font_download, null);

      UrlImageView imgPreview = V.get(res, R.id.imgPreview);
      TextView lFontName = V.get(res, R.id.lFontName);
      View bDownload = V.get(res, R.id.bDownload);
      View bDelete = V.get(res, R.id.bDelete);
      ProgressBar progressbar = V.get(res, R.id.progressbar);
      TextView lErrorMsg = V.get(res, R.id.lErrorMsg);

      FontItem item = getItem(position);
      String dlkey = getFontDownloadKey(item.name);

      lFontName.setText(item.name);
      lFontName.setVisibility(View.VISIBLE);
      imgPreview.setTag(R.id.TAG_fontName, lFontName);
      imgPreview.setOnStateChangeListener(imgPreview_stateChange);
      imgPreview.setUrl(String.format(URL_fontPreview, item.name));
      bDownload.setTag(R.id.TAG_fontItem, item);
      bDownload.setOnClickListener(bDownload_click);
      bDelete.setTag(R.id.TAG_fontItem, item);
      bDelete.setOnClickListener(bDelete_click);

      if (FontManager.isInstalled(item.name)) {
        progressbar.setIndeterminate(false);
        progressbar.setMax(100);
        progressbar.setProgress(100);
        bDownload.setVisibility(View.GONE);
        bDelete.setVisibility(View.VISIBLE);
        lErrorMsg.setVisibility(View.GONE);
      } else {
        DownloadEntry entry = dls.getEntry(dlkey);
        if (entry == null) {
          progressbar.setIndeterminate(false);
          progressbar.setMax(100);
          progressbar.setProgress(0);
          bDownload.setVisibility(View.VISIBLE);
          bDownload.setEnabled(true);
          bDelete.setVisibility(View.GONE);
          lErrorMsg.setVisibility(View.GONE);
        } else {
          if (entry.state == DownloadService.State.created) {
            progressbar.setIndeterminate(true);
            bDownload.setVisibility(View.VISIBLE);
            bDownload.setEnabled(false);
            bDelete.setVisibility(View.GONE);
            lErrorMsg.setVisibility(View.GONE);
          } else if (entry.state == DownloadService.State.downloading) {
            if (entry.length == -1) {
              progressbar.setIndeterminate(true);
            } else {
              progressbar.setIndeterminate(false);
              progressbar.setMax((int) entry.length);
              progressbar.setProgress((int) entry.progress);
            }
            bDownload.setVisibility(View.VISIBLE);
            bDownload.setEnabled(false);
            bDelete.setVisibility(View.GONE);
            lErrorMsg.setVisibility(View.GONE);
          } else if (entry.state == DownloadService.State.finished) {
            progressbar.setIndeterminate(false);
            progressbar.setMax(100); // consider full
            progressbar.setProgress(100); // consider full
            bDownload.setVisibility(View.GONE);
            bDelete.setVisibility(View.VISIBLE);
            lErrorMsg.setVisibility(View.GONE);
          } else if (entry.state == DownloadService.State.failed) {
            progressbar.setIndeterminate(false);
            progressbar.setMax(100);
            progressbar.setProgress(0);
            bDownload.setVisibility(View.VISIBLE);
            bDownload.setEnabled(true);
            bDelete.setVisibility(View.GONE);
            lErrorMsg.setVisibility(View.VISIBLE);
            lErrorMsg.setText(entry.errorMsg);
          }
        }
      }

      return res;
    }
Exemplo n.º 5
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_song_view);

    setTitle(R.string.sn_songs_activity_title);

    song_container = V.get(this, R.id.song_container);
    no_song_data_container = V.get(this, R.id.no_song_data_container);
    bChangeBook = V.get(this, R.id.bChangeBook);
    bChangeCode = V.get(this, R.id.bChangeCode);
    bSearch = V.get(this, R.id.bSearch);
    bDownload = V.get(this, R.id.bDownload);

    qaChangeBook = SongBookUtil.getSongBookQuickAction(this, false);
    qaChangeBook.setOnActionItemClickListener(
        SongBookUtil.getOnActionItemConverter(songBookSelected));

    codeKeypad = new SongCodePopup(this);

    bChangeBook.setOnClickListener(bChangeBook_click);
    bChangeCode.setOnClickListener(bChangeCode_click);
    bSearch.setOnClickListener(bSearch_click);
    bDownload.setOnClickListener(bDownload_click);

    // for colors of bg, text, etc
    V.get(this, android.R.id.content).setBackgroundColor(S.applied.backgroundColor);

    templateCustomVars = new Bundle();
    templateCustomVars.putString(
        "background_color",
        String.format("#%06x", S.applied.backgroundColor & 0xffffff)); // $NON-NLS-1$ //$NON-NLS-2$
    templateCustomVars.putString(
        "text_color",
        String.format("#%06x", S.applied.fontColor & 0xffffff)); // $NON-NLS-1$ //$NON-NLS-2$
    templateCustomVars.putString(
        "verse_number_color",
        String.format("#%06x", S.applied.verseNumberColor & 0xffffff)); // $NON-NLS-1$ //$NON-NLS-2$
    templateCustomVars.putString(
        "text_size",
        S.applied.fontSize2dp
            + "px"); // somehow this is automatically scaled to dp. //$NON-NLS-1$ //$NON-NLS-2$
    templateCustomVars.putString(
        "line_spacing_mult", String.valueOf(S.applied.lineSpacingMult)); // $NON-NLS-1$

    {
      String fontName = Preferences.getString(getString(R.string.pref_jenisHuruf_key), null);
      if (FontManager.isCustomFont(fontName)) {
        templateCustomVars.putString(
            "custom_font_loader",
            String.format(
                "@font-face{ font-family: '%s'; src: url('%s'); }",
                fontName, FontManager.getCustomFontUri(fontName))); // $NON-NLS-1$ //$NON-NLS-2$
      } else {
        templateCustomVars.putString("custom_font_loader", ""); // $NON-NLS-1$ //$NON-NLS-2$
      }
      templateCustomVars.putString("text_font", fontName); // $NON-NLS-1$
    }

    { // show latest viewed song
      String bookName =
          Preferences.getString(Prefkey.song_last_bookName, null); // let KJ become the default.
      String code = Preferences.getString(Prefkey.song_last_code, null);

      if (bookName == null || code == null) {
        displaySong(null, null);
      } else {
        displaySong(bookName, S.getSongDb().getSong(bookName, code));
      }
    }
  }