diff --git a/android/res/drawable/news_image1.jpg b/android/res/drawable/news_image1.jpg
deleted file mode 100644
index 7d10cf2..0000000
Binary files a/android/res/drawable/news_image1.jpg and /dev/null differ
diff --git a/android/res/drawable/news_image2.jpg b/android/res/drawable/news_image2.jpg
deleted file mode 100644
index 8489f93..0000000
Binary files a/android/res/drawable/news_image2.jpg and /dev/null differ
diff --git a/android/res/drawable/news_image3.jpg b/android/res/drawable/news_image3.jpg
deleted file mode 100644
index f803625..0000000
Binary files a/android/res/drawable/news_image3.jpg and /dev/null differ
diff --git a/android/res/layout/news_page_list_element.xml b/android/res/layout/news_page_list_element.xml
index 7d50f41..690cb70 100644
--- a/android/res/layout/news_page_list_element.xml
+++ b/android/res/layout/news_page_list_element.xml
@@ -19,15 +19,28 @@
android:src="@drawable/transparent_button"
android:contentDescription="@string/news_content_description" />
-
+ android:orientation="vertical" >
+
+
+
+
+
+
diff --git a/android/src/fishrungames/bashgid/MainActivity.java b/android/src/fishrungames/bashgid/MainActivity.java
index 03851da..7fc723b 100644
--- a/android/src/fishrungames/bashgid/MainActivity.java
+++ b/android/src/fishrungames/bashgid/MainActivity.java
@@ -210,8 +210,6 @@ implements NavigationDrawerFragment.NavigationDrawerCallbacks {
return;
}
-
-
super.onBackPressed();
@@ -226,13 +224,15 @@ implements NavigationDrawerFragment.NavigationDrawerCallbacks {
@Override
protected void onResume() {
- newsDataSource.open();
+ //Xperimental -- opening and closing leads to fail
+ //newsDataSource.open();
super.onResume();
}
@Override
protected void onPause() {
- newsDataSource.close();
+ //Xperimental -- opening and closing leads to fail
+ //newsDataSource.close();
super.onPause();
}
diff --git a/android/src/fishrungames/bashgid/NewsListFragment.java b/android/src/fishrungames/bashgid/NewsListFragment.java
index e74018c..a258b8b 100644
--- a/android/src/fishrungames/bashgid/NewsListFragment.java
+++ b/android/src/fishrungames/bashgid/NewsListFragment.java
@@ -7,6 +7,7 @@ import fishrungames.bashgid.core.ImageManager;
import fishrungames.bashgid.core.NewsManager;
import fishrungames.bashgid.core.NewsManager.NewsRecord;
import fishrungames.bashgid.core.NewsManager.NewsUpdatedCallbackInterface;
+import fishrungames.bashgid.core.db.NewsDataSource;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
@@ -18,6 +19,7 @@ import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
+import android.widget.TextView;
public class NewsListFragment extends Fragment implements NewsUpdatedCallbackInterface
@@ -102,10 +104,14 @@ public class NewsListFragment extends Fragment implements NewsUpdatedCallbackInt
Button button = (Button) view.findViewById(R.id.button);
+ TextView textView = (TextView) view.findViewById(R.id.textView);
+
ImageManager.getInstance().ApplyImageToImageView(imageButton, newsRecordArr.get(position).imageId);
button.setText(newsRecordArr.get(position).title);
+ textView.setText(NewsDataSource.iso8601Format.format(newsRecordArr.get(position).pubDate));
+
button.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(position)));
imageButton.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(position)));
diff --git a/android/src/fishrungames/bashgid/NewsRecordFragment.java b/android/src/fishrungames/bashgid/NewsRecordFragment.java
index 5cd81d5..91a27f2 100644
--- a/android/src/fishrungames/bashgid/NewsRecordFragment.java
+++ b/android/src/fishrungames/bashgid/NewsRecordFragment.java
@@ -30,9 +30,6 @@ public class NewsRecordFragment extends Fragment
TextView titleTextView = (TextView) rootView.findViewById(R.id.titleTextView);
titleTextView.setText(newsRecord.title);
- //TextView descriptionTextView = (TextView) rootView.findViewById(R.id.descriptionTextView);
- //descriptionTextView.setText(newsRecord.description);
-
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
ImageManager.getInstance().ApplyImageToImageView(imageView, newsRecord.imageId);
diff --git a/android/src/fishrungames/bashgid/core/ImageManager.java b/android/src/fishrungames/bashgid/core/ImageManager.java
index 3bb3240..29075ed 100644
--- a/android/src/fishrungames/bashgid/core/ImageManager.java
+++ b/android/src/fishrungames/bashgid/core/ImageManager.java
@@ -35,9 +35,7 @@ public class ImageManager {
public ImageManager()
{
- PredefinedImageId.put("R.drawable.news_image1", R.drawable.news_image1);
- PredefinedImageId.put("R.drawable.news_image2", R.drawable.news_image2);
- PredefinedImageId.put("R.drawable.news_image3", R.drawable.news_image3);
+ PredefinedImageId.put("R.drawable.transparent", R.drawable.transparent);
}
diff --git a/android/src/fishrungames/bashgid/core/NewsManager.java b/android/src/fishrungames/bashgid/core/NewsManager.java
index 390fbab..10c1804 100644
--- a/android/src/fishrungames/bashgid/core/NewsManager.java
+++ b/android/src/fishrungames/bashgid/core/NewsManager.java
@@ -1,6 +1,10 @@
package fishrungames.bashgid.core;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+
import fishrungames.bashgid.MainActivity;
@@ -8,6 +12,9 @@ public class NewsManager {
private static NewsManager instance = null;
+ //Xperimental -- need do something with this. Change to semaphore?
+ public static volatile boolean SynchronizationInProcess = false;
+
public static NewsManager GetInstance()
{
@@ -25,12 +32,14 @@ public class NewsManager {
public String title;
public String description;
public String imageId;
+ public Date pubDate;
- public NewsRecord(String title, String description, String imageId)
+ public NewsRecord(String title, String description, String imageId, Date pubDate)
{
this.title = title;
this.description = description;
this.imageId = imageId;
+ this.pubDate = pubDate;
}
NewsRecord(RssReader.Rss2Item rss2Item)
@@ -46,6 +55,8 @@ public class NewsManager {
{
this.imageId = "R.drawable.transparent";
}
+
+ this.pubDate = rss2Item.pubDate;
}
}
@@ -82,90 +93,54 @@ public class NewsManager {
@Override
public void run()
{
- //String url = "http://www.bashinform.ru/rss/all.xml";
-
- String url = "http://www.bashedu.ru/rss.xml";
-
- String xmlCode = DownloadManager.getXmlFromUrl(url);
-
- if (xmlCode == null)
+ if (SynchronizationInProcess)
{
return;
}
- //Remove BOM character if present
- if (xmlCode.startsWith("\uFEFF"))
+ SynchronizationInProcess = true;
+
+ final String [] urlArr = {
+ "https://www.bashkortostan.ru/presscenter/news/rss/",
+ "http://www.bashedu.ru/rss.xml",
+ "http://www.bashinform.ru/rss/all.xml",
+ "http://www.minkult-rb.ru/news/rss/",
+ "http://02.mvd.ru/news/rss/"
+
+ };
+
+ ArrayList localNewsRecord = MainActivity.getInstance().newsDataSource.getNews();
+
+ ArrayList imageToDownloadList = new ArrayList();
+
+ for (int i = 0; i < urlArr.length; i++)
{
- xmlCode = xmlCode.substring(1);
- }
-
-
- RssReader rssReader = new RssReader();
-
- rssReader.ReadRss(xmlCode);
-
- ArrayList localNewsRecord = new ArrayList();
-
- for (RssReader.Rss2Item rss2Item : rssReader.rss2ItemArr)
- {
- //Enclosure
- if (rss2Item.rss2Enclosure != null)
- {
- if (DownloadManager.MimeTypeIsImage(rss2Item.rss2Enclosure.mimeType))
- {
- ImageManager.getInstance().DownloadImageIfNeeded(rss2Item.rss2Enclosure.url);
- }
- }
+ LoadNewsAndImagesFromRss2(urlArr[i], localNewsRecord, imageToDownloadList);
- //And images inside text
- int i = rss2Item.description.indexOf(" ExtractEnclosureImageUrl(ArrayList rss2ItemArr)
+ {
+ ArrayList imageToDownloadList = new ArrayList();
+
+ for (RssReader.Rss2Item rss2Item : rss2ItemArr)
+ {
+ //Enclosure
+ if (rss2Item.rss2Enclosure != null)
+ {
+ if (DownloadManager.MimeTypeIsImage(rss2Item.rss2Enclosure.mimeType))
+ {
+ imageToDownloadList.add(rss2Item.rss2Enclosure.url);
+ }
+ }
+ }
+
+ return imageToDownloadList;
+ }
+ public static ArrayList ExtractDescriptionImageUrl(ArrayList rss2ItemArr)
+ {
+ ArrayList imageToDownloadList = new ArrayList();
+
+ for (RssReader.Rss2Item rss2Item : rss2ItemArr)
+ {
+ //And images inside text
+ int i = rss2Item.description.indexOf(" ConvertRelativeToAbsoluteImageUrlList(ArrayList relativeImageUrlList, String url)
+ {
+ ArrayList absoluteImageUrlList = new ArrayList();
+
+ for (String relativeImageUrl : relativeImageUrlList)
+ {
+ if (!relativeImageUrl.startsWith("http://"))
+ {
+ String homeFolder = DownloadManager.GetUrlFolder(url);
+
+ relativeImageUrl = homeFolder + relativeImageUrl;
+ }
+
+ absoluteImageUrlList.add(relativeImageUrl);
+ }
+
+ return absoluteImageUrlList;
+ }
+
+
+ static void ReplaceRemoteUrlToLocalUrl(ArrayList rss2ItemArr, ArrayList relativeImageUrlList, ArrayList absoluteImageUrlList)
+ {
+
+ for (RssReader.Rss2Item rss2Item : rss2ItemArr)
+ {
+ for (int i = 0; i < relativeImageUrlList.size(); i++)
+ {
+ String fileName = ImageManager.getInstance().GetImageLocalUrl(absoluteImageUrlList.get(i));
+
+ rss2Item.description = rss2Item.description.replace("\"" +relativeImageUrlList.get(i) + "\"" , "\"" +fileName + "\"");
+ }
+ }
+ }
+
+ static void LoadNewsAndImagesFromRss2(String url, ArrayList newsRecordArr, ArrayList imageUrlArr)
+ {
+ //Xperimental -- need to optimize this code. slow block BEGINS
+ String xmlCode = DownloadManager.getXmlFromUrl(url);
+
+ if (xmlCode == null)
+ {
+ return;
+ }
+
+ //Remove BOM character if present
+ if (xmlCode.startsWith("\uFEFF"))
+ {
+ xmlCode = xmlCode.substring(1);
+ }
+
+
+
+ RssReader rssReader = new RssReader();
+
+ rssReader.ReadRss(xmlCode);
+
+
+ //Xperimental -- need to optimize this code. slow block ENDS
+
+ imageUrlArr.addAll(ExtractEnclosureImageUrl(rssReader.rss2ItemArr));
+
+
+ ArrayList relativeImageUrlList = ExtractDescriptionImageUrl(rssReader.rss2ItemArr);
+
+ ArrayList absoluteImageUrlList = ConvertRelativeToAbsoluteImageUrlList(relativeImageUrlList, url);
+
+ imageUrlArr.addAll(absoluteImageUrlList);
+
+ //Xperimental -- this function is very slow, need to speed up
+ ReplaceRemoteUrlToLocalUrl(rssReader.rss2ItemArr, relativeImageUrlList, absoluteImageUrlList);
+
+ for (RssReader.Rss2Item rss2Item : rssReader.rss2ItemArr)
+ {
+ boolean exists = false;
+
+ for (NewsRecord newsRecord : newsRecordArr)
+ {
+ if (newsRecord.title.equals(rss2Item.title))
+ {
+ exists = true;
+ break;
+ }
+ }
+
+ if (!exists)
+ {
+ newsRecordArr.add(new NewsRecord(rss2Item));
+ }
+ }
+
+ }
+
+ public static class NewsSortComparator implements Comparator {
+ public int compare(NewsRecord newsRecord1, NewsRecord newsRecord2) {
+ return newsRecord2.pubDate.compareTo(newsRecord1.pubDate);
+ }
+ }
+
}
diff --git a/android/src/fishrungames/bashgid/core/RssReader.java b/android/src/fishrungames/bashgid/core/RssReader.java
index 789baff..56c0090 100644
--- a/android/src/fishrungames/bashgid/core/RssReader.java
+++ b/android/src/fishrungames/bashgid/core/RssReader.java
@@ -118,6 +118,12 @@ public class RssReader {
String enclosureUrl = XmlProcessor.getAttributeValue(e, "enclosure", "url");
String enclosureLengthStr = XmlProcessor.getAttributeValue(e, "enclosure", "length");
+
+ if (enclosureLengthStr.equals(""))
+ {
+ enclosureLengthStr = "0";
+ }
+
int enclosureLength = Integer.valueOf(enclosureLengthStr);
String enclosureMimeType = XmlProcessor.getAttributeValue(e, "enclosure", "type");
diff --git a/android/src/fishrungames/bashgid/core/db/BashgidSqliteHelper.java b/android/src/fishrungames/bashgid/core/db/BashgidSqliteHelper.java
index 5137997..1fc157e 100644
--- a/android/src/fishrungames/bashgid/core/db/BashgidSqliteHelper.java
+++ b/android/src/fishrungames/bashgid/core/db/BashgidSqliteHelper.java
@@ -13,6 +13,8 @@ public class BashgidSqliteHelper extends SQLiteOpenHelper
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_IMAGE_ID = "imageId";
+ public static final String COLUMN_PUBDATE = "pubDate";
+
private static final String DATABASE_NAME = "bashgid.db";
@@ -25,6 +27,7 @@ public class BashgidSqliteHelper extends SQLiteOpenHelper
", " + COLUMN_TITLE + " text not null" +
", " + COLUMN_DESCRIPTION + " text not null" +
", " + COLUMN_IMAGE_ID + " text not null" +
+ ", " + COLUMN_PUBDATE + " text not null" +
");";
public BashgidSqliteHelper(Context context) {
diff --git a/android/src/fishrungames/bashgid/core/db/NewsDataSource.java b/android/src/fishrungames/bashgid/core/db/NewsDataSource.java
index c7b6168..7c6a997 100644
--- a/android/src/fishrungames/bashgid/core/db/NewsDataSource.java
+++ b/android/src/fishrungames/bashgid/core/db/NewsDataSource.java
@@ -1,6 +1,9 @@
package fishrungames.bashgid.core.db;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
import java.util.ArrayList;
+import java.util.Locale;
import fishrungames.bashgid.core.NewsManager;
@@ -12,13 +15,16 @@ import android.database.sqlite.SQLiteDatabase;
public class NewsDataSource
{
+
+ //Xperimental -- move somewhere
+ public static final SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
private SQLiteDatabase database;
private BashgidSqliteHelper dbHelper;
private String[] allColumns = { BashgidSqliteHelper.COLUMN_ID, BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION,
- BashgidSqliteHelper.COLUMN_IMAGE_ID };
+ BashgidSqliteHelper.COLUMN_IMAGE_ID, BashgidSqliteHelper.COLUMN_PUBDATE };
public NewsDataSource(Context context) {
dbHelper = new BashgidSqliteHelper(context);
@@ -42,7 +48,8 @@ public class NewsDataSource
values.put(BashgidSqliteHelper.COLUMN_TITLE, newsRecord.title);
values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, newsRecord.description);
values.put(BashgidSqliteHelper.COLUMN_IMAGE_ID, newsRecord.imageId);
-
+ values.put(BashgidSqliteHelper.COLUMN_PUBDATE, iso8601Format.format(newsRecord.pubDate));
+
database.insert(BashgidSqliteHelper.TABLE_NEWS, null, values);
}
@@ -81,7 +88,15 @@ public class NewsDataSource
private NewsManager.NewsRecord cursorToNewsRecord(Cursor cursor)
{
- NewsManager.NewsRecord newsRecord = new NewsManager.NewsRecord(cursor.getString(1), cursor.getString(2), cursor.getString(3));
+ NewsManager.NewsRecord newsRecord = null;
+ try
+ {
+ newsRecord = new NewsManager.NewsRecord(cursor.getString(1), cursor.getString(2), cursor.getString(3), iso8601Format.parse(cursor.getString(4)));
+ }
+ catch (ParseException e)
+ {
+ e.printStackTrace();
+ }
return newsRecord;
}
diff --git a/android/src/fishrungames/bashgid/core/db/NewsDbRecord.java b/android/src/fishrungames/bashgid/core/db/NewsDbRecord.java
index 3b6b6aa..f5719e6 100644
--- a/android/src/fishrungames/bashgid/core/db/NewsDbRecord.java
+++ b/android/src/fishrungames/bashgid/core/db/NewsDbRecord.java
@@ -1,9 +1,12 @@
package fishrungames.bashgid.core.db;
+import java.util.Date;
+
public class NewsDbRecord
{
public long id;
public String title;
public String description;
public String imageId;
+ public Date pubDate;
}