Java Code Examples for android.media.AudioManager#getRingerMode()

The following examples show how to use android.media.AudioManager#getRingerMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CaptureActivity.java    From MaterialHome with Apache License 2.0 6 votes vote down vote up
@Override
    protected void onResume() {
        super.onResume();
        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
        SurfaceHolder surfaceHolder = surfaceView.getHolder();
        if (hasSurface) {
            initCamera(surfaceHolder);
        } else {
            surfaceHolder.addCallback(this);
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
        decodeFormats = null;
        characterSet = null;

        playBeep = true;
        AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE);
        if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
            playBeep = false;
        }
        initBeepSound();
        vibrate = true;
//        MobclickAgent.onResume(this);
    }
 
Example 2
Source File: FollowingCheckService.java    From droidddle with Apache License 2.0 6 votes vote down vote up
private void notifyNewShots(ArrayList<Shot> shots) {
    if (shots.isEmpty()) {
        return;
    }

    getSharedPreferences().edit().putLong(PREF_LAST_SHOT_ID, shots.get(0).id).apply();

    int size = shots.size();
    // if only one shot, using BigPictureStyle
    // TODO test one notification
    boolean test = false && BuildConfig.DEBUG && new Random().nextBoolean();
    AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    boolean sound = audio.getRingerMode() == AudioManager.RINGER_MODE_NORMAL;
    if (size == 1 || test) {
        postShot(shots.get(0), sound);
    } else {
        //if have more shot, using InboxStyle and wearable pages...
        postShots(shots, sound);

    }

}
 
Example 3
Source File: ClockBackService.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void onServiceConnected() {
    if (isInfrastructureInitialized) {
        return;
    }

    mContext = this;

    // Send a message to start the TTS.
    mHandler.sendEmptyMessage(MESSAGE_START_TTS);

    // Get the vibrator service.
    mVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);

    // Get the AudioManager and configure according the current ring mode.
    mAudioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
    // In Froyo the broadcast receiver for the ringer mode is called back with the
    // current state upon registering but in Eclair this is not done so we poll here.
    int ringerMode = mAudioManager.getRingerMode();
    configureForRingerMode(ringerMode);

    // We are in an initialized state now.
    isInfrastructureInitialized = true;
}
 
Example 4
Source File: SilentOffAction.java    From beaconloc with Apache License 2.0 5 votes vote down vote up
@Override
public String execute(Context context) {
    final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int ringerMode = audioManager.getRingerMode();
    int old_mode = PreferencesUtil.getSilentModeProfile(context);
    if (ringerMode != AudioManager.RINGER_MODE_VIBRATE) {
        audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    }
    return super.execute(context);
}
 
Example 5
Source File: BeepManager.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
private static boolean shouldBeep(SharedPreferences prefs, Context activity) {
  boolean shouldPlayBeep = prefs.getBoolean(PreferencesActivity.KEY_PLAY_BEEP, true);
  if (shouldPlayBeep) {
    // See if sound settings overrides this
    AudioManager audioService = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
    if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
      shouldPlayBeep = false;
    }
  }
  return shouldPlayBeep;
}
 
Example 6
Source File: InterruptionFilterChangedBroadcastReceiver.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.M)
private static int getZenMode(Context context, AudioManager audioManager) {
    // convert to profile zenMode
    int zenMode = 0;
    NotificationManager mNotificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    if (mNotificationManager != null) {
        int interruptionFilter = mNotificationManager.getCurrentInterruptionFilter();
        //PPApplication.logE("InterruptionFilterChangedBroadcastReceiver.getZenMode", "interruptionFilter=" + interruptionFilter);
        int ringerMode = audioManager.getRingerMode();
        switch (interruptionFilter) {
            case NotificationManager.INTERRUPTION_FILTER_ALL:
                //if (ActivateProfileHelper.vibrationIsOn(/*context, */audioManager, true))
                if (ringerMode == AudioManager.RINGER_MODE_VIBRATE)
                    zenMode = 4;
                else
                    zenMode = 1;
                break;
            case NotificationManager.INTERRUPTION_FILTER_PRIORITY:
                //if (ActivateProfileHelper.vibrationIsOn(/*context, */audioManager, true))
                if (ringerMode == AudioManager.RINGER_MODE_VIBRATE)
                    zenMode = 5;
                else
                    zenMode = 2;
                break;
            case NotificationManager.INTERRUPTION_FILTER_NONE:
                zenMode = 3;
                break;
            case NotificationManager.INTERRUPTION_FILTER_ALARMS:
                zenMode = 6;
                break;
            case NotificationManager.INTERRUPTION_FILTER_UNKNOWN:
                zenMode = 1;
                break;
        }
        //PPApplication.logE("InterruptionFilterChangedBroadcastReceiver.getZenMode", "zenMode=" + zenMode);
    }
    return zenMode;
}
 
Example 7
Source File: Vibration.java    From reacteu-app with MIT License 5 votes vote down vote up
/**
 * Vibrates the device for a given amount of time.
 *
 * @param time      Time to vibrate in ms.
 */
public void vibrate(long time) {
    // Start the vibration, 0 defaults to half a second.
    if (time == 0) {
        time = 500;
    }
    AudioManager manager = (AudioManager) this.cordova.getActivity().getSystemService(Context.AUDIO_SERVICE);
    if (manager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) {
        Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(time);
    }
}
 
Example 8
Source File: BeepManager.java    From FoodOrdering with Apache License 2.0 5 votes vote down vote up
/**
 * 判断是否需要响铃
 * 
 * @param prefs
 * @param activity
 * @return
 */
private static boolean shouldBeep(SharedPreferences prefs, Context activity) {
	boolean shouldPlayBeep = prefs.getBoolean(
			PreferencesActivity.KEY_PLAY_BEEP, true);
	if (shouldPlayBeep) {
		// See if sound settings overrides this
		AudioManager audioService = (AudioManager) activity
				.getSystemService(Context.AUDIO_SERVICE);
		if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
			shouldPlayBeep = false;
		}
	}
	return shouldPlayBeep;
}
 
Example 9
Source File: VolumeHelper.java    From Saiy-PS with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Check if the device can currently output sound, by checking the media profile. This is unfortunately
 * not foolproof, due to the various ways different ROM and device manufacturers link the stream types.
 *
 * @param audioManager object
 * @return true if the device will output sounds, false otherwise
 */
private static boolean volumeProfileEnabled(@NonNull final AudioManager audioManager) {

    switch (audioManager.getRingerMode()) {

        case AudioManager.RINGER_MODE_NORMAL:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "volumeProfileEnabled: RINGER_MODE_NORMAL");
            }
            return true;
        case AudioManager.RINGER_MODE_VIBRATE:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "volumeProfileEnabled: RINGER_MODE_VIBRATE");
            }
            break;
        case AudioManager.RINGER_MODE_SILENT:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "volumeProfileEnabled: RINGER_MODE_SILENT");
            }
            break;
        default:
            if (DEBUG) {
                MyLog.w(CLS_NAME, "volumeProfileEnabled: Default");
            }
            break;
    }

    return false;

}
 
Example 10
Source File: Device.java    From unity-ads-android with Apache License 2.0 5 votes vote down vote up
public static int getRingerMode () {
	if (ClientProperties.getApplicationContext() != null) {
		AudioManager am = (AudioManager)ClientProperties.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);

		if (am != null)
			return am.getRingerMode();
		else
			return -2;
	}

	return -1;
}
 
Example 11
Source File: BeepManager.java    From FamilyChat with Apache License 2.0 5 votes vote down vote up
private static boolean shouldBeep(SharedPreferences prefs, Context activity) {
    boolean shouldPlayBeep = true;
    if (shouldPlayBeep) {
        // See if sound settings overrides this
        AudioManager audioService = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
        if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
            shouldPlayBeep = false;
        }
    }
    return shouldPlayBeep;
}
 
Example 12
Source File: PrayerNotification.java    From MuslimMateAndroid with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Function to check to make mobile silent in prayer
 */
private void changeMobileToSilentMood() {
    AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int ringerMode = mAudioManager.getRingerMode();
    if (ringerMode != AudioManager.RINGER_MODE_SILENT) {
        Alarms.switchToSilent(10, this);
    }
}
 
Example 13
Source File: AlertPlayer.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
private boolean isLoudPhone(Context ctx) {
    AudioManager am = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);

    switch (am.getRingerMode()) {
        case AudioManager.RINGER_MODE_SILENT:
            return false;
        case AudioManager.RINGER_MODE_VIBRATE:
            return false;
        case AudioManager.RINGER_MODE_NORMAL:
            return true;
    }
    // unknown mode, not sure let's play just in any case.
    return true;
}
 
Example 14
Source File: BeepManager.java    From ZXing-Standalone-library with Apache License 2.0 5 votes vote down vote up
private static boolean shouldBeep(SharedPreferences prefs, Context activity) {
  boolean shouldPlayBeep = prefs.getBoolean(PreferencesActivity.KEY_PLAY_BEEP, true);
  if (shouldPlayBeep) {
    // See if sound settings overrides this
    AudioManager audioService = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
    if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
      shouldPlayBeep = false;
    }
  }
  return shouldPlayBeep;
}
 
Example 15
Source File: IncomingRinger.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public void start() {
  AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);

  if (player != null) player.release();
  player = createPlayer();

  int ringerMode = audioManager.getRingerMode();

  if (shouldVibrate(context, player, ringerMode)) {
    Log.i(TAG, "Starting vibration");
    vibrator.vibrate(VIBRATE_PATTERN, 1);
  }

  if (player != null && ringerMode == AudioManager.RINGER_MODE_NORMAL) {
    try {
      if (!player.isPlaying()) {
        player.prepare();
        player.start();
        Log.w(TAG, "Playing ringtone now...");
      } else {
        Log.w(TAG, "Ringtone is already playing, declining to restart.");
      }
    } catch (IllegalStateException | IOException e) {
      Log.w(TAG, e);
      player = null;
    }
  } else {
    Log.w(TAG, "Not ringing, mode: " + ringerMode);
  }
}
 
Example 16
Source File: SilenterReceiver.java    From prayer-times-android with Apache License 2.0 4 votes vote down vote up
public static void silent(Context c, int mins) {
    if (!PermissionUtils.get(c).pNotPolicy && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        androidx.core.app.NotificationCompat.Builder builder;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            builder = new androidx.core.app.NotificationCompat.Builder(c, NotificationUtils.getPlayingChannel(c));
        } else {
            builder = new NotificationCompat.Builder(c);
        }

        builder = builder.setContentTitle(c.getString(R.string.silenterNotificationTitle))
                .setContentText(c.getString(R.string.silenterNotificationInfo))
                .setContentIntent(PendingIntent.getActivity(c, 0, new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS), 0))
                .setSmallIcon(R.drawable.ic_abicon);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(NotificationUtils.getAlarmChannel(c));
        } else {
            builder.setPriority(Notification.PRIORITY_DEFAULT);
        }

        NotificationManager nm = (NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify("silenter", 557457, builder.build());
        return;
    }


    AudioManager aum = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
    int ringermode = aum.getRingerMode();
    boolean modeVibrate = "vibrate".equals(Preferences.SILENTER_MODE.get());
    boolean isSilent = ringermode == AudioManager.RINGER_MODE_SILENT;
    boolean isVibrate = ringermode == AudioManager.RINGER_MODE_VIBRATE;
    if ((modeVibrate && !isVibrate && !isSilent) || (!modeVibrate && !isSilent)) {
        AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);

        Intent i = new Intent(c, SilenterReceiver.class);
        i.putExtra("mode", ringermode);

        PendingIntent service = PendingIntent.getBroadcast(c, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000 * 60 * mins), service);


        aum.setRingerMode(modeVibrate ? AudioManager.RINGER_MODE_VIBRATE : AudioManager.RINGER_MODE_SILENT);
    }
}
 
Example 17
Source File: NfcVReaderTask.java    From OpenLibre with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onPostExecute(Boolean success) {
    mainActivity.findViewById(R.id.pb_scan_circle).setVisibility(View.INVISIBLE);

    Vibrator vibrator = (Vibrator) mainActivity.getSystemService(VIBRATOR_SERVICE);
    AudioManager audioManager = (AudioManager) mainActivity.getSystemService(Context.AUDIO_SERVICE);

    if (!success) {
        Toast.makeText(mainActivity,
                mainActivity.getResources().getString(R.string.reading_sensor_error),
                Toast.LENGTH_SHORT
        ).show();

        if (audioManager.getRingerMode() != RINGER_MODE_SILENT) {
            vibrator.vibrate(vibrationPatternFailure, -1);
        }
        return;
    }

    if (audioManager.getRingerMode() != RINGER_MODE_SILENT) {
        vibrator.vibrate(vibrationPatternSuccess, -1);
    }

    if (RawTagData.getSensorReadyInMinutes(data) > 0) {
        Toast.makeText(mainActivity,
                mainActivity.getResources().getString(R.string.reading_sensor_not_ready) + " " +
                        String.format(mainActivity.getResources().getString(R.string.sensor_ready_in), RawTagData.getSensorReadyInMinutes(data)) + " " +
                        mainActivity.getResources().getString(R.string.minutes),
                Toast.LENGTH_LONG
        ).show();

        final TextView tv_sensor_ready_counter = (TextView) mainActivity.findViewById(R.id.tv_sensor_ready_counter);
        tv_sensor_ready_counter.setVisibility(View.VISIBLE);

        new CountDownTimer(TimeUnit.MINUTES.toMillis(RawTagData.getSensorReadyInMinutes(data)), TimeUnit.MINUTES.toMillis(1)) {
            public void onTick(long millisUntilFinished) {
                int readyInMinutes = (int) Math.ceil(((double) millisUntilFinished) / TimeUnit.MINUTES.toMillis(1));
                tv_sensor_ready_counter.setText(String.format(
                        mainActivity.getResources().getString(R.string.sensor_ready_in),
                        readyInMinutes));
            }

            public void onFinish() {
                tv_sensor_ready_counter.setVisibility(View.INVISIBLE);
            }
        }.start();

        return;
    }

    Toast.makeText(mainActivity,
            mainActivity.getResources().getString(R.string.reading_sensor_success),
            Toast.LENGTH_SHORT
    ).show();

    // FIXME: the new data should be propagated transparently through the database backend
    mainActivity.onNfcReadingFinished(processRawData(sensorTagId, data));
}
 
Example 18
Source File: Utility.java    From iBeebo with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isSystemRinger(Context context) {
    AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    return manager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL;
}
 
Example 19
Source File: VoIPBaseService.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
protected void startRingtoneAndVibration(int chatID){
	SharedPreferences prefs = MessagesController.getNotificationsSettings(currentAccount);
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	boolean needRing=am.getRingerMode()!=AudioManager.RINGER_MODE_SILENT;
	if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
		try{
			int mode=Settings.Global.getInt(getContentResolver(), "zen_mode");
			if(needRing)
				needRing=mode==0;
		}catch(Exception ignore){}
	}
	if(needRing){
		if(!USE_CONNECTION_SERVICE){
			am.requestAudioFocus(this, AudioManager.STREAM_RING, AudioManager.AUDIOFOCUS_GAIN);
		}
		ringtonePlayer=new MediaPlayer();
		ringtonePlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
			@Override
			public void onPrepared(MediaPlayer mediaPlayer){
				ringtonePlayer.start();
			}
		});
		ringtonePlayer.setLooping(true);
		ringtonePlayer.setAudioStreamType(AudioManager.STREAM_RING);
		try{
			String notificationUri;
			if(prefs.getBoolean("custom_"+chatID, false))
				notificationUri=prefs.getString("ringtone_path_"+chatID, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE).toString());
			else
				notificationUri=prefs.getString("CallsRingtonePath", RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE).toString());
			ringtonePlayer.setDataSource(this, Uri.parse(notificationUri));
			ringtonePlayer.prepareAsync();
		}catch(Exception e){
			FileLog.e(e);
			if(ringtonePlayer!=null){
				ringtonePlayer.release();
				ringtonePlayer=null;
			}
		}
		int vibrate;
		if(prefs.getBoolean("custom_"+chatID, false))
			vibrate=prefs.getInt("calls_vibrate_"+chatID, 0);
		else
			vibrate=prefs.getInt("vibrate_calls", 0);
		if((vibrate!=2 && vibrate!=4 && (am.getRingerMode()==AudioManager.RINGER_MODE_VIBRATE || am.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)) ||
				(vibrate==4 && am.getRingerMode()==AudioManager.RINGER_MODE_VIBRATE)){
			vibrator=(Vibrator) getSystemService(VIBRATOR_SERVICE);
			long duration=700;
			if(vibrate==1)
				duration/=2;
			else if(vibrate==3)
				duration*=2;
			vibrator.vibrate(new long[]{0, duration, 500}, 0);
		}
	}
}
 
Example 20
Source File: DeviceEventUpdatesProvider.java    From PrivacyStreams with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String event = null;
    String type = null;

    switch(intent.getAction()){
        case Intent.ACTION_SCREEN_OFF:
            event = DeviceEvent.EVENT_SCREEN_OFF;
            type = DeviceEvent.TYPE_SCREEN;
            break;

        case Intent.ACTION_SCREEN_ON:
            event = DeviceEvent.EVENT_SCREEN_ON;
            type = DeviceEvent.TYPE_SCREEN;
            break;

        case Intent.ACTION_USER_PRESENT:
            event = DeviceEvent.EVENT_SCREEN_USER_PRESENT;
            type = DeviceEvent.TYPE_SCREEN;
            break;

        case Intent.ACTION_BOOT_COMPLETED:
            event = DeviceEvent.EVENT_BOOT_COMPLETED;
            type = DeviceEvent.TYPE_BOOT;
            break;

        case Intent.ACTION_SHUTDOWN:
            event = DeviceEvent.EVENT_BOOT_SHUTDOWN;
            type = DeviceEvent.TYPE_BOOT;
            break;

        case Intent.ACTION_BATTERY_LOW:
            event = DeviceEvent.EVENT_BATTERY_LOW;
            type = DeviceEvent.TYPE_BATTERY;
            break;

        case Intent.ACTION_BATTERY_OKAY:
            event = DeviceEvent.EVENT_BATTERY_OKAY;
            type = DeviceEvent.TYPE_BATTERY;
            break;

        case Intent.ACTION_POWER_CONNECTED:
            event = DeviceEvent.EVENT_BATTERY_AC_CONNECTED;
            type = DeviceEvent.TYPE_BATTERY;
            break;

        case Intent.ACTION_POWER_DISCONNECTED:
            event = DeviceEvent.EVENT_BATTERY_AC_DISCONNECTED;
            type = DeviceEvent.TYPE_BATTERY;
            break;

        case AudioManager.RINGER_MODE_CHANGED_ACTION:
            AudioManager am = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
            switch (am.getRingerMode()) {
                case AudioManager.RINGER_MODE_SILENT:
                    event = DeviceEvent.EVENT_RINGER_SILENT;
                    type = DeviceEvent.TYPE_RINGER;
                    break;

                case AudioManager.RINGER_MODE_VIBRATE:
                    event = DeviceEvent.EVENT_RINGER_VIBRATE;
                    type = DeviceEvent.TYPE_RINGER;
                    break;

                case AudioManager.RINGER_MODE_NORMAL:
                    event = DeviceEvent.EVENT_RINGER_NORMAL;
                    type = DeviceEvent.TYPE_RINGER;
                    break;
            }
        default:
            break;
    }

    if (type != null)
        output(new DeviceEvent(type, event));
}