18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/gradient_button_remove.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
10 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/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/src/main/res/drawable/gradient_button_add.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
10 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/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/example/android/inventory/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory;
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.example.android.inventory", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_isbn.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
15 |
16 |
17 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_isbn.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Soojeong Shin
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | defaultConfig {
6 | applicationId "com.example.android.inventory"
7 | minSdkVersion 16
8 | targetSdkVersion 27
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:27.1.1'
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 |
29 | implementation 'com.android.support:design:27.1.1'
30 |
31 | implementation 'pub.devrel:easypermissions:0.2.1'
32 |
33 | implementation 'com.jakewharton:butterknife:8.8.1'
34 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
35 |
36 | implementation 'com.android.support:recyclerview-v7:27.1.1'
37 | implementation 'com.android.support:cardview-v7:27.1.1'
38 | }
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/android/inventory/BookLoader.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory;
2 |
3 | import android.content.AsyncTaskLoader;
4 | import android.content.Context;
5 |
6 | import com.example.android.inventory.utils.QueryUtils;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | * Loads a list of books by using an AsyncTask to perform the network request to the given URL.
12 | */
13 |
14 | public class BookLoader extends AsyncTaskLoader> {
15 |
16 | /** Tag for log messages */
17 | private static final String LOG_TAG = BookLoader.class.getName();
18 |
19 | /** Query URL */
20 | private String mUrl;
21 |
22 | /**
23 | * Constructs a new {@link BookLoader}.
24 | *
25 | * @param context of the activity
26 | * @param url to load data from
27 | */
28 | public BookLoader(Context context, String url) {
29 | super(context);
30 | mUrl = url;
31 | }
32 |
33 | @Override
34 | protected void onStartLoading() {
35 | // Trigger the loadInBackground() method to execute.
36 | forceLoad();
37 | }
38 |
39 | /**
40 | * This is on a background thread.
41 | */
42 | @Override
43 | public List loadInBackground() {
44 | if (mUrl == null) {
45 | return null;
46 | }
47 |
48 | // Perform the network request, parse the response, and extract a list of books.
49 | return QueryUtils.fetchBookData(mUrl);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 |
6 |
7 | 16dp
8 |
9 | 8dp
10 | 4dp
11 | 8dp
12 | 2dp
13 |
14 |
15 | 8dp
16 | 72dp
17 | 48dp
18 | 2dp
19 | 100dp
20 | 80dp
21 | 90dp
22 |
23 |
24 | 120dp
25 | 80dp
26 | 5dp
27 |
28 |
29 | 45dp
30 |
31 |
32 | 46dp
33 | 5dp
34 | 2dp
35 | 10dp
36 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/android/inventory/Book.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory;
2 |
3 | /**
4 | * An {@link Book} object contains information related to a single book.
5 | */
6 |
7 | public class Book {
8 |
9 | /** Title of the book */
10 | private String mTitle;
11 |
12 | /** Author of the book */
13 | private String mAuthor;
14 |
15 | /** ISBN, the International Standard Book Number, of the book */
16 | private String mIsbn;
17 |
18 | /** Publisher of the book */
19 | private String mPublisher;
20 |
21 | /**
22 | * Constructs a new{@link Book} object.
23 | * @param title is the title of the book.
24 | * @param author is the author of the book.
25 | * @param isbn is the ISBN of the book.
26 | * @param publisher is the publisher of the book.
27 | */
28 | public Book(String title, String author, String isbn, String publisher) {
29 | mTitle = title;
30 | mAuthor = author;
31 | mIsbn = isbn;
32 | mPublisher = publisher;
33 | }
34 |
35 | /**
36 | * Returns the title of the book.
37 | */
38 | public String getBookTitle() {
39 | return mTitle;
40 | }
41 |
42 | /**
43 | * Returns the author of the book.
44 | */
45 | public String getAuthor() {
46 | return mAuthor;
47 | }
48 |
49 | /**
50 | * Returns the ISBN of the book.
51 | */
52 | public String getIsbn() {
53 | return mIsbn;
54 | }
55 |
56 | /**
57 | * Returns the publisher of the book.
58 | */
59 | public String getPublisher() {
60 | return mPublisher;
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/android/inventory/utils/Constants.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory.utils;
2 |
3 | /**
4 | * Store constants for the inventory app.
5 | */
6 |
7 | public class Constants {
8 |
9 | /**
10 | * Creates a private constructor because no one should ever create a {@link Constants} object.
11 | */
12 | private Constants() {
13 | }
14 |
15 | /** Extract the key associated with the JSONObject and JSONArray */
16 | static final String JSON_KEY_ITEMS = "items";
17 | static final String JSON_KEY_VOLUME_INFO = "volumeInfo";
18 | static final String JSON_KEY_TITLE = "title";
19 | static final String JSON_KEY_AUTHORS = "authors";
20 | static final String JSON_KEY_INDUSTRY_IDENTIFIERS = "industryIdentifiers";
21 | static final String JSON_KEY_TYPE = "type";
22 | static final String JSON_KEY_ISBN_13 = "ISBN_13";
23 | static final String JSON_KEY_IDENTIFIER = "identifier";
24 | static final String JSON_KEY_PUBLISHER = "publisher";
25 |
26 | /** Read timeout for setting up the HTTP request */
27 | static final int READ_TIMEOUT = 10000; /* milliseconds */
28 |
29 | /** Connect timeout for setting up the HTTP request */
30 | static final int CONNECT_TIMEOUT = 15000; /* milliseconds */
31 |
32 | /** HTTP response code when the request is successful */
33 | static final int SUCCESS_RESPONSE_CODE = 200;
34 |
35 | /** Request method type "GET" for reading information from the server */
36 | static final String REQUEST_METHOD_GET = "GET";
37 |
38 | /** URL for the book data from the Google books data set */
39 | public static final String BOOK_REQUEST_URL = "https://www.googleapis.com/books/v1/volumes?";
40 |
41 | /** Default number to set the image on the top of the textView */
42 | public static final int DEFAULT_NUMBER = 0;
43 | }
44 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
30 |
31 |
35 |
36 |
37 |
38 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #ab47bc
4 | #790e8b
5 | #ab47bc
6 |
7 |
8 | #66bb6a
9 |
10 |
11 | #338a3e
12 |
13 | #880d0d0f
14 | #885d5d5e
15 | #880f0f10
16 |
17 |
18 | #f3e5f5
19 |
20 |
21 | #ff9800
22 |
23 |
24 | #fafafa
25 |
26 |
27 | #004d40
28 |
29 |
30 | #338a3e
31 |
32 |
33 | #e8f5e9
34 |
35 |
36 | #7986cb
37 | #9e9e9e
38 |
39 |
40 | #494949
41 | #a4a4a4
42 |
43 |
44 | #000a12
45 |
46 |
47 | #b0bec5
48 |
49 |
50 | #2B3D4D
51 |
52 |
53 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Inventory App
2 |
3 | Project as a part of Android Basics Nanodegree at Udacity
4 |
5 | ### Project Overview
6 |
7 | The goal is to design and create the structure of an Inventory App which would allow a store to keep track of its inventory of products.
8 |
9 | ### Features
10 |
11 | * SQLite Database
12 | * SQLiteOpenHelper
13 | * Content Provider
14 | * URI Matcher
15 | * Content Resolver
16 | * Cursor Loader
17 | * CursorRecyclerViewAdapter
18 | * RecyclerView
19 | * CardView
20 | * Google Books API
21 | * JSON Parsing
22 |
23 | ### Screenshots
24 |
25 | 
26 | 
27 | 
28 | 
29 | 
30 | 
31 |
32 | ```
33 | MIT License
34 |
35 | Copyright (c) 2018 Soojeong Shin
36 |
37 | Permission is hereby granted, free of charge, to any person obtaining a copy
38 | of this software and associated documentation files (the "Software"), to deal
39 | in the Software without restriction, including without limitation the rights
40 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
41 | copies of the Software, and to permit persons to whom the Software is
42 | furnished to do so, subject to the following conditions:
43 |
44 | The above copyright notice and this permission notice shall be included in all
45 | copies or substantial portions of the Software.
46 |
47 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
48 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
49 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
50 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
51 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
52 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
53 | SOFTWARE.
54 | ```
55 |
--------------------------------------------------------------------------------
/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 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
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 Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/android/inventory/data/ProductDbHelper.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory.data;
2 |
3 | import android.content.Context;
4 | import android.database.sqlite.SQLiteDatabase;
5 | import android.database.sqlite.SQLiteOpenHelper;
6 |
7 | import com.example.android.inventory.data.ProductContract.ProductEntry;
8 |
9 | /**
10 | * Database helper for Inventory app. Manages database creation and version management.
11 | */
12 | public class ProductDbHelper extends SQLiteOpenHelper {
13 |
14 | /** Name of the database file */
15 | private static final String DATABASE_NAME = "inventory.db";
16 |
17 | /** Database version. If you change the database schema, you must increment the database version. */
18 | private static final int DATABASE_VERSION = 1;
19 |
20 | /**
21 | * Constructs a new instance of {@link ProductDbHelper}.
22 | * @param context of the app
23 | */
24 | ProductDbHelper(Context context) {
25 | super(context, DATABASE_NAME, null, DATABASE_VERSION);
26 | }
27 |
28 | /**
29 | * This is called when the database is created for the first time.
30 | */
31 | @Override
32 | public void onCreate(SQLiteDatabase db) {
33 | // Create a String that contains the SQL statement to create the products table
34 | String SQL_CREATE_PRODUCTS_TABLE = "CREATE TABLE " + ProductEntry.TABLE_NAME + " ("
35 | + ProductEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
36 | + ProductEntry.COLUMN_PRODUCT_NAME + " TEXT NOT NULL, "
37 | + ProductEntry.COLUMN_PRODUCT_AUTHOR + " TEXT NOT NULL, "
38 | + ProductEntry.COLUMN_PRODUCT_PUBLISHER + " TEXT, "
39 | + ProductEntry.COLUMN_PRODUCT_ISBN + " TEXT NOT NULL, "
40 | + ProductEntry.COLUMN_PRODUCT_PRICE + " REAL NOT NULL DEFAULT 0.0, "
41 | + ProductEntry.COLUMN_PRODUCT_QUANTITY + " INTEGER NOT NULL DEFAULT 0, "
42 | + ProductEntry.COLUMN_PRODUCT_IMAGE + " TEXT, "
43 | + ProductEntry.COLUMN_SUPPLIER_NAME + " TEXT NOT NULL, "
44 | + ProductEntry.COLUMN_SUPPLIER_EMAIL + " TEXT, "
45 | + ProductEntry.COLUMN_SUPPLIER_PHONE + " TEXT NOT NULL);";
46 |
47 | // Execute the SQL statement
48 | db.execSQL(SQL_CREATE_PRODUCTS_TABLE);
49 | }
50 |
51 | /**
52 | * This is called when the database needs to be upgraded.
53 | */
54 | @Override
55 | public void onUpgrade(SQLiteDatabase db, int i, int i1) {
56 | // The database is still at version 1, so there's nothing to do be done here.
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
17 |
18 |
19 |
24 |
25 |
34 |
35 |
48 |
49 |
50 |
51 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/android/inventory/EmptyRecyclerView.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory;
2 |
3 | import android.content.Context;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.util.AttributeSet;
6 | import android.view.View;
7 | import android.widget.RelativeLayout;
8 |
9 | /**
10 | * EmptyRecyclerView is RecyclerView subclass that provides empty view support for RecyclerView
11 | * to show or hide an empty view based on whether the adapter provided to the RecyclerView has
12 | * data or not.
13 | */
14 |
15 | public class EmptyRecyclerView extends RecyclerView {
16 |
17 | private View mEmptyView;
18 |
19 | /**
20 | * The AdapterDataObserver calls checkIfEmpty() method every time, and it observes
21 | * an event that changes the content of the adapter
22 | */
23 | final private AdapterDataObserver observer = new AdapterDataObserver() {
24 | @Override
25 | public void onChanged() {
26 | checkIfEmpty();
27 | }
28 |
29 | @Override
30 | public void onItemRangeInserted(int positionStart, int itemCount) {
31 | checkIfEmpty();
32 | }
33 |
34 | @Override
35 | public void onItemRangeRemoved(int positionStart, int itemCount) {
36 | checkIfEmpty();
37 | }
38 | };
39 |
40 | public EmptyRecyclerView(Context context) {
41 | super(context);
42 | }
43 |
44 | public EmptyRecyclerView(Context context, AttributeSet attrs) {
45 | super(context, attrs);
46 | }
47 |
48 | public EmptyRecyclerView(Context context, AttributeSet attrs,
49 | int defStyle) {
50 | super(context, attrs, defStyle);
51 | }
52 |
53 | /**
54 | * Checks if both mEmptyView and adapter are not null.
55 | * Hide or show mEmptyView depending on the size of the data(item count) in the adapter.
56 | */
57 | private void checkIfEmpty() {
58 | // If the item count provided by the adapter is equal to zero, make the empty View visible
59 | // and hide the EmptyRecyclerView.
60 | // Otherwise, hide the empty View and make the EmptyRecyclerView visible.
61 | if (mEmptyView != null && getAdapter() != null) {
62 | final boolean emptyViewVisible =
63 | getAdapter().getItemCount() == 0;
64 | mEmptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
65 | setVisibility(emptyViewVisible ? GONE : VISIBLE);
66 | }
67 | }
68 |
69 | @Override
70 | public void setAdapter(Adapter adapter) {
71 | final Adapter oldAdapter = getAdapter();
72 | if (oldAdapter != null) {
73 | oldAdapter.unregisterAdapterDataObserver(observer);
74 | }
75 | super.setAdapter(adapter);
76 | if (adapter != null) {
77 | adapter.registerAdapterDataObserver(observer);
78 | }
79 |
80 | checkIfEmpty();
81 | }
82 |
83 | /**
84 | * Set an empty layout on the EmptyRecyclerView
85 | * @param relativeLayout refers to the empty state of the relative layout
86 | */
87 | public void setEmptyLayout(RelativeLayout relativeLayout) {
88 | mEmptyView = relativeLayout;
89 | checkIfEmpty();
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/android/inventory/data/ProductContract.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory.data;
2 |
3 | import android.content.ContentResolver;
4 | import android.net.Uri;
5 | import android.provider.BaseColumns;
6 |
7 | /**
8 | * API Contract for the Inventory app.
9 | */
10 |
11 | public final class ProductContract {
12 |
13 | // To prevent someone from accidentally instantiating the contract class,
14 | // give it an empty constructor.
15 | private ProductContract() {
16 | }
17 |
18 | /**
19 | * The "Content authority" is a name for the entire content provider, similar to the
20 | * relationship between a domain name and its website. A convenient string to use for the
21 | * content authority is the package name for the app, which is guaranteed to be unique on the
22 | * device.
23 | */
24 | static final String CONTENT_AUTHORITY = "com.example.android.inventory";
25 |
26 | /**
27 | * Use CONTENT_AUTHORITY to create the base of all URI's which apps will use to contact
28 | * the content provider.
29 | */
30 | private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
31 |
32 | /**
33 | * Possible path (appended to base content URI for possible URI's)
34 | * For instance, content://com.example.android.inventory/products/ is a valid path for
35 | * looking at product data. content://com.example.android.inventory/staff/ will fail,
36 | * as the ContentProvider hasn't been given any information on what to do with "staff".
37 | */
38 | static final String PATH_PRODUCT = "products";
39 |
40 |
41 | /**
42 | * Inner class that defines constant values for the products database table.
43 | * Each entry in the table represents a single product.
44 | */
45 | public static final class ProductEntry implements BaseColumns {
46 |
47 | /** The content URI to access the product data in the provider */
48 | public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_PRODUCT);
49 |
50 | /**
51 | * The MIME type of the {@link #CONTENT_URI} for a list of products.
52 | */
53 | static final String CONTENT_LIST_TYPE =
54 | ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_PRODUCT;
55 |
56 | /**
57 | * The MIME type of the {@link #CONTENT_URI} for a single product.
58 | */
59 | static final String CONTENT_ITEM_TYPE =
60 | ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_PRODUCT;
61 |
62 | /** Name of database table for products */
63 | static final String TABLE_NAME = "products";
64 |
65 | /** Name of the product. Type: TEXT */
66 | public static final String COLUMN_PRODUCT_NAME = "product_name";
67 |
68 | /** Price of the product. Type: INTEGER */
69 | public static final String COLUMN_PRODUCT_PRICE = "price";
70 |
71 | /** Quantity of the product. Type: INTEGER */
72 | public static final String COLUMN_PRODUCT_QUANTITY = "quantity";
73 |
74 | /** Image of the product. */
75 | public static final String COLUMN_PRODUCT_IMAGE = "product_image";
76 |
77 | /** Supplier name. Type: TEXT */
78 | public static final String COLUMN_SUPPLIER_NAME = "supplier_name";
79 |
80 | /** Supplier email. Type: TEXT */
81 | public static final String COLUMN_SUPPLIER_EMAIL = "supplier_email";
82 |
83 | /** Supplier phone number. Type: TEXT */
84 | public static final String COLUMN_SUPPLIER_PHONE= "supplier_phone";
85 |
86 | /** Author of the product. Type: TEXT */
87 | public static final String COLUMN_PRODUCT_AUTHOR = "author";
88 |
89 | /** Publisher of the product. Type: Text */
90 | public static final String COLUMN_PRODUCT_PUBLISHER = "publisher";
91 |
92 | /** ISBN of the product. Type: Text */
93 | public static final String COLUMN_PRODUCT_ISBN = "isbn";
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/card_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
17 |
18 |
23 |
24 |
31 |
32 |
38 |
39 |
45 |
46 |
50 |
51 |
58 |
59 |
65 |
66 |
67 |
68 |
69 |
70 |
78 |
79 |
86 |
87 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
16 |
17 |
22 |
23 |
30 |
31 |
32 |
42 |
43 |
44 |
57 |
58 |
65 |
66 |
75 |
76 |
83 |
84 |
90 |
91 |
96 |
97 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/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/example/android/inventory/activity/IsbnActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory.activity;
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.v7.app.AppCompatActivity;
12 | import android.view.MenuItem;
13 | import android.view.View;
14 | import android.widget.TextView;
15 |
16 | import com.example.android.inventory.Book;
17 | import com.example.android.inventory.BookLoader;
18 | import com.example.android.inventory.R;
19 | import com.example.android.inventory.utils.Constants;
20 |
21 | import java.util.List;
22 |
23 | import butterknife.BindView;
24 | import butterknife.ButterKnife;
25 |
26 | /**
27 | * IsbnActivity implements the LoaderManager.LoaderCallbacks interface in order for Activity to be a
28 | * client that interacts with the LoaderManager.
29 | */
30 |
31 | public class IsbnActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks> {
32 |
33 | /**Constant value for the book loader ID */
34 | private static final int BOOK_LOADER_ID = 1;
35 |
36 | /** TextView that is displayed when there is no data or when there is no internet connectivity */
37 | @BindView(R.id.empty_isbn_view) TextView mEmptyTextView;
38 |
39 | /** Loading indicator that is displayed before the first load is completed */
40 | @BindView(R.id.loading_indicator) View mLoadingIndicator;
41 |
42 | @Override
43 | protected void onCreate(Bundle savedInstanceState) {
44 | super.onCreate(savedInstanceState);
45 | setContentView(R.layout.activity_isbn);
46 |
47 | // Bind the view using ButterKnife
48 | ButterKnife.bind(this);
49 |
50 | // Check for network connectivity and initialize the loader
51 | initializeLoader(isConnected());
52 |
53 | // Navigate with the app icon in the app bar
54 | getSupportActionBar().setDisplayHomeAsUpEnabled(true);
55 | getSupportActionBar().setDisplayShowHomeEnabled(true);
56 | }
57 |
58 | @Override
59 | public Loader> onCreateLoader(int i, Bundle bundle) {
60 | // Parse breaks apart the URI string that is passed into its parameter
61 | Uri baseUri = Uri.parse(Constants.BOOK_REQUEST_URL);
62 |
63 | // buildUpon prepares the baseUri that we just parsed so we can add query parameters to it
64 | Uri.Builder uriBuilder = baseUri.buildUpon();
65 |
66 | // Since starting this activity with data, using getIntent to retrieve this data.
67 | Intent intent = getIntent();
68 |
69 | // Get String ISBN from the user input in the isbn dialog of the MainActivity
70 | String isbnStringFromDialog = intent.getStringExtra(getString(R.string.isbn_in_a_dialog));
71 |
72 | // To query a book by ISBN, use "isbn:"
73 | isbnStringFromDialog = getString(R.string.query_isbn) + isbnStringFromDialog;
74 |
75 | // Append query parameter and its value. (e.g. the 'q=isbn:9780553902808')
76 | uriBuilder.appendQueryParameter(getString(R.string.q), isbnStringFromDialog);
77 |
78 | // Create a new loader for the given URL
79 | return new BookLoader(this, uriBuilder.toString());
80 | }
81 |
82 | @Override
83 | public void onLoadFinished(Loader> loader, List booksData) {
84 | // Hide loading indicator because the data has been loaded
85 | mLoadingIndicator.setVisibility(View.GONE);
86 |
87 |
88 |
89 | // If there is a valid list of {@link Book}, actually in this app there will be one list item,
90 | // then send the data to the EditorActivity.
91 | if (booksData != null && !booksData.isEmpty()) {
92 | // Get the first list item
93 | Book book = booksData.get(0);
94 | // Get the title, author, ISBN, publisher of the book
95 | String title = book.getBookTitle();
96 | String author = book.getAuthor();
97 | String isbn = book.getIsbn();
98 | String publisher = book.getPublisher();
99 |
100 | // Create a new intent to open the {@link EditorActivity}
101 | Intent intent = new Intent(this, EditorActivity.class);
102 | // Send the data
103 | intent.putExtra(getString(R.string.title), title);
104 | intent.putExtra(getString(R.string.author), author);
105 | intent.putExtra(getString(R.string.isbn), isbn);
106 | intent.putExtra(getString(R.string.publisher), publisher);
107 | // start the new activity
108 | startActivity(intent);
109 | } else {
110 | // Set empty text to display "No matches found.
111 | // An ISBN is usually found on the back cover, near the barcode."
112 | mEmptyTextView.setText(getString(R.string.no_matches_found));
113 | mEmptyTextView.setTextColor(getResources().getColor(R.color.color_no_matches_found_text));
114 | }
115 | }
116 |
117 | @Override
118 | public void onLoaderReset(Loader> loader) {
119 |
120 | }
121 |
122 | /**
123 | * Check for network connectivity.
124 | */
125 | private boolean isConnected() {
126 | // Get a reference to the ConnectivityManager to check state of network connectivity
127 | ConnectivityManager connectivityManager =
128 | (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
129 |
130 | // Get details on the currently active default data network
131 | NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
132 |
133 | return (networkInfo != null && networkInfo.isConnected());
134 | }
135 |
136 |
137 | /**
138 | * If there is internet connectivity, initialize the loader as usual.
139 | * Otherwise, hide loading indicator and set empty state TextView to display
140 | * "You are offline. Please check your Internet connection."
141 | *
142 | * @param isConnected internet connection is available or not
143 | */
144 | private void initializeLoader(boolean isConnected) {
145 | if (isConnected) {
146 | // Get a reference to the LoaderManager, in order to interact with loaders.
147 | LoaderManager loaderManager = this.getLoaderManager();
148 | // Initialize the loader with the BOOK_LOADER_ID
149 | loaderManager.initLoader(BOOK_LOADER_ID, null, this);
150 | } else {
151 | // Otherwise, display error.
152 | // First, hide loading indicator so error message will be visible
153 | mLoadingIndicator.setVisibility(View.GONE);
154 | // Set empty text to display no connection error message
155 | mEmptyTextView.setText(getString(R.string.no_internet_connection));
156 | mEmptyTextView.setTextColor(getResources().getColor(R.color.color_grey_text));
157 | mEmptyTextView.setCompoundDrawablesWithIntrinsicBounds(Constants.DEFAULT_NUMBER,
158 | R.drawable.ic_network_check,Constants.DEFAULT_NUMBER,Constants.DEFAULT_NUMBER);
159 | }
160 | }
161 |
162 | // Go back to the MainActivity when up button in app bar is clicked on.
163 | @Override
164 | public boolean onOptionsItemSelected(MenuItem item) {
165 | switch (item.getItemId()) {
166 | case android.R.id.home:
167 | finish();
168 | return true;
169 | }
170 | return super.onOptionsItemSelected(item);
171 | }
172 | }
173 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_editor.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
20 |
21 |
29 |
30 |
31 |
32 |
37 |
38 |
46 |
47 |
48 |
51 |
52 |
56 |
57 |
64 |
65 |
72 |
73 |
74 |
81 |
82 |
89 |
90 |
91 |
92 |
93 |
94 |
102 |
103 |
116 |
117 |
118 |
119 |
123 |
124 |
132 |
133 |
134 |
139 |
140 |
147 |
148 |
149 |
160 |
161 |
166 |
167 |
174 |
175 |
176 |
180 |
181 |
188 |
189 |
190 |
195 |
196 |
203 |
204 |
205 |
206 |
207 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/android/inventory/ProductCursorAdapter.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory;
2 |
3 | import android.content.ContentUris;
4 | import android.content.ContentValues;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.database.Cursor;
8 | import android.net.Uri;
9 | import android.support.annotation.NonNull;
10 | import android.support.v7.widget.CardView;
11 | import android.support.v7.widget.RecyclerView;
12 | import android.view.LayoutInflater;
13 | import android.view.View;
14 | import android.view.ViewGroup;
15 | import android.widget.Button;
16 | import android.widget.TextView;
17 | import android.widget.Toast;
18 |
19 | import com.example.android.inventory.activity.DetailActivity;
20 | import com.example.android.inventory.data.ProductContract.ProductEntry;
21 |
22 | /**
23 | * {@link ProductCursorAdapter} is an adapter for a recycler view
24 | * that uses a {@link Cursor} of product data as its data source. This adapter knows
25 | * how to create card items for each row of product data in the {@link Cursor}.
26 | */
27 | public class ProductCursorAdapter extends RecyclerView.Adapter {
28 |
29 | // Declare variables for the Cursor that holds product data
30 | private Cursor mCursor;
31 | private Context mContext;
32 |
33 | /**
34 | * Constructor for the ProductCursorAdapter that initializes the Context.
35 | *
36 | * @param context Context of the app
37 | */
38 | public ProductCursorAdapter(Context context) {
39 | mContext = context;
40 | }
41 |
42 | /**
43 | * Called when ViewHolders are created to fill a RecyclerView.
44 | *
45 | * @return A new ProductViewHolder that holds the view for each product
46 | */
47 | @NonNull
48 | @Override
49 | public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
50 | View v = LayoutInflater.from(mContext).inflate(R.layout.card_item, parent, false);
51 | return new ProductViewHolder(v);
52 | }
53 |
54 | /**
55 | * Called by the RecyclerView to display data at a specified position in the Cursor.
56 | *
57 | * @param viewHolder The ViewHolder to bind Cursor data to
58 | * @param position The position of the data in the Cursor
59 | */
60 | @Override
61 | public void onBindViewHolder(@NonNull ProductViewHolder viewHolder, int position) {
62 | final ProductViewHolder holder = viewHolder;
63 | mCursor.moveToPosition(position); // Get to the right location in the cursor
64 | // Set data
65 | holder.setData(mCursor);
66 | // Indices for _id
67 | final long id = mCursor.getLong(mCursor.getColumnIndex(ProductEntry._ID));
68 | // Set an OnClickListener to open a DetailActivity
69 | holder.cardView.setOnClickListener(new View.OnClickListener() {
70 | @Override
71 | public void onClick(View view) {
72 | // Create a new intent to go to {@link DetailActivity}
73 | Intent intent = new Intent(mContext, DetailActivity.class);
74 |
75 | // Form the content URI that represents the specific product that was clicked on,
76 | // by appending the "id" (passed as input to this method) onto the
77 | // {@link ProductEntry#CONTENT_URI}.
78 | // For example, the URI would be "content://com.example.android.inventory/products/2"
79 | // if the product with ID 2 was clicked on.
80 | Uri currentProductUri = ContentUris.withAppendedId(ProductEntry.CONTENT_URI, id);
81 |
82 | // Set the URI on the data field of the intent
83 | intent.setData(currentProductUri);
84 |
85 | // Launch the {@link DetailActivity} to display the data for the current product.
86 | mContext.startActivity(intent);
87 | }
88 | });
89 |
90 | // Find the columns of product quantity
91 | int quantityColumnIndex = mCursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_QUANTITY);
92 | // Read the product quantity from the Cursor for the current product
93 | int quantity = mCursor.getInt(quantityColumnIndex);
94 |
95 | // If the quantity is more than 0, set the text of a sale button to display 'sell'.
96 | // Otherwise, set the text of a sale button to display 'sold out'.
97 | if (quantity > 0) {
98 | holder.saleButton.setText(mContext.getString(R.string.sell));
99 | } else{
100 | holder.saleButton.setText(mContext.getString(R.string.sold_out));
101 | }
102 |
103 | //Set OnClickListener on the sale button. We can decrement the available quantity by one.
104 | holder.saleButton.setOnClickListener(new View.OnClickListener() {
105 | @Override
106 | public void onClick(View view) {
107 | // Read from text fields
108 | String quantityString = holder.quantityTextView.getText().toString().trim();
109 |
110 | // Parse the string into an Integer value.
111 | int quantity = Integer.parseInt(quantityString);
112 | // If the quantity is more than 0, decrement the quantity by 1.
113 | // If quantity is 0, show a toast message.
114 | if (quantity > 0) {
115 | // Set the text of a sale button to display 'sell'.
116 | holder.saleButton.setText(mContext.getString(R.string.sell));
117 | quantity = quantity - 1;
118 | } else if (quantity == 0) {
119 | // Set the text of a sale button to display 'sold out'
120 | holder.saleButton.setText(mContext.getString(R.string.sold_out));
121 | Toast.makeText(view.getContext(),
122 | view.getContext().getString(R.string.detail_update_zero_quantity),
123 | Toast.LENGTH_SHORT).show();
124 | }
125 |
126 | // Create a ContentValues object where column names are the keys,
127 | // and product attributes from the textView fields are the values.
128 | ContentValues values = new ContentValues();
129 | values.put(ProductEntry.COLUMN_PRODUCT_QUANTITY, quantity);
130 |
131 | // Update the product with content URI: mCurrentProductUri
132 | // and pass in the new ContentValues. Pass in null for the selection and selection args
133 | // because mCurrentProductUri will already identify the correct row in the database that
134 | // we want to modify.
135 | Uri mCurrentProductUri = ContentUris.withAppendedId(ProductEntry.CONTENT_URI, id);
136 | int rowsAffected = view.getContext().getContentResolver().update(mCurrentProductUri, values,
137 | null, null);
138 | }
139 | });
140 | }
141 |
142 | /**
143 | * Returns the number of items to display
144 | */
145 | @Override
146 | public int getItemCount() {
147 | if (mCursor == null) {
148 | return 0;
149 | }
150 | return mCursor.getCount();
151 | }
152 |
153 | /**
154 | * When data changes and a re-query occurs, this function swaps the old Cursor
155 | * with a newly updated Cursor (Cursor c) that is passed in.
156 | */
157 | public Cursor swapCursor(Cursor cursor) {
158 | // Check if this cursor is the same as the previous cursor (mCursor)
159 | if (mCursor == cursor) {
160 | return null;
161 | }
162 | Cursor temp = mCursor;
163 | this.mCursor = cursor; // new cursor value assigned
164 |
165 | // Check if this is a valid cursor, then update the cursor
166 | if (cursor != null) {
167 | this.notifyDataSetChanged();
168 | }
169 | return temp;
170 | }
171 |
172 | // Inner class for creating ViewHolders
173 | class ProductViewHolder extends RecyclerView.ViewHolder {
174 |
175 | private TextView productNameTextView;
176 | private TextView authorTextView;
177 | private TextView priceTextView;
178 | private TextView quantityTextView;
179 | private Button saleButton;
180 | private CardView cardView;
181 |
182 | /**
183 | * Constructor for the ProductViewHolders.
184 | *
185 | * @param itemView The view inflated in onCreateViewHolder
186 | */
187 | ProductViewHolder(View itemView) {
188 | super(itemView);
189 |
190 | // Find individual views that we want to modify in the card item layout
191 | productNameTextView = itemView.findViewById(R.id.product_name_card);
192 | authorTextView = itemView.findViewById(R.id.product_author_card);
193 | priceTextView =itemView.findViewById(R.id.product_price_card);
194 | quantityTextView =itemView.findViewById(R.id.product_quantity_card);
195 | saleButton = itemView.findViewById(R.id.product_sale_button_card);
196 | cardView = itemView.findViewById(R.id.card_view);
197 | }
198 |
199 | /**
200 | * Find the columns of product attributes that we're interested in, then
201 | * read the product attributes from the Cursor for the current product.
202 | * Update the TextViews with the attributes for the current product
203 | * @param c The cursor from which to get the data.
204 | */
205 | public void setData(Cursor c) {
206 | productNameTextView.setText(c.getString(c.getColumnIndex(ProductEntry.COLUMN_PRODUCT_NAME)));
207 | authorTextView.setText(c.getString(c.getColumnIndex(ProductEntry.COLUMN_PRODUCT_AUTHOR)));
208 | priceTextView.setText(String.valueOf(c.getDouble(c.getColumnIndex(ProductEntry.COLUMN_PRODUCT_PRICE))));
209 | quantityTextView.setText(String.valueOf(c.getInt(c.getColumnIndex(ProductEntry.COLUMN_PRODUCT_QUANTITY))));
210 | }
211 | }
212 | }
213 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/android/inventory/utils/QueryUtils.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory.utils;
2 |
3 | import android.text.TextUtils;
4 | import android.util.Log;
5 |
6 | import com.example.android.inventory.Book;
7 |
8 | import org.json.JSONArray;
9 | import org.json.JSONException;
10 | import org.json.JSONObject;
11 |
12 | import java.io.BufferedReader;
13 | import java.io.IOException;
14 | import java.io.InputStream;
15 | import java.io.InputStreamReader;
16 | import java.net.HttpURLConnection;
17 | import java.net.MalformedURLException;
18 | import java.net.URL;
19 | import java.nio.charset.Charset;
20 | import java.util.ArrayList;
21 | import java.util.List;
22 |
23 | /**
24 | * Helper methods related to requesting and receiving book data from the Google Books.
25 | */
26 |
27 | public class QueryUtils {
28 |
29 | /** Tag for the log messages */
30 | private static final String LOG_TAG = QueryUtils.class.getSimpleName();
31 |
32 | /**
33 | * Create a private constructor because no one should ever create a {@link QueryUtils} object.
34 | */
35 | private QueryUtils() {
36 | }
37 |
38 | /**
39 | * Query the Google Books data set and return a list of {@link Book} objects.
40 | */
41 | public static List fetchBookData(String requestUrl) {
42 | // Create URL object
43 | URL url = createUrl(requestUrl);
44 |
45 | // Perform HTTP request to the URL and receive a JSON response back
46 | String jsonResponse = null;
47 | try {
48 | jsonResponse = makeHttpRequest(url);
49 | } catch (IOException e) {
50 | Log.e(LOG_TAG, "Problem making the HTTP request.", e);
51 | }
52 |
53 | // Extract relevant fields from the JSON response and create a list of {@link Book}s
54 | List books = extractFeatureFromJSON(jsonResponse);
55 |
56 | // Return the list of {@link Book}s
57 | return books;
58 | }
59 |
60 | /**
61 | * Returns new URL object from the given string URL.
62 | */
63 | private static URL createUrl(String stringUrl) {
64 | URL url = null;
65 | try {
66 | url = new URL(stringUrl);
67 | } catch (MalformedURLException e) {
68 | Log.e(LOG_TAG, "Problem building the URL.", e);
69 | }
70 | return url;
71 | }
72 |
73 | /**
74 | * Make an HTTP request to the given URL and return a String as the response.
75 | */
76 | private static String makeHttpRequest(URL url) throws IOException {
77 | String jsonResponse = "";
78 |
79 | // If the URL is null, then return early.
80 | if (url == null) {
81 | return jsonResponse;
82 | }
83 |
84 | HttpURLConnection urlConnection = null;
85 | InputStream inputStream = null;
86 | try {
87 | urlConnection = (HttpURLConnection) url.openConnection();
88 | urlConnection.setReadTimeout(Constants.READ_TIMEOUT /* milliseconds */);
89 | urlConnection.setConnectTimeout(Constants.CONNECT_TIMEOUT /* milliseconds */);
90 | urlConnection.setRequestMethod(Constants.REQUEST_METHOD_GET);
91 | urlConnection.connect();
92 |
93 | // If the request was successful (response code 200),
94 | // then read the input stream and parse the response.
95 | if (urlConnection.getResponseCode() == Constants.SUCCESS_RESPONSE_CODE) {
96 | inputStream = urlConnection.getInputStream();
97 | jsonResponse = readFromStream(inputStream);
98 | } else {
99 | Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
100 | }
101 | } catch (IOException e) {
102 | Log.e(LOG_TAG, "Problem retrieving the book JSON results.", e);
103 | } finally {
104 | if (urlConnection != null) {
105 | urlConnection.disconnect();
106 | }
107 | if (inputStream != null) {
108 | // Closing the input stream could throw an IOException, which is why
109 | // the makeHttpRequest(URL url) method signature specifies that an IOException
110 | // could be thrown.
111 | inputStream.close();
112 | }
113 | }
114 | return jsonResponse;
115 | }
116 |
117 | /**
118 | * Convert the {@link InputStream} into a String which contains the
119 | * whole JSON response from the server.
120 | */
121 | private static String readFromStream(InputStream inputStream) throws IOException {
122 | StringBuilder output = new StringBuilder();
123 | if (inputStream != null) {
124 | InputStreamReader inputStreamReader = new InputStreamReader(inputStream,
125 | Charset.forName("UTF-8"));
126 | BufferedReader reader = new BufferedReader(inputStreamReader);
127 | String line = reader.readLine();
128 | while (line != null) {
129 | output.append(line);
130 | line = reader.readLine();
131 | }
132 | }
133 | return output.toString();
134 | }
135 |
136 | /**
137 | * Return a list of {@link Book}s objects that has been built up from
138 | * parsing the given JSON response. In this app, returns only {@link Book} that matches ISBN
139 | * number.
140 | */
141 | private static List extractFeatureFromJSON(String bookJSON) {
142 | // If the JSON string is empty or null, then return early.
143 | if (TextUtils.isEmpty(bookJSON)) {
144 | return null;
145 | }
146 |
147 | // Create an empty ArrayList that we can start adding books to
148 | List bookList = new ArrayList<>();
149 |
150 | // Try to parse the JSON response string. If there's a problem with the way the JSON
151 | // is formatted, a JSONException exception object will be thrown.
152 | try {
153 | // Create a JSONObject from the JSON response string
154 | JSONObject baseJsonResponse = new JSONObject(bookJSON);
155 | if (baseJsonResponse.has(Constants.JSON_KEY_ITEMS)) {
156 | // Extract the JSONArray associated with the key called "items"
157 | JSONArray bookArray = baseJsonResponse.getJSONArray(Constants.JSON_KEY_ITEMS);
158 |
159 | // Extract the first JSONObject in the bookArray
160 | JSONObject firstItemObject = bookArray.getJSONObject(0);
161 |
162 | // Extract the JSONObject associated with the key called "volumeInfo"
163 | JSONObject volumeInfoObject = firstItemObject.getJSONObject(Constants.JSON_KEY_VOLUME_INFO);
164 |
165 | // For a given book, extract the value for the key called "title"
166 | String title = volumeInfoObject.getString(Constants.JSON_KEY_TITLE);
167 |
168 | // For a given book, extract the JSONArray associated with the key called "authors"
169 | JSONArray authorsArray = volumeInfoObject.getJSONArray(Constants.JSON_KEY_AUTHORS);
170 |
171 | // For a given book, if there are authors, extract the value in the first
172 | String author = null;
173 | if (authorsArray.length() != 0) {
174 | author = authorsArray.getString(0);
175 | }
176 |
177 | // For a given book, extract the JSONArray associated with the key called "industryIdentifiers"
178 | JSONArray industryIdentifiersArray =
179 | volumeInfoObject.getJSONArray(Constants.JSON_KEY_INDUSTRY_IDENTIFIERS);
180 | String isbn = null;
181 | // If there is element in the JSONArray, for each element create a JSONObject.
182 | if (industryIdentifiersArray.length() != 0) {
183 | for (int i = 0; i < industryIdentifiersArray.length(); i++) {
184 | JSONObject currentObject = industryIdentifiersArray.getJSONObject(i);
185 | if (currentObject.has(Constants.JSON_KEY_TYPE)) {
186 | // Extract the value for the key "type" and the value will be "ISBN_13" or
187 | // "ISBN_10".
188 | String isbnType = currentObject.getString(Constants.JSON_KEY_TYPE);
189 | // If the value for the key "type" is "ISBN_13", extract the value for the
190 | // key "identifier"
191 | if (isbnType.equals(Constants.JSON_KEY_ISBN_13)) {
192 | isbn = currentObject.getString(Constants.JSON_KEY_IDENTIFIER);
193 | }
194 | }
195 | }
196 | }
197 |
198 | // For a given book, if it contains the key called "publisher", extract the value for the
199 | // key called "publisher"
200 | String publisher = null;
201 | if (volumeInfoObject.has(Constants.JSON_KEY_PUBLISHER)) {
202 | publisher = volumeInfoObject.getString(Constants.JSON_KEY_PUBLISHER);
203 | }
204 |
205 | // Create a new {@link Book} object with the title, author, ISBN, and publisher from
206 | // the JSON response.
207 | Book book = new Book(title, author, isbn, publisher);
208 |
209 | // Add the new {@link Book} to the list of books.
210 | bookList.add(book);
211 | }
212 | } catch (JSONException e) {
213 | // If an error is thrown when executing any of the above statements in the "try" block,
214 | // catch the exception here, so the app doesn't crash. Print a log message
215 | // with the message from the exception.
216 | Log.e(LOG_TAG, "Problem parsing the book JSON results", e);
217 | }
218 |
219 | // Returns the list of books. In this app, since searching for the book that matches the ISBN number,
220 | // returns only one book that matches the ISBN number.
221 | return bookList;
222 | }
223 | }
224 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Inventory
3 |
4 |
5 | Product Details
6 |
7 |
8 | Edit
9 |
10 |
11 | Save
12 |
13 |
14 | Delete
15 |
16 |
17 | Insert Dummy Data
18 |
19 |
20 | Delete All Products
21 |
22 |
23 | Add a Book
24 |
25 |
26 | Edit Book
27 |
28 |
29 | Product Name
30 |
31 |
32 | Title
33 |
34 |
35 | Author
36 |
37 |
38 | Publisher
39 |
40 |
41 | ISBN
42 |
43 |
44 | Price
45 |
46 |
47 | Quantity
48 |
49 |
50 | Supplier Name
51 |
52 |
53 | Supplier Email
54 |
55 |
56 | Supplier Phone
57 |
58 |
59 | Name
60 |
61 |
62 | Email
63 |
64 |
65 | Phone
66 |
67 |
68 | Add image
69 |
70 |
71 | 0 is the lowest amount
72 |
73 |
74 | Product saved
75 |
76 |
77 | Error with saving product
78 |
79 |
80 | Product updated
81 |
82 |
83 | Error with updating product
84 |
85 |
86 | It\'s a bit quite in here…
87 |
88 |
89 | Click + button in the bottom right corner to add a new product
90 |
91 |
92 | Discard your changes and quit editing?
93 |
94 |
95 | Discard
96 |
97 |
98 | Keep Editing
99 |
100 |
101 | Product deleted
102 |
103 |
104 | Error with deleting product
105 |
106 |
107 | Delete this product?
108 |
109 |
110 | Delete
111 |
112 |
113 | Cancel
114 |
115 |
116 | Delete all products?
117 |
118 |
119 | Add a book by an ISBN?
120 |
121 |
122 | Enter\nan ISBN
123 |
124 |
125 | Add\nmanually
126 |
127 |
128 | Order for more
129 | I\'d like to place an order for
130 | .
131 | ( ) copies of
132 | Best,
133 | :
134 | Compose email
135 | mailto:
136 | \n
137 | \n\n
138 |
139 |
140 | product image
141 | email image
142 | phone image
143 |
144 |
145 | Supplier
146 |
147 |
148 | Permission granted
149 |
150 | Permission denied
151 |
152 |
153 | Title cannot be empty.
154 | Author cannot be empty.
155 | ISBN cannot be empty
156 | Price cannot be empty
157 | Quantity cannot be empty
158 | Supplier name cannot be empty
159 | Supplier phone number cannot be empty
160 |
161 |
162 | Enter 13-digit ISBN
163 |
164 |
165 | Please enter a valid title.
166 | Please enter a valid author.
167 | Please enter a valid ISBN.
168 | Please enter a valid price.
169 | Please enter a valid quantity.
170 | Please enter a valid supplier name.
171 | Please enter a valid supplier phone number.
172 |
173 |
174 | sell
175 | sold out
176 |
177 |
178 | title
179 | author
180 | isbn
181 | publisher
182 | ISBN in a Dialog
183 |
184 |
185 | Select Picture
186 |
187 | image/*
188 |
189 |
190 | tel:
191 |
192 |
193 | isbn:
194 |
195 | q
196 |
197 | No matches found.\n\nAn ISBN is usually found on the back
198 | cover, near the barcode.
199 | You are offline. Please check your Internet
200 | connection.
201 |
202 |
203 | Make a phone call
204 |
205 | $
206 |
207 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/android/inventory/activity/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory.activity;
2 |
3 | import android.app.AlertDialog;
4 | import android.app.LoaderManager;
5 | import android.content.ContentValues;
6 | import android.content.CursorLoader;
7 | import android.content.DialogInterface;
8 | import android.content.Intent;
9 | import android.content.Loader;
10 | import android.database.Cursor;
11 | import android.net.Uri;
12 | import android.os.Bundle;
13 | import android.support.v7.app.AppCompatActivity;
14 | import android.support.v7.widget.LinearLayoutManager;
15 | import android.view.LayoutInflater;
16 | import android.view.Menu;
17 | import android.view.MenuItem;
18 | import android.view.View;
19 | import android.widget.Button;
20 | import android.widget.EditText;
21 | import android.widget.RelativeLayout;
22 | import android.widget.Toast;
23 |
24 | import com.example.android.inventory.ProductCursorAdapter;
25 | import com.example.android.inventory.EmptyRecyclerView;
26 | import com.example.android.inventory.R;
27 | import com.example.android.inventory.data.ProductContract.ProductEntry;
28 |
29 | /**
30 | * Displays list of products that were entered and stored in the app.
31 | */
32 | public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks{
33 |
34 | /** Identifier for the product data loader */
35 | private static final int PRODUCT_LOADER = 0;
36 |
37 | /** Adapter for the RecyclerView */
38 | private ProductCursorAdapter mCursorAdapter;
39 |
40 | @Override
41 | protected void onCreate(Bundle savedInstanceState) {
42 | super.onCreate(savedInstanceState);
43 | setContentView(R.layout.activity_main);
44 |
45 | // Find a reference to the {@link RecyclerView} in the layout
46 | // Replaced RecyclerView with EmptyRecyclerView
47 | EmptyRecyclerView recyclerView = findViewById(R.id.recycler_view);
48 | LinearLayoutManager layoutManager = new LinearLayoutManager(this);
49 | recyclerView.setHasFixedSize(true);
50 |
51 | // Set the layoutManager on the {@link RecyclerView}
52 | recyclerView.setLayoutManager(layoutManager);
53 |
54 | // Find the empty layout and set it on the new recycler view
55 | RelativeLayout mEmptyLayout = findViewById(R.id.empty_view);
56 | recyclerView.setEmptyLayout(mEmptyLayout);
57 |
58 | // Setup a ProductCursorAdapter to create a card item for each row of product data in the Cursor.
59 | mCursorAdapter = new ProductCursorAdapter(this);
60 | // Set the adapter on the {@link recyclerView}
61 | recyclerView.setAdapter(mCursorAdapter);
62 |
63 | // Kick off the loader
64 | getLoaderManager().initLoader(PRODUCT_LOADER, null, this);
65 | }
66 |
67 | /**
68 | * Helper method to insert hardcoded product data into the database. For debugging purpose only.
69 | */
70 | private void insertDummyProduct() {
71 | // Create a ContentValues object where column names are the keys,
72 | // and product attributes are the values.
73 | ContentValues values = new ContentValues();
74 | values.put(ProductEntry.COLUMN_PRODUCT_NAME, "The Little Prince");
75 | values.put(ProductEntry.COLUMN_PRODUCT_AUTHOR, "Antoine de Saint-Exupéry");
76 | values.put(ProductEntry.COLUMN_PRODUCT_PUBLISHER, "Houghton Mifflin Harcourt");
77 | values.put(ProductEntry.COLUMN_PRODUCT_ISBN, "9780156012195");
78 | values.put(ProductEntry.COLUMN_PRODUCT_PRICE, 6.35);
79 | values.put(ProductEntry.COLUMN_PRODUCT_QUANTITY, 10);
80 | values.put(ProductEntry.COLUMN_SUPPLIER_NAME, "Neho & Becky Supplier");
81 | values.put(ProductEntry.COLUMN_SUPPLIER_EMAIL, "nehoandbecky@gmail.com");
82 | values.put(ProductEntry.COLUMN_SUPPLIER_PHONE, "(200) 000-0000");
83 |
84 | // Insert a new row for "The Little Prince" into the provider using the ContentResolver.
85 | // Use the {@link ProductEntry.CONTENT_URI} to indicate that we want to insert
86 | // into the products database table.
87 | // Receive the new content UrI that will allow us to access The Little Prince's data in the future.
88 | Uri newUri = getContentResolver().insert(ProductEntry.CONTENT_URI, values);
89 | }
90 |
91 | /**
92 | * Helper method to delete all products in the database.
93 | */
94 | private void deleteAllProducts() {
95 | int rowsDeleted = getContentResolver().delete(ProductEntry.CONTENT_URI,
96 | null, null);
97 | }
98 |
99 | @Override
100 | public boolean onCreateOptionsMenu(Menu menu) {
101 | // Inflate the menu options from the res/menu/menu_catalog.xml file.
102 | // This adds menu items to the app bar.
103 | getMenuInflater().inflate(R.menu.menu_catalog, menu);
104 | return true;
105 | }
106 |
107 | @Override
108 | public boolean onOptionsItemSelected(MenuItem item) {
109 | // User clicked on a menu option in the app bar overflow menu
110 | switch (item.getItemId()) {
111 | // Respond to a click on the "Insert dummy data" menu option
112 | case R.id.action_insert_dummy_data:
113 | insertDummyProduct();
114 | return true;
115 | // Respond to a click on the "Delete all entries" menu option
116 | case R.id.action_delete_all_entries:
117 | // Pop up confirmation dialog for deletion
118 | showDeleteConfirmationDialog();
119 | return true;
120 | }
121 | return super.onOptionsItemSelected(item);
122 | }
123 |
124 | @Override
125 | public Loader onCreateLoader(int i, Bundle bundle) {
126 | // Define a projection that specifies the columns from the table we care about.
127 | String[] projection = {
128 | ProductEntry._ID,
129 | ProductEntry.COLUMN_PRODUCT_NAME,
130 | ProductEntry.COLUMN_PRODUCT_AUTHOR,
131 | ProductEntry.COLUMN_PRODUCT_PRICE,
132 | ProductEntry.COLUMN_PRODUCT_QUANTITY,
133 | ProductEntry.COLUMN_PRODUCT_IMAGE};
134 |
135 | // This loader will execute the ContentProvider's query method on a background thread
136 | return new CursorLoader(this, // Parent activity context
137 | ProductEntry.CONTENT_URI, // Provider content URI to query
138 | projection, // Columns to include in the resulting Cursor
139 | null, // No selection clause
140 | null, // No selection arguments
141 | null); // Default sort order
142 | }
143 |
144 | @Override
145 | public void onLoadFinished(Loader loader, Cursor data) {
146 | // Update {@link ProductCursorAdapter} with this new cursor containing updated product data
147 | mCursorAdapter.swapCursor(data);
148 | }
149 |
150 | @Override
151 | public void onLoaderReset(Loader loader) {
152 | // Callback called when the data needs to be deleted
153 | mCursorAdapter.swapCursor(null);
154 | }
155 |
156 | /**
157 | * Prompt the user to confirm that they want to delete this product.
158 | */
159 | private void showDeleteConfirmationDialog() {
160 | // Create an AlertDialog.Builder and set the message, and click listeners
161 | // for the positive and negative buttons on the dialog.
162 | AlertDialog.Builder builder = new AlertDialog.Builder(this);
163 | builder.setMessage(R.string.delete_all_dialog_msg);
164 | builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
165 | @Override
166 | public void onClick(DialogInterface dialogInterface, int id) {
167 | // User clicked the "Delete" button, so delete all products.
168 | deleteAllProducts();
169 | }
170 | });
171 |
172 | // The User clicked the "Cancel" button, so dismiss the dialog and continue displaying
173 | // the list of products. Any button will dismiss the popup dialog by default,
174 | // so the whole OnClickListener is null.
175 | builder.setNegativeButton(R.string.cancel, null);
176 |
177 | // Create and show the AlertDialog
178 | AlertDialog alertDialog = builder.create();
179 | alertDialog.show();
180 | }
181 |
182 | /**
183 | * Pop up ISBN edit text dialog for adding a product when a user press FAB button.
184 | * Prompt the user to select how to add a product. When a user wants to add a product,
185 | * the user can add a book by entering an ISBN in the edit text field or add it manually.
186 | */
187 | public void showIsbnDialog(View v) {
188 | // Create an AlertDialog.Builder and set the message.
189 | AlertDialog.Builder builder = new AlertDialog.Builder(this);
190 | builder.setMessage(R.string.add_by_isbn_dialog_msg);
191 |
192 | // Get the layout inflater
193 | LayoutInflater inflater = this.getLayoutInflater();
194 | // Inflate and set the layout for the dialog
195 | // Pass null as the parent view because its going in the dialog layout
196 | builder.setView(inflater.inflate(R.layout.dialog_isbn, null));
197 |
198 | // Set click listeners for the positive and negative buttons on the dialog
199 | // Do not dismiss AlertDialog after clicking Positive button
200 | builder.setPositiveButton(R.string.enter_an_isbn, null);
201 | builder.setNegativeButton(R.string.add_manually, new DialogInterface.OnClickListener() {
202 | @Override
203 | public void onClick(DialogInterface dialogInterface, int i) {
204 | // Create a new intent to open the {@link EditorActivity}
205 | Intent intent = new Intent(MainActivity.this, EditorActivity.class);
206 | // Start a new activity
207 | startActivity(intent);
208 | }
209 | });
210 |
211 | // Create the AlertDialog
212 | final AlertDialog alertDialog = builder.create();
213 | // To prevent a dialog from closing when the positive button clicked, set onShowListener to
214 | // the AlertDialog
215 | alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
216 | @Override
217 | public void onShow(final DialogInterface dialogInterface) {
218 | // Find the isbn edit text
219 | final EditText isbnEditText = alertDialog.findViewById(R.id.edit_dialog_isbn);
220 |
221 | Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
222 | button.setOnClickListener(new View.OnClickListener() {
223 | @Override
224 | public void onClick(View view) {
225 | // Read from isbn input field
226 | String isbnStringDialog = isbnEditText.getText().toString().trim();
227 | // If the length of the isbn String is 13, create a new intent.
228 | // Otherwise, show toast message "Enter 13-digit ISBN".
229 | if (isbnStringDialog.length() == 13) {
230 | // Create a new intent to open the {@link IsbnActivity}
231 | Intent intent = new Intent(MainActivity.this, IsbnActivity.class);
232 | // Send the data
233 | intent.putExtra(getString(R.string.isbn_in_a_dialog), isbnStringDialog);
234 | // Start a new activity
235 | startActivity(intent);
236 | dialogInterface.dismiss();
237 | } else {
238 | Toast.makeText(MainActivity.this, getString(R.string.enter_13_digit_isbn),
239 | Toast.LENGTH_SHORT).show();
240 | }
241 | }
242 |
243 | });
244 |
245 | }
246 | });
247 |
248 | // Show the AlertDialog
249 | alertDialog.show();
250 | }
251 | }
252 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_detail.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
21 |
22 |
25 |
26 |
32 |
33 |
40 |
41 |
42 |
43 |
46 |
47 |
52 |
53 |
59 |
60 |
61 |
62 |
65 |
66 |
71 |
72 |
78 |
79 |
80 |
81 |
84 |
85 |
90 |
91 |
97 |
98 |
99 |
100 |
103 |
104 |
109 |
110 |
116 |
117 |
118 |
119 |
120 |
125 |
126 |
132 |
133 |
140 |
141 |
147 |
148 |
149 |
150 |
155 |
156 |
164 |
165 |
175 |
176 |
184 |
185 |
186 |
187 |
192 |
193 |
199 |
200 |
207 |
208 |
214 |
215 |
216 |
217 |
220 |
221 |
226 |
227 |
233 |
234 |
235 |
236 |
239 |
240 |
245 |
246 |
253 |
254 |
262 |
263 |
264 |
265 |
268 |
269 |
274 |
275 |
282 |
283 |
291 |
292 |
293 |
294 |
295 |
296 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/android/inventory/data/ProductProvider.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory.data;
2 |
3 | import android.content.ContentProvider;
4 | import android.content.ContentUris;
5 | import android.content.ContentValues;
6 | import android.content.UriMatcher;
7 | import android.database.Cursor;
8 | import android.database.sqlite.SQLiteDatabase;
9 | import android.net.Uri;
10 | import android.support.annotation.NonNull;
11 | import android.util.Log;
12 |
13 | import com.example.android.inventory.data.ProductContract.ProductEntry;
14 |
15 | /**
16 | * {@link ContentProvider} for Inventory app.
17 | */
18 | public class ProductProvider extends ContentProvider{
19 |
20 | /** URI matcher code for the content URI for the products table */
21 | private static final int PRODUCTS = 100;
22 |
23 | /** URI matcher code for the content URI for a single product in the products table */
24 | private static final int PRODUCT_ID = 101;
25 |
26 | /**
27 | * UriMatcher object to match a content URI to a corresponding code.
28 | * The input passed into the constructor represents the code to return for the root URI.
29 | * It's common to use NO_MATCH as the input for this case.
30 | */
31 | private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
32 |
33 | // Static initializer. This is run the first time anything is called from this class.
34 | static {
35 | // The calls to addURI() go here, for all of the content URI patterns that the provider
36 | // should recognize. All paths added to the UriMatcher have a corresponding code to return
37 | // when a match is found.
38 | sUriMatcher.addURI(ProductContract.CONTENT_AUTHORITY, ProductContract.PATH_PRODUCT, PRODUCTS);
39 | sUriMatcher.addURI(ProductContract.CONTENT_AUTHORITY,
40 | ProductContract.PATH_PRODUCT + "/#", PRODUCT_ID);
41 | }
42 |
43 | /** Tag for the log messages */
44 | private static final String LOG_TAG = ProductProvider.class.getSimpleName();
45 |
46 | /** Database helper object */
47 | private ProductDbHelper mDbHelper;
48 |
49 | /**
50 | * Initialize the provider and the database helper object.
51 | */
52 | @Override
53 | public boolean onCreate() {
54 | // Create and initialize a ProductDbHelper object to gain access to the products database.
55 | mDbHelper = new ProductDbHelper(getContext());
56 | return true;
57 | }
58 |
59 | /**
60 | * Perform the query for the given URI. Use the given projection, selection, selection arguments, and sort order.
61 | */
62 | @Override
63 | public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs,
64 | String sortOrder) {
65 | // Get readable database
66 | SQLiteDatabase database = mDbHelper.getReadableDatabase();
67 |
68 | // This cursor will hold the result of the query
69 | Cursor cursor;
70 |
71 | // Figure out if the URI matcher can match the URI to a specific code
72 | int match = sUriMatcher.match(uri);
73 | switch (match) {
74 | case PRODUCTS:
75 | // For the PRODUCTS code, query the products table directly with the given
76 | // projection, selection, selection arguments, and sort order. The cursor
77 | // could contain multiple rows of the products table.
78 | // Perform database query on products table
79 | cursor = database.query(ProductEntry.TABLE_NAME, projection, selection, selectionArgs,
80 | null, null, sortOrder);
81 | break;
82 | case PRODUCT_ID:
83 | // For the PRODUCT_ID code, extract out the ID from the URI.
84 | // For an example URI such as "content://com.example.android.inventory/products/3",
85 | // the selection will be "_id=?" and the selection argument will be a
86 | // String array containing the actual ID of 3 in this case.
87 | //
88 | // For every "?" in the selection, we need to have an element in the selection
89 | // arguments that will fill in the "?". Since we have 1 question mark in the
90 | // selection, we have 1 String in the selection arguments' String array.
91 | selection = ProductEntry._ID + "=?";
92 | selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
93 |
94 | // This will perform a query on the products table where the _id equals 3 to
95 | // return a Cursor containing that row of the table.
96 | cursor = database.query(ProductEntry.TABLE_NAME, projection, selection, selectionArgs,
97 | null, null, sortOrder);
98 | break;
99 | default:
100 | throw new IllegalArgumentException("Cannot query unknown URI " + uri);
101 | }
102 |
103 | // Set notification URI on the Cursor,
104 | // so we know what content URI the Cursor was created for.
105 | // If the data at this URI changes, then we know we need to update the Cursor.
106 | cursor.setNotificationUri(getContext().getContentResolver(), uri);
107 |
108 | // Return the cursor
109 | return cursor;
110 | }
111 |
112 | /**
113 | * Returns the MIME type of data for the content URI.
114 | */
115 | @Override
116 | public String getType(@NonNull Uri uri) {
117 | final int match = sUriMatcher.match(uri);
118 | switch (match) {
119 | case PRODUCTS:
120 | return ProductEntry.CONTENT_LIST_TYPE;
121 | case PRODUCT_ID:
122 | return ProductEntry.CONTENT_ITEM_TYPE;
123 | default:
124 | throw new IllegalStateException("Unknown URI " + uri + " with match " + match);
125 | }
126 | }
127 |
128 |
129 | /**
130 | * Insert new data into the provider with the given ContentValues.
131 | */
132 | @Override
133 | public Uri insert(@NonNull Uri uri, ContentValues contentValues) {
134 | final int match = sUriMatcher.match(uri);
135 | switch(match) {
136 | case PRODUCTS:
137 | return insertProduct(uri, contentValues);
138 | default:
139 | throw new IllegalArgumentException("Insertion is not supported for " + uri);
140 | }
141 | }
142 |
143 | /**
144 | * Insert a product into the database with the given content values. Return the new content URI
145 | * for that specific row in the database.
146 | */
147 | private Uri insertProduct(Uri uri, ContentValues values) {
148 | // Check that the product name is not null
149 | String name = values.getAsString(ProductEntry.COLUMN_PRODUCT_NAME);
150 | if (name == null) {
151 | throw new IllegalArgumentException("Product requires a name");
152 | }
153 | // Check that the product author is valid
154 | String author = values.getAsString(ProductEntry.COLUMN_PRODUCT_AUTHOR);
155 | if (author == null) {
156 | throw new IllegalArgumentException("Product requires an author name");
157 | }
158 | // No need to check the publisher, any value is valid (including null).
159 |
160 | // Check that the ISBN is valid
161 | String isbn = values.getAsString(ProductEntry.COLUMN_PRODUCT_ISBN);
162 | if (isbn == null) {
163 | throw new IllegalArgumentException("Product requires valid ISBN");
164 | }
165 | // Check that the price is valid
166 | Double price = values.getAsDouble(ProductEntry.COLUMN_PRODUCT_PRICE);
167 | if (price == null || price < 0) {
168 | throw new IllegalArgumentException("Product requires a valid price");
169 | }
170 | // If the quantity is provided, check that it's greater than or equal to 0
171 | Integer quantity = values.getAsInteger(ProductEntry.COLUMN_PRODUCT_QUANTITY);
172 | if (quantity != null && quantity < 0) {
173 | throw new IllegalArgumentException("Product requires valid quantity");
174 | }
175 | // No need to check the image, any value is valid
176 |
177 | // Check that the supplier name is valid
178 | String supplierName = values.getAsString(ProductEntry.COLUMN_SUPPLIER_NAME);
179 | if (supplierName == null) {
180 | throw new IllegalArgumentException("Product requires a supplier name");
181 | }
182 | // No need to check the supplier email, any value is valid (including null).
183 |
184 | // Check that the supplier phone number is valid
185 | String supplierPhone = values.getAsString(ProductEntry.COLUMN_SUPPLIER_PHONE);
186 | if (supplierPhone == null) {
187 | throw new IllegalArgumentException("Product requires supplier phone number");
188 | }
189 |
190 | // Get writable database
191 | SQLiteDatabase database = mDbHelper.getWritableDatabase();
192 |
193 | // Insert the new product with the given values
194 | long id = database.insert(ProductEntry.TABLE_NAME, null, values);
195 |
196 | // If the ID is -1, then the insertion failed. Log an error and return null.
197 | if (id == -1) {
198 | Log.e(LOG_TAG, "Failed to insert row for " + uri);
199 | return null;
200 | }
201 |
202 | // Notify all listeners that the data has changed for the product content URI
203 | // uri: content://com.example.android.inventory/products
204 | getContext().getContentResolver().notifyChange(uri, null);
205 |
206 | // Return the new URI with the ID (of the newly inserted row) append at the end
207 | return ContentUris.withAppendedId(uri, id);
208 | }
209 |
210 | /**
211 | * Delete the data at the given selection and selection arguments.
212 | */
213 | @Override
214 | public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
215 | // Get writable database
216 | SQLiteDatabase database = mDbHelper.getWritableDatabase();
217 |
218 | // Track the number of rows that were deleted
219 | int rowsDeleted;
220 |
221 | final int match = sUriMatcher.match(uri);
222 | switch (match) {
223 | case PRODUCTS:
224 | // Delete all rows that match the selection and selection args
225 | rowsDeleted = database.delete(ProductEntry.TABLE_NAME, selection, selectionArgs);
226 | break;
227 | case PRODUCT_ID:
228 | // Delete a single row given by the ID in the URI
229 | selection = ProductEntry._ID + "=?";
230 | selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
231 | rowsDeleted = database.delete(ProductEntry.TABLE_NAME, selection, selectionArgs);
232 | break;
233 | default:
234 | throw new IllegalArgumentException("Deletion is not supported for " + uri);
235 | }
236 |
237 | // If 1 or more rows were deleted, then notify all listeners that the data at the given URI
238 | // has changed.
239 | if (rowsDeleted != 0) {
240 | getContext().getContentResolver().notifyChange(uri, null);
241 | }
242 |
243 | // Return the number of rows deleted
244 | return rowsDeleted;
245 | }
246 |
247 | /**
248 | * Updates the data at the given selection and selection arguments, with the new ContentValues.
249 | */
250 | @Override
251 | public int update(@NonNull Uri uri, ContentValues contentValues, String selection,
252 | String[] selectionArgs) {
253 | final int match = sUriMatcher.match(uri);
254 | switch (match) {
255 | case PRODUCTS:
256 | return updateProduct(uri, contentValues, selection, selectionArgs);
257 | case PRODUCT_ID:
258 | // For the PRODUCT_ID code, extract out the ID from the URI,
259 | // so we know which row to update. Selection will be "_id=?" and selection
260 | // arguments will be a String array containing the actual ID.
261 | selection = ProductEntry._ID + "=?";
262 | selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
263 | return updateProduct(uri, contentValues, selection, selectionArgs);
264 | default:
265 | throw new IllegalArgumentException("Update is not supported for " + uri);
266 | }
267 | }
268 |
269 | /**
270 | * Update products in the database with the given content values. Apply the changes to the rows
271 | * specified in the selection ans selection arguments (which could be 0 or 1 or more products).
272 | * Return the number of rows that were successfully updated.
273 | */
274 | private int updateProduct(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
275 | // If the {@link ProductEntry#COLUMN_PRODUCT_NAME} key is present,
276 | // check that the name value is not null.
277 | if (values.containsKey(ProductEntry.COLUMN_PRODUCT_NAME)) {
278 | String name = values.getAsString(ProductEntry.COLUMN_PRODUCT_NAME);
279 | if (name == null) {
280 | throw new IllegalArgumentException("Product requires a name");
281 | }
282 | }
283 | // If the {@link ProductEntry#COLUMN_PRODUCT_AUTHOR} key is present,
284 | // check that the author value is not null.
285 | if (values.containsKey(ProductEntry.COLUMN_PRODUCT_AUTHOR)) {
286 | String author = values.getAsString(ProductEntry.COLUMN_PRODUCT_AUTHOR);
287 | if (author == null) {
288 | throw new IllegalArgumentException("Product requires an author name");
289 | }
290 | }
291 |
292 | // No need to check the publisher, any value is valid (including null).
293 |
294 | // If the {@link ProductEntry#COLUMN_PRODUCT_ISBN} key is present,
295 | // check that the ISBN value is not null.
296 | if (values.containsKey(ProductEntry.COLUMN_PRODUCT_ISBN)) {
297 | String isbn = values.getAsString(ProductEntry.COLUMN_PRODUCT_ISBN);
298 | if (isbn == null) {
299 | throw new IllegalArgumentException("Product requires valid ISBN");
300 | }
301 | }
302 | // If the {@link ProductEntry#COLUMN_PRICE} key is present,
303 | // check that the price value is valid.
304 | if (values.containsKey(ProductEntry.COLUMN_PRODUCT_PRICE)) {
305 | Double price = values.getAsDouble(ProductEntry.COLUMN_PRODUCT_PRICE);
306 | if (price == null || price < 0) {
307 | throw new IllegalArgumentException("Product requires a valid price");
308 | }
309 | }
310 | // If the {@link ProductEntry#COLUMN_QUANTITY} key is present,
311 | // check that the quantity value is valid.
312 | if (values.containsKey(ProductEntry.COLUMN_PRODUCT_QUANTITY)) {
313 | Integer quantity = values.getAsInteger(ProductEntry.COLUMN_PRODUCT_QUANTITY);
314 | if (quantity != null && quantity < 0) {
315 | throw new IllegalArgumentException("Product requires valid quantity");
316 | }
317 | }
318 | // No need to check the image, any value is valid
319 |
320 | // If the {@link ProductEntry#COLUMN_SUPPLIER_NAME} key is present,
321 | // check that the supplier name value is not null.
322 | if (values.containsKey(ProductEntry.COLUMN_SUPPLIER_NAME)) {
323 | String supplierName = values.getAsString(ProductEntry.COLUMN_SUPPLIER_NAME);
324 | if (supplierName == null) {
325 | throw new IllegalArgumentException("Product requires a supplier name");
326 | }
327 | }
328 | // No need to check the supplier email, any value is valid (including null).
329 |
330 | // If the {@link ProductEntry#COLUMN_SUPPLIER_PHONE_NUMBER} key is present,
331 | // check that the supplier phone number value is not null.
332 | if (values.containsKey(ProductEntry.COLUMN_SUPPLIER_PHONE)) {
333 | String supplierPhone = values.getAsString(ProductEntry.COLUMN_SUPPLIER_PHONE);
334 | if (supplierPhone == null) {
335 | throw new IllegalArgumentException("Product requires supplier phone number");
336 | }
337 | }
338 |
339 | // If there are no values to update, then don't try to update the database
340 | if (values.size() == 0) {
341 | return 0;
342 | }
343 |
344 | // Otherwise, get writable database to update the data
345 | SQLiteDatabase database = mDbHelper.getWritableDatabase();
346 |
347 | // Perform the update on the database and get the number of rows affected
348 | int rowsUpdated = database.update(ProductEntry.TABLE_NAME, values, selection, selectionArgs);
349 |
350 | // If 1 or more rows were updated, then notify all listeners that the data at the given URI
351 | // has changed
352 | if (rowsUpdated != 0 ) {
353 | getContext().getContentResolver().notifyChange(uri, null);
354 | }
355 |
356 | // Return the number of rows updated
357 | return rowsUpdated;
358 | }
359 | }
360 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/android/inventory/activity/DetailActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.android.inventory.activity;
2 |
3 | import android.Manifest;
4 | import android.app.AlertDialog;
5 | import android.app.LoaderManager;
6 | import android.content.ContentValues;
7 | import android.content.CursorLoader;
8 | import android.content.DialogInterface;
9 | import android.content.Intent;
10 | import android.content.Loader;
11 | import android.content.pm.PackageManager;
12 | import android.database.Cursor;
13 | import android.graphics.Bitmap;
14 | import android.graphics.BitmapFactory;
15 | import android.net.Uri;
16 | import android.os.Bundle;
17 | import android.support.annotation.NonNull;
18 | import android.support.v4.app.ActivityCompat;
19 | import android.support.v7.app.AppCompatActivity;
20 | import android.text.TextUtils;
21 | import android.util.Log;
22 | import android.view.Menu;
23 | import android.view.MenuItem;
24 | import android.view.View;
25 | import android.view.ViewTreeObserver;
26 | import android.widget.Button;
27 | import android.widget.ImageButton;
28 | import android.widget.ImageView;
29 | import android.widget.TextView;
30 | import android.widget.Toast;
31 |
32 | import com.example.android.inventory.R;
33 | import com.example.android.inventory.data.ProductContract.ProductEntry;
34 |
35 | import java.io.FileNotFoundException;
36 | import java.io.IOException;
37 | import java.io.InputStream;
38 |
39 | import butterknife.BindView;
40 | import butterknife.ButterKnife;
41 |
42 | /**
43 | * DetailActivity displays the product details which are stored in the database.
44 | */
45 | public class DetailActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks {
46 |
47 | /** Tag for the log messages */
48 | private static final String LOG_TAG = DetailActivity.class.getSimpleName();
49 |
50 | /** Identifier for the product data loader */
51 | private static final int EXISTING_PRODUCT_LOADER = 0;
52 |
53 | /** Content URI for the existing product */
54 | private Uri mCurrentProductUri;
55 |
56 | /** TextView for the product name */
57 | @BindView(R.id.detail_product_name) TextView mProductNameTextView;
58 |
59 | /** TextView field to enter the author */
60 | @BindView(R.id.detail_product_author) TextView mAuthorTextView;
61 |
62 | /** TextView field to enter the publisher */
63 | @BindView(R.id.detail_product_publisher) TextView mPublisherTextView;
64 |
65 | /** TextView field to enter the ISBN */
66 | @BindView(R.id.detail_product_isbn) TextView mIsbnTextView;
67 |
68 | /** TextView field to enter the price of the product */
69 | @BindView(R.id.detail_product_price) TextView mPriceTextView;
70 |
71 | /** TextView field to enter the quantity of the product */
72 | @BindView(R.id.detail_product_quantity) TextView mQuantityTextView;
73 |
74 | /** ImageView for the product */
75 | @BindView(R.id.detail_product_image) ImageView mImageView;
76 |
77 | /** TextView field to enter supplier's name */
78 | @BindView(R.id.detail_supplier_name) TextView mSupplierNameTextView;
79 |
80 | /** TextView field to enter supplier's email */
81 | @BindView(R.id.detail_supplier_email) TextView mSupplierEmailTextView;
82 |
83 | /** TextView field to enter supplier's phone number */
84 | @BindView(R.id.detail_supplier_phone) TextView mSupplierPhoneTextView;
85 |
86 | /** ImageButton for the supplier email */
87 | @BindView(R.id.detail_email_button) ImageButton mSupplierEmailButton;
88 |
89 | private static final int MY_PERMISSONS_REQUEST_READ_CONTACTS = 1;
90 |
91 | @Override
92 | protected void onCreate(Bundle savedInstanceState) {
93 | super.onCreate(savedInstanceState);
94 | setContentView(R.layout.activity_detail);
95 |
96 | // Examine the intent that was used to launch this activity
97 | Intent intent = getIntent();
98 | mCurrentProductUri = intent.getData();
99 |
100 | // Bind the view using ButterKnife
101 | ButterKnife.bind(this);
102 |
103 | // Find all relevant button that we will need to increment and decrement the quantity
104 | Button plusButton = findViewById(R.id.detail_plus_button);
105 | Button minusButton = findViewById(R.id.detail_minus_button);
106 | ImageButton supplierPhoneButton = findViewById(R.id.detail_phone_button);
107 |
108 | // Set OnClickListener on the plus button. We can increment the available quantity displayed.
109 | plusButton.setOnClickListener(new View.OnClickListener() {
110 | @Override
111 | public void onClick(View view) {
112 | increment();
113 | }
114 | });
115 |
116 | // Set OnClickListener on the minus button. We can decrement the available quantity displayed.
117 | minusButton.setOnClickListener(new View.OnClickListener() {
118 | @Override
119 | public void onClick(View view) {
120 | decrement();
121 | }
122 | });
123 |
124 | mSupplierEmailButton.setOnClickListener(new View.OnClickListener() {
125 | @Override
126 | public void onClick(View view) {
127 | // Create order message and email to the supplier of the product.
128 | composeEmail();
129 | }
130 | });
131 |
132 | supplierPhoneButton.setOnClickListener(new View.OnClickListener() {
133 | @Override
134 | public void onClick(View view) {
135 | // Make a phone call
136 | call();
137 | }
138 | });
139 |
140 | // Initialize a loader to read the product data from the database
141 | // and display the current values in the editor
142 | getLoaderManager().initLoader(EXISTING_PRODUCT_LOADER, null, this);
143 |
144 | // Allow Up navigation with the app icon in the app bar
145 | getSupportActionBar().setDisplayHomeAsUpEnabled(true);
146 | getSupportActionBar().setDisplayShowHomeEnabled(true);
147 | }
148 |
149 | @Override
150 | public void onRequestPermissionsResult(int requestCode,
151 | @NonNull String permissions[], @NonNull int[] grantResults) {
152 | switch (requestCode) {
153 | case MY_PERMISSONS_REQUEST_READ_CONTACTS: {
154 | // If request is cancelled, the result arrays are empty.
155 | if (grantResults.length > 0
156 | && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
157 | // permission was granted, yay! Do the contacts-related task you need to do.
158 | Toast.makeText(this, getString(R.string.permission_granted),
159 | Toast.LENGTH_SHORT).show();
160 | } else {
161 | // permission denied, boo! Disable the functionality that depends on this permission.
162 | Toast.makeText(this, getString(R.string.permission_denied),
163 | Toast.LENGTH_SHORT).show();
164 | }
165 | return;
166 | }
167 | // other 'case' lines to check for other permissions this app might request
168 | }
169 | }
170 |
171 | /**
172 | * Start a phone call intent when supplier phone button is clicked.
173 | */
174 | private void call() {
175 | // Read from text field
176 | String phoneString = mSupplierPhoneTextView.getText().toString().trim();
177 |
178 | Intent phoneIntent = new Intent(Intent.ACTION_CALL);
179 | phoneIntent.setData(Uri.parse(getString(R.string.tel_colon) + phoneString));
180 | // Check whether the app has a given permission
181 | if (ActivityCompat.checkSelfPermission(DetailActivity.this,
182 | Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
183 |
184 | if (ActivityCompat.shouldShowRequestPermissionRationale(DetailActivity.this,
185 | Manifest.permission.CALL_PHONE)) {
186 |
187 | } else {
188 | // Request permission to be granted to this application
189 | ActivityCompat.requestPermissions(DetailActivity.this,
190 | new String[]{ Manifest.permission.CALL_PHONE},
191 | MY_PERMISSONS_REQUEST_READ_CONTACTS);
192 | }
193 | return;
194 | }
195 | startActivity(Intent.createChooser(phoneIntent, getString(R.string.make_a_phone_call)));
196 | }
197 |
198 | /**
199 | * Increment the available quantity displayed by 1.
200 | */
201 | private void increment() {
202 | // Read from text fields
203 | String quantityString = mQuantityTextView.getText().toString().trim();
204 |
205 | // Parse the string into an Integer value.
206 | int quantity = Integer.parseInt(quantityString);
207 | quantity = quantity + 1;
208 |
209 | // Create a ContentValues object where column names are the keys,
210 | // and product attributes from the textView fields are the values.
211 | ContentValues values = new ContentValues();
212 | values.put(ProductEntry.COLUMN_PRODUCT_QUANTITY, quantity);
213 |
214 | // Update the product with content URI: mCurrentProductUri
215 | // and pass in the new ContentValues. Pass in null for the selection and selection args
216 | // because mCurrentProductUri will already identify the correct row in the database that
217 | // we want to modify.
218 | int rowsAffected = getContentResolver().update(mCurrentProductUri, values,
219 | null, null);
220 | }
221 |
222 | /**
223 | * Decrement the available quantity displayed by 1 and check that no negative quantities display.
224 | */
225 | private void decrement() {
226 | // Read from text fields
227 | String quantityString = mQuantityTextView.getText().toString().trim();
228 |
229 | // Parse the string into an Integer value.
230 | int quantity = Integer.parseInt(quantityString);
231 | // If the quantity is more than 0, decrement the quantity by 1.
232 | // If quantity is 0, show a toast message.
233 | if (quantity > 0) {
234 | quantity = quantity - 1;
235 | } else if (quantity == 0) {
236 | Toast.makeText(DetailActivity.this, getString(R.string.detail_update_zero_quantity),
237 | Toast.LENGTH_SHORT).show();
238 | }
239 |
240 | // Create a ContentValues object where column names are the keys,
241 | // and product attributes from the textView fields are the values.
242 | ContentValues values = new ContentValues();
243 | values.put(ProductEntry.COLUMN_PRODUCT_QUANTITY, quantity);
244 |
245 | // Update the product with content URI: mCurrentProductUri
246 | // and pass in the new ContentValues. Pass in null for the selection and selection args
247 | // because mCurrentProductUri will already identify the correct row in the database that
248 | // we want to modify.
249 | int rowsAffected = getContentResolver().update(mCurrentProductUri, values,
250 | null, null);
251 | }
252 |
253 | /**
254 | * If a user clicks the email button, creates order message and email to the supplier of the product.
255 | */
256 | private void composeEmail() {
257 | // Read from text fields
258 | String productNameString = mProductNameTextView.getText().toString().trim();
259 | String authorString = mAuthorTextView.getText().toString().trim();
260 | String publisherString = mPublisherTextView.getText().toString().trim();
261 | String isbnString = mIsbnTextView.getText().toString().trim();
262 | String[] supplierEmailString = {mSupplierEmailTextView.getText().toString().trim()};
263 |
264 | // Create order message
265 | String subject = getString(R.string.email_subject) + " " + productNameString;
266 | String message = getString(R.string.place_an_order) + " " + getString(R.string.copies_of) +
267 | " " + productNameString + getString(R.string.period);
268 | message += getString(R.string.nn) + getString(R.string.app_product_details);
269 | message += getString(R.string.nn) + getString(R.string.category_product_name) +
270 | getString(R.string.colon) + " " + productNameString;
271 | message += getString(R.string.n) + getString(R.string.category_product_author) +
272 | getString(R.string.colon) + " " + authorString;
273 | message += getString(R.string.n) + getString(R.string.category_product_publisher) +
274 | getString(R.string.colon) + " " + publisherString;
275 | message += getString(R.string.n) + getString(R.string.category_product_isbn) +
276 | getString(R.string.colon) + " "+ isbnString;
277 | message += getString(R.string.nn) + getString(R.string.best);
278 |
279 | Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
280 | emailIntent.setData(Uri.parse(getString(R.string.mailto)));
281 | // Email address
282 | emailIntent.putExtra(Intent.EXTRA_EMAIL, supplierEmailString);
283 | // Email subject
284 | emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
285 | // The body of the email
286 | emailIntent.putExtra(Intent.EXTRA_TEXT, message);
287 |
288 | if(emailIntent.resolveActivity(getPackageManager()) != null) {
289 | startActivity(Intent.createChooser(emailIntent, getString(R.string.compose_email)));
290 | }
291 | }
292 |
293 | @Override
294 | public boolean onCreateOptionsMenu(Menu menu) {
295 | // Inflate the menu options from the res/menu/menu_catalog.xml file.
296 | // This adds menu items to the app bar.
297 | getMenuInflater().inflate(R.menu.menu_detail, menu);
298 | return true;
299 | }
300 |
301 | @Override
302 | public boolean onOptionsItemSelected(MenuItem item) {
303 | // User clicked on a menu option in the app bar overflow menu
304 | switch (item.getItemId()) {
305 | // Respond to a click on the "Save" menu option
306 | case R.id.action_edit:
307 | // Create new intent to go to {@link EditorActivity}
308 | Intent intent = new Intent(DetailActivity.this, EditorActivity.class);
309 | // Set the current product URI on the data field of the intent
310 | intent.setData(mCurrentProductUri);
311 | // Launch the {@link EditorActivity} to display the data for the current product.
312 | startActivity(intent);
313 | return true;
314 | // Respond to a click on the "Delete" menu option
315 | case R.id.action_delete:
316 | // Pop up confirmation dialog for deletion
317 | showDeleteConfirmationDialog();
318 | return true;
319 | // Respond to a click on the "Up" arrow button in the app bar
320 | case android.R.id.home:
321 | // Exit activity
322 | finish();
323 | return true;
324 | }
325 | return super.onOptionsItemSelected(item);
326 | }
327 |
328 | /**
329 | * Perform the deletion of the product in the database.
330 | */
331 | private void deleteProduct() {
332 | // Only perform the delete if this is an existing product.
333 | if (mCurrentProductUri != null) {
334 | // Call the ContentResolver to delete the product at the given content URI.
335 | // Pass in null for the selection and selection args because the mCurrentProductUri
336 | // content URI already identifies the product that we want.
337 | int rowsDeleted = getContentResolver().delete(mCurrentProductUri, null,
338 | null);
339 |
340 | // Show a toast message depending on whether or not the delete was successful.
341 | if (rowsDeleted == 0) {
342 | // If no rows were deleted, then there was an error with the delete.
343 | Toast.makeText(this, getString(R.string.editor_delete_product_failed),
344 | Toast.LENGTH_SHORT).show();
345 | } else {
346 | // Otherwise, the delete was successful and we can display a toast.
347 | Toast.makeText(this, getString(R.string.editor_delete_product_successful),
348 | Toast.LENGTH_SHORT).show();
349 | }
350 | }
351 | // Close the activity
352 | finish();
353 | }
354 |
355 | /**
356 | * Prompt the user to confirm that they want to delete this product.
357 | */
358 | private void showDeleteConfirmationDialog() {
359 | // Create an AlertDialog.Builder and set the message, and click listeners
360 | // for the positive and negative buttons on the dialog.
361 | AlertDialog.Builder builder = new AlertDialog.Builder(this);
362 | builder.setMessage(R.string.delete_dialog_msg);
363 | builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
364 | @Override
365 | public void onClick(DialogInterface dialogInterface, int id) {
366 | // User clicked the "Delete" button, so delete the product.
367 | deleteProduct();
368 | }
369 | });
370 | builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
371 | @Override
372 | public void onClick(DialogInterface dialogInterface, int id) {
373 | // User clicked the "Cancel" button, so dismiss the dialog
374 | // and continue editing the product.
375 | if (dialogInterface != null) {
376 | dialogInterface.dismiss();
377 | }
378 | }
379 | });
380 |
381 | // Create and show the AlertDialog
382 | AlertDialog alertDialog = builder.create();
383 | alertDialog.show();
384 | }
385 |
386 | @Override
387 | public Loader onCreateLoader(int i, Bundle bundle) {
388 | // Since the editor shows all product attributes, define a projection that contains
389 | // all columns from the product table
390 | String[] projection = {
391 | ProductEntry._ID,
392 | ProductEntry.COLUMN_PRODUCT_NAME,
393 | ProductEntry.COLUMN_PRODUCT_AUTHOR,
394 | ProductEntry.COLUMN_PRODUCT_PUBLISHER,
395 | ProductEntry.COLUMN_PRODUCT_ISBN,
396 | ProductEntry.COLUMN_PRODUCT_PRICE,
397 | ProductEntry.COLUMN_PRODUCT_QUANTITY,
398 | ProductEntry.COLUMN_PRODUCT_IMAGE,
399 | ProductEntry.COLUMN_SUPPLIER_NAME,
400 | ProductEntry.COLUMN_SUPPLIER_EMAIL,
401 | ProductEntry.COLUMN_SUPPLIER_PHONE};
402 |
403 | // This loader will execute the ContentProvider's query method on a background thread
404 | return new CursorLoader(this, // Parent activity context
405 | mCurrentProductUri, // Query the content URI for the current product
406 | projection, // Columns to include in the resulting Cursor
407 | null, // No selection clause
408 | null, // No selection arguments
409 | null); // Default sort order
410 | }
411 |
412 | @Override
413 | public void onLoadFinished(Loader loader, Cursor cursor) {
414 | // Bail early if the cursor is null or there is less than 1 row in the cursor
415 | if (cursor == null || cursor.getCount() < 1) {
416 | return;
417 | }
418 |
419 | // Proceed with moving to the first row of the cursor and reading data from it
420 | // (This should be the only row in the cursor)
421 | if (cursor.moveToFirst()) {
422 | // Find the columns of product attributes that we're interested in
423 | int titleColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_NAME);
424 | int authorColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_AUTHOR);
425 | int publisherColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_PUBLISHER);
426 | int isbnColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_ISBN);
427 | int priceColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_PRICE);
428 | int quantityColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_QUANTITY);
429 | int imageColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_IMAGE);
430 | int supplierNameColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_SUPPLIER_NAME);
431 | int supplierEmailColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_SUPPLIER_EMAIL);
432 | int supplierPhoneColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_SUPPLIER_PHONE);
433 |
434 | // Extract out the value from the Cursor for the given column index
435 | String title = cursor.getString(titleColumnIndex);
436 | String author = cursor.getString(authorColumnIndex);
437 | String publisher = cursor.getString(publisherColumnIndex);
438 | String isbn = cursor.getString(isbnColumnIndex);
439 | double price = cursor.getDouble(priceColumnIndex);
440 | int quantity = cursor.getInt(quantityColumnIndex);
441 | final String imageString = cursor.getString(imageColumnIndex);
442 | String supplierName = cursor.getString(supplierNameColumnIndex);
443 | String supplierEmail = cursor.getString(supplierEmailColumnIndex);
444 | String supplierPhone = cursor.getString(supplierPhoneColumnIndex);
445 |
446 | // Update the views on the screen with the values from the database
447 | mProductNameTextView.setText(title);
448 | mAuthorTextView.setText(author);
449 | mPublisherTextView.setText(publisher);
450 | mIsbnTextView.setText(isbn);
451 | mPriceTextView.setText(String.valueOf(price));
452 | mQuantityTextView.setText(String.valueOf(quantity));
453 |
454 | if(imageString != null) {
455 |
456 | // Attach a ViewTreeObserver listener to ImageView.
457 | ViewTreeObserver viewTreeObserver = mImageView.getViewTreeObserver();
458 | viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
459 | @Override
460 | public void onGlobalLayout() {
461 | mImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
462 | mImageView.setImageBitmap(getBitmapFromUri(Uri.parse(imageString)));
463 | }
464 | });
465 | } else {
466 | mImageView.setImageResource(R.drawable.ic_image_black_24dp);
467 | }
468 |
469 | mSupplierNameTextView.setText(supplierName);
470 | // If supplierEmail string is empty, hide the email button
471 | if(TextUtils.isEmpty(supplierEmail)) {
472 | mSupplierEmailButton.setVisibility(View.GONE);
473 | }
474 | mSupplierEmailTextView.setText(supplierEmail);
475 | mSupplierPhoneTextView.setText(supplierPhone);
476 | }
477 | }
478 |
479 | @Override
480 | public void onLoaderReset(Loader loader) {
481 | // If the loader is invalidated, clear out all the data from the input fields.
482 | mProductNameTextView.setText("");
483 | mAuthorTextView.setText("");
484 | mPublisherTextView.setText("");
485 | mIsbnTextView.setText("");
486 | mPriceTextView.setText(String.valueOf(""));
487 | mQuantityTextView.setText(String.valueOf(""));
488 | mSupplierNameTextView.setText("");
489 | mSupplierEmailTextView.setText("");
490 | mSupplierPhoneTextView.setText("");
491 | }
492 |
493 | /**
494 | * Returns a Bitmap object from the URI which is the location of the image.
495 | */
496 | public Bitmap getBitmapFromUri(Uri uri) {
497 | // Check the Uri is null or empty
498 | if (uri == null || uri.toString().isEmpty()) {
499 | return null;
500 | }
501 |
502 | // Get the dimensions of the View
503 | int targetW = mImageView.getWidth();
504 | int targetH = mImageView.getHeight();
505 |
506 | InputStream inputStream = null;
507 | try {
508 | inputStream = this.getContentResolver().openInputStream(uri);
509 |
510 | // Get the dimensions of the bitmap
511 | BitmapFactory.Options bmOptions = new BitmapFactory.Options();
512 | bmOptions.inJustDecodeBounds = true;
513 | BitmapFactory.decodeStream(inputStream, null, bmOptions);
514 | inputStream.close();
515 |
516 | int photoW = bmOptions.outWidth;
517 | int photoH = bmOptions.outHeight;
518 |
519 | // Determine how much to scale down the image
520 | int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
521 |
522 | // Decode the image file into a Bitmap sized to fill the View
523 | bmOptions.inJustDecodeBounds = false;
524 | bmOptions.inSampleSize = scaleFactor;
525 | bmOptions.inPurgeable = true;
526 |
527 | inputStream = this.getContentResolver().openInputStream(uri);
528 | Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, bmOptions);
529 | inputStream.close();
530 | return bitmap;
531 |
532 | } catch (FileNotFoundException fne) {
533 | Log.e(LOG_TAG, "Failed to open the image file.", fne);
534 | return null;
535 | } catch (Exception e) {
536 | Log.e(LOG_TAG, "Failed to load image.", e);
537 | return null;
538 | } finally {
539 | try {
540 | inputStream.close();
541 | } catch (IOException e) {
542 | Log.e(LOG_TAG, "Problem with loading a file.");
543 | }
544 | }
545 | }
546 | }
547 |
--------------------------------------------------------------------------------