package com.advanced_android.screenonoffbroadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;
import android.app.Activity;
import android.util.Log;
import android.media.AudioManager;
import java.io.*;

public class Receiver extends BroadcastReceiver
{
		private static AudioManager mAudioManager;
		private static int mLastVolume;
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            intent.getExtras();
            if (TextUtils.equals(action,  "android.media.VOLUME_CHANGED_ACTION")) {
            	if(mAudioManager == null)
            	mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            	int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                //Toast.makeText(MainActivity.this, "音量が変化しました", Toast.LENGTH_SHORT).show();
                Log.d("FUCK_META","volume change received " + currentVolume + " " + mLastVolume);
                if(mLastVolume == 0 && currentVolume == 1)
                {
                	try{
                	Log.d("FUCK_META", "running script");
                	Process child = Runtime.getRuntime().exec(new String[]{"/system/bin/sh", "-c", "echo sh /data/local/tmp/toggle.sh |nc 127.0.0.1 6667"});
// Выходной и входной потоки
                    OutputStream out = child.getOutputStream();
                                        InputStream in = child.getInputStream();
                                        
                                                            //Входной поток может что-нибудь вернуть
                                                                                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                                                                                                    String line;
                                                                                                                        String result = "";
                                                                                                                                            while ((line = bufferedReader.readLine()) != null)
                                                                                                                                                                    result += line;
                                                                                                                                                                    
                                                                                                                                                                                        //Обработка того, что он вернул
					Log.d("FUCK META",result)         ;                                                                                                                                                                               
                	
                	}catch(Exception e){Log.d("FUCK META",e.toString());}
                }
                mLastVolume = currentVolume;
            }
            Log.d("FUCK_META",action);
            Intent prox_close = new Intent();
            prox_close.setAction("com.oculus.vrpowermanager.prox_close");
            context.sendBroadcast(prox_close);
        }

}
