com.sidekickApp.PusherClass.java Source code

Java tutorial

Introduction

Here is the source code for com.sidekickApp.PusherClass.java

Source

package com.sidekickApp;

/*   Copyright (C) 2011 Emory Myers
 * 
 *  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. 
 *  
 *  Contributors: Martin Linkhorst
 */

import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONObject;

import android.content.Intent;
import android.util.Log;

public class PusherClass {

    /* the log tag constant */
    private static Main main;
    private static PusherClass PusherClassSingleton;
    private static final String TAG = "PusherClass";

    private static final String PUSHER_APP_KEY = "6a872d870502a173d7bc";
    private static final String PUSHER_APP_SECRET = "284b66ad590a9480c423";

    private static String PUBLIC_CHANNEL = "tammy";

    private static Pusher mPusher;

    private static Timer decayTimer;
    private static Timer inputTimer;
    private int numEvents;
    private double decayFactor = 0.997;
    private double interest;

    public static synchronized PusherClass getInstance(Main mainIn) {
        log("getInstance()");
        if (PusherClassSingleton == null) {
            PusherClassSingleton = new PusherClass(mainIn);
        }
        return PusherClassSingleton;
    }

    private PusherClass(Main mainIn) {
        main = mainIn;

        mPusher = new Pusher(PUSHER_APP_KEY, PUSHER_APP_SECRET);
        mPusher.bindAll(new PusherCallback() {
            @Override
            public void onEvent(String eventName, JSONObject eventData, String channelName) {
                log("Received " + eventData.toString() + " for event '" + eventName + "' on channel '" + channelName
                        + "'.");
                if (eventName.equals("open-app")) {
                    // A user is looking at their app
                    log("Pusher: open-app!");
                    numEvents++;
                }
            }
        });
    }

    public void initialize() {
        log("setup()");
        mPusher.connect();
        mPusher.subscribe(PUBLIC_CHANNEL);

        numEvents = 0;
        interest = 0.0;
        startDecayTimer();
        startInputTimer();
    }

    private void startDecayTimer() {
        log("startDecayTimer()");
        decayTimer = new Timer("decayTimer", true);
        long initialDelay = 0;
        long interval = 250;

        decayTimer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                // calculate decay
                interest = interest * decayFactor;
                if (interest > 100) {
                    interest = 100;
                }
                if (interest < 0.1) {
                    interest = 0;
                }
                log("Pusher: interest = " + interest);

                Intent i = new Intent();
                int interestI = (int) interest;
                i.putExtra("com.sidekickApp.interest", interestI);
                i.setAction(AppState.SIDECAR_INTEREST);
                main.sendBroadcast(i);
            }
        }, initialDelay, interval);

    }

    private void startInputTimer() {
        log("startInputTimer()");
        inputTimer = new Timer("inputTimer", true);
        long initialDelay = 0;
        long interval = 2000;

        inputTimer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                // calculate decay
                interest = interest + numEvents * 50;
                numEvents = 0;
            }
        }, initialDelay, interval);
    }

    public static void send(String eventName, String eventData) {
        try {
            String channelName = PUBLIC_CHANNEL;
            JSONObject eventJSON = new JSONObject(eventData);
            mPusher.sendEvent(eventName, eventJSON, channelName);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void log(String msg) {
        Log.v(TAG, "Debug: " + msg);
    }

}