Android Open Source - friendica-for-android Message Content Adapter






From Project

Back to project page friendica-for-android.

License

The source code is released under:

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

If you think the Android project friendica-for-android 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 de.wikilab.android.friendica01.adapter;
// w ww.java2  s  .c  om
import java.io.File;
import java.sql.Date;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import de.wikilab.android.friendica01.Max;
import de.wikilab.android.friendica01.R;
import de.wikilab.android.friendica01.TwAjax;
import de.wikilab.android.friendica01.R.drawable;
import de.wikilab.android.friendica01.R.id;
import de.wikilab.android.friendica01.R.layout;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.format.DateUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MessageContentAdapter extends ArrayAdapter<JSONObject> {
  private static final String TAG="Friendica/MessageContentAdapter";
  
  protected static class ViewHolder {
    int Type;
    ImageView profileImage;
    TextView subject, userName, htmlContent, dateTime;
  }
  
  
  public MessageContentAdapter(Context context, List<JSONObject> objects) {
    super(context, R.layout.msg_contentitem, objects);
    
  }
  
  @Override
  public boolean hasStableIds() {
    return true;
  }
  @Override
  public long getItemId(int position) {
    try {
      return ((JSONObject) getItem(position)).getLong("id");
    } catch (JSONException e) {
      Log.e(TAG, "Item without ID!");
      return 0;
    }
  }
  
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder H;
    if (convertView == null) {
      LayoutInflater inf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      H = new ViewHolder();
      convertView = inf.inflate(R.layout.msg_contentitem, null);
      //H.subject = (TextView) convertView.findViewById(R.id.subject);
      H.userName = (TextView) convertView.findViewById(R.id.userName);
      H.htmlContent = (TextView) convertView.findViewById(R.id.htmlContent);
      H.profileImage = (ImageView) convertView.findViewById(R.id.profileImage);
    
    
      H.htmlContent.setFocusable(true);
      H.htmlContent.setMovementMethod(LinkMovementMethod.getInstance());

      H.dateTime = (TextView) convertView.findViewById(R.id.date);
      
      
      convertView.setTag(H);
    } else {
      H = (ViewHolder) convertView.getTag();
    }
    
    JSONObject post = (JSONObject) getItem(position);
    
    if (H.profileImage != null) {
      H.profileImage.setImageResource(R.drawable.ic_launcher);
      try {
        final String piurl = post.getString("sender_profile_img");
        Log.i(TAG, "TRY Download profile img: " + piurl);
        final TwAjax pidl = new TwAjax(getContext(), true, false);
        pidl.ignoreSSLCerts = true;
        
        //NEW: download cached
        final File pifile = new File(Max.IMG_CACHE_DIR + "/pi_" + Max.cleanFilename(piurl));
        if (pifile.isFile()) {
          Log.i(TAG, "OK  Load cached profile Img: " + piurl);
          //profileImage.setImageURI(Uri.parse("file://" + pifile.getAbsolutePath()));
          H.profileImage.setImageDrawable(new BitmapDrawable(pifile.getAbsolutePath()));
        } else {
          pidl.urlDownloadToFile(piurl, pifile.getAbsolutePath(), new Runnable() {
            @Override
            public void run() {
              Log.i(TAG, "OK  Download profile Img: " + piurl);
              H.profileImage.setImageDrawable(new BitmapDrawable(pifile.getAbsolutePath()));
            }
          });
        }
      } catch (JSONException e) {
      }
    }
    
    if (H.userName != null) {
      try {
        String appendix = "";
        H.userName.setText(post.getString("sender_screen_name") + " --> " + post.getString("recipient_screen_name") );
      } catch (Exception e) {
        H.userName.setText("Invalid Dataset!");
      }
    }
    
    if (H.dateTime != null) {
      try {
        H.dateTime.setText(DateUtils.getRelativeDateTimeString(parent.getContext(), 
            java.util.Date.parse(post.getString("created_at")),
            DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, DateUtils.FORMAT_24HOUR));
        
      } catch (Exception e) {
        H.dateTime.setText("Invalid Dataset!");
      }
    }
    
    try {
      
      //Max.setHtmlWithImages(H.htmlContent, post.getString("statusnet_html"));
      String filtered_html = post.getString("text");
      if (filtered_html.length()>500) filtered_html=filtered_html.substring(0,500)+"<br><br><b>click to read more...</b>";
      filtered_html = filtered_html.replace("\n", "<br>");
      
      Spanned spanned = Html.fromHtml(filtered_html);
      H.htmlContent.setText(spanned);
      //H.subject.setText(post.getString("title"));
      
    } catch (JSONException e) {
      H.htmlContent.setText("Invalid Dataset!");
    }
    
    return convertView;
  }
  
  
}




Java Source Code List

com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.java
com.handmark.pulltorefresh.library.PullToRefreshBase.java
com.handmark.pulltorefresh.library.PullToRefreshExpandableListView.java
com.handmark.pulltorefresh.library.PullToRefreshGridView.java
com.handmark.pulltorefresh.library.PullToRefreshListView.java
com.handmark.pulltorefresh.library.PullToRefreshWebView.java
com.handmark.pulltorefresh.library.internal.EmptyViewMethodAccessor.java
com.handmark.pulltorefresh.library.internal.IndicatorLayout.java
com.handmark.pulltorefresh.library.internal.LoadingLayout.java
de.wikilab.android.friendica01.FileUploadService.java
de.wikilab.android.friendica01.FragmentParentListener.java
de.wikilab.android.friendica01.GCMIntentService.java
de.wikilab.android.friendica01.HtmlImageHelper.java
de.wikilab.android.friendica01.LoginListener.java
de.wikilab.android.friendica01.Max.java
de.wikilab.android.friendica01.NotificationCheckerService.java
de.wikilab.android.friendica01.Notification.java
de.wikilab.android.friendica01.TwAjax.java
de.wikilab.android.friendica01.ViewServer.java
de.wikilab.android.friendica01.activity.FriendicaImgUploadActivity.java
de.wikilab.android.friendica01.activity.GenericContentActivity.java
de.wikilab.android.friendica01.activity.HomeActivity.java
de.wikilab.android.friendica01.activity.MainScreenActivity.java
de.wikilab.android.friendica01.activity.MessageDetailActivity.java
de.wikilab.android.friendica01.activity.MessagesActivity.java
de.wikilab.android.friendica01.activity.PreferenceContainerActivity.java
de.wikilab.android.friendica01.activity.PreferencesActivity.java
de.wikilab.android.friendica01.activity.UserProfileActivity.java
de.wikilab.android.friendica01.activity.WritePostActivity.java
de.wikilab.android.friendica01.adapter.HtmlStringArrayAdapter.java
de.wikilab.android.friendica01.adapter.MessageContentAdapter.java
de.wikilab.android.friendica01.adapter.MessageListAdapter.java
de.wikilab.android.friendica01.adapter.PhotoGalleryAdapter.java
de.wikilab.android.friendica01.adapter.PostListAdapter.java
de.wikilab.android.friendica01.fragment.ContentFragment.java
de.wikilab.android.friendica01.fragment.FriendListFragment.java
de.wikilab.android.friendica01.fragment.MainMenuFragment.java
de.wikilab.android.friendica01.fragment.MessageViewFragment.java
de.wikilab.android.friendica01.fragment.MessageWriteFragment.java
de.wikilab.android.friendica01.fragment.PhotoGalleryFragment.java
de.wikilab.android.friendica01.fragment.PostDetailFragment.java
de.wikilab.android.friendica01.fragment.PostListFragment.java
de.wikilab.android.friendica01.fragment.WelcomeFragment.java
de.wikilab.android.friendica01.fragment.WritePostFragment.java