/*
 * Copyright (C) 2010 Google Inc.
 *
 * 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 com.google.ase.condition;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;

import com.google.ase.AseLog;
import com.google.ase.trigger.ConditionListener;

public class RingerModeCondition implements Condition {
  private ConditionListener mConditionListener;
  private final AudioManager mAudioManager;
  private Context mContext;
  private final Configuration mConfiguration;
  private boolean mInCondition;
  
  /** Our broadcast receiver dealing with changes to the ringer mode. */
  private final BroadcastReceiver ringerModeBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      int ringerMode = intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1);

      switch (ringerMode) {
      case AudioManager.RINGER_MODE_NORMAL:
      case AudioManager.RINGER_MODE_SILENT:
      case AudioManager.RINGER_MODE_VIBRATE:
        if (mConfiguration.getMode() == ringerMode && !mInCondition) {
          invokeListener(null);
          mInCondition = true;
        } else if (mInCondition) {
          mInCondition = false;
          invokeListener(null);
        }
      default:
        AseLog.e("Invalid ringer mode.");
      }
    }
  };

  /** Configuration of the RingerModeCondition.  Stores the mode on which to trigger. */
  public static class Configuration implements ConditionConfiguration {
    int mmMode;

    /**
     * @param mode
     *          the mode on which to trigger: see {@link AudioManager#RINGER_MODE_NORMAL} etc.
     */
    public Configuration(int mode) {
      mmMode = mode;
    }

    public int getMode() {
      return mmMode;
    }

    @Override
    public Condition getCondition(Context context) {
      return new RingerModeCondition(context, this);
    }
  }

  private RingerModeCondition(Context context, Configuration configuration) {
    mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    mContext = context;
    mConfiguration = configuration;
  }

  @Override
  public void addListener(ConditionListener listener) {
    mConditionListener = listener;
  }

  @Override
  public void start() {
    if (mAudioManager.getRingerMode() == mConfiguration.getMode()) {
      mInCondition = true;
      invokeListener(null);
    } else {
      mInCondition = false;
      invokeListener(null);
    }

    mContext.registerReceiver(ringerModeBroadcastReceiver, new IntentFilter(
        AudioManager.RINGER_MODE_CHANGED_ACTION));
  }

  private void invokeListener(Bundle state) {
    if (mConditionListener != null) {
      mConditionListener.run(state);
    }
  }

  @Override
  public void stop() {
    mContext.unregisterReceiver(ringerModeBroadcastReceiver);
  }
}

@@ -963,16 +963,87 @@
 ition %7B%0A
+  private static final String RINGER_MODE_STATE_EXTRA = %22ringer_mode%22;%0A
   privat
@@ -1214,28 +1214,31 @@
 ate 
-boolean mInCondition
+int mPreviousRingerMode
 ;%0A  
@@ -1733,33 +1733,28 @@
 f (m
-Configuration.get
+PreviousRinger
 Mode
-() =
+ !
 = ri
@@ -1765,188 +1765,147 @@
 Mode
- && !mInCondition) %7B%0A          invokeListener(null);%0A          mInCondition = true;%0A        %7D else if (mInCondition) %7B%0A          mInCondition = false;%0A          invokeListener(null
+) %7B%0A          mPreviousRingerMode = ringerMode;%0A          invokeListener();%0A        %7D else %7B%0A          ringerMode = mConfiguration.getMode(
 );%0A 
@@ -2928,29 +2928,17 @@
     
-if (mAudioManager.get
+mPrevious
 Ring
@@ -2947,140 +2947,42 @@
 Mode
-()
  =
-=
  m
-Configuration.getMode()) %7B%0A      mInCondition = true;%0A      invokeListener(null);%0A    %7D else %7B%0A      mInCondition = false;%0A  
+AudioManager.getRingerMode();%0A
     
@@ -3000,21 +3000,10 @@
 ner(
-null);%0A    %7D%0A
+);
 %0A   
@@ -3166,20 +3166,8 @@
 ner(
-Bundle state
 ) %7B%0A
@@ -3200,24 +3200,125 @@
  != null) %7B%0A
+      Bundle state = new Bundle();%0A      state.putInt(RINGER_MODE_STATE_EXTRA, mPreviousRingerMode);%0A
       mCondi

Make it so that RingerModeCondition passes the current ringer mode to the triggered script as an extra.