ExecuteCommandTask

 avatar
Trrieu112233
java
2 months ago
1.9 kB
6
Indexable
package com.example.testapp2;

import android.content.Context;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ExecuteCommandTask {
    private static final String TAG = "TEE_Autofill";
    private static final String SYSTEM_EXECUTABLE_PATH = "/vendor/bin/ca/ca";
    private static final String APP_EXECUTABLE_NAME = "ca";
    private static final int PERMISSION_REQUEST_CODE = 100;

    private boolean isSystemExecution;
    private Context context;

    public ExecuteCommandTask(Context context, boolean isSystemExecution){
        this.context = context;
        this.isSystemExecution = isSystemExecution;
    }

    public String executeSync(String... params) {
        StringBuilder output = new StringBuilder();
        Process process = null;
        try {
            // Xác định đường dẫn
            String path = isSystemExecution ? SYSTEM_EXECUTABLE_PATH : new File(context.getFilesDir(), APP_EXECUTABLE_NAME).getAbsolutePath();
            List<String> command = new ArrayList<>();
            command.add(path);
            Collections.addAll(command, params);

            process = Runtime.getRuntime().exec(command.toArray(new String[0]));

            // Đọc luồng output từ binary
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while((line = reader.readLine()) != null){
                output.append(line);
            }
            process.waitFor();
        } catch (Exception e) {
            Log.e(TAG, "Lỗi thực thi" + e.getMessage());
        } finally {
            if(process != null) process.destroy();
        }
        return output.toString().trim();
    }
}
Editor is loading...
Leave a Comment