properly download feed icons, properly adjust feedsfragment on
preference change
This commit is contained in:
parent
989a904f3d
commit
3bc2ad56e9
@ -54,5 +54,5 @@
|
|||||||
<string name="enable_ads">Enable ads</string>
|
<string name="enable_ads">Enable ads</string>
|
||||||
<string name="enable_ads_summary">Showing ads to you supports the project</string>
|
<string name="enable_ads_summary">Showing ads to you supports the project</string>
|
||||||
<string name="ttrss_url_summary">URL of your tt-rss installation directory, e.g. http://site.com/tt-rss/</string>
|
<string name="ttrss_url_summary">URL of your tt-rss installation directory, e.g. http://site.com/tt-rss/</string>
|
||||||
|
<string name="download_feed_icons">Download and display feed icons</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
android:entryValues="@array/pref_theme_values" android:summary="@string/pref_theme_long"/>
|
android:entryValues="@array/pref_theme_values" android:summary="@string/pref_theme_long"/>
|
||||||
|
|
||||||
<CheckBoxPreference android:title="@string/sort_feeds_by_unread" android:key="sort_feeds_by_unread"/>
|
<CheckBoxPreference android:title="@string/sort_feeds_by_unread" android:key="sort_feeds_by_unread"/>
|
||||||
|
<CheckBoxPreference android:title="@string/download_feed_icons" android:key="download_feed_icons"/>
|
||||||
<CheckBoxPreference android:defaultValue="false" android:summary="@string/enable_ads_summary" android:title="@string/enable_ads" android:key="enable_ads" />
|
<CheckBoxPreference android:defaultValue="false" android:summary="@string/enable_ads_summary" android:title="@string/enable_ads" android:key="enable_ads" />
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
|
|||||||
InputStream content = execute.getEntity().getContent();
|
InputStream content = execute.getEntity().getContent();
|
||||||
|
|
||||||
BufferedReader buffer = new BufferedReader(
|
BufferedReader buffer = new BufferedReader(
|
||||||
new InputStreamReader(content));
|
new InputStreamReader(content), 8192);
|
||||||
|
|
||||||
String s = "";
|
String s = "";
|
||||||
String response = "";
|
String response = "";
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
package org.fox.ttrss;
|
package org.fox.ttrss;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -11,9 +15,25 @@ import java.util.Comparator;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.auth.AuthScope;
|
||||||
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.conn.scheme.PlainSocketFactory;
|
||||||
|
import org.apache.http.conn.scheme.Scheme;
|
||||||
|
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
|
||||||
|
import org.apache.http.params.BasicHttpParams;
|
||||||
|
import org.apache.http.params.HttpParams;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
@ -36,9 +56,10 @@ import com.google.gson.Gson;
|
|||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
public class FeedsFragment extends Fragment implements OnItemClickListener {
|
public class FeedsFragment extends Fragment implements OnItemClickListener, OnSharedPreferenceChangeListener {
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private final String TAG = this.getClass().getSimpleName();
|
private final String TAG = this.getClass().getSimpleName();
|
||||||
private SharedPreferences m_prefs;
|
private SharedPreferences m_prefs;
|
||||||
@ -46,7 +67,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
|
|||||||
private FeedList m_feeds = new FeedList();
|
private FeedList m_feeds = new FeedList();
|
||||||
private OnFeedSelectedListener m_feedSelectedListener;
|
private OnFeedSelectedListener m_feedSelectedListener;
|
||||||
private int m_selectedFeedId;
|
private int m_selectedFeedId;
|
||||||
private static final String ICON_PATH = "/org.fox.ttrss-icons/";
|
private static final String ICON_PATH = "/org.fox.ttrss/icons/";
|
||||||
private boolean m_enableFeedIcons;
|
private boolean m_enableFeedIcons;
|
||||||
|
|
||||||
public interface OnFeedSelectedListener {
|
public interface OnFeedSelectedListener {
|
||||||
@ -93,7 +114,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
|
|||||||
list.setAdapter(m_adapter);
|
list.setAdapter(m_adapter);
|
||||||
list.setOnItemClickListener(this);
|
list.setOnItemClickListener(this);
|
||||||
|
|
||||||
m_enableFeedIcons = m_prefs.getBoolean("enable_feed_icons", false);
|
m_enableFeedIcons = m_prefs.getBoolean("download_feed_icons", false);
|
||||||
|
|
||||||
if (m_feeds == null || m_feeds.size() == 0)
|
if (m_feeds == null || m_feeds.size() == 0)
|
||||||
refresh(false);
|
refresh(false);
|
||||||
@ -113,6 +134,8 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
|
|||||||
super.onAttach(activity);
|
super.onAttach(activity);
|
||||||
|
|
||||||
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
|
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
|
||||||
|
m_prefs.registerOnSharedPreferenceChangeListener(this);
|
||||||
|
|
||||||
m_feedSelectedListener = (OnFeedSelectedListener) activity;
|
m_feedSelectedListener = (OnFeedSelectedListener) activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -404,36 +427,17 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
|
|||||||
try {
|
try {
|
||||||
File storage = Environment.getExternalStorageDirectory();
|
File storage = Environment.getExternalStorageDirectory();
|
||||||
final File iconPath = new File(storage.getAbsolutePath() + ICON_PATH);
|
final File iconPath = new File(storage.getAbsolutePath() + ICON_PATH);
|
||||||
if (!iconPath.exists()) iconPath.mkdir();
|
if (!iconPath.exists()) iconPath.mkdirs();
|
||||||
|
|
||||||
final FeedList feeds = params[0];
|
|
||||||
|
|
||||||
if (iconPath.exists()) {
|
if (iconPath.exists()) {
|
||||||
for (Feed feed : feeds) {
|
for (Feed feed : params[0]) {
|
||||||
if (feed.id > 0 && feed.has_icon) {
|
if (feed.id > 0 && feed.has_icon) {
|
||||||
File iconFile = new File(iconPath.getAbsolutePath() + "/" + feed.id + ".ico");
|
File outputFile = new File(iconPath.getAbsolutePath() + "/" + feed.id + ".ico");
|
||||||
String fetchUrl = m_baseUrl + "/" + feed.id + ".ico";
|
String fetchUrl = m_baseUrl + "/" + feed.id + ".ico";
|
||||||
|
|
||||||
if (!iconFile.exists()) {
|
if (!outputFile.exists()) {
|
||||||
//Log.d(TAG, "Downloading " + fetchUrl);
|
downloadFile(fetchUrl, outputFile.getAbsolutePath());
|
||||||
|
Thread.sleep(2000);
|
||||||
try {
|
|
||||||
BufferedInputStream is = new BufferedInputStream(new URL(fetchUrl).openStream(), 1024);
|
|
||||||
FileOutputStream fos = new FileOutputStream(iconFile);
|
|
||||||
|
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int len = 0;
|
|
||||||
while ((len = is.read(buffer)) != -1) {
|
|
||||||
fos.write(buffer, 0, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
fos.close();
|
|
||||||
is.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.d(TAG, "Error downloading " + fetchUrl);
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -445,9 +449,79 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void downloadFile(String fetchUrl, String outputFile) {
|
||||||
|
DefaultHttpClient client;
|
||||||
|
|
||||||
|
if (m_prefs.getBoolean("ssl_trust_any", false)) {
|
||||||
|
SchemeRegistry schemeRegistry = new SchemeRegistry();
|
||||||
|
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
|
||||||
|
schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
|
||||||
|
|
||||||
|
HttpParams httpParams = new BasicHttpParams();
|
||||||
|
|
||||||
|
client = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams);
|
||||||
|
} else {
|
||||||
|
client = new DefaultHttpClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpGet httpGet = new HttpGet(fetchUrl);
|
||||||
|
|
||||||
|
String httpLogin = m_prefs.getString("http_login", "");
|
||||||
|
String httpPassword = m_prefs.getString("http_password", "");
|
||||||
|
|
||||||
|
if (httpLogin.length() > 0) {
|
||||||
|
|
||||||
|
URL targetUrl;
|
||||||
|
try {
|
||||||
|
targetUrl = new URL(fetchUrl);
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpHost targetHost = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol());
|
||||||
|
|
||||||
|
client.getCredentialsProvider().setCredentials(
|
||||||
|
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
|
||||||
|
new UsernamePasswordCredentials(httpLogin, httpPassword));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
HttpResponse execute = client.execute(httpGet);
|
||||||
|
|
||||||
|
InputStream content = execute.getEntity().getContent();
|
||||||
|
|
||||||
|
BufferedInputStream is = new BufferedInputStream(content, 1024);
|
||||||
|
FileOutputStream fos = new FileOutputStream(outputFile);
|
||||||
|
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int len = 0;
|
||||||
|
while ((len = is.read(buffer)) != -1) {
|
||||||
|
fos.write(buffer, 0, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
fos.close();
|
||||||
|
is.close();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
protected void onPostExecute(Integer result) {
|
protected void onPostExecute(Integer result) {
|
||||||
m_adapter.notifyDataSetInvalidated();
|
m_adapter.notifyDataSetInvalidated();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
|
||||||
|
String key) {
|
||||||
|
|
||||||
|
sortFeeds();
|
||||||
|
m_enableFeedIcons = m_prefs.getBoolean("download_feed_icons", false);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -262,12 +262,6 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
|
|||||||
Intent refresh = new Intent(this, MainActivity.class);
|
Intent refresh = new Intent(this, MainActivity.class);
|
||||||
startActivity(refresh);
|
startActivity(refresh);
|
||||||
finish();
|
finish();
|
||||||
} else {
|
|
||||||
FeedsFragment frag = (FeedsFragment)getSupportFragmentManager().findFragmentById(R.id.feeds_fragment);
|
|
||||||
|
|
||||||
if (frag != null) {
|
|
||||||
frag.sortFeeds();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user