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:com.wizardsofm.deskclock.data.TimerModel.java

/**
 * @return the title of the ringtone that is played for all timers
 *//*from   w ww  . j ava2 s  . c o  m*/
String getTimerRingtoneTitle() {
    if (mTimerRingtoneTitle == null) {
        if (isTimerRingtoneSilent()) {
            // Special case: no ringtone has a title of "Silent".
            mTimerRingtoneTitle = mContext.getString(com.wizardsofm.deskclock.R.string.silent_ringtone_title);
        } else {
            final Uri defaultUri = getDefaultTimerRingtoneUri();
            final Uri uri = getTimerRingtoneUri();

            if (defaultUri.equals(uri)) {
                // Special case: default ringtone has a title of "Timer Expired".
                mTimerRingtoneTitle = mContext
                        .getString(com.wizardsofm.deskclock.R.string.default_timer_ringtone_title);
            } else {
                final Ringtone ringtone = RingtoneManager.getRingtone(mContext, uri);
                mTimerRingtoneTitle = ringtone.getTitle(mContext);
            }
        }
    }

    return mTimerRingtoneTitle;
}

From source file:com.android.deskclock.data.TimerModel.java

/**
 * @return the title of the ringtone that is played for all timers
 *//*from   w  w w.j  a  v  a 2 s .  com*/
String getTimerRingtoneTitle() {
    if (mTimerRingtoneTitle == null) {
        if (isTimerRingtoneSilent()) {
            // Special case: no ringtone has a title of "Silent".
            mTimerRingtoneTitle = mContext.getString(R.string.silent_timer_ringtone_title);
        } else {
            final Uri defaultUri = getDefaultTimerRingtoneUri();
            final Uri uri = getTimerRingtoneUri();

            if (defaultUri.equals(uri)) {
                // Special case: default ringtone has a title of "Timer Expired".
                mTimerRingtoneTitle = mContext.getString(R.string.default_timer_ringtone_title);
            } else {
                final Ringtone ringtone = RingtoneManager.getRingtone(mContext, uri);
                mTimerRingtoneTitle = ringtone.getTitle(mContext);
            }
        }
    }

    return mTimerRingtoneTitle;
}

From source file:com.xorcode.andtweet.PreferencesActivity.java

protected void showRingtone(Object newValue) {
    String ringtone = (String) newValue;
    Uri uri;/*from  w w w . j  a v a 2 s  .  com*/
    Ringtone rt;
    if (ringtone == null) {
        uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    } else if ("".equals(ringtone)) {
        mNotificationRingtone.setSummary(R.string.summary_preference_no_ringtone);
    } else {
        uri = Uri.parse(ringtone);
        rt = RingtoneManager.getRingtone(this, uri);
        mNotificationRingtone.setSummary(rt.getTitle(this));
    }
}

From source file:shetye.prathamesh.notifyme.Utilities.java

public void generateNotification(Context context, int ID, String title, String message, boolean isOngoing) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Intent notificationIntent = new Intent(context, NotifyMe.class);
    notificationIntent.setData(Uri.parse(Integer.toString(ID)));
    notificationIntent.putExtra(Utilities.NOTIF_EXTRA_ID_KEY, ID);
    notificationIntent.putExtra(Utilities.NOTIF_EXTRA_KEY, message);
    notificationIntent.putExtra(Utilities.NOTIF_EXTRA_TITLE_KEY, title);

    Intent laterIntent = new Intent(context, NotificationDetail.class);
    laterIntent.setData(Uri.parse(Integer.toString(ID)));
    laterIntent.putExtra(Utilities.NOTIF_EXTRA_ID_KEY, ID);
    laterIntent.putExtra(Utilities.NOTIF_EXTRA_DONE_LATER_KEY, true);

    Intent doneIntent = new Intent(context, RecieveAndNotify.class);
    doneIntent.setData(Uri.parse(Integer.toString(ID)));
    doneIntent.setAction(NOTIF_SERVICE_DONE_ACTION);
    doneIntent.putExtra(Utilities.NOTIF_EXTRA_ID_KEY, ID);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent remindLaterIntent = PendingIntent.getActivity(context, 0, laterIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent completedIntent = PendingIntent.getBroadcast(context, 0, doneIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    String newTitle;/*from   w  w  w. j  av  a2  s. com*/
    if (title == null || title.isEmpty())
        newTitle = context.getResources().getString(R.string.app_name);
    else
        newTitle = title;
    inboxStyle.setBigContentTitle(newTitle);
    inboxStyle.addLine(message);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_notif).setContentTitle(newTitle).setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_MAX).setContentIntent(resultPendingIntent)
            .addAction(R.drawable.ic_restore, "Later", remindLaterIntent)
            .addAction(R.drawable.ic_done, "Done", completedIntent).setStyle(inboxStyle);
    Log.d(LOG_TAG, "Notifying for ID : " + ID);
    notificationManager.notify(ID, mBuilder.build());
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(context, notification);
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

        // Vibrate for 300 milliseconds
        v.vibrate(200);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.smssecure.smssecure.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context, Recipients recipients) {
    if (!SilencePreferences.isInThreadNotifications(context)
            || ServiceUtil.getAudioManager(context).getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
        return;//from w w w.j  av  a  2s.c  om
    }

    Uri uri = recipients != null ? recipients.getRingtone() : null;

    if (uri == null) {
        String ringtone = SilencePreferences.getNotificationRingtone(context);

        if (ringtone == null) {
            Log.w(TAG, "ringtone preference was null.");
            return;
        }

        uri = Uri.parse(ringtone);

        if (uri == null) {
            Log.w(TAG, "couldn't parse ringtone uri " + ringtone);
            return;
        }
    }

    if (uri.toString().isEmpty()) {
        Log.d(TAG, "ringtone uri is empty");
        return;
    }

    Ringtone ringtone = RingtoneManager.getRingtone(context, uri);

    if (ringtone == null) {
        Log.w(TAG, "ringtone is null");
        return;
    }

    if (Build.VERSION.SDK_INT >= 21) {
        ringtone.setAudioAttributes(
                new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT).build());
    } else {
        ringtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
    }

    ringtone.play();
}

From source file:com.stasbar.knowyourself.data.TimerModel.java

/**
 * @return the title of the ringtone that is played for all timers
 *//*from  www  .j av  a  2  s .co m*/
String getTimerRingtoneTitle() {
    if (mTimerRingtoneTitle == null) {
        if (isTimerRingtoneSilent()) {
            // Special case: no ringtone has a title of "Silent".
            mTimerRingtoneTitle = mContext.getString(R.string.silent_ringtone_title);
        } else {
            final Uri defaultUri = getDefaultTimerRingtoneUri();
            final Uri uri = getTimerRingtoneUri();

            if (defaultUri.equals(uri)) {
                // Special case: default ringtone has a title of "Timer Expired".
                mTimerRingtoneTitle = mContext.getString(R.string.default_timer_ringtone_title);
            } else {
                final Ringtone ringtone = RingtoneManager.getRingtone(mContext, uri);
                mTimerRingtoneTitle = ringtone.getTitle(mContext);
            }
        }
    }

    return mTimerRingtoneTitle;
}

From source file:edu.mit.viral.shen.DroidFish.java

/**
 * Plays device's default notification sound
 * *///from   w w w.  ja  va  2 s .c  om
public void playBeep() {

    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.coinblesk.client.KeyboardFragment.java

private void playNotificationSound() {
    try {// w w  w  . j av  a2s  .c o  m
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getActivity(), notification);
        r.play();
    } catch (Exception e) {
        Log.e(TAG, "Error playing notification.", e);
    }
}

From source file:com.googlecode.mindbell.MindBellPreferences.java

/**
 * Sets the ringtone title into the summary of the ringtone preference.
 *
 * @param preferenceRingtone//from w w w . j  a  v a  2  s  .c  om
 * @param uriString
 */
private void setPreferenceRingtoneSummary(RingtonePreference preferenceRingtone, String uriString) {
    CharSequence summary;
    if (uriString == null || uriString.isEmpty()) {
        summary = getText(R.string.summaryRingtoneNotSet);
    } else {
        Uri ringtoneUri = Uri.parse(uriString);
        Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
        summary = ringtone.getTitle(this);
    }
    preferenceRingtone.setSummary(summary);
}

From source file:org.matrix.console.activity.CallViewActivity.java

/**
 * Provices a ringtone from a resource and a filename.
 * The audio file must have a ANDROID_LOOP metatada set to true to loop the sound.
 * @param resid The audio resource.//from  ww w.ja  v a 2s.co  m
 * @param filename the audio filename
 * @return a RingTone, null if the operation fails.
 */
static Ringtone getRingTone(Context context, int resid, String filename) {
    Ringtone ringtone = null;

    try {
        Uri ringToneUri = null;
        File directory = new File(Environment.getExternalStorageDirectory(),
                "/" + context.getApplicationContext().getPackageName().hashCode() + "/Audio/");

        // create the directory if it does not exist
        if (!directory.exists()) {
            directory.mkdirs();
        }

        File file = new File(directory + "/", filename);

        // if the file exists, check if the resource has been created
        if (file.exists()) {
            Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    new String[] { MediaStore.Audio.Media._ID }, MediaStore.Audio.Media.DATA + "=? ",
                    new String[] { file.getAbsolutePath() }, null);

            if ((null != cursor) && cursor.moveToFirst()) {
                int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
                ringToneUri = Uri.withAppendedPath(Uri.parse("content://media/external/audio/media"), "" + id);
            }
        }

        // the Uri has been received
        if (null == ringToneUri) {
            // create the file
            if (!file.exists()) {
                try {
                    byte[] readData = new byte[1024];
                    InputStream fis = context.getResources().openRawResource(resid);
                    FileOutputStream fos = new FileOutputStream(file);
                    int i = fis.read(readData);

                    while (i != -1) {
                        fos.write(readData, 0, i);
                        i = fis.read(readData);
                    }

                    fos.close();
                } catch (Exception e) {
                }
            }

            // and the resource Uri
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, filename);
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
            values.put(MediaStore.MediaColumns.SIZE, file.length());
            values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
            values.put(MediaStore.Audio.Media.IS_ALARM, true);
            values.put(MediaStore.Audio.Media.IS_MUSIC, true);

            ringToneUri = context.getContentResolver()
                    .insert(MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()), values);
        }

        if (null != ringToneUri) {
            ringtone = RingtoneManager.getRingtone(context, ringToneUri);
        }
    } catch (Exception e) {

    }

    return ringtone;
}