Android Open Source - links Bookmark Card






From Project

Back to project page links.

License

The source code is released under:

MIT License

If you think the Android project links listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.iiitb.se.links.home.cards;
//from w ww  . j a  v a2 s .c om
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.internal.ViewToClickToExpand;
import it.gmariotti.cardslib.library.internal.base.BaseCard;
import it.gmariotti.cardslib.library.view.CardView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.iiitb.se.links.R;
import org.iiitb.se.links.home.cards.expand.BookmarkCardExpand;
import org.iiitb.se.links.home.fragments.EditBookmarkFragment;
import org.iiitb.se.links.home.fragments.adapter.ShareGroupsAdapter;
import org.iiitb.se.links.utils.DomainExtractor;
import org.iiitb.se.links.utils.StringConstants;
import org.iiitb.se.links.utils.network.bookmarks.BookmarkDeleter;
import org.iiitb.se.links.utils.network.bookmarks.BookmarkSharer;
import org.iiitb.se.links.utils.network.groups.subscribed.SubscribedGroupsLoader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;

public class BookmarkCard extends Card {

  protected TextView mDomain;
  protected TextView mUpdatedTime;
  protected TextView mMore;
  protected TextView mGroupInfo;
  protected LinearLayout expandLayout;
  private JSONObject bookmark;
  private Context context;

  private String id = null;
  private String url = null;
  private String title = null;
  private String description = null;
  private String groupname = null;
  private String username = null;
  private boolean myBookmark = false;
  private String formattedDate = null;

  private CardView cardView = null;
  private CardHeader header = null;
  private BookmarkCardExpand bookmarkCardExpand = null;

  private MenuItem shareWithApps = null;
  private MenuItem delete = null;
  private MenuItem shareWithGroups = null;
  private MenuItem edit = null;

  private static final String TAG = "BookmarkCard";

  private ListView mListView;
  private ShareGroupsAdapter groupsAdapter;
  private List<JSONObject> groups = new ArrayList<JSONObject>();

  private BookmarkDeleter bookmarkDeleter;
  private BookmarkSharer bookmarkSharer;
  private SubscribedGroupsLoader subscribedGroupsLoader;

  public JSONObject getBookmark() {
    return bookmark;
  }

  public void setBookmark(JSONObject bookmark) {
    this.bookmark = bookmark;
    initData();
    setData();
  }

  public String getBookmarkId() {
    return id;
  }

  public ShareGroupsAdapter getGroupsAdapter() {
    return groupsAdapter;
  }

  /**
   * Constructor with a custom inner layout
   * 
   * @param context
   */
  public BookmarkCard(Context context, JSONObject bookmark) {
    super(context, R.layout.bookmark_card);
    this.bookmark = bookmark;
    this.context = context;
    bookmarkDeleter = new BookmarkDeleter(context, this);
    bookmarkSharer = new BookmarkSharer(context, this);
    init();
  }

  protected void shareBookmarkWithGroups() {
    LayoutInflater li = LayoutInflater.from(context);
    View promptsView = li.inflate(R.layout.fragment_groups, null);

    mListView = (ListView) promptsView.findViewById(R.id.groups_card_listview);
    groupsAdapter = new ShareGroupsAdapter(context, groups);
    subscribedGroupsLoader = new SubscribedGroupsLoader(context, groupsAdapter,
        groups);

    mListView.setAdapter(groupsAdapter);
    subscribedGroupsLoader.authorizeOrLoadGroups();

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

    // set prompts.xml to alertdialog builder
    alertDialogBuilder.setView(promptsView);

    // set dialog message
    alertDialogBuilder
        .setTitle(context.getString(R.string.share_with_groups))
        .setCancelable(false)
        .setPositiveButton(context.getString(android.R.string.ok),
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                bookmarkSharer.shareBookmarkWithGroupsIamPartOf();
              }
            })
        .setNegativeButton(context.getString(android.R.string.cancel),
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
              }
            });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();

  } 

  private void shareBookmarkWithApps() {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, url);
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
    context.startActivity(Intent.createChooser(intent,
        context.getString(R.string.share_with_apps)));
  }

  /**
   * Init
   */
  private void init() {
    header = new CardHeader(context);
    // Add a popup menu. This method set OverFlow button to visible
    header.setButtonOverflowVisible(true);
    header
        .setPopupMenuListener(new CardHeader.OnClickCardHeaderPopupMenuListener() {
          @Override
          public void onMenuItemClick(BaseCard card, MenuItem item) {
            initData();
            if (item == shareWithGroups) {
              shareBookmarkWithGroups();
            } else if (item == edit) {
              editBookmark();
            } else if (item == delete) {
              bookmarkDeleter.deleteBookmark();
            } else if (item == shareWithApps) {
              shareBookmarkWithApps();
            }
          }
        });
    // Add a PopupMenuPrepareListener to add dynamically a menu entry it is
    // optional.
    header
        .setPopupMenuPrepareListener(new CardHeader.OnPrepareCardHeaderPopupMenuListener() {
          @Override
          public boolean onPreparePopupMenu(BaseCard card, PopupMenu popupMenu) {
            shareWithGroups = popupMenu.getMenu().add(
                context.getString(R.string.share));
            if (myBookmark) {
              edit = popupMenu.getMenu().add(context.getString(R.string.edit));
              delete = popupMenu.getMenu().add(
                  context.getString(R.string.delete));
            }
            shareWithApps = popupMenu.getMenu().add(
                context.getString(R.string.share_with_apps));
            return true;
          }
        });
    addCardHeader(header);

    bookmarkCardExpand = new BookmarkCardExpand(context, bookmark);
    addCardExpand(bookmarkCardExpand);

    this.setOnClickListener(new OnCardClickListener() {
      @Override
      public void onClick(Card card, View view) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        context.startActivity(browserIntent);
      }
    });
  }

  protected void editBookmark() {
    Fragment fragment = new EditBookmarkFragment();
    Bundle args = new Bundle();
    args.putString(StringConstants.BOOKMARK_ID, id);
    args.putString(StringConstants.URL, url);
    args.putString(StringConstants.TITLE, title);
    args.putString(StringConstants.DESCRIPTION, description);

    StringBuilder tags = new StringBuilder("");
    try {
      JSONArray tagsArr = bookmark.getJSONArray(StringConstants.TAGS);
      String comma = "";
      for (int index = 0; index < tagsArr.length(); index += 1) {
        tags.append(comma).append(tagsArr.get(index));
        comma = ", ";
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
    args.putString(StringConstants.TAGS, tags.toString());

    fragment.setArguments(args);

    FragmentManager fragmentManager = ((Activity) context)
        .getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment)
        .commit();

  }

  private void initData() {
    try {
      id = bookmark.getString(StringConstants.ID);
      url = bookmark.getString(StringConstants.URL);
      title = bookmark.getString(StringConstants.TITLE);
      description = bookmark.getString(StringConstants.DESCRIPTION);
      groupname = bookmark.optString(StringConstants.LINK_GROUP_NAME, null);
      username = bookmark.optString(StringConstants.USER_NAME, null);
      myBookmark = bookmark.getBoolean(StringConstants.MY_BOOKMARK);

      long epoch = bookmark.getLong(StringConstants.UPDATED_AT);

      Date date = new Date(epoch * 1000);
      java.text.DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",
          Locale.US);
      formattedDate = format.format(date);
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  private void setData() {
    // Set the header title
    init();
    header.setTitle(title);
    bookmarkCardExpand.setBookmark(bookmark);
    mDomain.setText(DomainExtractor.getBaseDomain(url));
    mUpdatedTime.setText(formattedDate);

    if (null != groupname) {
      mGroupInfo.setText(context.getString(R.string.bookmark_sharer, username,
          groupname));
    } else {
      mGroupInfo.setText("");
    }

    setExpanded(false);
  }

  @Override
  public void setupInnerViewElements(ViewGroup parent, View view) {
    // Retrieve elements
    mDomain = (TextView) parent.findViewById(R.id.bookmark_domain);
    mUpdatedTime = (TextView) parent.findViewById(R.id.bookmark_updated_time);
    mMore = (TextView) parent.findViewById(R.id.bookmark_more);
    mGroupInfo = (TextView) parent.findViewById(R.id.bookmark_shared_group);
    expandLayout = (LinearLayout) parent.findViewById(R.id.bookmark_expand);
    initData();
    setData();
    ViewToClickToExpand viewToClickToExpand = ViewToClickToExpand.builder()
        .setupView(expandLayout);
    setViewToClickToExpand(viewToClickToExpand);
  }
}




Java Source Code List

org.iiitb.se.links.GroupActivity.java
org.iiitb.se.links.MainActivity.java
org.iiitb.se.links.custom.ExpandableHeightGridView.java
org.iiitb.se.links.group.fragments.AddBookmarkInGroupFragment.java
org.iiitb.se.links.group.fragments.BookmarkSearchInGroupsFragment.java
org.iiitb.se.links.group.fragments.GroupLinkFragment.java
org.iiitb.se.links.home.ResourceLoader.java
org.iiitb.se.links.home.cards.AbstractGroupCard.java
org.iiitb.se.links.home.cards.BookmarkCard.java
org.iiitb.se.links.home.cards.RequestsGroupCard.java
org.iiitb.se.links.home.cards.ShareGroupCard.java
org.iiitb.se.links.home.cards.SubscribedGroupCard.java
org.iiitb.se.links.home.cards.expand.BookmarkCardExpand.java
org.iiitb.se.links.home.cards.expand.adapter.TagViewAdapter.java
org.iiitb.se.links.home.fragments.AddBookmarkFragment.java
org.iiitb.se.links.home.fragments.BookmarkSearchFragment.java
org.iiitb.se.links.home.fragments.EditBookmarkFragment.java
org.iiitb.se.links.home.fragments.LinkFragment.java
org.iiitb.se.links.home.fragments.RequestsGroupFragment.java
org.iiitb.se.links.home.fragments.SubscribedGroupFragment.java
org.iiitb.se.links.home.fragments.adapter.AbstractGroupsAdapter.java
org.iiitb.se.links.home.fragments.adapter.BookmarksAdapter.java
org.iiitb.se.links.home.fragments.adapter.RequestsGroupsAdapter.java
org.iiitb.se.links.home.fragments.adapter.ShareGroupsAdapter.java
org.iiitb.se.links.home.fragments.adapter.SubscribedGroupsAdapter.java
org.iiitb.se.links.utils.AuthorizationClient.java
org.iiitb.se.links.utils.BookmarkLoadType.java
org.iiitb.se.links.utils.BookmarkOperations.java
org.iiitb.se.links.utils.DomainExtractor.java
org.iiitb.se.links.utils.FragmentTypes.java
org.iiitb.se.links.utils.StringConstants.java
org.iiitb.se.links.utils.URLConstants.java
org.iiitb.se.links.utils.network.AbstractResourceDownloader.java
org.iiitb.se.links.utils.network.MyProperties.java
org.iiitb.se.links.utils.network.WebpageLoader.java
org.iiitb.se.links.utils.network.bookmarks.BookmarkAdder.java
org.iiitb.se.links.utils.network.bookmarks.BookmarkDeleter.java
org.iiitb.se.links.utils.network.bookmarks.BookmarkEditor.java
org.iiitb.se.links.utils.network.bookmarks.BookmarkGroupAdder.java
org.iiitb.se.links.utils.network.bookmarks.BookmarkSharer.java
org.iiitb.se.links.utils.network.bookmarks.Logout.java
org.iiitb.se.links.utils.network.bookmarks.TimelineLoader.java
org.iiitb.se.links.utils.network.bookmarks.search.BookmarkInGroupsSearchLoader.java
org.iiitb.se.links.utils.network.bookmarks.search.BookmarkSearchLoader.java
org.iiitb.se.links.utils.network.groups.GroupTimelineLoader.java
org.iiitb.se.links.utils.network.groups.requests.GroupRequestsDecider.java
org.iiitb.se.links.utils.network.groups.requests.RequestsGroupsLoader.java
org.iiitb.se.links.utils.network.groups.subscribed.GroupUnsubscriber.java
org.iiitb.se.links.utils.network.groups.subscribed.SubscribedGroupsLoader.java