Files
assistant-android/app/src/main/java/com/gh/base/BaseActivity.java
2016-04-28 14:55:17 +08:00

251 lines
7.1 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.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.gh.common.constant.Config;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.DisplayUtils;
import com.gh.common.util.RunningUtils;
import com.gh.common.util.Utils;
import com.gh.gamecenter.R;
import com.gh.gamecenter.eventbus.EBShowDialog;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.manager.SystemBarTintManager;
import com.gh.gamecenter.manager.SystemBarTintManager.SystemBarConfig;
import com.tendcloud.tenddata.TCAgent;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.PlatformListFakeActivity.OnShareButtonClickListener;
import de.greenrobot.event.EventBus;
public class BaseActivity extends Activity {
private String LOG = this.getClass().getName();
private boolean LOG_ON = true;
private boolean isPause;
private SystemBarTintManager tintManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppLog("onCreate");
Utils.log(this.getClass().getSimpleName());
AppController.getInstance().addActivity(this);
EventBus.getDefault().register(this);
}
protected void init(View contentView, String title) {
init(contentView);
TextView actionbar_tv_title = (TextView) findViewById(R.id.actionbar_tv_title);
actionbar_tv_title.setText(title);
}
protected void init(View contentView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTheme(R.style.AppTheme);
setTranslucentStatus(true);
tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.theme_colors);
SystemBarConfig config = tintManager.getConfig();
contentView.setPadding(0, config.getPixelInsetTop(false), 0,
config.getPixelInsetBottom());
}
setContentView(contentView);
int actionbar_height = getSharedPreferences(Config.PREFERENCE,
Context.MODE_PRIVATE).getInt("actionbar_height",
DisplayUtils.dip2px(getApplicationContext(), 48));
RelativeLayout reuse_actionbar = (RelativeLayout) findViewById(R.id.reuse_actionbar);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, actionbar_height);
reuse_actionbar.setLayoutParams(lparams);
findViewById(R.id.actionbar_rl_back).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
//简化findViewById
try {
Class<?> clazz = this.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
int id = Utils.getId(field.getName());
if (id != -1) {
Utils.log("reflect name = " + field.getName());
field.setAccessible(true);
Class<?> fieldType = field.getType();
Object injectedValue = fieldType.cast(findViewById(id));
field.set(this, injectedValue);
field.setAccessible(false);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
protected SystemBarTintManager getTintManager() {
return tintManager;
}
@Override
public void finish() {
super.finish();
AppController.getInstance().removeActivity(this);
}
public void AppLog(String str) {
if (LOG_ON) {
Log.i(LOG, str);
}
}
public void toast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
public void toast(int msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
@TargetApi(19)
protected void setTranslucentStatus(boolean status) {
Window window = getWindow();
WindowManager.LayoutParams winParams = window.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (status) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
window.setAttributes(winParams);
}
public void showShare(String url, String title, String icon, String entrance, String type) {
ShareSDK.initSDK(this);
OnekeyShare oks = new OnekeyShare();
// 关闭sso授权
oks.disableSSOWhenAuthorize();
// 分享时Notification的图标和文字 2.5.9以后的版本不调用此方法
// oks.setNotification(R.drawable.ic_launcher,
// getString(R.string.app_name));
// title标题印象笔记、邮箱、信息、微信、人人网和QQ空间使用
oks.setTitle(title);
// titleUrl是标题的网络链接仅在人人网和QQ空间使用
oks.setTitleUrl(url);
// text是分享文本所有平台都需要这个字段
oks.setText(title + " " + url);
// imagePath是图片的本地路径Linked-In以外的平台都支持此参数
// oks.setImagePath(icon);//确保SDcard下面存在此张图片
oks.setImageUrl(icon);
// url仅在微信包括好友和朋友圈中使用
oks.setUrl(url);
// comment是我对这条分享的评论仅在人人网和QQ空间使用
oks.setComment("精彩尽在" + url);
// site是分享此内容的网站名称仅在QQ空间使用
oks.setSite(this.getString(R.string.app_name));
// siteUrl是分享此内容的网站地址仅在QQ空间使用
oks.setSiteUrl(url);
final Map<String, Object> map = new HashMap<String, Object>();
map.put("title", title);
map.put("url", url);
map.put("from", entrance);
map.put("createdOn", System.currentTimeMillis() / 1000);
oks.setOnShareButtonClickListener(new OnShareButtonClickListener() {
@Override
public void onClick(View v, List<Object> checkPlatforms) {
map.put("method", checkPlatforms.get(0).getClass().getSimpleName());
DataCollectionManager.onEvent(BaseActivity.this, "share", map);
}
});
// 启动分享GUI
oks.show(this);
TCAgent.onEvent(this, "内容分享", title);
}
public void onEventMainThread(EBShowDialog showDialog) {
if (!isPause && this.getClass().getName().equals(RunningUtils.getTopActivity(this))) {
if ("hijack".equals(showDialog.getType())) {
DialogUtils.showHijackDialog(this);
} else if ("delete".equals(showDialog.getType())) {
DialogUtils.showDeleteDialog(this, showDialog.getPath());
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override
protected void onPause() {
super.onPause();
AppLog("onPause");
TCAgent.onPause(this);
isPause = true;
}
@Override
protected void onRestart() {
super.onRestart();
AppLog("onRestart");
}
@Override
protected void onResume() {
super.onResume();
AppLog("onResume");
TCAgent.onResume(this);
isPause = false;
}
@Override
protected void onStart() {
super.onStart();
AppLog("onStart");
}
@Override
protected void onStop() {
super.onStop();
AppLog("onStop");
}
}