wpc
2018-11-27 c52fb0e4d9168e75390b3cf3536d66c06c50d605
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
package com.haicaojie.android.ui.mine;
 
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback;
import com.bumptech.glide.Glide;
import com.haicaojie.android.BuXinConstant;
import com.haicaojie.android.R;
import com.haicaojie.android.ShoppingApi;
import com.haicaojie.android.ui.BaseActivity;
import com.lcjian.library.util.DataCleanManager;
import com.lcjian.library.util.SingleToast;
import com.lcjian.library.util.SystemCommon;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.ysh.wpc.appupdate.AppUpdate;
 
import java.util.Map;
 
/**
 * Created by weikou2015 on 2017/2/21.
 * 设置
 */
 
public class SettingActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_left;
    private TextView tv_middle;
    private TextView tv_version;
    private TextView tv_cache_num;
    private CheckBox cb_msg;
    private LinearLayout ll_clear_cache;
    private LinearLayout ll_check_update;
    private LinearLayout ll_exit_account;
    private LinearLayout ll_person_info;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
          /*
         * 计算状态栏高度并设置
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            int result = 0;
            int resourceId = getResources().getIdentifier("status_bar_height",
                    "dimen", "android");
            if (resourceId > 0) {
                result = getResources().getDimensionPixelSize(resourceId);
            }
            //设置状态栏文字颜色及图标为深色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    result);
            findViewById(R.id.v_status_bar).setLayoutParams(params);
        } else {
            findViewById(R.id.v_status_bar).setVisibility(View.GONE);
        }
        tv_left = (TextView) findViewById(R.id.tv_top_bar_left);
        tv_middle = (TextView) findViewById(R.id.tv_top_bar_middle);
        tv_cache_num = (TextView) findViewById(R.id.tv_cache_num);
        tv_version = (TextView) findViewById(R.id.tv_version);
        cb_msg = (CheckBox) findViewById(R.id.cb_msg);
        ll_clear_cache = (LinearLayout) findViewById(R.id.ll_clear_cache);
        ll_check_update = (LinearLayout) findViewById(R.id.ll_check_update);
        ll_exit_account = (LinearLayout) findViewById(R.id.ll_exit_account);
        ll_person_info = (LinearLayout) findViewById(R.id.ll_person_info);
        final SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        if (!sp.getBoolean("isLogin", false)) {
            ll_exit_account.setVisibility(View.GONE);
        } else {
            ll_exit_account.setVisibility(View.VISIBLE);
        }
//        tv_left.setText("返回");
        tv_middle.setText("设置");
        String cache = "";
        try {
            cache = DataCleanManager.getOutsideCacheSize(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        tv_cache_num.setText(cache);
        tv_version.setText("版本号:" + SystemCommon.getVersonName(SettingActivity.this));
        tv_left.setOnClickListener(this);
        ll_clear_cache.setOnClickListener(this);
        ll_check_update.setOnClickListener(this);
        ll_exit_account.setOnClickListener(this);
        ll_person_info.setOnClickListener(this);
 
        cb_msg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor = sp.edit();
                if (isChecked) {
                    editor.putBoolean("doNotDisturb", true);
//                    if (MobileUtil.getDeviceBrand().equalsIgnoreCase("HUAWEI") || MobileUtil.getDeviceBrand().equalsIgnoreCase("HONOR")) {
//                        HMSAgent.Push.enableReceiveNormalMsg(false, null);
//                        HMSAgent.Push.enableReceiveNotifyMsg(false, null);
//                    } else {
                    MiPushClient.unregisterPush(SettingActivity.this);
//                    }
                } else {
                    editor.putBoolean("doNotDisturb", false);
//                    if (MobileUtil.getDeviceBrand().equalsIgnoreCase("HUAWEI") || MobileUtil.getDeviceBrand().equalsIgnoreCase("HONOR")) {
//                        HMSAgent.Push.enableReceiveNormalMsg(true, null);
//                        HMSAgent.Push.enableReceiveNotifyMsg(true, null);
//                    } else {
                    MiPushClient.registerPush(SettingActivity.this, BuXinConstant.MIPUSH_ID, BuXinConstant.MIPUSH_KEY);
//                    }
                }
                editor.commit();
            }
        });
        if (sp.getBoolean("doNotDisturb", false)) {
            cb_msg.setChecked(true);
        } else {
            cb_msg.setChecked(false);
        }
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_top_bar_left:
                finish();
                break;
            case R.id.ll_clear_cache:
                ImageLoader.getInstance().clearDiskCache();
                ImageLoader.getInstance().clearMemoryCache();
                DataCleanManager.clearAllCache(SettingActivity.this, tv_cache_num);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.get(SettingActivity.this).clearMemory();
                        Glide.get(SettingActivity.this).clearDiskCache();
                    }
                });
                break;
            case R.id.ll_check_update:
                //自动更新
                AppUpdate.setAppUpdateActivity(this);
                AppUpdate.setAppUpdateKey(BuXinConstant.APP_UPDATE_KEY);
                AppUpdate.initAppUpdate(true);
                break;
            case R.id.ll_exit_account:
                if (getSharedPreferences("user", MODE_PRIVATE).getBoolean("isLogin", false)) {
                    tbLoginOut();
                    loginOut();
//                    boolean isauth = UMShareAPI.get(SettingActivity.this).isAuthorize(SettingActivity.this, SHARE_MEDIA.WEIXIN);
//                    if (isauth)
//                        UMShareAPI.get(SettingActivity.this).deleteOauth(SettingActivity.this, SHARE_MEDIA.WEIXIN, authListener);
                    SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
                    MiPushClient.unsetAlias(SettingActivity.this, sp.getString("uid", "0"), null);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putBoolean("isLogin", false);
                    editor.putBoolean("isTBBind", false);
                    editor.putBoolean("isWxBind", false);
//                    editor.putBoolean("haveHongbao", false);
                    editor.putString("uid", "0");
                    editor.commit();
                    ll_exit_account.setVisibility(View.GONE);
                    SingleToast.showToast(SettingActivity.this, "退出登录成功");
                }
                break;
            case R.id.ll_person_info:
                if (getSharedPreferences("user", MODE_PRIVATE).getBoolean("isLogin", false)) {
                    startActivity(new Intent(SettingActivity.this, MyInfoActivity.class));
                } else {
                    SingleToast.showToast(SettingActivity.this, "登录后才能查看信息!");
                    startActivity(new Intent(SettingActivity.this, LoginSelectActivity.class));
                }
                break;
        }
    }
 
 
    private void loginOut() {
        ShoppingApi.loginOut(this, getSharedPreferences("user", MODE_PRIVATE).getString("uid", "0"), new JsonHttpResponseHandler());
    }
 
    /**
     * 退出登录
     */
    private void tbLoginOut() {
        AlibcLogin alibcLogin = AlibcLogin.getInstance();
 
        alibcLogin.logout(new AlibcLoginCallback() {
            @Override
            public void onSuccess(int i) {
                SingleToast.showToast(SettingActivity.this, "退出登录成功");
                SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("isLogin", false);
                editor.putString("uid", "0");
//                editor.putBoolean("haveHongbao", false);
                editor.commit();
                ll_exit_account.setVisibility(View.GONE);
            }
 
            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(SettingActivity.this, "退出登录失败 " + code + msg,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppUpdate.destoryAppUpdate();
    }
 
    UMAuthListener authListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
//            SocializeUtils.safeShowDialog(dialog);
        }
 
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
//            SocializeUtils.safeCloseDialog(dialog);
//            Toast.makeText(SettingActivity.this, "成功了", Toast.LENGTH_LONG).show();
        }
 
        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
//            SocializeUtils.safeCloseDialog(dialog);
//            Toast.makeText(SettingActivity.this, "失败:" + t.getMessage(), Toast.LENGTH_LONG).show();
        }
 
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
//            SocializeUtils.safeCloseDialog(dialog);
//            Toast.makeText(SettingActivity.this, "取消了", Toast.LENGTH_LONG).show();
        }
    };
}