X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/3ac67b3001eb36b0bcf49b0798a8d4e0dc1e9953..75b88e18b40bdb8abb6eec74027720209bee9349:/src/com/owncloud/android/media/MediaService.java diff --git a/src/com/owncloud/android/media/MediaService.java b/src/com/owncloud/android/media/MediaService.java index db64610a..d6fbfcc2 100644 --- a/src/com/owncloud/android/media/MediaService.java +++ b/src/com/owncloud/android/media/MediaService.java @@ -40,6 +40,7 @@ import android.net.wifi.WifiManager.WifiLock; import android.os.IBinder; import android.os.PowerManager; import android.support.v7.app.AlertDialog; +import android.support.v7.app.NotificationCompat; import android.widget.Toast; import java.io.IOException; @@ -128,7 +129,6 @@ public class MediaService extends Service implements OnCompletionListener, OnPre /** Notification to keep in the notification bar while a song is playing */ private NotificationManager mNotificationManager; - private Notification mNotification = null; /** File being played */ private OCFile mFile; @@ -147,8 +147,9 @@ public class MediaService extends Service implements OnCompletionListener, OnPre /** Control panel shown to the user to control the playback, to register through binding */ private MediaControlView mMediaController; - + /** Notification builder to create notifications, new reuse way since Android 6 */ + private NotificationCompat.Builder mNotificationBuilder; /** * Helper method to get an error message suitable to show to users for errors occurred in media playback, @@ -217,15 +218,16 @@ public class MediaService extends Service implements OnCompletionListener, OnPre public static AlertDialog.Builder streamWithExternalApp(final String uri, final Activity activity){ AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setMessage("May expose password?") - .setPositiveButton("Stream", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - Intent i = new Intent(android.content.Intent.ACTION_VIEW); - i.setData(Uri.parse(uri)); - activity.startActivity(i); - } - }) - .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + builder.setMessage(activity.getString(R.string.stream_expose_password)) + .setPositiveButton(activity.getString(R.string.common_yes), + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + Intent i = new Intent(Intent.ACTION_VIEW); + i.setData(Uri.parse(uri)); + activity.startActivity(i); + } + }) + .setNegativeButton(activity.getString(R.string.common_no), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } @@ -249,6 +251,8 @@ public class MediaService extends Service implements OnCompletionListener, OnPre createWifiLock(WifiManager.WIFI_MODE_FULL, MEDIA_WIFI_LOCK_TAG); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + mNotificationBuilder = new NotificationCompat.Builder(this); + mNotificationBuilder.setColor(this.getResources().getColor(R.color.primary)); mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE); mBinder = new MediaServiceBinder(this); } @@ -309,7 +313,6 @@ public class MediaService extends Service implements OnCompletionListener, OnPre mState = State.PLAYING; setUpAsForeground(String.format(getString(R.string.media_state_playing), mFile.getFileName())); configAndStartMediaPlayer(); - } } @@ -453,11 +456,6 @@ public class MediaService extends Service implements OnCompletionListener, OnPre releaseResources(false); // release everything except MediaPlayer try { -// if (mFile == null) { -// Toast.makeText(this, R.string.media_err_nothing_to_play, Toast.LENGTH_LONG).show(); -// processStopRequest(true); -// return; - if (mAccount == null) { Toast.makeText(this, R.string.media_err_not_in_owncloud, Toast.LENGTH_LONG).show(); processStopRequest(true); @@ -467,7 +465,7 @@ public class MediaService extends Service implements OnCompletionListener, OnPre createMediaPlayerIfNeeded(); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); String url = mFile.getStoragePath(); - // Streaming is not possible right now + if (url == null || url.length() <= 0) { url = AccountUtils.constructFullURLForAccount(this, mAccount) + mFile.getRemotePath(); } @@ -557,22 +555,25 @@ public class MediaService extends Service implements OnCompletionListener, OnPre /** * Updates the status notification */ - @SuppressWarnings("deprecation") private void updateNotification(String content) { + String ticker = String.format(getString(R.string.media_notif_ticker), getString(R.string.app_name)); + // TODO check if updating the Intent is really necessary Intent showDetailsIntent = new Intent(this, FileDisplayActivity.class); showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, mFile); showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, mAccount); showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), - (int)System.currentTimeMillis(), - showDetailsIntent, - PendingIntent.FLAG_UPDATE_CURRENT); - mNotification.when = System.currentTimeMillis(); - //mNotification.contentView.setTextViewText(R.id.status_text, content); - String ticker = String.format(getString(R.string.media_notif_ticker), getString(R.string.app_name)); - mNotification.setLatestEventInfo(getApplicationContext(), ticker, content, mNotification.contentIntent); - mNotificationManager.notify(R.string.media_notif_ticker, mNotification); + + mNotificationBuilder.setContentIntent(PendingIntent.getActivity(getApplicationContext(), + (int) System.currentTimeMillis(), + showDetailsIntent, + PendingIntent.FLAG_UPDATE_CURRENT)); + mNotificationBuilder.setWhen(System.currentTimeMillis()); + mNotificationBuilder.setTicker(ticker); + mNotificationBuilder.setContentTitle(ticker); + mNotificationBuilder.setContentText(content); + + mNotificationManager.notify(R.string.media_notif_ticker, mNotificationBuilder.build()); } @@ -583,35 +584,29 @@ public class MediaService extends Service implements OnCompletionListener, OnPre * * A notification must be created to keep the user aware of the existance of the service. */ - @SuppressWarnings("deprecation") private void setUpAsForeground(String content) { + String ticker = String.format(getString(R.string.media_notif_ticker), getString(R.string.app_name)); + /// creates status notification // TODO put a progress bar to follow the playback progress - mNotification = new Notification(); - mNotification.icon = android.R.drawable.ic_media_play; + mNotificationBuilder.setSmallIcon(R.drawable.ic_play_arrow); //mNotification.tickerText = text; - mNotification.when = System.currentTimeMillis(); - mNotification.flags |= Notification.FLAG_ONGOING_EVENT; - //mNotification.contentView.setTextViewText(R.id.status_text, "ownCloud Music Player"); // NULL POINTER - //mNotification.contentView.setTextViewText(R.id.status_text, getString(R.string.downloader_download_in_progress_content)); - + mNotificationBuilder.setWhen(System.currentTimeMillis()); + mNotificationBuilder.setOngoing(true); /// includes a pending intent in the notification showing the details view of the file Intent showDetailsIntent = new Intent(this, FileDisplayActivity.class); showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, mFile); showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, mAccount); showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), - (int)System.currentTimeMillis(), - showDetailsIntent, - PendingIntent.FLAG_UPDATE_CURRENT); - - - //mNotificationManager.notify(R.string.downloader_download_in_progress_ticker, mNotification); - String ticker = String.format(getString(R.string.media_notif_ticker), getString(R.string.app_name)); - mNotification.setLatestEventInfo(getApplicationContext(), ticker, content, mNotification.contentIntent); - startForeground(R.string.media_notif_ticker, mNotification); - + mNotificationBuilder.setContentIntent(PendingIntent.getActivity(getApplicationContext(), + (int) System.currentTimeMillis(), + showDetailsIntent, + PendingIntent.FLAG_UPDATE_CURRENT)); + mNotificationBuilder.setContentTitle(ticker); + mNotificationBuilder.setContentText(content); + + startForeground(R.string.media_notif_ticker, mNotificationBuilder.build()); } /** @@ -664,6 +659,7 @@ public class MediaService extends Service implements OnCompletionListener, OnPre mState = State.STOPPED; releaseResources(true); giveUpAudioFocus(); + stopForeground(true); super.onDestroy(); }