package com.ninty.system.setting;

import android.content.Context;
import android.media.AudioManager;
import android.provider.Settings;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

/**
 * Created by ninty on 2017/5/29.
 */

public class SystemSetting extends ReactContextBaseJavaModule{

    private AudioManager am ;

    public SystemSetting(ReactApplicationContext reactContext) {
        super(reactContext);
        am = (AudioManager) getReactApplicationContext().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    }

    @Override
    public String getName() {
        return "SystemSetting";
    }

    @ReactMethod
    public void setScreenMode(int mode){
        mode = mode == Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL ? mode : Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        Settings.System.putInt(getReactApplicationContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, mode);
    }

    @ReactMethod
    public void getScreenMode(Promise promise){
        try {
            int mode = Settings.System.getInt(getReactApplicationContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
            promise.resolve(mode);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
            promise.reject("-1","get screen mode fail", e);
        }
    }

    @ReactMethod
    public void setBrightness(float val){
//        setScreenMode(Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        final int brightness = (int)(val * 255);
        Settings.System.putInt(getReactApplicationContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
    }

    @ReactMethod
    public void getBrightness(Promise promise){
        try {
            int val = Settings.System.getInt(getReactApplicationContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
            promise.resolve(val * 1.0f / 255);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
            promise.reject("-1","get brightness fail", e);
        }
    }

    @ReactMethod
    public void setVolume(float val){
        am.setStreamVolume(AudioManager.STREAM_MUSIC, (int) (val * am.getStreamMaxVolume(AudioManager.STREAM_MUSIC)), AudioManager.FLAG_PLAY_SOUND);
    }

    @ReactMethod
    public  void getVolume(Promise promise){
        float volume = am.getStreamVolume(AudioManager.STREAM_MUSIC) * 1.0f / am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        promise.resolve(volume);
    }
}

@@ -51,23 +51,133 @@
 content.
-Context
+BroadcastReceiver;%0Aimport android.content.Context;%0Aimport android.content.Intent;%0Aimport android.content.IntentFilter
 ;%0Aimport
@@ -240,16 +240,60 @@
 tings;%0A%0A
+import com.facebook.react.bridge.Arguments;%0A
 import c
@@ -490,16 +490,127 @@
 tMethod;
+%0Aimport com.facebook.react.bridge.WritableMap;%0Aimport com.facebook.react.modules.core.DeviceEventManagerModule;
 %0A%0A/**%0A *
@@ -707,16 +707,17 @@
 vaModule
+ 
 %7B%0A%0A    p
@@ -727,24 +727,133 @@
 ate 
-AudioManager am 
+ReactApplicationContext context;%0A    private AudioManager am;%0A    private BroadcastReceiver volumeBR;%0A    IntentFilter filter
 ;%0A%0A 
@@ -945,16 +945,48 @@
 ntext);%0A
+        context = reactContext;%0A
         
@@ -1098,16 +1098,823 @@
 ERVICE);
+%0A%0A        listerVolume(reactContext);%0A    %7D%0A%0A    private void listerVolume(final ReactApplicationContext reactContext) %7B%0A        volumeBR = new BroadcastReceiver() %7B%0A            @Override%0A            public void onReceive(Context context, Intent intent) %7B%0A                if (intent.getAction().equals(%22android.media.VOLUME_CHANGED_ACTION%22)) %7B%0A                    WritableMap para = new Arguments().createMap();%0A                    para.putDouble(%22value%22, getNormalizationVolume());%0A                    reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)%0A                            .emit(%22EventVolume%22, para);%0A                %7D%0A            %7D%0A        %7D;%0A        filter = new IntentFilter(%22android.media.VOLUME_CHANGED_ACTION%22);%0A%0A        context.registerReceiver(volumeBR, filter);
 %0A    %7D%0A%0A
@@ -2052,16 +2052,17 @@
 nt mode)
+ 
 %7B%0A      
@@ -2374,32 +2374,33 @@
 Promise promise)
+ 
 %7B%0A        try %7B%0A
@@ -2689,24 +2689,25 @@
 reject(%22-1%22,
+ 
 %22get screen 
@@ -2800,82 +2800,11 @@
 val)
+ 
 %7B%0A
-//        setScreenMode(Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);%0A
     
@@ -2835,16 +2835,17 @@
  = (int)
+ 
 (val * 2
@@ -3041,32 +3041,33 @@
 Promise promise)
+ 
 %7B%0A        try %7B%0A
@@ -3366,16 +3366,17 @@
 ct(%22-1%22,
+ 
 %22get bri
@@ -3464,18 +3464,65 @@
 oat val)
+ 
 %7B%0A
+        context.unregisterReceiver(volumeBR);%0A
         
@@ -3662,16 +3662,68 @@
 SOUND);%0A
+        context.registerReceiver(volumeBR, filter);%0A
     %7D%0A%0A 
@@ -3749,17 +3749,16 @@
  public 
- 
 void get
@@ -3780,16 +3780,17 @@
 promise)
+ 
 %7B%0A      
@@ -3795,22 +3795,117 @@
     
-float volume =
+promise.resolve(getNormalizationVolume());%0A    %7D%0A%0A    private float getNormalizationVolume() %7B%0A        return
  am.
@@ -4010,41 +4010,8 @@
 C);%0A
-        promise.resolve(volume);%0A
     

add volume listener for Android