Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for proxies #356

Merged
merged 2 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import net.programmierecke.radiodroid2.data.DataRadioStation;

import okhttp3.OkHttpClient;

public class ActivityRadioStationDetail extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {
private DataRadioStation itsStation;
private MenuItem m_Menu_Star;
Expand Down Expand Up @@ -48,10 +50,13 @@ protected void onCreate(Bundle savedInstanceState) {
UpdateMenu();

getApplicationContext().sendBroadcast(new Intent(ActivityMain.ACTION_SHOW_LOADING));

final OkHttpClient httpClient = radioDroidApp.getHttpClient();

new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
return Utils.downloadFeed(getApplicationContext(), RadioBrowserServerManager.getWebserviceEndpoint(getApplicationContext(), String.format(Locale.US, "json/stations/byid/%s", aStationID)),true,null);
return Utils.downloadFeed(httpClient, getApplicationContext(), RadioBrowserServerManager.getWebserviceEndpoint(getApplicationContext(), String.format(Locale.US, "json/stations/byid/%s", aStationID)),true,null);
}

@Override
Expand Down Expand Up @@ -100,13 +105,17 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_play:
Utils.Play(itsStation,this,false);
case R.id.action_play: {
RadioDroidApp radioDroidApp = (RadioDroidApp) getApplication();
Utils.Play(radioDroidApp.getHttpClient(), itsStation, this, false);
return true;
}

case R.id.action_share:
Utils.Play(itsStation,this,true);
case R.id.action_share: {
RadioDroidApp radioDroidApp = (RadioDroidApp) getApplication();
Utils.Play(radioDroidApp.getHttpClient(), itsStation, this, true);
return true;
}

case R.id.action_star:
Star();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import net.programmierecke.radiodroid2.data.DataRadioStation;

import okhttp3.OkHttpClient;

public class AlarmReceiver extends BroadcastReceiver {
String url;
int alarmId;
Expand Down Expand Up @@ -118,12 +120,15 @@ public void onServiceDisconnected(ComponentName className) {
int timeout = 10;

private void Play(final Context context, final String stationId) {
RadioDroidApp radioDroidApp = (RadioDroidApp) context.getApplicationContext();
final OkHttpClient httpClient = radioDroidApp.getHttpClient();

new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String result = null;
for (int i=0;i<20;i++){
result = Utils.getRealStationLink(context, stationId);
result = Utils.getRealStationLink(httpClient, context, stationId);
if (result != null){
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import java.util.HashMap;

import okhttp3.OkHttpClient;

public class FragmentBase extends Fragment {
private static final String TAG = "FragmentBase";

Expand Down Expand Up @@ -77,14 +79,18 @@ public void DownloadUrl(final boolean forceUpdate, final boolean displayProgress
if (getContext() != null && displayProgress) {
getContext().sendBroadcast(new Intent(ActivityMain.ACTION_SHOW_LOADING));
}

RadioDroidApp radioDroidApp = (RadioDroidApp) getActivity().getApplication();
final OkHttpClient httpClient = radioDroidApp.getHttpClient();

new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
HashMap<String, String> p = new HashMap<String, String>();
if (!show_broken) {
p.put("hidebroken", "true");
}
return Utils.downloadFeed(getActivity(), url, forceUpdate, p);
return Utils.downloadFeed(httpClient, getActivity(), url, forceUpdate, p);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class FragmentHistory extends Fragment {
void onStationClick(DataRadioStation theStation) {
Context context = getContext();

Utils.Play(theStation, context);
RadioDroidApp radioDroidApp = (RadioDroidApp) getActivity().getApplication();
Utils.Play(radioDroidApp.getHttpClient(), theStation, context);

historyManager.add(theStation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ private void SetInfoFromHistory(boolean startPlaying) {

if(history.length > 0) {
DataRadioStation lastStation = history[0];
if(startPlaying)
Utils.Play(lastStation, getContext());
else {
if(startPlaying) {
Utils.Play(radioDroidApp.getHttpClient(),lastStation, getContext());
} else {
aTextViewName.setText(lastStation.Name);

if (!Utils.shouldLoadIcons(getContext()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import net.programmierecke.radiodroid2.data.DataStatistics;
import net.programmierecke.radiodroid2.interfaces.IFragmentRefreshable;

import okhttp3.OkHttpClient;

public class FragmentServerInfo extends Fragment implements IFragmentRefreshable {
private ItemAdapterStatistics itemAdapterStatistics;

Expand All @@ -38,12 +40,16 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

void Download(final boolean forceUpdate){
getContext().sendBroadcast(new Intent(ActivityMain.ACTION_SHOW_LOADING));

RadioDroidApp radioDroidApp = (RadioDroidApp) getActivity().getApplication();
final OkHttpClient httpClient = radioDroidApp.getHttpClient();

new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String endpoint = RadioBrowserServerManager.getWebserviceEndpoint(getActivity(),"json/stats");
if (endpoint != null) {
return Utils.downloadFeed(getActivity(), endpoint, forceUpdate, null);
return Utils.downloadFeed(httpClient, getActivity(), endpoint, forceUpdate, null);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import net.programmierecke.radiodroid2.data.MPDServer;
import net.programmierecke.radiodroid2.interfaces.IApplicationSelected;
import net.programmierecke.radiodroid2.proxy.ProxySettingsDialog;

import java.lang.ref.WeakReference;
import java.net.InetAddress;
Expand Down Expand Up @@ -66,6 +67,17 @@ public boolean onPreferenceClick(Preference preference) {
return false;
}
});

findPreference("settings_proxy").setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
ProxySettingsDialog proxySettingsDialog = new ProxySettingsDialog();
proxySettingsDialog.setCancelable(true);
proxySettingsDialog.show(getFragmentManager(), "");
return false;
}
});

findPreference("mpd_servers_viewer").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public class FragmentStarred extends Fragment implements IAdapterRefreshable, IC
private HistoryManager historyManager;

void onStationClick(DataRadioStation theStation) {
Utils.Play(theStation, getContext());
RadioDroidApp radioDroidApp = (RadioDroidApp) getActivity().getApplication();

Utils.Play(radioDroidApp.getHttpClient(), theStation, getContext());

historyManager.add(theStation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public class FragmentStations extends FragmentBase {

void onStationClick(DataRadioStation theStation) {
Context context = getContext();
Utils.Play(theStation, context);

RadioDroidApp radioDroidApp = (RadioDroidApp) getActivity().getApplication();
Utils.Play(radioDroidApp.getHttpClient(), theStation, context);

historyManager.add(theStation);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,63 @@
package net.programmierecke.radiodroid2;

import android.app.Application;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.preference.PreferenceManager;

import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;

import net.programmierecke.radiodroid2.proxy.ProxySettings;
import net.programmierecke.radiodroid2.recording.RecordingsManager;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.ConnectionPool;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RadioDroidApp extends Application {

private HistoryManager historyManager;
private FavouriteManager favouriteManager;
private RecordingsManager recordingsManager;

private ConnectionPool connectionPool;
private OkHttpClient httpClient;

public class UserAgentInterceptor implements Interceptor {

private final String userAgent;

public UserAgentInterceptor(String userAgent) {
this.userAgent = userAgent;
}

@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
Request requestWithUserAgent = originalRequest.newBuilder()
.header("User-Agent", userAgent)
.build();
return chain.proceed(requestWithUserAgent);
}
}

@Override
public void onCreate() {
super.onCreate();

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

connectionPool = new ConnectionPool();

rebuildHttpClient();

Picasso.Builder builder = new Picasso.Builder(this);
builder.downloader(new OkHttp3Downloader(this, Integer.MAX_VALUE));
Picasso picassoInstance = builder.build();
Expand All @@ -33,6 +71,15 @@ public void onCreate() {
recordingsManager = new RecordingsManager();
}

public void rebuildHttpClient() {
httpClient = newHttpClient()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.addInterceptor(new UserAgentInterceptor("RadioDroid2/" + BuildConfig.VERSION_NAME))
.build();
}

public HistoryManager getHistoryManager() {
return historyManager;
}
Expand All @@ -44,4 +91,22 @@ public FavouriteManager getFavouriteManager() {
public RecordingsManager getRecordingsManager() {
return recordingsManager;
}

public OkHttpClient getHttpClient() {
return httpClient;
}

public OkHttpClient.Builder newHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder().connectionPool(connectionPool);
setCurrentOkHttpProxy(builder);
return builder;
}

public void setCurrentOkHttpProxy(@NonNull OkHttpClient.Builder builder) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
ProxySettings proxySettings = ProxySettings.fromPreferences(sharedPref);
if (proxySettings != null) {
Utils.setOkHttpProxy(builder, proxySettings);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.List;
import java.util.Vector;

import okhttp3.OkHttpClient;

public class StationSaveManager {
Context context;
List<DataRadioStation> listStations = new ArrayList<DataRadioStation>();
Expand Down Expand Up @@ -206,14 +208,17 @@ protected void onPostExecute(DataRadioStation[] result) {
protected final String M3U_PREFIX = "#RADIOBROWSERUUID:";

boolean SaveM3UInternal(String filePath, String fileName){
final RadioDroidApp radioDroidApp = (RadioDroidApp) context.getApplicationContext();
final OkHttpClient httpClient = radioDroidApp.getHttpClient();

try {
File f = new File(filePath, fileName);
BufferedWriter bw = new BufferedWriter(new FileWriter(f, false));
bw.write("#EXTM3U\n");
for (DataRadioStation station : listStations) {
String result = null;
for (int i=0;i<20;i++){
result = Utils.getRealStationLink(context, station.ID);
result = Utils.getRealStationLink(httpClient, context, station.ID);
if (result != null){
break;
}
Expand Down Expand Up @@ -256,11 +261,14 @@ DataRadioStation[] LoadM3UInternal(String filePath, String fileName){
BufferedReader br = new BufferedReader(new FileReader(f));
String line;

final RadioDroidApp radioDroidApp = (RadioDroidApp) context.getApplicationContext();
final OkHttpClient httpClient = radioDroidApp.getHttpClient();

while ((line = br.readLine()) != null) {
if (line.startsWith(M3U_PREFIX)){
try {
String uuid = line.substring(M3U_PREFIX.length()).trim();
DataRadioStation station = Utils.getStationByUuid(context, uuid);
DataRadioStation station = Utils.getStationByUuid(httpClient, context, uuid);
if (station != null) {
loadedItems.add(station);
}
Expand Down
Loading