JuickMessageMenu.java :  » Client » juick-android » com » juick » Android Open Source

Android Open Source » Client » juick android 
juick android » com » juick » JuickMessageMenu.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.juick;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;
import com.juick.api.JuickMessage;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.regex.Matcher;

/**
 *
 * @author ugnich
 */
public class JuickMessageMenu implements OnItemLongClickListener, OnClickListener {

    Activity activity;
    JuickMessage listSelectedItem;
    ArrayList<String> urls;
    int menuLength;

    public JuickMessageMenu(Activity activity) {
        this.activity = activity;
    }

    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        listSelectedItem = (JuickMessage) parent.getAdapter().getItem(position);

        urls = new ArrayList<String>();
        if (listSelectedItem.Photo != null) {
            urls.add(listSelectedItem.Photo);
        }
        if (listSelectedItem.Video != null) {
            urls.add(listSelectedItem.Video);
        }

        int pos = 0;
        Matcher m = JuickMessagesAdapter.urlPattern.matcher(listSelectedItem.Text);
        while (m.find(pos)) {
            urls.add(listSelectedItem.Text.substring(m.start(), m.end()));
            pos = m.end();
        }

        menuLength = 3 + urls.size();
        if (listSelectedItem.RID == 0) {
            menuLength++;
        }
        CharSequence[] items = new CharSequence[menuLength];
        int i = 0;
        if (urls.size() > 0) {
            for (String url : urls) {
                items[i++] = url;
            }
        }
        if (listSelectedItem.RID == 0) {
            items[i++] = activity.getResources().getString(R.string.Recommend_message);
        }
        String UName = listSelectedItem.User.UName;
        items[i++] = '@' + UName + " " + activity.getResources().getString(R.string.blog);
        items[i++] = activity.getResources().getString(R.string.Subscribe_to) + " @" + UName;
        items[i++] = activity.getResources().getString(R.string.Blacklist) + " @" + UName;

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setItems(items, this);
        builder.create().show();
        return true;
    }

    public void onClick(DialogInterface dialog, int which) {
        if (urls != null) {
            if (which < urls.size()) {
                activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urls.get(which))));
                return;
            }
            which -= urls.size();
        }
        if (listSelectedItem.RID != 0) {
            which += 1;
        }
        switch (which) {
            case 0:
                postMessage("! #" + listSelectedItem.MID, activity.getResources().getString(R.string.Recommended));
                break;
            case 1:
                Intent i = new Intent(activity, MessagesActivity.class);
                i.putExtra("uid", listSelectedItem.User.UID);
                i.putExtra("uname", listSelectedItem.User.UName);
                activity.startActivity(i);
                break;
            case 2:
                postMessage("S @" + listSelectedItem.User.UName, activity.getResources().getString(R.string.Subscribed));
                break;
            case 3:
                postMessage("BL @" + listSelectedItem.User.UName, activity.getResources().getString(R.string.Added_to_BL));
                break;
        }
    }

    private void postMessage(final String body, final String ok) {
        Thread thr = new Thread(new Runnable() {

            public void run() {
                try {
                    final String ret = Utils.postJSON(activity, "http://api.juick.com/post", "body=" + URLEncoder.encode(body, "utf-8"));
                    activity.runOnUiThread(new Runnable() {

                        public void run() {
                            Toast.makeText(activity, (ret != null) ? ok : activity.getResources().getString(R.string.Error), Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (Exception e) {
                    Log.e("postMessage", e.toString());
                }
            }
        });
        thr.start();
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.