org.openchaos.android.buildmessage.GcmIntentService.java Source code

Java tutorial

Introduction

Here is the source code for org.openchaos.android.buildmessage.GcmIntentService.java

Source

/*
 * Copyright 2014 Hauke Lampe
 *
 * 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 org.openchaos.android.buildmessage;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.text.Html;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GcmIntentService extends IntentService {
    private static final String TAG = GcmIntentService.class.getSimpleName();
    private static final Pattern jenkinsPattern = Pattern
            .compile("^(.*\\n\\s*)?Project (.*) build #([0-9]*): (.*) in ([^:]*): (https?://.*)$");

    private SharedPreferences prefs;

    public GcmIntentService() {
        super(TAG);
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate()");
        super.onCreate();
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) { // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                Log.w(TAG, "Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                Log.w(TAG, "Deleted messages on server: " + extras.toString());
                // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                Log.i(TAG, "Received message: " + extras.toString());
                String sender = extras.getString("from");
                int senderIndex = 0;
                // TODO: multiple senders in a string set
                if (!((sender != null) && sender.equals(prefs.getString("sender_id_1", null)))) {
                    Log.w(TAG, "Unknown GCM sender: " + sender);
                    //               return;
                }

                createNotification(senderIndex, extras.getString("m"), extras.getString("i"));
            } else {
                Log.d(TAG, "Unknown GCM message type. Message ignored: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    private void createNotification(int senderIndex, String message, String committers) {
        Log.d(TAG, "Notification from sender " + senderIndex);

        if (message == null) {
            Log.e(TAG, "Message is null");
            return;
        }

        Matcher jenkinsMatch = jenkinsPattern.matcher(message);
        if (!jenkinsMatch.matches()) {
            Log.e(TAG, "Could not parse message: " + message);
            return;
        }

        String project = jenkinsMatch.group(2);
        String buildNum = jenkinsMatch.group(3);
        String buildStatusString = jenkinsMatch.group(4);
        String buildDuration = jenkinsMatch.group(5);
        String url = jenkinsMatch.group(6);

        String basicText = getString(R.string.basic_text, project, buildStatusString);
        String longText = getString(R.string.long_text, project, buildStatusString, buildNum, buildDuration);

        long[] vibe = { 250, 250, 250, 500, 250, 250 };
        int titleId;
        int soundId;
        int iconId;

        buildStatusEnum buildStatus;
        try {
            buildStatus = buildStatusEnum.valueOf(buildStatusString.replace(' ', '_'));
        } catch (IllegalArgumentException e) {
            buildStatus = buildStatusEnum.UNKNOWN;
            Log.e(TAG, "Unknown build status", e);
        }

        // TODO: configure sounds in preferences
        // TODO: static map
        switch (buildStatus) {
        case SUCCESS:
            titleId = R.string.message_success;
            soundId = R.raw.build_success;
            iconId = R.drawable.ic_build_success;
            break;
        case FAILURE:
            titleId = R.string.message_failure;
            soundId = R.raw.build_failure;
            iconId = R.drawable.ic_build_failure;
            break;
        case STILL_FAILING:
            titleId = R.string.message_still_failing;
            soundId = R.raw.build_still_failing;
            iconId = R.drawable.ic_build_still_failing;
            break;
        case FIXED:
            titleId = R.string.message_fixed;
            soundId = R.raw.build_fixed;
            iconId = R.drawable.ic_build_fixed;
            break;
        default:
            titleId = R.string.message_unknown;
            soundId = R.raw.build_unknown;
            iconId = R.drawable.ic_build_unknown;
            break;
        }

        ((NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE)).notify(senderIndex,
                new NotificationCompat.Builder(this).setContentTitle(getString(titleId))
                        .setContentText(Html.fromHtml(basicText))
                        //                  .setSubText(getString(R.string.clickToOpen))
                        .setWhen(System.currentTimeMillis())
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(Html.fromHtml(longText)))
                        .setSmallIcon(iconId)
                        .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + soundId))
                        .setLights(Color.RED, 250, 1500).setVibrate(vibe).setAutoCancel(true)
                        .setContentIntent(PendingIntent.getActivity(this, 0,
                                new Intent(Intent.ACTION_VIEW, Uri.parse(url).normalizeScheme()),
                                PendingIntent.FLAG_ONE_SHOT))
                        .build());
    }

    private enum buildStatusEnum {
        UNKNOWN, SUCCESS, FAILURE, STILL_FAILING, FIXED
    }
}