Skip to content

Commit

Permalink
sdkversion
Browse files Browse the repository at this point in the history
  • Loading branch information
haozi committed Apr 24, 2019
1 parent 79aaac9 commit 4378b0e
Show file tree
Hide file tree
Showing 18 changed files with 519 additions and 73 deletions.
50 changes: 44 additions & 6 deletions Toastlib/src/main/java/com/android/toastlib/ToastUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,39 @@
import android.os.Build;
import android.util.Log;

import com.android.toastlib.toast.ActivityCustomizeToast;
import com.android.toastlib.toast.ActivityToast;
import com.android.toastlib.toast.IToast;
import com.android.toastlib.toast.SystemToast;
import com.android.toastlib.toast.TopCustomizeToast;
import com.android.toastlib.toast.TopToast;
import com.android.toastlib.util.ToastHandler;

public class ToastUtils {
public final static int TOP_STYLE = 0x1023;
public final static int SYSTEM_STYLE = 0x1024;
/**
* 顶部下拉回弹样式
*/
public final static int TOP_TOAST_STYLE = 0x1023;
/**
*系统透明度样式
*/
public final static int SYSTEM_TOAST_STYLE = 0x1024;
public final static String TAG = "ToastUtils";
private static IToast topToast = null;
private static IToast systemToast = null;
private static ToastHandler topToastHandler = null;
private static ToastHandler systemToastHandler = null;

/**
* Should init in Application.java
* @param application
*/
public static void init(Application application){
//init TopToast 大于7.1.1 使用ActivityToast,否则使用TopToast
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1){
topToast = new ActivityToast(application);
topToast = new ActivityCustomizeToast(application);
}else{
topToast = new TopToast(application);
topToast = new TopCustomizeToast(application);
}

//init SystemToast
Expand All @@ -33,22 +45,48 @@ public static void init(Application application){
systemToastHandler = new ToastHandler(systemToast);
}

/**
* Show Toast use the default TOP_TOAST_STYLE
* @param id
*/
public static void show(int id){
show(id,TOP_TOAST_STYLE);
}

/**
* Show Toast use the default TOP_TOAST_STYLE
* @param message
*/
public static void show(CharSequence message){
show(message,TOP_TOAST_STYLE);
}

/**
* Show Toast with the Toast style you choosed
* @param id
* @param toastStyle
*/
public static void show(int id,int toastStyle){
checkToastUtils();
IToast toast = null;
try {
toast = toastStyle == TOP_STYLE ? topToast :systemToast;
toast = toastStyle == TOP_TOAST_STYLE ? topToast :systemToast;
show(toast.getView().getContext().getResources().getText(id),toastStyle);
} catch (Exception e) {
Log.e(TAG,e.toString());
}
}

/**
* Show Toast with the Toast style you choosed
* @param message
* @param toastStyle
*/
public static void show(CharSequence message,int toastStyle) {
checkToastUtils();
ToastHandler toastHandler = null;
try {
toastHandler = toastStyle == TOP_STYLE ? topToastHandler : systemToastHandler;
toastHandler = toastStyle == TOP_TOAST_STYLE ? topToastHandler : systemToastHandler;
toastHandler.add(message);
toastHandler.show();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.android.toastlib.toast;

import android.app.Application;
import android.view.View;

import com.android.toastlib.util.ActivityCustomizeToastHandler;

public class ActivityCustomizeToast implements IToast{
// 吐司弹窗显示辅助类
private final ActivityCustomizeToastHandler mToastHelper;

public ActivityCustomizeToast(Application application) {
mToastHelper = new ActivityCustomizeToastHandler(application);
}

@Override
public void cancel() {
// 取消显示
mToastHelper.removeView();
}

@Override
public View getView() {
return mToastHelper.getView();
}

@Override
public void showToast(CharSequence s) {
mToastHelper.setMessage(s);
if(mToastHelper.isToastShowing())
mToastHelper.sendHideToastMessage(s);
else
mToastHelper.sendShowToastMessage(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public void cancel() {
}

@Override
public void showToast() {
public void showToast(CharSequence s) {
if(messageView != null){
messageView.setText(s);
}
mToastHelper.show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import android.view.View;

public interface IToast{
void setMessageText(CharSequence s);

void showToast();
void showToast(CharSequence s);

void cancel();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.android.toastlib.toast;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
Expand All @@ -20,13 +18,8 @@ public SystemToast(Context context) {
}

@Override
public void setMessageText(CharSequence s) {
public void showToast(CharSequence s) {
toast.setText(s);
Log.i("haozi","setMessageText =="+s);
}

@Override
public void showToast() {
toast.show();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.android.toastlib.toast;

import android.app.Application;
import android.view.View;

import com.android.toastlib.util.TopCustomizeToastHandler;

public class TopCustomizeToast implements IToast{
private TopCustomizeToastHandler topToastHandler;
public TopCustomizeToast(Application application){
topToastHandler = new TopCustomizeToastHandler(application);
}

@Override
public void showToast(CharSequence s) {
topToastHandler.setMessage(s);
if(topToastHandler.isToastShowing()){
topToastHandler.sendHideToastMessage(s);
}else {
topToastHandler.sendShowToastMessage(s);
}
}

@Override
public void cancel() {
topToastHandler.removeView();
}

@Override
public View getView() {
return topToastHandler.getView();
}
}
18 changes: 8 additions & 10 deletions Toastlib/src/main/java/com/android/toastlib/toast/TopToast.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,26 @@
import android.widget.Toast;

import com.android.toastlib.R;
import com.android.toastlib.ToastUtils;
import com.android.toastlib.util.ScreenUtils;

import java.lang.reflect.Field;

public class TopToast implements IToast{
public class TopToast implements IToast {
private final static String TAG = "TopToast";
private static TextView messageView = null;
public static TextView messageView = null;
public Toast toast = null;

public TopToast(Context context) {
initToast(context);
}

@Override
public void setMessageText(CharSequence s) {
public void showToast(CharSequence s) {
if(messageView != null){
messageView.setText(s);
}
}

@Override
public void showToast() {
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}

Expand All @@ -48,7 +46,7 @@ public View getView() {
private void initToast(Context context) {
View toastView = LayoutInflater.from(context).inflate(R.layout.common_toast_layout,null);
messageView = toastView.findViewById(R.id.common_toast_text);
toastView.setMinimumHeight(ScreenUtils.getActionBarHeight(context)+ScreenUtils.getStatusHeight(context));
toastView.setMinimumHeight(ScreenUtils.getActionBarHeight(context)+ ScreenUtils.getStatusHeight(context));
toastView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
toast = new Toast(context);
toast.setView(toastView);
Expand All @@ -75,10 +73,10 @@ private void makeToastSelfViewAnim(){
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
} catch (NoSuchFieldException e) {
e.printStackTrace();
Log.e("haozi","e.to=="+e.toString());
Log.e(TAG,e.toString());
} catch (IllegalAccessException e) {
e.printStackTrace();
Log.e("haozi","e.to=="+e.toString());
Log.e(TAG,e.toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.android.toastlib.util;

import android.app.Application;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.WindowManager;

public class ActivityCustomizeToastHandler extends BaseCustomToastHandler {
private ActivityLifecycleCallback mWindowHelper;
private String mPackageName;

public ActivityCustomizeToastHandler(Application context) {
super(context);
init(context);
}

private void init(Application application) {
mPackageName = application.getPackageName();
mWindowHelper = new ActivityLifecycleCallback(this, application);
}

@Override
public WindowManager getWindowManager() {
return mWindowHelper.getWindowManager();
}

@Override
public WindowManager.LayoutParams getWindowManagerLayoutParams() {
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.format = PixelFormat.TRANSLUCENT;
// params.windowAnimations = R.style.top_toast_style;
params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
params.packageName = mPackageName;
// 重新初始化位置
params.gravity = Gravity.TOP;
params.x = 0;
params.y = 0;

return params;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ final class ActivityLifecycleCallback implements Application.ActivityLifecycleCa
private final ArrayMap<String, Activity> mActivitySet = new ArrayMap<>();

// 用于 Activity 暂停时移除 WindowManager
private final ActivityToastHelper mToastHelper;
private ActivityToastHelper mToastHelper;
private ActivityCustomizeToastHandler mToastHandler;

// 当前 Activity 对象标记
private String mCurrentTag;
Expand All @@ -33,6 +34,11 @@ final class ActivityLifecycleCallback implements Application.ActivityLifecycleCa
application.registerActivityLifecycleCallbacks(this);
}

ActivityLifecycleCallback(ActivityCustomizeToastHandler helper, Application application) {
mToastHandler = helper;
application.registerActivityLifecycleCallbacks(this);
}

/**
* 获取一个WindowManager对象
*
Expand All @@ -42,8 +48,6 @@ WindowManager getWindowManager() throws NullPointerException {
if (mCurrentTag != null) {
// 如果使用的 WindowManager 对象不是当前 Activity 创建的,则会抛出异常
// android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Log.i("shihao","getWindowManager mCurrentTag =="+mCurrentTag);
Log.i("shihao","getWindowManager mActivityset .size =="+mActivitySet.size());
Activity activity = mActivitySet.get(mCurrentTag);
if (activity != null) {
return getWindowManagerObject(activity);
Expand All @@ -59,9 +63,7 @@ WindowManager getWindowManager() throws NullPointerException {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
mCurrentTag = getObjectTag(activity);
Log.i("shihao","onActivityCreated mCurrentTag =="+mCurrentTag);
mActivitySet.put(mCurrentTag, activity);
Log.i("shihao","onActivityCreated mActivityset .size =="+mActivitySet.size());
}

@Override
Expand All @@ -72,8 +74,6 @@ public void onActivityStarted(Activity activity) {
@Override
public void onActivityResumed(Activity activity) {
mCurrentTag = getObjectTag(activity);
Log.i("shihao","onActivityResumed mCurrentTag =="+mCurrentTag);
Log.i("shihao","onActivityResumed mActivityset .size =="+mActivitySet.size());
}

// A跳转B页面的生命周期方法执行顺序:
Expand All @@ -82,7 +82,10 @@ public void onActivityResumed(Activity activity) {
@Override
public void onActivityPaused(Activity activity) {
// 取消这个吐司的显示
mToastHelper.cancel();
if(mToastHandler != null)
mToastHandler.removeView();
if(mToastHelper != null)
mToastHelper.cancel();
// 不能放在 onStop 或者 onDestroyed 方法中,因为此时新的 Activity 已经创建完成,必须在这个新的 Activity 未创建之前关闭这个 WindowManager
// 调用取消显示会直接导致新的 Activity 的 onCreate 调用显示吐司可能显示不出来的问题(立马显示然后立马消失的效果)
}
Expand All @@ -96,10 +99,7 @@ public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
@Override
public void onActivityDestroyed(Activity activity) {
// 移除对这个 Activity 的引用
Log.i("shihao","onActivityDestroyed mCurrentTag =="+mCurrentTag);
Log.i("shihao","onActivityDestroyed mActivityset .size =="+mActivitySet.size());
mActivitySet.remove(getObjectTag(activity));
Log.e("shihao","onActivityDestroyed mActivityset .size11111 =="+mActivitySet.size());
// 如果当前的 Activity 是最后一个的话
if (getObjectTag(activity).equals(mCurrentTag)) {
// 清除当前标记
Expand Down
Loading

0 comments on commit 4378b0e

Please sign in to comment.