com.personalcapital.personalperks.PerkNotificationService.java Source code

Java tutorial

Introduction

Here is the source code for com.personalcapital.personalperks.PerkNotificationService.java

Source

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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.
 */

package com.personalcapital.personalperks;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat;

import com.personalcapital.personalperks.manager.SessionManager;
import com.personalcapital.personalperks.model.Perk;

import java.util.ArrayList;

public class PerkNotificationService extends Service {
    public static final String ACTION_SEND_NOTIFICATION = "com.personalcapital.personalperks.SEND_NOTIFICATION";
    public static final String EXTRA_NOTIFICATION_ID = "notification_id";

    protected NotificationManagerCompat notificationManager;
    protected Binder binder = new LocalBinder();
    protected Perk perk;

    public class LocalBinder extends Binder {
        PerkNotificationService getService() {
            return PerkNotificationService.this;
        }
    }

    @Override
    public void onCreate() {
        notificationManager = NotificationManagerCompat.from(this);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null && intent.getAction() != null && intent.getAction().equals(ACTION_SEND_NOTIFICATION)) {
            createNotification(intent);
            return START_STICKY;
        }
        return START_NOT_STICKY;
    }

    private void createNotification(Intent intent) {
        int notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, 1);
        String perkId = intent.getStringExtra(Perk.PERK_ID);
        Perk perk = SessionManager.getInstance().getPerkWithPerkId(perkId);

        /*
        ArrayList<Notification> notificationPages = new ArrayList<Notification>();
            
        int stepCount = mRecipe.recipeSteps.size();
            
        for (int i = 0; i < stepCount; ++i) {
        Recipe.RecipeStep recipeStep = mRecipe.recipeSteps.get(i);
        NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
        style.bigText(recipeStep.stepText);
        style.setBigContentTitle(String.format(
                getResources().getString(R.string.step_count), i + 1, stepCount));
        style.setSummaryText("");
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setStyle(style);
        notificationPages.add(builder.build());
        }
            
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            
        if (mRecipe.recipeImage != null) {
        Bitmap recipeImage = Bitmap.createScaledBitmap(
                AssetUtils.loadBitmapAsset(this, mRecipe.recipeImage),
                Constants.NOTIFICATION_IMAGE_WIDTH, Constants.NOTIFICATION_IMAGE_HEIGHT, false);
        builder.setLargeIcon(recipeImage);
        }
        builder.setContentTitle(mRecipe.titleText);
        builder.setContentText(mRecipe.summaryText);
        builder.setSmallIcon(R.mipmap.ic_notification_recipe);
            
        Notification notification = builder
            .extend(new NotificationCompat.WearableExtender()
                    .addPages(notificationPages))
            .build();
        mNotificationManager.notify(Constants.NOTIFICATION_ID, notification);
        */

        ArrayList<Notification> notificationPages = new ArrayList<Notification>();

        if (perk.barCodeImage != null) {
            NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
            style.bigPicture(perk.barCodeImage);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setStyle(style);
            builder.setContentText(perk.modoCheckoutCode);
            notificationPages.add(builder.build());
        }

        //        NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
        //        style.bigText(recipeStep.stepText);
        //        style.setBigContentTitle(String.format(getResources().getString(R.string.step_count), i + 1, stepCount));
        //            style.setSummaryText("");
        //            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        //            builder.setStyle(style);
        //            notificationPages.add(builder.build());

        // Build an intent for an action to view a map
        Intent mapIntent = new Intent(Intent.ACTION_VIEW);
        Uri geoUri = Uri.parse("geo:0,0?q=" + Uri.encode(perk.address));
        mapIntent.setData(geoUri);
        PendingIntent mapPendingIntent = PendingIntent.getActivity(this, 0, mapIntent, 0);

        NotificationCompat.Action mapAction = new NotificationCompat.Action.Builder(R.drawable.ic_map,
                getString(R.string.map), mapPendingIntent).build();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        if (perk.logoImage != null) {
            Bitmap recipeImage = Bitmap.createScaledBitmap(perk.logoImage, 280, 280, false);
            builder.setLargeIcon(recipeImage);
        }
        builder.setContentTitle(perk.merchantName);
        builder.setContentText(perk.description);
        builder.setSmallIcon(R.drawable.ic_launcher);

        Notification notification = builder
                .extend(new NotificationCompat.WearableExtender().addPages(notificationPages).addAction(mapAction))
                .build();
        NotificationManagerCompat.from(this).notify(notificationId, notification);
    }
}