1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.dashboard.conditional; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.media.AudioManager; 24 import android.provider.Settings; 25 26 import com.android.settings.R; 27 28 public abstract class AbnormalRingerConditionBase extends Condition { 29 30 private final IntentFilter mFilter; 31 32 protected final AudioManager mAudioManager; 33 34 private final RingerModeChangeReceiver mReceiver; 35 AbnormalRingerConditionBase(ConditionManager manager)36 AbnormalRingerConditionBase(ConditionManager manager) { 37 super(manager); 38 mAudioManager = 39 (AudioManager) mManager.getContext().getSystemService(Context.AUDIO_SERVICE); 40 mReceiver = new RingerModeChangeReceiver(this); 41 42 mFilter = new IntentFilter(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION); 43 manager.getContext().registerReceiver(mReceiver, mFilter); 44 } 45 46 @Override getActions()47 public CharSequence[] getActions() { 48 return new CharSequence[] { 49 mManager.getContext().getText(R.string.condition_device_muted_action_turn_on_sound) 50 }; 51 } 52 53 @Override onPrimaryClick()54 public void onPrimaryClick() { 55 mManager.getContext().startActivity( 56 new Intent(Settings.ACTION_SOUND_SETTINGS) 57 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); 58 } 59 60 @Override onActionClick(int index)61 public void onActionClick(int index) { 62 mAudioManager.setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL); 63 mAudioManager.setStreamVolume(AudioManager.STREAM_RING, 1, 0 /* flags */); 64 refreshState(); 65 } 66 67 static class RingerModeChangeReceiver extends BroadcastReceiver { 68 69 private final AbnormalRingerConditionBase mCondition; 70 RingerModeChangeReceiver(AbnormalRingerConditionBase condition)71 public RingerModeChangeReceiver(AbnormalRingerConditionBase condition) { 72 mCondition = condition; 73 } 74 75 @Override onReceive(Context context, Intent intent)76 public void onReceive(Context context, Intent intent) { 77 final String action = intent.getAction(); 78 if (AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION.equals(action)) { 79 mCondition.refreshState(); 80 } 81 } 82 } 83 } 84