package com.fanxin.huangfangyi.main.activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.easemob.redpacketui.utils.RedPacketUtil;
import com.fanxin.huangfangyi.DemoApplication;
import com.fanxin.huangfangyi.DemoHelper;
import com.fanxin.huangfangyi.DemoModel;
import com.fanxin.huangfangyi.R;
import com.fanxin.huangfangyi.ui.BaseActivity;
import com.fanxin.huangfangyi.ui.BlacklistActivity;
import com.fanxin.huangfangyi.ui.DiagnoseActivity;
import com.fanxin.easeui.widget.EaseSwitchButton;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMOptions;
import com.hyphenate.util.EMLog;
/**
* Created by huangfangyi on 2016/7/4.\
* QQ:84543217
*/
public class SettingsActivity extends BaseActivity implements View.OnClickListener {
/**
* new message notification
*/
private RelativeLayout rl_switch_notification;
/**
* sound
*/
private RelativeLayout rl_switch_sound;
/**
* vibration
*/
private RelativeLayout rl_switch_vibrate;
/**
* speaker
*/
private RelativeLayout rl_switch_speaker;
/**
* line between sound and vibration
*/
private TextView textview1, textview2;
private LinearLayout blacklistContainer;
private LinearLayout userProfileContainer;
/**
* logout
*/
private Button logoutBtn;
private RelativeLayout rl_switch_chatroom_leave;
private RelativeLayout rl_switch_delete_msg_when_exit_group;
private RelativeLayout rl_switch_auto_accept_group_invitation;
private RelativeLayout rl_switch_adaptive_video_encode;
/**
* Diagnose
*/
private LinearLayout llDiagnose;
/**
* display name for APNs
*/
private LinearLayout pushNick;
private EaseSwitchButton notifiSwitch;
private EaseSwitchButton soundSwitch;
private EaseSwitchButton vibrateSwitch;
private EaseSwitchButton speakerSwitch;
private EaseSwitchButton ownerLeaveSwitch;
private EaseSwitchButton switch_delete_msg_when_exit_group;
private EaseSwitchButton switch_auto_accept_group_invitation;
private EaseSwitchButton switch_adaptive_video_encode;
private DemoModel settingsModel;
private EMOptions chatOptions;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.em_fragment_conversation_settings);
rl_switch_notification = (RelativeLayout) findViewById(R.id.rl_switch_notification);
rl_switch_sound = (RelativeLayout) findViewById(R.id.rl_switch_sound);
rl_switch_vibrate = (RelativeLayout) findViewById(R.id.rl_switch_vibrate);
rl_switch_speaker = (RelativeLayout) findViewById(R.id.rl_switch_speaker);
rl_switch_chatroom_leave = (RelativeLayout) findViewById(R.id.rl_switch_chatroom_owner_leave);
rl_switch_delete_msg_when_exit_group = (RelativeLayout) findViewById(R.id.rl_switch_delete_msg_when_exit_group);
rl_switch_auto_accept_group_invitation = (RelativeLayout) findViewById(R.id.rl_switch_auto_accept_group_invitation);
rl_switch_adaptive_video_encode = (RelativeLayout) findViewById(R.id.rl_switch_adaptive_video_encode);
notifiSwitch = (EaseSwitchButton) findViewById(R.id.switch_notification);
soundSwitch = (EaseSwitchButton) findViewById(R.id.switch_sound);
vibrateSwitch = (EaseSwitchButton) findViewById(R.id.switch_vibrate);
speakerSwitch = (EaseSwitchButton) findViewById(R.id.switch_speaker);
ownerLeaveSwitch = (EaseSwitchButton) findViewById(R.id.switch_owner_leave);
switch_delete_msg_when_exit_group = (EaseSwitchButton) findViewById(R.id.switch_delete_msg_when_exit_group);
switch_auto_accept_group_invitation = (EaseSwitchButton) findViewById(R.id.switch_auto_accept_group_invitation);
switch_adaptive_video_encode = (EaseSwitchButton) findViewById(R.id.switch_adaptive_video_encode);
LinearLayout llChange = (LinearLayout) findViewById(R.id.ll_change);
logoutBtn = (Button) findViewById(R.id.btn_logout);
if(!TextUtils.isEmpty(EMClient.getInstance().getCurrentUser())){
logoutBtn.setText(getString(R.string.button_logout) );
}
//"(" + EMClient.getInstance().getCurrentUser() + ")"
textview1 = (TextView) findViewById(R.id.textview1);
textview2 = (TextView) findViewById(R.id.textview2);
blacklistContainer = (LinearLayout) findViewById(R.id.ll_black_list);
userProfileContainer = (LinearLayout) findViewById(R.id.ll_user_profile);
llDiagnose=(LinearLayout) findViewById(R.id.ll_diagnose);
pushNick=(LinearLayout) findViewById(R.id.ll_set_push_nick);
settingsModel = DemoHelper.getInstance().getModel();
chatOptions = EMClient.getInstance().getOptions();
blacklistContainer.setOnClickListener(this);
userProfileContainer.setOnClickListener(this);
rl_switch_notification.setOnClickListener(this);
rl_switch_sound.setOnClickListener(this);
rl_switch_vibrate.setOnClickListener(this);
rl_switch_speaker.setOnClickListener(this);
logoutBtn.setOnClickListener(this);
llDiagnose.setOnClickListener(this);
pushNick.setOnClickListener(this);
rl_switch_chatroom_leave.setOnClickListener(this);
rl_switch_delete_msg_when_exit_group.setOnClickListener(this);
rl_switch_auto_accept_group_invitation.setOnClickListener(this);
rl_switch_adaptive_video_encode.setOnClickListener(this);
llChange.setOnClickListener(this);
// the vibrate and sound notification are allowed or not?
if (settingsModel.getSettingMsgNotification()) {
notifiSwitch.openSwitch();
} else {
notifiSwitch.closeSwitch();
}
// sound notification is switched on or not?
if (settingsModel.getSettingMsgSound()) {
soundSwitch.openSwitch();
} else {
soundSwitch.closeSwitch();
}
// vibrate notification is switched on or not?
if (settingsModel.getSettingMsgVibrate()) {
vibrateSwitch.openSwitch();
} else {
vibrateSwitch.closeSwitch();
}
// the speaker is switched on or not?
if (settingsModel.getSettingMsgSpeaker()) {
speakerSwitch.openSwitch();
} else {
speakerSwitch.closeSwitch();
}
// if allow owner leave
if(settingsModel.isChatroomOwnerLeaveAllowed()){
ownerLeaveSwitch.openSwitch();
}else{
ownerLeaveSwitch.closeSwitch();
}
// delete messages when exit group?
if(settingsModel.isDeleteMessagesAsExitGroup()){
switch_delete_msg_when_exit_group.openSwitch();
} else {
switch_delete_msg_when_exit_group.closeSwitch();
}
if (settingsModel.isAutoAcceptGroupInvitation()) {
switch_auto_accept_group_invitation.openSwitch();
} else {
switch_auto_accept_group_invitation.closeSwitch();
}
if (settingsModel.isAdaptiveVideoEncode()) {
switch_adaptive_video_encode.openSwitch();
EMClient.getInstance().callManager().getVideoCallHelper().setAdaptiveVideoFlag(true);
} else {
switch_adaptive_video_encode.closeSwitch();
EMClient.getInstance().callManager().getVideoCallHelper().setAdaptiveVideoFlag(false);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ll_change:
RedPacketUtil.startChangeActivity(this);
break;
case R.id.rl_switch_notification:
if (notifiSwitch.isSwitchOpen()) {
notifiSwitch.closeSwitch();
rl_switch_sound.setVisibility(View.GONE);
rl_switch_vibrate.setVisibility(View.GONE);
textview1.setVisibility(View.GONE);
textview2.setVisibility(View.GONE);
settingsModel.setSettingMsgNotification(false);
} else {
notifiSwitch.openSwitch();
rl_switch_sound.setVisibility(View.VISIBLE);
rl_switch_vibrate.setVisibility(View.VISIBLE);
textview1.setVisibility(View.VISIBLE);
textview2.setVisibility(View.VISIBLE);
settingsModel.setSettingMsgNotification(true);
}
break;
case R.id.rl_switch_sound:
if (soundSwitch.isSwitchOpen()) {
soundSwitch.closeSwitch();
settingsModel.setSettingMsgSound(false);
} else {
soundSwitch.openSwitch();
settingsModel.setSettingMsgSound(true);
}
break;
case R.id.rl_switch_vibrate:
if (vibrateSwitch.isSwitchOpen()) {
vibrateSwitch.closeSwitch();
settingsModel.setSettingMsgVibrate(false);
} else {
vibrateSwitch.openSwitch();
settingsModel.setSettingMsgVibrate(true);
}
break;
case R.id.rl_switch_speaker:
if (speakerSwitch.isSwitchOpen()) {
speakerSwitch.closeSwitch();
settingsModel.setSettingMsgSpeaker(false);
} else {
speakerSwitch.openSwitch();
settingsModel.setSettingMsgVibrate(true);
}
break;
case R.id.rl_switch_chatroom_owner_leave:
if(ownerLeaveSwitch.isSwitchOpen()){
ownerLeaveSwitch.closeSwitch();
settingsModel.allowChatroomOwnerLeave(false);
chatOptions.allowChatroomOwnerLeave(false);
}else{
ownerLeaveSwitch.openSwitch();
settingsModel.allowChatroomOwnerLeave(true);
chatOptions.allowChatroomOwnerLeave(true);
}
break;
case R.id.rl_switch_delete_msg_when_exit_group:
if(switch_delete_msg_when_exit_group.isSwitchOpen()){
switch_delete_msg_when_exit_group.closeSwitch();
settingsModel.setDeleteMessagesAsExitGroup(false);
chatOptions.setDeleteMessagesAsExitGroup(false);
}else{
switch_delete_msg_when_exit_group.openSwitch();
settingsModel.setDeleteMessagesAsExitGroup(true);
chatOptions.setDeleteMessagesAsExitGroup(true);
}
break;
case R.id.rl_switch_auto_accept_group_invitation:
if(switch_auto_accept_group_invitation.isSwitchOpen()){
switch_auto_accept_group_invitation.closeSwitch();
settingsModel.setAutoAcceptGroupInvitation(false);
chatOptions.setAutoAcceptGroupInvitation(false);
}else{
switch_auto_accept_group_invitation.openSwitch();
settingsModel.setAutoAcceptGroupInvitation(true);
chatOptions.setAutoAcceptGroupInvitation(true);
}
break;
case R.id.rl_switch_adaptive_video_encode:
EMLog.d("switch", "" + !switch_adaptive_video_encode.isSwitchOpen());
if (switch_adaptive_video_encode.isSwitchOpen()){
switch_adaptive_video_encode.closeSwitch();
settingsModel.setAdaptiveVideoEncode(false);
EMClient.getInstance().callManager().getVideoCallHelper().setAdaptiveVideoFlag(false);
}else{
switch_adaptive_video_encode.openSwitch();
settingsModel.setAdaptiveVideoEncode(true);
EMClient.getInstance().callManager().getVideoCallHelper().setAdaptiveVideoFlag(true);
}
break;
case R.id.btn_logout:
showDialog();
break;
case R.id.ll_black_list:
startActivity(new Intent(SettingsActivity.this, BlacklistActivity.class));
break;
case R.id.ll_diagnose:
startActivity(new Intent(SettingsActivity.this, DiagnoseActivity.class));
break;
case R.id.ll_set_push_nick:
// startActivity(new Intent(SettingsActivity.this, OfflinePushNickActivity.class));
break;
case R.id.ll_user_profile:
// startActivity(new Intent(SettingsActivity.this, UserProfileActivity.class).putExtra("setting", true)
// .putExtra("username", EMClient.getInstance().getCurrentUser()));
break;
default:
break;
}
}
void logout() {
final ProgressDialog pd = new ProgressDialog(SettingsActivity.this);
String st = getResources().getString(R.string.Are_logged_out);
pd.setMessage(st);
pd.setCanceledOnTouchOutside(false);
pd.show();
DemoHelper.getInstance().logout(false,new EMCallBack() {
@Override
public void onSuccess() {
runOnUiThread(new Runnable() {
public void run() {
pd.dismiss();
// show login scree
DemoApplication.getInstance().setUserJson(null);
DemoApplication.getInstance().finishActivities();
startActivity(new Intent(SettingsActivity.this, LoginActivity.class));
finish();
}
});
}
@Override
public void onProgress(int progress, String status) {
}
@Override
public void onError(int code, String message) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
pd.dismiss();
Toast.makeText(SettingsActivity.this, "unbind devicetokens failed", Toast.LENGTH_SHORT).show();
}
});
}
});
}
/**
* 这是兼容的 AlertDialog
*/
private void showDialog() {
/*
这里使用了 android.support.v7.app.AlertDialog.Builder
可以直接在头部写 import android.support.v7.app.AlertDialog
那么下面就可以写成 AlertDialog.Builder
*/
AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);
builder.setTitle("是否选择退出?");
builder.setMessage("退出后不会删除任何历史数据,下次登录依然可以使用本账号");
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
logout();
}
});
AlertDialog alertDialog= builder.create();
alertDialog.show();
}
}