Skip to content

Commit

Permalink
quick play widget, don't wrap news cards, minor improvements
Browse files Browse the repository at this point in the history
* restart player when error occurred, don't wrap news

* update .travis.yml

* improve playback error listener

* add quick play widget

* widget improvements
  • Loading branch information
indywidualny committed Apr 5, 2016
1 parent d47eb14 commit 03052c4
Show file tree
Hide file tree
Showing 27 changed files with 258 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android:
- platform-tools
- tools # see https://github.com/travis-ci/travis-ci/issues/5036
- android-23
- build-tools-23.0.2
- build-tools-23.0.3
- extra-google-m2repository
- extra-android-m2repository

Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "org.indywidualni.centrumfm"
minSdkVersion 15
targetSdkVersion 23
versionCode 7
versionName "0.7"
versionCode 8
versionName "0.8beta"
}
buildTypes {
release {
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
</intent-filter>
</receiver>

<service
android:name="com.google.android.gms.analytics.AnalyticsService"
android:enabled="true"
Expand Down Expand Up @@ -106,6 +107,28 @@
</intent-filter>
</receiver>

<receiver android:name=".WidgetProvider"
android:label="@string/widget_quick_play_label">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>

<receiver
android:name=".WidgetIntentReceiver"
android:label="@string/widget_broadcast_receiver"
tools:ignore="ExportedReceiver">
<intent-filter>
<action android:name="org.indywidualni.centrumfm.intent.action.QUICK_PLAY" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ public class StartupBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.i("StartupBroadcast", "Boot time or package replaced!");
final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
final String ACTION_QUICK_BOOT = "android.intent.action.QUICKBOOT_POWERON";
final String ACTION_REPLACED = "android.intent.action.PACKAGE_REPLACED";

// set all reminders
if (AlarmHelper.isEnabled())
AlarmHelper.setAllAlarms();
String action = intent.getAction();

if (ACTION_BOOT.equals(action) || ACTION_QUICK_BOOT.equals(action)
|| ACTION_REPLACED.equals(action)) {
Log.i("StartupBroadcast", "Boot time or package replaced!");

// set all reminders
if (AlarmHelper.isEnabled())
AlarmHelper.setAllAlarms();
}
}

}
38 changes: 33 additions & 5 deletions app/src/main/java/org/indywidualni/centrumfm/StreamService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import android.os.PowerManager;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.google.android.gms.analytics.HitBuilders;

import org.indywidualni.centrumfm.activity.MainActivity;
import org.indywidualni.centrumfm.util.Connectivity;

Expand All @@ -44,6 +47,7 @@ public class StreamService extends Service implements MediaPlayer.OnPreparedList
private IntentFilter intentFilter;
private NoisyReceiver noisyReceiver;
private boolean isReceiverRegistered;
private static String currentUrl;

public class LocalBinder extends Binder {
public StreamService getService() {
Expand Down Expand Up @@ -97,6 +101,20 @@ public void onCompletion(MediaPlayer mp) {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
stopPlayer();
// try to restart the player
if (currentUrl != null && Connectivity.isConnected(this)) {
Log.e("StreamService", "An error occurred, trying to restart the player");
// report this error to the tracker
((MyApplication) getApplication()).getDefaultTracker()
.send(new HitBuilders.EventBuilder()
.setCategory("Playback error " + what + " " + extra)
.setAction("Restart player")
.setLabel("error playback")
.build());
// reinitialize the player
playUrl(currentUrl);
foregroundStart();
}
return true;
}

Expand All @@ -109,7 +127,7 @@ public void onLowMemory() {
public void onDestroy() {
super.onDestroy();
stopPlayer();
Log.v("StreamService", "Service is gonna stop now");
Log.i("StreamService", "Service is gonna stop now");
}

public void onAudioFocusChange(int focusChange) {
Expand Down Expand Up @@ -169,6 +187,7 @@ private void initMediaPlayer(String url) {

try {
mMediaPlayer.prepareAsync();
updateWidget(true);
} catch (IllegalStateException e) {
e.printStackTrace();
}
Expand All @@ -181,6 +200,7 @@ public void playUrl(String url) {

switch (result) {
case AudioManager.AUDIOFOCUS_REQUEST_GRANTED:
currentUrl = url;
initMediaPlayer(url);
break;
case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
Expand All @@ -201,6 +221,7 @@ public void stopPlayer() {
audioManager.abandonAudioFocus(this);
unlockWifi();
stopForeground(true);
updateWidget(false);
}

private void pausePlayer() {
Expand Down Expand Up @@ -247,6 +268,16 @@ private void unlockWifi() {
wifiLock.release();
}

private void updateWidget(boolean isPlaying) {
RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_quick_play);
if (isPlaying)
views.setImageViewResource(R.id.button, R.drawable.ic_stop_white_48dp);
else
views.setImageViewResource(R.id.button, R.drawable.ic_play_arrow_white_48dp);
views.setOnClickPendingIntent(R.id.button, WidgetProvider.buildButtonPendingIntent(this));
WidgetProvider.pushWidgetUpdate(getApplicationContext(), views);
}

public int getCurrentPosition() {
return mMediaPlayer.getCurrentPosition();
}
Expand Down Expand Up @@ -314,10 +345,7 @@ private Notification buildNotification(boolean paused) {
.setContentText(getString(R.string.stream))
.setContentTitle(getString(R.string.toolbar_default_title));

if (Connectivity.isConnectedMobile(this))
builder.setContentInfo(getString(R.string.mobile_connection));
else
builder.setContentInfo(getString(R.string.wifi_connection));
builder.setContentInfo(getString(R.string.high_quality));

if (paused)
builder.addAction(R.drawable.ic_play_arrow_white_24dp, "play", retrievePlaybackAction(1));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.indywidualni.centrumfm;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

import org.indywidualni.centrumfm.activity.MainActivity;
import org.indywidualni.centrumfm.util.Connectivity;

public class WidgetIntentReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (WidgetProvider.QUICK_PLAY.equals(intent.getAction()))
bindStreamService(context);
}

private void bindStreamService(Context context) {
context = context.getApplicationContext();
Intent intent = new Intent(context, StreamService.class);
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
Log.d("ServiceConnectionWidget", "connected");
StreamService.LocalBinder binder = (StreamService.LocalBinder) service;
StreamService streamService = binder.getService();

Context context = MyApplication.getContextOfApplication();
Tracker tracker = ((MyApplication) context).getDefaultTracker();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

if (streamService.isPlaying()) {
streamService.stopPlayer();
} else {
if (Connectivity.isConnected(context)) {
if (Connectivity.isConnectedMobile(context)
&& !preferences.getBoolean("mobile_streaming", false)) {
Toast.makeText(context, context.getString(R.string.mobile_restriction),
Toast.LENGTH_SHORT).show();
} else {
streamService.playUrl(MainActivity.STREAM_URL);
streamService.foregroundStart();

if (Connectivity.isConnectedMobile(context)) {
tracker.send(new HitBuilders.EventBuilder()
.setCategory("Quick Play Radio")
.setAction("Play stream mobile")
.build());
} else {
tracker.send(new HitBuilders.EventBuilder()
.setCategory("Quick Play Radio")
.setAction("Play stream Wi-Fi")
.build());
}
}
} else {
Toast.makeText(context, context.getString(R.string.no_network),
Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onServiceDisconnected(ComponentName className) {
Log.d("ServiceConnectionWidget", "disconnected");
}
};

}
36 changes: 36 additions & 0 deletions app/src/main/java/org/indywidualni/centrumfm/WidgetProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.indywidualni.centrumfm;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class WidgetProvider extends AppWidgetProvider {

public static final String QUICK_PLAY = "org.indywidualni.centrumfm.intent.action.QUICK_PLAY";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_quick_play);
views.setOnClickPendingIntent(R.id.button, buildButtonPendingIntent(context));

pushWidgetUpdate(context, views);
}

public static PendingIntent buildButtonPendingIntent(Context context) {
Intent intent = new Intent();
intent.setAction(QUICK_PLAY);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, WidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class MainActivity extends AppCompatActivity
private static final String TAG = MainActivity.class.getSimpleName();
private static final String WEBSITE_URL = "http://centrum.fm";
private static final String WEBSITE_PEOPLE = "http://centrum.fm/dyzury/";
private static final String STREAM_URL = "http://5.201.13.191:80/live";
public static final String STREAM_URL = "http://5.201.13.191:80/live";
private static final String APP_PLAY_URL = "https://play.google.com/store/apps/details?id=" +
"org.indywidualni.centrumfm";
private static final int RDS_REFRESH_INTERVAL = 30000;
Expand Down Expand Up @@ -109,6 +109,7 @@ protected void onCreate(Bundle savedInstanceState) {

toolbar.setTitleTextColor(Color.WHITE);
toolbar.setSubtitleTextColor(Color.WHITE);
toolbar.setTitle(getString(R.string.toolbar_default_title));
setSupportActionBar(toolbar);

mDrawer.setNavigationItemSelectedListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
Expand All @@ -11,6 +13,7 @@
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.indywidualni.centrumfm.MyApplication;
import org.indywidualni.centrumfm.R;
import org.indywidualni.centrumfm.activity.MainActivity;
import org.indywidualni.centrumfm.rest.model.Channel;
Expand All @@ -23,6 +26,9 @@
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {

private static PrettyTime prettyTime = new PrettyTime(Locale.getDefault());
private SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
MyApplication.getContextOfApplication());

private List<Channel.Item> mDataset;
private static Context mContext;
private boolean shouldHide;
Expand Down Expand Up @@ -120,6 +126,10 @@ public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

return new ViewHolder(v, new ViewHolder.ViewHolderClicks() {
public void onExpand(TextView caller) {
// don't continue when all the news should be expanded
if (preferences.getBoolean("show_all_news", false))
return;

final RelativeLayout expandable = (RelativeLayout) v.findViewById(R.id.expandable);
if (!expandable.isShown()) {
expandable.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -181,6 +191,12 @@ public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.getPlay().setAlpha(1);
}

// don't continue when all the news should be expanded
if (preferences.getBoolean("show_all_news", false)) {
viewHolder.getExpandable().setVisibility(View.VISIBLE);
return;
}

// expand the first item
if (position == 0)
viewHolder.getExpandable().setVisibility(View.VISIBLE);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-hdpi/widget_preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/background_circle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/colorAccent" />
</shape>
Loading

0 comments on commit 03052c4

Please sign in to comment.