it.tidalwave.bluebill.mobile.android.news.NewsActivity Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Mobile - open source birdwatching
* ==========================================
*
* Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
* http://bluebill.tidalwave.it/mobile/
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* $Id: NewsActivity.java,v d808b6cb5d50 2010/07/10 21:19:33 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.news;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.inject.Provider;
import java.io.IOException;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.mobile.ui.CommonUITasks;
import it.tidalwave.mobile.android.ui.AndroidCommonUITasks;
import it.tidalwave.mobile.rss.Message;
import it.tidalwave.mobile.rss.RSSFeed;
import it.tidalwave.bluebill.mobile.GeneralPreferences;
import it.tidalwave.bluebill.mobile.android.util.CommonOptionsMenuController;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.bluebill.mobile.news.NewsController;
import it.tidalwave.bluebill.mobile.news.NewsUI;
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.content.Intent;
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 static it.tidalwave.mobile.rss.Vocabulary.*;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class NewsActivity extends Activity implements NewsUI
{
private static final String CLASS = NewsActivity.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
@CheckForNull
private ListView list;
private ProgressDialog dialog;
private RSSFeedAdapter rssFeedAdapter;
@CheckForNull
private RSSFeed rssFeed;
private final CommonUITasks commonUITasks = new AndroidCommonUITasks(this);
private final CommonOptionsMenuController commonOptionsMenuController = new CommonOptionsMenuController(this);
@Nonnull
private final Provider preferences = Locator.createProviderFor(GeneralPreferences.class);
@Nonnull
private final Provider newsController = Locator.createProviderFor(NewsController.class);
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private final OnItemClickListener onItemClickListener = new OnItemClickListener()
{
@Override
public void onItemClick (final @Nonnull AdapterView> av,
final @Nonnull View view,
final @Nonnegative int index,
final long id)
{
try
{
final Message message = rssFeedAdapter.getItem(index);
final String date = preferences.get().formatDateAndTime(message.get(PUB_DATE));
final Intent intent = new Intent(getBaseContext(), NewsItemActivity.class);
intent.putExtra("date", date);
intent.putExtra("title", message.get(TITLE));
intent.putExtra("content", message.get(CONTENT));
newsController.get().markRead(message);
rssFeedAdapter.notifyDataSetInvalidated();
startActivity(intent);
}
catch (Exception e)
{
logger.warning("Can't show news item: %s", e);
logger.throwing(CLASS, "onItemClick()", e);
}
}
};
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public CommonUITasks getCommonUITasks()
{
return commonUITasks;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Override
protected void onCreate (final @Nonnull Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.news_activity);
list = (ListView)findViewById(R.id.list);
final NotificationManager notificationManager = (NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(AndroidNewsController.NEWS_NOTIFICATION_ID);
final String loadingNews = getResources().getString(R.string.loadingNews);
dialog = ProgressDialog.show(this, "", loadingNews, true);
dialog.setCancelable(true);
dialog.setOnCancelListener(new OnCancelListener()
{
public void onCancel (final @Nonnull DialogInterface dialogInterface)
{
finish();
}
});
// TODO: add a timeout
((Button)findViewById(R.id.btMarkAllRead)).setOnClickListener(new View.OnClickListener()
{
public void onClick (final @Nonnull View view)
{
if (rssFeed != null) // defensive
{
try
{
newsController.get().markAllRead(rssFeed, NewsActivity.this);
rssFeedAdapter.notifyDataSetInvalidated();
}
catch (IOException e)
{
logger.warning("Can't mark all read: %s", e);
logger.throwing(CLASS, "onClick()", e);
}
}
}
});
new Thread()
{
@Override
public void run()
{
newsController.get().loadNewsFeed(NewsActivity.this);
}
}.start();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void notifyFeedAvailable (final @Nonnull RSSFeed rssFeed)
{
this.rssFeed = rssFeed;
runOnUiThread(new Runnable()
{
public void run()
{
try
{
rssFeedAdapter = new RSSFeedAdapter(getBaseContext(), rssFeed.get(MESSAGES));
list.setAdapter(rssFeedAdapter);
list.setOnItemClickListener(onItemClickListener);
dialog.dismiss();
}
catch (NotFoundException e)
{
logger.warning("Can't show news item: %s", e);
logger.throwing(CLASS, "notifyFeedAvailable()", e);
}
}
});
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void notifyFeedUnavailable (final @Nonnull Exception e)
{
closeProgressDialog();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void close()
{
closeProgressDialog();
finish();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private void closeProgressDialog()
{
runOnUiThread(new Runnable()
{
public void run()
{
dialog.dismiss();
}
});
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@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);
}
}