admin
2021-07-20 27bd1f81221b8c8e8047118a64c2beb7bc214bbb
MGVideo/src/com/mugua/mgvideo/ui/mine/SettingsActivity.java
@@ -30,6 +30,7 @@
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;
@@ -40,291 +41,292 @@
import com.ysh.wpc.appupdate.AppUpdate;
public class SettingsActivity extends BaseActivity implements OnClickListener,
      OnCheckedChangeListener {
        OnCheckedChangeListener {
   private TextView tv_top_bar_left;
   private TextView tv_top_bar_middle;
   private TextView tv_version_name;// 版本号
    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 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 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());
    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);
   }
        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() {
    /**
     * 初始化内存显示
     */
    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);
         }
      }
   }
        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);
    @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);
        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 = (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);
        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);
        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);
        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);
      }
   }
        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 "";
      }
   }
    /*** 获取版本号 */
    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();
      }
      MobclickAgent.onPageStart("设置页");
   }
    @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();
        }
        MobclickAgent.onPageStart("设置页");
    }
   @Override
   public void onPause() {
      super.onPause();
      MobclickAgent.onPageEnd("设置页");
   }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("设置页");
    }
   @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", BeibeiConstant.HOST + "/new_video/"
               + "api_about_us.jsp?package=" + getPackageName());
         startActivity(intent);
      }
         break;
      default:
         break;
      }
   }
    @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");
               }
            }
    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;
   }
            }
        } 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
    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();
   }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        AppUpdate.destoryAppUpdate();
    }
}