All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.mobile.android.news.NewsActivity Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * blueBill Mobile - Android - open source birding
 * Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it/mobile
 * SCM: https://kenai.com/hg/bluebill~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.news;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.ui.android.app.AndroidActivityHelper;
import it.tidalwave.mobile.rss.RSSFeed;
import it.tidalwave.mobile.ui.QuestionWithFeedback;
import it.tidalwave.bluebill.mobile.news.NewsView;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import it.tidalwave.bluebill.mobile.android.util.CommonOptionsMenuController;
import it.tidalwave.bluebill.mobile.android.R;
import static it.tidalwave.mobile.rss.RssVocabulary.*;

/***********************************************************************************************************************
 *
 * @stereotype View
 * @stereotype Activity
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class NewsActivity extends Activity implements NewsView
  {
    private static final String CLASS = NewsActivity.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    private final AndroidNewsViewController controller = new AndroidNewsViewController(this);
    
    @CheckForNull
    private ListView list;

    private ProgressDialog progressDialog;

    @CheckForNull
    private RSSFeedAdapter rssFeedAdapter;

    @CheckForNull
    private RSSFeed rssFeed;

    private final AndroidActivityHelper activityHelper = new AndroidActivityHelper(this);

    private final CommonOptionsMenuController commonOptionsMenuController = new CommonOptionsMenuController(this);

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private final OnItemClickListener messageClickListener = new OnItemClickListener()
      {
        @Override
        public void onItemClick (final @Nonnull AdapterView av,
                                 final @Nonnull View view,
                                 final @Nonnegative int index,
                                 final long id)
          {
            controller.readMessage(rssFeedAdapter.getItem(index));
          }
      };
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private final View.OnClickListener markAllReadListener = new View.OnClickListener()
      {
        public void onClick (final @Nonnull View view)
          {
            controller.markAllRead(rssFeed);
          }
      };

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    public void dismiss()
      {
        runOnUiThread(new Runnable() 
          {
            public void run() 
              {
                progressDialog.dismiss();
                finish();
              }
          });
      }
    
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    public void notifyFeedUnavailable()
      {
        runOnUiThread(new Runnable() 
          {
            public void run() 
              {
                progressDialog.dismiss();
              }
          });
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    public void notifyFeedAvailable (final @Nonnull RSSFeed rssFeed)
      {
        runOnUiThread(new Runnable()
          {
            public void run()
              {
                try
                  {
                    NewsActivity.this.rssFeed = rssFeed;
                    rssFeedAdapter = controller.getNewsFeedAdapter(rssFeed.get(MESSAGES));
                    list.setAdapter(rssFeedAdapter);
                    list.setOnItemClickListener(messageClickListener);
                    progressDialog.dismiss();
                  }
                catch (NotFoundException e)
                  {
                    logger.warning("Can't show news item: %s", e);
                    logger.throwing(CLASS, "notifyFeedAvailable()", e);
                  }
              }
          });
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    public void notifyAllMessagesMarkedAsRead (final @Nonnull String message) 
      {
        activityHelper.showLightNotification(message);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    public void confirmToDownloadNews (final @Nonnull QuestionWithFeedback questionWithFeedback) 
      {
        activityHelper.showConfirmationDialog(questionWithFeedback);
      }
    
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    protected void onCreate (final @Nonnull Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        list = (ListView)findViewById(R.id.list);        
        ((Button)findViewById(R.id.btMarkAllRead)).setOnClickListener(markAllReadListener);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    protected void onResume() 
      {
        super.onResume();

        // The user is reading the news, don't notify him any longer.
        final NotificationManager notificationManager = (NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(AndroidNewsService.NEWS_NOTIFICATION_ID);

        final String loadingNews = getResources().getString(R.string.loadingNews);
        progressDialog = ProgressDialog.show(this, "", loadingNews, true);
        progressDialog.setCancelable(true);
        progressDialog.setOnCancelListener(new OnCancelListener()
          {
            public void onCancel (final @Nonnull DialogInterface dialogInterface)
              {
                finish();
              }
          });

        // TODO: add a timeout and show an error - do in the controller
        new Thread()
          {
            @Override
            public void run()
              {
                controller.loadNewsFeed();
              }
          }.start();
      }    
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean onCreateOptionsMenu (final @Nonnull Menu menu)
      {
        getMenuInflater().inflate(R.menu.news_options_menu, menu);
        return true;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean onOptionsItemSelected (final @Nonnull MenuItem item)
      {
        return commonOptionsMenuController.onOptionsItemSelected(item);
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy