From fcfb4f6288fc5865e014ab3fd4e59c881a36e6a8 Mon Sep 17 00:00:00 2001 From: buddygr Date: Thu, 21 Apr 2022 00:41:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E7=9B=B8=E6=9C=BA=20?= =?UTF-8?q?=E6=89=8B=E7=94=B5=E7=AD=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../qsl4a/qsl4a/facade/AndroidFacade.java | 25 +++++++++----- .../qsl4a/qsl4a/facade/CameraFacade.java | 33 +++++++++++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/qpython/qsl4a/qsl4a/facade/AndroidFacade.java b/src/main/java/org/qpython/qsl4a/qsl4a/facade/AndroidFacade.java index c574cb6..a117c70 100644 --- a/src/main/java/org/qpython/qsl4a/qsl4a/facade/AndroidFacade.java +++ b/src/main/java/org/qpython/qsl4a/qsl4a/facade/AndroidFacade.java @@ -29,12 +29,16 @@ import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; +import android.hardware.camera2.CameraCharacteristics; +import android.hardware.camera2.CameraManager; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.StatFs; import android.os.Vibrator; +import android.support.annotation.RequiresApi; import android.text.ClipboardManager; import android.text.InputType; import android.text.method.PasswordTransformationMethod; @@ -105,6 +109,8 @@ public class AndroidFacade extends RpcReceiver { private final Resources mResources; private ClipboardManager mClipboard = null; + public final Context context; + @Override public void shutdown() { } @@ -125,6 +131,8 @@ public class AndroidFacade extends RpcReceiver { mNotificationManager = (NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE); mResources = manager.getAndroidFacadeResources(); + //乘着船 修改 + context = mService.getApplicationContext(); } @@ -364,7 +372,7 @@ public class AndroidFacade extends RpcReceiver { void startActivity(final Intent intent) { try { intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mService.getApplicationContext().startActivity(intent); + context.startActivity(intent); } catch (Exception e) { LogUtil.e("Failed to launch intent.", e); } @@ -606,17 +614,17 @@ public class AndroidFacade extends RpcReceiver { if (attachmentUri!=null) { android.util.Log.d("AndroidFacade", "attachmentUri:"+attachmentUri); if (attachmentUri.startsWith("http:") || attachmentUri.startsWith("https:")) { - intent = SPFUtils.getLinkAsIntent(mService.getApplicationContext(), attachmentUri); + intent = SPFUtils.getLinkAsIntent(context, attachmentUri); } else { intent = new Intent(); - intent.setClassName(mService.getApplicationContext().getPackageName(), attachmentUri); + intent.setClassName(context.getPackageName(), attachmentUri); } } else { intent = new Intent(); } PendingIntent contentIntent = PendingIntent.getActivity(mService, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); - Notification notification = SPFUtils.getNotification(mService.getApplicationContext(), title, message, contentIntent, + Notification notification = SPFUtils.getNotification(context, title, message, contentIntent, SPFUtils.getDrawableId(mService, "img_home_logo"), null, Notification.FLAG_AUTO_CANCEL); // Get a unique notification id from the application. @@ -625,7 +633,7 @@ public class AndroidFacade extends RpcReceiver { @Rpc(description = "Returns the status of network connection.") public boolean getNetworkStatus() { - return SPFUtils.netCheckin(mService.getApplicationContext()); + return SPFUtils.netCheckin(context); } @@ -682,7 +690,7 @@ public class AndroidFacade extends RpcReceiver { return null; } - @Rpc(description = "Checks if version of QPython SL4A is greater than or equal to the specified version.") + /*@Rpc(description = "Checks if version of QPython SL4A is greater than or equal to the specified version.") public boolean requiredVersion(@RpcParameter(name = "requiredVersion") final Integer version) { boolean result = false; int packageVersion = getPackageVersionCode("com.googlecode.android_scripting"); @@ -690,7 +698,7 @@ public class AndroidFacade extends RpcReceiver { result = (packageVersion >= version); } return result; - } + }*/ @Rpc(description = "Writes message to logcat.") public void log(@RpcParameter(name = "message") String message) { @@ -727,7 +735,7 @@ public class AndroidFacade extends RpcReceiver { zone.put("offset", tz.getOffset((new Date()).getTime())); result.put("TZ", zone); result.put("SDK", android.os.Build.VERSION.SDK); - result.put("download", FileUtils.getExternalDownload(mService.getApplicationContext()).getAbsolutePath()); + result.put("download", FileUtils.getExternalDownload(context).getAbsolutePath()); result.put("appcache", mService.getCacheDir().getAbsolutePath()); try { StatFs fs = new StatFs("/sdcard"); @@ -767,4 +775,5 @@ public class AndroidFacade extends RpcReceiver { } return result; } + } diff --git a/src/main/java/org/qpython/qsl4a/qsl4a/facade/CameraFacade.java b/src/main/java/org/qpython/qsl4a/qsl4a/facade/CameraFacade.java index 10cc9c9..6d376e5 100644 --- a/src/main/java/org/qpython/qsl4a/qsl4a/facade/CameraFacade.java +++ b/src/main/java/org/qpython/qsl4a/qsl4a/facade/CameraFacade.java @@ -18,14 +18,19 @@ package org.qpython.qsl4a.qsl4a.facade; import android.app.Service; +import android.content.Context; import android.content.Intent; import android.hardware.Camera; import android.hardware.Camera.AutoFocusCallback; import android.hardware.Camera.Parameters; import android.hardware.Camera.PictureCallback; +import android.hardware.camera2.CameraCharacteristics; +import android.hardware.camera2.CameraManager; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; +import android.support.annotation.RequiresApi; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.WindowManager; @@ -58,6 +63,7 @@ public class CameraFacade extends RpcReceiver { private final Service mService; private final Parameters mParameters; + private final Context context; private class BooleanResult { boolean mmResult = false; @@ -66,6 +72,11 @@ public class CameraFacade extends RpcReceiver { public CameraFacade(FacadeManager manager) { super(manager); mService = manager.getService(); + + //乘着船 修改 + context = mService.getApplicationContext(); + // ↑ // + Camera camera = Camera.open(); try { mParameters = camera.getParameters(); @@ -211,4 +222,26 @@ public class CameraFacade extends RpcReceiver { LogUtil.e("No camera found"); } } + + // 打开或关闭闪光灯 + //@SuppressLint("NewApi") + @RequiresApi(api = Build.VERSION_CODES.M) + @Rpc(description = "open or close flash light torch of camera.") + public void cameraSetTorchMode(@RpcParameter(name = "enabled") Boolean enabled) throws Exception { + //获取CameraManager + CameraManager mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE); + //获取当前手机所有摄像头设备ID + String[] ids = mCameraManager.getCameraIdList(); + for (String id : ids) { + CameraCharacteristics c = mCameraManager.getCameraCharacteristics(id); + //查询该摄像头组件是否包含闪光灯 + Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE); + Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING); + if (flashAvailable != null && flashAvailable + && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) { + //打开或关闭手电筒 + mCameraManager.setTorchMode(id, enabled); + } + } + } } -- Gitee From d445c9a2d29ef0ae4f559ed919576987a0d4cfde Mon Sep 17 00:00:00 2001 From: buddygr Date: Fri, 22 Apr 2022 21:29:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E4=BF=A1=E5=8F=B7=E6=A3=80=E6=B5=8B=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../qsl4a/qsl4a/facade/PhoneFacade.java | 193 +++++++++++++++++- 1 file changed, 182 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/qpython/qsl4a/qsl4a/facade/PhoneFacade.java b/src/main/java/org/qpython/qsl4a/qsl4a/facade/PhoneFacade.java index 40fdf82..57e1a00 100644 --- a/src/main/java/org/qpython/qsl4a/qsl4a/facade/PhoneFacade.java +++ b/src/main/java/org/qpython/qsl4a/qsl4a/facade/PhoneFacade.java @@ -16,6 +16,8 @@ package org.qpython.qsl4a.qsl4a.facade; +import static android.os.Build.VERSION.SDK_INT; + import android.Manifest; import android.app.Service; import android.content.ContentResolver; @@ -23,14 +25,34 @@ import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.provider.Contacts.PhonesColumns; +import android.support.annotation.RequiresApi; import android.support.annotation.RequiresPermission; +import android.telephony.CellIdentityCdma; +import android.telephony.CellIdentityGsm; +import android.telephony.CellIdentityLte; +import android.telephony.CellIdentityNr; +import android.telephony.CellIdentityWcdma; +import android.telephony.CellInfo; +import android.telephony.CellInfoCdma; +import android.telephony.CellInfoGsm; +import android.telephony.CellInfoLte; +import android.telephony.CellInfoNr; +import android.telephony.CellInfoWcdma; import android.telephony.CellLocation; +import android.telephony.CellSignalStrengthCdma; +import android.telephony.CellSignalStrengthGsm; +import android.telephony.CellSignalStrengthLte; +import android.telephony.CellSignalStrengthNr; +import android.telephony.CellSignalStrengthWcdma; import android.telephony.NeighboringCellInfo; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; +import org.json.JSONArray; +import org.json.JSONObject; import org.qpython.qsl4a.qsl4a.MainThread; import org.qpython.qsl4a.qsl4a.jsonrpc.RpcReceiver; import org.qpython.qsl4a.qsl4a.rpc.Rpc; @@ -162,11 +184,11 @@ public class PhoneFacade extends RpcReceiver { phoneDial("tel:" + URLEncoder.encode(number, "ASCII")); } - @Rpc(description = "Returns the current cell location.") + /*@Rpc(description = "Returns the current cell location.") @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public CellLocation getCellLocation() { return mTelephonyManager.getCellLocation(); - } + }*/ @Rpc(description = "Returns the numeric name (MCC+MNC) of current registered operator.") public String getNetworkOperator() { @@ -178,20 +200,48 @@ public class PhoneFacade extends RpcReceiver { return mTelephonyManager.getNetworkOperatorName(); } + @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE) @Rpc(description = "Returns a the radio technology (network type) currently in use on the device.") - public String getNetworkType() { + public String getNetworkType() throws Exception { // TODO(damonkohler): API level 5 has many more types. - switch (mTelephonyManager.getNetworkType()) { - case TelephonyManager.NETWORK_TYPE_EDGE: - return "edge"; - case TelephonyManager.NETWORK_TYPE_GPRS: - return "gprs"; + int networkType; + if (SDK_INT >= 24) { + networkType = mTelephonyManager.getDataNetworkType(); + } else { + networkType = mTelephonyManager.getNetworkType(); + } + switch (networkType) { + //case TelephonyManager.NETWORK_TYPE_IWLAN: //这个不是wifi + //return "WIFI"; + case TelephonyManager.NETWORK_TYPE_NR: + return "5G"; + case TelephonyManager.NETWORK_TYPE_LTE: + return "4G"; + case TelephonyManager.NETWORK_TYPE_HSDPA: + case TelephonyManager.NETWORK_TYPE_HSUPA: + case TelephonyManager.NETWORK_TYPE_HSPA: + return "3G HSPA"; + case TelephonyManager.NETWORK_TYPE_HSPAP: + return "3G HSPA+"; case TelephonyManager.NETWORK_TYPE_UMTS: - return "umts"; + return "3G UMTS"; + case TelephonyManager.NETWORK_TYPE_TD_SCDMA: + case TelephonyManager.NETWORK_TYPE_EHRPD: + case TelephonyManager.NETWORK_TYPE_EVDO_0: + case TelephonyManager.NETWORK_TYPE_EVDO_A: + case TelephonyManager.NETWORK_TYPE_EVDO_B: + return "3G Other"; + case TelephonyManager.NETWORK_TYPE_GSM: + case TelephonyManager.NETWORK_TYPE_GPRS: + case TelephonyManager.NETWORK_TYPE_EDGE: + return "2G GSM"; + case TelephonyManager.NETWORK_TYPE_1xRTT: + case TelephonyManager.NETWORK_TYPE_CDMA: + return "2G CDMA"; case TelephonyManager.NETWORK_TYPE_UNKNOWN: - return "unknown"; + return "Unknown"; default: - return null; + return "Other"; } } @@ -293,4 +343,125 @@ public class PhoneFacade extends RpcReceiver { // public List getNeighboringCellInfo() { // return mTelephonyManager.getNeighboringCellInfo(); // } + + //这是一个getCellLocation下级函数 + private void getCellLocation(CellInfo cellInfo, JSONObject result, boolean complex) throws Exception { + if(complex) + result.put("Registered",cellInfo.isRegistered()); + if (SDK_INT >= 29 && cellInfo instanceof CellInfoNr){ + CellIdentityNr nrCellInfoIdetity = (CellIdentityNr) ((CellInfoNr) cellInfo).getCellIdentity(); + result.put("Nci",nrCellInfoIdetity.getNci()); + result.put("Pci",nrCellInfoIdetity.getPci()); + result.put("Tac",nrCellInfoIdetity.getTac()); + CellSignalStrengthNr cellSignalStrengthNr = (CellSignalStrengthNr) ((CellInfoNr) cellInfo).getCellSignalStrength(); + result.put("NrDbm",cellSignalStrengthNr.getDbm()); + if (complex) { + result.put("Mcc,Mnc",nrCellInfoIdetity.getMccString()+","+nrCellInfoIdetity.getMncString()); + result.put("NrArfcn",nrCellInfoIdetity.getNrarfcn()); + if (SDK_INT >= Build.VERSION_CODES.R) { + result.put("Bands",nrCellInfoIdetity.getBands()); + } + } + } + else if (cellInfo instanceof CellInfoLte){ + CellIdentityLte lteCellInfoIdetity = ((CellInfoLte) cellInfo).getCellIdentity(); + result.put("Ci", lteCellInfoIdetity.getCi()); + result.put("Pci",lteCellInfoIdetity.getPci()); + result.put("Tac",lteCellInfoIdetity.getTac()); + CellSignalStrengthLte cellSignalStrengthLte = ((CellInfoLte) cellInfo).getCellSignalStrength(); + result.put("LteDbm",cellSignalStrengthLte.getDbm()); + if (complex) { + result.put("Mcc,Mnc",lteCellInfoIdetity.getMcc()+","+lteCellInfoIdetity.getMnc()); + if (SDK_INT >= Build.VERSION_CODES.N) { + result.put("Earfcn",lteCellInfoIdetity.getEarfcn()); + } + //result.put("Bandwidth",lteCellInfoIdetity.getBandwidth()); //总是2147483647 + if (SDK_INT >= Build.VERSION_CODES.R) { + result.put("Bands",lteCellInfoIdetity.getBands()); + } + } + } + else if (cellInfo instanceof CellInfoWcdma){ + CellIdentityWcdma wcdmaCellInfoIdetity = ((CellInfoWcdma) cellInfo).getCellIdentity(); + result.put("Lac",wcdmaCellInfoIdetity.getLac()); + result.put("Cid",wcdmaCellInfoIdetity.getCid()); + result.put("Psc",wcdmaCellInfoIdetity.getPsc()); + CellSignalStrengthWcdma cellSignalStrengthWcdma = ((CellInfoWcdma) cellInfo).getCellSignalStrength(); + result.put("WcdmaDbm",cellSignalStrengthWcdma.getDbm()); + if (complex) { + result.put("Mcc,Mnc",wcdmaCellInfoIdetity.getMcc()+","+wcdmaCellInfoIdetity.getMnc()); + if (SDK_INT >= Build.VERSION_CODES.N) { + result.put("Uarfcn",wcdmaCellInfoIdetity.getUarfcn()); + } + } + } + else if (cellInfo instanceof CellInfoCdma){ + CellIdentityCdma cdmaCellInfoIdetity = ((CellInfoCdma) cellInfo).getCellIdentity(); + result.put("Sid",cdmaCellInfoIdetity.getSystemId()); + result.put("Nid",cdmaCellInfoIdetity.getNetworkId()); + result.put("Bid",cdmaCellInfoIdetity.getBasestationId()); + CellSignalStrengthCdma cellSignalStrengthCdma = ((CellInfoCdma) cellInfo).getCellSignalStrength(); + result.put("CdmaDbm",cellSignalStrengthCdma.getDbm()); + if(complex){ + result.put("Lat,Long",tudeIntToStr(cdmaCellInfoIdetity.getLatitude())+","+tudeIntToStr(cdmaCellInfoIdetity.getLongitude())); + } + } + else if (cellInfo instanceof CellInfoGsm){ + CellIdentityGsm gsmCellInfoIdetity = ((CellInfoGsm) cellInfo).getCellIdentity(); + result.put("Lac",gsmCellInfoIdetity.getLac()); + result.put("Cid",gsmCellInfoIdetity.getCid()); + CellSignalStrengthGsm cellSignalStrengthGsm = ((CellInfoGsm) cellInfo).getCellSignalStrength(); + result.put("GsmDbm",cellSignalStrengthGsm.getDbm()); + if (complex) { + result.put("Mcc,Mnc",gsmCellInfoIdetity.getMcc()+","+gsmCellInfoIdetity.getMnc()); + if (SDK_INT >= Build.VERSION_CODES.N) { + result.put("Arfcn",gsmCellInfoIdetity.getArfcn()); + } + } + } + } + + private String tudeIntToStr(int x){ + return String.valueOf(Math.round((double) x/0.144D)/100000D); + } + + @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION) + @Rpc(description = "Returns the current cell location.") + public JSONObject getCellLocation() throws Exception { + JSONObject result = new JSONObject(); + try{ + List cellInfos = mTelephonyManager.getAllCellInfo(); + result.put("CellCount",cellInfos.size()); + for (CellInfo cellInfo : cellInfos) { + if(!cellInfo.isRegistered()) continue; + getCellLocation(cellInfo,result,false); + } + return result; + } catch (Exception e){ + throw new Exception("getCellLocation can't get any information because of No Location Permission or Completely No Signal ."); + } + } + + @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION) + @Rpc(description = "Returns all the neighboring cell information of the device.") + public List getAllCellInfo() throws Exception { + return mTelephonyManager.getAllCellInfo(); + } + + @RequiresApi(api = Build.VERSION_CODES.N) + @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION) + @Rpc(description = "Returns all the cells location.") + public JSONArray getAllCellsLocation() throws Exception { + try{ + List cellInfos = mTelephonyManager.getAllCellInfo(); + JSONArray Result = new JSONArray(); + for (CellInfo cellInfo : cellInfos) { + JSONObject result = new JSONObject(); + getCellLocation(cellInfo,result,true); + Result.put(result); + } + return Result; + } catch (Exception e){ + throw new Exception("getAllCellsLocation can't get any information because of No Location Permission or Completely No Signal ."); + }} } -- Gitee