1、处理viewpager fragment
2、将MainActivity管理的逻辑移到MainFragment,MessageFragment,NewsFragment同理, 3、处理首页ViewPager等手动计算size的问题 4、添加一堆ScaleXXXView,可以用来设定View本身的比例大小 TODO 1、将DownloadManagerActivity也处理成Fragment 2、其他界面也慢慢处理成Fragment
This commit is contained in:
@ -5,29 +5,20 @@ import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
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.provider.Settings;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.support.v4.util.ArrayMap;
|
||||
import android.text.Html;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextUtils;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.CheckedTextView;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.gh.base.AppController;
|
||||
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;
|
||||
@ -47,37 +38,27 @@ import com.gh.download.DataWatcher;
|
||||
import com.gh.download.DownloadEntity;
|
||||
import com.gh.download.DownloadManager;
|
||||
import com.gh.download.DownloadStatus;
|
||||
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.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.fragment.GameFragment;
|
||||
import com.gh.gamecenter.fragment.MainFragment;
|
||||
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.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.Response;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
import com.readystatesoftware.systembartint.SystemBarTintManager;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
@ -95,7 +76,6 @@ import java.util.concurrent.CountDownLatch;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import butterknife.BindView;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
@ -109,40 +89,13 @@ import rx.schedulers.Schedulers;
|
||||
/**
|
||||
* 项目的主Activity,3个Fragment都嵌入在这里。
|
||||
*/
|
||||
public class MainActivity extends BaseActivity implements OnClickListener {
|
||||
public class MainActivity extends BaseActivity {
|
||||
|
||||
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;
|
||||
public final static String EB_MAINACTIVITY_TAG = "MainActivity";
|
||||
public final static String EB_SKIP_GAMEFRAGMENT = "GameFragment";
|
||||
@BindView(R.id.main_iv_game)
|
||||
ImageView ivGame;
|
||||
@BindView(R.id.main_iv_news)
|
||||
ImageView ivNews;
|
||||
@BindView(R.id.main_iv_message)
|
||||
ImageView ivMessage;
|
||||
@BindView(R.id.main_iv_personal)
|
||||
ImageView ivPersonal;
|
||||
@BindView(R.id.main_tv_game)
|
||||
CheckedTextView tvGame;
|
||||
@BindView(R.id.main_tv_news)
|
||||
CheckedTextView tvNews;
|
||||
@BindView(R.id.main_tv_message)
|
||||
CheckedTextView tvMessage;
|
||||
@BindView(R.id.main_tv_personal)
|
||||
CheckedTextView tvPersonal;
|
||||
@BindView(R.id.main_iv_message_hint)
|
||||
View mMessageHint;
|
||||
private NewsFragment newsFragment;
|
||||
private MessageFragment messageFragment;
|
||||
private PersonalFragment personalFragment;
|
||||
private GameFragment gameFragment;
|
||||
private SharedPreferences sp;
|
||||
private ConcernManager concernManager;
|
||||
private ArrayList<String> mSearchHintLint;
|
||||
private int currentTab;
|
||||
|
||||
private boolean isSkipped;
|
||||
Runnable skipRun = new Runnable() {
|
||||
@Override
|
||||
@ -374,13 +327,12 @@ public class MainActivity extends BaseActivity implements OnClickListener {
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putInt("currentTab", currentTab);
|
||||
outState.putBoolean("isSkipped", isSkipped);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
return R.layout.activity_main;
|
||||
return R.layout.layout_wrapper_activity;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -681,19 +633,18 @@ public class MainActivity extends BaseActivity implements OnClickListener {
|
||||
|
||||
isSkipped = false;
|
||||
|
||||
initViews();
|
||||
// initViews();
|
||||
if (savedInstanceState != null) {
|
||||
currentTab = savedInstanceState.getInt("currentTab");
|
||||
// currentTab = savedInstanceState.getInt("currentTab");
|
||||
isSkipped = savedInstanceState.getBoolean("isSkipped");
|
||||
} else {
|
||||
currentTab = 0;
|
||||
// } else {
|
||||
// currentTab = 0;
|
||||
}
|
||||
setTabSelection(currentTab);
|
||||
|
||||
replaceFragment(new MainFragment());
|
||||
|
||||
concernManager = new ConcernManager(getApplicationContext());
|
||||
|
||||
getSearchHints();
|
||||
|
||||
if (!isNewFirstLaunch && sp.getBoolean("autoupdate", true)) {
|
||||
// 检查助手更新
|
||||
UpdateManager.getInstance(this).checkUpdate(true, null);
|
||||
@ -745,9 +696,6 @@ public class MainActivity extends BaseActivity implements OnClickListener {
|
||||
|
||||
getSuggestionType();
|
||||
|
||||
checkMarkRead(); // 检查是否有记录已读消息
|
||||
|
||||
checkMessageUnread(true); // 检查是否有未读消息
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -762,123 +710,6 @@ public class MainActivity extends BaseActivity implements OnClickListener {
|
||||
handler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
|
||||
private void checkMessageUnread(boolean isCheck) {
|
||||
TokenUtils.getToken(this, isCheck)
|
||||
.flatMap(new Func1<String, Observable<MessageUnreadEntity>>() {
|
||||
@Override
|
||||
public Observable<MessageUnreadEntity> call(String token) {
|
||||
return RetrofitManager.getMessage().getMessageUnread(token);
|
||||
}
|
||||
})
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<MessageUnreadEntity>() {
|
||||
@Override
|
||||
public void onResponse(MessageUnreadEntity response) {
|
||||
super.onResponse(response);
|
||||
if (response != null && response.getKefuList() != null && response.getKefuList().size() > 0) {
|
||||
KeFuMarkReadDao dao = new KeFuMarkReadDao(MainActivity.this);
|
||||
for (int i = 0; i < response.getKefuList().size(); i++) {
|
||||
if (dao.isMarkRead(response.getKefuList().get(i))) {
|
||||
response.getKefuList().remove(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
if (response.getKefuList() != null && response.getKefuList().size() > 0) {
|
||||
mMessageHint.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (response != null && response.getComment() > 0) {
|
||||
mMessageHint.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
if (e != null && e.code() == 401) {
|
||||
checkMessageUnread(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void checkMarkRead() {
|
||||
MessageMarkReadDao readDao = new MessageMarkReadDao(MainActivity.this);
|
||||
List<MessageMarkReadInfo> readInfos = readDao.getAll();
|
||||
if (readInfos == null || readInfos.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
MessageMarkReadInfo readInfo = readInfos.get(0);
|
||||
HashMap<String, Boolean> kefuIds = readInfo.getKefuIds();
|
||||
HashMap<String, Boolean> noticeIds = readInfo.getNoticeIds();
|
||||
|
||||
if (kefuIds != null && kefuIds.size() > 0) {
|
||||
Utils.log("======提交已读客服消息");
|
||||
JSONArray array = new JSONArray();
|
||||
for (String s : kefuIds.keySet()) {
|
||||
array.put(s);
|
||||
}
|
||||
Utils.log("======客服id::" + array.toString());
|
||||
RequestBody body = RequestBody.create(MediaType.parse("application/json"), array.toString());
|
||||
postMarkRead("kefu", true, body, readDao);
|
||||
}
|
||||
|
||||
if (noticeIds != null && noticeIds.size() > 0) {
|
||||
Utils.log("======提交已读公告消息");
|
||||
JSONArray array = new JSONArray();
|
||||
for (String s : noticeIds.keySet()) {
|
||||
array.put(s);
|
||||
}
|
||||
Utils.log("======客服id::" + array.toString());
|
||||
RequestBody body = RequestBody.create(MediaType.parse("application/json"), array.toString());
|
||||
postMarkRead("notice", true, body, readDao);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void postMarkRead(final String type, boolean isCkeck, final RequestBody body, final MessageMarkReadDao readDao) {
|
||||
TokenUtils
|
||||
.getToken(MainActivity.this, isCkeck)
|
||||
.flatMap(new Func1<String, Observable<ResponseBody>>() {
|
||||
@Override
|
||||
public Observable<ResponseBody> call(String token) {
|
||||
if ("kefu".equals(type)) {
|
||||
return RetrofitManager.getMessage().postMarkKeFuRead(body, token);
|
||||
} else {
|
||||
return RetrofitManager.getMessage().postMarkNoticeRead(body, token);
|
||||
}
|
||||
}
|
||||
})
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<ResponseBody>() {
|
||||
@Override
|
||||
public void onResponse(ResponseBody response) {
|
||||
super.onResponse(response);
|
||||
Utils.log("=========postMarkRead::" + type);
|
||||
if ("kefu".equals(type)) {
|
||||
readDao.cleanKeFu();
|
||||
} else {
|
||||
readDao.cleanNotice();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
super.onFailure(e);
|
||||
if (e != null && e.code() == 401) {
|
||||
postMarkRead(type, false, body, readDao);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void getSuggestionType() {
|
||||
RetrofitManager
|
||||
.getApi()
|
||||
@ -996,7 +827,7 @@ public class MainActivity extends BaseActivity implements OnClickListener {
|
||||
final ArrayList<GameEntity> concernDigest = new ArrayList<>();
|
||||
|
||||
if (arrGameId.size() == 0) {
|
||||
EventBus.getDefault().post(new EBUISwitch("NewsFragment", -2)); // 没有关注
|
||||
EventBus.getDefault().post(new EBUISwitch("NewsWrapperFragment", -2)); // 没有关注
|
||||
}
|
||||
|
||||
List<Observable<GameEntity>> list = new ArrayList<>();
|
||||
@ -1019,13 +850,6 @@ public class MainActivity extends BaseActivity implements OnClickListener {
|
||||
});
|
||||
}
|
||||
|
||||
private void initViews() {
|
||||
findViewById(R.id.main_ll_game).setOnClickListener(this);
|
||||
findViewById(R.id.main_ll_news).setOnClickListener(this);
|
||||
findViewById(R.id.main_ll_message).setOnClickListener(this);
|
||||
findViewById(R.id.main_ll_personal).setOnClickListener(this);
|
||||
}
|
||||
|
||||
private void checkPermission() {
|
||||
List<PackageInfo> installedList = getPackageManager().getInstalledPackages(0);
|
||||
if (installedList.isEmpty()) {
|
||||
@ -1049,32 +873,6 @@ public class MainActivity extends BaseActivity implements OnClickListener {
|
||||
}
|
||||
}
|
||||
|
||||
private void getSearchHints() {
|
||||
RetrofitManager.getApi().getSearchHints()
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<ArrayList<String>>() {
|
||||
@Override
|
||||
public void onResponse(ArrayList<String> response) {
|
||||
if (response.size() != 0) {
|
||||
// if (response.size() == 1) {
|
||||
// mSearchHintLint = response.get(0);
|
||||
// } else {
|
||||
// mSearchHintLint = response.get(RandomUtils.nextInt(response.size()));
|
||||
// }
|
||||
|
||||
mSearchHintLint = response;
|
||||
if (gameFragment != null) {
|
||||
gameFragment.setHint(mSearchHintLint);
|
||||
}
|
||||
if (newsFragment != null) {
|
||||
newsFragment.setHint(mSearchHintLint);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 获取META-INF中的plugin_update 文件,判断是否从游戏插件中下载的app,是则获取游戏id,启动游戏更新,下载该游戏
|
||||
private void getPluginUpdate() {
|
||||
ApplicationInfo appinfo = getApplicationInfo();
|
||||
@ -1109,133 +907,6 @@ public class MainActivity extends BaseActivity implements OnClickListener {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final int id = v.getId();
|
||||
switch (v.getId()) {
|
||||
case R.id.main_ll_game:
|
||||
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, INDEX_GAME));
|
||||
setTabSelection(INDEX_GAME);
|
||||
break;
|
||||
case R.id.main_ll_news:
|
||||
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, INDEX_NEWS));
|
||||
setTabSelection(INDEX_NEWS);
|
||||
break;
|
||||
case R.id.main_ll_message:
|
||||
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, INDEX_MESSAGE));
|
||||
setTabSelection(INDEX_MESSAGE);
|
||||
break;
|
||||
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 void setTabSelection(int index) {
|
||||
clearSelection();
|
||||
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
||||
hideFragments(transaction);
|
||||
switch (index) {
|
||||
case INDEX_GAME:
|
||||
ivGame.setImageResource(R.drawable.ic_game_select);
|
||||
tvGame.setChecked(true);
|
||||
gameFragment = alterFragment(transaction, GameFragment.class);
|
||||
break;
|
||||
case INDEX_NEWS:
|
||||
ivNews.setImageResource(R.drawable.ic_news_select);
|
||||
tvNews.setChecked(true);
|
||||
newsFragment = alterFragment(transaction, NewsFragment.class);
|
||||
break;
|
||||
case INDEX_MESSAGE:
|
||||
ivMessage.setImageResource(R.drawable.ic_message_select);
|
||||
tvMessage.setChecked(true);
|
||||
messageFragment = alterFragment(transaction, MessageFragment.class);
|
||||
break;
|
||||
case INDEX_PERSONAL:
|
||||
ivPersonal.setImageResource(R.drawable.ic_user_select);
|
||||
tvPersonal.setChecked(true);
|
||||
personalFragment = alterFragment(transaction, PersonalFragment.class);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
currentTab = index;
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
// 清除掉所有的选中状态。
|
||||
private void clearSelection() {
|
||||
ivGame.setImageResource(R.drawable.ic_game_unselect);
|
||||
ivNews.setImageResource(R.drawable.ic_news_unselect);
|
||||
ivMessage.setImageResource(R.drawable.ic_message_unselect);
|
||||
ivPersonal.setImageResource(R.drawable.ic_user_unselect);
|
||||
|
||||
tvGame.setChecked(false);
|
||||
tvNews.setChecked(false);
|
||||
tvMessage.setChecked(false);
|
||||
tvPersonal.setChecked(false);
|
||||
}
|
||||
|
||||
// 将所有的Fragment都置为隐藏状态。
|
||||
private void hideFragments(FragmentTransaction transaction) {
|
||||
List<Fragment> list = getSupportFragmentManager().getFragments();
|
||||
if (list != null) {
|
||||
for (Fragment fragment : list) {
|
||||
transaction.hide(fragment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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());
|
||||
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;
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onEventMainThread(EBSkip skip) {
|
||||
if (EB_SKIP_GAMEFRAGMENT.equals(skip.getType())) {
|
||||
EventBus.getDefault().post(new EBUISwitch(EB_MAINACTIVITY_TAG, INDEX_GAME));
|
||||
setTabSelection(INDEX_GAME);
|
||||
EventBus.getDefault().post(new EBSkip(EB_MAINACTIVITY_TAG, skip.getCurrentItem()));
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onEventMainThread(EBReuse reuse) {
|
||||
if (MessageFragment.MESSAGE_READ_OVER.equals(reuse.getType())) {
|
||||
Utils.log("======= over??");
|
||||
mMessageHint.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onEventMainThread(EBPackage busFour) {
|
||||
final String packageName = busFour.getPackageName();
|
||||
|
||||
Reference in New Issue
Block a user