Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. package e.vegard.rssfeed;
  2.  
  3.  
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.os.AsyncTask;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10.  
  11.  
  12. import android.text.TextUtils;
  13. import android.util.Log;
  14. import android.util.Xml;
  15. import android.view.View;
  16. import android.widget.Button;
  17.  
  18. import android.widget.Toast;
  19.  
  20. import org.xmlpull.v1.XmlPullParser;
  21. import org.xmlpull.v1.XmlPullParserException;
  22.  
  23.  
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26.  
  27. import java.net.URL;
  28. import java.util.ArrayList;
  29. import java.util.Timer;
  30. import java.util.TimerTask;
  31.  
  32.  
  33. public class MainActivity extends AppCompatActivity {
  34.  
  35. private static final String TAG = "MainActivity";
  36.  
  37. private Button mPreferenceButton;
  38. private Button mNewsButton;
  39.  
  40. public static ArrayList<RssFeedModel> mFeedModelList;
  41. private Timer timer;
  42. private TimerTask freq;
  43.  
  44. private String mFeedTitle;
  45. private String mFeedDescription;
  46. private String mFeedLink;
  47.  
  48. private boolean ok;
  49.  
  50. @Override
  51. protected void onCreate(Bundle savedInstanceState) {
  52. super.onCreate(savedInstanceState);
  53. setContentView(R.layout.activity_main);
  54.  
  55. // referencing the buttons to the ui elements
  56. mPreferenceButton = findViewById(R.id.btn_preference);
  57. mNewsButton = findViewById(R.id.btn_newsList);
  58.  
  59. // here we will send to the next activity
  60. mPreferenceButton.setOnClickListener(new View.OnClickListener() {
  61. @Override
  62. public void onClick(View v) {
  63. Intent i = new Intent(MainActivity.this, Preferences.class);
  64. startActivity(i);
  65. if (getSharedPreferences(Preferences.URL, MODE_PRIVATE).contains("url")) {
  66. mNewsButton.setEnabled(true);
  67. }
  68.  
  69. }
  70. });
  71. mNewsButton.setOnClickListener(new View.OnClickListener() {
  72. @Override
  73. public void onClick(View v) {
  74. // the ok boolean helps us check if it was a scheduled
  75. // or a button press that fetched the rssfeed
  76. ok = true;
  77. timer = new Timer();
  78. freq = new TimerTask() {
  79. @Override
  80. public void run() {
  81. FetchTheURL();
  82. }
  83. };
  84.  
  85. // we are setting this timer 60000 is the same as 1 min key values contains values as min
  86. timer.schedule(freq,1 , getSharedPreferences(Preferences.URL, MODE_PRIVATE).getInt("frequency", 0)*60000);
  87.  
  88. }
  89. });
  90.  
  91. //if no link is provided in the sharedPreference
  92. //the button for news will be disabled
  93. if (!getSharedPreferences(Preferences.URL, MODE_PRIVATE).contains("url")) {
  94. mNewsButton.setEnabled(false);
  95. }
  96.  
  97. }
  98.  
  99. // just a shorter way to start the asynctask
  100. public void FetchTheURL() {
  101. new FetchFeedTask(MainActivity.this).execute((Void) null);
  102. }
  103.  
  104. public ArrayList<RssFeedModel> parseFeed(InputStream inputStream) throws XmlPullParserException, IOException {
  105. String title = null;
  106. String link = null;
  107. String description = null;
  108. String img = null;
  109. boolean isItem = false;
  110. ArrayList<RssFeedModel> items = new ArrayList<>();
  111. int amount;
  112.  
  113. try {
  114. XmlPullParser xmlPullParser = Xml.newPullParser();
  115. xmlPullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
  116. xmlPullParser.setInput(inputStream, null);
  117.  
  118. SharedPreferences rssURL;
  119. rssURL = getSharedPreferences(Preferences.URL, MODE_PRIVATE);
  120. amount = rssURL.getInt("amount", 0);
  121. Log.d(TAG, "" + amount);
  122.  
  123. xmlPullParser.nextTag();
  124. while (xmlPullParser.next() != XmlPullParser.END_DOCUMENT && amount > 0) {
  125. int eventType = xmlPullParser.getEventType();
  126.  
  127. String name = xmlPullParser.getName();
  128. if(name == null)
  129. continue;
  130.  
  131. if(eventType == XmlPullParser.END_TAG) {
  132. if(name.equalsIgnoreCase("item")) {
  133. isItem = false;
  134. }
  135. continue;
  136. }
  137.  
  138. if (eventType == XmlPullParser.START_TAG) {
  139. if(name.equalsIgnoreCase("item")) {
  140. isItem = true;
  141. continue;
  142. }
  143. }
  144.  
  145. Log.d("MainActivity", "Parsing name ==> " + name);
  146. String result = "";
  147. String attr = "";
  148. if (xmlPullParser.next() == XmlPullParser.TEXT) {
  149. result = xmlPullParser.getText();
  150. xmlPullParser.nextTag();
  151. }
  152.  
  153. if (name.equalsIgnoreCase("title")) {
  154. title = result;
  155. } else if (name.equalsIgnoreCase("link")) {
  156. link = result;
  157. } else if (name.equalsIgnoreCase("description")) {
  158. description = result;
  159. } else if (name.equalsIgnoreCase("enclosure")) {
  160. attr = xmlPullParser.getAttributeValue(xmlPullParser.getNamespace(), "url");
  161. img = attr;
  162. Log.d(TAG, "chr: " + img);
  163. }
  164.  
  165. if (title != null && link != null && description != null) {
  166. if(isItem) {
  167. RssFeedModel item = new RssFeedModel(title, link, description, img);
  168. items.add(item);
  169. amount--;
  170. }
  171. else {
  172. mFeedTitle = title;
  173. mFeedLink = link;
  174. mFeedDescription = description;
  175. }
  176.  
  177. title = null;
  178. link = null;
  179. description = null;
  180. img = null;
  181. isItem = false;
  182. }
  183. }
  184.  
  185. return items;
  186. } finally {
  187. inputStream.close();
  188. }
  189. }
  190.  
  191. private class FetchFeedTask extends AsyncTask<Void, Void, Boolean> {
  192.  
  193. private String urlLink;
  194. private Context context;
  195.  
  196. private FetchFeedTask(Context context) {
  197. this.context = context.getApplicationContext();
  198. }
  199.  
  200. @Override
  201. protected void onPreExecute() {
  202. mFeedTitle = null;
  203. mFeedLink = null;
  204. mFeedDescription = null;
  205.  
  206. //when we have load with fetch we get the value from a preference
  207. SharedPreferences rssURL;
  208. rssURL = getSharedPreferences(Preferences.URL, MODE_PRIVATE);
  209. urlLink = rssURL.getString("url", "");
  210. }
  211.  
  212. @Override
  213. protected Boolean doInBackground(Void... voids) {
  214. if (TextUtils.isEmpty(urlLink)) {
  215. return false;
  216. }
  217. try {
  218. if(!urlLink.startsWith("http://") && !urlLink.startsWith("https://"))
  219. urlLink = "https://" + urlLink;
  220.  
  221. URL url = new URL(urlLink);
  222. InputStream inputStream = url.openConnection().getInputStream();
  223. mFeedModelList = parseFeed(inputStream);
  224. return true;
  225. } catch (IOException e) {
  226. Log.e(TAG, "Error", e);
  227. } catch (XmlPullParserException e) {
  228. Log.e(TAG, "Error", e);
  229. }
  230. return false;
  231. }
  232.  
  233. @Override
  234. protected void onPostExecute(Boolean success) {
  235.  
  236. if (success) {
  237. if (ok) {
  238. Intent i = new Intent(MainActivity.this, NewsList.class);
  239. i.putExtra("listOfRss", mFeedModelList);
  240. startActivity(i);
  241. ok = false;
  242. }
  243.  
  244.  
  245. } else {
  246. Toast.makeText(MainActivity.this,
  247. "Enter a valid Rss feed url",
  248. Toast.LENGTH_LONG).show();
  249.  
  250. }
  251. }
  252. }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement