package com.owncloud.android.media;
import android.accounts.Account;
+import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
+import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
+import android.net.Uri;
import android.net.wifi.WifiManager;
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;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.ui.activity.FileActivity;
import com.owncloud.android.ui.activity.FileDisplayActivity;
/** 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;
/** 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,
return context.getString(messageId);
}
+ public static AlertDialog.Builder streamWithExternalApp(final String uri, final Activity activity){
+ AlertDialog.Builder builder = new AlertDialog.Builder(activity);
+ 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
+ }
+ });
+ return builder;
+ }
+
/**
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);
}
mState = State.PLAYING;
setUpAsForeground(String.format(getString(R.string.media_state_playing), mFile.getFileName()));
configAndStartMediaPlayer();
-
}
}
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;
-
- } else if (mAccount == null) {
+ if (mAccount == null) {
Toast.makeText(this, R.string.media_err_not_in_owncloud, Toast.LENGTH_LONG).show();
processStopRequest(true);
return;
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();
}
mIsStreaming = url.startsWith("http:") || url.startsWith("https:");
- */
+
mIsStreaming = false;
mPlayer.setDataSource(url);
Log_OC.e(TAG, "IllegalArgumentException " + mAccount.name + mFile.getRemotePath(), e);
Toast.makeText(this, String.format(getString(R.string.media_err_unexpected), mFile.getFileName()), Toast.LENGTH_LONG).show();
processStopRequest(true);
+ } catch (AccountUtils.AccountNotFoundException e) {
+ e.printStackTrace();
}
}
/**
* 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());
}
*
* 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());
}
/**
mState = State.STOPPED;
releaseResources(true);
giveUpAudioFocus();
+ stopForeground(true);
super.onDestroy();
}