Skip to content

Commit

Permalink
Merge pull request #552 from morckx/auto-upgrade-ids-to-uuids
Browse files Browse the repository at this point in the history
Automatically add missing uuids while loading station lists
  • Loading branch information
segler-alex authored Jul 30, 2019
2 parents 203a81f + 9fc7f33 commit fa0bf7f
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ public boolean has(String id){
return station != null;
}

public boolean hasInvalidUuids() {
for (DataRadioStation station : listStations) {
if (!station.hasValidUuid()) {
return true;
}
}

return false;
}

public List<DataRadioStation> getList() {
return Collections.unmodifiableList(listStations);
}
Expand All @@ -153,6 +163,36 @@ public void setChangedListener(IChanged handler){
this.changedHandler = handler;
}

void refreshStationsFromServer() {
final RadioDroidApp radioDroidApp = (RadioDroidApp) context.getApplicationContext();
final OkHttpClient httpClient = radioDroidApp.getHttpClient();
context.sendBroadcast(new Intent(ActivityMain.ACTION_SHOW_LOADING));

new AsyncTask<Void, Void, Integer>() {
@Override
protected Integer doInBackground(Void... params) {
int deletedCount = 0;
for (DataRadioStation station : listStations) {
if (!station.refresh(httpClient, context) && !station.hasValidUuid() && station.RefreshRetryCount > DataRadioStation.MAX_REFRESH_RETRIES) {
listStations.remove(station);
deletedCount++;
}
}
return deletedCount;
}

@Override
protected void onPostExecute(Integer result) {
Save();
if (changedHandler != null){
changedHandler.onChanged();
}
context.sendBroadcast(new Intent(ActivityMain.ACTION_HIDE_LOADING));
super.onPostExecute(result);
}
}.execute();
}

void Load() {
listStations.clear();

Expand All @@ -161,6 +201,9 @@ void Load() {
if (str != null) {
DataRadioStation[] arr = DataRadioStation.DecodeJson(str);
Collections.addAll(listStations, arr);
if (hasInvalidUuids() && Utils.hasAnyConnection(context)) {
refreshStationsFromServer();
}
} else {
Log.w("SAVE", "Load() no stations to load");
}
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/net/programmierecke/radiodroid2/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ public static String getRealStationLink(OkHttpClient httpClient, Context ctx, St
return null;
}

@Deprecated
public static DataRadioStation getStationById(OkHttpClient httpClient, Context ctx, String stationId) {
Log.w("UTIL", "Search by id:" + stationId);
String result = Utils.downloadFeed(httpClient, ctx, RadioBrowserServerManager.getWebserviceEndpoint(ctx, "json/stations/byid/" + stationId), true, null);
if (result != null) {
try {
DataRadioStation[] list = DataRadioStation.DecodeJson(result);
if (list != null) {
if (list.length == 1) {
return list[0];
}
Log.e("UTIL", "stations by id did have length:" + list.length);
}
} catch (Exception e) {
Log.e("UTIL", "getStationByid() " + e);
}
}
return null;
}

public static DataRadioStation getStationByUuid(OkHttpClient httpClient, Context ctx, String stationUuid){
Log.w("UTIL","Search by uuid:"+stationUuid);
String result = Utils.downloadFeed(httpClient, ctx, RadioBrowserServerManager.getWebserviceEndpoint(ctx, "json/stations/byuuid/" + stationUuid), true, null);
Expand Down Expand Up @@ -246,6 +266,9 @@ private static void playInternal(final OkHttpClient httpClient, final DataRadioS
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
if (!station.hasValidUuid()) {
station.refresh(httpClient, context);
}
return Utils.getRealStationLink(httpClient, context.getApplicationContext(), station.StationUuid);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import jp.wasabeef.picasso.transformations.CropCircleTransformation;
import jp.wasabeef.picasso.transformations.CropSquareTransformation;
import jp.wasabeef.picasso.transformations.RoundedCornersTransformation;
import okhttp3.OkHttpClient;

import static net.programmierecke.radiodroid2.Utils.resourceToUri;

public class DataRadioStation {
static final String TAG = "DATAStation";
public static final int MAX_REFRESH_RETRIES = 16;

public DataRadioStation() {
}
Expand All @@ -51,11 +53,15 @@ public DataRadioStation() {
public int ClickCount;
public int ClickTrend;
public int Votes;
public int RefreshRetryCount;
public int Bitrate;
public String Codec;
public boolean Working = true;
public boolean Hls = false;

@Deprecated
public String StationId = "";

public String getShortDetails(Context ctx) {
List<String> aList = new ArrayList<String>();
if (!Working){
Expand Down Expand Up @@ -94,10 +100,18 @@ public static DataRadioStation[] DecodeJson(String result) {
if (anObject.has("stationuuid")) {
aStation.StationUuid = anObject.getString("stationuuid");
}
if (!aStation.hasValidUuid()) {
aStation.StationId = anObject.getString("id");
}
if (anObject.has("changeuuid")) {
aStation.ChangeUuid = anObject.getString("changeuuid");
}
aStation.Votes = anObject.getInt("votes");
if (anObject.has("refreshretrycount")) {
aStation.RefreshRetryCount = anObject.getInt("refreshretrycount");
} else {
aStation.RefreshRetryCount = 0;
}
aStation.HomePageUrl = anObject.getString("homepage");
aStation.TagsAll = anObject.getString("tags");
aStation.Country = anObject.getString("country");
Expand Down Expand Up @@ -150,10 +164,18 @@ public static DataRadioStation DecodeJsonSingle(String result) {
if (anObject.has("stationuuid")) {
aStation.StationUuid = anObject.getString("stationuuid");
}
if (!aStation.hasValidUuid()) {
aStation.StationId = anObject.getString("id");
}
if (anObject.has("changeuuid")) {
aStation.ChangeUuid = anObject.getString("changeuuid");
}
aStation.Votes = anObject.getInt("votes");
if (anObject.has("refreshretrycount")) {
aStation.RefreshRetryCount = anObject.getInt("refreshretrycount");
} else {
aStation.RefreshRetryCount = 0;
}
aStation.HomePageUrl = anObject.getString("homepage");
aStation.TagsAll = anObject.getString("tags");
aStation.Country = anObject.getString("country");
Expand Down Expand Up @@ -186,7 +208,11 @@ public static DataRadioStation DecodeJsonSingle(String result) {
public JSONObject toJson(){
JSONObject obj = new JSONObject();
try {
obj.put("stationuuid",StationUuid);
if (TextUtils.isEmpty(StationUuid)) {
obj.put("id", StationId);
} else {
obj.put("stationuuid", StationUuid);
}
obj.put("changeuuid",ChangeUuid);
obj.put("name",Name);
obj.put("homepage",HomePageUrl);
Expand All @@ -198,6 +224,9 @@ public JSONObject toJson(){
obj.put("language",Language);
obj.put("clickcount",ClickCount);
obj.put("clicktrend",ClickTrend);
if (RefreshRetryCount > 0) {
obj.put("refreshretrycount", RefreshRetryCount);
}
obj.put("votes",Votes);
obj.put("bitrate",""+Bitrate);
obj.put("codec",Codec);
Expand All @@ -210,6 +239,45 @@ public JSONObject toJson(){
return null;
}

public boolean refresh(final OkHttpClient httpClient, final Context context) {
boolean success = false;
DataRadioStation refreshedStation = (!TextUtils.isEmpty(StationUuid) ? Utils.getStationByUuid(httpClient, context, StationUuid) : Utils.getStationById(httpClient, context, StationId));

if (refreshedStation != null && refreshedStation.hasValidUuid()) {
copyPropertiesFrom(refreshedStation);
RefreshRetryCount = 0;
success = true;
} else if (Utils.hasAnyConnection(context)) {
RefreshRetryCount++;
}
return success;
}

public boolean hasValidUuid() {
return !TextUtils.isEmpty(StationUuid);
}

public void copyPropertiesFrom(DataRadioStation station) {
StationUuid = station.StationUuid;
StationId = station.StationId;
ChangeUuid = station.ChangeUuid;
Name = station.Name;
HomePageUrl = station.HomePageUrl;
StreamUrl = station.StreamUrl;
IconUrl = station.IconUrl;
Country = station.Country;
State = station.State;
TagsAll = station.TagsAll;
Language = station.Language;
ClickCount = station.ClickCount;
ClickTrend = station.ClickTrend;
Votes = station.Votes;
RefreshRetryCount = station.RefreshRetryCount;
Bitrate = station.Bitrate;
Codec = station.Codec;
Working = station.Working;
}

public interface ShortcutReadyListener {
void onShortcutReadyListener(ShortcutInfo shortcutInfo);
}
Expand Down

0 comments on commit fa0bf7f

Please sign in to comment.