Commit a3eb444c a3eb444c40184fb4f8c6ca2f0b855d2f734b211a by 胡波

新增aidl服务模拟

1 parent a9850a1a
......@@ -4,10 +4,10 @@
<selectionStates>
<SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2024-10-12T05:40:06.328823Z">
<DropdownSelection timestamp="2024-10-14T01:44:33.742803Z">
<Target type="DEFAULT_BOOT">
<handle>
<DeviceId pluginId="Default" identifier="serial=0123456789;connection=725ee2fc" />
<DeviceId pluginId="LocalEmulator" identifier="path=/Users/bohu/.android/avd/Pixel_Tablet_API_31.avd" />
</handle>
</Target>
</DropdownSelection>
......
......@@ -29,6 +29,7 @@ android {
buildFeatures{
buildConfig true
aidl true
}
flavorDimensions 'APP'
......
......@@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<permission android:name="com.android.smart.terminal.iptv.aidl.SERVICES" android:protectionLevel="normal"/>
<application
android:allowBackup="true"
......@@ -30,6 +31,11 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:exported="true" android:name="com.android.smart.terminal.iptv.aidl.ServiceCfg">
<intent-filter>
<action android:name="com.android.smart.terminal.iptv.aidl.ServiceCfg"/>
</intent-filter>
</service>
</application>
</manifest>
\ No newline at end of file
......
{
"iptvauthurl": "1http://epg.itv.cq.cn:8080/iptvepg/platform/index.jsp",
"iptvaccount": "kdspb2",
"userid": "kdspb2",
"iptvpassword": "159357",
"stbid": "0010059900E06810042094D505F12A7E",
"usertoken": "IlrUGIlrUG17tYxUgfawMrNRAmf8XV7S",
"epgdomain": "http://172.23.88.137:33200/EPG/jsp/xinaishangbanmuban/en/Category.jsp",
"epginfo": "",
"epggroupnmb": "xinaishangbanmuban",
"usergroupnmb": "717",
"productmodel": "",
"terminaltype": "",
"areaid": "",
"countyid": "",
"romversion": "",
"terminalversion": "",
"mac": "",
"iptvstate": "",
"ottcp": "",
"sessionid": "DF10A297F8449B9526B58811497FC4B4"
}
\ No newline at end of file
package com.android.smart.terminal.iptv.aidl;
import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Parcel;
import android.os.RemoteException;
//aidl接口
public interface IServiceCfg extends IInterface {
boolean getBoolean(String key, boolean defValue) throws RemoteException;
int getInt(String key, int defValue) throws RemoteException;
String getString(String key, String defValue) throws RemoteException;
boolean putBoolean(String key, boolean value) throws RemoteException;
boolean putInt(String key, int value) throws RemoteException;
boolean putString(String key, String value) throws RemoteException;
abstract class Stub extends Binder implements IServiceCfg {
private static final String DESCRIPTOR = "com.android.smart.terminal.iptv.aidl.IServiceCfg";
static final int TRANSACTION_getBoolean = 6;
static final int TRANSACTION_getInt = 5;
static final int TRANSACTION_getString = 4;
static final int TRANSACTION_putBoolean = 3;
static final int TRANSACTION_putInt = 2;
static final int TRANSACTION_putString = 1;
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
@Override
public IBinder asBinder() {
return this;
}
public static IServiceCfg asInterface(IBinder obj) {
if (obj == null) return null;
IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
return (iin instanceof IServiceCfg) ? (IServiceCfg) iin : new Proxy(obj);
}
@Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
switch (code) {
case TRANSACTION_putString:
data.enforceInterface(DESCRIPTOR);
boolean resultPutString = putString(data.readString(), data.readString());
reply.writeNoException();
reply.writeInt(resultPutString ? 1 : 0);
return true;
case TRANSACTION_putInt:
data.enforceInterface(DESCRIPTOR);
boolean resultPutInt = putInt(data.readString(), data.readInt());
reply.writeNoException();
reply.writeInt(resultPutInt ? 1 : 0);
return true;
case TRANSACTION_putBoolean:
data.enforceInterface(DESCRIPTOR);
boolean resultPutBoolean = putBoolean(data.readString(), data.readInt() != 0);
reply.writeNoException();
reply.writeInt(resultPutBoolean ? 1 : 0);
return true;
case TRANSACTION_getString:
data.enforceInterface(DESCRIPTOR);
String resultGetString = getString(data.readString(), data.readString());
reply.writeNoException();
reply.writeString(resultGetString);
return true;
case TRANSACTION_getInt:
data.enforceInterface(DESCRIPTOR);
int resultGetInt = getInt(data.readString(), data.readInt());
reply.writeNoException();
reply.writeInt(resultGetInt);
return true;
case TRANSACTION_getBoolean:
data.enforceInterface(DESCRIPTOR);
boolean resultGetBoolean = getBoolean(data.readString(), data.readInt() != 0);
reply.writeNoException();
reply.writeInt(resultGetBoolean ? 1 : 0);
return true;
case 1598968902: // Interface descriptor
reply.writeString(DESCRIPTOR);
return true;
default:
return super.onTransact(code, data, reply, flags);
}
}
private static class Proxy implements IServiceCfg {
private final IBinder mRemote;
Proxy(IBinder remote) {
mRemote = remote;
}
@Override
public IBinder asBinder() {
return mRemote;
}
@Override
public boolean getBoolean(String key, boolean defValue) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
boolean result;
try {
data.writeInterfaceToken(DESCRIPTOR);
data.writeString(key);
data.writeInt(defValue ? 1 : 0);
mRemote.transact(TRANSACTION_getBoolean, data, reply, 0);
reply.readException();
result = reply.readInt() != 0;
} finally {
reply.recycle();
data.recycle();
}
return result;
}
@Override
public int getInt(String key, int defValue) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
int result;
try {
data.writeInterfaceToken(DESCRIPTOR);
data.writeString(key);
data.writeInt(defValue);
mRemote.transact(TRANSACTION_getInt, data, reply, 0);
reply.readException();
result = reply.readInt();
} finally {
reply.recycle();
data.recycle();
}
return result;
}
@Override
public String getString(String key, String defValue) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
String result;
try {
data.writeInterfaceToken(DESCRIPTOR);
data.writeString(key);
data.writeString(defValue);
mRemote.transact(TRANSACTION_getString, data, reply, 0);
reply.readException();
result = reply.readString();
} finally {
reply.recycle();
data.recycle();
}
return result;
}
@Override
public boolean putBoolean(String key, boolean value) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
boolean result;
try {
data.writeInterfaceToken(DESCRIPTOR);
data.writeString(key);
data.writeInt(value ? 1 : 0);
mRemote.transact(TRANSACTION_putBoolean, data, reply, 0);
reply.readException();
result = reply.readInt() != 0;
} finally {
reply.recycle();
data.recycle();
}
return result;
}
@Override
public boolean putInt(String key, int value) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
boolean result;
try {
data.writeInterfaceToken(DESCRIPTOR);
data.writeString(key);
data.writeInt(value);
mRemote.transact(TRANSACTION_putInt, data, reply, 0);
reply.readException();
result = reply.readInt() != 0;
} finally {
reply.recycle();
data.recycle();
}
return result;
}
@Override
public boolean putString(String key, String value) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
boolean result;
try {
data.writeInterfaceToken(DESCRIPTOR);
data.writeString(key);
data.writeString(value);
mRemote.transact(TRANSACTION_putString, data, reply, 0);
reply.readException();
result = reply.readInt() != 0;
} finally {
reply.recycle();
data.recycle();
}
return result;
}
}
}
}
package com.android.smart.terminal.iptv.aidl;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.smart.terminal.iptv.aidl.IServiceCfg.Stub;
import com.topdraw.iptvlaunchertest.IptvConfig;
import com.topdraw.iptvlaunchertest.TestConfig;
public class ServiceCfg extends Service {
private static final String TAG = "ServiceCfg";
private final Stub mBinder;
private IptvConfig mConfig;
public ServiceCfg() {
this.mConfig = null;
this.mBinder = new Stub() {
@Override
public boolean getBoolean(String key, boolean defValue) throws RemoteException {
Log.d(TAG, "getBoolean() key = " + key + ", defValue = " + defValue);
return defValue;
}
@Override
public int getInt(String key, int defValue) throws RemoteException {
Log.d(TAG, "getInt() key = " + key + ", defValue = " + defValue);
return defValue;
}
@Override
public String getString(String key, String defValue) throws RemoteException {
String value = defValue;
if (TextUtils.isEmpty(key)) {
return value;
}
switch (key.toLowerCase()) {
case "service/serviceinfo/iptvauthurl":
value = mConfig.getAuthAddress();
break;
case "service/serviceinfo/iptvaccount":
case "service/serviceinfo/userid":
value = mConfig.getBusinessUserId();
break;
case "service/serviceinfo/iptvpassword":
value = mConfig.getBusinessPasswd();
break;
case "service/serviceinfo/stbid":
value = mConfig.getStbID();
break;
case "service/serviceinfo/usertoken":
value = mConfig.getValue("usertoken");
break;
case "service/serviceinfo/epgdomain":
case "service/serviceinfo/epginfo":
value = mConfig.getValue("epgdomain");
break;
case "service/serviceinfo/epggroupnmb":
value = mConfig.getEPGGroupNMB();
break;
case "service/serviceinfo/usergroupnmb":
value = mConfig.getUserGroupNMB();
break;
case "service/serviceinfo/productmodel":
case "service/serviceinfo/terminaltype":
value = IptvConfig.getStbType();
break;
case "service/serviceinfo/areaid":
value = mConfig.getValue("areaid");
break;
case "service/serviceinfo/countyid":
value = mConfig.getValue("countyid");
break;
case "service/serviceinfo/romversion":
case "service/serviceinfo/terminalversion":
value = IptvConfig.getSoftVersion();
break;
case "service/serviceinfo/platforminfo":
value = getPlatform();
break;
case "service/serviceinfo/manufacturerinfo":
value = "Fiberhome";
break;
case "service/serviceinfo/mac":
value = mConfig.getMac();
break;
case "service/serviceinfo/livechannels":
value = mConfig.getValue("livechannels");
break;
case "service/serviceinfo/stbhdflag":
value = "HD";
break;
case "service/serviceinfo/adshowstatus":
value = getADShowStatus();
break;
case "service/serviceinfo/iptvstate":
value = mConfig.getIPTVState();
break;
case "service/serviceinfo/ottcp":
value = mConfig.getOttCp();
break;
default:
Log.e(TAG, "getString() unknown key = " + key);
break;
}
Log.d(TAG, "getString() key = " + key + ", value = " + value);
return value;
}
@Override
public boolean putBoolean(String key, boolean value) throws RemoteException {
Log.d(TAG, "putBoolean() key = " + key + ", value = " + value);
return false;
}
@Override
public boolean putInt(String key, int value) throws RemoteException {
Log.d(TAG, "putInt() key = " + key + ", value = " + value);
return false;
}
@Override
public boolean putString(String key, String value) throws RemoteException {
Log.d(TAG, "putString() key = " + key + ", value = " + value);
return false;
}
};
}
private String getADShowStatus() {
boolean logoStatus = false;
boolean anmiStatus = false;
boolean authStatus = false;
try {
JSONObject jsonData = new JSONObject();
JSONArray arrayName = new JSONArray();
JSONArray arrayValue = new JSONArray();
arrayName.put("logo");
arrayValue.put(logoStatus ? "1" : "2");
arrayName.put("anmi");
arrayValue.put(anmiStatus ? "1" : "2");
arrayName.put("auth");
arrayValue.put(authStatus ? "1" : "2");
jsonData.put("adtype", arrayName);
jsonData.put("advalue", arrayValue);
String status = jsonData.toString();
Log.d(TAG, "getADShowStatus() status = " + status);
return status;
} catch (JSONException e) {
Log.e(TAG, "getADShowStatus() failed to generate JSON", e);
return null;
}
}
private String getPlatform() {
Log.d(TAG, "getPlatform() platform = " + TestConfig.platform);
return TestConfig.platform;
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "service on bind");
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
mConfig = IptvConfig.getInstance(this);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
@Override
public void onRebind(Intent intent) {
Log.d(TAG, "service on rebind");
super.onRebind(intent);
}
@Override
public void onStart(Intent intent, int startId) {
Log.d(TAG, "service start id=" + startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "service on unbind");
return super.onUnbind(intent);
}
}
package com.topdraw.iptvlaunchertest;
import android.content.Context;
import android.util.Log;
import com.topdraw.iptvlaunchertest.common.JsonReader;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* iptv 参数
*/
public class IptvConfig {
private static final String TAG = IptvConfig.class.getSimpleName();
private static final Map<String,String> iptvConfigMap = new HashMap<>();
private static volatile IptvConfig instance = null;
public static final String IPTV_AUTH_URL = "iptvauthurl";
public static final String IPTV_ACCOUNT = "iptvaccount";
public static final String USER_ID = "userid";
public static final String IPTV_PASSWORD = "iptvpassword";
public static final String STB_ID = "stbid";
public static final String USER_TOEKN = "usertoken";
public static final String EPG_DOMAIN = "epgdomain";
public static final String EPG_INFO = "epginfo";
public static final String EPG_GROUP_NMB = "epggroupnmb";
public static final String USER_GROUP_NMB = "usergroupnmb";
public static final String PRODUCT_MODEL = "productmodel";
public static final String TERMINAL_TYPE = "terminaltype";
public static final String AREA_ID = "areaid";
public static final String COUNTY_ID = "countyid";
public static final String ROM_VERSION = "romversion";
public static final String TERMINAL_VERSION = "terminalversion";
public static final String MAC = "mac";
public static final String IPTV_STATE = "iptvstate";
public static final String OT_TCP = "ottcp";
public static final String LIVE_CHANNELS = "LiveChannels";
private IptvConfig(Context context){
JSONObject param = JsonReader.readJsonFromAssets(context,"iptv_param.json");
JSONArray channels = JsonReader.readJsonArrFromAssets(context,"livechannels.json");
Log.d(TAG, "IptvConfig param:"+param.toString());
Log.d(TAG, "IptvConfig channels:"+channels.toString());
for (Iterator<String> it = param.keys(); it.hasNext(); ) {
String key = it.next();
iptvConfigMap.put(key,param.optString(key,""));
}
iptvConfigMap.put("LiveChannels",channels.toString());
}
public static IptvConfig getInstance(Context context) {
if(instance == null){
synchronized (IptvConfig.class){
if(instance==null){
instance = new IptvConfig(context);
}
}
}
return instance;
}
public static String getStbType() {
return TestConfig.stbType;
}
public static String getSoftVersion() {
return "1";
}
public String getAuthAddress() {
return iptvConfigMap.get(IPTV_AUTH_URL);
}
public String getBusinessUserId() {
return iptvConfigMap.get(IPTV_ACCOUNT);
}
public String getBusinessPasswd() {
return iptvConfigMap.get(IPTV_PASSWORD);
}
public String getStbID() {
return iptvConfigMap.get(STB_ID);
}
public String getValue(String key) {
return iptvConfigMap.get(key);
}
public String getEPGGroupNMB() {
return iptvConfigMap.get(EPG_GROUP_NMB);
}
public String getUserGroupNMB() {
return iptvConfigMap.get(USER_GROUP_NMB);
}
public String getMac() {
return iptvConfigMap.get(MAC);
}
public String getIPTVState() {
return iptvConfigMap.get(IPTV_STATE);
}
public String getOttCp() {
return iptvConfigMap.get(OT_TCP);
}
}
package com.topdraw.iptvlaunchertest;
import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.webkit.WebSettings;
......@@ -12,6 +13,9 @@ import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.android.smart.terminal.iptv.aidl.ServiceCfg;
import com.topdraw.iptvlaunchertest.common.Authentication;
public class MainActivity extends AppCompatActivity {
private WebView webView;
......@@ -23,6 +27,7 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this, ServiceCfg.class));
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
......@@ -32,10 +37,10 @@ public class MainActivity extends AppCompatActivity {
});
webView = findViewById(R.id.webview);
initWeb();
initObject();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(permissions,0);
}
initObject();
webView.loadUrl(TestConfig.homeUrl);
}
......@@ -51,6 +56,7 @@ public class MainActivity extends AppCompatActivity {
//注册接口
private void initObject(){
webView.addJavascriptInterface(new Authentication(MainActivity.this),"Authentication");
if(TestConfig.testDist==TestConfig.Skyworth){
webView.addJavascriptInterface(new com.topdraw.iptvlaunchertest.sky.STBAppManager(this),"STBAppManager");
}else if(TestConfig.testDist==TestConfig.FiberHome){
......
......@@ -9,6 +9,11 @@ public class TestConfig {
//测试目标盒子
public static int testDist = FiberHome;
//测试平台
public static final String platform = "HUAWEI";//"ZTE"
//设备名称
public static final String stbType = "IPTV-TEST";
//启动页地址
public static final String homeUrl = "http://139.196.145.150:8060/JsAppLaunch/JsAppStartUp.html";
public static final String homeUrl = "http://172.23.51.26:8070/AppEngine/apps/startApp/jsview.html";
}
......
package com.topdraw.iptvlaunchertest.common;
import android.content.Context;
import android.webkit.JavascriptInterface;
import com.topdraw.iptvlaunchertest.IptvConfig;
import java.util.HashMap;
public class Authentication {
private static final HashMap<String,String> valueMap = new HashMap<>();
public static final String UserID = "UserID";
public static final String STBType = "STBType";
public static final String STBVersion = "STBVersion";
public static final String STBID = "STBID";
public static final String EPGDomain = "EPGDomain";
public static final String UserToken = "UserToken";
public static final String SessionID = "SessionID";
public Authentication(Context context) {
valueMap.put(UserID,IptvConfig.getInstance(context).getValue(IptvConfig.USER_ID));
valueMap.put(STBType, IptvConfig.getStbType());
valueMap.put(STBVersion, IptvConfig.getSoftVersion());
valueMap.put(STBID, IptvConfig.getInstance(context).getStbID());
valueMap.put(EPGDomain, IptvConfig.getInstance(context).getValue("epgdomain"));
valueMap.put(UserToken, IptvConfig.getInstance(context).getValue("usertoken"));
valueMap.put(SessionID, IptvConfig.getInstance(context).getValue("sessionid"));
}
@JavascriptInterface
public String CTCGetConfig(String encryToken){
return valueMap.get(encryToken);
}
@JavascriptInterface
public String CTCSetConfig(String key,String value){
return valueMap.put(key,value);
}
}
package com.topdraw.iptvlaunchertest.common;
import android.content.Context;
import android.content.res.AssetManager;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class JsonReader {
public static JSONObject readJsonFromAssets(Context context, String jsonFileName) {
AssetManager assetManager = context.getAssets();
JSONObject jsonObject = null;
try (InputStream inputStream = assetManager.open(jsonFileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
// 将 JSON 字符串转换为 JSONObject
jsonObject = new JSONObject(stringBuilder.toString());
} catch (IOException | org.json.JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
public static JSONArray readJsonArrFromAssets(Context context, String jsonFileName) {
AssetManager assetManager = context.getAssets();
JSONArray jsonArray = null;
try (InputStream inputStream = assetManager.open(jsonFileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
// 将 JSON 字符串转换为 JSONObject
jsonArray = new JSONArray(stringBuilder.toString());
} catch (IOException | org.json.JSONException e) {
e.printStackTrace();
}
return jsonArray;
}
}