1、将所有Activity统一到一个base(主题AppCompatTheme),layout和contentView统一处理

2、MainActivity tab切换方式的重构
3、下一步更改toolbar实现方式,然后再是尽量用fragment替换
This commit is contained in:
CsHeng
2017-05-05 18:12:51 +08:00
parent 7ca9272fe7
commit d69e75480e
83 changed files with 2036 additions and 1191 deletions

View File

@ -1,105 +1,44 @@
package com.gh.gamecenter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.*;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.os.*;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.ArrayMap;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.*;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.gh.base.BaseFragmentActivity;
import android.widget.*;
import com.gh.base.BaseActivity;
import com.gh.base.SearchBarHint;
import com.gh.common.constant.Config;
import com.gh.common.util.ClassUtils;
import com.gh.common.util.DataCollectionUtils;
import com.gh.common.util.DataLogUtils;
import com.gh.common.util.DataUtils;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.FileUtils;
import com.gh.common.util.GameUtils;
import com.gh.common.util.LibaoUtils;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.PlatformUtils;
import com.gh.common.util.TokenUtils;
import com.gh.common.util.TrafficUtils;
import com.gh.common.util.Utils;
import com.gh.download.DataWatcher;
import com.gh.download.DownloadEntity;
import com.gh.download.DownloadManager;
import com.gh.download.DownloadStatus;
import com.gh.common.util.*;
import com.gh.download.*;
import com.gh.gamecenter.db.KeFuMarkReadDao;
import com.gh.gamecenter.db.MessageMarkReadDao;
import com.gh.gamecenter.db.info.ConcernInfo;
import com.gh.gamecenter.db.info.GameInfo;
import com.gh.gamecenter.db.info.MessageMarkReadInfo;
import com.gh.gamecenter.entity.ApkEntity;
import com.gh.gamecenter.entity.GameDigestEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.entity.GameUpdateEntity;
import com.gh.gamecenter.entity.MessageUnreadEntity;
import com.gh.gamecenter.entity.SuggestionTypeEntity;
import com.gh.gamecenter.eventbus.EBDownloadStatus;
import com.gh.gamecenter.eventbus.EBPackage;
import com.gh.gamecenter.eventbus.EBReuse;
import com.gh.gamecenter.eventbus.EBShowDialog;
import com.gh.gamecenter.eventbus.EBSkip;
import com.gh.gamecenter.eventbus.EBUISwitch;
import com.gh.gamecenter.db.info.*;
import com.gh.gamecenter.entity.*;
import com.gh.gamecenter.eventbus.*;
import com.gh.gamecenter.game.GameFragment;
import com.gh.gamecenter.manager.ConcernManager;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.manager.FilterManager;
import com.gh.gamecenter.manager.GameManager;
import com.gh.gamecenter.manager.PackageManager;
import com.gh.gamecenter.manager.UpdateManager;
import com.gh.gamecenter.manager.*;
import com.gh.gamecenter.message.MessageFragment;
import com.gh.gamecenter.news.NewsFragment;
import com.gh.gamecenter.personal.PersonalFragment;
import com.gh.gamecenter.retrofit.JSONObjectResponse;
import com.gh.gamecenter.retrofit.ObservableUtil;
import com.gh.gamecenter.retrofit.*;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import de.greenrobot.event.EventBus;
import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import de.greenrobot.event.EventBus;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.HttpException;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
@ -107,26 +46,31 @@ import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* 项目的主Activity3个Fragment都嵌入在这里。
*/
public class MainActivity extends BaseFragmentActivity implements OnClickListener {
public class MainActivity extends BaseActivity implements OnClickListener {
private SystemBarTintManager mTintManager;
public static final int INDEX_GAME = 0;
public static final int INDEX_NEWS = 1;
public static final int INDEX_MESSAGE = 2;
public static final int INDEX_PERSONAL = 3;
private NewsFragment newsFragment;
private MessageFragment messageFragment;
// private WenDaFragment wenDaFragment;
// private ZiXunFragment ziXunFragment;
private PersonalFragment personalFragment;
private GameFragment gameFragment;
private ImageView ivGame;
private ImageView ivNews;
// private ImageView ivWenDa;
private ImageView ivMessage;
private ImageView ivPersonal;
private TextView tvGame;
private TextView tvNews;
// private TextView tvWenDa;
private TextView tvMessage;
private TextView tvPersonal;
@ -135,14 +79,14 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
private SharedPreferences sp;
private ConcernManager concernManager;
private ArrayList<String> searchHint;
private ArrayList<String> mSearchHintLint;
private int currentTab;
private boolean isSkipped;
public final static String EB_MAINACTIVITY_TAG = "MainActivity" ;
public final static String EB_SKIP_GAMEFRAGMENT = "GameFragment" ;
public final static String EB_MAINACTIVITY_TAG = "MainActivity";
public final static String EB_SKIP_GAMEFRAGMENT = "GameFragment";
Runnable skipRun = new Runnable() {
@Override
@ -364,6 +308,11 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
outState.putBoolean("isSkipped", isSkipped);
}
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -371,59 +320,8 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
// 初始化gameMap
DownloadManager.getInstance(this).initGameMap();
View contentView = View.inflate(this, R.layout.activity_main, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTheme(R.style.AppTheme);
setTranslucentStatus(true);
mTintManager = new SystemBarTintManager(this);
mTintManager.setStatusBarTintEnabled(true);
if (Build.MANUFACTURER.equals("Meizu") || Build.MANUFACTURER.equals("Xiaomi")) {
mTintManager.setStatusBarTintColor(Color.WHITE);
} else {
mTintManager.setStatusBarTintColor(Color.BLACK);
}
SystemBarTintManager.SystemBarConfig config = mTintManager.getConfig();
contentView.setPadding(0, config.getPixelInsetTop(false), 0,
config.getPixelInsetBottom());
if (Build.MANUFACTURER.equals("Meizu")) {
try {
Window window = getWindow();
if (window != null) {
WindowManager.LayoutParams lp = window.getAttributes();
Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
darkFlag.setAccessible(true);
meizuFlags.setAccessible(true);
int bit = darkFlag.getInt(null);
int value = meizuFlags.getInt(lp);
value |= bit;
meizuFlags.setInt(lp, value);
window.setAttributes(lp);
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (Build.MANUFACTURER.equals("Xiaomi")) {
try {
Window window = getWindow();
if (window != null) {
Class<?> clazz = window.getClass();
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
int darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
extraFlagField.invoke(window, darkModeFlag, darkModeFlag);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
setContentView(contentView);
sp = getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
boolean isNewFirstLaunch = sp.getBoolean("isNewFirstLaunchV" + PackageUtils.getVersionName(getApplicationContext()), true);
boolean isNewFirstLaunch = sp.getBoolean("isNewFirstLaunchV" + PackageUtils.getVersionName(this), true);
isSkipped = false;
@ -446,7 +344,7 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
}
if (isNewFirstLaunch) {
getPluginUpdate();
sp.edit().putBoolean("isNewFirstLaunchV" + PackageUtils.getVersionName(getApplicationContext()), false).apply();
sp.edit().putBoolean("isNewFirstLaunchV" + PackageUtils.getVersionName(this), false).apply();
}
// 初始化PlatformUtils
@ -508,7 +406,7 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<MessageUnreadEntity>(){
.subscribe(new Response<MessageUnreadEntity>() {
@Override
public void onResponse(MessageUnreadEntity response) {
super.onResponse(response);
@ -1127,17 +1025,17 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
public void onResponse(ArrayList<String> response) {
if (response.size() != 0) {
// if (response.size() == 1) {
// searchHint = response.get(0);
// mSearchHintLint = response.get(0);
// } else {
// searchHint = response.get(RandomUtils.nextInt(response.size()));
// mSearchHintLint = response.get(RandomUtils.nextInt(response.size()));
// }
searchHint = response;
mSearchHintLint = response;
if (gameFragment != null) {
gameFragment.setHint(searchHint);
gameFragment.setHint(mSearchHintLint);
}
if (newsFragment != null) {
newsFragment.setHint(searchHint);
newsFragment.setHint(mSearchHintLint);
}
}
}
@ -1181,165 +1079,89 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
@Override
public void onClick(View v) {
final int id = v.getId();
if (id == R.id.main_ll_game) {
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, 0));
setTabSelection(0);
} else if (id == R.id.main_ll_news) {
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, 1));
setTabSelection(1);
}
// else if (id == R.id.main_ll_wenda) {
// setTabSelection(2);
// }
else if (id == R.id.main_ll_message) {
setTabSelection(2);
} else if (id == R.id.main_ll_personal) {
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, 2));
setTabSelection(3);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (id == R.id.main_ll_personal) {
mTintManager.setStatusBarTintResource(R.color.theme);
} else if (Build.MANUFACTURER.equals("Meizu") || Build.MANUFACTURER.equals("Xiaomi")) {
mTintManager.setStatusBarTintColor(Color.WHITE);
} else {
mTintManager.setStatusBarTintColor(Color.BLACK);
}
}
}
private void setTabSelection(int index) {
clearSelection();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
hideFragments(transaction);
switch (index) {
case 0:
ivGame.setImageResource(R.drawable.ic_game_select);
tvGame.setTextColor(getResources().getColor(R.color.theme));
if (gameFragment == null) {
List<Fragment> list = getSupportFragmentManager().getFragments();
if (list != null) {
for (Fragment fragment : list) {
if (fragment.getClass().equals(GameFragment.class)) {
gameFragment = (GameFragment) fragment;
transaction.show(gameFragment);
break;
}
}
}
if (gameFragment == null) {
gameFragment = new GameFragment();
if (searchHint != null && searchHint.size() > 0) {
gameFragment.setHint(searchHint);
}
transaction.add(R.id.main_content, gameFragment);
}
} else {
transaction.show(gameFragment);
}
currentTab = 0;
switch (v.getId()) {
case R.id.main_ll_game:
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, INDEX_GAME));
setTabSelection(INDEX_GAME);
break;
case 1:
ivNews.setImageResource(R.drawable.ic_news_select);
tvNews.setTextColor(getResources().getColor(R.color.theme));
if (newsFragment == null) {
List<Fragment> list = getSupportFragmentManager().getFragments();
if (list != null) {
for (Fragment fragment : list) {
if (fragment.getClass().equals(NewsFragment.class)) {
newsFragment = (NewsFragment) fragment;
transaction.show(newsFragment);
break;
}
}
}
if (newsFragment == null) {
newsFragment = new NewsFragment();
if (searchHint != null && searchHint.size() > 0) {
newsFragment.setHint(searchHint);
}
transaction.add(R.id.main_content, newsFragment);
}
} else {
transaction.show(newsFragment);
}
currentTab = 1;
case R.id.main_ll_news:
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, INDEX_NEWS));
setTabSelection(INDEX_NEWS);
break;
// case 2:
// ivWenDa.setImageResource(R.drawable.ic_wenda_select);
// tvWenDa.setTextColor(getResources().getColor(R.color.theme));
// if (wenDaFragment == null) {
// List<Fragment> list = getSupportFragmentManager().getFragments();
// if (list != null) {
// for (Fragment fragment : list) {
// if (fragment.getClass().equals(WenDaFragment.class)) {
// wenDaFragment = (WenDaFragment) fragment;
// transaction.show(wenDaFragment);
// break;
// }
// }
// }
// if (wenDaFragment == null) {
// wenDaFragment = new WenDaFragment();
// transaction.add(R.id.main_content, wenDaFragment);
// }
// } else {
// transaction.show(wenDaFragment);
// }
// currentTab = 2;
// break;
case 2:
ivMessage.setImageResource(R.drawable.ic_message_select);
tvMessage.setTextColor(getResources().getColor(R.color.theme));
if (messageFragment == null) {
List<Fragment> list = getSupportFragmentManager().getFragments();
if (list != null) {
for (Fragment fragment : list) {
if (fragment.getClass().equals(MessageFragment.class)) {
messageFragment = (MessageFragment) fragment;
transaction.show(messageFragment);
break;
}
}
}
if (messageFragment == null) {
messageFragment = new MessageFragment();
transaction.add(R.id.main_content, messageFragment);
}
} else {
transaction.show(messageFragment);
}
currentTab = 2;
case R.id.main_ll_message:
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, INDEX_MESSAGE));
setTabSelection(INDEX_MESSAGE);
break;
case 3:
ivPersonal.setImageResource(R.drawable.ic_user_select);
tvPersonal.setTextColor(getResources().getColor(R.color.theme));
if (personalFragment == null) {
List<Fragment> list = getSupportFragmentManager().getFragments();
if (list != null) {
for (Fragment fragment : list) {
if (fragment.getClass().equals(PersonalFragment.class)) {
personalFragment = (PersonalFragment) fragment;
transaction.show(personalFragment);
break;
}
}
}
if (personalFragment == null) {
personalFragment = new PersonalFragment();
transaction.add(R.id.main_content, personalFragment);
}
} else {
transaction.show(personalFragment);
}
currentTab = 3;
case R.id.main_ll_personal:
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, INDEX_PERSONAL));
setTabSelection(INDEX_PERSONAL);
break;
default:
break;
}
SystemBarTintManager tintManager = getTintManager();
if (tintManager != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (id == R.id.main_ll_personal) {
tintManager.setStatusBarTintResource(R.color.theme);
} else if (Build.MANUFACTURER.equals("Meizu") || Build.MANUFACTURER.equals("Xiaomi")) {
tintManager.setStatusBarTintColor(Color.WHITE);
} else {
tintManager.setStatusBarTintColor(Color.BLACK);
}
}
}
// private <T extends Fragment & SearchBarHint> T alterFragment(Class<T> cls) {
private <T extends Fragment> T alterFragment(FragmentTransaction transaction, Class<T> cls) {
T fragmentByTag = (T) getSupportFragmentManager().findFragmentByTag(cls.getSimpleName());
AppDebugConfig.logMethodWithParams(this, cls, cls.getSimpleName(), fragmentByTag);
try {
if (fragmentByTag != null) {
transaction.show(fragmentByTag);
} else {
fragmentByTag = cls.newInstance();
transaction.add(R.id.main_content, fragmentByTag, cls.getSimpleName());
if (fragmentByTag instanceof SearchBarHint && mSearchHintLint != null && mSearchHintLint.size() > 0) {
((SearchBarHint) fragmentByTag).setHint(mSearchHintLint);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return fragmentByTag;
}
private void setTabSelection(int index) {
AppDebugConfig.logMethodWithParams(this, index);
clearSelection();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
hideFragments(transaction);
switch (index) {
case INDEX_GAME:
ivGame.setImageResource(R.drawable.ic_game_select);
tvGame.setTextColor(ContextCompat.getColor(this, R.color.theme));
gameFragment = alterFragment(transaction, GameFragment.class);
break;
case INDEX_NEWS:
ivNews.setImageResource(R.drawable.ic_news_select);
tvNews.setTextColor(ContextCompat.getColor(this, R.color.theme));
newsFragment = alterFragment(transaction, NewsFragment.class);
break;
case INDEX_MESSAGE:
ivMessage.setImageResource(R.drawable.ic_message_select);
tvMessage.setTextColor(ContextCompat.getColor(this, R.color.theme));
messageFragment = alterFragment(transaction, MessageFragment.class);
break;
case INDEX_PERSONAL:
ivPersonal.setImageResource(R.drawable.ic_user_select);
tvPersonal.setTextColor(ContextCompat.getColor(this, R.color.theme));
personalFragment = alterFragment(transaction, PersonalFragment.class);
break;
default:
break;
}
currentTab = index;
transaction.commit();
}
@ -1347,34 +1169,17 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
private void clearSelection() {
ivGame.setImageResource(R.drawable.ic_game_unselect);
ivNews.setImageResource(R.drawable.ic_news_unselect);
// ivWenDa.setImageResource(R.drawable.ic_wenda_unselect);
ivMessage.setImageResource(R.drawable.ic_message_unselect);
ivPersonal.setImageResource(R.drawable.ic_user_unselect);
tvGame.setTextColor(getResources().getColor(R.color.btn_gray));
tvNews.setTextColor(getResources().getColor(R.color.btn_gray));
// tvWenDa.setTextColor(Color.parseColor("#a7a8b2"));
tvMessage.setTextColor(getResources().getColor(R.color.btn_gray));
tvPersonal.setTextColor(getResources().getColor(R.color.btn_gray));
tvGame.setTextColor(ContextCompat.getColor(this, R.color.btn_gray));
tvNews.setTextColor(ContextCompat.getColor(this, R.color.btn_gray));
tvMessage.setTextColor(ContextCompat.getColor(this, R.color.btn_gray));
tvPersonal.setTextColor(ContextCompat.getColor(this, R.color.btn_gray));
}
// 将所有的Fragment都置为隐藏状态。
private void hideFragments(FragmentTransaction transaction) {
if (personalFragment != null) {
transaction.hide(personalFragment);
}
if (gameFragment != null) {
transaction.hide(gameFragment);
}
// if (wenDaFragment != null) {
// transaction.hide(wenDaFragment);
// }
if (messageFragment != null) {
transaction.hide(messageFragment);
}
if (newsFragment != null) {
transaction.hide(newsFragment);
}
List<Fragment> list = getSupportFragmentManager().getFragments();
if (list != null) {
for (Fragment fragment : list) {
@ -1385,8 +1190,8 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
public void onEventMainThread(EBSkip skip) {
if (EB_SKIP_GAMEFRAGMENT.equals(skip.getType())) {
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, 0));
setTabSelection(0);
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, INDEX_GAME));
setTabSelection(INDEX_GAME);
EventBus.getDefault().post(new EBSkip(EB_MAINACTIVITY_TAG, skip.getCurrentItem()));
}
}