Android 小米手机劫持toast提示,内容被追加应用名称的解决方案

17 篇文章 0 订阅

背景:

Android在开发过程中,经常使用toast消息提示。Toast为系统提供工具使用方法:

Toast.make(Context context,String msg,int durcation).show();

目前小米厂家的系统,已对toast底层做了修改,msg=appLabel+":"+msg,这种看似没问题,但是在插件开发中,如果资源出现错乱,Resid出现指向错误,就会导致,applabel显示异常。

分析:

/**
 * Show the view for the specified duration.
 */
public void show() {
    if (mNextView == null) {
        throw new RuntimeException("setView must have been called");
    }

    INotificationManager service = getService();
    String pkg = mContext.getOpPackageName();
    TN tn = mTN;
    tn.mNextView = mNextView;

    try {
        service.enqueueToast(pkg, tn, mDuration);
    } catch (RemoteException e) {
        // Empty
    }
}

Toast的show()方法,调用了INotificationManager,通过通知enqueueToast去提交信息。

mNextView为需要显示的view,如果我们通过hook,获取TN对象,获取到mNextView,把mNextView中的text修改成我们原有的?目前是可以实现的,一种是动态代理INotificationManager,另外就是不通过mNextView去做设置,直接通过Tost设置。

解决方案1:

简单修改Toast,通过设置setext(String msg)

public void setText(CharSequence s) {
    if (mNextView == null) {
        throw new RuntimeException("This Toast was not created with Toast.makeText()");
    }
    TextView tv = mNextView.findViewById(com.android.internal.R.id.message);
    if (tv == null) {
        throw new RuntimeException("This Toast was not created with Toast.makeText()");
    }
    tv.setText(s);
}
Toast toast=Toast.make(getapplication(),null,Toast.LENGTH_SHORT);

toast.setText("你好");

这种方法:

public static Toast makeText(@NonNull Context context, @Nullable Looper looper,
        @NonNull CharSequence text, @Duration int duration) {
    Toast result = new Toast(context, looper);

    LayoutInflater inflate = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
    TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
    tv.setText(text);

    result.mNextView = v;
    result.mDuration = duration;

    return result;
}

还是内部创建一个临时的,然后通过mNextView创建一个视图,这样就绕开了TN这个环节,但是在minu12还是有问题,绕不开。

解决方案2:

既然绕不开,那我们是否通过Hook,动态代理INotificationManager,对enqueueToast进行拦截?

证明是可行的。

INotificationManager是系统类,但是Toast中已定义了,可以通过反射拿到,

 2.2一个对象被使用,就要实例化。INotificationManager实例化,可以通过getService()来获取

Method ServiceMethod = toastClass.getDeclaredMethod("getService", new Class[0]);

获取到这个方法,我们需要进行调用:

//invoke是反射中的方法调用,不懂的,可以先了解反射机制

final Object service = ServiceMethod .invoke(null);

这个时候我们就可以设置创建代理:

Object proxy = Proxy.newProxyInstance(Thread.class.getClassLoader(), new Class[]{INotificationManager}, new InvocationHandler(){};

INotificationManager:是系统类,通过反射获取。

//service这个时候已被初始化了,静态类,设置代理
sServiceField.set(null, proxy);

接下:我们要处理代理获取的类

1. Toast通过TN的mNextView来完成消息的通过,所以先获取内部类

//内部类反射
Class<?> tnClass = Class.forName(Toast.class.getName() + "$TN");
// 获取mNextView的Field
Field mNextViewField = tnClass.getDeclaredField("mNextView");

// 获取mNextView实例,查看元布局

LinearLayout mNextView = (LinearLayout) mNextViewField.get(item);
//获取布局下的子view,获取text的,这样就完成text的过滤和重新赋值
TextView childView = (TextView) mNextView.getChildAt(0);
CharSequence text = childView.getText();

//动态代理:

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
             //enqueueToast的是service发送通知消息的,通过这个方法来获取TN View
        if (method.getName().equals("enqueueToast")) {
            modifyMethod(args[1], context);
        }


        return method.invoke(service, args);
    }
});

//返回当前service的代理
return sServiceField.set(null, proxy);

//**************************************************************************

完整代码如下:

public class ToastInvokenManager {

    public void init(final Context context) {
        Class<Toast> toastClass = Toast.class;
        try {

            Field sServiceField = toastClass.getDeclaredField("sService");

            sServiceField.setAccessible(true);

            Class INotificationManager = Class.forName("android.app.INotificationManager");

            Method getServiceMethod = toastClass.getDeclaredMethod("getService", new Class[0]);
            getServiceMethod.setAccessible(true);
            final Object service = getServiceMethod.invoke(null);
            Object proxy = Proxy.newProxyInstance(Thread.class.getClassLoader(), new Class[]{INotificationManager}, new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                    if (method.getName().equals("enqueueToast")) {
                        modifyMethod(args[1], context);
                    }


                    return method.invoke(service, args);
                }
            });

            sServiceField.set(null, proxy);
        } catch (Exception e) {

        }


    }

    private void modifyMethod(Object item, Context context) {
        if (item == null)
            return;
        // 获取TN的class

        try {
            //内部类反射
            Class<?> tnClass = Class.forName(Toast.class.getName() + "$TN");
            // 获取mNextView的Field
            Field mNextViewField = tnClass.getDeclaredField("mNextView");
            mNextViewField.setAccessible(true);


            // 获取mNextView实例,查看元布局

            LinearLayout mNextView = (LinearLayout) mNextViewField.get(item);

            // 获取textview

            if (mNextView != null && mNextView.getChildCount() > 0) {
                TextView childView = (TextView) mNextView.getChildAt(0);
                CharSequence text = childView.getText();
                if (!TextUtils.isEmpty(text)) {
                    String appName = getAppLabel(context);
                    if (!TextUtils.isEmpty(appName) &&          text.toString().startsWith(appName)) {
                        int count = appName.length() + 1;
                        String val = text.toString().substring(count, text.length());
                        childView.setText(val);
                        childView.invalidate();
                    }

                }


            }

        } catch (Exception e) {
              Log.d("e=" + e.getMessage());

        }


    }

    private String getAppLabel(Context context) {
        PackageManager manager = context.getPackageManager();
        try {
            ApplicationInfo pkinfo = manager.getApplicationInfo(context.getPackageName(), 0);
            if (pkinfo != null) {
                return context.getResources().getString(pkinfo.labelRes) + "";
            }


        } catch (Exception e) {

        }

        return null;
    }
}

//***********************************************************************

在Application 初始化即可。

资源下载地址

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值