From 0add9a0ea22ff7e2d9f35b06a76553bcb35e87c3 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 5 Mar 2018 17:19:56 +0100 Subject: [PATCH] audio player now requests permission when hitting play --- .../ui/ConversationActivity.java | 4 ++++ .../ui/ConversationFragment.java | 7 ++++++ .../ui/adapter/MessageAdapter.java | 9 ++++++++ .../conversations/ui/service/AudioPlayer.java | 23 +++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index 304aff6b2..c09acfa5d 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -85,6 +85,7 @@ public class ConversationActivity extends XmppActivity implements OnConversation public static final String EXTRA_IS_PRIVATE_MESSAGE = "pm"; public static final int REQUEST_OPEN_MESSAGE = 0x9876; + public static final int REQUEST_PLAY_PAUSE = 0x5432; //secondary fragment (when holding the conversation, must be initialized before refreshing the overview fragment @@ -291,6 +292,9 @@ public class ConversationActivity extends XmppActivity implements OnConversation refreshUiReal(); ConversationFragment.openPendingMessage(this); break; + case REQUEST_PLAY_PAUSE: + ConversationFragment.startStopPending(this); + break; } } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index e1af7d56f..865a9d042 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -424,6 +424,13 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke return null; } + public static void startStopPending(Activity activity) { + ConversationFragment fragment = findConversationFragment(activity); + if (fragment != null) { + fragment.messageListAdapter.startStopPending(); + } + } + public static void downloadFile(Activity activity, Message message) { ConversationFragment fragment = findConversationFragment(activity); if (fragment != null) { diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index 1561d2eaa..fa6029ea5 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -1,6 +1,7 @@ package eu.siacs.conversations.ui.adapter; import android.Manifest; +import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.content.SharedPreferences; @@ -182,6 +183,10 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie this.mOnContactPictureClickedListener = listener; } + public Activity getActivity() { + return activity; + } + public void setOnContactPictureLongClicked( OnContactPictureLongClicked listener) { this.mOnContactPictureLongClickedListener = listener; @@ -903,6 +908,10 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie audioPlayer.stop(); } + public void startStopPending() { + audioPlayer.startStopPending(); + } + public void openDownloadable(Message message) { if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ConversationFragment.registerPendingMessage(activity, message); diff --git a/src/main/java/eu/siacs/conversations/ui/service/AudioPlayer.java b/src/main/java/eu/siacs/conversations/ui/service/AudioPlayer.java index 8e9d5a8d6..699e042ba 100644 --- a/src/main/java/eu/siacs/conversations/ui/service/AudioPlayer.java +++ b/src/main/java/eu/siacs/conversations/ui/service/AudioPlayer.java @@ -1,9 +1,13 @@ package eu.siacs.conversations.ui.service; +import android.Manifest; +import android.content.pm.PackageManager; import android.content.res.ColorStateList; +import android.media.Image; import android.media.MediaPlayer; import android.os.Build; import android.os.Handler; +import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.view.View; import android.widget.ImageButton; @@ -16,7 +20,9 @@ import java.util.Locale; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Message; +import eu.siacs.conversations.ui.ConversationActivity; import eu.siacs.conversations.ui.adapter.MessageAdapter; +import eu.siacs.conversations.ui.util.PendingItem; import eu.siacs.conversations.utils.WeakReferenceSet; public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompletionListener, SeekBar.OnSeekBarChangeListener, Runnable { @@ -28,6 +34,8 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti private final MessageAdapter messageAdapter; private final WeakReferenceSet audioPlayerLayouts = new WeakReferenceSet<>(); + private final PendingItem> pendingOnClickView = new PendingItem<>(); + private final Handler handler = new Handler(); public AudioPlayer(MessageAdapter adapter) { @@ -97,6 +105,11 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti } private void startStop(ImageButton playPause) { + if (ContextCompat.checkSelfPermission(messageAdapter.getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + pendingOnClickView.push(new WeakReference<>(playPause)); + ActivityCompat.requestPermissions(messageAdapter.getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, ConversationActivity.REQUEST_PLAY_PAUSE); + return; + } final RelativeLayout audioPlayer = (RelativeLayout) playPause.getParent(); final ViewHolder viewHolder = ViewHolder.get(audioPlayer); final Message message = (Message) audioPlayer.getTag(); @@ -143,6 +156,16 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti } } + public void startStopPending() { + WeakReference reference = pendingOnClickView.pop(); + if (reference != null) { + ImageButton imageButton = reference.get(); + if (imageButton != null) { + startStop(imageButton); + } + } + } + private boolean startStop(ViewHolder viewHolder, Message message) { if (message == currentlyPlayingMessage && player != null) { return playPauseCurrent(viewHolder);