it.tidalwave.bluebill.mobile.android.news.AndroidNewsController 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: AndroidNewsController.java,v a9640977ac37 2010/08/07 19:02:37 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.news;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.bluebill.mobile.android.util.Analytics;
import it.tidalwave.bluebill.mobile.network.DefaultNetworkingPreferences;
import it.tidalwave.bluebill.mobile.news.DefaultNewsController;
import it.tidalwave.netbeans.util.Locator;
import java.io.IOException;
import java.net.URL;
import javax.annotation.Nonnull;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
@ServiceProvider(service=AndroidNewsController.class, supersedes={"it.tidalwave.bluebill.mobile.news.DefaultNewsController"})
public class AndroidNewsController extends DefaultNewsController
{
public static final int NEWS_NOTIFICATION_ID = 999;
@Nonnull
private final SharedPreferences sharedPreferences = Locator.find(SharedPreferences.class);
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private final OnSharedPreferenceChangeListener preferencesListener = new OnSharedPreferenceChangeListener()
{
public void onSharedPreferenceChanged (final @Nonnull SharedPreferences sharedPreferences,
final @Nonnull String preferenceName)
{
if (DefaultNetworkingPreferences.PREF_NETWORK_CONNECTION_ALLOWED.equals(preferenceName))
{
start();
}
}
};
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public AndroidNewsController()
throws IOException
{
sharedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Override
protected void postUnreadNewsNotification()
{
final String title = NbBundle.getMessage(DefaultNewsController.class, "unreadNewsTitle");
final String message = NbBundle.getMessage(DefaultNewsController.class, "unreadNewsMessage");
final Context context = Locator.find(Context.class);
final NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.bluebill_notification, title, System.currentTimeMillis());
final Intent notificationIntent = new Intent(context, NewsActivity.class);
final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, contentIntent);
notificationManager.notify(NEWS_NOTIFICATION_ID, notification);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull @Override
protected URL createUrl (final String string)
throws IOException
{
return Analytics.createURL(string);
}
}