1、fix skipactivity/baseappcompatactivity potential npe

2、fix appcontroller logic
This commit is contained in:
CsHeng
2017-05-08 15:22:57 +08:00
parent 1c6907373b
commit 375f538bc1
8 changed files with 139 additions and 126 deletions

View File

@ -10,19 +10,16 @@ import android.support.v4.util.ArrayMap;
import android.util.Log;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.stetho.Stetho;
import com.gh.common.util.*;
import com.gh.common.util.DataUtils;
import com.gh.common.util.HttpsUtils;
import com.gh.gamecenter.BuildConfig;
import com.umeng.message.*;
import com.xiaomi.channel.commonutils.logger.LoggerInterface;
import com.xiaomi.mipush.sdk.Logger;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class AppController extends Application {
//public class AppController extends TinkerApplication {
public static final String TAG = AppController.class.getSimpleName();
// TODO xiaomi push appid 2882303761517352993
@ -37,11 +34,6 @@ public class AppController extends Application {
private static AppController mInstance;
private static ArrayMap<String, Object> objectMap = new ArrayMap<>();
// public AppController() {
// super(ShareConstants.TINKER_ENABLE_ALL, "com.gh.base.AppControllerLike",
// "com.tencent.tinker.loader.TinkerLoader", false);
// }
public static void put(String key, Object object) {
if (objectMap == null) {
objectMap = new ArrayMap<>();
@ -97,70 +89,77 @@ public class AppController extends Application {
HttpsUtils.initHttpsUrlConnection(this);
// AppUncaughtHandler uncaHandler = new AppUncaughtHandler(this);
// Thread.setDefaultUncaughtExceptionHandler(uncaHandler);
//TODO 强烈不建议开发阶段开启这个Handler必须处理错误
if (!BuildConfig.DEBUG) {
AppUncaughtHandler appUncaughtHandler = new AppUncaughtHandler(this);
Thread.setDefaultUncaughtExceptionHandler(appUncaughtHandler);
}
mInstance = this;
if (BuildConfig.DEBUG) {
Stetho.initializeWithDefaults(this);
}
// 注册push服务注册成功后会向GHPushMessageReceiver发送广播
// 可以从GHPushMessageReceiver的onCommandResult方法中MiPushCommandMessage对象参数中获取注册信息
if (shouldInit()) {
MiPushClient.registerPush(this, APP_ID, APP_KEY);
}
/**
* 注册push服务注册成功后会向{@link GHPushMessageReceiver}发送广播
* 可以从{@link GHPushMessageReceiver#onCommandResult(Context, MiPushCommandMessage)}
* 的{@link MiPushCommandMessage} 对象参数中获取注册信息
*/
// if (shouldInit()) {
// MiPushClient.registerPush(this, APP_ID, APP_KEY);
// }
LoggerInterface newLogger = new LoggerInterface() {
@Override
public void setTag(String tag) {
// ignore
}
@Override
public void log(String content, Throwable t) {
Log.d(TAG, content, t);
}
@Override
public void log(String content) {
Log.d(TAG, content);
}
};
Logger.setLogger(this, newLogger);
// LoggerInterface newLogger = new LoggerInterface() {
//
// @Override
// public void setTag(String tag) {
// // ignore
// }
//
// @Override
// public void log(String content, Throwable t) {
// Log.d(TAG, content, t);
// }
//
// @Override
// public void log(String content) {
// Log.d(TAG, content);
// }
// };
// Logger.setLogger(this, newLogger);
//友盟推送
final PushAgent mPushAgent = PushAgent.getInstance(this);
//注册推送服务每次调用register方法都会回调该接口
mPushAgent.register(new IUmengRegisterCallback() {
@Override
public void onSuccess(String deviceToken) {
//注册成功会返回device token
Utils.log("deviceToken::" + deviceToken);
//设置别名
mPushAgent.addExclusiveAlias(TokenUtils.getDeviceId(getApplicationContext())
, "GHDID", new UTrack.ICallBack() {
@Override
public void onMessage(boolean b, String s) {
Utils.log(StringUtils.buildString("ExclusiveAlias::", String.valueOf(b), "==", s));
}
});
}
@Override
public void onFailure(String s, String s1) {
Utils.log("deviceToken::" + "注册失败");
}
});
// 友盟推送数据处理
mPushAgent.setNotificationClickHandler(new GHUmengNotificationClickHandler());
// final PushAgent pushAgent = PushAgent.getInstance(this);
//
//
// //注册推送服务每次调用register方法都会回调该接口
// pushAgent.register(new IUmengRegisterCallback() {
//
// @Override
// public void onSuccess(String deviceToken) {
// //注册成功会返回device token
// Utils.log("deviceToken::" + deviceToken);
//
// //设置别名
// pushAgent.addExclusiveAlias(TokenUtils.getDeviceId(getApplicationContext()),
// "GHDID", new UTrack.ICallBack() {
// @Override
// public void onMessage(boolean b, String s) {
// Utils.log(StringUtils.buildString("ExclusiveAlias::", String.valueOf(b), "==", s));
//
// }
// });
// }
//
// @Override
// public void onFailure(String s, String s1) {
// Utils.log("deviceToken::" + "注册失败");
// }
// });
//
// // 友盟推送数据处理
// pushAgent.setNotificationClickHandler(new GHUmengNotificationClickHandler());
// // 监听屏幕状态广播
// if (shouldInit()) {

View File

@ -200,8 +200,11 @@ abstract class BaseAppCompatActivity extends BaseAppCompatActivityLog implements
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContentView = getLayoutInflater().inflate(getLayoutId(), null);
setContentView(getLayoutId());
final int layoutId = getLayoutId();
if (layoutId != 0) {
mContentView = getLayoutInflater().inflate(layoutId, null);
setContentView(mContentView);
}
if (savedInstanceState == null) {
handleRedirectIntent(getIntent());
}

View File

@ -12,46 +12,47 @@ import com.gh.gamecenter.R;
*/
abstract class BaseAppCompatToolBarActivity extends BaseAppCompatActivity implements ToolbarController {
protected Toolbar mToolbar;
protected Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initNavigationBar();
Util_Window.initStatusBarColor(getWindow(), ContextCompat.getColor(this, R.color.theme));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initNavigationBar();
Util_Window.initStatusBarColor(getWindow(), ContextCompat.getColor(this, R.color.theme));
}
private void initNavigationBar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar_navigation);
if ( mToolbar!=null)
mToolbar.setTitle("");
setSupportActionBar(mToolbar);
}
private void initNavigationBar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar_navigation);
if (mToolbar != null) {
mToolbar.setTitle("");
}
setSupportActionBar(mToolbar);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
return onNavigationIconClicked();
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
return onNavigationIconClicked();
}
return super.onOptionsItemSelected(item);
}
protected abstract boolean onNavigationIconClicked();
protected abstract boolean onNavigationIconClicked();
@Override
public void setNavigationTitle(int res) {
mToolbar.setTitle(res);
}
@Override
public void setNavigationTitle(int res) {
mToolbar.setTitle(res);
}
@Override
public void setNavigationTitle(CharSequence res) {
mToolbar.setTitle(res);
}
@Override
public void setNavigationTitle(CharSequence res) {
mToolbar.setTitle(res);
}
@Override
public Toolbar getToolBar() {
return mToolbar;
}
@Override
public Toolbar getToolBar() {
return mToolbar;
}
}

View File

@ -13,12 +13,15 @@ import com.gh.gamecenter.R;
*/
public class NotificationUtils {
public static final String ACTION_INSTALL = "com.gh.gamecenter.INSTALL";
public static final String ACTION_DOWNLOAD = "com.gh.gamecenter.DOWNLOAD";
// 快传传输完成消息
public static void showKuaiChuanDoneNotification(Context context, String apkPath, String apkName, String packName) {
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manager = getNotificationManager(context);
Intent intent = new Intent();
intent.putExtra("path", apkPath);
intent.setAction("com.gh.gamecenter.INSTALL");
intent.setAction(ACTION_INSTALL);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0x321,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
String title = "接收完成,点击立即安装";
@ -31,14 +34,14 @@ public class NotificationUtils {
.setContentText(title)
.setContentIntent(pendingIntent).build();
notification.flags |= Notification.FLAG_AUTO_CANCEL; // // FLAG_AUTO_CANCEL表明当通知被用户点击时通知将被清除。
nManager.notify(packName, 0x321, notification);
manager.notify(packName, 0x321, notification);
}
public static void showDownloadDoneNotification(Context context, DownloadEntity downloadEntity, int flag) {
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manager = getNotificationManager(context);
Intent intent = new Intent();
intent.putExtra("path", downloadEntity.getPath());
intent.setAction("com.gh.gamecenter.INSTALL");
intent.setAction(ACTION_INSTALL);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, flag,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
String text;
@ -64,10 +67,15 @@ public class NotificationUtils {
.setContentIntent(pendingIntent).build();
// notification.defaults = Notification.DEFAULT_SOUND;// 添加系统默认声音
notification.flags |= Notification.FLAG_AUTO_CANCEL; // // FLAG_AUTO_CANCEL表明当通知被用户点击时通知将被清除。
nManager.notify(flag, notification);
manager.notify(flag, notification);
}
private static NotificationManager getNotificationManager(Context context) {
return (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
}
public static void showDownloadingNotification(Context context) {
NotificationManager manager = getNotificationManager(context);
int downloadingSize = 0;
for (DownloadEntity entity : DownloadManager.getInstance(context).getAll()) {
if (entity.getStatus().equals(DownloadStatus.downloading)
@ -79,14 +87,11 @@ public class NotificationUtils {
}
}
if (downloadingSize == 0) {
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.cancel(0x123);
manager.cancel(0x123);
} else {
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent();
intent.setAction("com.gh.gamecenter.DOWNLOAD");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0x123,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent.setAction(ACTION_DOWNLOAD);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0x123, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.logo)
.setTicker("点击查看详情")
@ -95,7 +100,7 @@ public class NotificationUtils {
.setContentIntent(pendingIntent).build();
// notification.defaults = Notification.DEFAULT_SOUND;// 添加系统默认声音
notification.flags |= Notification.FLAG_NO_CLEAR; // 通知无法手动清除
nManager.notify(0x123, notification);
manager.notify(0x123, notification);
}
}

View File

@ -18,16 +18,18 @@ public class SkipActivity extends BaseActivity {
@Override
protected int getLayoutId() {
return 0;
//TODO 暂时无意义设置避免崩溃罢了修改完主题和Toolbar相关内容会干掉这个
return R.layout.activity_main;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent() != null) {
String host = getIntent().getData().getHost();
String id = getIntent().getData().getPath();
Uri uri = getIntent().getData();
if (uri != null) {
String host = uri.getHost();
String id = uri.getPath();
if (!TextUtils.isEmpty(id)) {
id = id.substring(1);
}
@ -55,8 +57,6 @@ public class SkipActivity extends BaseActivity {
intent.putExtra(KEY_NAME, getIntent().getData().getQueryParameter(KEY_NAME));
break;
case HOST_SUGGESTION:
Uri uri = getIntent().getData();
String content = String.format("【%s-%s-V%s】",
uri.getQueryParameter(KEY_GAME_NAME),
PlatformUtils.getInstance(this).getPlatformName(uri.getQueryParameter(KEY_PLATFORM)),
@ -96,7 +96,6 @@ public class SkipActivity extends BaseActivity {
intent.putExtra(KEY_NAME, getIntent().getData().getQueryParameter(KEY_NAME));
break;
case HOST_SUGGESTION:
Uri uri = getIntent().getData();
String content = String.format("【%s-%s-V%s】",
uri.getQueryParameter(KEY_GAME_NAME),
PlatformUtils.getInstance(this).getPlatformName(uri.getQueryParameter(KEY_PLATFORM)),

View File

@ -3,8 +3,7 @@ package com.gh.gamecenter.receiver;
import android.content.*;
import android.os.Bundle;
import android.text.TextUtils;
import com.gh.common.util.ClassUtils;
import com.gh.common.util.RunningUtils;
import com.gh.common.util.*;
import com.gh.gamecenter.SplashScreenActivity;
/**
@ -13,9 +12,11 @@ import com.gh.gamecenter.SplashScreenActivity;
*/
public class ActivitySkipReceiver extends BroadcastReceiver {
public static final String ACTION_ACTIVITY_SKIP = "com.gh.gamecenter.ACTIVITYSKIP";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.gh.gamecenter.ACTIVITYSKIP")) {
if (ACTION_ACTIVITY_SKIP.equals(intent.getAction())) {
Bundle bundle = intent.getExtras();
if (RunningUtils.isRunning(context)) {
// 应用正在运行,前台或后台

View File

@ -74,6 +74,7 @@ public class AppStaticService extends Service {
try {
isScreenOn = mPowerManager.isScreenOn(); //判断屏幕状态(屏幕亮起/屏幕熄灭)
if (isScreenOn) {
//TODO remove this
// // 获取当前正在运行的任务
mTaskInfo = mActivityManager.getRunningTasks(1);