Files
assistant-android/app/src/main/java/com/gh/base/BaseAppCompatActivity.java
2017-06-12 15:08:48 +08:00

326 lines
11 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.gh.base;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import com.gh.common.util.AppDebugConfig;
import com.gh.common.util.AppManager;
import com.gh.common.util.Util_System_Keyboard;
import com.gh.gamecenter.R;
/**
* @author CsHeng
*/
public abstract class BaseAppCompatActivity extends BaseAppCompatActivityLog implements FragmentNavigationDelegate,
OnBackPressedListener {
protected static final String ARGS_FRAGMENT_NAME = "frgName";
protected static final String ARGS_FRAGMENT_BUNDLE = "frgBundle";
protected View mContentView;
protected static Intent clearTop(Context context, Class<? extends Activity> cls) {
final Intent intent = getReorderToFrontIntent(context, cls);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}
protected static Intent getReorderToFrontIntent(Context context, Class<? extends Activity> cls) {
final Intent intent = new Intent(context, cls);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}
protected static void showActivity(Context context, Class<? extends Activity> cls) {
final Intent intent = getReorderToFrontIntent(context, cls);
context.startActivity(intent);
}
protected static void startFragmentForResult(Context context, Class<? extends Activity> activity,
Class<? extends Fragment> fragment, Bundle bundle, int requestCode) {
if (context instanceof Activity) {
((Activity) context).startActivityForResult(getFragmentIntent(context, activity, fragment, bundle),
requestCode);
}
}
/**
* 根据传进来的fragment class和bundle extra来决定跳转到哪一个fragment
*
* @param context
* @param fragment fragment.getCanonicalName()
* @param bundle fragment的构造参数
* @return
*/
protected static Intent getFragmentIntent(Context context, Class<? extends Activity> activity,
Class<? extends Fragment> fragment, Bundle bundle) {
final Intent intent = getReorderToFrontIntent(context, activity);
intent.putExtra(ARGS_FRAGMENT_NAME, fragment.getCanonicalName());
intent.putExtra(ARGS_FRAGMENT_BUNDLE, bundle);
return intent;
}
protected static void startFragment(Context context, Class<? extends Activity> activity,
Class<? extends Fragment> fragment) {
startFragment(context, activity, fragment, null);
}
/**
* 启动Fragment
*
* @param context
* @param fragment
* @param bundle
*/
protected static void startFragment(Context context, Class<? extends Activity> activity,
Class<? extends Fragment> fragment, Bundle bundle) {
context.startActivity(getFragmentIntent(context, activity, fragment, bundle));
}
private void handleRedirectFromUri(Uri uri) {
try {
if (AppDebugConfig.IS_DEBUG) {
AppDebugConfig.logMethodWithParams(this, uri, uri.getHost(), uri.getPath(), uri.getEncodedQuery());
}
// switch (uri.getPath()) {
// case SchemeConstants.PATH_OPEN_GAME: {
// /**
// * 对于联运游戏传入的game_id为0game_url为对应的游戏链接aid传联运游戏的aid
// * 对于非联运游戏传入非联运游戏的game_idgame_url不传aid传0
// */
// GameModel game = new GameModel();
// int gameId = Integer.parseInt(uri.getQueryParameter(SchemeConstants.PARAMS_GAMEID));
// game.setId(gameId);
//// if (AppDebugConfig.IS_DEBUG) {
//// int debugId = 1049;
//// game.setId(debugId);
//// }
// if (gameId == 0) {
// // cooperate
// int aid = Integer.parseInt(uri.getQueryParameter(SchemeConstants.PARAMS_AID));
// game.setAid(aid);
// String gameUrl = uri.getQueryParameter(SchemeConstants.PARAMS_GAMEURL);
// game.setUrl(gameUrl);
// }
// ApiRequester.requestGameDetail(this, game);
//
// break;
// }
// case SchemeConstants.PATH_OPEN_ARTICLE: {
//
// String url = uri.getQueryParameter(SchemeConstants.PARAMS_ARTICLE_URL);
// String title = uri.getQueryParameter(SchemeConstants.PARAMS_ARTICLE_TITLE);
//
// if (!TextUtils.isEmpty(url) && !TextUtils.isEmpty(title)) {
// Bundle args = new Bundle();
// args.putString(Fragment_Browser_Web.ARGS_URL, url);
// args.putString(Fragment_Browser_Web.ARGS_TITLE, getString(R.string.title_article_detail));
// CommonActivity.startFragment(this, Fragment_Browser_Web.class, args);
// }
//
// break;
// }
// default:
// break;
// }
} catch (Exception e) {
if (AppDebugConfig.IS_DEBUG) {
// Debug_SDK.e(e);
}
}
}
@Override
public boolean onHandleBackPressed() {
onBackPressed();
return true;
}
protected boolean handleBackPressed() {
if (AppDebugConfig.IS_DEBUG) {
AppDebugConfig.logMethodWithParams(this);
}
final Fragment curFragment = getTopFragment();
if (curFragment instanceof OnBackPressedListener &&
((OnBackPressedListener) curFragment).onHandleBackPressed()) {
return true;
}
if (popFragment()) {
return true;
}
return false;
}
public Fragment getTopFragment() {
return getSupportFragmentManager().findFragmentById(R.id.layout_activity_content);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final int layoutId = getLayoutId();
if (layoutId != 0) {
mContentView = getLayoutInflater().inflate(layoutId, null);
setContentView(mContentView);
}
if (savedInstanceState == null) {
handleRedirectIntent(getIntent());
}
AppManager.getInstance().addActivity(this);
}
protected abstract int getLayoutId();
/**
* 1、判断当前是否存在某Fragment例如下载管理器已经在栈中则不重新启动而将其带到最上层 (FindFragmentWithTag)
* 2、支持URI调用
* 是否需要重新导向到其他Activity
*/
protected void handleRedirectIntent(Intent intent) {
try {
if (AppDebugConfig.IS_DEBUG) {
AppDebugConfig.logMethodWithParams(this, intent.toUri(Intent.URI_INTENT_SCHEME));
}
// 1、根据从网页抓取到的Intent跳转进来处理custom uri
// final Uri uri = intent.getData();
// if (uri != null && SchemeConstants.SCHEME_FFSS.equals(uri.getScheme())) {
// handleRedirectFromUri(uri);
// return;
// }
//
// // 2、根据序列化的数据来重新构建Fragment实例若没有则是旧的形式来决定传参
// final String fragmentName = intent.getStringExtra(ARGS_FRAGMENT_NAME);
// if (AppDebugConfig.IS_DEBUG) {
// AppDebugConfig.logMethodWithParams(this, fragmentName);
// }
// if (!TextUtils.isEmpty(fragmentName)) {
// final Bundle args = intent.getBundleExtra(ARGS_FRAGMENT_BUNDLE);
// final Fragment fragment = Fragment.instantiate(this, fragmentName, args);
// if (fragment instanceof DialogFragment) {
// ((DialogFragment) fragment).show(getSupportFragmentManager(), fragmentName);
// } else {
// getSupportFragmentManager().beginTransaction().replace(R.id.layout_activity_content, fragment)
// .commitAllowingStateLoss();
// }
// }
} catch (Throwable e) {
if (AppDebugConfig.IS_DEBUG) {
AppDebugConfig.logMethodWithParams(this, e);
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
AppManager.getInstance().finishActivity(this);
}
@Override
public void addFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction().add(R.id.layout_activity_content, fragment)
.addToBackStack(fragment.toString()).commitAllowingStateLoss();
Util_System_Keyboard.hideSoftKeyboard(this);
}
@Override
public void replaceFragment(Fragment toReplace) {
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.layout_activity_content, toReplace).addToBackStack(toReplace.toString());
ft.commitAllowingStateLoss();
Util_System_Keyboard.hideSoftKeyboard(this);
}
@Override
public void removeFragment(Fragment toRemove) {
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.layout_activity_content, toRemove).addToBackStack(toRemove.toString());
ft.commitAllowingStateLoss();
Util_System_Keyboard.hideSoftKeyboard(this);
}
@Override
public void showFragment(Fragment toShow) {
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.show(toShow).addToBackStack(toShow.toString());
ft.commitAllowingStateLoss();
Util_System_Keyboard.hideSoftKeyboard(this);
}
@Override
public void hideFragment(Fragment toHide) {
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.show(toHide).addToBackStack(toHide.toString());
ft.commitAllowingStateLoss();
Util_System_Keyboard.hideSoftKeyboard(this);
}
@Override
public boolean popFragment() {
if (AppDebugConfig.IS_DEBUG) {
AppDebugConfig.logMethodWithParams(this, getSupportFragmentManager().getBackStackEntryCount());
}
final int backStackCount = getSupportFragmentManager().getBackStackEntryCount();
if (backStackCount > 0) {
Util_System_Keyboard.hideSoftKeyboard(this);
getSupportFragmentManager().popBackStack();
return true;
}
return false;
}
@Override
public void popFragmentToBase() {
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
/**
* forward ActivityResultListener to fragments this activity hosts
*
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (AppDebugConfig.IS_DEBUG) {
AppDebugConfig.logMethodWithParams(this, requestCode, resultCode, data);
}
final Fragment fragment = getTopFragment();
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onBackPressed() {
if (AppDebugConfig.IS_DEBUG) {
AppDebugConfig.logMethodWithParams(this);
}
if (handleBackPressed()) {
return;
}
// default finish activity
Util_System_Keyboard.hideSoftKeyboard(this);
finish();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleRedirectIntent(intent);
}
}