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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
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);
    }
}