示例#1
1
  public void importBookmarks(
      final InputStream inputStream,
      boolean overwriteExisting,
      final boolean finishActivityAfterwards) {
    new AsyncTask<Boolean, Integer, Object>() {
      ProgressDialog pd;
      int count_bookmark = 0;
      int count_label = 0;

      @Override
      protected void onPreExecute() {
        pd = new ProgressDialog(BookmarkActivity.this);
        pd.setMessage(getString(R.string.mengimpor_titiktiga));
        pd.setIndeterminate(true);
        pd.setCancelable(false);
        pd.show();
      }

      @Override
      protected Object doInBackground(Boolean... params) {
        final boolean tumpuk = params[0];

        final List<Bookmark2> bookmarks = new ArrayList<Bookmark2>();
        final TObjectIntHashMap<Bookmark2> bookmarkToRelIdMap = new TObjectIntHashMap<Bookmark2>();
        final List<Label> labels = new ArrayList<Label>();
        final TObjectIntHashMap<Label> labelToRelIdMap = new TObjectIntHashMap<Label>();
        final TIntLongHashMap labelRelIdToAbsIdMap = new TIntLongHashMap();
        final TIntObjectHashMap<TIntList> bookmark2RelIdToLabelRelIdsMap =
            new TIntObjectHashMap<TIntList>();

        try {
          InputStream fis;

          if (inputStream == null) {
            File in = getFileBackup();
            fis = new FileInputStream(in);
          } else {
            fis = inputStream;
          }

          Xml.parse(
              fis,
              Xml.Encoding.UTF_8,
              new DefaultHandler2() {
                @Override
                public void startElement(
                    String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                  if (localName.equals(Bookmark2.XMLTAG_Bukmak2)) {
                    Bookmark2 bookmark = Bookmark2.fromAttributes(attributes);
                    int bookmark2_relId = Bookmark2.getRelId(attributes);
                    bookmarks.add(bookmark);
                    bookmarkToRelIdMap.put(bookmark, bookmark2_relId);
                    count_bookmark++;
                  } else if (localName.equals(Label.XMLTAG_Label)) {
                    Label label = Label.dariAttributes(attributes);
                    int label_relId = Label.getRelId(attributes);
                    labels.add(label);
                    labelToRelIdMap.put(label, label_relId);
                    count_label++;
                  } else if (localName.equals(Bookmark2_Label.XMLTAG_Bookmark2_Label)) {
                    int bookmark2_relId =
                        Integer.parseInt(
                            attributes.getValue(
                                "", Bookmark2_Label.XMLATTR_bookmark2_relId)); // $NON-NLS-1$
                    int label_relId =
                        Integer.parseInt(
                            attributes.getValue(
                                "", Bookmark2_Label.XMLATTR_label_relId)); // $NON-NLS-1$

                    TIntList labelRelIds = bookmark2RelIdToLabelRelIdsMap.get(bookmark2_relId);
                    if (labelRelIds == null) {
                      labelRelIds = new TIntArrayList();
                      bookmark2RelIdToLabelRelIdsMap.put(bookmark2_relId, labelRelIds);
                    }
                    labelRelIds.add(label_relId);
                  }
                }
              });
          fis.close();
        } catch (Exception e) {
          return e;
        }

        { // bikin label-label yang diperlukan, juga map relId dengan id dari label.
          HashMap<String, Label> judulMap = new HashMap<String, Label>();
          List<Label> xlabelLama = S.getDb().getAllLabels();

          for (Label labelLama : xlabelLama) {
            judulMap.put(labelLama.title, labelLama);
          }

          for (Label label : labels) {
            // cari apakah label yang judulnya persis sama udah ada
            Label labelLama = judulMap.get(label.title);
            if (labelLama != null) {
              // update warna label lama
              if (tumpuk && label.backgroundColor != null && label.backgroundColor.length() > 0) {
                labelLama.backgroundColor = label.backgroundColor;
                S.getDb().updateLabel(labelLama);
              }
              labelRelIdToAbsIdMap.put(labelToRelIdMap.get(label), labelLama._id);
              Log.d(
                  TAG,
                  "label (lama) r->a : "
                      + labelToRelIdMap.get(label)
                      + "->"
                      + labelLama._id); // $NON-NLS-1$ //$NON-NLS-2$
            } else { // belum ada, harus bikin baru
              Label labelBaru = S.getDb().tambahLabel(label.title, label.backgroundColor);
              labelRelIdToAbsIdMap.put(labelToRelIdMap.get(label), labelBaru._id);
              Log.d(
                  TAG,
                  "label (baru) r->a : "
                      + labelToRelIdMap.get(label)
                      + "->"
                      + labelBaru._id); // $NON-NLS-1$ //$NON-NLS-2$
            }
          }
        }

        S.getDb()
            .importBookmarks(
                bookmarks,
                tumpuk,
                bookmarkToRelIdMap,
                labelRelIdToAbsIdMap,
                bookmark2RelIdToLabelRelIdsMap);

        return null;
      }

      @Override
      protected void onPostExecute(Object result) {
        pd.dismiss();

        AlertDialog dialog;
        if (result instanceof Exception) {
          dialog =
              msgbox(
                  getString(
                      R.string.terjadi_kesalahan_ketika_mengimpor_pesan,
                      ((Exception) result).getMessage()));
        } else {
          dialog =
              msgbox(
                  getString(R.string.impor_berhasil_angka_diproses, count_bookmark, count_label));
        }

        if (finishActivityAfterwards) {
          dialog.setOnDismissListener(finishActivityListener);
        }

        adapter.reload();
      }
    }.execute((Boolean) overwriteExisting);
  }