admin
2021-10-21 7e2ac66d7c532a5725635fa3913789bb17c1e157
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package com.mugua.mgvideo.ui.mine;
 
import java.util.HashMap;
import java.util.Map;
 
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.bumptech.glide.Glide;
import com.lcjian.library.util.common.StringUtils;
import com.mugua.mgvideo.R;
import com.mugua.mgvideo.util.ConfigUtil;
import com.umeng.analytics.MobclickAgent;
import com.mugua.mgvideo.entity.SDCardEntity;
import com.mugua.mgvideo.ui.BaseActivity;
import com.mugua.mgvideo.ui.main.MineFragment;
import com.yeshi.base.utils.BeibeiConstant;
import com.mugua.mgvideo.util.SDCardUtil;
import com.mugua.mgvideo.widget.BadgeView;
import com.ysh.wpc.appupdate.AppUpdate;
 
public class SettingsActivity extends BaseActivity implements OnClickListener,
        OnCheckedChangeListener {
 
    private TextView tv_top_bar_left;
    private TextView tv_top_bar_middle;
    private TextView tv_version_name;// 版本号
 
    private CheckBox chb_only_wifi_download;
    private RelativeLayout rl_clear_cache;
    private RelativeLayout rl_about_us;
    private RelativeLayout rl_check_update;// 检查更新
    private RelativeLayout rl_disclaimer;
 
    private Button btn_exit;// 离开按钮
    private View view_sdcard;
    private TextView tv_mobile;
    private TextView tv_sdcard;
    private CheckBox cb_mobile;
    private CheckBox cb_sdcard;
 
    private void chageSize(TextView tv, int start, int end) {
        SpannableStringBuilder spanBuilder = new SpannableStringBuilder(tv
                .getText().toString());
 
        spanBuilder.setSpan(new TextAppearanceSpan(null, 0, getResources()
                        .getDimensionPixelSize(R.dimen.alphabet_size), null, null),
                start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        tv.setText(spanBuilder);
    }
 
    /**
     * 初始化内存显示
     */
    private void initStorageView() {
 
        tv_mobile.setText("手机存储: 总量:"
                + SDCardUtil.getSotrageSize(SDCardUtil
                .getTotalExternalMemorySize() / (1024 * 1024))
                + "   可用:"
                + SDCardUtil.getSotrageSize(SDCardUtil
                .getAvailableExternalMemorySize() / (1024 * 1024)));
        chageSize(tv_mobile, 5, tv_mobile.getText().length());
        SDCardEntity entity = SDCardUtil.getSDCardPath(this);
        if (entity == null) {// 没有外部存储卡
            view_sdcard.setVisibility(View.GONE);
            cb_mobile.setVisibility(View.GONE);
        } else {
            tv_sdcard.setText("SD卡存储: 总量:"
                    + SDCardUtil.getSotrageSize(entity.getTotalSize()
                    / (1024 * 1024))
                    + "   可用:"
                    + SDCardUtil.getSotrageSize(entity.getAvailableSize()
                    / (1024 * 1024)));
            chageSize(tv_sdcard, 6, tv_sdcard.getText().length());
            view_sdcard.setVisibility(View.VISIBLE);
            cb_mobile.setVisibility(View.VISIBLE);
            if (SDCardUtil.getDeaultStorage(this) == SDCardUtil.STORAGE_SDCARD) {
                cb_sdcard.setChecked(true);
                cb_mobile.setChecked(false);
            } else {
                cb_sdcard.setChecked(false);
                cb_mobile.setChecked(true);
            }
        }
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
 
        view_sdcard = findViewById(R.id.rl_storage_sdcard);
        tv_mobile = (TextView) findViewById(R.id.tv_storage_mobile);
        tv_sdcard = (TextView) findViewById(R.id.tv_storage_sdcard);
        cb_mobile = (CheckBox) findViewById(R.id.cb_storage_mobile);
        cb_sdcard = (CheckBox) findViewById(R.id.cb_storage_sdcard);
        cb_mobile.setOnCheckedChangeListener(this);
        cb_sdcard.setOnCheckedChangeListener(this);
 
        tv_top_bar_left = (TextView) findViewById(R.id.tv_top_bar_left);
        tv_top_bar_middle = (TextView) findViewById(R.id.tv_top_bar_middle);
        tv_top_bar_left.setText("返回");
        tv_top_bar_middle.setText("设置");
 
        tv_top_bar_left.setOnClickListener(this);
 
        chb_only_wifi_download = (CheckBox) findViewById(R.id.chb_only_wifi_download);
        rl_clear_cache = (RelativeLayout) findViewById(R.id.rl_clear_cache);
        rl_disclaimer = (RelativeLayout) findViewById(R.id.rl_disclaimer);
        rl_about_us = (RelativeLayout) findViewById(R.id.rl_about_us);
        rl_check_update = (RelativeLayout) findViewById(R.id.rl_check_update);
        tv_version_name = (TextView) findViewById(R.id.tv_version_name);
        btn_exit = (Button) findViewById(R.id.setting_btn_exit);
 
        btn_exit.setOnClickListener(this);
        rl_clear_cache.setOnClickListener(this);
        rl_disclaimer.setOnClickListener(this);
        rl_about_us.setOnClickListener(this);
        rl_check_update.setOnClickListener(this);
 
        final SharedPreferences settings = getSharedPreferences("settings",
                Context.MODE_PRIVATE);
        chb_only_wifi_download.setChecked(settings.getBoolean(
                "only_wifi_download", true));
        chb_only_wifi_download
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        settings.edit()
                                .putBoolean("only_wifi_download", isChecked)
                                .commit();
                    }
                });
        initStorageView();
        tv_version_name.setText("版本号:" + getVersionName(this));
        // 离开按钮是否显示
        if (StringUtils.isEmpty(getSharedPreferences("user",
                Context.MODE_PRIVATE).getString("LoginUid", ""))) {// 如果为空字符
            btn_exit.setVisibility(View.GONE);
        }
    }
 
    /*** 获取版本号 */
    public static String getVersionName(Context context) {
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
            return pi.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }
 
    @Override
    public void onResume() {
        super.onResume();
        BadgeView mBadge = new BadgeView(this, tv_version_name);
        mBadge.setBackgroundResource(R.drawable.ic_red_dot);
        mBadge.setTextSize(1);
        mBadge.setText("");
        SharedPreferences preferences = this.getSharedPreferences("update",
                Context.MODE_PRIVATE);
        boolean mStatus = preferences.getBoolean("status", false);
        if (mStatus) {
            mBadge.show();
        } else {
            mBadge.hide();
        }
    }
 
    @Override
    public void onPause() {
        super.onPause();
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_btn_exit:// 点击离开
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("确定要退出登录吗?")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        Editor edit = getSharedPreferences("user",
                                                Context.MODE_PRIVATE).edit();
                                        edit.putString("LoginUid", "");
                                        edit.commit();
                                        MineFragment.isLogin = false;// 改为未登录状态
                                        finish();
                                    }
                                });
                builder.create().show();
                break;
            case R.id.rl_check_update:// 点击检查更新
                AppUpdate.setAppUpdateActivity(this);
                Map<String, String> map = new HashMap<>();
                map.put("360", "6c8349cc7260ae62e3b1396831a8398f");
                map.put("QQ", "d9d4f495e875a2e075a1a4a6e1b9770f");
                map.put("GuanWang", "a5771bce93e200c36f7cd9dfd0e5deaa");
                if ("360".equalsIgnoreCase(getChannelName())) {
                    AppUpdate.setAppUpdateKey(map.get("360"));
                } else if ("QQ".equalsIgnoreCase(getChannelName())) {
                    AppUpdate.setAppUpdateKey(map.get("QQ"));
                } else {
                    AppUpdate.setAppUpdateKey(map.get("GuanWang"));
                }
                AppUpdate.initAppUpdate();
                break;
            case R.id.tv_top_bar_left: {
                finish();
            }
            break;
            case R.id.rl_clear_cache: {
                AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
                builder2.setMessage("确定清空图片缓存?")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        new Thread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Glide.get(SettingsActivity.this)
                                                        .clearDiskCache();
                                            }
                                        }).start();
                                        // ImageLoader.getInstance().clearDiskCache();
                                        Toast.makeText(SettingsActivity.this,
                                                "缓存已清空", Toast.LENGTH_SHORT).show();
                                    }
                                });
                builder2.create().show();
            }
            break;
            case R.id.rl_disclaimer: {
                Intent intent = new Intent(this, BrowserActivity.class);
                intent.putExtra("url", BeibeiConstant.HOST + "/new_video/"
                        + "api_disclaimer.jsp?package=" + getPackageName());
                startActivity(intent);
            }
            break;
            case R.id.rl_about_us: {
                Intent intent = new Intent(this, BrowserActivity.class);
                intent.putExtra("url", getString(R.string.about_us_url));
                startActivity(intent);
            }
            break;
            default:
                break;
        }
    }
 
    private String getChannelName() {
        String channelName = null;
        try {
            PackageManager packageManager = getPackageManager();
            if (packageManager != null) {
                // 注意此处为ApplicationInfo 而不是
                // ActivityInfo,因为友盟设置的meta-data是在application标签中,而不是某activity标签中,所以用ApplicationInfo
                ApplicationInfo applicationInfo = packageManager
                        .getApplicationInfo(
                                SettingsActivity.this.getPackageName(),
                                PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        channelName = applicationInfo.metaData
                                .getString("UMENG_CHANNEL");
                    }
                }
 
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return channelName;
    }
 
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getId() == R.id.cb_storage_mobile) {
            if (isChecked) {
                SDCardUtil.setDeaultStorage(this, SDCardUtil.STORAGE_MOBILE);
                cb_sdcard.setChecked(false);
            }
        } else {
            if (isChecked) {
                SDCardUtil.setDeaultStorage(this, SDCardUtil.STORAGE_SDCARD);
                cb_mobile.setChecked(false);
            }
        }
 
    }
 
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        AppUpdate.destoryAppUpdate();
    }
}