/*
 * Copyright 2014 Hauke Lampe
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


package org.openchaos.android.fooping.service;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Base64;
import android.util.Log;

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

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.UUID;


public class PingServiceGCM extends WakefulBroadcastReceiver {
	private static final String tag = PingServiceGCM.class.getSimpleName();

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d(tag, "Received broadcast: " + intent.toString());

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		if (!prefs.getBoolean("EnableGCM", false)) {
			Log.w(tag, "GCM control is disabled. Message ignored");
			return;
		}

		final String gcm_sender = prefs.getString("GCM_SENDER", "");
		if ("".equals(gcm_sender)) {
			Log.w(tag, "No GCM Sender ID configured. Message ignored");
			return;
		}

		Bundle extras = intent.getExtras();
		if ((extras == null) || extras.isEmpty()) {
			Log.w(tag, "Extra section is empty. Message ignored");
			return;
		}

		final GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
		String messageType = gcm.getMessageType(intent);
		if (messageType == null) {
			Log.w(tag, "No GCM message found. Message ignored");
			return;
		}

		/*
		 * 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.i(tag, "Messages deleted: " + extras.toString());
			// If it's a regular GCM message, do some work.
		} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
			Log.i(tag, "Received command: " + extras.toString());

			final String action = extras.getString("action");
			final String msgId = extras.getString("message_id");
			if ((action == null) || (msgId == null)) {
				Log.w(tag, "Required request parameters not set. Message ignored");
				return;
			}

			startWakefulService(context, new Intent(action, null, context, PingService.class).putExtra(PingService.EXTRA_RECEIVER, new ResultReceiver(null) {
				@Override
				protected void onReceiveResult(int resultCode, Bundle resultData) {
					String output_string = "";
					long msglen = 0;

					Bundle data = new Bundle();
					data.putString("action", action);
					data.putString("message_id", msgId);
					data.putString("result_code", Integer.toString(resultCode));

					ArrayList<Bundle> results = resultData.getParcelableArrayList(PingService.EXTRA_RESULTS);
					if (results != null) {
						Log.d(tag, "Results received: " + results.size());

						JSONArray outputs = new JSONArray();

						for (Bundle result : results) {
							if (result == null) {
								Log.e(tag, "NULL result received");
								continue;
							}

							byte[] output = result.getByteArray(PingService.EXTRA_OUTPUT);
							msglen += result.getLong(PingService.EXTRA_MSGLEN);

							if (output == null) {
								Log.e(tag, "NULL output received");
								continue;
							}

							outputs.put(Base64.encodeToString(output, Base64.NO_WRAP));
						}

						output_string = outputs.toString();
						data.putString("output", output_string);
					} else {
						Log.e(tag, "NULL results received");
					}

					try {
						gcm.send(gcm_sender + "@gcm.googleapis.com", UUID.randomUUID().toString(), data);
						Log.d(tag, "Message sent: " + output_string.getBytes().length + " bytes (raw: " + msglen + " bytes)");
					} catch (Exception e) {
						Log.e(tag, "GCM send failed", e);
					}

					Intent serviceIntent = resultData.getParcelable(PingService.EXTRA_INTENT);
					if ((serviceIntent == null) || !WakefulBroadcastReceiver.completeWakefulIntent(serviceIntent)) {
						Log.w(tag, "Wake lock release failed. No active wake lock?");
					}
				}
			}));
		} else {
			Log.d(tag, "Unknown GCM message type. Message ignored: " + extras.toString());
		}
	}
}

@@ -3425,26 +3425,25 @@
 tring output
-_s
+S
 tring = %22%22;%0A
@@ -3452,22 +3452,25 @@
 %09%09%09long 
-msglen
+rawLength
  = 0;%0A%0A%09
@@ -4090,22 +4090,25 @@
 %0A%09%09%09%09%09%09%09
-msglen
+rawLength
  += resu
@@ -4331,26 +4331,25 @@
 %09%09%09%09%09%09output
-_s
+S
 tring = outp
@@ -4397,26 +4397,25 @@
 put%22, output
-_s
+S
 tring);%0A%09%09%09%09
@@ -4471,24 +4471,196 @@
 %22);%0A%09%09%09%09%09%7D%0A%0A
+%09%09%09%09%09long outputLength = outputString.getBytes().length;%0A%09%09%09%09%09if (outputLength %3E 4096) %7B%0A%09%09%09%09%09%09Log.w(tag, %22Message probably too long: %22 + outputLength + %22 bytes%22);%0A%09%09%09%09%09%7D%0A%0A
 %09%09%09%09%09try %7B%0A%09
@@ -4792,28 +4792,9 @@
 tput
-_string.getBytes().l
+L
 engt
@@ -4819,14 +4819,17 @@
 %22 + 
-msglen
+rawLength
  + %22

rename variables