/*
 * Copyright (C) 2012 OTA Updater
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may only use this file in compliance with the license and provided you are not associated with or are in co-operation anyone by the name 'X Vanderpoel'.
 * 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.updater.ota;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

import com.google.android.gcm.GCMRegistrar;
import com.updater.ota.FetchRomInfoTask.RomInfoListener;

public class UpdateCheckReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(final Context context, Intent intent) {
	    if (Utils.isROMSupported()) {
	        if (Utils.marketAvailable(context)) {
        	    GCMRegistrar.checkDevice(context.getApplicationContext());
                GCMRegistrar.checkManifest(context.getApplicationContext());
                final String regId = GCMRegistrar.getRegistrationId(context.getApplicationContext());
                if (regId.equals("")) {
                    GCMRegistrar.register(context.getApplicationContext(), Config.GCM_SENDER_ID);
                    Log.v("OTAUpdater::GCM", "GCM registered");
                } else {
                    Log.v("OTAUpdater::GCM", "Already registered");
                }
	        } else {
	            if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
	                setAlarm(context);
	            }

	            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
	            final WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, UpdateCheckReceiver.class.getName());
	            wl.acquire();

	            new FetchRomInfoTask(context, new RomInfoListener() {
	                @Override
	                public void onStartLoading() { }
	                @Override
	                public void onLoaded(RomInfo info) {
	                    boolean available = false;
	                    String buildVersion = android.os.Build.ID;
	                    if (info != null && info.mRom != null && !info.mRom.isEmpty() && !buildVersion.equals(info.mRom)) {
	                        available = true;
	                    }

	                    if (available) {
	                        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
	                        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);

	                        Notification.Builder builder = new Notification.Builder(context);
	                        builder.setContentIntent(contentIntent);
	                        builder.setContentTitle(context.getString(R.string.notif_source));
	                        builder.setContentText(context.getString(R.string.notif_text_rom));
	                        builder.setTicker(context.getString(R.string.notif_text_rom));
	                        builder.setWhen(System.currentTimeMillis());
	                        builder.setSmallIcon(R.drawable.updates);
	                        nm.notify(1, builder.getNotification());
	                    }

	                    wl.release();
	                }
	                @Override
	                public void onError(String error) {
	                    wl.release();
	                }
	            }).execute();
	        }
	    } else {
	        Log.w("OTAUpdater", "Unsupported ROM");
	    }
	}

    protected static void setAlarm(Context ctx) {
        AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ctx, UpdateCheckReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        am.cancel(pendingIntent);
        am.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}

@@ -2952,24 +2952,254 @@
 ailable) %7B%0D%0A
+%09                        Intent i = new Intent(context, OTAUpdaterActivity.class);%0D%0A%09                        i.setAction(OTAUpdaterActivity.NOTIF_ACTION);%0D%0A%09                        info.addToIntent(i);%0D%0A%09                        %0D%0A
 %09           
@@ -3412,20 +3412,9 @@
  0, 
-new Intent()
+i
 , 0)

fix intent in pull notif