├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── values
│ │ │ │ ├── colors.xml
│ │ │ │ ├── styles.xml
│ │ │ │ └── strings.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── article_activity.xml
│ │ │ │ └── article_list_item.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── botsone
│ │ │ └── android
│ │ │ └── technews
│ │ │ ├── Article.java
│ │ │ ├── ArticleLoader.java
│ │ │ ├── ArticleAdapter.java
│ │ │ ├── ArticleActivity.java
│ │ │ └── QueryUtils.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── botsone
│ │ │ └── android
│ │ │ └── technews
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── botsone
│ │ └── android
│ │ └── technews
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── .idea
├── modules.xml
├── runConfigurations.xml
├── gradle.xml
└── misc.xml
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bots/TechNews/master/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bots/TechNews/master/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bots/TechNews/master/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bots/TechNews/master/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bots/TechNews/master/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bots/TechNews/master/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bots/TechNews/master/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bots/TechNews/master/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bots/TechNews/master/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bots/TechNews/master/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bots/TechNews/master/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #2b2b2b
4 | #414141
5 | #4056ff
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun Feb 18 20:50:08 MST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/botsone/android/technews/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.botsone.android.technews;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | TechNews
4 |
5 |
6 | No articles found.
7 |
8 |
9 | No internet connection.
10 |
11 | Section:\u0020
12 |
13 | Author:\u0020
14 |
15 | Date:\u0020
16 |
17 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/botsone/android/technews/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.botsone.android.technews;
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 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
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.botsone.android.technews", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 | defaultConfig {
6 | applicationId "com.botsone.android.technews"
7 | minSdkVersion 19
8 | targetSdkVersion 26
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | implementation 'com.android.support:appcompat-v7:26.1.0'
24 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
28 | compile 'com.squareup.picasso:picasso:2.5.2'
29 | }
30 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/article_activity.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
16 |
22 |
23 |
24 |
30 |
31 |
--------------------------------------------------------------------------------
/app/src/main/java/com/botsone/android/technews/Article.java:
--------------------------------------------------------------------------------
1 | package com.botsone.android.technews;
2 |
3 | /**
4 | * Created by bots on 2/18/18.
5 | */
6 |
7 | public class Article {
8 |
9 | private String mImageUrl;
10 |
11 | private String mTitle;
12 |
13 | private String mSection;
14 |
15 | private String mAuthor;
16 |
17 | private String mDate;
18 |
19 | private String mBody;
20 |
21 | private String mUrl;
22 |
23 | public Article(String imageUrl, String title, String section, String author,
24 | String date, String body, String url) {
25 |
26 | mImageUrl = imageUrl;
27 | mTitle = title;
28 | mSection = section;
29 | mAuthor = author;
30 | mDate = date;
31 | mBody = body;
32 | mUrl = url;
33 | }
34 |
35 | public String getImageUrl() {
36 | return mImageUrl;
37 | }
38 |
39 | public String getTitle() {
40 | return mTitle;
41 | }
42 |
43 | public String getSection() {
44 | return mSection;
45 | }
46 |
47 | public String getAuthor() {
48 | return mAuthor;
49 | }
50 |
51 | public String getDate() {
52 | return mDate;
53 | }
54 |
55 | public String getBody() {
56 | return mBody;
57 | }
58 |
59 | public String getUrl() {
60 | return mUrl;
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/app/src/main/java/com/botsone/android/technews/ArticleLoader.java:
--------------------------------------------------------------------------------
1 | package com.botsone.android.technews;
2 |
3 | import android.content.AsyncTaskLoader;
4 | import android.content.Context;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * Created by bots on 2/18/18.
10 | */
11 |
12 | public class ArticleLoader extends AsyncTaskLoader> {
13 |
14 | /**
15 | * Tag for log messages
16 | */
17 | private static final String LOG_TAG = ArticleLoader.class.getName();
18 |
19 | /**
20 | * Query URL
21 | */
22 | private String mUrl;
23 |
24 | /**
25 | * Constructs a new {@link ArticleLoader}.
26 | *
27 | * @param context of the activity
28 | * @param url to load data from
29 | */
30 | public ArticleLoader(Context context, String url) {
31 | super(context);
32 | mUrl = url;
33 | }
34 |
35 | @Override
36 | protected void onStartLoading() {
37 | forceLoad();
38 | }
39 |
40 | /**
41 | * This is on a background thread.
42 | */
43 | @Override
44 | public List loadInBackground() {
45 | if (mUrl == null) {
46 | return null;
47 | }
48 |
49 | // Perform the network request, parse the response, and extract a list of articles.
50 | List articles = QueryUtils.fetchArticleData(mUrl);
51 | return articles;
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 | 1.8
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/article_list_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
16 |
17 |
22 |
23 |
28 |
29 |
39 |
40 |
47 |
48 |
55 |
56 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
75 |
76 |
--------------------------------------------------------------------------------
/app/src/main/java/com/botsone/android/technews/ArticleAdapter.java:
--------------------------------------------------------------------------------
1 | package com.botsone.android.technews;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.NonNull;
5 | import android.support.annotation.Nullable;
6 | import android.text.Html;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.ArrayAdapter;
11 | import android.widget.ImageView;
12 | import android.widget.TextView;
13 |
14 | import com.squareup.picasso.Picasso;
15 |
16 | import java.text.ParseException;
17 | import java.text.SimpleDateFormat;
18 | import java.util.Date;
19 | import java.util.List;
20 | import java.util.Locale;
21 |
22 | /**
23 | * Created by bots on 2/18/18.
24 | */
25 |
26 | public class ArticleAdapter extends ArrayAdapter {
27 |
28 | public ArticleAdapter(Context context, List articles) {
29 | super(context, 0, articles);
30 | }
31 |
32 | @NonNull
33 | @Override
34 | public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
35 |
36 | View listItemView = convertView;
37 |
38 | if (listItemView == null) {
39 | listItemView = LayoutInflater.from(getContext()).inflate(
40 | R.layout.article_list_item, parent, false);
41 | }
42 |
43 | // Get article object at current position
44 | Article currentArticle = getItem(position);
45 |
46 | // Set the imageView to display the current image
47 | ImageView imageView = (ImageView) listItemView.findViewById(R.id.image_view);
48 | Picasso.with(getContext()).load(currentArticle.getImageUrl()).resize(250, 250).centerCrop().into(imageView);
49 |
50 | // Set the title textView to display the current title
51 | TextView titleTextView = (TextView) listItemView.findViewById(R.id.title_textview);
52 | titleTextView.setText(currentArticle.getTitle());
53 |
54 | // Set the section textView to display the current section
55 | TextView sectionTextView = (TextView) listItemView.findViewById(R.id.section_textview);
56 | sectionTextView.setText(getContext().getString(R.string.section) + currentArticle.getSection());
57 |
58 | // Set the author textView to display the current author
59 | TextView authorTextView = (TextView) listItemView.findViewById(R.id.author_textview);
60 | authorTextView.setText(getContext().getString(R.string.author) + currentArticle.getAuthor());
61 |
62 | // Set the date textView to display the current date
63 | TextView dateTextView = (TextView) listItemView.findViewById(R.id.date_textview);
64 | dateTextView.setText(getContext().getString(R.string.date) + formatDate(currentArticle.getDate()));
65 |
66 | // Set the article body textView to display the current article body
67 | TextView bodyTextView = (TextView) listItemView.findViewById(R.id.body_textview);
68 | bodyTextView.setText(Html.fromHtml(currentArticle.getBody()));
69 |
70 | return listItemView;
71 | }
72 |
73 | private String formatDate(String date) {
74 | SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.getDefault());
75 | String formattedDate = "";
76 |
77 | try {
78 | Date formatDate = sdfSource.parse(date);
79 | formattedDate = formatDate.toString();
80 | return formattedDate;
81 | } catch (ParseException e) {
82 | e.printStackTrace();
83 | }
84 | return formattedDate;
85 | }
86 |
87 | }
88 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/com/botsone/android/technews/ArticleActivity.java:
--------------------------------------------------------------------------------
1 | package com.botsone.android.technews;
2 |
3 | import android.app.LoaderManager;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.Loader;
7 | import android.net.ConnectivityManager;
8 | import android.net.NetworkInfo;
9 | import android.net.Uri;
10 | import android.os.Bundle;
11 | import android.support.annotation.Nullable;
12 | import android.support.v7.app.AppCompatActivity;
13 | import android.view.View;
14 | import android.widget.AdapterView;
15 | import android.widget.ListView;
16 | import android.widget.TextView;
17 |
18 | import java.util.ArrayList;
19 | import java.util.List;
20 |
21 | /**
22 | * Created by bots on 2/18/18.
23 | */
24 |
25 | public class ArticleActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks> {
26 |
27 | public static final String LOG_TAG = ArticleActivity.class.getName();
28 |
29 | /**
30 | * TextView that is displayed when the list is empty
31 | */
32 | private TextView mEmptyStateTextView;
33 |
34 | /**
35 | * Constant value for the article loader ID. We can choose any integer.
36 | * This really only comes into play if you're using multiple loaders.
37 | */
38 | private static final int ARTICLE_LOADER_ID = 0;
39 |
40 | /**
41 | * URL for article data from the guardian
42 | */
43 | private static final String GUARDIAN_REQUEST_URL =
44 | "http://content.guardianapis.com/search?q=technology&from-date=2018-02-01&show-fields=all&page-size=20&api-key=8dd1aae4-c14c-4fc9-8d99-8fbc47e038ef";
45 |
46 | /**
47 | * Adapter for the list of articles
48 | */
49 | private ArticleAdapter mAdapter;
50 |
51 | @Override
52 | protected void onCreate(@Nullable Bundle savedInstanceState) {
53 | super.onCreate(savedInstanceState);
54 | setContentView(R.layout.article_activity);
55 |
56 | // Find a reference to the {@link ListView} in the layout
57 | ListView articleListView = (ListView) findViewById(R.id.list);
58 |
59 | // Define empty textview for when no data is returned
60 | mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);
61 | articleListView.setEmptyView(mEmptyStateTextView);
62 |
63 | // Create a new adapter that takes an empty list of articles as input
64 | mAdapter = new ArticleAdapter(this, new ArrayList());
65 |
66 | // Set the adapter on the {@link ListView}
67 | // so the list can be populated in the user interface
68 | articleListView.setAdapter(mAdapter);
69 |
70 | // Set an item click listener on the ListView, which sends an intent to a web browser
71 | // to open a website with the full article.
72 | articleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
73 | @Override
74 | public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
75 | // Find the current article that was clicked on
76 | Article currentArticle = mAdapter.getItem(i);
77 |
78 | // Convert the String URL into a URI object (to pass into the Intent constructor)
79 | Uri articleUri = Uri.parse(currentArticle.getUrl());
80 |
81 | // Convert the String URL into a URI object (to pass into the Intent constructor)
82 | Intent websiteIntent = new Intent(Intent.ACTION_VIEW, articleUri);
83 |
84 | // Send the intent to launch a new activity
85 | startActivity(websiteIntent);
86 |
87 | }
88 |
89 | });
90 | // Get a reference to the ConnectivityManager to check state of network connectivity
91 | ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
92 |
93 | // Get details on the currently active default data network
94 | NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
95 |
96 | // If there is a network connection,fetch data
97 | if (networkInfo != null && networkInfo.isConnected()) {
98 | // Get a reference to the loaderManager to interact with loaders
99 | LoaderManager loaderManager = getLoaderManager();
100 |
101 | // Initialize the loader. Pass in the int ID constant defined above and pass in null for
102 | // the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
103 | // because this activity implements the LoaderCallbacks interface).
104 | loaderManager.initLoader(ARTICLE_LOADER_ID, null, this);
105 | } else {
106 | // Otherwise show error
107 | // First hide the loading indicator so that the error message will be visible
108 | View loadingIndicator = (View) findViewById(R.id.loading_indicator);
109 | loadingIndicator.setVisibility(View.GONE);
110 |
111 | mEmptyStateTextView.setText(R.string.no_internet_connection);
112 | }
113 |
114 | }
115 |
116 | @Override
117 | public Loader> onCreateLoader(int i, Bundle bundle) {
118 | // Create a new loader for the given URL
119 | return new ArticleLoader(this, GUARDIAN_REQUEST_URL);
120 | }
121 |
122 | @Override
123 | public void onLoadFinished(Loader> loader, List articles) {
124 | // Hide loading indicator because the data has been loaded
125 | View loadingIndicator = (View) findViewById(R.id.loading_indicator);
126 | loadingIndicator.setVisibility(View.GONE);
127 |
128 | // Set empty state text to display "No articles found."
129 | mEmptyStateTextView.setText(R.string.no_articles);
130 |
131 | // Clear the adapter of previous article data
132 | mAdapter.clear();
133 |
134 | // If there is a valid list of {@link Article}s, then add them to the adapter's
135 | // data set. This will trigger the ListView to update.
136 | if (articles != null && !articles.isEmpty()) {
137 | mAdapter.addAll(articles);
138 | }
139 | }
140 |
141 | @Override
142 | public void onLoaderReset(Loader> loader) {
143 | // Loader reset, so we can clear out our existing data.
144 | mAdapter.clear();
145 | }
146 |
147 | }
148 |
--------------------------------------------------------------------------------
/app/src/main/java/com/botsone/android/technews/QueryUtils.java:
--------------------------------------------------------------------------------
1 | package com.botsone.android.technews;
2 |
3 | import android.text.TextUtils;
4 | import android.util.Log;
5 |
6 | import org.json.JSONArray;
7 | import org.json.JSONException;
8 | import org.json.JSONObject;
9 |
10 | import java.io.BufferedReader;
11 | import java.io.IOException;
12 | import java.io.InputStream;
13 | import java.io.InputStreamReader;
14 | import java.net.HttpURLConnection;
15 | import java.net.MalformedURLException;
16 | import java.net.URL;
17 | import java.nio.charset.Charset;
18 | import java.util.ArrayList;
19 | import java.util.List;
20 |
21 | import static com.botsone.android.technews.ArticleActivity.LOG_TAG;
22 |
23 | /**
24 | * Created by bots on 2/18/18.
25 | */
26 |
27 | public final class QueryUtils {
28 |
29 | private QueryUtils() {
30 | }
31 |
32 | /**
33 | * Return a list of {@link Article} objects that has been built up from
34 | * parsing a JSON response.
35 | */
36 |
37 | private static List extractFeatureFromJson(String articleJson) {
38 |
39 | //if the json string is empty or null then return early
40 | if (TextUtils.isEmpty(articleJson)) {
41 | return null;
42 | }
43 |
44 | // Create an empty ArrayList to add articles to
45 | List articles = new ArrayList<>();
46 |
47 | // Try to parse the JSON response string. If there's a problem with the way the JSON
48 | // is formatted, a JSONException exception object will be thrown.
49 | // Catch the exception so the app doesn't crash, and print the error message to the logs.
50 | try {
51 |
52 | // Create a JSONObject from the JSON response string
53 | JSONObject baseJsonResponse = new JSONObject(articleJson);
54 |
55 | // Get the response object
56 | JSONObject response = baseJsonResponse.getJSONObject("response");
57 |
58 | // Get the results array
59 | JSONArray results = response.getJSONArray("results");
60 |
61 | for (int i = 0; i < results.length(); i++) {
62 |
63 | // Get the current article
64 | JSONObject currentArticle = results.getJSONObject(i);
65 |
66 | // Get the section ID
67 | String section = currentArticle.getString("sectionName");
68 |
69 | // Get the publication date
70 | String date = currentArticle.getString("webPublicationDate");
71 |
72 | // Get the title of the article
73 | String title = currentArticle.getString("webTitle");
74 |
75 | // Get the URL of the article so we can open it in a browser
76 | String url = currentArticle.getString("webUrl");
77 |
78 | // Get the fields object
79 | JSONObject fields = currentArticle.getJSONObject("fields");
80 |
81 | // Get the thumbnail image url
82 | String thumbnail = fields.getString("thumbnail");
83 |
84 | // Get the Author of the article
85 | String author = fields.getString("byline");
86 |
87 | // Get the body of the article
88 | String body = fields.getString("body");
89 |
90 | Article article = new Article(thumbnail, title, section, author, date, body, url);
91 |
92 | articles.add(article);
93 |
94 | }
95 |
96 | } catch (JSONException e) {
97 |
98 | // If an error is thrown when executing any of the above statements in the "try" block,
99 | // catch the exception here, so the app doesn't crash. Print a log message
100 | // with the message from the exception.
101 | Log.e("QueryUtils", "Problem parsing the article JSON results", e);
102 | }
103 | return articles;
104 | }
105 |
106 | /**
107 | * Returns new URL object from the given string URL.
108 | */
109 | private static URL createUrl(String stringUrl) {
110 | URL url = null;
111 | try {
112 | url = new URL(stringUrl);
113 | } catch (MalformedURLException e) {
114 | Log.e(LOG_TAG, "Problem building the URL", e);
115 | }
116 | return url;
117 | }
118 |
119 | /**
120 | * Returns new URL object from the given string URL.
121 | */
122 | private static String makeHttpRequest(URL url) throws IOException {
123 | String jsonResponse = "";
124 |
125 | if (url == null) {
126 | return jsonResponse;
127 | }
128 |
129 | HttpURLConnection urlConnection = null;
130 | InputStream inputStream = null;
131 |
132 | try {
133 | urlConnection = (HttpURLConnection) url.openConnection();
134 | urlConnection.setReadTimeout(10000 /* milliseconds */);
135 | urlConnection.setConnectTimeout(15000 /* milliseconds */);
136 | urlConnection.setRequestMethod("GET");
137 | urlConnection.connect();
138 |
139 | if (urlConnection.getResponseCode() == 200) {
140 | inputStream = urlConnection.getInputStream();
141 | jsonResponse = readFromStream(inputStream);
142 | } else {
143 | Log.e(LOG_TAG, "ERROR Response Code" + urlConnection.getResponseCode());
144 | }
145 | } catch (IOException e) {
146 | Log.e(LOG_TAG, "Problem retrieving the article JSON results.", e);
147 | } finally {
148 | if (urlConnection == null) {
149 | urlConnection.disconnect();
150 | }
151 | if (inputStream != null) {
152 | // Closing the input stream could throw an IOException, which is why
153 | // the makeHttpRequest(URL url) method signature specifies than an IOException
154 | // could be thrown.
155 | inputStream.close();
156 | }
157 | }
158 | return jsonResponse;
159 | }
160 |
161 | /**
162 | * Convert the {@link InputStream} into a String which contains the
163 | * whole JSON response from the server.
164 | */
165 | private static String readFromStream(InputStream inputStream) throws IOException {
166 | StringBuilder output = new StringBuilder();
167 | if (inputStream != null) {
168 | InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
169 | BufferedReader reader = new BufferedReader(inputStreamReader);
170 | String line = reader.readLine();
171 | while (line != null) {
172 | output.append(line);
173 | line = reader.readLine();
174 | }
175 | }
176 | return output.toString();
177 | }
178 |
179 | /**
180 | * Query the Guardian dataset and return a list of {@link Article} objects.
181 | */
182 | public static List fetchArticleData(String requestUrl) {
183 |
184 | // Create URL object
185 | URL url = createUrl(requestUrl);
186 |
187 | // Perform HTTP request to the URL and receive a JSON response back
188 | String jsonResponse = null;
189 | try {
190 | jsonResponse = makeHttpRequest(url);
191 | } catch (IOException e) {
192 | Log.e(LOG_TAG, "Problem making the http request", e);
193 | }
194 |
195 | // Extract relevant fields from the JSON response and create a list of {@link Article}s
196 | List articles = extractFeatureFromJson(jsonResponse);
197 |
198 | // Return the list of {@link Article}s
199 | return articles;
200 | }
201 |
202 | }
203 |
--------------------------------------------------------------------------------