Skip to content

Commit

Permalink
Update Lab6
Browse files Browse the repository at this point in the history
  • Loading branch information
jackshendrikov committed Feb 17, 2021
1 parent db2b1e0 commit 869acaf
Show file tree
Hide file tree
Showing 17 changed files with 420 additions and 37 deletions.
14 changes: 12 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</intent-filter>
</activity>

<activity android:name=".AddBookActivity"
<activity android:name=".BookAddActivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:configChanges="orientation|screenSize">
<intent-filter>
Expand All @@ -79,7 +79,7 @@
</activity>


<activity android:name=".AboutBookActivity"
<activity android:name=".BookAboutActivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:configChanges="orientation|screenSize">
<intent-filter>
Expand All @@ -98,6 +98,16 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<activity android:name=".PictureAboutActivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.PictureAboutActivity" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

import androidx.appcompat.app.AppCompatActivity;

public class AboutBookActivity extends AppCompatActivity {

public class BookAboutActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_book);

Book fullBook = (Book) getIntent().getSerializableExtra("book_full");


ImageView cover = findViewById(R.id.image_full);
assert fullBook != null;
if (fullBook.getImageUrl().equals("")) {
Expand Down Expand Up @@ -54,4 +56,4 @@ protected void onCreate(Bundle savedInstanceState) {
description.setText(fullBook.getDescription());

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon

Book currentBook = mAdapter.getItem(position);

Intent intent = new Intent(BookActivity.this, AboutBookActivity.class);
Intent intent = new Intent(BookActivity.this, BookAboutActivity.class);
intent.putExtra("book_full", currentBook);

startActivity(intent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import static ua.kpi.comsys.io8227.jackshen.BookActivity.hideSoftKeyboard;

public class AddBookActivity extends AppCompatActivity {
public class BookAddActivity extends AppCompatActivity {

String rate = "0.0";

Expand Down Expand Up @@ -96,7 +96,7 @@ public void setupUI(View view) {
public boolean onTouch(View v, MotionEvent event) {
// Hide keypad
v.performClick();
hideSoftKeyboard(AddBookActivity.this);
hideSoftKeyboard(BookAddActivity.this);
return false;
}
});
Expand Down
77 changes: 75 additions & 2 deletions app/src/main/java/ua/kpi/comsys/io8227/jackshen/Picture.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,88 @@
import java.io.Serializable;

public class Picture implements Serializable {
/** URL of image */
/** URL of the image */
private String mImageUrl;

/** Width of the image */
private String mImageWidth;

Picture(String imageUrl) {
/** Height of the image */
private String mImageHeight;

/** Number of image views */
private String mImageViews;

/** Number of image downloads */
private String mImageDownloads;

/** Number of image saves */
private String mImageFavorites;

/** Number of image likes */
private String mImageLikes;

/** User name */
private String mUserName;

/** User image */
private String mUserImage;


/**
* Create picture object
*
* @param imageUrl - the URL of the image
* @param imageWidth - width of the image
* @param imageHeight - height of the image
* @param imageViews - number of image views
* @param imageDownloads - number of image downloads
* @param imageFavorites - number of image saves
* @param imageLikes - number of image likes
* @param userName - user name
* @param userImage - user image
*/
Picture(String imageUrl, String imageWidth, String imageHeight, String imageViews,
String imageDownloads, String imageFavorites, String imageLikes, String userName,
String userImage) {
this.mImageUrl = imageUrl;
this.mImageWidth = imageWidth;
this.mImageHeight = imageHeight;
this.mImageViews = imageViews;
this.mImageDownloads = imageDownloads;
this.mImageFavorites = imageFavorites;
this.mImageLikes = imageLikes;
this.mUserName = userName;
this.mUserImage = userImage;

}

/** Return the URL of the image */
String getImageUrl() { return mImageUrl; }

/** Return width of the image */
String getImageWidth() { return mImageWidth; }

/** Return height of the image */
String getImageHeight() { return mImageHeight; }

/** Return number of image views */
String getViews() { return mImageViews; }

/** Return number of image downloads */
String getDownloads() { return mImageDownloads; }

/** Return number of image saves */
String getFavorites() { return mImageFavorites; }

/** Return number of image likes */
String getLikes() { return mImageLikes; }

/** Return user names */
String getUser() { return mUserName; }

/** Return user image */
String getUserImage() { return mUserImage; }


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package ua.kpi.comsys.io8227.jackshen;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.Objects;

public class PictureAboutActivity extends AppCompatActivity {

private static int REQUEST_CODE = 1;

Picture fullPicture;

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_image);

fullPicture = (Picture) getIntent().getSerializableExtra("image_full");

Button mDownloadButton = findViewById(R.id.action_download);

ImageView picture = findViewById(R.id.thumbnail_view);

if (Objects.requireNonNull(fullPicture).getImageUrl().equals("")) {
picture.setImageResource(R.drawable.noimage);
} else {
new PictureAdapter.DownloadImage(picture).execute(fullPicture.getImageUrl());
}

ImageView authorImage = findViewById(R.id.author_img);
if (Objects.requireNonNull(fullPicture).getUserImage().equals("")) {
authorImage.setImageResource(R.drawable.noimage);
} else {
new PictureAdapter.DownloadImage(authorImage).execute(fullPicture.getUserImage());
}

TextView author = findViewById(R.id.txt_view_author);
author.setText(fullPicture.getUser());

TextView likes = findViewById(R.id.txt_view_like);
likes.setText(fullPicture.getLikes());

TextView favorites = findViewById(R.id.txt_view_favorite);
favorites.setText(fullPicture.getFavorites());

TextView downloads = findViewById(R.id.txt_view_downloads);
String imgDownloads = "Downloads: " + fullPicture.getDownloads();
downloads.setText(imgDownloads);

TextView size = findViewById(R.id.txt_view_size);
String imgSize = "Size: " + fullPicture.getImageWidth() + "x" + fullPicture.getImageHeight();
size.setText(imgSize);

mDownloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(checkPermission()) {
downloadImage();
}
}
});
}

public boolean checkPermission()
{
int currentAPIVersion = Build.VERSION.SDK_INT;
if(currentAPIVersion>=android.os.Build.VERSION_CODES.M)
{
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("To download a file it is necessary to allow required permission");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(PictureAboutActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
}
return false;
} else {
return true;
}
} else {
return true;
}
}


private void downloadImage() {
String url = fullPicture.getImageUrl();
String name = getImageNameFromUrl(url);

DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, name);

if (downloadManager != null) {
downloadManager.enqueue(request);
} else {
Toast.makeText(PictureAboutActivity.this, "Cannot download image", Toast.LENGTH_SHORT).show();
}
}

public static String getImageNameFromUrl(String url) {
int index = url.lastIndexOf("/");
return url.substring(index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.textfield.TextInputEditText;
Expand Down Expand Up @@ -103,7 +104,9 @@ public SpannedGridLayoutManager.SpanInfo getSpanInfo(int position) {
4, // number of columns
1f // how big is default item
);

imageListView.setLayoutManager(mGridLayoutManager);
imageListView.setBackground(ContextCompat.getDrawable(this, R.drawable.border_black));

mLoadingIndicator = findViewById(R.id.progress_bar);
mLoadingIndicator.setVisibility(View.GONE);
Expand Down Expand Up @@ -166,7 +169,6 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
});



/*
* Initialize the loader
*/
Expand Down Expand Up @@ -200,7 +202,7 @@ public void onClickSearch(String searchText) {
String imageName = URLEncoder.encode(searchText, "UTF-8");

// Set the URL with the suitable imageName
mQueryText = "https://pixabay.com/api/" + "?key=" + "19193969-87191e5db266905fe8936d565" + "&q=" + imageName + "&page=" + mPageNum;
mQueryText = "https://pixabay.com/api/" + "?key=" + "19193969-87191e5db266905fe8936d565" + "&q=" + imageName + "&per_page=" + mPageNum;

// Show the loading indicator.
mLoadingIndicator.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -309,7 +311,6 @@ public void onLoadFinished(Loader<List<Picture>> loader, List<Picture> data) {
// data set. This will trigger the ListView to update
if (data != null && !data.isEmpty()) {
images.addAll(data);

HttpRequestImgHelper requestImgHelper = new HttpRequestImgHelper(PictureJSONParser.mPreviewImageUrls, this);
requestImgHelper.setOnTaskExecFinishedEvent(new HttpRequestImgHelper.OnTaskExecFinished() {
@Override
Expand Down
Loading

0 comments on commit 869acaf

Please sign in to comment.