|
1 | 1 | package ru.aleien.yapplication;
|
2 | 2 |
|
3 | 3 | import android.app.NotificationManager;
|
| 4 | +import android.app.PendingIntent; |
4 | 5 | import android.content.BroadcastReceiver;
|
5 | 6 | import android.content.Context;
|
6 | 7 | import android.content.Intent;
|
|
22 | 23 | import android.view.MenuItem;
|
23 | 24 | import android.widget.Toast;
|
24 | 25 |
|
| 26 | +import ru.aleien.yapplication.utils.IntentBuilder; |
25 | 27 | import ru.aleien.yapplication.utils.Utils;
|
26 | 28 |
|
27 | 29 | public class ListArtistsActivity extends AppCompatActivity implements MainView {
|
@@ -127,25 +129,37 @@ public void onBackPressed() {
|
127 | 129 | getSupportActionBar().setDisplayHomeAsUpEnabled(false);
|
128 | 130 | }
|
129 | 131 |
|
| 132 | + // TODO: Вынести в отдельный класс |
| 133 | + // TODO: При открытой странице инфо об артисте, открывать страницу артиста |
130 | 134 | private void showHeadphonesNotification(boolean wiredHeadsetOn) {
|
131 |
| - String headphonesMessage = wiredHeadsetOn ? "Headphones are plugged in" : "Headphones are unplugged"; |
132 |
| - Toast.makeText(ListArtistsActivity.this, headphonesMessage, Toast.LENGTH_LONG).show(); |
| 135 | + Intent musicIntent = IntentBuilder.buildOpenAppOrMarketPageIntent("ru.yandex.music", this); |
| 136 | + Intent radioIntent = IntentBuilder.buildOpenAppOrMarketPageIntent("ru.yandex.radio",this); |
133 | 137 |
|
134 |
| - NotificationCompat.Builder mBuilder = |
135 |
| - new NotificationCompat.Builder(this) |
136 |
| - .setSmallIcon(R.drawable.ic_stat_hardware_headset) |
137 |
| - .setContentTitle("My notification") |
138 |
| - .setContentText("Hello World!"); |
| 138 | + PendingIntent musicPendingIntent = PendingIntent.getActivity(this, 1010, musicIntent, 0); |
| 139 | + PendingIntent radioPendingIntent = PendingIntent.getActivity(this, 1020, radioIntent, 0); |
139 | 140 |
|
140 | 141 | int musicNotificationId = 001;
|
141 |
| - int radioNotificationId = 002; |
142 | 142 |
|
143 |
| - NotificationManager mNotifyMgr = |
144 |
| - (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
| 143 | + if (wiredHeadsetOn) { |
| 144 | + NotificationCompat.Builder musicNotificationBuilder = |
| 145 | + new NotificationCompat.Builder(this) |
| 146 | + .setSmallIcon(R.drawable.ic_stat_hardware_headset) |
| 147 | + .setColor(ContextCompat.getColor(this, R.color.colorPrimary)) |
| 148 | + .setContentTitle("Headphones plugged in") |
| 149 | + .addAction(R.drawable.ic_stat_yamusic, "Ya.Music", musicPendingIntent) |
| 150 | + .addAction(R.drawable.ic_stat_hardware_headset, "Ya.Radio", radioPendingIntent) |
| 151 | + .setContentText("Open in:"); |
145 | 152 |
|
146 |
| - mNotifyMgr.notify(musicNotificationId, mBuilder.build()); |
147 |
| - mNotifyMgr.notify(radioNotificationId, mBuilder.build()); |
| 153 | + NotificationManager mNotifyMgr = |
| 154 | + (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
148 | 155 |
|
| 156 | + mNotifyMgr.notify(musicNotificationId, musicNotificationBuilder.build()); |
| 157 | + } else { |
| 158 | + NotificationManager mNotifyMgr = |
| 159 | + (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
| 160 | + |
| 161 | + mNotifyMgr.cancel(musicNotificationId); |
| 162 | + } |
149 | 163 |
|
150 | 164 | }
|
151 | 165 |
|
|
0 commit comments