desc:长按确定键关机,长按左右键调整音量

main
xiaowusky 2 years ago
parent 6c1e229183
commit f45d78748e

@ -3,6 +3,7 @@ package com.yinuo.safetywatcher.watcher.base
import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.LayoutInflater.Factory2
import android.view.View
@ -14,12 +15,15 @@ import android.widget.SeekBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatRadioButton
import com.common.commonlib.utils.LogUtils
import com.loper7.date_time_picker.number_picker.NumberPicker
import com.yinuo.safetywatcher.R
import com.yinuo.safetywatcher.databinding.ActivityBaseBinding
import com.yinuo.safetywatcher.watcher.constant.NAMESPACE_ANDROID
import com.yinuo.safetywatcher.watcher.ui.view.CommonDialog
import com.yinuo.safetywatcher.watcher.ui.view.CommonTopBar
import com.yinuo.safetywatcher.watcher.utils.PlatformUtils
import com.yinuo.safetywatcher.watcher.utils.VolumeUtil
import com.yinuo.safetywatcher.watcher.utils.trySetCommonBg
abstract class BaseActivity : AppCompatActivity() {
@ -28,17 +32,28 @@ abstract class BaseActivity : AppCompatActivity() {
ActivityBaseBinding.inflate(layoutInflater)
}
private val volumeUtil by lazy {
VolumeUtil(this@BaseActivity)
}
var loadingDialog: CommonDialog? = null;
override fun onCreate(savedInstanceState: Bundle?) {
setViewFactory()
super.onCreate(savedInstanceState)
setContentView(baseBinding.root)
initSound()
setChildContent()
initView()
setTopBar()
}
private fun initSound() {
baseBinding.sbSound.max = volumeUtil.mediaMaxVolume
val mediaVolume = volumeUtil.mediaVolume
baseBinding.sbSound.progress = mediaVolume
}
private fun setChildContent() {
val contentView = generateContentView()
baseBinding.container.addView(
@ -73,7 +88,9 @@ abstract class BaseActivity : AppCompatActivity() {
loadingDialog?.dismiss()
loadingDialog = CommonDialog(resId)
loadingDialog!!.isCancelable = cancelable
loadingDialog!!.show(supportFragmentManager, "loading")
if (!this.isFinishing) {
loadingDialog!!.show(supportFragmentManager, "loading")
}
}
fun isLoadingShowing(): Boolean {
@ -145,4 +162,57 @@ abstract class BaseActivity : AppCompatActivity() {
super.onDestroy()
closeLoadingDialog()
}
var isHomeLongPress = false
var isLeftRightLongPress = false
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
LogUtils.w("cyy dispatchKeyEvent keyCode = ${event?.keyCode} count = ${event?.repeatCount} acton = ${event?.action}")
val action = event.action
if (KeyEvent.ACTION_DOWN == action) {
return dealActionDown(event)
} else if (KeyEvent.ACTION_UP == action) {
return dealActionUp(event)
}
return super.dispatchKeyEvent(event)
}
private fun dealActionUp(event: KeyEvent): Boolean {
val repeatCount = event.repeatCount
val keyCode = event.keyCode
if (KeyEvent.KEYCODE_DPAD_LEFT == keyCode || KeyEvent.KEYCODE_DPAD_RIGHT == keyCode) {
if (isLeftRightLongPress) {
baseBinding.areaSound.visibility = View.GONE
volumeUtil.mediaVolume = baseBinding.sbSound.progress
return true
}
} else if (keyCode == KeyEvent.KEYCODE_ENTER) {
if (isHomeLongPress) {
PlatformUtils.shutDown()
return true
}
}
return super.dispatchKeyEvent(event)
}
private fun dealActionDown(event: KeyEvent): Boolean {
val repeatCount = event.repeatCount
val keyCode = event.keyCode
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
isLeftRightLongPress = repeatCount != 0
if (repeatCount != 0) {
val visibility = baseBinding.areaSound.visibility
if (View.GONE == visibility) {
baseBinding.areaSound.visibility = View.VISIBLE
baseBinding.sbSound.requestFocus()
}
var mediaVolume = baseBinding.sbSound.progress
if (KeyEvent.KEYCODE_DPAD_LEFT == keyCode) mediaVolume-- else mediaVolume++
baseBinding.sbSound.progress = mediaVolume++
return true
}
} else if (keyCode == KeyEvent.KEYCODE_ENTER) {
isHomeLongPress = repeatCount != 0
}
return super.dispatchKeyEvent(event)
}
}

@ -59,4 +59,10 @@ object PlatformUtils {
intent.putExtra("save", true) //true,断电保存false断电不保存
CommonApplication.getContext()?.sendBroadcast(intent)
}
fun shutDown() {
val intent = Intent("android.ido.intent.action.set.shutdown")
intent.putExtra("confirm", true)
CommonApplication.getContext()?.sendBroadcast(intent)
}
}

@ -0,0 +1,115 @@
package com.yinuo.safetywatcher.watcher.utils;
import android.app.Service;
import android.content.Context;
import android.media.AudioManager;
import android.util.Log;
import com.yinuo.library.vlc.utils.LogUtils;
/**
*
*/
public class VolumeUtil {
private AudioManager mAudioManager;
public VolumeUtil(){}
//获取系统的Audio管理者
public VolumeUtil(Context context){
mAudioManager = (AudioManager) context.getSystemService(Service.AUDIO_SERVICE);
}
//获取最大多媒体音量
public int getMediaMaxVolume(){
Log.e("ccm=======>", "getMediaMaxVolume: " + mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC));
return mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
}
//获取当前多媒体音量
public int getMediaVolume(){
Log.e("ccm=======>", "getMediaVolume: " +mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC ));
return mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );
}
//获取最大通话音量
public int getCallMaxVolume(){
return mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );
}
//获取当前通话音量
public int getCallVolume(){
return mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL );
}
//获取最大系统音量
public int getSystemMaxVolume(){
return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM );
}
//获取当前系统音量
public int getSystemVolume(){
return mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM );
}
//获取最大提示音量
public int getAlermMaxVolume(){
return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM );
}
//获取当前提示音量
public int getAlermVolume(){
return mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM );
}
// 设置多媒体音量
public void setMediaVolume(int volume){
LogUtils.w("setMediaVolume volume =" + volume);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, //音量类型
volume,
AudioManager.FLAG_PLAY_SOUND
| AudioManager.FLAG_SHOW_UI);
}
public void setMediaVolumeDown(){
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
}
public void setMediaVolumeUp(){
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
}
//设置通话音量
public void setCallVolume(int volume){
mAudioManager.setStreamVolume( AudioManager.STREAM_VOICE_CALL,
volume,
AudioManager.FLAG_PLAY_SOUND
| AudioManager.FLAG_SHOW_UI);
}
//设置提示音量
public void setAlermVolume(int volume){
mAudioManager.setStreamVolume( AudioManager.STREAM_ALARM,
volume,AudioManager.FLAG_PLAY_SOUND
|AudioManager.FLAG_SHOW_UI);
}
// 关闭/打开扬声器播放
public void setSpeakerStatus(boolean on) {
if (on) { //扬声器
mAudioManager.setSpeakerphoneOn(true);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
} else {
// 设置最大音量
int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, max, AudioManager.STREAM_VOICE_CALL);
// 设置成听筒模式
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.setSpeakerphoneOn(false);// 关闭扬声器
mAudioManager.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
}
}
}

@ -11,4 +11,33 @@
android:layout_width="match_parent"
android:layout_height="@dimen/_87dp"/>
<RelativeLayout
android:id="@+id/area_sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/warn_setting_btn_bg"
android:padding="@dimen/_20dp"
android:visibility="gone">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="@dimen/_40dp"
android:textColor="@color/white"
android:text="音量"/>
<SeekBar
android:id="@+id/sb_sound"
style="@style/LightSeekbarStyle"
android:focusable="true"
android:layout_width="@dimen/_630dp"
android:layout_height="@dimen/_100dp"
android:layout_marginTop="@dimen/_20dp"
android:layout_below="@+id/tv"
android:min="0"
android:padding="0dp"/>
</RelativeLayout>
</RelativeLayout>
Loading…
Cancel
Save