package sk.henrichg.phoneprofilesplus;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.os.Handler;

import java.util.Calendar;

public class SettingsContentObserver  extends ContentObserver {

    //public static boolean internalChange = false;

    private static int previousVolumeRing = 0;
    private static int previousVolumeNotification = 0;
    private static int previousVolumeMusic = 0;
    private static int previousVolumeAlarm = 0;
    private static int previousVolumeSystem = 0;
    private static int previousVolumeVoice = 0;
    //private int defaultRingerMode = 0;

    Context context;

    public SettingsContentObserver(Context c, Handler handler) {
        super(handler);
        context=c;

        //Log.e("### SettingsContentObserver", "xxx");

        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        previousVolumeRing = audioManager.getStreamVolume(AudioManager.STREAM_RING);
        previousVolumeNotification = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
        previousVolumeMusic = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        previousVolumeAlarm = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
        previousVolumeSystem = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
        previousVolumeVoice = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return super.deliverSelfNotifications();
    }

    private int volumeChangeDetect(int volumeStream, int previousVolume, AudioManager audioManager) {

        int currentVolume = audioManager.getStreamVolume(volumeStream);
        //Log.e("### SettingsContentObserver", "channel="+volumeStream+" currentVolume="+currentVolume);
        //Log.e("### SettingsContentObserver", "channel="+volumeStream+" previousVolume="+previousVolume);

        int delta=previousVolume-currentVolume;

        if(delta>0)
        {
            //Log.e("### SettingsContentObserver", "channel="+volumeStream+" Decreased");
            if (!RingerModeChangeReceiver.internalChange) {
                if (volumeStream == AudioManager.STREAM_RING)
                    GlobalData.setRingerVolume(context, currentVolume);
                if (volumeStream == AudioManager.STREAM_NOTIFICATION)
                    GlobalData.setRingerVolume(context, currentVolume);
            }
        }
        else if(delta<0)
        {
            //Log.e("### SettingsContentObserver", "channel="+volumeStream+" Increased");
            if (!RingerModeChangeReceiver.internalChange) {
                if (volumeStream == AudioManager.STREAM_RING)
                    GlobalData.setRingerVolume(context, currentVolume);
                if (volumeStream == AudioManager.STREAM_NOTIFICATION)
                    GlobalData.setRingerVolume(context, currentVolume);
            }
        }
        return currentVolume;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);

        //Log.e("### SettingsContentObserver", "onChange - internalChange=" + internalChange);

        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        previousVolumeRing = volumeChangeDetect(AudioManager.STREAM_RING, previousVolumeRing, audioManager);
        previousVolumeNotification = volumeChangeDetect(AudioManager.STREAM_NOTIFICATION, previousVolumeNotification, audioManager);
        previousVolumeMusic = volumeChangeDetect(AudioManager.STREAM_MUSIC, previousVolumeMusic, audioManager);
        previousVolumeAlarm = volumeChangeDetect(AudioManager.STREAM_ALARM, previousVolumeAlarm, audioManager);
        previousVolumeSystem = volumeChangeDetect(AudioManager.STREAM_SYSTEM, previousVolumeSystem, audioManager);
        previousVolumeVoice = volumeChangeDetect(AudioManager.STREAM_VOICE_CALL, previousVolumeVoice, audioManager);

        //internalChange = false;

        //Context context = getApplicationContext();
        Intent _intent = new Intent(context, DisableInernalChangeBroadcastReceiver.class);
        //intent.putExtra(EXTRA_ONESHOT, 1);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 1, _intent, PendingIntent.FLAG_CANCEL_CURRENT);

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 5);
        long alarmTime = calendar.getTimeInMillis();

        AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, alarmTime, alarmIntent);

    }

}

@@ -3436,17 +3436,122 @@
 RVICE);%0A
-%0A
+        int audioMode = audioManager.getMode();%0A%0A        if (audioMode == AudioManager.MODE_NORMAL) %7B%0A    
         
@@ -3651,32 +3651,36 @@
 nager);%0A        
+  
+  
 previousVolumeNo
@@ -3788,32 +3788,36 @@
 nager);%0A        
+    
 previousVolumeMu
@@ -3904,32 +3904,36 @@
 nager);%0A        
+  
+  
 previousVolumeAl
@@ -4012,32 +4012,36 @@
  audioManager);%0A
+    
         previous
@@ -4131,32 +4131,42 @@
  audioManager);%0A
+        %7D%0A
         previous

Update same volumes from external chamge, only in NORMAL_MODE