package com.geel9.facepunch.activities;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import com.geel9.facepunch.APISession.AvatarCallback;
import com.geel9.facepunch.APISession.Category;
import com.geel9.facepunch.APISession.FPPost;
import com.geel9.facepunch.APISession.GetSinglePMCallback;
import com.geel9.facepunch.APISession.QuoteCallback;
import com.geel9.facepunch.APISession.RateCallback;
import com.geel9.facepunch.APISession.Rating;
import com.geel9.facepunch.APISession.ReplyPMCallback;
import com.geel9.facepunch.R.anim;
import com.geel9.facepunch.R.id;
import com.geel9.facepunch.R.layout;
import com.geel9.facepunch.APISession;
import com.geel9.facepunch.R;
import com.geel9.facepunch.misc.FPPostSection;
import com.geel9.facepunch.misc.PostSplitter;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class PMActivity extends FPActivity {
private class State
{
FPPost PM;
String title;
}
public FPPost thePM;
public String title;
public int id;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.pmview);
final State data = (State)getLastCustomNonConfigurationInstance();
if(data != null){
thePM = data.PM;
title = data.title;
DoPM(thePM);
return;
}
Intent intent = getIntent();
title = intent.getStringExtra("title");
id = intent.getIntExtra("id", 0);
ActionText(title);
api.GetPM(this, id, new GetSinglePMCallback(){
public void onResult(boolean success, FPPost PM) {
if(!success) return;
thePM = PM;
DoPM(thePM);
}
});
}
public void DoPM(FPPost PM){
LinearLayout root = (LinearLayout)findViewById(R.id.pmLayout);
final FPPost p = PM;
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final String postHTML = p.getMessageHTML();
final LinearLayout postItem = (LinearLayout)inflater.inflate(R.layout.listpost, root, false);
((TextView)postItem.findViewById(R.id.textViewPostUsername2)).setText(Html.fromHtml(p.getAuthor().getStyledName()));
TextView userTitle = (TextView)postItem.findViewById(R.id.textViewUserTitle);
userTitle.setText(Html.fromHtml(p.getAuthor().userTitle));
if(p.getAuthor().userTitle.equals("")) userTitle.setVisibility(View.GONE);
((TextView)postItem.findViewById(R.id.textViewPostDate)).setText("sent " + p.getDate());
((TextView)postItem.findViewById(R.id.textViewPostPostcount)).setText(String.valueOf(p.getAuthor().getPostCount() + " posts"));
final EditText replyText = (EditText)findViewById(R.id.editTextPMReply);
final Button replyButton = (Button)findViewById(R.id.buttonReplyPM);
final TextView replyHeader = (TextView)findViewById(R.id.headerPMReply);
final LinearLayout postContent = (LinearLayout)postItem.findViewById(R.id.layoutPostContent);
api.getAvatar(p.getAuthor().getId(), PMActivity.this, showAvatars(), new APISession.AvatarCallback() {
public void onResult(boolean success, Bitmap avatar) {
if(success && avatar.getWidth() != -1 && avatar.getHeight() != -1){
if(cacheAvatars())
api.cacheAvatar(avatar, p.getAuthor().getId(), PMActivity.this);
((ImageView) postItem.findViewById(R.id.imageViewUserAvatar)).setImageBitmap(avatar);
}
}
});
ArrayList<FPPostSection> sections = p.sections;
try {
if(p.sections.size() == 0)
sections = PostSplitter.splitPost(postHTML);
createPostViews(sections, postContent, inflater, false);
root.addView(postItem);
String quoteHTML = p.getBBCode();
final String recipients = p.getAuthor().getName() + ";";
final int PMID = id;
final String PMTitle = "Re: " + title.replace("Re: ", "");
replyText.setVisibility(View.VISIBLE);
replyText.setText(quoteHTML);
replyButton.setVisibility(View.VISIBLE);
replyHeader.setVisibility(View.VISIBLE);
replyButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
replyHeader.setText("Sending...");
String message = replyText.getText().toString();
api.ReplyPM(PMID, URLEncoder.encode(PMTitle), URLEncoder.encode(recipients), message, new ReplyPMCallback(){
public void onResult(boolean success) {
replyText.setVisibility(View.GONE);
replyButton.setVisibility(View.GONE);
replyHeader.setVisibility(View.GONE);
}
});
}
});
} catch (Exception e){
Toast.makeText(PMActivity.this, "Post error: " + p.getMessageHTML() + "\nError: " + e.toString(), Toast.LENGTH_LONG).show();
}
}
private void createPostViews(final ArrayList<FPPostSection> sections, final LinearLayout root, final LayoutInflater inflater, final boolean isLink){
runOnUiThread(new Runnable(){
public void run() {
for(int i = 0; i < sections.size(); i++){
final FPPostSection section = sections.get(i);
switch(section.postSection){
case LINK:
final LinearLayout customViewLink = (LinearLayout)inflater.inflate(R.layout.customviewlink, root, false);
try{
String linkHTML = section.getLinkHTML();
if(!linkHTML.contains("<img")){
linkHTML = linkHTML;
}
ArrayList<FPPostSection> linkSections = PostSplitter.splitPost(linkHTML);
createPostViews(linkSections, customViewLink, inflater, true);
customViewLink.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
try{
String url = section.getLinkUrl();
Uri data2 = Uri.parse(url);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}catch(ActivityNotFoundException e){
}
}
});
root.addView(customViewLink);
}catch(Exception e){
}
break;
case IMAGE:
final LinearLayout customViewImage = (LinearLayout)inflater.inflate(R.layout.customviewimage, root, false);
try{
final ImageView im = (ImageView)customViewImage.findViewById(R.id.imageViewCustom);
final String url = section.getImgUrl();
if(showImages()){
im.setAdjustViewBounds(true);
im.setImageResource( R.anim.loadingspinner );
final AnimationDrawable loadingAnimation = (AnimationDrawable)im.getDrawable();
im.post( new Runnable()
{
public void run()
{
loadingAnimation.start();
}
} );
api.getImage(url, PMActivity.this, new AvatarCallback(){
public void onResult(boolean success, Bitmap avatar) {
if(success){
try{
im.setImageBitmap(avatar);
}catch(Exception e){im.setVisibility(View.GONE);}
}
else
im.setVisibility(View.GONE);
}
});
if(showImages())
root.addView(customViewImage);
}
}catch(Exception e){
}
break;
case TEXT:
final LinearLayout customView = (LinearLayout)inflater.inflate(R.layout.customviewtext, root, false);
try{
TextView t = (TextView)customView.findViewById(R.id.textViewCustom);
String text = section.getHTML();
if(isLink && !text.startsWith("<a")){
text = "<a href='http://www.walkthedinosaur.net'>" + text + "</a>";
}
t.setText(Html.fromHtml(text));
t.setMovementMethod(LinkMovementMethod.getInstance());
root.addView(customView);
}catch(Exception e){
}
break;
case QUOTE:
final LinearLayout customViewQuote = (LinearLayout)inflater.inflate(R.layout.customviewquote, root, false);
try{
String content = section.getQuoteContent();
String author = section.getQuoteAuthor();
TextView quoteAuthor = (TextView)customViewQuote.findViewById(R.id.textViewQuoteAuthor);
quoteAuthor.setText(Html.fromHtml("Posted by " + author + ":"));
if(author.equals("")){
quoteAuthor.setVisibility(View.GONE);
}
ArrayList<FPPostSection> quoteSections = PostSplitter.splitPost(content);
createPostViews(quoteSections, customViewQuote, inflater, false);
root.addView(customViewQuote);
}catch(Exception e){
}
break;
case IFRAME:
final LinearLayout customViewIframe = (LinearLayout)inflater.inflate(R.layout.customviewiframe, root, false);
try{
final Button showIframeButton = (Button)customViewIframe.findViewById(R.id.buttonShowIframe);
final WebView iFrame = (WebView)customViewIframe.findViewById(R.id.webViewIframe);
showIframeButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
showIframeButton.setVisibility(View.GONE);
iFrame.setVisibility(View.VISIBLE);
String url = section.iFrameLink;
iFrame.loadUrl(url);
}
});
}catch(Exception e){
Toast.makeText(PMActivity.this, "Exception in iframe: " + e.toString(), Toast.LENGTH_LONG).show();
}
root.addView(customViewIframe);
break;
case MEDIA:
final LinearLayout customViewMedia = (LinearLayout)inflater.inflate(R.layout.customviewiframe, root, false);
try{
final Button showMediaButton = (Button)customViewMedia.findViewById(R.id.buttonShowIframe);
final WebView iFrame = (WebView)customViewMedia.findViewById(R.id.webViewIframe);
customViewMedia.removeView(iFrame);
showMediaButton.setText("Go to " + section.mediaName);
showMediaButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
String url = section.mediaURL;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}catch(Exception e){
Toast.makeText(PMActivity.this, "Exception in media: " + e.toString(), Toast.LENGTH_LONG).show();
}
root.addView(customViewMedia);
break;
case RELEASE:
final LinearLayout customViewRelease = (LinearLayout)inflater.inflate(R.layout.customviewrelease, root, false);
try{
String content = section.getHTML();
ArrayList<FPPostSection> releaseSections = PostSplitter.splitPost(content);
createPostViews(releaseSections, customViewRelease, inflater, false);
root.addView(customViewRelease);
}catch(Exception e){
}
}
}
}
});
}
}