Example usage for android.media RingtoneManager getRingtone

List of usage examples for android.media RingtoneManager getRingtone

Introduction

In this page you can find the example usage for android.media RingtoneManager getRingtone.

Prototype

public static Ringtone getRingtone(final Context context, Uri ringtoneUri) 

Source Link

Document

Returns a Ringtone for a given sound URI.

Usage

From source file:monakhv.android.samlib.SamlibPreferencesActivity.java

private void updateRingtoneSummary(RingtonePreference preference, Uri ringtoneUri) {
    Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
    if (ringtone != null) {
        preference.setSummary(ringtone.getTitle(this));
    } else {/*  w  w w .  j av a  2 s  .c om*/
        preference.setSummary(getString(R.string.pref_no_sound));
    }

}

From source file:com.remobile.dialogs.Notification.java

/**
 * Beep plays the default notification ringtone.
 *
 * @param count Number of times to play notification
 *///from www  . ja v a  2  s  . c  o m
public void beep(final long count) {
    final Activity activity = this.cordova.getActivity();
    this.cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone notification = RingtoneManager.getRingtone(activity.getBaseContext(), ringtone);

            // If phone is not set to silent mode
            if (notification != null) {
                for (long i = 0; i < count; ++i) {
                    notification.play();
                    long timeout = 5000;
                    while (notification.isPlaying() && (timeout > 0)) {
                        timeout = timeout - 100;
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                        }
                    }
                }
            }
        }
    });
}

From source file:org.proninyaroslav.libretorrent.settings.BehaviorSettingsFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_ALERT_RINGTONE && data != null) {
        Uri ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
        if (ringtone != null) {
            SettingsManager pref = new SettingsManager(getActivity().getApplicationContext());

            String keyNotifySound = getString(R.string.pref_key_notify_sound);
            Preference notifySound = findPreference(keyNotifySound);
            notifySound.setSummary(RingtoneManager.getRingtone(getActivity().getApplicationContext(), ringtone)
                    .getTitle(getActivity().getApplicationContext()));
            pref.put(keyNotifySound, ringtone.toString());
        }//from w ww  .j a  v  a 2  s.c o m
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

From source file:org.wso2.iot.agent.AlertActivity.java

/**
 * This method is used to start ringing the phone.
 *//*from  w  ww.  j  a v  a 2s  . c  om*/
@TargetApi(21)
private void startRing() {
    if (audio != null) {
        ringerMode = audio.getRingerMode();
        ringerVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
        audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        audio.setStreamVolume(AudioManager.STREAM_RING, audio.getStreamMaxVolume(AudioManager.STREAM_RING),
                AudioManager.FLAG_PLAY_SOUND);

        defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);

        if (defaultRingtoneUri != null) {
            defaultRingtone = RingtoneManager.getRingtone(this, defaultRingtoneUri);

            if (defaultRingtone != null) {
                if (deviceInfo.getSdkVersion() >= Build.VERSION_CODES.LOLLIPOP) {
                    AudioAttributes attributes = new AudioAttributes.Builder()
                            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build();
                    defaultRingtone.setAudioAttributes(attributes);
                } else {
                    defaultRingtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
                }
                defaultRingtone.play();
            }
        }
    }
}

From source file:eu.geopaparazzi.library.util.Utilities.java

/**
 * Ring action.//from w w w. ja  v  a2  s  .com
 *
 * @param context if something goes wrong.
 */
public static void ring(Context context) {
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(context, notification);
    r.play();

}

From source file:com.ariesmcrae.mymemories.ui.story.StoryViewFragment.java

public void setUiToStoryData(long getUniqueKey) throws RemoteException {
    Log.d(LOG_TAG, "setUiToStoryData");
    storyData = resolver.getStoryDataViaRowID(getUniqueKey);

    if (storyData == null) {
        getView().setVisibility(View.GONE);
    } else { // else it just displays empty screen
        Log.d(LOG_TAG, "setUiToStoryData + storyData:" + storyData.toString());
        titleTV.setText(String.valueOf(storyData.title).toString());
        bodyTV.setText(String.valueOf(storyData.body).toString());

        audioButton.setOnClickListener(new OnClickListener() {
            @Override/*from   www .  ja  v  a  2s .  c o  m*/
            public void onClick(View v) {
                String audioLinkPath = String.valueOf(storyData.audioLink).toString();

                if (audioLinkPath != null && audioLinkPath.length() > 0) {
                    Ringtone ringtone = RingtoneManager.getRingtone(getActivity(), Uri.parse(audioLinkPath));

                    if (!ringtone.isPlaying()) {
                        ringtone.play();
                    }
                }
            }
        });

        // Display the video
        String videoLinkPath = String.valueOf(storyData.videoLink).toString();

        if (videoLinkPath != null && videoLinkPath.length() > 0) {
            MediaController mediaController = new MediaController(getActivity());
            mediaController.setAnchorView(videoLinkView);
            videoLinkView.setMediaController(mediaController);
            videoLinkView.setVideoURI(Uri.parse(videoLinkPath));

            if (!videoLinkView.isPlaying()) {
                videoLinkView.start();
            }
        }

        // Display the image data
        imageNameTV.setText(String.valueOf(storyData.imageName).toString());

        String imageMetaDataPath = String.valueOf(storyData.imageLink).toString();
        imageMetaDataView.setImageURI(Uri.parse(imageMetaDataPath));

        Long time = Long.valueOf(storyData.storyTime);
        storyTimeTV.setText(StoryData.FORMAT.format(time));

        latitudeTV.setText(Double.valueOf(storyData.latitude).toString());
        longitudeTV.setText(Double.valueOf(storyData.longitude).toString());
    }
}

From source file:org.wso2.emm.agent.AlertActivity.java

/**
 * This method is used to start ringing the phone.
 *//*from  ww w. ja  v a2s.c  o m*/
@TargetApi(21)
private void startRing() {
    if (audio != null) {
        audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        audio.setStreamVolume(AudioManager.STREAM_RING, audio.getStreamMaxVolume(AudioManager.STREAM_RING),
                AudioManager.FLAG_PLAY_SOUND);

        defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);

        if (defaultRingtoneUri != null) {
            defaultRingtone = RingtoneManager.getRingtone(this, defaultRingtoneUri);

            if (defaultRingtone != null) {
                if (deviceInfo.getSdkVersion() >= Build.VERSION_CODES.LOLLIPOP) {
                    AudioAttributes attributes = new AudioAttributes.Builder()
                            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build();
                    defaultRingtone.setAudioAttributes(attributes);
                } else {
                    defaultRingtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
                }
                defaultRingtone.play();
            }
        }
    }
}

From source file:de.j4velin.chess.Main.java

@Override
public void onTurnBasedMatchReceived(final TurnBasedMatch match) {
    if (BuildConfig.DEBUG)
        Logger.log("Main onTurnBasedMatchReceived: " + match.getMatchId());
    if (match.getTurnStatus() == TurnBasedMatch.MATCH_TURN_STATUS_MY_TURN
            && match.getStatus() == TurnBasedMatch.MATCH_STATUS_ACTIVE) {
        final Ringtone tone = RingtoneManager.getRingtone(this,
                RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION));
        tone.setStreamType(AudioManager.STREAM_NOTIFICATION);
        tone.play();//from w  ww.j a va  2s.c  o  m
    }
    if (startFragment != null && startFragment.isVisible()) {
        startFragment.loadMatches();
    }
    if (gameFragment != null && gameFragment.isVisible()
            && match.getMatchId().equals(gameFragment.currentMatch)) {
        if (Game.load(match.getData(), match, mGoogleApiClient)) {
            gameFragment.update(match.getStatus() != TurnBasedMatch.MATCH_STATUS_ACTIVE
                    && match.getStatus() != TurnBasedMatch.MATCH_STATUS_AUTO_MATCHING);
        } else {
            updateApp();
        }
    }
}

From source file:ca.mudar.snoozy.receiver.PowerConnectionReceiver.java

private void nativeRingtone(Context context, boolean hasSound) {
    if (hasSound) {
        final AudioManager.OnAudioFocusChangeListener listener = this;
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        int result = audioManager.requestAudioFocus(listener, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {

            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            mRingtone = RingtoneManager.getRingtone(context, notification);
            mRingtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
            mRingtone.play();//from  ww w.j ava  2  s . co m

            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    audioManager.abandonAudioFocus(listener);
                }
            }, AUDIO_FOCUS_DURATION);
        }
    }
}

From source file:org.sipdroid.sipua.ui.Receiver.java

public static void onState(int state, String caller) {
    if (ccCall == null) {
        ccCall = new Call();
        ccConn = new Connection();
        ccCall.setConn(ccConn);//  www  . j ava2s. c o m
        ccConn.setCall(ccCall);
    }
    if (call_state != state) {
        if (state != UserAgent.UA_STATE_IDLE)
            call_end_reason = -1;
        call_state = state;
        switch (call_state) {
        case UserAgent.UA_STATE_INCOMING_CALL:
            enable_wifi(true);
            RtpStreamReceiver.good = RtpStreamReceiver.lost = RtpStreamReceiver.loss = RtpStreamReceiver.late = 0;
            RtpStreamReceiver.speakermode = speakermode();
            bluetooth = -1;
            String text = caller.toString();
            if (text.indexOf("<sip:") >= 0 && text.indexOf("@") >= 0)
                text = text.substring(text.indexOf("<sip:") + 5, text.indexOf("@"));
            String text2 = caller.toString();
            if (text2.indexOf("\"") >= 0)
                text2 = text2.substring(text2.indexOf("\"") + 1, text2.lastIndexOf("\""));
            broadcastCallStateChanged("RINGING", caller);
            mContext.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
            ccCall.setState(Call.State.INCOMING);
            ccConn.setUserData(null);
            ccConn.setAddress(text, text2);
            ccConn.setIncoming(true);
            ccConn.date = System.currentTimeMillis();
            ccCall.base = 0;
            AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
            int rm = am.getRingerMode();
            int vs = am.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER);
            KeyguardManager mKeyguardManager = (KeyguardManager) mContext
                    .getSystemService(Context.KEYGUARD_SERVICE);
            if (v == null)
                v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
            if ((pstn_state == null || pstn_state.equals("IDLE"))
                    && PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(
                            org.sipdroid.sipua.ui.Settings.PREF_AUTO_ON,
                            org.sipdroid.sipua.ui.Settings.DEFAULT_AUTO_ON)
                    && !mKeyguardManager.inKeyguardRestrictedInputMode())
                v.vibrate(vibratePattern, 1);
            else {
                if ((pstn_state == null || pstn_state.equals("IDLE")) && (rm == AudioManager.RINGER_MODE_VIBRATE
                        || (rm == AudioManager.RINGER_MODE_NORMAL && vs == AudioManager.VIBRATE_SETTING_ON)))
                    v.vibrate(vibratePattern, 1);
                if (am.getStreamVolume(AudioManager.STREAM_RING) > 0) {
                    String sUriSipRingtone = PreferenceManager.getDefaultSharedPreferences(mContext).getString(
                            org.sipdroid.sipua.ui.Settings.PREF_SIPRINGTONE,
                            Settings.System.DEFAULT_RINGTONE_URI.toString());
                    if (!TextUtils.isEmpty(sUriSipRingtone)) {
                        oRingtone = RingtoneManager.getRingtone(mContext, Uri.parse(sUriSipRingtone));
                        if (oRingtone != null)
                            oRingtone.play();
                    }
                }
            }
            moveTop();
            if (wl == null) {
                PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
                wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
                        "Sipdroid.onState");
            }
            wl.acquire();
            Checkin.checkin(true);
            break;
        case UserAgent.UA_STATE_OUTGOING_CALL:
            RtpStreamReceiver.good = RtpStreamReceiver.lost = RtpStreamReceiver.loss = RtpStreamReceiver.late = 0;
            RtpStreamReceiver.speakermode = speakermode();
            bluetooth = -1;
            onText(MISSED_CALL_NOTIFICATION, null, 0, 0);
            engine(mContext).register();
            broadcastCallStateChanged("OFFHOOK", caller);
            ccCall.setState(Call.State.DIALING);
            ccConn.setUserData(null);
            ccConn.setAddress(caller, caller);
            ccConn.setIncoming(false);
            ccConn.date = System.currentTimeMillis();
            ccCall.base = 0;
            moveTop();
            Checkin.checkin(true);
            break;
        case UserAgent.UA_STATE_IDLE:
            broadcastCallStateChanged("IDLE", null);
            onText(CALL_NOTIFICATION, null, 0, 0);
            ccCall.setState(Call.State.DISCONNECTED);
            if (listener_video != null)
                listener_video.onHangup();
            stopRingtone();
            if (wl != null && wl.isHeld())
                wl.release();
            mContext.startActivity(createIntent(InCallScreen.class));
            ccConn.log(ccCall.base);
            ccConn.date = 0;
            engine(mContext).listen();
            break;
        case UserAgent.UA_STATE_INCALL:
            broadcastCallStateChanged("OFFHOOK", null);
            if (ccCall.base == 0) {
                ccCall.base = SystemClock.elapsedRealtime();
            }
            progress();
            ccCall.setState(Call.State.ACTIVE);
            stopRingtone();
            if (wl != null && wl.isHeld())
                wl.release();
            mContext.startActivity(createIntent(InCallScreen.class));
            break;
        case UserAgent.UA_STATE_HOLD:
            onText(CALL_NOTIFICATION, mContext.getString(R.string.card_title_on_hold),
                    android.R.drawable.stat_sys_phone_call_on_hold, ccCall.base);
            ccCall.setState(Call.State.HOLDING);
            if (InCallScreen.started && (pstn_state == null || !pstn_state.equals("RINGING")))
                mContext.startActivity(createIntent(InCallScreen.class));
            break;
        }
        pos(true);
        RtpStreamReceiver.ringback(false);
    }
}