android高版本适配使用Tools.java

随着android版本的提升,原生Tools不公开并且不能被正常使用,为了延续项目的功能,修改如下:

复制代码
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os;


import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.compat.annotation.UnsupportedAppUsage;
import android.util.Log;

import java.io.DataOutputStream;

/**

 * {@hide}
 */
@SystemApi
public class Tools {
    private static final String TAG = "Tools_agenew";

    /**
     * @hide
     */
    @SystemApi
    public static int system(@NonNull String cmd) {
        return executeCommand(cmd);
    }

    @UnsupportedAppUsage
    private Tools() {
    }

    /**
     * @hide
     */
    @SystemApi
    public synchronized static int executeCommand(String command) {
        int status = -1;
        java.lang.Process process = null;
        DataOutputStream os = null;
        Log.d(TAG, "exec_start:" + command);
        try {
            process = Runtime.getRuntime().exec(command);
            status = process.waitFor();
            if (status == 0) {
                Log.i(TAG, "exec succeed.");
            } else {
                Log.i(TAG, "exec failed.");
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (process != null) {
                    process.destroy();
                }
            } catch (Exception e) {
                Log.e(TAG, e.toString());
                e.printStackTrace();
            }
        }
        Log.d(TAG, "exec_end:" + command);
        return status;
    }

}

vendor/sprd/platform/frameworks/base/ex-interface/core/java/android/provider/UnisocSettings.java

复制代码
+        /**
+         * add for com.sprd.runtime test
+         * putInt 1 means start testing, 0 for the end.
+         * @hide
+         */
+       @UnisocHiddenApi
+        public static final String UNISOC_RUNTIME_USER_ACTION = "com.sprd.runtime.USER_ACTION";

注意:请更新对应平台的api和sepolicy,笔者使用展锐T606 android 13平台,对应修改如下

system-current.txt

复制代码
+  public class Tools {
+    method public static int executeCommand(String);
+    method public static int system(@NonNull String);
+  }

system-lint-baseline.txt

复制代码
+VisiblySynchronized: android.os.Tools#executeCommand(String):
+   Internal locks must not be exposed: method android.os.Tools.executeCommand(String)

unisoc-hidden-current.txt

复制代码
+  public class Tools {
+   method public static int executeCommand(String);
+    method public static void system(@NonNull String);
+  }

device/sprd/mpool/sepolicy/vendor/untrusted_app.te

复制代码
+allow untrusted_app property_socket:sock_file { write };
+allow untrusted_app init:unix_stream_socket { connectto };
+allow untrusted_app default_prop:property_service { set };

system/sepolicy/private/app_neverallows.te

复制代码
# Do not allow untrusted apps to connect to the property service
# or set properties. b/10243159
#neverallow { all_untrusted_apps -mediaprovider } property_socket:sock_file write;
#neverallow { all_untrusted_apps -mediaprovider } init:unix_stream_socket connectto;
#neverallow { all_untrusted_apps -mediaprovider } property_type:property_service set;

system/sepolicy/private/property.te

复制代码
neverallow {
  domain
  -init
+  -untrusted_app
} default_prop:property_service set;
相关推荐
某空_36 分钟前
【Android】使用ViewPager2实现简单的轮播图
java
武子康37 分钟前
Java-145 深入浅出 MongoDB 基本操作详解:数据库查看、切换、创建集合与删除完整教程
java·数据库·sql·mysql·mongodb·性能优化·系统架构
练习时长一年1 小时前
Spring内置功能
java·前端·spring
铉铉这波能秀1 小时前
如何在Android Studio中使用Gemini进行AI Coding
android·java·人工智能·ai·kotlin·app·android studio
_Yoke1 小时前
Java 枚举多态在系统中的实战演进:从枚举策略到自动注册
java·springboot·策略模式
人生导师yxc1 小时前
Java中Mock的写法
java·开发语言
青岛少儿编程-王老师2 小时前
CCF编程能力等级认证GESP—C++5级—20250927
java·数据结构·c++
毕设源码-郭学长2 小时前
【开题答辩全过程】以 办公管理系统为例,包含答辩的问题和答案
java·eclipse
脑子慢且灵2 小时前
C语言与Java语言编译过程及文件类型
java·c语言·开发语言·汇编·编辑器
是2的10次方啊2 小时前
看完JDK、Spring官方架构图,我发现:大厂的图也就这样!
java