Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncTask with support LRUCache. Ruslan Murzin #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Code refactoring
  • Loading branch information
rusyamurzin committed Apr 4, 2019
commit fd323f9b71be8be1ded326459605205fefc81fc2
9 changes: 4 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
applicationId "ru.android_2019.citycam"
Expand All @@ -20,8 +19,8 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:23.4.0'
implementation 'com.android.support:recyclerview-v7:23.4.0'
}
235 changes: 9 additions & 226 deletions app/src/main/java/ru/android_2019/citycam/CityCamActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.JsonReader;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
Expand All @@ -14,10 +13,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
Expand All @@ -26,6 +23,8 @@

import ru.android_2019.citycam.cache.MyCache;
import ru.android_2019.citycam.model.City;
import ru.android_2019.citycam.reader.ResponseJsonReader;
import ru.android_2019.citycam.reader.WebcamsMessage;
import ru.android_2019.citycam.webcams.Webcams;

/**
Expand Down Expand Up @@ -105,7 +104,7 @@ public Object onRetainCustomNonConfigurationInstance() {
}


public class DownloadImageTask extends AsyncTask<URL, Integer, DownloadImageTask.WebcamsMessage> {
public class DownloadImageTask extends AsyncTask<URL, Integer, WebcamsMessage> {

private CityCamActivity activity;
private Integer progress = 0;
Expand All @@ -130,14 +129,10 @@ void updateView() {
else {
activity.errorTextView.setVisibility(View.INVISIBLE);
activity.camImageView.setImageBitmap(mess.getBitmap());
String idText = getResources().getString(R.string.id_cam)+mess.getId();
activity.idTextView.setText(idText);
String titleText = getResources().getString(R.string.title_cam)+mess.getTitle();
activity.titleTextView.setText(titleText);
String timezoneText = getResources().getString(R.string.timezone_cam)+mess.getTimezone();
activity.timezoneTextView.setText(timezoneText);
String countViewsText = getResources().getString(R.string.views_cam)+mess.getViews();
activity.countViewsTextView.setText(countViewsText);
activity.idTextView.setText(getString(R.string.id_cam, mess.getId()));
activity.titleTextView.setText(getString(R.string.title_cam, mess.getTitle()));
activity.timezoneTextView.setText(getString(R.string.timezone_cam, mess.getTimezone()));
activity.countViewsTextView.setText(getString(R.string.views_cam, mess.getViews()));
}
}
}
Expand Down Expand Up @@ -247,7 +242,8 @@ private WebcamsMessage downloadWebcamsMessageByUrl(URL url) throws IOException {
}
stream = connection.getInputStream();
if (stream != null) {
List<WebcamsMessage> messagesList = readJsonStream(stream);
ResponseJsonReader reader = new ResponseJsonReader();
List<WebcamsMessage> messagesList = reader.readJsonStream(stream);
Iterator iter = messagesList.iterator();
WebcamsMessage message = null;
while (iter.hasNext()) {
Expand Down Expand Up @@ -277,162 +273,6 @@ private WebcamsMessage downloadWebcamsMessageByUrl(URL url) throws IOException {
return result;
}

public List<WebcamsMessage> readJsonStream(InputStream in) throws IOException {
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
try {
return readResult(reader);
} finally {
reader.close();
}
}

public List<WebcamsMessage> readResult(JsonReader reader) throws IOException {
List<WebcamsMessage> messages = new ArrayList<>();

reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("result")) {
messages = readWebcams(reader);
} else {
reader.skipValue();
}
}
reader.endObject();
return messages;
}

public List<WebcamsMessage> readWebcams(JsonReader reader) throws IOException {
List<WebcamsMessage> messages = new ArrayList<>();
Integer total = -1;

reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("total")) {
total = reader.nextInt();
}
else if (name.equals("webcams") && total > 0) {
messages = readArrayCams(reader);
}
else {
reader.skipValue();
}
}
reader.endObject();
//Если не найдено ни одной камеры
if (total == 0) {
Log.w(TAG, "There is no camera available");
throw new IOException("There is no camera available");
}
return messages;
}

public List<WebcamsMessage> readArrayCams(JsonReader reader) throws IOException {
List<WebcamsMessage> messages = new ArrayList<>();

reader.beginArray();
while (reader.hasNext()) {
messages.add(readWebcamsMessage(reader));
}
reader.endArray();
return messages;
}

public WebcamsMessage readWebcamsMessage(JsonReader reader) throws IOException {
String id = null;
String status = null;
String title = null;
String preview = null;
String timezone = null;
Integer views = -1;

reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("id")) {
id = reader.nextString();
}
else if (name.equals("status")) {
status = reader.nextString();
}
else if (name.equals("title")) {
title = reader.nextString();
}
else if (name.equals("image")) {
preview = readCurrent(reader);
}
else if (name.equals("location")) {
timezone = readTimeZone(reader);
}
else if (name.equals("statistics")) {
reader.beginObject();
reader.nextName();
views = reader.nextInt();
reader.endObject();
}
else {
reader.skipValue();
}
}
reader.endObject();
return new WebcamsMessage(id, status, title, preview, timezone, views);
}


public String readCurrent(JsonReader reader) throws IOException {
String preview = null;

reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("current")) {
preview = readPreview(reader);
}
else {
reader.skipValue();
}
}
reader.endObject();

return preview;
}
public String readPreview(JsonReader reader) throws IOException {
String preview = null;

reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("preview")) {
preview = reader.nextString();
}
else {
reader.skipValue();
}
}
reader.endObject();

return preview;
}

public String readTimeZone(JsonReader reader) throws IOException {
String timezone = null;

reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("timezone")) {
timezone = reader.nextString();
}
else {
reader.skipValue();
}
}
reader.endObject();

return timezone;
}

public WebcamsMessage getMess() {
return mess;
}
Expand All @@ -448,63 +288,6 @@ public void setProgress(Integer progress) {
public void setErrorOccured(Boolean errorOccured) {
this.errorOccured = errorOccured;
}

//["result"]["webcams"][0]["id"] ("1350096618")
//["result"]["webcams"][0]["status"] if "active"
//["result"]["webcams"][0]["title"] ("St Petersburg: Saint Petersburg − Intermodal Terminal")
//["result"]["webcams"][0]["image"]["current"]["preview"] ("https://images.webcams.travel/preview/1240664025.jpg")
//["result"]["webcams"][0]["location"]["timezone"] ("Europe/Moscow")
//["result"]["webcams"][0]["statistics"]["views"] (28718)
public class WebcamsMessage {
private String id;
private String status;
private String title;
private String preview;
private String timezone;
private Integer views;
private Bitmap bitmap = null;

public WebcamsMessage(String id, String status, String title, String preview, String timezone, Integer views) {
this.id = id;
this.status = status;
this.title = title;
this.preview = preview;
this.timezone = timezone;
this.views = views;
}

public String getId() {
return id;
}

public String getStatus() {
return status;
}

public String getTitle() {
return title;
}

public String getPreview() {
return preview;
}

public String getTimezone() {
return timezone;
}

public Integer getViews() {
return views;
}

public Bitmap getBitmap() {
return bitmap;
}

public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
}
}

private static final String TAG = "CityCam";
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/ru/android_2019/citycam/cache/MyCache.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ru.android_2019.citycam.cache;

import android.util.LruCache;
import ru.android_2019.citycam.CityCamActivity;
import ru.android_2019.citycam.reader.WebcamsMessage;

public class MyCache {

private static MyCache instance;
private LruCache<String, CityCamActivity.DownloadImageTask.WebcamsMessage> lru;
private LruCache<String, WebcamsMessage> lru;
private int cacheSize = 8 * 1024 * 1024; //8 MiB

private MyCache() {
Expand All @@ -20,7 +20,7 @@ public static MyCache getInstance() {
return instance;
}

public LruCache<String, CityCamActivity.DownloadImageTask.WebcamsMessage> getLru() {
public LruCache<String, WebcamsMessage> getLru() {
return lru;
}
}
Loading