Skip to content

Commit

Permalink
Add Lab7
Browse files Browse the repository at this point in the history
jackshendrikov committed Feb 20, 2021
1 parent fe14735 commit 2ee4548
Showing 20 changed files with 1,297 additions and 124 deletions.
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -108,6 +108,16 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<provider
android:name=".books.BookProvider"
android:authorities="ua.kpi.comsys.io8227.jackshen.books"
android:exported="false" />

<provider
android:name=".gallery.PictureProvider"
android:authorities="ua.kpi.comsys.io8227.jackshen.gallery"
android:exported="false" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package ua.kpi.comsys.io8227.jackshen.books;

import android.os.Build;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.squareup.picasso.Picasso;

import java.io.File;

import ua.kpi.comsys.io8227.jackshen.R;


public class BookAboutActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -21,10 +28,12 @@ protected void onCreate(Bundle savedInstanceState) {

ImageView cover = findViewById(R.id.image_full);
assert fullBook != null;
if (fullBook.getImageUrl().equals("")) {
if (fullBook.getImageUrl() == null || fullBook.getImageUrl().equals("")) {
cover.setImageResource(R.drawable.noimage);
} else {
new BookAdapter.DownloadImage(cover).execute(fullBook.getImageUrl());
if (BookActivity.isNetworkConnected(this))
Picasso.get().load(fullBook.getImageUrl()).into(cover);
else Picasso.get().load(new File(fullBook.getImageUrl())).into(cover);
}

TextView title = findViewById(R.id.title_full);
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
@@ -33,6 +34,7 @@
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.google.android.material.textfield.TextInputEditText;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
@@ -74,6 +76,8 @@ public class BookActivity extends AppCompatActivity implements LoaderManager.Loa

private String mQueryText;

private static Context mContext;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -84,6 +88,8 @@ protected void onCreate(Bundle savedInstanceState) {
// Inflate the activity's UI
setContentView(R.layout.activity_book);

mContext = getApplicationContext();

// Setup UI to hide soft keyboard when clicked outside the {@link EditText}
setupUI(findViewById(R.id.main_parent));

@@ -166,6 +172,22 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
bookListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
String bookISBN = mAdapter.getItem(position).getISBN();

// remove image
String imagePath = BookProvider.getDBImageURL(bookISBN);
if (!imagePath.equals(""))
if (new File(imagePath).delete())
Log.i("Delete image", "image successfully deleted from storage!");

// delete from DB
getContentResolver().delete(
BookContract.BookEntry.CONTENT_URI,
"book_isbn = ?",
new String[]{bookISBN}
);

// delete from View
mAdapter.remove(mAdapter.getItem(position));
// false : close the menu; true : not close the menu
return false;
@@ -184,7 +206,6 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
intent.putExtra("book_full", currentBook);

startActivity(intent);

}
});

@@ -209,7 +230,6 @@ public void getOutline(View view, Outline outline) {
};
mAddButton.setOutlineProvider(viewOutlineProvider);
mAddButton.setClipToOutline(true);

}


@@ -220,16 +240,10 @@ public void onClickSearch(String searchText) {
mAdapter.clear();
mEmptyTextView.setVisibility(View.GONE);

// Get a reference to the ConnectivityManager to check state of network connectivity
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

// Get details on the currently active default data network
NetworkInfo networkInfo = Objects.requireNonNull(connMgr).getActiveNetworkInfo();

try {
// If there is a network connection -> get data
if (networkInfo != null && networkInfo.isConnected()) {
if (!searchText.isEmpty() && searchText.length() >= 3 && searchText != null) {
if (isNetworkConnected(this)) {
if (!searchText.isEmpty() && searchText.length() >= 3) {
String bookName = URLEncoder.encode(searchText.trim().replaceAll(" ", "%20"), "UTF-8");

// Set the URL with the suitable bookName
@@ -264,8 +278,30 @@ public void onClickSearch(String searchText) {
mLoadingIndicator.setVisibility(View.INVISIBLE);

// Update empty state with no connection error message
Toast.makeText(BookActivity.this, "No Internet Connection", Toast.LENGTH_SHORT).show();
mEmptyTextView.setText("No Internet Connection");
mEmptyTextView.setText("No Internet Connection! Trying to retrieve data from DB..");
Toast.makeText(this, "No Internet Connection! Trying to retrieve data from DB", Toast.LENGTH_SHORT).show();

if (!searchText.isEmpty() && searchText.length() >= 3) {
mLoadingIndicator.setVisibility(View.VISIBLE);

List<Book> booksDB = BookProvider.getData(searchText);

mAdapter.clear();

// Hide the indicator after the data is appeared
mLoadingIndicator.setVisibility(View.GONE);

if (booksDB != null && !booksDB.isEmpty()) {
mAdapter.addAll(booksDB);
} else {
// Set empty text to display "No books found."
mEmptyTextView.setText("No books found...");
mEmptyTextView.setVisibility(View.VISIBLE);
Toast.makeText(BookActivity.this, "No books found at all", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "You need to introduce some text to search (>=3 symbols)", Toast.LENGTH_LONG).show();
}
}

} catch (UnsupportedEncodingException e) {
@@ -375,7 +411,6 @@ public void onLoadFinished(Loader<List<Book>> loader, List<Book> data) {
if (data != null && !data.isEmpty()) {
mAdapter.addAll(data);
}

}

@Override
@@ -391,4 +426,21 @@ protected void onSaveInstanceState(Bundle outState) {
outState.putString(REQUEST_URL, mQueryText);
}

/** Get current app context */
public static Context getAppContext() {
return BookActivity.mContext;
}

/** Check network connection */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static boolean isNetworkConnected(Context context) {
// Get a reference to the ConnectivityManager to check state of network connectivity
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

// Get details on the currently active default data network
NetworkInfo networkInfo = Objects.requireNonNull(connMgr).getActiveNetworkInfo();

// If there is a network connection -> return true
return (networkInfo != null && networkInfo.isConnected());
}
}
Loading

0 comments on commit 2ee4548

Please sign in to comment.