com.licenta.android.licenseapp.location.GeofenceTransitionsService.java Source code

Java tutorial

Introduction

Here is the source code for com.licenta.android.licenseapp.location.GeofenceTransitionsService.java

Source

package com.licenta.android.licenseapp.location;

import android.app.IntentService;
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.media.RingtoneManager;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingEvent;
import com.licenta.android.licenseapp.R;
import com.licenta.android.licenseapp.activity.MainTabActivity;
import com.licenta.android.licenseapp.util.Constants;

import java.util.List;

/**
 * Created by oana.ilovan on 22.05.2016.
 */
public class GeofenceTransitionsService extends IntentService {

    private static final String TAG = "Geofence";

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public GeofenceTransitionsService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        if (geofencingEvent.hasError()) {
            //            String errorMessage = GeofenceErrorMessages.getErrorString(this,
            //                    geofencingEvent.getErrorCode());
            //            Log.e(TAG, errorMessage);
            return;
        }

        // Get the transition type.
        int geofenceTransition = geofencingEvent.getGeofenceTransition();

        // Test that the reported transition was of interest.
        if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER
                || geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

            // Get the geofences that were triggered. A single event can trigger
            // multiple geofences.
            List triggeringGeofences = geofencingEvent.getTriggeringGeofences();

            // Get the transition details as a String.
            //            String geofenceTransitionDetails = getGeofenceTransitionDetails(
            //                    this,
            //                    geofenceTransition,
            //                    triggeringGeofences
            //            );

            // Send notification and log the transition details.
            sendNotification("From the midst of cold ash");//geofenceTransitionDetails);
            //Log.i(TAG, geofenceTransitionDetails);
        } else {
            // Log the error.
            //            Log.e(TAG, getString(R.string.geofence_transition_invalid_type,
            //                    geofenceTransition));
        }
    }

    private void sendNotification(String msg) {
        NotificationManager mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intentDismiss = new Intent(this, MainTabActivity.class);
        intentDismiss.putExtra(Constants.KEY_NOTIFICATION_ID, Constants.ALARM_ID);
        intentDismiss.putExtra(Constants.KEY_DISMISS_ALARM, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentDismiss,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher).setContentTitle("HellO")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg)
                .setCategory(Notification.CATEGORY_ALARM).setSound(alarmSound);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        mBuilder.setSound(Uri.parse(prefs.getString("alarm_ringtone", "")));
        if (prefs.getBoolean("alarm_vibrate", false))
            mBuilder.setVibrate(new long[] { 1000, 1000 });

        mBuilder.addAction(0, getString(R.string.check_in), pendingIntent);
        mBuilder.addAction(R.drawable.ic_stat_action_alarm_off_notif, getString(R.string.dismiss_alarm),
                pendingIntent);
        //mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(Constants.ALARM_ID, mBuilder.build());
    }
}