haijiang
2018-09-19 82cc6c94fdc08b1814fbf7195e88cf335eed3e8d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package com.moral.yunfushao.fragment;
 
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.Button;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import com.bigkoo.pickerview.OptionsPickerView;
import com.lzy.okgo.cache.CacheMode;
import com.moral.andbrickslib.utils.ActivityManager;
import com.moral.andbrickslib.utils.FastJsonTools;
import com.moral.yunfushao.MainActivity;
import com.moral.yunfushao.MainApp;
import com.moral.yunfushao.R;
import com.moral.yunfushao.activity.LoginActivity;
import com.moral.yunfushao.activity.TestActivity;
import com.moral.yunfushao.activity.VoiceSetActivity;
import com.moral.yunfushao.base.BaseFragment;
import com.moral.yunfushao.common.API;
import com.moral.yunfushao.common.AppConfig;
import com.moral.yunfushao.httputils.HttpCallBack;
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;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 设置界面
 * Created by haijiang on 2017/7/4.
 */
 
public class FragmentSettings extends BaseFragment {
    private TextView tv_version;
    private RelativeLayout rl_setvoice,rl_refresh;
    private Button bt_sure;
    private Button bt_look;
    private SwitchCompat sc_open,sc_male;
    private int refreshIndex=0;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_setting_layout;
    }
 
    @Override
    protected void initViews() {
        rl_setvoice = findView(R.id.rl_setvoice);
        rl_refresh = findView(R.id.rl_refresh);
        bt_sure = findView(R.id.bt_sure);
        bt_look = findView(R.id.bt_look);
        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);
        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
    protected void initListener() {
        rl_setvoice.setOnClickListener(this);
        rl_refresh.setOnClickListener(this);
        bt_sure.setOnClickListener(this);
        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");
                }
            }
        });
        sc_male.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @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();
            }
        });
 
        bt_look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  Intent intent = new Intent(getActivity(), TestActivity.class);
                  startActivity(intent);
            }
        });
    }
 
    @Override
    protected void initData() {
        timeList.add("10s");
        timeList.add("20s");
        timeList.add("30s");
        int open = MainApp.theApp.sharedPreferencesUtil.getLoginInfo().getIs_open_upload();
        if(open == 0){
            sc_open.setChecked(false);
        }else{
            sc_open.setChecked(true);
        }
        int refresh = MainApp.theApp.sharedPreferencesUtil.getLoginInfo().getRefresh_frequency();
        if(refresh == 10){
            refreshIndex = 0;
        }else if(refresh == 20){
            refreshIndex = 1;
        }else if(refresh == 30){
            refreshIndex = 2;
        }
    }
 
    @Override
    protected void processClick(View v) {
        switch (v.getId()) {
            case R.id.rl_setvoice:
                Intent intent = new Intent(getActivity(), VoiceSetActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_refresh:
                initDialog();
                break;
            case R.id.bt_sure:
                MainApp.theApp.sharedPreferencesUtil.exitLogin();
                Intent intent1 = new Intent();
                intent1.setClass(getActivity(), LoginActivity.class);
                startActivity(intent1);
                ActivityManager.getActivityManager().finishActivity(MainActivity.class);
                break;
        }
 
    }
 
    @Override
    protected void onErrorPagerClick() {
 
    }
 
    private ArrayList<String> timeList = new ArrayList<>();
 
    private void initDialog() {
        OptionsPickerView<String> sexView = new OptionsPickerView.Builder(getActivity(), new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                refreshIndex = options1;
                update("refresh_frequency",(options1+1)*10+"");
            }
        }).setOutSideCancelable(true).build();
        sexView.setSelectOptions(refreshIndex);
        sexView.setPicker(timeList);
        sexView.show();
    }
 
    /**
     * 字段名称,目前仅支持修改['refresh_frequency', 'video', 'is_open_upload']
     * @param field
     * @param value
     */
    private void update(final String field, final String value) {
        Map<String, String> params = new HashMap<>();
        params.put("user_id", MainApp.userId);
        params.put("field", field);
        params.put("value", value);
        String url = API.UPDATEUSERINFO;
        HttpUtils.doPost(url, params, CacheMode.DEFAULT, true, new HttpCallBack() {
            @Override
            public void onSuccess(String res, String msg) {
                mToatUtils.showSingletonToast(msg);
                if("refresh_frequency".equals(field)){
                    MainApp.theApp.sharedPreferencesUtil.setRefreh(Integer.parseInt(value));
                }else if("is_open_upload".equals(field)){
                    MainApp.theApp.sharedPreferencesUtil.setUpload(Integer.parseInt(value));
                }
                EventMessage event = new EventMessage();
                event.setType(AppConfig.CHANGE_SETTING);
                EventBus.getDefault().post(event);
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            }
 
            @Override
            public void showLoadingDialog() {
                progressDialog.setTitleText("提交中...");
                progressDialog.show();
            }
 
            @Override
            public void onFail(int errno,String s) {
                mToatUtils.showSingletonToast(s);
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            }
        });
    }
 
    /**
     * 获取版本号
     * @return 当前应用的版本号
     */
    public String getVersion() {
        try {
            PackageManager manager = getActivity().getPackageManager();
            PackageInfo info = manager.getPackageInfo(getActivity().getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "1.0";
        }
    }
 
    private ArrayList<VoiceBean> voiceList = new ArrayList<>();
    private void reset() {
        voiceList.clear();
        VoiceBean voiceBean = new VoiceBean();
        voiceBean.setId(0);
        voiceBean.setRes(AppConfig.voideUrl1);
        voiceBean.setName("建议回避铃声");
        voiceBean.setVoiceName("默认");
        voiceList.add(voiceBean);
        voiceBean = new VoiceBean();
        voiceBean.setId(1);
        voiceBean.setRes(AppConfig.voideUrl2);
        voiceBean.setName("及时闪躲铃声");
        voiceBean.setVoiceName("默认");
        voiceList.add(voiceBean);
        voiceBean = new VoiceBean();
        voiceBean.setId(2);
        voiceBean.setRes(AppConfig.voideUrl3);
        voiceBean.setName("紧急撤离铃声");
        voiceBean.setVoiceName("默认");
        voiceList.add(voiceBean);
        MainApp.theApp.sharedPreferencesUtil.saveVideoList(FastJsonTools.toJson(voiceList));
        EventMessage eventMessage = new EventMessage();
        eventMessage.setType(AppConfig.SET_VOICE);
        EventBus.getDefault().post(eventMessage);
    }
}