Example #1
1
 private void updateRingtoneName(int type, Preference preference, int msg) {
   if (preference == null) return;
   Context context = getActivity();
   if (context == null) return;
   Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, type);
   CharSequence summary = context.getString(com.android.internal.R.string.ringtone_unknown);
   // Is it a silent ringtone?
   if (ringtoneUri == null) {
     summary = context.getString(com.android.internal.R.string.ringtone_silent);
   } else {
     // Fetch the ringtone title from the media provider
     try {
       Cursor cursor =
           context
               .getContentResolver()
               .query(ringtoneUri, new String[] {MediaStore.Audio.Media.TITLE}, null, null, null);
       if (cursor != null) {
         if (cursor.moveToFirst()) {
           summary = cursor.getString(0);
         }
         cursor.close();
       }
     } catch (SQLiteException sqle) {
       // Unknown title for the ringtone
     }
   }
   mHandler.sendMessage(mHandler.obtainMessage(msg, summary));
 }
Example #2
0
  @Override
  public void onReceive(Context context, Intent data) {
    String actionString = data.getAction();

    switch (actionString) {
      case BROADCAST_ACTION:
        String message = data.getStringExtra(TweetService.MESSAGE);
        String title = data.getStringExtra(TweetService.TITLE);
        TweetService.PN type = (TweetService.PN) data.getSerializableExtra(TweetService.TYPE);

        Intent resultIntent = new Intent(context, MainActivity.class);

        switch (type) {
          case MESSAGE:
            resultIntent.putExtra(TweetService.LOCATION, TweetService.DESTINATION.MESSAGES);
            break;
          case MENTION:
            resultIntent.putExtra(TweetService.LOCATION, TweetService.DESTINATION.MENTIONS);
            break;
          case UPDATE:
            break;
        }

        PendingIntent resultPendingIntent =
            PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intent = new Intent(CANCEL_NOTIFICATION_ACTION);
        PendingIntent deleteIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(
                    RingtoneManager.getActualDefaultRingtoneUri(
                        context, RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(resultPendingIntent)
                .setLights(Color.CYAN, 1000, 4000)
                .setDeleteIntent(deleteIntent);

        // TODO different notification design for different types
        switch (type) {
          case MESSAGE:
          case MENTION:
          case UPDATE:
            builder.setSmallIcon(com.rtweel.R.drawable.rtweel);
        }

        int mNotificationId = 1;
        NotificationManager mNotifyMgr =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = builder.build();
        mNotifyMgr.notify(mNotificationId, notification);
        break;

      case CANCEL_NOTIFICATION_ACTION:
        TweetService.setNewTweets(0);
        break;

      case BOOT_WAKEUP_ACTION:
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        boolean isAlreadySet = prefs.contains(SettingsFragment.PN_INTERVAL);
        int intervalInMinutes;
        if (isAlreadySet) intervalInMinutes = prefs.getInt(SettingsFragment.PN_INTERVAL, 4 * 60);
        else intervalInMinutes = 60;

        long intervalInMillis = TimeUnit.MINUTES.toMillis(intervalInMinutes);
        Intent serviceIntent = new Intent(context, TweetService.class);
        PendingIntent alarmIntent =
            PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setInexactRepeating(
            AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + intervalInMillis,
            intervalInMillis,
            alarmIntent);
    }
  }
Example #3
0
 private void asyncAddAlarm() {
   Alarm a = new Alarm();
   a.alert = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
   asyncAddAlarm(a, true);
 }