package com.mugua.mgvideo.ui.main;
|
|
import android.Manifest;
|
import android.app.ProgressDialog;
|
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.ActivityInfo;
|
import android.content.pm.ApplicationInfo;
|
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager.NameNotFoundException;
|
import android.database.Cursor;
|
import android.net.wifi.WifiInfo;
|
import android.net.wifi.WifiManager;
|
import android.os.Build;
|
import android.os.Bundle;
|
|
import androidx.loader.app.LoaderManager;
|
import androidx.loader.content.CursorLoader;
|
import androidx.loader.content.Loader;
|
|
import android.telephony.TelephonyManager;
|
import android.view.KeyEvent;
|
import android.view.View;
|
import android.widget.RadioButton;
|
import android.widget.RadioGroup;
|
import android.widget.RadioGroup.OnCheckedChangeListener;
|
|
import com.lcjian.library.DeviceUuidFactory;
|
import com.lcjian.library.util.FragmentSwitchHelper;
|
import com.lcjian.library.util.common.DimenUtils;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.mozillaonline.providers.downloads.DownloadService;
|
import com.mugua.mgvideo.R;
|
import com.mugua.mgvideo.entity.recommend.HomeClass;
|
import com.umeng.socialize.UMShareAPI;
|
import com.yeshi.base.utils.YouthUtil;
|
import com.yeshi.base.utils.http.BasicTextHttpResponseHandler;
|
import com.mugua.mgvideo.MGVideoAPI;
|
import com.mugua.mgvideo.MGVideoApplication;
|
import com.mugua.mgvideo.db.MessageTable;
|
import com.mugua.mgvideo.entity.AdTag;
|
import com.mugua.mgvideo.entity.NewComment;
|
import com.mugua.mgvideo.service.HelpService;
|
import com.mugua.mgvideo.ui.BaseActivity;
|
import com.yeshi.base.utils.BeibeiConstant;
|
import com.mugua.mgvideo.util.ExitDialog;
|
import com.mugua.mgvideo.util.LoginFirstDialog;
|
import com.mugua.mgvideo.widget.BadgeView;
|
import com.ysh.wpc.appupdate.AppUpdate;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.io.BufferedReader;
|
import java.io.InputStreamReader;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
import de.greenrobot.event.EventBus;
|
|
public class MainActivity extends BaseActivity implements
|
LoaderManager.LoaderCallbacks<Cursor> {
|
|
private RadioGroup rg_nav;
|
|
private BadgeView mBadgeView;
|
|
private FragmentSwitchHelper mFragmentSwitchHelper;
|
|
private ProgressDialog pd = null;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 锁定竖屏
|
// mPushAgent.enable();//测试
|
|
setContentView(R.layout.activity_main);
|
|
// 将imei mac lat lng存入sharedpreference中
|
SharedPreferences mPre = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
WifiInfo info = wifi.getConnectionInfo();
|
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
|
String imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getDeviceId();
|
Editor editor = mPre.edit();
|
editor.putString("imei", imei);
|
if (StringUtils.isEmpty(getMacAddress())) {
|
editor.putString("mac", info.getMacAddress());
|
} else {
|
editor.putString("mac", getMacAddress());
|
}
|
editor.apply();
|
}
|
|
pd = new ProgressDialog(this);
|
pd.setMessage("正在请求网络...");
|
pd.setCanceledOnTouchOutside(false);
|
|
HomeClass homeClass = new HomeClass();
|
homeClass.setDataKey("recommend");
|
homeClass.setId("1626144258979");
|
homeClass.setDataType("recommend");
|
mFragmentSwitchHelper = FragmentSwitchHelper.create(
|
R.id.fl_main_fragment_container, getSupportFragmentManager(),
|
RecommendNewFragment.newInstance(homeClass, 0), BaiDuVideosFragment.newInstance(), new CategoryFragment(),
|
new ShopFragment(), new MineFragment());
|
rg_nav = findViewById(R.id.rg_nav);
|
rg_nav.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
|
@Override
|
public void onCheckedChanged(RadioGroup group, int checkedId) {
|
switch (checkedId) {
|
case R.id.rb_nav_recommend: {
|
mFragmentSwitchHelper
|
.changeFragment(RecommendNewFragment.class);
|
}
|
break;
|
case R.id.rb_nav_short_video:
|
mFragmentSwitchHelper
|
.changeFragment(BaiDuVideosFragment.class);
|
break;
|
case R.id.rb_nav_category: {
|
mFragmentSwitchHelper
|
.changeFragment(CategoryFragment.class);
|
}
|
|
break;
|
case R.id.rb_shop: {
|
mFragmentSwitchHelper.changeFragment(ShopFragment.class);
|
}
|
break;
|
case R.id.rb_nav_mine: {
|
mFragmentSwitchHelper.changeFragment(MineFragment.class);
|
}
|
break;
|
}
|
}
|
});
|
mBadgeView = new BadgeView(this, rg_nav);
|
mBadgeView.setBackgroundResource(R.drawable.ic_red_dot);
|
mBadgeView.setTextSize(1);
|
mBadgeView.setText("");
|
mBadgeView.setBadgeMargin(DimenUtils.dipToPixels(24, this),
|
DimenUtils.dipToPixels(10, this));
|
|
getSupportLoaderManager().initLoader(0, null, this);
|
getUid();
|
|
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();
|
}
|
|
private String getChannelName() {
|
String channelName = null;
|
try {
|
PackageManager packageManager = getPackageManager();
|
if (packageManager != null) {
|
// 注意此处为ApplicationInfo 而不是
|
// ActivityInfo,因为友盟设置的meta-data是在application标签中,而不是某activity标签中,所以用ApplicationInfo
|
ApplicationInfo applicationInfo = packageManager
|
.getApplicationInfo(MainActivity.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;
|
}
|
|
/**
|
* 判断此用户是否有新消息
|
*/
|
private void getNewInfo() {
|
SharedPreferences preferences = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
if (StringUtils.isEmpty(preferences.getString("LoginUid", ""))) {
|
return;
|
}
|
MGVideoAPI.getReadState(MainActivity.this,
|
preferences.getString("LoginUid", ""),
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.optBoolean("IsPost")) {
|
boolean isCheck = jsonObject.optJSONObject("Data")
|
.optBoolean("Read");
|
SharedPreferences preferences = getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
Editor editor = preferences.edit();
|
editor.putBoolean("isCheckedCom", isCheck);
|
editor.commit();
|
if (isCheck) {
|
mBadgeView.show();
|
} else {
|
mBadgeView.hide();
|
}
|
}
|
}
|
});
|
}
|
|
private boolean isCheckedComment = true;// 消息是否被查看
|
private boolean isCheckedUpdate = false;// 消息是否被查看
|
|
// EventBus事件响应
|
public void onEventMainThread(NewComment info) {
|
isCheckedComment = info.getState();
|
if (isCheckedUpdate || !isCheckedComment) {
|
mBadgeView.show();
|
} else {
|
mBadgeView.hide();
|
}
|
}
|
|
private boolean isLogin = false;
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
EventBus.getDefault().register(this);
|
SharedPreferences preferences = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
isLogin = StringUtils.isEmpty(preferences.getString("LoginUid", "")) ? false
|
: true;
|
if (isLogin) {
|
getNewInfo();
|
}
|
|
if (YouthUtil.isOpenYouthMode(getApplicationContext())) {
|
findViewById(R.id.rb_nav_short_video).setVisibility(View.GONE);
|
findViewById(R.id.rb_nav_category).setVisibility(View.GONE);
|
findViewById(R.id.rb_shop).setVisibility(View.GONE);
|
} else {
|
findViewById(R.id.rb_nav_short_video).setVisibility(View.GONE);
|
findViewById(R.id.rb_nav_category).setVisibility(View.VISIBLE);
|
findViewById(R.id.rb_shop).setVisibility(View.VISIBLE);
|
}
|
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
EventBus.getDefault().unregister(this);
|
}
|
|
@Override
|
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
|
// TODO Auto-generated method stub
|
super.onActivityResult(arg0, arg1, arg2);
|
UMShareAPI.get(this).onActivityResult(arg0, arg1, arg2);
|
}
|
|
// EventBus事件响应
|
public void onEventMainThread(AdTag values) {
|
((RadioButton) rg_nav.findViewById(R.id.rb_nav_category))
|
.setChecked(true);
|
}
|
|
/*
|
* ****************************************************************
|
* 子函数:获得本地MAC地址
|
* ****************************************************************
|
*/
|
private String getMacAddress() {
|
String result = "";
|
String Mac = "";
|
result = callCmd("busybox ifconfig", "HWaddr");
|
|
// 如果返回的result == null,则说明网络不可取
|
if (result == null) {
|
return "网络出错,请检查网络";
|
}
|
|
// 对该行数据进行解析
|
// 例如:eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67
|
if (result.length() > 0 && result.contains("HWaddr") == true) {
|
Mac = result.substring(result.indexOf("HWaddr") + 6,
|
result.length() - 1);
|
if (Mac.length() > 1) {
|
Mac = Mac.replaceAll(" ", "");
|
result = "";
|
String[] tmp = Mac.split(":");
|
for (int i = 0; i < tmp.length; ++i) {
|
result += tmp[i];
|
}
|
}
|
}
|
return result;
|
}
|
|
private String callCmd(String cmd, String filter) {
|
String result = "";
|
String line = "";
|
try {
|
Process proc = Runtime.getRuntime().exec(cmd);
|
InputStreamReader is = new InputStreamReader(proc.getInputStream());
|
BufferedReader br = new BufferedReader(is);
|
|
// 执行命令cmd,只取结果中含有filter的这一行
|
while ((line = br.readLine()) != null
|
&& line.contains(filter) == false) {
|
// result += line;
|
}
|
|
result = line;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
private String getChannel() throws NameNotFoundException {
|
/*
|
* ApplicationInfo appInfo = this.getPackageManager()
|
* .getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
|
* return appInfo.metaData.getString("CHANNEL");
|
*/
|
return "360";
|
}
|
|
@Override
|
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
|
return new CursorLoader(this, MessageTable.CONTENT_URI, null,
|
MessageTable.MESSAGE_STATUS + " = ? ", new String[]{"0"},
|
MessageTable.UPDATE_TIME + " DESC");
|
}
|
|
@Override
|
public void onLoadFinished(Loader<Cursor> arg0, Cursor data) {
|
// Swap the new cursor in. (The framework will take care of closing the
|
// old cursor once we return.)
|
isCheckedUpdate = (data != null && data.getCount() != 0);
|
if (isCheckedUpdate || !isCheckedComment) {
|
mBadgeView.show();
|
} else {
|
mBadgeView.hide();
|
}
|
}
|
|
@Override
|
public void onLoaderReset(Loader<Cursor> arg0) {
|
// This is called when the last Cursor provided to onLoadFinished()
|
// above is about to be closed. We need to make sure we are no
|
// longer using it.
|
if (isCheckedComment) {
|
mBadgeView.hide();
|
}
|
}
|
|
private Runnable initialer = new Runnable() {
|
|
@Override
|
public void run() {
|
((RadioButton) findViewById(R.id.rb_nav_recommend))
|
.setChecked(true);
|
startDownloadService();
|
}
|
};
|
|
private void getUid() {
|
final SharedPreferences preferences = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String channel = "";
|
try {
|
channel = getChannel();
|
} catch (NameNotFoundException e1) {
|
e1.printStackTrace();
|
}
|
MGVideoAPI.getUid(this, channel, new DeviceUuidFactory(this)
|
.getDeviceUuid().toString(), preferences.getString("imei", ""),
|
preferences.getString("mac", ""), "", "",
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
if (!BeibeiConstant.ISHUAWEI && !StringUtils.isEmpty(jsonObject.getJSONObject("Data")
|
.optString("shopurl")) && !preferences.getBoolean("isDown", false)) {
|
addDownloadBuxin(jsonObject.getJSONObject("Data")
|
.optString("shopurl"));
|
}
|
preferences
|
.edit()
|
.putString(
|
"uid",
|
jsonObject.getJSONObject("Data")
|
.getString("Uid"))
|
.putString(
|
"share_url",
|
jsonObject.getJSONObject("Data")
|
.getString("ShareUrl"))
|
.putString(
|
"share_content",
|
jsonObject.getJSONObject("Data")
|
.getString("ShareContent"))
|
.putString(
|
"share_url_wechat",
|
jsonObject.getJSONObject("Data")
|
.getString("WXShareUrl"))
|
.putString(
|
"share_content_wechat",
|
jsonObject
|
.getJSONObject("Data")
|
.getString("WXShareContent"))
|
.putString(
|
"COMMENT_ALERT",
|
jsonObject.getJSONObject("Data")
|
.optString("COMMENT_ALERT"))
|
.putString(
|
"show_more_app",
|
jsonObject.getJSONObject("Data")
|
.optString("ShowMoreApp"))
|
.putString(
|
"evalueate",
|
jsonObject.getJSONObject("Data")
|
.optString("evalueate"))
|
.putString(
|
"show_ad",
|
jsonObject.optJSONObject("Data")
|
.optString("ShowAd"))
|
.putString(
|
"tuiguang_add",
|
jsonObject.optJSONObject("Data")
|
.optString("TuiGuang"))
|
.putString(
|
"top_icon",
|
jsonObject.optJSONObject("Data")
|
.optString("TopIcon"))
|
.commit();
|
|
String showAd = jsonObject.getJSONObject("Data")
|
.getString("ShowAd");
|
if (showAd == null || showAd.equalsIgnoreCase("1")) {
|
MGVideoApplication.showAd = true;
|
} else {
|
MGVideoApplication.showAd = false;
|
}
|
|
startService(new Intent(MainActivity.this,
|
HelpService.class));
|
MGVideoAPI.ERROR_NOTICE = jsonObject
|
.getJSONObject("Data").optString(
|
"NoticeContent");
|
|
if (isLogin) {
|
getNewInfo();
|
}
|
}
|
}
|
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (pd != null && !pd.isShowing())
|
pd.show();
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
try {
|
if (pd != null && pd.isShowing())
|
pd.dismiss();
|
} catch (Exception e) {
|
pd = null;
|
pd = new ProgressDialog(MainActivity.this);
|
}
|
try {
|
if (!isDestroyed()) {
|
rg_nav.post(initialer);
|
}
|
} catch (NoSuchMethodError e) {
|
if (rg_nav != null)
|
rg_nav.post(initialer);
|
}
|
}
|
});
|
}
|
|
private void addDownloadBuxin(final String url) {
|
if (isFinishing())
|
return;
|
LoginFirstDialog.Builder builder = new LoginFirstDialog.Builder(this);
|
builder.setPositiveButton("", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
// mateAppMarket1();
|
try {
|
Intent intent = new Intent(MainActivity.this,
|
com.mugua.mgvideo.service.DownLoadFileService.class);
|
stopService(intent);
|
} catch (Exception e) {
|
}
|
try {
|
Bundle bundle = new Bundle();
|
bundle.putString("downloadurl", url);
|
Intent intent = new Intent(MainActivity.this,
|
com.mugua.mgvideo.service.DownLoadFileService.class);
|
intent.putExtras(bundle);
|
startService(intent);
|
} catch (Exception e) {
|
}
|
|
dialog.dismiss();
|
}
|
});
|
builder.setNegativeButton("", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
dialog.dismiss();
|
}
|
});
|
builder.create().show();
|
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
|
Editor editor = sp.edit();
|
editor.putBoolean("isDown", true);
|
editor.commit();
|
}
|
|
private void getScoreOpen() {
|
SharedPreferences preferences = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
MGVideoAPI.getScoreOpen(this, uid,
|
new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
}
|
});
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
AppUpdate.destoryAppUpdate();
|
}
|
|
private long mExitTime;
|
|
@Override
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
// if ((System.currentTimeMillis() - mExitTime) > 2000) {
|
// Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
|
// mExitTime = System.currentTimeMillis();
|
// return true;
|
// }
|
ExitDialog dialog = new ExitDialog.Builder(this).create();
|
dialog.setCanceledOnTouchOutside(true);
|
dialog.show();
|
return true;
|
}
|
return super.onKeyDown(keyCode, event);
|
}
|
|
// 开开启下载
|
private void startDownloadService() {
|
Intent intent = new Intent(MainActivity.this, DownloadService.class);
|
startService(intent);
|
}
|
}
|