Android Open Source - sthlmtraveling Deviation Service






From Project

Back to project page sthlmtraveling.

License

The source code is released under:

Apache License

If you think the Android project sthlmtraveling 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.markupartist.sthlmtraveling.service;
//w  ww . ja v  a2s .  co m
import android.app.AlarmManager;
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.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;

import com.markupartist.sthlmtraveling.DeviationsActivity;
import com.markupartist.sthlmtraveling.R;
import com.markupartist.sthlmtraveling.provider.deviation.Deviation;
import com.markupartist.sthlmtraveling.provider.deviation.DeviationNotificationDbAdapter;
import com.markupartist.sthlmtraveling.provider.deviation.DeviationStore;
import com.markupartist.sthlmtraveling.receivers.OnAlarmReceiver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Pattern;

public class DeviationService extends WakefulIntentService {
    private static String TAG = "DeviationService";
    private static String LINE_PATTERN = "[A-Za-z ]?([\\d]+)[ A-Z]?";
    private static Pattern sLinePattern = Pattern.compile(LINE_PATTERN);
    private NotificationManager mNotificationManager;
    //private Intent mInvokeIntent;
    private DeviationNotificationDbAdapter mDb;

    public DeviationService() {
        super("DeviationService");
    }

    @Override
    protected void doWakefulWork(Intent intent) {
        mNotificationManager =
            (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        //mInvokeIntent = new Intent(this, DeviationsActivity.class);

        mDb = new DeviationNotificationDbAdapter(getApplicationContext());
        mDb.open();

        SharedPreferences sharedPreferences =
            PreferenceManager.getDefaultSharedPreferences(this);
        String filterString = sharedPreferences.getString("notification_deviations_lines_csv", "");
        ArrayList<Integer> triggerFor = DeviationStore.extractLineNumbers(filterString, null);

        DeviationStore deviationStore = new DeviationStore();

        try {
            ArrayList<Deviation> deviations = deviationStore.getDeviations(this);
            deviations = DeviationStore.filterByLineNumbers(deviations, triggerFor);

            for (Deviation deviation : deviations) {
                if (!mDb.containsReference(deviation.getReference(),
                        deviation.getMessageVersion())) {
                    mDb.create(deviation.getReference(), deviation.getMessageVersion());
                    Log.d(TAG, "Notification triggered for " + deviation.getReference());
                    showNotification(deviation);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        mDb.close();
    }

    /**
     * Show a notification while this service is running.
     */
    private void showNotification(Deviation deviation) {
        // Set the icon, scrolling text and timestamp
        Notification notification =
            new Notification(android.R.drawable.stat_notify_error, deviation.getDetails(),
                System.currentTimeMillis());

        notification.flags = Notification.FLAG_AUTO_CANCEL;

        // The PendingIntent to launch our activity if the user selects this notification
        Intent i = new Intent(this, DeviationsActivity.class);
        i.setAction(DeviationsActivity.DEVIATION_FILTER_ACTION);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.deviations_label),
                getText(R.string.new_deviations), contentIntent);

        // Send the notification.
        mNotificationManager.notify(R.string.deviations_label, notification);
    }

    public static void startAsRepeating(Context context) {
        SharedPreferences sharedPreferences =
            PreferenceManager.getDefaultSharedPreferences(context);

        boolean enabled =
            sharedPreferences.getBoolean("notification_deviations_enabled", false);
        Log.d(TAG, "notification_deviations_enabled: " + enabled);

        AlarmManager mgr =
            (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

        Intent i = new Intent(context, OnAlarmReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

        if (enabled) {
            Log.d(TAG, "Starting DeviationService in the background");
            // Default one hour.
            long updateInterval = Long.parseLong(
                    sharedPreferences.getString("notification_deviations_update_interval", "3600000"));
            mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    SystemClock.elapsedRealtime() + 60000,
                    updateInterval,
                    pi);        
        } else {
            mgr.cancel(pi);
        }
    }

    public static void startService(Context context) {
        SharedPreferences sharedPreferences =
            PreferenceManager.getDefaultSharedPreferences(context);

        boolean enabled =
            sharedPreferences.getBoolean("notification_deviations_enabled", false);

        if (enabled) {
            WakefulIntentService.acquireStaticLock(context);
            context.startService(new Intent(context, DeviationService.class));
        } else {
            Log.d(TAG, "Stopping DeviationService");
            AlarmManager mgr =
                (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            Intent i = new Intent(context, OnAlarmReceiver.class);
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
            mgr.cancel(pi);
        }
    }
}




Java Source Code List

com.markupartist.sthlmtraveling.AboutActivity.java
com.markupartist.sthlmtraveling.AllTests.java
com.markupartist.sthlmtraveling.AppConfig.java
com.markupartist.sthlmtraveling.AutoCompleteStopAdapter.java
com.markupartist.sthlmtraveling.BaseActivity.java
com.markupartist.sthlmtraveling.BaseFragmentActivity.java
com.markupartist.sthlmtraveling.BaseFragment.java
com.markupartist.sthlmtraveling.BaseListActivity.java
com.markupartist.sthlmtraveling.BaseListFragmentActivity.java
com.markupartist.sthlmtraveling.BaseListFragment.java
com.markupartist.sthlmtraveling.BasePreferenceActivity.java
com.markupartist.sthlmtraveling.ChangeRouteTimeActivity.java
com.markupartist.sthlmtraveling.DepartureAdapter.java
com.markupartist.sthlmtraveling.DeparturesActivity.java
com.markupartist.sthlmtraveling.DeviationDetailActivity.java
com.markupartist.sthlmtraveling.DeviationsActivity.java
com.markupartist.sthlmtraveling.DialogHelper.java
com.markupartist.sthlmtraveling.FavoritesFragment.java
com.markupartist.sthlmtraveling.MultipleListAdapter.java
com.markupartist.sthlmtraveling.MyApplication.java
com.markupartist.sthlmtraveling.MyLocationManager.java
com.markupartist.sthlmtraveling.NearbyActivity.java
com.markupartist.sthlmtraveling.PlannerFragmentActivity.java
com.markupartist.sthlmtraveling.PlannerFragment.java
com.markupartist.sthlmtraveling.PointOnMapActivity.java
com.markupartist.sthlmtraveling.RouteDetailActivity.java
com.markupartist.sthlmtraveling.RouteParserTest.java
com.markupartist.sthlmtraveling.RoutesActivity.java
com.markupartist.sthlmtraveling.SearchDeparturesFragmentActivity.java
com.markupartist.sthlmtraveling.SearchDeparturesFragment.java
com.markupartist.sthlmtraveling.SectionedAdapter.java
com.markupartist.sthlmtraveling.SettingsActivity.java
com.markupartist.sthlmtraveling.StartActivity.java
com.markupartist.sthlmtraveling.TrafficStatusFragment.java
com.markupartist.sthlmtraveling.ViewOnMapActivity.java
com.markupartist.sthlmtraveling.provider.FavoritesDbAdapter.java
com.markupartist.sthlmtraveling.provider.HistoryDbAdapter.java
com.markupartist.sthlmtraveling.provider.JourneysProvider.java
com.markupartist.sthlmtraveling.provider.PlacesProvider.java
com.markupartist.sthlmtraveling.provider.TransportMode.java
com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.java
com.markupartist.sthlmtraveling.provider.deviation.DeviationNotificationDbAdapter.java
com.markupartist.sthlmtraveling.provider.deviation.DeviationStore.java
com.markupartist.sthlmtraveling.provider.deviation.Deviation.java
com.markupartist.sthlmtraveling.provider.planner.JourneyQuery.java
com.markupartist.sthlmtraveling.provider.planner.Planner.java
com.markupartist.sthlmtraveling.provider.site.Site.java
com.markupartist.sthlmtraveling.provider.site.SitesStore.java
com.markupartist.sthlmtraveling.receivers.OnAlarmReceiver.java
com.markupartist.sthlmtraveling.receivers.OnBootReceiver.java
com.markupartist.sthlmtraveling.service.DataMigrationService.java
com.markupartist.sthlmtraveling.service.DeviationService.java
com.markupartist.sthlmtraveling.service.WakefulIntentService.java
com.markupartist.sthlmtraveling.ui.view.DelayAutoCompleteTextView.java
com.markupartist.sthlmtraveling.ui.view.LineSegment.java
com.markupartist.sthlmtraveling.ui.view.SmsTicketDialog.java
com.markupartist.sthlmtraveling.ui.view.TripView.java
com.markupartist.sthlmtraveling.utils.Analytics.java
com.markupartist.sthlmtraveling.utils.BarcodeScannerIntegrator.java
com.markupartist.sthlmtraveling.utils.DateTimeUtil.java
com.markupartist.sthlmtraveling.utils.DisplayMetricsHelper.java
com.markupartist.sthlmtraveling.utils.ErrorReporter.java
com.markupartist.sthlmtraveling.utils.HttpHelper.java
com.markupartist.sthlmtraveling.utils.IntentUtil.java
com.markupartist.sthlmtraveling.utils.LocationUtils.java
com.markupartist.sthlmtraveling.utils.StreamUtils.java
com.markupartist.sthlmtraveling.utils.StringUtils.java
com.markupartist.sthlmtraveling.utils.ViewHelper.java
com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java