Skip to content

Commit 35412e4

Browse files
author
David Weiser
committed
adds rssreader exercise
1 parent b448bef commit 35412e4

52 files changed

Lines changed: 1252 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

RssReader/app/build.gradle

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.3"
6+
defaultConfig {
7+
applicationId "com.example.android.rssreader"
8+
minSdkVersion 25
9+
targetSdkVersion 25
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(include: ['*.jar'], dir: 'libs')
24+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25+
exclude group: 'com.android.support', module: 'support-annotations'
26+
})
27+
compile "com.android.support:recyclerview-v7:25.3.1"
28+
testCompile 'junit:junit:4.12'
29+
compile project(':com.example.android.rssfeedlibrary')
30+
compile 'com.github.bumptech.glide:glide:3.8.0'
31+
compile 'com.android.support:support-v4:25.3.1'
32+
compile 'com.google.code.gson:gson:2.8.1'
33+
compile 'com.squareup:otto:1.3.8'
34+
}

RssReader/app/proguard-rules.pro

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/david/Android/Sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.android.rssreader;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.example.android.rssreader", appContext.getPackageName());
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.android.rssreader" >
4+
<uses-permission android:name="android.permission.INTERNET"/>
5+
6+
<application
7+
android:name=".RssApplication"
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:theme="@style/AppTheme" >
12+
<activity
13+
android:name=".RssfeedActivity"
14+
android:label="@string/app_name" >
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
<category android:name="android.intent.category.LAUNCHER" />
18+
</intent-filter>
19+
</activity>
20+
21+
<service android:name=".RssDownloadService"/>
22+
<activity android:name=".SettingsFragment"/>
23+
</application>
24+
</manifest>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.example.android.rssreader;
2+
3+
import android.app.Fragment;
4+
import android.os.Bundle;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.webkit.WebView;
9+
import android.widget.TextView;
10+
11+
public class DetailFragment extends Fragment {
12+
public static final String EXTRA_URL ="url";
13+
private WebView webview;
14+
15+
@Override
16+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
17+
Bundle savedInstanceState) {
18+
// no layout file is needed, as we only use one view
19+
webview = new WebView(getActivity());
20+
return webview;
21+
}
22+
@Override
23+
public void onActivityCreated(Bundle savedInstanceState) {
24+
super.onActivityCreated(savedInstanceState);
25+
Bundle bundle = getArguments();
26+
if (bundle != null) {
27+
String link = bundle.getString("url");
28+
setText(link);
29+
}
30+
}
31+
public void setText(String url) {
32+
// Set the scale factor
33+
webview.setInitialScale(50);
34+
webview.getSettings().setBuiltInZoomControls(true);
35+
webview.getSettings().setSupportZoom(true);
36+
webview.loadUrl(url);
37+
}
38+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.example.android.rssreader;
2+
3+
import android.app.Fragment;
4+
import android.app.Notification;
5+
import android.app.NotificationManager;
6+
import android.app.PendingIntent;
7+
import android.content.BroadcastReceiver;
8+
import android.content.Context;
9+
import android.content.Intent;
10+
import android.content.IntentFilter;
11+
import android.os.AsyncTask;
12+
import android.os.Bundle;
13+
import android.support.v7.widget.GridLayoutManager;
14+
import android.support.v7.widget.LinearLayoutManager;
15+
import android.support.v7.widget.RecyclerView;
16+
import android.view.LayoutInflater;
17+
import android.view.View;
18+
import android.view.ViewGroup;
19+
20+
import com.example.android.rssfeedlibrary.RssFeedProvider;
21+
import com.example.android.rssfeedlibrary.RssItem;
22+
import com.squareup.otto.Subscribe;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
public class MyListFragment extends Fragment {
28+
private OnItemSelectedListener listener;
29+
RssItemAdapter adapter;
30+
List<RssItem> rssItems;
31+
ParseTask parseTask;
32+
33+
@Override
34+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
35+
View view = inflater.inflate(R.layout.fragment_rsslist_overview, container, false);
36+
RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
37+
GridLayoutManager mLayoutManager = new GridLayoutManager(getContext(), 2);
38+
mRecyclerView.setLayoutManager(mLayoutManager);
39+
rssItems = RssApplication.list;;
40+
adapter = new RssItemAdapter(rssItems, this);
41+
mRecyclerView.setAdapter(adapter);
42+
if(rssItems.isEmpty()) { // #1
43+
updateListContent();
44+
}
45+
RssApplication.bus.register(this);
46+
return view;
47+
}
48+
49+
public interface OnItemSelectedListener {
50+
void onRssItemSelected(String link);
51+
}
52+
53+
@Override
54+
public void onAttach(Context context) {
55+
super.onAttach(context);
56+
if (context instanceof OnItemSelectedListener) {
57+
listener = (OnItemSelectedListener) context;
58+
} else {
59+
throw new ClassCastException(context.toString()
60+
+ " must implement MyListFragment.OnItemSelectedListener");
61+
}
62+
}
63+
64+
public void updateDetail(String uri) {
65+
listener.onRssItemSelected(uri);
66+
}
67+
68+
public void updateListContent() {
69+
if (parseTask == null) {
70+
parseTask = new ParseTask();
71+
parseTask.setFragment(this);
72+
parseTask.execute("http://www.vogella.com/article.rss");
73+
}
74+
}
75+
76+
public void setListContent(List<RssItem> result) {
77+
RssApplication.list = result;
78+
rssItems.clear();
79+
rssItems.addAll(result);
80+
adapter.notifyDataSetChanged();
81+
if(parseTask != null) {
82+
parseTask.setFragment(null);
83+
parseTask = null;
84+
}
85+
}
86+
87+
private static class ParseTask extends
88+
AsyncTask<String, Void, List<RssItem>> {
89+
private MyListFragment fragment;
90+
public void setFragment(MyListFragment fragment) {
91+
this.fragment = fragment;
92+
}
93+
@Override
94+
protected List<RssItem> doInBackground(String... params) {
95+
List<RssItem> list = RssFeedProvider.parse(params[0]);
96+
return list;
97+
}
98+
@Override
99+
protected void onPostExecute(List<RssItem> result) {
100+
fragment.setListContent(result);
101+
}
102+
}
103+
104+
@Subscribe
105+
public void update(Object object){
106+
getActivity().runOnUiThread(new Runnable() {
107+
@Override
108+
public void run() {
109+
setListContent(new ArrayList<RssItem>(RssApplication.list));
110+
MyListFragment.this.createNotification();
111+
}
112+
});
113+
}
114+
115+
private void createNotification() {
116+
Notification noti = new Notification.Builder(getActivity())
117+
.setContentTitle("Update").setAutoCancel(true)
118+
.setSmallIcon(R.mipmap.ic_launcher).
119+
setContentIntent(PendingIntent.getActivity(getContext(), 0, new Intent
120+
(getContext(), RssfeedActivity.class),PendingIntent.FLAG_UPDATE_CURRENT))
121+
.build();
122+
NotificationManager notificationManager =
123+
(NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
124+
notificationManager.notify((int) System.currentTimeMillis(), noti);
125+
}
126+
127+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.example.android.rssreader;
2+
import java.io.BufferedReader;
3+
import java.io.InputStreamReader;
4+
import java.lang.reflect.Type;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import android.app.Application;
8+
import com.example.android.rssfeedlibrary.RssItem;
9+
import com.google.gson.Gson;
10+
import com.google.gson.reflect.TypeToken;
11+
import com.squareup.otto.Bus;
12+
import com.squareup.otto.ThreadEnforcer;
13+
14+
public class RssApplication extends Application {
15+
public final static String URL ="url";
16+
public final static String RSS_UPDATE ="neuedaten";
17+
public final static String RSS_FILE ="rssitems.json";
18+
public static List<RssItem> list;
19+
20+
21+
static Bus bus;
22+
23+
@Override
24+
public void onCreate() {
25+
super.onCreate();
26+
bus = new Bus(ThreadEnforcer.ANY);
27+
28+
list = new ArrayList<>();
29+
StringBuffer buffer = new StringBuffer();
30+
try (BufferedReader input = new BufferedReader(
31+
new InputStreamReader(
32+
openFileInput(RSS_FILE)))) {
33+
String line;
34+
while ((line = input.readLine()) != null) {
35+
buffer.append(line);
36+
}
37+
} catch (Exception ex) {
38+
// do nothing
39+
}
40+
if (buffer!=null && buffer.length()>0 )
41+
{
42+
Gson gson = new Gson();
43+
Type type = new TypeToken<List<RssItem>>() {}.getType();
44+
List<RssItem> fromJson = gson.fromJson(buffer.toString(), type);
45+
list.addAll(fromJson);
46+
}
47+
}
48+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.example.android.rssreader;
2+
3+
import android.app.IntentService;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.os.Bundle;
7+
8+
import com.example.android.rssfeedlibrary.RssFeedProvider;
9+
import com.example.android.rssfeedlibrary.RssItem;
10+
import com.google.gson.Gson;
11+
import com.google.gson.reflect.TypeToken;
12+
13+
import java.io.FileOutputStream;
14+
import java.lang.reflect.Type;
15+
import java.util.List;
16+
17+
import static com.example.android.rssreader.RssApplication.RSS_UPDATE;
18+
19+
public class RssDownloadService extends IntentService {
20+
21+
public static String NOTIFICATION = "rssfeedupdated";
22+
23+
public RssDownloadService() {
24+
super("RssDownloadService");
25+
}
26+
27+
28+
@Override
29+
protected void onHandleIntent(Intent intent) {
30+
Bundle extras = intent.getExtras();
31+
String string = extras.getString("url");
32+
List<RssItem> list = RssFeedProvider.parse(string);
33+
RssApplication.list = list;
34+
RssApplication.bus.post(RSS_UPDATE);
35+
}
36+
}

0 commit comments

Comments
 (0)