diff --git a/MiBridge.jar b/MiBridge.jar index 9c76f18..9d89cd0 100644 Binary files a/MiBridge.jar and b/MiBridge.jar differ diff --git a/TestMiBridge/app/libs/MiBridge.jar b/TestMiBridge/app/libs/MiBridge.jar index 9c76f18..9d89cd0 100644 Binary files a/TestMiBridge/app/libs/MiBridge.jar and b/TestMiBridge/app/libs/MiBridge.jar differ diff --git a/TestMiBridge/app/src/main/java/com/mi/testmibridge/ApiActivity.java b/TestMiBridge/app/src/main/java/com/mi/testmibridge/ApiActivity.java index 4b3420c..2f90f03 100644 --- a/TestMiBridge/app/src/main/java/com/mi/testmibridge/ApiActivity.java +++ b/TestMiBridge/app/src/main/java/com/mi/testmibridge/ApiActivity.java @@ -7,6 +7,7 @@ import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; +import android.util.Log; import android.view.Window; import android.widget.EditText; import android.widget.TextView; @@ -14,12 +15,15 @@ import com.bun.miitmdid.core.JLibrary; import com.mi.mibridge.MiBridge; +import com.mi.mibridge.ThermalEventCallBack; import java.util.ArrayList; import java.util.List; public class ApiActivity extends AppCompatActivity { + private static final String TAG = "TestMiBridge"; + // 数据源 private List miBridgeModels = new ArrayList<>(); @@ -65,6 +69,15 @@ public class ApiActivity extends AppCompatActivity { // tid private int mBridgeTid; + // system state + private static final int QUERY_BOARD_TEMP = 1; + private static final int QUERY_POWER_MODE = 2; + + // ThermalEventCallBack + ThermalEventCallBack mThermalEventCallBack = null; + Context mContext; + + private MiitHelper.AppIdsUpdater appIdsUpdater = new MiitHelper.AppIdsUpdater() { @Override public void OnIdsAvalid(@NonNull String ids) { @@ -89,6 +102,7 @@ protected void onCreate(Bundle savedInstanceState) { initData(); initView(); + mContext = this; } private void initView() { @@ -239,6 +253,58 @@ public void run() { showResult(ret, "requestThreadPriority "); } })); + + miBridgeModels.add(new MiBridgeModel("getSystemState -> boardTemp", new Runnable() { + @Override + public void run() { + int ret = MiBridge.getSystemState(mBridgeUid, mContext, QUERY_BOARD_TEMP); + Toast.makeText(ApiActivity.this, "getSystemState: " + ret, Toast.LENGTH_SHORT).show(); + } + })); + + miBridgeModels.add(new MiBridgeModel("getSystemState -> powerMode", new Runnable() { + @Override + public void run() { + int ret = MiBridge.getSystemState(mBridgeUid, mContext, QUERY_POWER_MODE); + Toast.makeText(ApiActivity.this, "getSystemState: " + ret, Toast.LENGTH_SHORT).show(); + } + })); + + miBridgeModels.add(new MiBridgeModel("registerThermalEventCallback", new Runnable() { + @Override + public void run() { + if(mThermalEventCallBack == null) { + mThermalEventCallBack = new ThermalEventCallBack() { + @Override + public void onThermalLevelChanged(int level) { + Log.e(TAG, "onThermalLevelChanged: " + level); + //do somthing; + } + }; + int ret = MiBridge.registerThermalEventCallback(mBridgeUid, mThermalEventCallBack); + Toast.makeText(ApiActivity.this, "registerThermalEventCallback: " + ret, Toast.LENGTH_SHORT).show(); + } else { + Log.w(TAG, "You should unRegisterThermalEventCallback last one!"); + Toast.makeText(ApiActivity.this, "To unRegisterThermalEventCallback last one!!", Toast.LENGTH_SHORT).show(); + } + } + })); + + miBridgeModels.add(new MiBridgeModel("unRegisterThermalEventCallback", new Runnable() { + @Override + public void run() { + if(mThermalEventCallBack != null) { + int ret = MiBridge.unRegisterThermalEventCallback(mBridgeUid, mThermalEventCallBack); + Toast.makeText(ApiActivity.this, "unRegisterThermalEventCallback: " + ret, Toast.LENGTH_SHORT).show(); + mThermalEventCallBack = null; + } else { + Log.w(TAG, "You should registerThermalEventCallback first!"); + Toast.makeText(ApiActivity.this, "To registerThermalEventCallback first!", Toast.LENGTH_SHORT).show(); + } + + } + })); + } diff --git a/TestMiBridge/mibridge/src/main/java/com/mi/mibridge/MiBridge.java b/TestMiBridge/mibridge/src/main/java/com/mi/mibridge/MiBridge.java index ac3564b..b05c221 100644 --- a/TestMiBridge/mibridge/src/main/java/com/mi/mibridge/MiBridge.java +++ b/TestMiBridge/mibridge/src/main/java/com/mi/mibridge/MiBridge.java @@ -5,6 +5,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import dalvik.system.PathClassLoader; @@ -17,6 +18,7 @@ public class MiBridge { private static final String TAG = "MiBridge"; private static final String PERFORMANCE_JAR = "/system/framework/MiuiBooster.jar"; private static final String PERFORMANCE_CLASS = "com.miui.performance.MiuiBooster"; + private static final String ITHERMALEVENTCALLBACK_CLASS = "com.miui.performance.IThermalEventCallBack"; private static Method mCheckPermissionFunc = null; private static Method mCheckDebugPermissionFunc = null; @@ -31,8 +33,13 @@ public class MiBridge { private static Method mCancelDdrHighFunc = null; private static Method mRequestBindCoreFunc = null; private static Method mCancelBindCoreFunc = null; + private static Method mGetSystemStateFunc = null; + private static Method mRegisterThermalEventCallbackFunc = null; + private static Method mUnRegisterThermalEventCallbackFunc = null; + private static Class perfClass; + private static Class IThermalEventCallBackClass = null; private static PathClassLoader perfClassLoader; private static Constructor mConstructor = null; @@ -43,6 +50,7 @@ public class MiBridge { perfClassLoader = new PathClassLoader(PERFORMANCE_JAR, ClassLoader.getSystemClassLoader()); perfClass = perfClassLoader.loadClass(PERFORMANCE_CLASS); + IThermalEventCallBackClass = perfClassLoader.loadClass(ITHERMALEVENTCALLBACK_CLASS); mConstructor = perfClass.getConstructor(); Class[] argClasses = new Class[]{String.class, int.class}; try { @@ -140,6 +148,28 @@ public class MiBridge { } catch (Exception e) { Log.e(TAG, "requestIOPrefetch no exit"); } + + try { + argClasses = new Class[]{int.class, Context.class, int.class}; + mGetSystemStateFunc = perfClass.getDeclaredMethod("getSystemState", argClasses); + } catch (Exception e) { + Log.e(TAG, "getSystemState no exit"); + } + + try { + argClasses = new Class[] {int.class, IThermalEventCallBackClass }; + mRegisterThermalEventCallbackFunc = perfClass.getDeclaredMethod("registerThermalEventCallback", argClasses); + } catch (Exception e) { + Log.e(TAG, "registerThermalEventCallback no exit, " + e); + } + + try { + argClasses = new Class[]{int.class, IThermalEventCallBackClass }; + mUnRegisterThermalEventCallbackFunc = perfClass.getDeclaredMethod("unRegisterThermalEventCallback", argClasses); + } catch (Exception e) { + Log.e(TAG, "UnRegisterThermalEventCallback no exit"); + } + } catch (Exception e) { Log.e(TAG, "MiBridge() : Load Class Exception: " + e); } @@ -299,4 +329,54 @@ public static int requestIOPrefetch(int uid, String filePath) { } return ret; } + + public static int getSystemState(int uid, Context context, int type) { + int ret = -1; + try { + Object retVal = mGetSystemStateFunc.invoke(mPerf, uid, context, type); + ret = (int) retVal; + } catch (Exception e) { + Log.e(TAG, "get system state failed , e:" + e.toString()); + } + return ret; + } + + public static int registerThermalEventCallback(int uid, ThermalEventCallBack cb) { + int ret = -1; + Object Obj = null; + try { + Obj = cb.getProxy(IThermalEventCallBackClass); + } catch (Exception e) { + Log.e(TAG, "getProxy failed, e: " + e.toString()); + return ret; + } + + try { + Object retVal = mRegisterThermalEventCallbackFunc.invoke(mPerf, uid, Obj); + ret = (int) retVal; + } catch (Exception e) { + Log.e(TAG, "registerThermalEventCallback failed , e:" + e.toString()); + } + return ret; + } + + + public static int unRegisterThermalEventCallback(int uid, ThermalEventCallBack cb) { + int ret = -1; + Object Obj = null; + try { + Obj = cb.getProxy(IThermalEventCallBackClass); + } catch (Exception e) { + Log.e(TAG, "getProxy failed, e: " + e.toString()); + return ret; + } + try { + Object retVal = mUnRegisterThermalEventCallbackFunc.invoke(mPerf, uid, Obj); + ret = (int) retVal; + } catch (Exception e) { + Log.e(TAG, "unRegisterThermalEventCallback failed , e:" + e.toString()); + } + return ret; + } + } diff --git a/TestMiBridge/mibridge/src/main/java/com/mi/mibridge/ThermalEventCallBack.java b/TestMiBridge/mibridge/src/main/java/com/mi/mibridge/ThermalEventCallBack.java new file mode 100644 index 0000000..9bee448 --- /dev/null +++ b/TestMiBridge/mibridge/src/main/java/com/mi/mibridge/ThermalEventCallBack.java @@ -0,0 +1,51 @@ +package com.mi.mibridge; + +import android.util.Log; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +public class ThermalEventCallBack implements InvocationHandler { + private static final String TAG = "MiBridge"; + private Object mObj = null; + private Object mTarget = null; + + public void onThermalLevelChanged(int level){}; + + public int getProxyHashCode() { + return this.hashCode(); + } + + public Object getProxy(Class IThermalEventCallBack) { + mTarget = IThermalEventCallBack.getInterfaces(); + mObj = Proxy.newProxyInstance(IThermalEventCallBack.getClassLoader(), new Class[] { IThermalEventCallBack }, this); + return mObj; + } + + @Override + public Object invoke(Object o, Method method, Object[] args) throws Throwable { + Object ret = null; + + if(mTarget == null) { + Log.e(TAG, "getProxy fisrt!"); + } + + if(method.getName() == "onThermalLevelChanged" && args != null) { + int level = Integer.parseInt(String.valueOf(args[0])); + onThermalLevelChanged(level); + return null; + } else if(method.getName() == "getProxyHashCode") { + ret = getProxyHashCode(); + return ret; + } + + try { + ret = method.invoke(mTarget, args); + } catch (Exception e) { + Log.e(TAG, "method invoke failed, e: " + e); + } + + return ret; + } +}