html code and images in rss

This commit is contained in:
Vladislav Khorev 2014-11-13 08:17:13 +00:00
parent f50fd1ac61
commit 82a9fa0b3e
12 changed files with 249 additions and 85 deletions

View File

@ -1,16 +1,12 @@
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:orientation="vertical"
android:background="@color/bkg1"
tools:context="fishrungames.bashgid.MainPageFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -26,13 +22,11 @@
android:src="@drawable/transparent"
android:contentDescription="@string/news_content_description" />
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>

View File

@ -24,19 +24,25 @@
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp" >
<ImageView
android:id="@+id/news1ImageView"
<ImageButton
android:id="@+id/news1ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/transparent"
android:contentDescription="@string/news_content_description" />
<TextView
android:id="@+id/news1TextView"
<Button
android:id="@+id/news1Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp" />
android:layout_marginLeft="8dp"
android:background="@drawable/transparent_button"
android:gravity="left|center_vertical"
android:minHeight="0dp"
android:minWidth="0dp"
android:textColor="@color/text_grey" />
</LinearLayout>
@ -46,19 +52,25 @@
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp" >
<ImageView
android:id="@+id/news2ImageView"
<ImageButton
android:id="@+id/news2ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/transparent"
android:contentDescription="@string/news_content_description" />
<TextView
android:id="@+id/news2TextView"
<Button
android:id="@+id/news2Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp" />
android:layout_marginLeft="8dp"
android:background="@drawable/transparent_button"
android:gravity="left|center_vertical"
android:minHeight="0dp"
android:minWidth="0dp"
android:textColor="@color/text_grey" />
</LinearLayout>
@ -68,19 +80,25 @@
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp" >
<ImageView
android:id="@+id/news3ImageView"
<ImageButton
android:id="@+id/news3ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/transparent"
android:contentDescription="@string/news_content_description" />
<TextView
android:id="@+id/news3TextView"
<Button
android:id="@+id/news3Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp" />
android:layout_marginLeft="8dp"
android:background="@drawable/transparent_button"
android:gravity="left|center_vertical"
android:minHeight="0dp"
android:minWidth="0dp"
android:textColor="@color/text_grey" />
</LinearLayout>

View File

@ -1,29 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">巴什科尔托斯坦指南</string>
<string name="navigation_drawer_open">打开导航窗</string>
<string name="navigation_drawer_close">关闭导航窗</string>
<resources>
<string name="app_name">俄罗斯联邦巴什基尔自治共和国指南</string>
<string name="navigation_drawer_open">打开导航窗</string>
<string name="navigation_drawer_close">关闭导航窗</string>
<string name="action_settings">设置</string>
<string name="search_title">搜索</string>
<string name="search_title">搜索标题</string>
<string name="news_record_not_available">文章不可</string>
<string name="news_record_not_available">无法查看新闻记录</string>
<string name="other_news">其他新闻…</string>
<string name="see_all">查看更多新闻</string>
<string name="see_all">查看所有新闻</string>
<string name="news_header">新闻</string>
<string name="news_header">头条新闻</string>
<string name="news_content_description">图片相关的新闻</string>
<string name="news_content_description">新闻内容描述</string>
<string name="related_image">图片相关文章</string>
<string name="related_image">相关图片</string>
<string name="icon">图标</string>
<string name="RB_Coat"></string>
<string name="RB_Coat"></string>
<string name="drawer_header">巴什科尔托斯坦指南</string>
<string name="drawer_header">俄罗斯联邦巴什基尔自治共和国指南</string>
<string name="drawer_guide">指南</string>
<string name="drawer_news">新闻</string>
<string name="drawer_catalogue">目录</string>
@ -33,6 +33,6 @@
<string name="drawer_for_investor">投资者信息</string>
<string name="drawer_settings">设置</string>
</resources>
</resources>

View File

@ -23,6 +23,9 @@ public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
public static final String TAG_FROM_MAINFRAGMENT_TO_NEWSRECORDFRAGMENT = "TAG_FROM_MAINFRAGMENT_TO_NEWSRECORDFRAGMENT";
public static final String TAG_FROM_NEWSLISTFRAGMENT_TO_NEWSRECORDFRAGMENT = "TAG_FROM_NEWSLISTFRAGMENT_TO_NEWSRECORDFRAGMENT";
static MainActivity instance = null;
private NavigationDrawerFragment mNavigationDrawerFragment = null;
@ -116,11 +119,16 @@ implements NavigationDrawerFragment.NavigationDrawerCallbacks {
mNavigationDrawerFragment.EnableDrawer();
}
public void OpenNewsRecordScreen(NewsRecord newsRecord)
public void OpenNewsRecordScreen(NewsRecord newsRecord, String tag)
{
//Xperimental -- addToBackStack provoke error "Class not found". Need to resolve somehow!
//getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new NewsRecordFragment(newsRecord)).addToBackStack(null).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new NewsRecordFragment(newsRecord)).commit();
//I added workaround:
NewsRecordFragment newsRecordFragment = new NewsRecordFragment(newsRecord);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, newsRecordFragment, tag).commit();
mNavigationDrawerFragment.EnableDrawer();
}
@ -187,11 +195,33 @@ implements NavigationDrawerFragment.NavigationDrawerCallbacks {
@Override
public void onBackPressed() {
if ( getSupportFragmentManager().findFragmentByTag(TAG_FROM_MAINFRAGMENT_TO_NEWSRECORDFRAGMENT) != null)
{
OpenMainScreen();
return;
}
if ( getSupportFragmentManager().findFragmentByTag(TAG_FROM_NEWSLISTFRAGMENT_TO_NEWSRECORDFRAGMENT) != null)
{
OpenNewsScreen();
return;
}
super.onBackPressed();
/*
* AddToBackStack is falling. Here is workaround
if(getSupportFragmentManager().getBackStackEntryCount() != 0) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}*/
}
@Override

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import fishrungames.bashgid.core.ImageManager;
import fishrungames.bashgid.core.NewsManager;
import fishrungames.bashgid.core.NewsManager.NewsRecord;
import fishrungames.bashgid.core.NewsManager.NewsUpdatedCallbackInterface;
import android.content.Context;
import android.os.Bundle;
@ -14,7 +15,7 @@ import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import it.sephiroth.android.library.widget.HListView;
@ -93,48 +94,61 @@ public class MainPageFragment extends Fragment implements NewsUpdatedCallbackInt
{
ImageView news1ImageView = (ImageView) header.findViewById(R.id.news1ImageView);
ImageView news2ImageView = (ImageView) header.findViewById(R.id.news2ImageView);
ImageView news3ImageView = (ImageView) header.findViewById(R.id.news3ImageView);
ImageButton news1ImageButton = (ImageButton) header.findViewById(R.id.news1ImageButton);
ImageButton news2ImageButton = (ImageButton) header.findViewById(R.id.news2ImageButton);
ImageButton news3ImageButton = (ImageButton) header.findViewById(R.id.news3ImageButton);
TextView news1TextView = (TextView)header.findViewById(R.id.news1TextView);
TextView news2TextView = (TextView)header.findViewById(R.id.news2TextView);
TextView news3TextView = (TextView)header.findViewById(R.id.news3TextView);
Button news1Button = (Button)header.findViewById(R.id.news1Button);
Button news2Button = (Button)header.findViewById(R.id.news2Button);
Button news3Button = (Button)header.findViewById(R.id.news3Button);
ArrayList<NewsManager.NewsRecord> newsRecordArr = NewsManager.GetInstance().getNews();
if (newsRecordArr.size() > 0)
{
ImageManager.getInstance().ApplyImageToImageView(news1ImageView, newsRecordArr.get(0).imageId);
news1TextView.setText(newsRecordArr.get(0).title);
ImageManager.getInstance().ApplyImageToImageView(news1ImageButton, newsRecordArr.get(0).imageId);
news1Button.setText(newsRecordArr.get(0).title);
news1ImageButton.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(0)));
news1Button.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(0)));
}
else
{
news1ImageView.setImageResource(R.drawable.transparent);
news1TextView.setText(R.string.news_record_not_available);
news1ImageButton.setImageResource(R.drawable.transparent);
news1Button.setText(R.string.news_record_not_available);
news1ImageButton.setOnClickListener(null);
news1Button.setOnClickListener(null);
}
if (newsRecordArr.size() > 1)
{
ImageManager.getInstance().ApplyImageToImageView(news2ImageView, newsRecordArr.get(1).imageId);
news2TextView.setText(newsRecordArr.get(1).title);
ImageManager.getInstance().ApplyImageToImageView(news2ImageButton, newsRecordArr.get(1).imageId);
news2Button.setText(newsRecordArr.get(1).title);
news2ImageButton.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(1)));
news2Button.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(1)));
}
else
{
news2ImageView.setImageResource(R.drawable.transparent);
news2TextView.setText(R.string.news_record_not_available);
news2ImageButton.setImageResource(R.drawable.transparent);
news2Button.setText(R.string.news_record_not_available);
news2ImageButton.setOnClickListener(null);
news2Button.setOnClickListener(null);
}
if (newsRecordArr.size() > 2)
{
ImageManager.getInstance().ApplyImageToImageView(news3ImageView, newsRecordArr.get(2).imageId);
news3TextView.setText(newsRecordArr.get(2).title);
ImageManager.getInstance().ApplyImageToImageView(news3ImageButton, newsRecordArr.get(2).imageId);
news3Button.setText(newsRecordArr.get(2).title);
news3ImageButton.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(2)));
news3Button.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(2)));
}
else
{
news3ImageView.setImageResource(R.drawable.transparent);
news3TextView.setText(R.string.news_record_not_available);
news3ImageButton.setImageResource(R.drawable.transparent);
news3Button.setText(R.string.news_record_not_available);
news3ImageButton.setOnClickListener(null);
news3Button.setOnClickListener(null);
}
@ -222,5 +236,22 @@ public class MainPageFragment extends Fragment implements NewsUpdatedCallbackInt
});
}
public static class ButtonOnClickListener implements OnClickListener
{
NewsRecord newsRecord;
public ButtonOnClickListener(NewsRecord newsRecord)
{
this.newsRecord = newsRecord;
}
@Override
public void onClick(View v)
{
MainActivity.getInstance().OpenNewsRecordScreen(newsRecord, MainActivity.TAG_FROM_MAINFRAGMENT_TO_NEWSRECORDFRAGMENT);
}
}
}

View File

@ -143,7 +143,7 @@ public class NewsListFragment extends Fragment implements NewsUpdatedCallbackInt
@Override
public void onClick(View v)
{
MainActivity.getInstance().OpenNewsRecordScreen(newsRecord);
MainActivity.getInstance().OpenNewsRecordScreen(newsRecord, MainActivity.TAG_FROM_NEWSLISTFRAGMENT_TO_NEWSRECORDFRAGMENT);
}
}

View File

@ -8,6 +8,7 @@ import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
@ -27,16 +28,25 @@ public class NewsRecordFragment extends Fragment
View rootView = inflater.inflate(R.layout.fragment_news_record_page, container, false);
TextView titleTextView = (TextView) rootView.findViewById(R.id.titleTextView);
titleTextView.setText(newsRecord.title);
TextView descriptionTextView = (TextView) rootView.findViewById(R.id.descriptionTextView);
//TextView descriptionTextView = (TextView) rootView.findViewById(R.id.descriptionTextView);
//descriptionTextView.setText(newsRecord.description);
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
titleTextView.setText(newsRecord.title);
descriptionTextView.setText(newsRecord.description);
ImageManager.getInstance().ApplyImageToImageView(imageView, newsRecord.imageId);
WebView webView = (WebView) rootView.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(false);
String htmlCode = "<html><body>" + newsRecord.description + "</body></html>";
webView.loadDataWithBaseURL(null, htmlCode, "text/html", "UTF-8", null);
return rootView;
}
}

View File

@ -10,6 +10,8 @@ import java.net.URLConnection;
import org.apache.http.client.ClientProtocolException;
import android.util.Log;
public class DownloadManager {
@ -53,4 +55,20 @@ public class DownloadManager {
{
return mimeType.equals("image/bmp") || mimeType.equals("image/gif") || mimeType.equals("image/jpeg") || mimeType.equals("image/png");
}
public static String GetUrlFolder(String url)
{
int pos = url.lastIndexOf("/");
if (pos == -1)
{
Log.e("Error", "GetUrlFolder got incorrect agrument");
return "";
}
String substr = url.substring(0, pos);
return substr;
}
}

View File

@ -85,7 +85,7 @@ public class ImageManager {
String beforeExtension = imageUrl.substring(0, imageUrl.lastIndexOf("."));
String extension = imageUrl.substring(imageUrl.lastIndexOf("."));
return Base64.encodeToString(beforeExtension.getBytes(), Base64.DEFAULT) + extension;
return Base64.encodeToString(beforeExtension.getBytes(), Base64.DEFAULT).replace("\n", "") + extension;
}
public void DownloadImageIfNeeded(String imageUrl)
@ -121,6 +121,11 @@ public class ImageManager {
}
}
public String GetImageLocalUrl(String imageUrl)
{
return "file://" + MainActivity.getInstance().getFilesDir().getAbsolutePath() + "/" + ImageUrlToFileName(imageUrl);
}
}

View File

@ -82,13 +82,25 @@ public class NewsManager {
@Override
public void run()
{
String xmlCode = DownloadManager.getXmlFromUrl("http://www.bashinform.ru/rss/all.xml");
//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)
{
return;
}
//Remove BOM character if present
if (xmlCode.startsWith("\uFEFF"))
{
xmlCode = xmlCode.substring(1);
}
RssReader rssReader = new RssReader();
rssReader.ReadRss(xmlCode);
@ -97,6 +109,7 @@ public class NewsManager {
for (RssReader.Rss2Item rss2Item : rssReader.rss2ItemArr)
{
//Enclosure
if (rss2Item.rss2Enclosure != null)
{
if (DownloadManager.MimeTypeIsImage(rss2Item.rss2Enclosure.mimeType))
@ -104,6 +117,38 @@ public class NewsManager {
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)
@ -137,11 +182,6 @@ public class NewsManager {
public NewsManager()
{
/*
newsRecord.add(new NewsRecord("Власти Башкирии создадут выезд из Уфы на трассу М-5 «Урал»", "Власти Башкирии создадут выезд из Уфы на трассу М-5 «Урал»", "R.drawable.news_image1"));
newsRecord.add(new NewsRecord("Башкирия заняла первое место индекса «деловой активности»", "Башкирия заняла первое место индекса «деловой активности»", "R.drawable.news_image2"));
newsRecord.add(new NewsRecord("«Салават Юлаев» обыграл «Ладу» и прервал серию из 5 поражений в КХЛ", "«Салават Юлаев» обыграл «Ладу» и прервал серию из 5 поражений в КХЛ", "R.drawable.news_image3"));
*/
newsRecord = MainActivity.getInstance().newsDataSource.getNews();
}

View File

@ -120,8 +120,22 @@ public class RssReader {
String enclosureLengthStr = XmlProcessor.getAttributeValue(e, "enclosure", "length");
int enclosureLength = Integer.valueOf(enclosureLengthStr);
String enclosureMimeType = XmlProcessor.getAttributeValue(e, "enclosure", "type");
int enclosureWidth = Integer.valueOf(XmlProcessor.getAttributeValue(e, "enclosure", "width"));
int enclosureHeight = Integer.valueOf(XmlProcessor.getAttributeValue(e, "enclosure", "height"));
String enclosureWidthStr = XmlProcessor.getAttributeValue(e, "enclosure", "width");
if (enclosureWidthStr.equals(""))
{
enclosureWidthStr = "0";
}
int enclosureWidth = Integer.valueOf(enclosureWidthStr);
String enclosureHeightStr = XmlProcessor.getAttributeValue(e, "enclosure", "height");
if (enclosureHeightStr.equals(""))
{
enclosureHeightStr = "0";
}
int enclosureHeight = Integer.valueOf(enclosureHeightStr);
rss2Enclosure = new Rss2Item.Rss2Enclosure(enclosureUrl, enclosureLength, enclosureMimeType, enclosureHeight, enclosureWidth);

View File

@ -90,10 +90,14 @@ public class XmlProcessor
if (elem.hasAttributes())
{
child = elem.getAttributes().getNamedItem(attr);
if (child.getNodeName().equals(attr))
if (child != null)
{
String childNodeValue = child.getNodeValue();
return childNodeValue;
//Xperimental -- do we need this?
if (child.getNodeName().equals(attr))
{
String childNodeValue = child.getNodeValue();
return childNodeValue;
}
}
}
}