Bladeren bron

3.0 optimize:扬声器音量控制,保存文件bug修复

xwh 3 weken geleden
bovenliggende
commit
6b6a49f302

+ 29 - 9
app/src/main/AndroidManifest.xml

@@ -67,7 +67,9 @@
         android:theme="@style/AppTheme"
         tools:replace="android:name,android:allowBackup,android:theme,android:icon,android:roundIcon">
 
-        <activity android:name=".activity.SplashActivity">
+        <activity android:name=".activity.SplashActivity"
+            android:screenOrientation="portrait"
+            android:configChanges="keyboard|keyboardHidden|orientation">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 
@@ -77,17 +79,35 @@
         <activity
             android:name=".activity.MainActivity"
             android:launchMode="singleTop"
+            android:screenOrientation="portrait"
+            android:configChanges="keyboard|keyboardHidden|orientation"
             android:theme="@style/AppTheme.Full" />
 
 
-        <activity android:name=".activity.setting.PasswordActivity" />
-        <activity android:name=".activity.setting.SettingActivity" />
-        <activity android:name=".activity.setting.GatherActivity" />
-        <activity android:name=".activity.setting.BaseInfoActivity" />
-        <activity android:name=".activity.setting.PushActivity" />
-        <activity android:name=".activity.setting.SystemConfigActivity" />
-        <activity android:name=".activity.setting.ZoloActivity" />
-        <activity android:name=".activity.setting.FunctionActivity" />
+        <activity android:name=".activity.setting.PasswordActivity"
+            android:screenOrientation="portrait"
+            android:configChanges="keyboard|keyboardHidden|orientation"/>
+        <activity android:name=".activity.setting.SettingActivity"
+            android:screenOrientation="portrait"
+            android:configChanges="keyboard|keyboardHidden|orientation"/>
+        <activity android:name=".activity.setting.GatherActivity"
+            android:screenOrientation="portrait"
+            android:configChanges="keyboard|keyboardHidden|orientation"/>
+        <activity android:name=".activity.setting.BaseInfoActivity"
+            android:screenOrientation="portrait"
+            android:configChanges="keyboard|keyboardHidden|orientation"/>
+        <activity android:name=".activity.setting.PushActivity"
+            android:screenOrientation="portrait"
+            android:configChanges="keyboard|keyboardHidden|orientation"/>
+        <activity android:name=".activity.setting.SystemConfigActivity"
+            android:screenOrientation="portrait"
+            android:configChanges="keyboard|keyboardHidden|orientation"/>
+        <activity android:name=".activity.setting.ZoloActivity"
+            android:screenOrientation="portrait"
+            android:configChanges="keyboard|keyboardHidden|orientation"/>
+        <activity android:name=".activity.setting.FunctionActivity"
+            android:screenOrientation="portrait"
+            android:configChanges="keyboard|keyboardHidden|orientation"/>
 
         <receiver
             android:name=".broadcastreceiver.AutoStartBroadReceiver"

+ 2 - 2
config.gradle

@@ -7,8 +7,8 @@ ext {
 //            minSdkVersion    : 24,
             minSdkVersion    : 21,
             targetSdkVersion : 22,
-            versionCode      : 81,
-            versionName      : "3.2.0.241216"
+            versionCode      : 82,
+            versionName      : "3.2.1.241219"
     ]
     //依赖 配置
     dependencies = [

+ 10 - 1
lib_base/src/main/java/com/hh/lib_base/Constants.java

@@ -144,7 +144,10 @@ public class Constants {
         public static final String EVENT_STOP = "stop";
         //ampe event init
         public static final String EVENT_INIT = "init";
-
+        //ampe event get
+        public static final String EVENT_GET = "get";
+        //ampe event set
+        public static final String EVENT_SET = "set";
 
         // 二维码
         public static final String ACTION_QR = "getQRCode";
@@ -168,10 +171,16 @@ public class Constants {
 
         // 刷脸——江西(仅他一家的处理)
         public static final String ACTION_SCAN_FACE = "scanFace";
+
+
         // 设备信息
         public static final String ACTION_DEVICE = "getDeviceInfo";
         // 保存文件
         public static final String ACTION_STORAGE = "storage";
+        // 获取扬声器音量和设置扬声器音量
+        public static final String ACTION_LOUDSPEAKER = "loudspeaker";
+
+
 
         // 语音播报
         public static final String ACTION_SPEECH = "speech";

+ 86 - 1
lib_device/src/main/java/com/hh/lib_device/DevicesInvoker.java

@@ -1,7 +1,10 @@
 package com.hh.lib_device;
 
+import static com.hh.lib_base.Constants.Ampe.*;
+
 import android.app.Activity;
 import android.content.Context;
+import android.media.AudioManager;
 import android.text.TextUtils;
 import android.util.Log;
 
@@ -27,11 +30,14 @@ import com.hh.lib_device.bean.action.AmpeDeviceConfigBean;
 import com.hh.lib_device.bean.action.AmpeDeviceInfoResultBean;
 import com.hh.lib_device.bean.action.AmpeRequestDownloadFileBean;
 import com.hh.lib_device.bean.action.AmpeRequestFloatingBackBean;
+import com.hh.lib_device.bean.action.AmpeRequestVolumeBean;
 import com.hh.lib_device.bean.action.AmpeResultDownloadFileBean;
+import com.hh.lib_device.bean.action.AmpeResultVolumeBean;
 import com.hh.lib_device.bean.download.AmpeDownloadFileEvent;
 import com.hh.lib_device.deviceui.BaseDeviceUI;
 import com.hh.lib_device.deviceui.IDeviceUI;
 import com.hh.lib_device.utils.DownloadFileUtils;
+import com.hh.lib_device.utils.VolumeUtil;
 import com.hh.lib_device.view.TouchBackView;
 
 
@@ -78,6 +84,13 @@ public class DevicesInvoker {
                     removeStorage(dataBean);
                 }
                 break;
+                case Constants.Ampe.ACTION_LOUDSPEAKER:
+                    if(EVENT_GET.equals(dataBean.getEvent())){
+                        getMediaVolume(dataBean);
+                    }else if(EVENT_SET.equals(dataBean.getEvent())){
+                        setMediaVolume(dataBean);
+                    }
+                    break;
             default:
                 
                 break;
@@ -326,7 +339,7 @@ public class DevicesInvoker {
             return;
         }
         AmpeRequestDownloadFileBean params= newRequestBean.getParams();
-        DownloadFileUtils.getInstance().downLoadFile(params.getFileName(), params.getFileName()
+        DownloadFileUtils.getInstance().downLoadFile(params.getFileUrl(), params.getFileName()
                 , new DownloadFileUtils.DownloadFileListener() {
                     @Override
                     public void saveSuccess( AmpeResultDownloadFileBean data) {
@@ -432,6 +445,78 @@ public class DevicesInvoker {
                 ,jsonObject.toString());
     }
 
+
+
+    private void setMediaVolume(BaseAmpeRequestBean requestBean) {
+        if (requestBean == null) {
+            return;
+        }
+        BaseAmpeRequestBean<AmpeRequestVolumeBean> newRequestBean = MyTypeToken
+                .toBaseAmpeRequestBean(requestBean, AmpeRequestVolumeBean.class);
+        if (newRequestBean == null || newRequestBean.getParams() == null) {
+            return;
+        }
+        String volumeStr = newRequestBean.getParams().getVolume();
+        boolean isSuccess = true;
+        try {
+            float volume = Float.parseFloat(volumeStr);
+            if (volume < 0.0f || volume > 1.0f) {
+                isSuccess = false;
+            } else {
+                VolumeUtil.setMediaVolume(BaseApplication.getContext(), volume);
+            }
+        } catch (Exception e) {
+            isSuccess = false;
+        }
+
+        BaseAmpeAppletResultBean result = new BaseAmpeAppletResultBean();
+        result.setTaskId(requestBean.getTaskId());
+        result.setAction(requestBean.getAction());
+        result.setEvent(requestBean.getEvent());
+        result.setSuccess(isSuccess);
+        result.setMessage(isSuccess ? "成功" : "失败");
+
+        JsonObject jsonObject = GsonUtils.getGson().toJsonTree(result).getAsJsonObject();
+        if (requestBean.getBridgeCallback() != null) {
+            requestBean.getBridgeCallback().callback(jsonObject.toString());
+        }
+        String logLine = (new Throwable().getStackTrace())[0].toString();
+        UploadLogUtils.sendLogs("设备配置(设备音量)----",
+                Constants.Ampe.ACTION_DEVICE_CONFIG, logLine, jsonObject.toString());
+    }
+
+
+    private void getMediaVolume(BaseAmpeRequestBean requestBean) {
+        if (requestBean == null) {
+            return;
+        }
+        boolean isSuccess=true;
+        float currVolume=0f;
+        try {
+            currVolume= VolumeUtil.getNormalizedMediaVolume(BaseApplication.getContext());
+        }catch (Exception e){
+            isSuccess =false;
+        }
+
+        BaseAmpeAppletResultBean result = new BaseAmpeAppletResultBean();
+        result.setTaskId(requestBean.getTaskId());
+        result.setAction(requestBean.getAction());
+        result.setEvent(requestBean.getEvent());
+        result.setSuccess(isSuccess);
+        result.setMessage(isSuccess?"成功":"失败");
+        AmpeResultVolumeBean data =new AmpeResultVolumeBean();
+        data.setVolume(""+currVolume);
+        result.setData(data);
+
+        JsonObject jsonObject = GsonUtils.getGson().toJsonTree(result).getAsJsonObject();
+        if (requestBean.getBridgeCallback() != null) {
+            requestBean.getBridgeCallback().callback(jsonObject.toString());
+        }
+        String logLine = (new Throwable().getStackTrace())[0].toString();
+        UploadLogUtils.sendLogs("设备配置(设备音量)----",
+                Constants.Ampe.ACTION_DEVICE_CONFIG, logLine, jsonObject.toString());
+    }
+
     public static DevicesInvoker getInstance() {
         if (devicesInvoker == null) {
             synchronized (DevicesInvoker.class) {

+ 19 - 0
lib_device/src/main/java/com/hh/lib_device/bean/action/AmpeRequestVolumeBean.java

@@ -0,0 +1,19 @@
+package com.hh.lib_device.bean.action;
+
+/**
+ * Description: $
+ * @author xwh
+ * time 2024/11/14
+ **/
+public class AmpeRequestVolumeBean {
+
+    private String volume=""; //
+
+    public String getVolume() {
+        return volume;
+    }
+
+    public void setVolume(String volume) {
+        this.volume = volume;
+    }
+}

+ 20 - 0
lib_device/src/main/java/com/hh/lib_device/bean/action/AmpeResultVolumeBean.java

@@ -0,0 +1,20 @@
+package com.hh.lib_device.bean.action;
+
+/**
+ * Description: $
+ * @author xwh
+ * time 2024/11/14
+ **/
+public class AmpeResultVolumeBean {
+
+    private String volume="";
+
+
+    public String getVolume() {
+        return volume;
+    }
+
+    public void setVolume(String volume) {
+        this.volume = volume;
+    }
+}

+ 62 - 0
lib_device/src/main/java/com/hh/lib_device/utils/VolumeUtil.java

@@ -0,0 +1,62 @@
+package com.hh.lib_device.utils;
+
+import android.content.Context;
+import android.media.AudioManager;
+
+import java.math.BigDecimal;
+
+/**
+ * Description: $
+ *
+ * @author xwh
+ * time 2024/12/19
+ **/
+public class VolumeUtil {
+
+    public static int getMaxMediaVolume(Context context) {
+        AudioManager audioManager = (AudioManager) context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
+        // 获取最大媒体音量
+        int maxMediaVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
+        return maxMediaVolume;
+    }
+
+
+    public static int getMediaVolume(Context context) {
+        AudioManager audioManager = (AudioManager) context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
+        // 获取当前媒体音量
+        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
+        return currentVolume;
+    }
+
+
+    public static void setMediaVolume(int volume,Context context) {
+        AudioManager audioManager = (AudioManager) context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
+        if (audioManager != null) {
+            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_PLAY_SOUND | 0);
+        }
+    }
+
+    public static float getNormalizedMediaVolume(Context context) {
+        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        // 获取最大音量
+        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
+        // 获取当前音量
+        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
+        // 将当前音量转换为0到1的小数
+
+        BigDecimal bigA = new BigDecimal(currentVolume);
+        BigDecimal bigB = new BigDecimal(maxVolume);
+        BigDecimal result = bigA.divide(bigB, 2, BigDecimal.ROUND_HALF_UP); // 保留两位小数,四舍五入
+        return result.floatValue();
+    }
+
+    public static void setMediaVolume(Context context, float volume) {
+        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
+        BigDecimal bigDecimal = new BigDecimal(Float.toString(maxVolume * volume));
+        int volumeIndex = bigDecimal.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
+
+        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volumeIndex, AudioManager.FLAG_PLAY_SOUND | 0);
+    }
+
+}