Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Аганов Артур 2539 #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Built application files
*.apk
*.ap_

# Files for the Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log

.idea/
*.iml
Binary file added app-debug.apk
Binary file not shown.
24 changes: 24 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 20
buildToolsVersion "20.0.0"

defaultConfig {
applicationId "ru.ifmo.md.lesson5"
minSdkVersion 15
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\nik\AppData\Local\Android\android-studio\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
13 changes: 13 additions & 0 deletions app/src/androidTest/java/ru/ifmo/md/lesson5/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.ifmo.md.lesson5;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
25 changes: 25 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.ifmo.md.lesson5" >

<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NewsActivity"
android:label="@string/title_activity_web" >
</activity>
</application>

</manifest>
31 changes: 31 additions & 0 deletions app/src/main/java/ru/ifmo/md/lesson5/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.ifmo.md.lesson5;

public class Item {
private String title;
private String description;
private String date;
private String url;

public Item(String title, String description, String date, String url) {
this.title = title;
this.description = description;
this.date = date;
this.url = url;
}

public String getTitle() {
return title;
}

public String getDescription() {
return description;
}

public String getDate() {
return date;
}

public String getUrl() {
return url;
}
}
29 changes: 29 additions & 0 deletions app/src/main/java/ru/ifmo/md/lesson5/MyActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.ifmo.md.lesson5;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class MyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final Intent intent = new Intent(this, NewsActivity.class);
ListView listView1 = (ListView)findViewById(R.id.listView1);
new NewsDownloadTask(this, listView1).execute(getString(R.string.URL_NEWS));
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long x) {
Item item = (Item)adapter.getItemAtPosition(position);
intent.putExtra(getString(R.string.URL), item.getUrl());
startActivity(intent);
}
});

}
}
19 changes: 19 additions & 0 deletions app/src/main/java/ru/ifmo/md/lesson5/NewsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.ifmo.md.lesson5;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class NewsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
WebView webView = (WebView)findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(getIntent().getStringExtra(getString(R.string.URL)));
}

}
104 changes: 104 additions & 0 deletions app/src/main/java/ru/ifmo/md/lesson5/NewsDownloadTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package ru.ifmo.md.lesson5;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class NewsDownloadTask extends AsyncTask<String, Void, List<Item> > {
private ProgressDialog progressDialog;
private Context context;
private ListView listView;

protected NewsDownloadTask(Context context, ListView listView) {
this.context = context;
this.listView = listView;
}

static String normalize(String s) {
String res = s.replace("<br>", "\n");
res = res.replace("&quot;", "\"");
Log.i("!", res);
return res;
}

@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage(context.getString(R.string.DOWNLOADING_NEWS));
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}

@Override
protected List<Item> doInBackground(String... params) {
ArrayList<Item> news = new ArrayList<Item>();
try {
XmlPullParser xpp = XmlPullParserFactory.newInstance().newPullParser();
InputStream inputStream = new URL(params[0]).openStream();
xpp.setInput(inputStream, null);
while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
if (xpp.getEventType() == XmlPullParser.START_TAG && xpp.getName().equals("item")) {
String title = "";
String description = "";
String date = "";
String url = "";
xpp.next();
while (!(xpp.getEventType() == XmlPullParser.END_TAG && xpp.getName().equals("item"))) {
if (xpp.getEventType() == XmlPullParser.START_TAG && xpp.getName().equals("link")) {
xpp.next();
url = xpp.getText();
xpp.next();
}
if (xpp.getEventType() == XmlPullParser.START_TAG && xpp.getName().equals("title")) {
xpp.next();
title = xpp.getText();
xpp.next();
}
if (xpp.getEventType() == XmlPullParser.START_TAG && xpp.getName().equals("pubDate")) {
xpp.next();
date = xpp.getText();
xpp.next();
}
if (xpp.getEventType() == XmlPullParser.START_TAG && xpp.getName().equals("description")) {
xpp.next();
description = xpp.getText();
}
xpp.next();
}
news.add(new Item(title, normalize(description), date, url));
}
xpp.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
return news;
} catch (IOException e) {
e.printStackTrace();
return news;
}
return news;
}

@Override
protected void onPostExecute(List<Item> news) {
NewsListAdapter adapter = new NewsListAdapter(news);
listView.setAdapter(adapter);
if (news.isEmpty()) {
news.add(new Item(context.getString(R.string.ERROR), "", "", ""));
}
progressDialog.dismiss();
}
}
43 changes: 43 additions & 0 deletions app/src/main/java/ru/ifmo/md/lesson5/NewsListAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ru.ifmo.md.lesson5;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class NewsListAdapter extends BaseAdapter {
List<Item> news;

public NewsListAdapter(List<Item> news) {
this.news = news;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news, viewGroup, false);
}
((TextView)view.findViewById(R.id.title)).setText(news.get(i).getTitle());
((TextView)view.findViewById(R.id.date)).setText(news.get(i).getDate());
((TextView)view.findViewById(R.id.description)).setText(news.get(i).getDescription());
return view;
}

@Override
public int getCount() {
return news.size();
}

@Override
public Object getItem(int i) {
return news.get(i);
}

@Override
public long getItemId(int i) {
return i;
}
}
Binary file added app/src/main/res/drawable-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions app/src/main/res/layout/activity_my.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView1" />
</RelativeLayout>
16 changes: 16 additions & 0 deletions app/src/main/res/layout/activity_news.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="ru.ifmo.md.lesson5.NewsActivity">

<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView"
android:visibility="visible" />
</RelativeLayout>
Loading