diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-04-01 23:57:00 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-04-01 23:57:00 +0200 |
commit | ca16b910e14f269d4def0d6c57f966b981bf8759 (patch) | |
tree | 9fce5b3b1ee55457b46cded902f8ba946053cf8e /src/main/java | |
parent | d423752b17cb9b30d5c7606689b863a2b4153f89 (diff) |
audio player now requests permission when hitting play
Diffstat (limited to '')
4 files changed, 43 insertions, 0 deletions
diff --git a/src/main/java/de/pixart/messenger/services/AudioPlayer.java b/src/main/java/de/pixart/messenger/services/AudioPlayer.java index e6d1e7c08..4993935b5 100644 --- a/src/main/java/de/pixart/messenger/services/AudioPlayer.java +++ b/src/main/java/de/pixart/messenger/services/AudioPlayer.java @@ -1,9 +1,12 @@ package de.pixart.messenger.services; +import android.Manifest; +import android.content.pm.PackageManager; import android.content.res.ColorStateList; 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; @@ -17,7 +20,9 @@ import java.util.Locale; import de.pixart.messenger.Config; import de.pixart.messenger.R; import de.pixart.messenger.entities.Message; +import de.pixart.messenger.ui.ConversationActivity; import de.pixart.messenger.ui.adapter.MessageAdapter; +import de.pixart.messenger.ui.util.PendingItem; import de.pixart.messenger.utils.WeakReferenceSet; public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompletionListener, SeekBar.OnSeekBarChangeListener, Runnable { @@ -29,6 +34,8 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti private final MessageAdapter messageAdapter; private final WeakReferenceSet<RelativeLayout> audioPlayerLayouts = new WeakReferenceSet<>(); + private final PendingItem<WeakReference<ImageButton>> pendingOnClickView = new PendingItem<>(); + private final Handler handler = new Handler(); public AudioPlayer(MessageAdapter adapter) { @@ -99,6 +106,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(); @@ -145,6 +157,16 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti } } + public void startStopPending() { + WeakReference<ImageButton> 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); diff --git a/src/main/java/de/pixart/messenger/ui/ConversationActivity.java b/src/main/java/de/pixart/messenger/ui/ConversationActivity.java index 53f3b0a14..adfbde7c7 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationActivity.java @@ -99,6 +99,7 @@ public class ConversationActivity extends XmppActivity implements OnConversation public static final String EXTRA_IS_PRIVATE_MESSAGE = "pm"; public static final String ACTION_DESTROY_MUC = "de.pixart.messenger.DESTROY_MUC"; public static final int REQUEST_OPEN_MESSAGE = 0x9876; + public static final int REQUEST_PLAY_PAUSE = 0x5432; private boolean showLastSeen = false; @@ -350,6 +351,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/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index ef2910ab3..9425a4eb5 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -478,6 +478,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/de/pixart/messenger/ui/adapter/MessageAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java index 37dd32b4c..9f09531e1 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -1,6 +1,7 @@ package de.pixart.messenger.ui.adapter; import android.Manifest; +import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.content.SharedPreferences; @@ -71,6 +72,7 @@ import de.pixart.messenger.persistance.FileBackend; import de.pixart.messenger.services.AudioPlayer; import de.pixart.messenger.services.MessageArchiveService; import de.pixart.messenger.services.NotificationService; +import de.pixart.messenger.ui.ConversationActivity; import de.pixart.messenger.ui.ConversationFragment; import de.pixart.messenger.ui.ShowFullscreenMessageActivity; import de.pixart.messenger.ui.XmppActivity; @@ -197,6 +199,10 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie this.mOnContactPictureLongClickedListener = listener; } + public Activity getActivity() { + return activity; + } + public void setOnQuoteListener(OnQuoteListener listener) { this.onQuoteListener = listener; } @@ -1058,6 +1064,10 @@ public class MessageAdapter extends ArrayAdapter<Message> 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); |