陈奇
2018-11-30 c40ab5711d9025a5e25a85b5b24be72cfa90e375
app/src/main/java/com/moral/yunfushao/fragment/FragmentSettings.java
@@ -1,12 +1,16 @@
package com.moral.yunfushao.fragment;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
@@ -27,7 +31,6 @@
import com.moral.yunfushao.httputils.HttpUtils;
import com.moral.yunfushao.model.EventMessage;
import com.moral.yunfushao.model.VoiceBean;
import com.moral.yunfushao.utils.DebugUtils;
import org.greenrobot.eventbus.EventBus;
@@ -42,11 +45,12 @@
public class FragmentSettings extends BaseFragment {
    private TextView tv_version;
    private RelativeLayout rl_setvoice,rl_refresh;
    private RelativeLayout rl_setvoice, rl_refresh;
    private Button bt_sure;
    private Button bt_look;
    private SwitchCompat sc_open,sc_male;
    private int refreshIndex=0;
    private Button bt_look, sc_male;
    private SwitchCompat sc_open;
    private int refreshIndex = 0;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_setting_layout;
@@ -61,17 +65,8 @@
        sc_open = findView(R.id.sc_open);
        sc_male = findView(R.id.sc_male);
        tv_version = findView(R.id.tv_version);
        tv_version.setText("当前版本V"+getVersion());
//        bt_look.setVisibility(DebugUtils.debug_mode ? View.VISIBLE : View.GONE);
        tv_version.setText("当前版本V" + getVersion());
        bt_look.setVisibility(View.GONE);
        if (MainApp.theApp.sharedPreferencesUtil.getVoiceType() == 0) {
            sc_male.setChecked(false);
            sc_male.setText("男");
        }else{
            sc_male.setChecked(true);
            sc_male.setText("女");
        }
    }
    @Override
@@ -82,40 +77,123 @@
        sc_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sc_open.isChecked()){
                    update("is_open_upload","1");
                }else{
                    update("is_open_upload","0");
                if (sc_open.isChecked()) {
                    update("is_open_upload", "1");
                } else {
                    update("is_open_upload", "0");
                }
            }
        });
        sc_male.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        sc_male.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    sc_male.setText("女");
                    MainApp.theApp.sharedPreferencesUtil.saveVoiceType(1);
                    AppConfig.voideUrl1 = "http://yfsapi.7drlb.com/video/girl/music.mp3";
                    AppConfig.voideUrl2 = "http://yfsapi.7drlb.com/video/girl/music1.mp3";
                    AppConfig.voideUrl3 = "http://yfsapi.7drlb.com/video/girl/music2.mp3";
                }else{
                    sc_male.setText("男");
                    MainApp.theApp.sharedPreferencesUtil.saveVoiceType(0);
                    AppConfig.voideUrl1 = "http://yfsapi.7drlb.com/video/boy/music.mp3";
                    AppConfig.voideUrl2 = "http://yfsapi.7drlb.com/video/boy/music1.mp3";
                    AppConfig.voideUrl3 = "http://yfsapi.7drlb.com/video/boy/music2.mp3";
                }
                reset();
            public void onClick(View v) {
                chooseSoundCategory();
            }
        });
        bt_look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  Intent intent = new Intent(getActivity(), TestActivity.class);
                  startActivity(intent);
                Intent intent = new Intent(getActivity(), TestActivity.class);
                startActivity(intent);
            }
        });
    }
    @Override
    public void onResume() {
        super.onResume();
        switch (MainApp.theApp.sharedPreferencesUtil.getVoiceType()) {
            case 0:
                sc_male.setText("男声");
                break;
            case 1:
                sc_male.setText("女声");
                break;
            case 2:
                sc_male.setText("无声");
                break;
            case 3:
                sc_male.setText("自定义");
                break;
        }
    }
    /**
     * 选择声音弹出框
     */
    private void chooseSoundCategory() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        ListView listView = new ListView(getActivity());
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getActivity(),
                android.R.layout.simple_expandable_list_item_1,
                new String[]{"男声", "女声", "无声"});
        listView.setAdapter(arrayAdapter);
        builder.setTitle("选择默认铃声!");
        builder.setIcon(R.mipmap.icon_voice);
        builder.setView(listView);
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                alertDialog.dismiss();
                ejectTips(position);
            }
        });
    }
    /**
     * 温馨提示弹出框
     */
    private void ejectTips(final int position) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("温馨提示!");
        builder.setMessage("将会覆盖之前自定义或者默认的声音,确定这么做么?");
        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                setUpDefaultRingtone(position);
            }
        });
        builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
    private void setUpDefaultRingtone(int type) {
        switch (type) {
            case 0:
                sc_male.setText("男声");
                MainApp.theApp.sharedPreferencesUtil.saveVoiceType(0);
                AppConfig.voideUrl1 = "http://yfsapi.7drlb.com/video/boy/music.mp3";
                AppConfig.voideUrl2 = "http://yfsapi.7drlb.com/video/boy/music1.mp3";
                AppConfig.voideUrl3 = "http://yfsapi.7drlb.com/video/boy/music2.mp3";
                break;
            case 1:
                sc_male.setText("女声");
                MainApp.theApp.sharedPreferencesUtil.saveVoiceType(1);
                AppConfig.voideUrl1 = "http://yfsapi.7drlb.com/video/girl/music.mp3";
                AppConfig.voideUrl2 = "http://yfsapi.7drlb.com/video/girl/music1.mp3";
                AppConfig.voideUrl3 = "http://yfsapi.7drlb.com/video/girl/music2.mp3";
                break;
            case 2:
                sc_male.setText("无声");
                MainApp.theApp.sharedPreferencesUtil.saveVoiceType(2);
                AppConfig.voideUrl1 = null;
                AppConfig.voideUrl2 = null;
                AppConfig.voideUrl3 = null;
                break;
        }
        reset();
    }
    @Override
@@ -124,17 +202,17 @@
        timeList.add("20s");
        timeList.add("30s");
        int open = MainApp.theApp.sharedPreferencesUtil.getLoginInfo().getIs_open_upload();
        if(open == 0){
        if (open == 0) {
            sc_open.setChecked(false);
        }else{
        } else {
            sc_open.setChecked(true);
        }
        int refresh = MainApp.theApp.sharedPreferencesUtil.getLoginInfo().getRefresh_frequency();
        if(refresh == 10){
        if (refresh == 10) {
            refreshIndex = 0;
        }else if(refresh == 20){
        } else if (refresh == 20) {
            refreshIndex = 1;
        }else if(refresh == 30){
        } else if (refresh == 30) {
            refreshIndex = 2;
        }
    }
@@ -172,7 +250,7 @@
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                refreshIndex = options1;
                update("refresh_frequency",(options1+1)*10+"");
                update("refresh_frequency", (options1 + 1) * 10 + "");
            }
        }).setOutSideCancelable(true).build();
        sexView.setSelectOptions(refreshIndex);
@@ -182,6 +260,7 @@
    /**
     * 字段名称,目前仅支持修改['refresh_frequency', 'video', 'is_open_upload']
     *
     * @param field
     * @param value
     */
@@ -195,9 +274,9 @@
            @Override
            public void onSuccess(String res, String msg) {
                mToatUtils.showSingletonToast(msg);
                if("refresh_frequency".equals(field)){
                if ("refresh_frequency".equals(field)) {
                    MainApp.theApp.sharedPreferencesUtil.setRefreh(Integer.parseInt(value));
                }else if("is_open_upload".equals(field)){
                } else if ("is_open_upload".equals(field)) {
                    MainApp.theApp.sharedPreferencesUtil.setUpload(Integer.parseInt(value));
                }
                EventMessage event = new EventMessage();
@@ -215,7 +294,7 @@
            }
            @Override
            public void onFail(int errno,String s) {
            public void onFail(int errno, String s) {
                mToatUtils.showSingletonToast(s);
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
@@ -226,6 +305,7 @@
    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public String getVersion() {
@@ -241,6 +321,7 @@
    }
    private ArrayList<VoiceBean> voiceList = new ArrayList<>();
    private void reset() {
        voiceList.clear();
        VoiceBean voiceBean = new VoiceBean();
@@ -265,5 +346,6 @@
        EventMessage eventMessage = new EventMessage();
        eventMessage.setType(AppConfig.SET_VOICE);
        EventBus.getDefault().post(eventMessage);
        mToatUtils.showSingleLongToast("铃声设置成功");
    }
}