-
Notifications
You must be signed in to change notification settings - Fork 20
Shmakova Anastasia - @shmakova #12
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
Open
shmakova
wants to merge
6
commits into
yamblz-native:master
Choose a base branch
from
shmakova:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
app/src/main/java/ru/yandex/yamblz/handler/DefaultCriticalSectionsHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package ru.yandex.yamblz.handler; | ||
|
|
||
| import android.os.Handler; | ||
| import android.os.Looper; | ||
|
|
||
| import java.util.concurrent.ConcurrentLinkedQueue; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
|
|
||
| import timber.log.Timber; | ||
|
|
||
| public class DefaultCriticalSectionsHandler implements CriticalSectionsHandler { | ||
| private static int MAX_QUEUE_SIZE = 6; // столько влазит на экран ._. | ||
| private ConcurrentLinkedQueue<Task> lowPriorityTasksQueue; | ||
| private CopyOnWriteArrayList<Integer> sections; | ||
| private Handler mainHandler; | ||
|
|
||
| public DefaultCriticalSectionsHandler() { | ||
| lowPriorityTasksQueue = new ConcurrentLinkedQueue<>(); | ||
| sections = new CopyOnWriteArrayList<>(); | ||
| mainHandler = new Handler(Looper.getMainLooper()); | ||
| } | ||
|
|
||
| @Override | ||
| public void startSection(int id) { | ||
| Timber.d("Start section with id=" + String.valueOf(id)); | ||
| sections.add(id); | ||
| } | ||
|
|
||
| @Override | ||
| public void stopSection(int id) { | ||
| Timber.d("Stop section with id=" + String.valueOf(id)); | ||
| sections.remove(sections.indexOf(id)); | ||
|
|
||
| if (sections.isEmpty()) { | ||
| runLowPriorityTasksQueue(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void stopSections() { | ||
| Timber.d("Stop all sections"); | ||
| sections.clear(); | ||
| runLowPriorityTasksQueue(); | ||
| } | ||
|
|
||
| @Override | ||
| public void postLowPriorityTask(Task task) { | ||
| if (sections.isEmpty()) { | ||
| Timber.d("Low priority task running"); | ||
| mainHandler.post(task::run); | ||
| } else { | ||
| Timber.d("Add to queue"); | ||
| lowPriorityTasksQueue.add(task); | ||
|
|
||
| if (lowPriorityTasksQueue.size() > MAX_QUEUE_SIZE) { | ||
| lowPriorityTasksQueue.poll(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void postLowPriorityTaskDelayed(Task task, int delay) { | ||
| Timber.d("Post low priority task with delay"); | ||
| mainHandler.postDelayed(() -> postLowPriorityTask(task), delay); | ||
| } | ||
|
|
||
| @Override | ||
| public void removeLowPriorityTask(Task task) { | ||
| lowPriorityTasksQueue.remove(); | ||
| } | ||
|
|
||
| @Override | ||
| public void removeLowPriorityTasks() { | ||
| lowPriorityTasksQueue.clear(); | ||
| } | ||
|
|
||
| private void runLowPriorityTasksQueue() { | ||
| while (!lowPriorityTasksQueue.isEmpty()) { | ||
| Timber.d("Get task from queue"); | ||
| Task task = lowPriorityTasksQueue.peek(); | ||
| postLowPriorityTask(task); | ||
| removeLowPriorityTask(task); | ||
|
|
||
| if (!sections.isEmpty()) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } |
39 changes: 0 additions & 39 deletions
39
app/src/main/java/ru/yandex/yamblz/handler/StubCriticalSectionsHandler.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
app/src/main/java/ru/yandex/yamblz/loader/DefaultCollageLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package ru.yandex.yamblz.loader; | ||
|
|
||
| import android.graphics.Bitmap; | ||
| import android.graphics.BitmapFactory; | ||
| import android.widget.ImageView; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.URL; | ||
| import java.util.List; | ||
|
|
||
| import rx.Observable; | ||
| import rx.Subscription; | ||
| import rx.android.schedulers.AndroidSchedulers; | ||
| import rx.schedulers.Schedulers; | ||
| import timber.log.Timber; | ||
|
|
||
| public class DefaultCollageLoader implements CollageLoader { | ||
| private static final int MAX_COLLAGE_SIZE = 4; | ||
|
|
||
|
|
||
| @Override | ||
| public void loadCollage(List<String> urls, ImageView imageView) { | ||
| loadCollage(urls, new DefaultImageTarget(imageView)); | ||
| } | ||
|
|
||
| @Override | ||
| public void loadCollage(List<String> urls, ImageTarget imageTarget) { | ||
| StripCollageStrategy stripCollageStrategy = new StripCollageStrategy(MAX_COLLAGE_SIZE, | ||
| StripCollageStrategy.HORIZONTAL_STRIPES); | ||
| loadCollage(urls, imageTarget, stripCollageStrategy); | ||
| } | ||
|
|
||
| @Override | ||
| public void loadCollage(List<String> urls, ImageView imageView, | ||
| CollageStrategy collageStrategy) { | ||
| loadCollage(urls, new DefaultImageTarget(imageView), collageStrategy); | ||
| } | ||
|
|
||
| @Override | ||
| public void loadCollage(List<String> urls, ImageTarget imageTarget, | ||
| CollageStrategy collageStrategy) { | ||
|
|
||
| Subscription subscription = Observable.from(urls) | ||
| .take(MAX_COLLAGE_SIZE) | ||
| .flatMap(this::loadBitmap) | ||
| .toList() | ||
| .map(collageStrategy::create) | ||
| .subscribeOn(Schedulers.io()) | ||
| .observeOn(AndroidSchedulers.mainThread()) | ||
| .subscribe(imageTarget::onLoadBitmap, | ||
| throwable -> Timber.d(throwable.getMessage()), | ||
| () -> Timber.d("Completed")); | ||
|
|
||
| imageTarget.setTag(subscription); | ||
| } | ||
|
|
||
| private Observable<Bitmap> loadBitmap(String urlString) { | ||
| return Observable.create(subscriber -> { | ||
| HttpURLConnection httpURLConnection = null; | ||
|
|
||
| try { | ||
| httpURLConnection = (HttpURLConnection) new URL(urlString).openConnection(); | ||
| subscriber.onNext(BitmapFactory.decodeStream( | ||
| httpURLConnection.getInputStream())); | ||
| subscriber.onCompleted(); | ||
| } catch (IOException e) { | ||
| subscriber.onError(e); | ||
| } finally { | ||
| if (httpURLConnection != null) { | ||
| httpURLConnection.disconnect(); | ||
| } | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| } | ||
39 changes: 39 additions & 0 deletions
39
app/src/main/java/ru/yandex/yamblz/loader/DefaultImageTarget.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package ru.yandex.yamblz.loader; | ||
|
|
||
| import android.graphics.Bitmap; | ||
| import android.widget.ImageView; | ||
|
|
||
| import java.lang.ref.WeakReference; | ||
|
|
||
| import rx.Subscription; | ||
|
|
||
| /** | ||
| * Created by shmakova on 30.07.16. | ||
| */ | ||
|
|
||
| public class DefaultImageTarget implements ImageTarget { | ||
| private WeakReference<ImageView> weakReferenceImageView; | ||
|
|
||
| public DefaultImageTarget(ImageView imageView) { | ||
| this.weakReferenceImageView = new WeakReference<>(imageView); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void onLoadBitmap(Bitmap bitmap) { | ||
| ImageView imageView = weakReferenceImageView.get(); | ||
|
|
||
| if (imageView != null) { | ||
| imageView.setImageBitmap(bitmap); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void setTag(Subscription subscription) { | ||
| ImageView imageView = weakReferenceImageView.get(); | ||
|
|
||
| if (imageView != null) { | ||
| imageView.setTag(subscription); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно переиспользовать, а не создавать на каждый элемент