com.deliciousdroid.fragment.AddBookmarkFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.deliciousdroid.fragment.AddBookmarkFragment.java

Source

/*
 * DeliciousDroid - http://code.google.com/p/DeliciousDroid/
 *
 * Copyright (C) 2010 Matt Schmidt
 *
 * DeliciousDroid is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * DeliciousDroid is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DeliciousDroid; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

package com.deliciousdroid.fragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import org.apache.http.auth.AuthenticationException;

import com.deliciousdroid.R;
import com.deliciousdroid.action.GetWebpageTitleTask;
import com.deliciousdroid.activity.FragmentBaseActivity;
import com.deliciousdroid.client.DeliciousApi;
import com.deliciousdroid.client.TooManyRequestsException;
import com.deliciousdroid.platform.BookmarkManager;
import com.deliciousdroid.platform.TagManager;
import com.deliciousdroid.providers.BookmarkContent.Bookmark;
import com.deliciousdroid.providers.TagContent.Tag;
import com.deliciousdroid.ui.TagSpan;
import com.deliciousdroid.util.SpaceTokenizer;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class AddBookmarkFragment extends Fragment {

    private FragmentBaseActivity base;

    private EditText mEditUrl;
    private EditText mEditDescription;
    private ProgressBar mDescriptionProgress;
    private EditText mEditNotes;
    private MultiAutoCompleteTextView mEditTags;
    private TextView mRecommendedTags;
    private ProgressBar mRecommendedProgress;
    private TextView mPopularTags;
    private ProgressBar mPopularProgress;
    private CompoundButton mPrivate;
    private Bookmark bookmark;
    private Bookmark oldBookmark;
    private Boolean update = false;

    private long updateTime = 0;

    private AsyncTask<String, Integer, String> titleTask;
    private AsyncTask<String, Integer, ArrayList<Tag>> tagTask;

    private OnBookmarkSaveListener bookmarkSaveListener;

    public interface OnBookmarkSaveListener {
        public void onBookmarkSave(Bookmark b);

        public void onBookmarkCancel(Bookmark b);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        base = (FragmentBaseActivity) getActivity();

        setHasOptionsMenu(true);

        mEditUrl = (EditText) getView().findViewById(R.id.add_edit_url);
        mEditDescription = (EditText) getView().findViewById(R.id.add_edit_description);
        mDescriptionProgress = (ProgressBar) getView().findViewById(R.id.add_description_progress);
        mEditNotes = (EditText) getView().findViewById(R.id.add_edit_notes);
        mEditTags = (MultiAutoCompleteTextView) getView().findViewById(R.id.add_edit_tags);
        mRecommendedTags = (TextView) getView().findViewById(R.id.add_recommended_tags);
        mRecommendedProgress = (ProgressBar) getView().findViewById(R.id.add_recommended_tags_progress);
        mPopularTags = (TextView) getView().findViewById(R.id.add_popular_tags);
        mPopularProgress = (ProgressBar) getView().findViewById(R.id.add_popular_tags_progress);
        mPrivate = (CompoundButton) getView().findViewById(R.id.add_edit_private);

        mRecommendedTags.setMovementMethod(LinkMovementMethod.getInstance());
        mPopularTags.setMovementMethod(LinkMovementMethod.getInstance());

        if (base.mAccount != null) {
            String[] tagArray = new String[5];
            tagArray = TagManager.GetTagsAsArray(base.mAccount.name, Tag.Name + " ASC", base).toArray(tagArray);
            ArrayAdapter<String> autoCompleteAdapter = new ArrayAdapter<String>(base, R.layout.autocomplete_view,
                    tagArray);
            mEditTags.setAdapter(autoCompleteAdapter);
            mEditTags.setTokenizer(new SpaceTokenizer());
        }

        mEditUrl.setOnFocusChangeListener(new OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    String url = mEditUrl.getText().toString();

                    if (mEditDescription.getText().toString().equals("")) {
                        titleTask = new GetTitleTask().execute(url);
                    }
                    tagTask = new GetTagSuggestionsTask().execute(url);
                }
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();

        refreshView();
    }

    public void loadBookmark(Bookmark b, Bookmark oldB) {
        if (b != null)
            bookmark = b.copy();

        if (oldB != null)
            oldBookmark = oldB.copy();
    }

    public void refreshView() {
        if (bookmark != null) {
            mEditUrl.setText(bookmark.getUrl());

            if (bookmark.getDescription() != null)
                mEditDescription.setText(bookmark.getDescription());

            if (bookmark.getNotes() != null)
                mEditNotes.setText(bookmark.getNotes());

            if (bookmark.getTagString() != null)
                mEditTags.setText(bookmark.getTagString());

            mPrivate.setChecked(!bookmark.getShared());

            if (mEditDescription.getText().toString().equals(""))
                titleTask = new GetTitleTask().execute(bookmark.getUrl());

            tagTask = new GetTagSuggestionsTask().execute(bookmark.getUrl());
        } else {
            if (!this.isHidden()) {
                mEditUrl.requestFocus();
            }
            setDefaultValues();
        }
    }

    public void saveHandler(View v) {
        save();
        bookmarkSaveListener.onBookmarkSave(bookmark);
    }

    public void cancelHandler(View v) {
        bookmarkSaveListener.onBookmarkCancel(bookmark);
    }

    private void setDefaultValues() {
        mPrivate.setChecked(base.privateDefault);
    }

    private void save() {

        String url = mEditUrl.getText().toString();
        String description = mEditDescription.getText().toString();

        if (description.equals(""))
            description = getResources().getString(R.string.add_bookmark_default_title);

        if (url.equals("")) {
            Toast.makeText(base, R.string.add_bookmark_blank_url, Toast.LENGTH_LONG).show();
            return;
        }

        if (titleTask != null)
            titleTask.cancel(true);

        if (tagTask != null)
            tagTask.cancel(true);

        if (!url.startsWith("http")) {
            url = "http://" + url;
        }

        String tagstring = "";
        String[] tags = mEditTags.getText().toString().trim().split(" ");

        for (String s : tags) {
            if (!s.equals("") && !s.equals(" "))
                tagstring += (s + " ");
        }

        int oldid = 0;
        if (bookmark != null && bookmark.getId() != 0) {
            oldid = bookmark.getId();
            update = true;
            oldBookmark = bookmark.copy();
        }

        if (!update) {
            Date d = new Date();
            updateTime = d.getTime();
        }

        bookmark = new Bookmark(url, description, mEditNotes.getText().toString(), tagstring.trim(),
                !mPrivate.isChecked(), updateTime);

        bookmark.setId(oldid);

        if (update) {
            BookmarkManager.UpdateBookmark(bookmark, base.mAccount.name, base);

            for (Tag t : oldBookmark.getTags()) {
                if (!bookmark.getTags().contains(t)) {
                    TagManager.UpleteTag(t, base.mAccount.name, base);
                }
            }
        } else {
            BookmarkManager.AddBookmark(bookmark, base.mAccount.name, base);
        }

        for (Tag t : bookmark.getTags()) {
            TagManager.UpsertTag(t, base.mAccount.name, base);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        if (base.isMyself()) {
            inflater.inflate(R.menu.add_bookmark_menu, menu);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.menu_addbookmark_save:
            save();
            bookmarkSaveListener.onBookmarkSave(bookmark);
            return true;
        case R.id.menu_addbookmark_cancel:
            bookmarkSaveListener.onBookmarkCancel(bookmark);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    TagSpan.OnTagClickListener tagOnClickListener = new TagSpan.OnTagClickListener() {
        public void onTagClick(String tag) {
            String currentTagString = mEditTags.getText().toString();

            ArrayList<String> currentTags = new ArrayList<String>();
            Collections.addAll(currentTags, currentTagString.split(" "));

            if (tag != null && tag != "") {
                if (!currentTags.contains(tag)) {
                    currentTags.add(tag);
                } else {
                    currentTags.remove(tag);
                }
                mEditTags.setText(TextUtils.join(" ", currentTags.toArray()).trim());
                mEditTags.setSelection(mEditTags.getText().length());
            }
        }
    };

    public class GetTitleTask extends GetWebpageTitleTask {
        protected void onPreExecute() {
            mDescriptionProgress.setVisibility(View.VISIBLE);
        }

        protected void onPostExecute(String result) {
            mEditDescription.setText(Html.fromHtml(result));
            mDescriptionProgress.setVisibility(View.GONE);
        }
    }

    public class GetTagSuggestionsTask extends AsyncTask<String, Integer, ArrayList<Tag>> {
        private String url;

        @Override
        protected ArrayList<Tag> doInBackground(String... args) {
            url = args[0];

            try {
                return DeliciousApi.getSuggestedTags(url, base.mAccount, base);
            } catch (AuthenticationException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TooManyRequestsException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPreExecute() {
            mRecommendedTags.setVisibility(View.GONE);
            mPopularTags.setVisibility(View.GONE);
            mRecommendedProgress.setVisibility(View.VISIBLE);
            mPopularProgress.setVisibility(View.VISIBLE);
        }

        protected void onPostExecute(ArrayList<Tag> result) {

            if (result != null) {
                SpannableStringBuilder recommendedBuilder = new SpannableStringBuilder();
                SpannableStringBuilder popularBuilder = new SpannableStringBuilder();

                for (Tag t : result) {
                    if (t.getType().equals("recommended")) {
                        addTag(recommendedBuilder, t);
                    } else if (t.getType().equals("popular")) {
                        addTag(popularBuilder, t);
                    }
                }

                mRecommendedTags.setText(recommendedBuilder);
                mPopularTags.setText(popularBuilder);

                mRecommendedTags.setVisibility(View.VISIBLE);
                mPopularTags.setVisibility(View.VISIBLE);
                mRecommendedProgress.setVisibility(View.GONE);
                mPopularProgress.setVisibility(View.GONE);
            }
        }

        private void addTag(SpannableStringBuilder builder, Tag t) {
            int flags = 0;

            if (builder.length() != 0) {
                builder.append("  ");
            }

            int start = builder.length();
            builder.append(t.getTagName());
            int end = builder.length();

            TagSpan span = new TagSpan(t.getTagName());
            span.setOnTagClickListener(tagOnClickListener);

            builder.setSpan(span, start, end, flags);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.add_bookmark_fragment, container, false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            bookmarkSaveListener = (OnBookmarkSaveListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnBookmarkSaveListener");
        }
    }
}