Search in sources :

Example 1 with Feed

use of com.foobnix.opds.Feed in project LibreraReader by foobnix.

the class OpdsFragment2 method prepareDataInBackground.

@Override
public List<Entry> prepareDataInBackground() {
    try {
        LOG.d("OPDS URL", url);
        if ("/".equals(url)) {
            title = getString(R.string.catalogs);
            return allCatalogs = getAllCatalogs();
        }
        if (SamlibOPDS.isSamlibUrl(url)) {
            Pair<List<Entry>, String> pair = SamlibOPDS.getSamlibResult(url);
            List<Entry> samlibResult = pair.first;
            title = pair.second.replace(SamlibOPDS.ROOT_FAVORITES, getString(R.string.favorites)).replace(SamlibOPDS.ROOT_AWARDS, getString(R.string.awards));
            return samlibResult;
        }
        Feed feed = OPDS.getFeed(url);
        if (feed == null) {
            return Collections.emptyList();
        }
        isNeedLoginPassword = feed.isNeedLoginPassword;
        LOG.d("Load: >>>", feed.title, url);
        feed.updateLinksForUI();
        updateLinks(feed.title, urlRoot, feed.links);
        for (Link link : feed.links) {
            if ("next".equals(link.rel)) {
                feed.entries.add(new Entry("Next", link));
                break;
            }
        }
        for (Entry e : feed.entries) {
            updateLinks(e.getTitle(), urlRoot, e.links);
            if (e.authorUrl != null) {
                e.authorUrl = Hrefs.fixHref(e.authorUrl, urlRoot);
            }
        }
        title = TxtUtils.nullToEmpty(feed.title).replace("\n", "").replace("\r", "").trim();
        return feed.entries;
    } catch (Exception e) {
        LOG.e(e);
        return Collections.emptyList();
    }
}
Also used : Entry(com.foobnix.opds.Entry) List(java.util.List) ArrayList(java.util.ArrayList) Link(com.foobnix.opds.Link) Feed(com.foobnix.opds.Feed)

Example 2 with Feed

use of com.foobnix.opds.Feed in project LibreraReader by foobnix.

the class AddCatalogDialog method showDialog.

public static void showDialog(final Activity a, final Runnable onRefresh, final Entry e, final boolean validate) {
    AlertDialog.Builder builder = new AlertDialog.Builder(a);
    View dialog = LayoutInflater.from(a).inflate(R.layout.dialog_add_catalog, null, false);
    final EditText url = (EditText) dialog.findViewById(R.id.url);
    url.setText("http://");
    url.setSelection(url.getText().length());
    url.setEnabled(validate);
    final EditText name = (EditText) dialog.findViewById(R.id.name);
    final EditText description = (EditText) dialog.findViewById(R.id.description);
    final ProgressBar progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar);
    TintUtil.setDrawableTint(progressBar.getIndeterminateDrawable().getCurrent(), TintUtil.color);
    final ImageView image = (ImageView) dialog.findViewById(R.id.image);
    final CheckBox addAsWEb = (CheckBox) dialog.findViewById(R.id.addAsWEb);
    addAsWEb.setVisibility(View.GONE);
    final String editAppState = e != null ? e.appState : null;
    if (editAppState != null) {
        String[] line = e.appState.replace(";", "").split(",");
        url.setText(line[0]);
        name.setText(line[1]);
        description.setText(line[2]);
        ImageLoader.getInstance().displayImage(line[3], image, IMG.displayCacheMemoryDisc);
        if (e.logo != null) {
            image.setTag(e.logo);
        }
    }
    progressBar.setVisibility(View.GONE);
    image.setVisibility(View.GONE);
    builder.setView(dialog);
    builder.setTitle(R.string.add_catalog);
    builder.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
        }
    });
    builder.setNegativeButton(R.string.close, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            Keyboards.close(a);
        }
    });
    final AlertDialog infoDialog = builder.create();
    infoDialog.setOnDismissListener(new OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            Keyboards.close(a);
        }
    });
    infoDialog.show();
    url.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            infoDialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        }
    });
    infoDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

        AsyncTask asyncTask;

        @Override
        public void onClick(View v) {
            final String feedUrl = url.getText().toString();
            if (infoDialog.getButton(AlertDialog.BUTTON_POSITIVE).getText().equals(a.getString(R.string.ok)) || addAsWEb.isChecked() || !validate) {
                Entry entry = new Entry();
                entry.setAppState(feedUrl, name.getText().toString(), description.getText().toString(), image.getTag().toString());
                if (editAppState != null) {
                    AppState.get().myOPDSLinks = AppState.get().myOPDSLinks.replace(editAppState, "");
                }
                AppState.get().myOPDSLinks = entry.appState + AppState.get().myOPDSLinks;
                onRefresh.run();
                infoDialog.dismiss();
                AppState.get().save(a);
                return;
            }
            if (AsyncTasks.isRunning(asyncTask)) {
                AsyncTasks.toastPleaseWait(a);
                return;
            }
            asyncTask = new AsyncTask() {

                @Override
                protected Object doInBackground(Object... params) {
                    return OPDS.getFeed(feedUrl);
                }

                @Override
                protected void onPreExecute() {
                    progressBar.setVisibility(View.VISIBLE);
                    image.setVisibility(View.GONE);
                }

                @Override
                protected void onPostExecute(Object result) {
                    try {
                        progressBar.setVisibility(View.GONE);
                        if (result == null || ((Feed) result).entries.isEmpty()) {
                            if (result != null && ((Feed) result).isNeedLoginPassword) {
                                AddCatalogDialog.showDialogLogin(a, new Runnable() {

                                    @Override
                                    public void run() {
                                    }
                                });
                            } else {
                                Toast.makeText(a, a.getString(R.string.incorrect_value) + " OPDS " + feedUrl, Toast.LENGTH_LONG).show();
                                infoDialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add);
                                addAsWEb.setVisibility(View.VISIBLE);
                                name.setText(feedUrl);
                                image.setTag("assets://opds/web.png");
                            }
                            return;
                        }
                        Feed feed = (Feed) result;
                        name.setText(TxtUtils.nullToEmpty(feed.title));
                        if (TxtUtils.isNotEmpty(feed.subtitle)) {
                            description.setText(TxtUtils.nullToEmpty(feed.subtitle));
                        }
                        if (feed.icon != null) {
                            image.setVisibility(View.VISIBLE);
                            feed.icon = Hrefs.fixHref(feed.icon, feedUrl);
                            image.setTag(feed.icon);
                            ImageLoader.getInstance().displayImage(feed.icon, image, IMG.displayCacheMemoryDisc);
                        } else {
                            image.setTag("assets://opds/web.png");
                        }
                        infoDialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.ok);
                    } catch (Exception e) {
                        LOG.e(e);
                    }
                }
            }.execute();
        }
    });
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) Entry(com.foobnix.opds.Entry) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) ImageView(android.widget.ImageView) ProgressBar(android.widget.ProgressBar) Feed(com.foobnix.opds.Feed) EditText(android.widget.EditText) OnDismissListener(android.content.DialogInterface.OnDismissListener) AsyncTask(android.os.AsyncTask) OnClickListener(android.view.View.OnClickListener) ImageView(android.widget.ImageView) View(android.view.View) CheckBox(android.widget.CheckBox)

Aggregations

Entry (com.foobnix.opds.Entry)2 Feed (com.foobnix.opds.Feed)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 OnDismissListener (android.content.DialogInterface.OnDismissListener)1 AsyncTask (android.os.AsyncTask)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 CheckBox (android.widget.CheckBox)1 EditText (android.widget.EditText)1 ImageView (android.widget.ImageView)1 ProgressBar (android.widget.ProgressBar)1 Link (com.foobnix.opds.Link)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1