Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
haozi committed Apr 17, 2019
0 parents commit 2c1d511
Show file tree
Hide file tree
Showing 57 changed files with 1,774 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
1 change: 1 addition & 0 deletions Toastlib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
34 changes: 34 additions & 0 deletions Toastlib/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 28



defaultConfig {
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
21 changes: 21 additions & 0 deletions Toastlib/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
2 changes: 2 additions & 0 deletions Toastlib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.toastlib" />
74 changes: 74 additions & 0 deletions Toastlib/src/main/java/com/android/toastlib/ToastUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.android.toastlib;

import android.app.Application;
import android.os.Build;
import android.util.Log;

import com.android.toastlib.toast.ActivityToast;
import com.android.toastlib.toast.IToast;
import com.android.toastlib.toast.SystemToast;
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 String TAG = "ToastUtils";
private static IToast topToast = null;
private static IToast systemToast = null;
private static ToastHandler topToastHandler = null;
private static ToastHandler systemToastHandler = null;

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);
}else{
topToast = new TopToast(application);
}

//init SystemToast
systemToast = new SystemToast(application);
topToastHandler = new ToastHandler(topToast);
systemToastHandler = new ToastHandler(systemToast);
}

public static void show(int id,int toastStyle){
checkToastUtils();
IToast toast = null;
try {
toast = toastStyle == TOP_STYLE ? topToast :systemToast;
show(toast.getView().getContext().getResources().getText(id),toastStyle);
} catch (Exception e) {
Log.e(TAG,e.toString());
}
}

public static void show(CharSequence message,int toastStyle) {
checkToastUtils();
ToastHandler toastHandler = null;
try {
toastHandler = toastStyle == TOP_STYLE ? topToastHandler : systemToastHandler;
toastHandler.add(message);
toastHandler.show();
} catch (Exception e) {
Log.e(TAG,e.toString());
}
}

/**
* 取消吐司的显示
*/
public static void cancel() {
checkToastUtils();
topToastHandler.cancel();
systemToastHandler.cancel();
}

private static void checkToastUtils() {
if(topToast == null || systemToast == null
|| topToastHandler == null || systemToastHandler == null){
throw new IllegalStateException("ToastUtils has not been initialized");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.android.toastlib.toast;

import android.app.Application;

import com.android.toastlib.util.ActivityToastHelper;

public class ActivityToast extends TopToast {
// 吐司弹窗显示辅助类
private final ActivityToastHelper mToastHelper;

public ActivityToast(Application application) {
super(application);
mToastHelper = new ActivityToastHelper(toast, application);
}

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

@Override
public void showToast() {
mToastHelper.show();
}
}
13 changes: 13 additions & 0 deletions Toastlib/src/main/java/com/android/toastlib/toast/IToast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.android.toastlib.toast;

import android.view.View;

public interface IToast{
void setMessageText(CharSequence s);

void showToast();

void cancel();

View getView();
}
42 changes: 42 additions & 0 deletions Toastlib/src/main/java/com/android/toastlib/toast/SystemToast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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;

/**
* Created by hao.shi on 2016/4/20.
* Please use this for normal Toast ,or it will exception for above Build7.1.1
*/
public class SystemToast implements IToast {
private final static String TAG = "SystemToast";
private Toast toast;

public SystemToast(Context context) {
toast = Toast.makeText(context,"",Toast.LENGTH_SHORT);
}

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

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

@Override
public void cancel() {
toast.cancel();
}

@Override
public View getView() {
return toast.getView();
}
}
84 changes: 84 additions & 0 deletions Toastlib/src/main/java/com/android/toastlib/toast/TopToast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.android.toastlib.toast;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

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

import java.lang.reflect.Field;

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

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

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

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

@Override
public void cancel() {
toast.cancel();
}

@Override
public View getView() {
return toast.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.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
toast = new Toast(context);
toast.setView(toastView);
toast.setGravity(Gravity.TOP,0,0);
makeToastSelfViewAnim();
}

private void makeToastSelfViewAnim(){
try {
Field mTNField = toast.getClass().getDeclaredField("mTN");
mTNField.setAccessible(true);
Object mTNObject = mTNField.get(toast);
Class tnClass = mTNObject.getClass();
Field paramsField = tnClass.getDeclaredField("mParams");
/**由于WindowManager.LayoutParams mParams的权限是private*/
paramsField.setAccessible(true);
WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) paramsField.get(mTNObject);
layoutParams.windowAnimations = R.style.top_toast_style;
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.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;
} catch (NoSuchFieldException e) {
e.printStackTrace();
Log.e("haozi","e.to=="+e.toString());
} catch (IllegalAccessException e) {
e.printStackTrace();
Log.e("haozi","e.to=="+e.toString());
}
}
}
Loading

0 comments on commit 2c1d511

Please sign in to comment.