package com.dwin.DwinTools;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;

public class NavigationBarActivity extends AppCompatActivity {

    private final String TAG = NavigationBarActivity.class.getSimpleName();
    public static final String DWIN_NAVIGATION_BAR = "android.intent.action.dwin_navigation_bar";

    //发送"0"则显示导航栏，发送"1"则隐藏导航栏
    public static final String DWIN_NAVIGATION_BAR_SHOW_VALUE = "0";
    public static final String DWIN_NAVIGATION_BAR_HIDE_VALUE = "1";
    public static final String ALWAYS_HIDE_BAR = "always_hide_bar";

    private Switch mSwitchNavigationBarHide;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_bar);

        mSwitchNavigationBarHide = findViewById(R.id.switch_navigation_bar);

        int value = Settings.System.getInt(
                this.getContentResolver(), ALWAYS_HIDE_BAR, 0);
        mSwitchNavigationBarHide.setChecked(value == 1);
        mSwitchNavigationBarHide.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setNavigationBar(isChecked ? DWIN_NAVIGATION_BAR_HIDE_VALUE:DWIN_NAVIGATION_BAR_SHOW_VALUE);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        int value = Settings.System.getInt(
                this.getContentResolver(), ALWAYS_HIDE_BAR, 0);
        mSwitchNavigationBarHide.setChecked(value == 1);
    }

    /**
     * 通过发送广播，系统则会进行相应的隐藏\显示导航栏
     * @param value 是否显示导航栏的值
     */
    private void setNavigationBar(String value){
        Log.v(TAG,"Dwin test sendBroadcast!");
        Intent sr = new Intent(DWIN_NAVIGATION_BAR);
        sr.putExtra("message", value);
        sendBroadcast(sr);
    }
}
