/*
 * Copyright 2014 Hauke Lampe
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


package org.openchaos.android.fooping;

import org.openchaos.android.fooping.service.PingService;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Fragment;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;
import android.widget.ToggleButton;


public class MainFragment extends Fragment {
	private static final String tag = "MainFragment";

	private Activity activity;
	private Context appContext;
	private SharedPreferences prefs;
	private AlarmManager alarmManager;
	private boolean alarmRunning;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.main_fragment, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d(tag, "onActivityCreated()");
		super.onActivityCreated(savedInstanceState);

		activity = getActivity();
		prefs = PreferenceManager.getDefaultSharedPreferences(activity);
		alarmManager = (AlarmManager)activity.getSystemService(Context.ALARM_SERVICE);

		// alarm intent might live longer than this activity
		appContext = activity.getApplicationContext();

		// NB: a pending intent does not reliably indicate a running alarm
		// always cancel the intent after stopping the alarm
		alarmRunning = (PendingIntent.getBroadcast(appContext, 0, new Intent(appContext, PendingIntentReceiver.class), PendingIntent.FLAG_NO_CREATE)
				!= null);

		if (alarmRunning) {
			Log.d(tag, "Found pending alarm intent");
			Toast.makeText(activity, R.string.alarm_running, Toast.LENGTH_SHORT).show();
		}

		ToggleButton button = (ToggleButton)activity.findViewById(R.id.StartStopButton);
		button.setChecked(alarmRunning);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				alarmRunning = !alarmRunning;
				if (alarmRunning) {
					Log.d(tag, "onClick(): start");
					long updateInterval = Long.valueOf(prefs.getString("UpdateInterval", "-1"));
					if (updateInterval > 0) {
						alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, updateInterval * 1000,
								PendingIntent.getBroadcast(appContext, 0,
									new Intent(Intent.ACTION_RUN, null, appContext, PendingIntentReceiver.class)
										.putExtra(Intent.EXTRA_INTENT,
											PendingIntent.getService(appContext, 0, new Intent(appContext, PingService.class), 0)
									), PendingIntent.FLAG_CANCEL_CURRENT));
						Toast.makeText(activity, R.string.alarm_started, Toast.LENGTH_SHORT).show();
					}
				} else {
					Log.d(tag, "onClick(): stop");
					PendingIntent alarmIntent = PendingIntent.getBroadcast(appContext, 0, new Intent(appContext, PendingIntentReceiver.class), 0);
					alarmManager.cancel(alarmIntent);
					alarmIntent.cancel();
					Toast.makeText(activity, R.string.alarm_stopped, Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
}

@@ -1013,16 +1013,40 @@
 rences;%0A
+import android.net.Uri;%0A
 import a
@@ -1591,16 +1591,48 @@
 nning;%0A%0A
+%09private Intent serviceIntent;%0A%0A
 %09@Overri
@@ -2242,24 +2242,414 @@
 Context();%0A%0A
+%09%09// serviceUri is set only to identify this call to PendingIntentReceiver%0A%09%09Uri serviceUri = Uri.parse(%22content://%22 + appContext.getPackageName() + %22/PingService%22);%0A%09%09serviceIntent = new Intent(Intent.ACTION_RUN, serviceUri, appContext, PendingIntentReceiver.class)%0A%09%09%09.putExtra(Intent.EXTRA_INTENT, PendingIntent.getService(appContext, 0, new Intent(appContext, PingService.class), 0));%0A%0A
 %09%09// NB: a p
@@ -2824,59 +2824,21 @@
  0, 
-new Intent(appContext, PendingIntentReceiver.class)
+serviceIntent
 , Pe
@@ -2864,21 +2864,17 @@
 _CREATE)
-%0A%09%09%09%09
+ 
 != null)
@@ -3608,243 +3608,22 @@
 , 0,
-%0A%09%09%09%09%09%09%09%09%09new Intent(Intent.ACTION_RUN, null, appContext, PendingIntentReceiver.class)%0A%09%09%09%09%09%09%09%09%09%09.putExtra(Intent.EXTRA_INTENT,%0A%09%09%09%09%09%09%09%09%09%09%09PendingIntent.getService(appContext, 0, new Intent(appContext, PingService.class), 0)%0A%09%09%09%09%09%09%09%09%09)
+ serviceIntent
 , Pe
@@ -3875,59 +3875,21 @@
  0, 
-new Intent(appContext, PendingIntentReceiver.class)
+serviceIntent
 , 0)

save the inner intent in serviceIntent