2020-12-08三段式 一加7pro gsi projecttreble

package com.example.myapplicatio;


import android.Manifest;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.biometrics.BiometricPrompt;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.example.myapplicatio.R;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;


import static android.media.AudioManager.STREAM_MUSIC;
import static java.lang.Runtime.getRuntime;

public class MainActivity extends AppCompatActivity {


    private TextView showState;

    private boolean isRunning = false;
    private LogDumper mLogDumper = null;
    private String mCurrentCommand = null;
    private static final String TAG = "getevent";
    private static final String COMMAND_GET_EVENT = "getevent -l /dev/input/event13";
    public Handler ohandler = new Handler() {

        public void handleMessage(Message msg) {
            Log.d("handleMessage", ": " +msg);
            switch (msg.what) {
                case 1:
                    //在这里可以进行UI操作
                    //对msg.obj进行String强制转换
                    String line=(String)msg.obj;
                    //text.setText(string);
                    AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                    NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                    if(line.indexOf("b") != -1){
                        //NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                        //Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
                        //getApplicationContext().startActivity(intent);
                        //audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                        //int hasWriteContactsPermission = checkSelfPermission(Manifest.permission.ACCESS_NOTIFICATION_POLICY);
                        //Log.d(TAG, "handleMessage: "+hasWriteContactsPermission+","+PackageManager.PERMISSION_GRANTED);
                        //if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
                            //requestPermissions(new String[] {Manifest.permission.ACCESS_NOTIFICATION_POLICY},

                            //return;
                        //}
                        mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY);
                    }else if(line.indexOf("a") != -1){
                        mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALARMS);
                        //audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                    }else if(line.indexOf("9") != -1){
                        mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
                        audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    }
                    break;
                default:
                    break;
            }
        }

    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //doExec("su");
        //doExec2("getevent -v /dev/input/event13");
        //showState.setText();
        //DataInputStream in = new DataInputStream(new FileInputStream("/dev/input/event13"));
        //GetEventTask aaa = new GetEventTask(ohandler);
        //.getInstance();
        //aaa.startMonitorEvent(ohandler);
        //ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_NOTIFICATION_POLICY},1);
        //Log.d(TAG, "onCreate: "+checkSelfPermission(Manifest.permission.ACCESS_NOTIFICATION_POLICY));
        start(COMMAND_GET_EVENT);
        NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//这个权限类似于通知获取权限
//MIUI中声音与铃声并没有提供相关开关
//该Activity在MIUI系统设置中没有入口
//有通知获取权限该权限默认开始
        if (!mNotificationManager.isNotificationPolicyAccessGranted()) {
            Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
            startActivity(intent);
        }

//关键代码

    }

    public void start(String command) {
        if (TextUtils.isEmpty(command) == true) {
            Log.e(TAG, "start --- command is null! ");
            return;
        }

        Log.d(TAG, "command = " + command);

        if (isRunning == true) {
            // 同一个命令不重复执行
            if (mCurrentCommand.equals(command) == true) {
                Log.d(TAG, "start --- command is already run!");
                return;
            } else {
                stop();
            }
        }

        if (mLogDumper == null) {
            mLogDumper = new LogDumper(command);
        }
        mLogDumper.start();
        mCurrentCommand = command;
        isRunning = true;
    }
    /**
     * 停止
     */
    public void stop() {
        Log.d(TAG, "--- stop ---");
        if (mLogDumper != null) {
            mLogDumper.stopLogs();
            mLogDumper = null;
            isRunning = false;
        } else {
            Log.d(TAG, "stop --- mLogDumper is null!");
        }
    }

    private class LogDumper extends Thread {
        private Process logcatProc;
        private BufferedReader mReader = null;
        private boolean mRunning = true;
        String command = null;

        int count = 0;

        public LogDumper(String command) {
            this.command = command;
        }

        public void stopLogs() {
            Log.d(TAG, "--- LogDumper stopLogs ---");
            mRunning = false;
        }

        @Override
        public void run() {
            Process process = null;
            DataOutputStream os = null;

            while (true) {
                Log.d(TAG, "run --- count = " + count++);
                try {
                    process = Runtime.getRuntime().exec("su");
                    os = new DataOutputStream(process.getOutputStream());
                    os.write(command.getBytes());
                    os.writeBytes("\n");
                    os.flush();
                    //os.writeBytes("exit\n");
                    //os.flush();

                    // 保存执行结果
                    mReader = new BufferedReader(new InputStreamReader(
                            process.getInputStream()), 1024);
                    String line = null;
                    while (mRunning && (line = mReader.readLine()) != null) {
                        if (!mRunning) {
                            break;
                        }
                        if(line.indexOf("DOWN") != -1)
                        {
                            //System.out.println("包含该字符串");
                            //Log.d(TAG, ": " + line);
                            //MainActivity.getContext(line);
                            //Myapplication.getinstance().getsystemservice();
                            Message message = new Message();
                            message.what = 1;
                            message.obj=line;
                            ohandler.sendMessage(message);
                        }
                        if (line.length() == 0) {
                            continue;
                        }

                        // 事件记录
                        //EventManage.getInstance().onTouchEvent();
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (logcatProc != null) {
                        logcatProc.destroy();
                        logcatProc = null;
                    }
                    if (mReader != null) {
                        try {
                            mReader.close();
                            mReader = null;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }
    }

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值