haijiang
2018-05-10 800cf637391b4b237149907ef3b3323966d971da
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package com.moral.yunfushao.activity;
 
import android.content.Intent;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
 
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.lzy.okgo.cache.CacheMode;
import com.moral.andbrickslib.baseadapter.headandfooter.DividerItemDecoration;
import com.moral.andbrickslib.baseadapter.recyclerview.MultiItemTypeAdapter;
import com.moral.andbrickslib.utils.FastJsonTools;
import com.moral.andbrickslib.utils.dialog.MaterialDialogUtils;
import com.moral.yunfushao.MainApp;
import com.moral.yunfushao.R;
import com.moral.yunfushao.adapter.VoiceAdapter;
import com.moral.yunfushao.base.BaseListActivity;
import com.moral.yunfushao.ble.BLECommon;
import com.moral.yunfushao.common.API;
import com.moral.yunfushao.httputils.HttpCallBack;
import com.moral.yunfushao.httputils.HttpUtils;
import com.moral.yunfushao.model.VoiceBean;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
 
/**
 * Created by haijiang on 2017/7/12.
 */
 
public class VoiceSetActivity extends BaseListActivity implements VoiceAdapter.OnBtnClickListener {
    private VoiceAdapter adapter;
    private ArrayList<VoiceBean> voiceList = new ArrayList<>();
    AssetManager am;
    MediaPlayer mp;
    public static String voideUrl1 = "http://yfsapi.7drlb.com/video/music.mp3";
    public static String voideUrl2 = "http://yfsapi.7drlb.com/video/music1.mp3";
    public static String voideUrl3 = "http://yfsapi.7drlb.com/video/music2.mp3";
    private MaterialDialog resetDialog;
 
    private void showReset() {
        resetDialog = MaterialDialogUtils.showCallBaclDialog(this, "温馨提示", "是否重置为默认铃声?", new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                reset();
                resetDialog.dismiss();
            }
        }, new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                resetDialog.dismiss();
            }
        });
        resetDialog.show();
    }
 
    @Override
    protected void initOtherView() {
        tv_title.setText("设置铃声");
        tv_right.setText("重置");
        DividerItemDecoration line = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
        mRecyclerView.addItemDecoration(line);
        adapter = new VoiceAdapter(mRecyclerView, R.layout.list_item_voice, voiceList);
        adapter.setOnBtnClickListener(this);
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
 
            }
 
            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
 
 
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 111) {
            if (resultCode == RESULT_OK && data != null) {
                int index = data.getIntExtra("position", 0);
                String url = data.getStringExtra("url");
                String name = data.getStringExtra("name");
                voiceList.get(index).setDefaultMusic(false);
                voiceList.get(index).setSetMusic(url);
                voiceList.get(index).setVoiceName(name);
                MainApp.theApp.sharedPreferencesUtil.saveVideoList(FastJsonTools.toJson(voiceList));
                adapter.notifyDataSetChanged();
                mToatUtils.showSingleLongToast("铃声设置成功");
            }
        }
    }
 
    @Override
    protected void onLoadMore() {
 
 
    }
 
    @Override
    protected void onRefresh() {
 
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mp != null && mp.isPlaying()) {
            mp.stop();
            mp.release();
        }
    }
 
    @Override
    protected void getBundleExtras(Bundle extras) {
 
    }
 
    @Override
    protected void initListener() {
 
    }
 
    @Override
    protected void initData() {
        setNormalView();
        ArrayList<VoiceBean> temp = (ArrayList<VoiceBean>) FastJsonTools.getArrayJson(MainApp.theApp.sharedPreferencesUtil.getVideoList(), VoiceBean.class);
        if (temp != null) {
            voiceList.addAll(temp);
            adapter.notifyDataSetChanged();
        } else {
            reset();
        }
    }
 
    @Override
    protected void processClick(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right:
                showReset();
                break;
        }
 
    }
 
    @Override
    protected void onErrorPageClick() {
 
    }
 
    @Override
    public void playClick(int position) {
        VoiceBean voiceBean = voiceList.get(position);
        if (voiceBean.isPlay()) {
            mp.stop();
            voiceBean.setPlay(false);
        } else {
            if (mp != null && mp.isPlaying()) {
                mp.stop();
                mp.release();
                for (VoiceBean voice : voiceList) {
                    voice.setPlay(false);
                }
                adapter.notifyDataSetChanged();
            }
            if (voiceBean.isDefaultMusic()) {
                mp = MediaPlayer.create(this, Uri.parse(voiceBean.getRes()));
                mp.start();
            } else {
                mp = new MediaPlayer();
                try {
                    mp.setDataSource(voiceBean.getSetMusic());
                    mp.prepare();
                    mp.start();
                    voiceBean.setPlay(true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
 
            voiceBean.setPlay(true);
        }
        adapter.notifyDataSetChanged();
    }
 
    @Override
    public void setClick(int position) {
        Intent intent = new Intent(VoiceSetActivity.this, ChooseSongActivity.class);
        intent.putExtra("position", position);
        startActivityForResult(intent, 111);
    }
 
    /**
     * 字段名称,目前仅支持修改['refresh_frequency', 'video', 'is_open_upload']
     *
     * @param field
     * @param value
     */
    private void update(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);
                MainApp.theApp.sharedPreferencesUtil.setVideo(Integer.parseInt(value));
                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();
                }
            }
        });
    }
 
    private void reset() {
        voiceList.clear();
        VoiceBean voiceBean = new VoiceBean();
        voiceBean.setId(0);
        voiceBean.setRes(VoiceSetActivity.voideUrl1);
        voiceBean.setName("建议回避铃声");
        voiceBean.setVoiceName("默认");
        voiceList.add(voiceBean);
        voiceBean = new VoiceBean();
        voiceBean.setId(1);
        voiceBean.setRes(VoiceSetActivity.voideUrl2);
        voiceBean.setName("及时闪躲铃声");
        voiceBean.setVoiceName("默认");
        voiceList.add(voiceBean);
        voiceBean = new VoiceBean();
        voiceBean.setId(2);
        voiceBean.setRes(VoiceSetActivity.voideUrl3);
        voiceBean.setName("紧急撤离铃声");
        voiceBean.setVoiceName("默认");
        voiceList.add(voiceBean);
        MainApp.theApp.sharedPreferencesUtil.saveVideoList(FastJsonTools.toJson(voiceList));
        adapter.notifyDataSetChanged();
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        if (mp != null && mp.isPlaying()) {
            mp.stop();
            mp.release();
            for (VoiceBean voice : voiceList) {
                voice.setPlay(false);
            }
            adapter.notifyDataSetChanged();
            mp = null;
        }
    }
}