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:contentDescription="@string/news_content_description" />
<Button
android:id="@+id/button"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/transparent_button"
android:gravity="left|center_vertical"
android:minHeight="0dip"
android:minWidth="0dip"
android:textColor="@color/text_grey" />
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/transparent_button"
android:gravity="left|center_vertical"
android:minHeight="0dip"
android:minWidth="0dip"
android:text="yrrttedf"
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>

View File

@ -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();
}

View File

@ -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)));

View File

@ -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);

View File

@ -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);
}

View File

@ -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<NewsRecord> localNewsRecord = MainActivity.getInstance().newsDataSource.getNews();
ArrayList<String> imageToDownloadList = new ArrayList<String>();
for (int i = 0; i < urlArr.length; i++)
{
xmlCode = xmlCode.substring(1);
}
RssReader rssReader = new RssReader();
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);
}
}
LoadNewsAndImagesFromRss2(urlArr[i], localNewsRecord, imageToDownloadList);
//And images inside text
int i = rss2Item.description.indexOf("<img");
while (i != -1)
Collections.sort(localNewsRecord, new NewsSortComparator());
//NewsSortComparator
//Xperimental -- need mutex right here
newsRecord = localNewsRecord;
MainActivity.getInstance().newsDataSource.replaceNews(newsRecord);
//Xperimental -- need mutex here, too
if (callback != null)
{
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);
callback.OnNewsUpdated();
}
}
for (RssReader.Rss2Item rss2Item : rssReader.rss2ItemArr)
for (String imageToDownload : imageToDownloadList)
{
localNewsRecord.add(new NewsRecord(rss2Item));
ImageManager.getInstance().DownloadImageIfNeeded(imageToDownload);
}
//Xperimental -- need mutex right here
newsRecord = localNewsRecord;
MainActivity.getInstance().newsDataSource.replaceNews(newsRecord);
//Xperimental -- need mutex here, too
if (callback != null)
{
callback.OnNewsUpdated();
}
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 enclosureLengthStr = XmlProcessor.getAttributeValue(e, "enclosure", "length");
if (enclosureLengthStr.equals(""))
{
enclosureLengthStr = "0";
}
int enclosureLength = Integer.valueOf(enclosureLengthStr);
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_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) {

View File

@ -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;
}

View File

@ -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;
}