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 & Cache #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 11 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 28

defaultConfig {
applicationId "ru.android_2019.citycam"
minSdkVersion 14
targetSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
Expand All @@ -17,11 +16,16 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

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:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.squareup.okhttp3:okhttp:3.14.0'
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.android_2019.citycam" >

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
80 changes: 73 additions & 7 deletions app/src/main/java/ru/android_2019/citycam/CityCamActivity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package ru.android_2019.citycam;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;

import ru.android_2019.citycam.asynctask.CamTask;
import ru.android_2019.citycam.asynctask.camjson.CamInfo;
import ru.android_2019.citycam.model.City;

/**
Expand All @@ -22,30 +29,89 @@ public class CityCamActivity extends AppCompatActivity {

private City city;

private ImageView camImageView;
private ImageView ivCam;
private ProgressBar progressView;
private TextView tvId;
private TextView tvTitle;
private TextView tvCategories;
private TextView tvLocation;
private TextView tvConnection;
private CamTask camTask;
private Button btnUpdate;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);


city = getIntent().getParcelableExtra(EXTRA_CITY);
if (city == null) {
Log.w(TAG, "City object not provided in extra parameter: " + EXTRA_CITY);
Log.w("CityCam", "City object not provided in extra parameter: " + EXTRA_CITY);
finish();
}

setContentView(R.layout.activity_city_cam);
camImageView = (ImageView) findViewById(R.id.cam_image);
progressView = (ProgressBar) findViewById(R.id.progress);
ivCam = findViewById(R.id.cam_image);
progressView = findViewById(R.id.progress);
tvId = findViewById(R.id.textView_id);
tvTitle = findViewById(R.id.textView_title);
tvCategories = findViewById(R.id.textView_categories);
tvLocation = findViewById(R.id.textView_location);
tvConnection = findViewById(R.id.no_connection);
btnUpdate = findViewById(R.id.button_update_image);
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnUpdate.setEnabled(false);
progressView.setVisibility(View.VISIBLE);
camTask = new CamTask((CityCamActivity) v.getContext(), city);
camTask.execute();
}
});

getSupportActionBar().setTitle(city.name);

progressView.setVisibility(View.VISIBLE);

// Здесь должен быть код, инициирующий асинхронную загрузку изображения с веб-камеры
// в выбранном городе.
if (savedInstanceState != null) {
camTask = (CamTask) getLastCustomNonConfigurationInstance();
}

if (camTask == null) {
camTask = new CamTask(this, city);
camTask.execute();
} else {
camTask.attachActivity(this);
}
}

private static final String TAG = "CityCam";
public void updateView(ArrayList<CamInfo> camInfo, Bitmap bitmap, boolean connection) {
if (camInfo != null) {
tvId.setText(getString(R.string.webcam_id) + " : " + camInfo.get(0).getId());
tvTitle.setText(getString(R.string.webcam_title) + " : " + camInfo.get(0).getTitle());
tvCategories.setText(getString(R.string.webcam_catigories) + " : " + camInfo.get(0).getCategoriesAsString());
tvLocation.setText(getString(R.string.webcam_location) + " : " + camInfo.get(0).getLocationAsString());
if (bitmap != null) {
ivCam.setImageBitmap(bitmap);
progressView.setVisibility(View.GONE);
}
btnUpdate.setEnabled(true);
} else {
tvTitle.setText(city.name + " : " + getString(R.string.not_found_webcam));
}
if (!connection)
tvConnection.setVisibility(View.VISIBLE);
else
tvConnection.setVisibility(View.GONE);
}

public void unblockBtn(){
btnUpdate.setEnabled(true);
}

@Override
public Object onRetainCustomNonConfigurationInstance() {
return camTask;
}
}
146 changes: 146 additions & 0 deletions app/src/main/java/ru/android_2019/citycam/asynctask/CamTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package ru.android_2019.citycam.asynctask;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import ru.android_2019.citycam.CityCamActivity;
import ru.android_2019.citycam.asynctask.camjson.CamInfo;
import ru.android_2019.citycam.asynctask.camjson.CamJSONReader;
import ru.android_2019.citycam.model.City;
import ru.android_2019.citycam.webcams.Webcams;

import static android.content.Context.MODE_PRIVATE;

public class CamTask extends AsyncTask<Void, Void, Void> {
private City city;
private ArrayList<CamInfo> camInfos;
private CityCamActivity activity;
private Bitmap bitmap = null;
private SharedPreferences sPref;
private boolean connection;


public CamTask(CityCamActivity activity, City city) {
this.activity = activity;
this.city = city;
this.connection = false;
}

@Override
protected Void doInBackground(Void... voids) {
String savedJSON = loadJSON(city.toString()); //Загрузка сохранненого JSONa
try {
//Попытка загрузить JSON с сервера
System.out.println("try");
String response = Utils.getJSONResponse(Webcams.createNearbyUrl(city.latitude, city.longitude));
System.out.println("try2");
//Если не удалось загрузить, то пробуем взять данные из кеша
if (response == null && savedJSON != null) {
System.out.println("00001");
camInfos = CamJSONReader.getWebcamList(savedJSON);
bitmap = getBitmapFromCache(city.toString());
connection = false;
System.out.println("00000");
} else if (response != null) {
//Если удалось загрузить, то достаемы данные, а также сохраняем их в кеш
System.out.println("dada");
camInfos = CamJSONReader.getWebcamList(response);
System.out.println("dad2");
if (camInfos != null) {
bitmap = Utils.getBitmap(new URL(camInfos.get(0).getURLPreviewImage()));
saveJSON(city.toString(), response);
saveBitmapToCache(bitmap, city.toString());
System.out.println("agaga");
}
connection = true;
}
} catch (IOException e) {
e.printStackTrace();
//Тут также: если траблы с подключением к серверу, то пытаемся взять данные из кеша
if (savedJSON != null) {
try {
camInfos = CamJSONReader.getWebcamList(savedJSON);
} catch (IOException e1) {
e.printStackTrace();
System.out.println("lol");
}
bitmap = getBitmapFromCache(city.toString());
connection = false;
}
System.out.println("lol2");
}
return null;
}

private void saveJSON(String key, String json) {
sPref = activity.getPreferences(MODE_PRIVATE);
SharedPreferences.Editor ed = sPref.edit();
ed.putString(key, json);
ed.apply();
}

private String loadJSON(String key) {
sPref = activity.getPreferences(MODE_PRIVATE);
return sPref.getString(key, null);
}

private Bitmap getBitmapFromCache(String name) {
File cacheDir = activity.getCacheDir();
String imageName = name + ".png";
File bitmapFile = new File(cacheDir + File.separator + imageName);
Bitmap result = null;
if (!bitmapFile.exists()) {
return null;
}
try (FileInputStream fileInputStream = new FileInputStream(bitmapFile)) {
result = BitmapFactory.decodeStream(fileInputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}

private void saveBitmapToCache(Bitmap bitmap, String name) {
File cacheDir = activity.getCacheDir();
String imageName = name + ".png";
File bitmapFile = new File(cacheDir + File.separator + imageName);
try (FileOutputStream fileOutputStream = new FileOutputStream(bitmapFile, false)) {
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
activity.updateView(camInfos, bitmap, connection);
activity.unblockBtn();
System.out.println("what");
}

@Override
protected void onProgressUpdate(Void... Void) {
super.onProgressUpdate(Void);
activity.updateView(camInfos, bitmap, connection);
}

public void attachActivity(CityCamActivity cityCamActivity) {
this.activity = cityCamActivity;
activity.updateView(camInfos, bitmap, connection);
}
}
43 changes: 43 additions & 0 deletions app/src/main/java/ru/android_2019/citycam/asynctask/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ru.android_2019.citycam.asynctask;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.net.URL;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import ru.android_2019.citycam.webcams.Webcams;

class Utils {
static String getJSONResponse(URL url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.addHeader(Webcams.getHeaderApiKey(), Webcams.getDevId())
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String result = response.body().string();
response.close();
return result;
} else {
response.close();
return null;
}

}

static Bitmap getBitmap(URL url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
Bitmap bitmap = BitmapFactory.decodeStream(response.body().byteStream());
response.close();
return bitmap;
}
}
Loading