Android Open Source - dandy Drupal Node Activity






From Project

Back to project page dandy.

License

The source code is released under:

GNU General Public License

If you think the Android project dandy 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 com.workhabit.drupal.publisher;
//from   ww w. j  ava2s  .c om
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.*;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.workhabit.drupal.publisher.support.DrupalDialogHandler;
import org.workhabit.drupal.api.entity.drupal7.DrupalNode;
import org.workhabit.drupal.api.entity.drupal7.DrupalComment;
import org.workhabit.drupal.api.entity.drupal7.DrupalField;
import org.workhabit.drupal.api.site.Drupal7SiteContext;
import org.workhabit.drupal.api.site.DrupalSiteContext;
import org.workhabit.drupal.api.site.exceptions.DrupalFetchException;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright 2009 - WorkHabit, Inc. - acs
 * Date: Sep 25, 2010, 7:39:37 PM
 */
public class DrupalNodeActivity extends AbstractDandyActivity
{
    private boolean initialized = false;
    private DrupalNode lastNode;

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.nodeview, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item)
    {
        switch (item.getItemId()) {
            case R.id.refresh:
                onCreate(null);
                break;
        }
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Drupal7SiteContext drupalSiteContext = DandyApplication.getDrupalSiteContext(savedInstanceState);
        int nid = getIntent().getExtras().getInt("nid");

        setContentView(R.layout.node);
        try {
            DrupalNode node = drupalSiteContext.getNode(nid);
            lastNode = node;
            TextView titleView = (TextView)findViewById(R.id.nodeTitle);
            TextView bodyView = (TextView)findViewById(R.id.nodeBody);
            fetchAndDisplayImage(drupalSiteContext, node, titleView);
            titleView.setText(node.getTitle());
            // TODO: Support i18n incoming from Drupal
            String nodeContent = String.format("<p>%s</p>", node.getBody().get("und").get(0).getValue().replaceAll("\r\n", "\n").replaceAll("\n\n", "</p><p>"));
            bodyView.setText(Html.fromHtml(nodeContent));

            if (node.getComment() != 0) {
                // only show if comments are enabled for this node.
                //
                fetchAndDisplayComments(drupalSiteContext, node);
            }


        } catch (DrupalFetchException e) {
            DrupalDialogHandler.showMessageDialog(this, e.getMessage());
        } catch (IOException e) {
            DrupalDialogHandler.showMessageDialog(this, e.getMessage());
        }
    }

    private void fetchAndDisplayImage(final Drupal7SiteContext drupalSiteContext, final DrupalNode node, final TextView titleView) throws IOException
    {
        Thread t = new Thread()
        {
            @Override
            public void run()
            {
                if (node.getFields() != null) {
                    Map<String, DrupalField> fields = node.getFields();
                    for (Map.Entry<String, DrupalField> entry : fields.entrySet()) {
                        if ("field_title_image".equals(entry.getKey())) {
                            try {
                                WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
                                int displayWidth = wm.getDefaultDisplay().getWidth();

                                HashMap<String, String> imagedata = entry.getValue().getValues().get(0);
                                String filepath = "/sites/default/files/imagecache/w" + displayWidth + "/" + imagedata.get("filepath");

                                MessageDigest digest = MessageDigest.getInstance("MD5");
                                digest.update(filepath.getBytes());
                                byte messageDigest[] = digest.digest();
                                StringBuilder hash = new StringBuilder();
                                for (byte aMessageDigest : messageDigest) {
                                    hash.append(Integer.toHexString(0xFF & aMessageDigest));
                                }
                                File f = new File(getCacheDir() + File.separator + hash.toString());

                                InputStream fileStream;
                                if (f.exists()) {
                                    fileStream = new FileInputStream(f);
                                }
                                else {
                                    fileStream = drupalSiteContext.getFileStream(filepath);
                                    byte[] buf = new byte[1024];
                                    // persist the file locally
                                    boolean newFile = f.createNewFile();
                                    if (newFile) {
                                        FileOutputStream outStream = new FileOutputStream(f);

                                        int len;
                                        while ((len = fileStream.read(buf)) > 0) {
                                            outStream.write(buf, 0, len);
                                        }
                                        fileStream.close();
                                        outStream.close();
                                        fileStream = new FileInputStream(f);
                                    }
                                }

                                Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(fileStream));
                                if (bitmap != null) {
                                    // getStream ratio of width/height for drawable
                                    float ratio = displayWidth / bitmap.getWidth();
                                    float newHeight = bitmap.getHeight() * ratio;
                                    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, displayWidth, (int)newHeight, false);
                                    BitmapDrawable bitmapDrawable = new BitmapDrawable(resizedBitmap);
                                    titleView.setBackgroundDrawable(bitmapDrawable);
                                    titleView.setHeight((int)newHeight);
                                    titleView.invalidate();
                                }
                            } catch (IOException e) {
                                // do nothing
                            } catch (NoSuchAlgorithmException e) {
                                // don't do anything
                            }

                        }
                    }
                }
            }
        };
        t.start();
    }

    private void fetchAndDisplayComments(Drupal7SiteContext drupalSiteContext, DrupalNode node) throws DrupalFetchException
    {
        ListView lv = (ListView)findViewById(R.id.commentList);
        List<DrupalComment> comments = drupalSiteContext.getComments(node.getNid());
        ArrayAdapter<DrupalComment> commentArrayAdapter = new ArrayAdapter<DrupalComment>(getApplicationContext(), R.layout.commentrow, comments)
        {

            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                DrupalComment drupalComment = this.getItem(position);
                View v = convertView;
                if (v == null) {
                    v = getLayoutInflater().inflate(R.layout.commentrow, (ViewGroup)v);
                }
                if (drupalComment != null) {
                    TextView commentSubjectView = (TextView)v.findViewById(R.id.commentSubject);
                    String subject = drupalComment.getSubject();
                    if (commentSubjectView != null && subject != null && !"".equals(subject)) {
                        commentSubjectView.setText(subject);
                    }
                    TextView commentBodyView = (TextView)v.findViewById(R.id.commentBody);
                    String commentBody = drupalComment.getComment();
                    if (commentBodyView != null && commentBody != null && !"".equals(drupalComment.getComment())) {
                        commentBodyView.setText(commentBody);
                    }
                }
                return v;
            }
        };
        lv.setAdapter(commentArrayAdapter);
        commentArrayAdapter.notifyDataSetChanged();
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void onNewCommentButtonClick(View v)
    {
        Intent i = new Intent(getApplicationContext(), NewCommentActivity.class);
        i.putExtra("nid", lastNode.getNid());
        startActivity(i);
    }

    static class FlushedInputStream extends FilterInputStream
    {
        public FlushedInputStream(InputStream inputStream)
        {
            super(inputStream);
        }

        @Override
        public long skip(long n) throws IOException
        {
            long totalBytesSkipped = 0L;
            while (totalBytesSkipped < n) {
                long bytesSkipped = in.skip(n - totalBytesSkipped);
                if (bytesSkipped == 0L) {
                    int ibyte = read();
                    if (ibyte < 0) {
                        break;  // we reached EOF
                    }
                    else {
                        bytesSkipped = 1; // we read one byte
                    }
                }
                totalBytesSkipped += bytesSkipped;
            }
            return totalBytesSkipped;
        }
    }

}




Java Source Code List

com.workhabit.drupal.publisher.AbstractDandyActivity.java
com.workhabit.drupal.publisher.AbstractDandyListActivity.java
com.workhabit.drupal.publisher.AbstractDrupalNodeListActivity.java
com.workhabit.drupal.publisher.DandyApplication.java
com.workhabit.drupal.publisher.DrupalHeadlineNodeListActivity.java
com.workhabit.drupal.publisher.DrupalLoginActivity.java
com.workhabit.drupal.publisher.DrupalNodeActivity.java
com.workhabit.drupal.publisher.DrupalNodeListViewActivity.java
com.workhabit.drupal.publisher.DrupalTaxonomyListActivity.java
com.workhabit.drupal.publisher.NewCommentActivity.java
com.workhabit.drupal.publisher.support.BottomNavClickHandler.java
com.workhabit.drupal.publisher.support.DrupalDialogHandler.java
com.workhabit.drupal.publisher.support.DrupalNodeArrayAdapter.java
com.workhabit.drupal.publisher.support.DrupalTaxonomyAdapter.java
org.workhabit.drupal.api.annotations.IdFieldName.java
org.workhabit.drupal.api.entity.drupal7.DrupalBody.java
org.workhabit.drupal.api.entity.drupal7.DrupalComment.java
org.workhabit.drupal.api.entity.drupal7.DrupalEntity.java
org.workhabit.drupal.api.entity.drupal7.DrupalField.java
org.workhabit.drupal.api.entity.drupal7.DrupalFile.java
org.workhabit.drupal.api.entity.drupal7.DrupalNode.java
org.workhabit.drupal.api.entity.drupal7.DrupalTaxonomyTerm.java
org.workhabit.drupal.api.entity.drupal7.DrupalUser.java
org.workhabit.drupal.api.entity.drupal7.ReadItLater.java
org.workhabit.drupal.api.json.BooleanAdapter.java
org.workhabit.drupal.api.json.DrupalFieldAdapter.java
org.workhabit.drupal.api.json.DrupalJsonObjectSerializerFactory.java
org.workhabit.drupal.api.json.DrupalJsonObjectSerializer.java
org.workhabit.drupal.api.json.UnixTimeDateAdapter.java
org.workhabit.drupal.api.site.CalendarFactory.java
org.workhabit.drupal.api.site.Drupal7SiteContext.java
org.workhabit.drupal.api.site.DrupalSiteContext.java
org.workhabit.drupal.api.site.NonceFactory.java
org.workhabit.drupal.api.site.exceptions.DrupalFetchException.java
org.workhabit.drupal.api.site.exceptions.DrupalLoginException.java
org.workhabit.drupal.api.site.exceptions.DrupalLogoutException.java
org.workhabit.drupal.api.site.exceptions.DrupalSaveException.java
org.workhabit.drupal.api.site.exceptions.DrupalServicesResponseException.java
org.workhabit.drupal.api.site.impl.DrupalSiteContextInstanceStateImpl.java
org.workhabit.drupal.api.site.impl.DrupalSiteContextInstanceState.java
org.workhabit.drupal.api.site.impl.v3.Drupal6SiteContextImpl.java
org.workhabit.drupal.api.site.impl.v3.Drupal7SiteContextImpl.java
org.workhabit.drupal.api.site.impl.v3.DrupalSiteContextBridge.java
org.workhabit.drupal.api.site.support.AndroidDrupalServicesRequestManagerImpl.java
org.workhabit.drupal.api.site.support.GenericCookie.java
org.workhabit.drupal.api.site.support.HttpUrlConnectionFactoryImpl.java
org.workhabit.drupal.api.site.support.HttpUrlConnectionFactory.java
org.workhabit.drupal.api.site.support.NonceFactoryImpl.java
org.workhabit.drupal.http.DrupalServicesRequestManager.java
org.workhabit.drupal.http.ServicesResponse.java