Improving stability and fixing minor bugs

This commit is contained in:
Vladislav Khorev 2014-11-13 13:04:23 +00:00
parent 82a9fa0b3e
commit 6f234a435c
13 changed files with 255 additions and 92 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.0 KiB

View File

@ -19,15 +19,28 @@
android:src="@drawable/transparent_button" android:src="@drawable/transparent_button"
android:contentDescription="@string/news_content_description" /> android:contentDescription="@string/news_content_description" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button <Button
android:id="@+id/button" android:id="@+id/button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/transparent_button" android:background="@drawable/transparent_button"
android:gravity="left|center_vertical" android:gravity="left|center_vertical"
android:minHeight="0dip" android:minHeight="0dip"
android:minWidth="0dip" android:minWidth="0dip"
android:text="yrrttedf"
android:textColor="@color/text_grey" /> android:textColor="@color/text_grey" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rtrrtrt" />
</LinearLayout>
</LinearLayout> </LinearLayout>

View File

@ -210,8 +210,6 @@ implements NavigationDrawerFragment.NavigationDrawerCallbacks {
return; return;
} }
super.onBackPressed(); super.onBackPressed();
@ -226,13 +224,15 @@ implements NavigationDrawerFragment.NavigationDrawerCallbacks {
@Override @Override
protected void onResume() { protected void onResume() {
newsDataSource.open(); //Xperimental -- opening and closing leads to fail
//newsDataSource.open();
super.onResume(); super.onResume();
} }
@Override @Override
protected void onPause() { protected void onPause() {
newsDataSource.close(); //Xperimental -- opening and closing leads to fail
//newsDataSource.close();
super.onPause(); super.onPause();
} }

View File

@ -7,6 +7,7 @@ import fishrungames.bashgid.core.ImageManager;
import fishrungames.bashgid.core.NewsManager; import fishrungames.bashgid.core.NewsManager;
import fishrungames.bashgid.core.NewsManager.NewsRecord; import fishrungames.bashgid.core.NewsManager.NewsRecord;
import fishrungames.bashgid.core.NewsManager.NewsUpdatedCallbackInterface; import fishrungames.bashgid.core.NewsManager.NewsUpdatedCallbackInterface;
import fishrungames.bashgid.core.db.NewsDataSource;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
@ -18,6 +19,7 @@ import android.widget.BaseAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView;
public class NewsListFragment extends Fragment implements NewsUpdatedCallbackInterface 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); Button button = (Button) view.findViewById(R.id.button);
TextView textView = (TextView) view.findViewById(R.id.textView);
ImageManager.getInstance().ApplyImageToImageView(imageButton, newsRecordArr.get(position).imageId); ImageManager.getInstance().ApplyImageToImageView(imageButton, newsRecordArr.get(position).imageId);
button.setText(newsRecordArr.get(position).title); button.setText(newsRecordArr.get(position).title);
textView.setText(NewsDataSource.iso8601Format.format(newsRecordArr.get(position).pubDate));
button.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(position))); button.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(position)));
imageButton.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(position))); imageButton.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(position)));

View File

@ -30,9 +30,6 @@ public class NewsRecordFragment extends Fragment
TextView titleTextView = (TextView) rootView.findViewById(R.id.titleTextView); TextView titleTextView = (TextView) rootView.findViewById(R.id.titleTextView);
titleTextView.setText(newsRecord.title); titleTextView.setText(newsRecord.title);
//TextView descriptionTextView = (TextView) rootView.findViewById(R.id.descriptionTextView);
//descriptionTextView.setText(newsRecord.description);
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView); ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
ImageManager.getInstance().ApplyImageToImageView(imageView, newsRecord.imageId); ImageManager.getInstance().ApplyImageToImageView(imageView, newsRecord.imageId);

View File

@ -35,9 +35,7 @@ public class ImageManager {
public ImageManager() public ImageManager()
{ {
PredefinedImageId.put("R.drawable.news_image1", R.drawable.news_image1); PredefinedImageId.put("R.drawable.transparent", R.drawable.transparent);
PredefinedImageId.put("R.drawable.news_image2", R.drawable.news_image2);
PredefinedImageId.put("R.drawable.news_image3", R.drawable.news_image3);
} }

View File

@ -1,6 +1,10 @@
package fishrungames.bashgid.core; package fishrungames.bashgid.core;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import fishrungames.bashgid.MainActivity; import fishrungames.bashgid.MainActivity;
@ -8,6 +12,9 @@ public class NewsManager {
private static NewsManager instance = null; private static NewsManager instance = null;
//Xperimental -- need do something with this. Change to semaphore?
public static volatile boolean SynchronizationInProcess = false;
public static NewsManager GetInstance() public static NewsManager GetInstance()
{ {
@ -25,12 +32,14 @@ public class NewsManager {
public String title; public String title;
public String description; public String description;
public String imageId; 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.title = title;
this.description = description; this.description = description;
this.imageId = imageId; this.imageId = imageId;
this.pubDate = pubDate;
} }
NewsRecord(RssReader.Rss2Item rss2Item) NewsRecord(RssReader.Rss2Item rss2Item)
@ -46,6 +55,8 @@ public class NewsManager {
{ {
this.imageId = "R.drawable.transparent"; this.imageId = "R.drawable.transparent";
} }
this.pubDate = rss2Item.pubDate;
} }
} }
@ -82,79 +93,34 @@ public class NewsManager {
@Override @Override
public void run() public void run()
{ {
//String url = "http://www.bashinform.ru/rss/all.xml"; if (SynchronizationInProcess)
String url = "http://www.bashedu.ru/rss.xml";
String xmlCode = DownloadManager.getXmlFromUrl(url);
if (xmlCode == null)
{ {
return; return;
} }
//Remove BOM character if present SynchronizationInProcess = true;
if (xmlCode.startsWith("\uFEFF"))
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<NewsRecord> localNewsRecord = MainActivity.getInstance().newsDataSource.getNews();
ArrayList<String> imageToDownloadList = new ArrayList<String>();
for (int i = 0; i < urlArr.length; i++)
{ {
xmlCode = xmlCode.substring(1);
}
LoadNewsAndImagesFromRss2(urlArr[i], localNewsRecord, imageToDownloadList);
Collections.sort(localNewsRecord, new NewsSortComparator());
RssReader rssReader = new RssReader(); //NewsSortComparator
rssReader.ReadRss(xmlCode);
ArrayList<NewsRecord> localNewsRecord = new ArrayList<NewsRecord>();
for (RssReader.Rss2Item rss2Item : rssReader.rss2ItemArr)
{
//Enclosure
if (rss2Item.rss2Enclosure != null)
{
if (DownloadManager.MimeTypeIsImage(rss2Item.rss2Enclosure.mimeType))
{
ImageManager.getInstance().DownloadImageIfNeeded(rss2Item.rss2Enclosure.url);
}
}
//And images inside text
int i = rss2Item.description.indexOf("<img");
while (i != -1)
{
int j = rss2Item.description.indexOf("src=\"", i);
if (j != -1)
{
j += ("src=\"").length();
int k = rss2Item.description.indexOf("\"", j);
String imageUrl = rss2Item.description.substring(j, k);
String fullImageUrl = imageUrl;
if (!fullImageUrl.startsWith("http://"))
{
String homeFolder = DownloadManager.GetUrlFolder(url);
fullImageUrl = homeFolder + fullImageUrl;
}
ImageManager.getInstance().DownloadImageIfNeeded(fullImageUrl);
String fileName = ImageManager.getInstance().GetImageLocalUrl(fullImageUrl);
rss2Item.description = rss2Item.description.replace("\"" +imageUrl + "\"" , "\"" +fileName + "\"");
}
i = rss2Item.description.indexOf("<img", j);
}
}
for (RssReader.Rss2Item rss2Item : rssReader.rss2ItemArr)
{
localNewsRecord.add(new NewsRecord(rss2Item));
}
//Xperimental -- need mutex right here //Xperimental -- need mutex right here
newsRecord = localNewsRecord; newsRecord = localNewsRecord;
@ -166,6 +132,15 @@ public class NewsManager {
{ {
callback.OnNewsUpdated(); callback.OnNewsUpdated();
} }
}
for (String imageToDownload : imageToDownloadList)
{
ImageManager.getInstance().DownloadImageIfNeeded(imageToDownload);
}
SynchronizationInProcess = false;
} }
} }
@ -193,6 +168,153 @@ public class NewsManager {
} }
public static ArrayList<String> ExtractEnclosureImageUrl(ArrayList<RssReader.Rss2Item> rss2ItemArr)
{
ArrayList<String> imageToDownloadList = new ArrayList<String>();
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<String> ExtractDescriptionImageUrl(ArrayList<RssReader.Rss2Item> rss2ItemArr)
{
ArrayList<String> imageToDownloadList = new ArrayList<String>();
for (RssReader.Rss2Item rss2Item : rss2ItemArr)
{
//And images inside text
int i = rss2Item.description.indexOf("<img");
while (i != -1)
{
int j = rss2Item.description.indexOf("src=\"", i);
if (j != -1)
{
j += ("src=\"").length();
int k = rss2Item.description.indexOf("\"", j);
String imageUrl = rss2Item.description.substring(j, k);
imageToDownloadList.add(imageUrl);
}
i = rss2Item.description.indexOf("<img", j);
}
}
return imageToDownloadList;
}
public static ArrayList<String> ConvertRelativeToAbsoluteImageUrlList(ArrayList<String> relativeImageUrlList, String url)
{
ArrayList<String> absoluteImageUrlList = new ArrayList<String>();
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<RssReader.Rss2Item> rss2ItemArr, ArrayList<String> relativeImageUrlList, ArrayList<String> 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<NewsRecord> newsRecordArr, ArrayList<String> 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<String> relativeImageUrlList = ExtractDescriptionImageUrl(rssReader.rss2ItemArr);
ArrayList<String> 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<NewsRecord> {
public int compare(NewsRecord newsRecord1, NewsRecord newsRecord2) {
return newsRecord2.pubDate.compareTo(newsRecord1.pubDate);
}
}
} }

View File

@ -118,6 +118,12 @@ public class RssReader {
String enclosureUrl = XmlProcessor.getAttributeValue(e, "enclosure", "url"); String enclosureUrl = XmlProcessor.getAttributeValue(e, "enclosure", "url");
String enclosureLengthStr = XmlProcessor.getAttributeValue(e, "enclosure", "length"); String enclosureLengthStr = XmlProcessor.getAttributeValue(e, "enclosure", "length");
if (enclosureLengthStr.equals(""))
{
enclosureLengthStr = "0";
}
int enclosureLength = Integer.valueOf(enclosureLengthStr); int enclosureLength = Integer.valueOf(enclosureLengthStr);
String enclosureMimeType = XmlProcessor.getAttributeValue(e, "enclosure", "type"); String enclosureMimeType = XmlProcessor.getAttributeValue(e, "enclosure", "type");

View File

@ -13,6 +13,8 @@ public class BashgidSqliteHelper extends SQLiteOpenHelper
public static final String COLUMN_TITLE = "title"; public static final String COLUMN_TITLE = "title";
public static final String COLUMN_DESCRIPTION = "description"; public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_IMAGE_ID = "imageId"; public static final String COLUMN_IMAGE_ID = "imageId";
public static final String COLUMN_PUBDATE = "pubDate";
private static final String DATABASE_NAME = "bashgid.db"; private static final String DATABASE_NAME = "bashgid.db";
@ -25,6 +27,7 @@ public class BashgidSqliteHelper extends SQLiteOpenHelper
", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_TITLE + " text not null" +
", " + COLUMN_DESCRIPTION + " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" +
", " + COLUMN_IMAGE_ID + " text not null" + ", " + COLUMN_IMAGE_ID + " text not null" +
", " + COLUMN_PUBDATE + " text not null" +
");"; ");";
public BashgidSqliteHelper(Context context) { public BashgidSqliteHelper(Context context) {

View File

@ -1,6 +1,9 @@
package fishrungames.bashgid.core.db; package fishrungames.bashgid.core.db;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale;
import fishrungames.bashgid.core.NewsManager; import fishrungames.bashgid.core.NewsManager;
@ -13,12 +16,15 @@ import android.database.sqlite.SQLiteDatabase;
public class NewsDataSource 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 SQLiteDatabase database;
private BashgidSqliteHelper dbHelper; private BashgidSqliteHelper dbHelper;
private String[] allColumns = { BashgidSqliteHelper.COLUMN_ID, BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION, 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) { public NewsDataSource(Context context) {
dbHelper = new BashgidSqliteHelper(context); dbHelper = new BashgidSqliteHelper(context);
@ -42,6 +48,7 @@ public class NewsDataSource
values.put(BashgidSqliteHelper.COLUMN_TITLE, newsRecord.title); values.put(BashgidSqliteHelper.COLUMN_TITLE, newsRecord.title);
values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, newsRecord.description); values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, newsRecord.description);
values.put(BashgidSqliteHelper.COLUMN_IMAGE_ID, newsRecord.imageId); values.put(BashgidSqliteHelper.COLUMN_IMAGE_ID, newsRecord.imageId);
values.put(BashgidSqliteHelper.COLUMN_PUBDATE, iso8601Format.format(newsRecord.pubDate));
database.insert(BashgidSqliteHelper.TABLE_NEWS, null, values); database.insert(BashgidSqliteHelper.TABLE_NEWS, null, values);
@ -81,7 +88,15 @@ public class NewsDataSource
private NewsManager.NewsRecord cursorToNewsRecord(Cursor cursor) 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; return newsRecord;
} }

View File

@ -1,9 +1,12 @@
package fishrungames.bashgid.core.db; package fishrungames.bashgid.core.db;
import java.util.Date;
public class NewsDbRecord public class NewsDbRecord
{ {
public long id; public long id;
public String title; public String title;
public String description; public String description;
public String imageId; public String imageId;
public Date pubDate;
} }