/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.media;
import java.util.Arrays;
import java.util.List;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.util.Log;
import android.app.ActivityThread;
/**
* @hide
*
* damon.zhang Block app too frequent getStreamVolume
*/
public class NtAudioManager {
private static final String TAG = "NtAudioManager";
private long preGetVolumeTime = 0;
private String preGetVolumePkg = "";
private int mLastIndex = 0;
private static NtAudioManager mNtAudioManager;
private static String mPkg;
private static final BroadcastReceiver mNtAmReceiver;
public NtAudioManager(String pkg) {
mPkg = pkg;
}
public static NtAudioManager getNtAudioManager(Context contex, String pkg) {
//if (!mGetStreamVolumeBlackList.contains(pkg)) {
// return null;
//}
Context context = ActivityThread.currentApplication();
if (mNtAudioManager == null && context != null && mNtAmReceiver == null) {
mNtAudioManager = new NtAudioManager(pkg);
initReceiver(contex);
}
return mNtAudioManager;
}
private static final List<String> mGetStreamVolumeBlackList = Arrays.asList(new String[] {
"com.tencent.mobileqq",
"com.tencent.mtt",
"com.tencent.mm",
"com.tencent.KiHn",
"com.google.android.youtube",
"com.google.android.apps.youtube.music",
"com.google.android.googlequicksearchbox",
"com.facebook.katana",
"com.instagram.android",
"com.truecaller",
"com.miHoYo.hkrpg",
"com.spotify.music",
"ahaflix.tv",
"com.vkontakte.android",
"com.vk.equals",
"com.microsoft.appmanager",
"com.jio.media.ondemand",
"com.kiloo.subwaysurf",
"com.hongsong.live.lite",
"com.nothing.smartcenter"
});
public static int[] LAST_STREAM_VOLUME = new int[] {
-1, // STREAM_VOICE_CALL
-1, // STREAM_SYSTEM
-1, // STREAM_RING
-1, // STREAM_MUSIC
-1, // STREAM_ALARM
-1, // STREAM_NOTIFICATION
-1, // STREAM_BLUETOOTH_SCO
-1, // STREAM_SYSTEM_ENFORCED
-1, // STREAM_DTMF
-1, // STREAM_TTS
-1, // STREAM_ACCESSIBILITY
-1, // STREAM_ASSISTANT
};
public int getVolume(int streamType) {
return LAST_STREAM_VOLUME[streamType];
}
public void setVolume(int index, int streamType){
LAST_STREAM_VOLUME[streamType] = index;
}
public boolean isInitVolume(String pkgName, int streamType) {
ensureValidStreamType(streamType);
//if (!mGetStreamVolumeBlackList.contains(pkgName)) {
// return false;
//}
if (LAST_STREAM_VOLUME[streamType] == -1) {
return false;
}
return true;
}
private void ensureValidStreamType(int streamType) {
if (streamType < 0 || streamType >= LAST_STREAM_VOLUME.length) {
throw new IllegalArgumentException("Bad stream type " + streamType);
}
}
private void initReceiver(Context con) {
mNtAmReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(AudioManager.VOLUME_CHANGED_ACTION)) {
int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1);
int currentVolume = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, -1);
int lastVolume = intent.getIntExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, -1);
int streamTypeAltas = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS, -1);
Log.i(TAG, "mNtAudioManager: "+ mNtAudioManager
+ " mPkg: " + mPkg
+ " streamType : " + streamType
+ " streamTypeAltas: " + streamTypeAltas
+ " currentVolume: " + currentVolume
+ " lastVolume: " + lastVolume);
LAST_STREAM_VOLUME[streamType] = currentVolume;
}
}
};
IntentFilter intentFilter = new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION);
con.registerReceiver(mNtAmReceiver, intentFilter, null, null);
}
}
最新发布