Skip to content

Commit

Permalink
Merge branch 'rv_optimise' of https://github.com/archie94/haven into …
Browse files Browse the repository at this point in the history
…archie94-rv_optimise
  • Loading branch information
n8fr8 committed Apr 17, 2019
2 parents 44f1909 + b2c1967 commit 5f31227
Show file tree
Hide file tree
Showing 13 changed files with 474 additions and 204 deletions.
26 changes: 19 additions & 7 deletions src/main/java/org/havenapp/main/ui/EventActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.havenapp.main.model.EventTrigger;
import org.havenapp.main.resources.IResourceManager;
import org.havenapp.main.resources.ResourceManager;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -126,7 +127,7 @@ private void onEventFetched(@NonNull Event event) {
*/
private void onEventTriggerListFetched(@NonNull List<EventTrigger> eventTriggerList) {
this.eventTriggerList = eventTriggerList;
setEventTriggerImagePaths(eventTriggerList);
setEventTriggerImagePaths();
mAdapter.setEventTriggers(eventTriggerList);
}

Expand Down Expand Up @@ -228,20 +229,20 @@ private String generateLog () {
}

@Override
public void onVideoClick(EventTrigger eventTrigger) {
public void onVideoClick(@NotNull EventTrigger eventTrigger) {
Intent intent = new Intent(this, VideoPlayerActivity.class);
intent.setData(Uri.fromFile(new File(eventTrigger.getPath())));
startActivity(intent);
}

@Override
public void onVideoLongClick(EventTrigger eventTrigger) {
public void onVideoLongClick(@NotNull EventTrigger eventTrigger) {
shareMedia(eventTrigger);
}

@Override
public void onImageClick(EventTrigger eventTrigger) {
int startPosition = 0;
public void onImageClick(@NotNull EventTrigger eventTrigger, int position) {
int startPosition = getPositionOfImagePath(position);

ShareOverlayView overlayView = new ShareOverlayView(this);
ImageViewer viewer = new ImageViewer.Builder<>(this, eventTriggerImagePaths)
Expand All @@ -252,7 +253,7 @@ public void onImageClick(EventTrigger eventTrigger) {
}

@Override
public void onImageLongClick(EventTrigger eventTrigger) {
public void onImageLongClick(@NotNull EventTrigger eventTrigger) {
shareMedia(eventTrigger);
}

Expand All @@ -264,7 +265,7 @@ private void shareMedia (EventTrigger eventTrigger) {
startActivity(shareIntent);
}

private void setEventTriggerImagePaths(List<EventTrigger> eventTriggerList) {
private void setEventTriggerImagePaths() {
this.eventTriggerImagePaths = new ArrayList<>();
for (EventTrigger trigger : eventTriggerList)
{
Expand All @@ -275,4 +276,15 @@ private void setEventTriggerImagePaths(List<EventTrigger> eventTriggerList) {
}
}
}

private int getPositionOfImagePath(int position) {
int pos = -1;
for (int i = 0; i <= position; i++) {
if (eventTriggerList.get(i).getType() == EventTrigger.CAMERA &&
(!TextUtils.isEmpty(eventTriggerList.get(i).getPath()))) {
pos++;
}
}
return pos;
}
}
6 changes: 4 additions & 2 deletions src/main/java/org/havenapp/main/ui/EventAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void onBindViewHolder(@NonNull EventVH holder, int position) {
String desc = event.getEventTriggerCount() + " " +
resourceManager.getString(R.string.detection_events);

holder.index.setText("#" + (position + 1));
holder.title.setText(title);
holder.note.setText(desc);

Expand All @@ -58,12 +59,13 @@ public int getItemCount() {
}

class EventVH extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title, note;
TextView index, title, note;

EventVH(View itemView) {
super(itemView);

title = itemView.findViewById(R.id.event_item_title);
index = itemView.findViewById(R.id.index_number);
title = itemView.findViewById(R.id.title);
note = itemView.findViewById(R.id.event_item_desc);

itemView.setOnClickListener(this);
Expand Down
181 changes: 41 additions & 140 deletions src/main/java/org/havenapp/main/ui/EventTriggerAdapter.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package org.havenapp.main.ui;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import com.github.derlio.waveform.SimpleWaveformView;
import com.github.derlio.waveform.soundfile.SoundFile;

import org.havenapp.main.R;
import org.havenapp.main.model.EventTrigger;
import org.havenapp.main.resources.IResourceManager;
import org.havenapp.main.ui.viewholder.AudioVH;
import org.havenapp.main.ui.viewholder.EventTriggerVH;
import org.havenapp.main.ui.viewholder.ImageVH;
import org.havenapp.main.ui.viewholder.VideoVH;

import java.io.File;
import java.util.List;

import androidx.annotation.NonNull;
Expand All @@ -30,16 +22,14 @@
* Created by n8fr8 on 4/16/17.
*/

public class EventTriggerAdapter extends RecyclerView.Adapter<EventTriggerAdapter.EventTriggerVH> {
public class EventTriggerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context context;
private IResourceManager resourceManager;
private List<EventTrigger> eventTriggers;

private EventTriggerClickListener eventTriggerClickListener;

private final static String AUTHORITY = "org.havenapp.main.fileprovider";

EventTriggerAdapter(Context context, @NonNull List<EventTrigger> eventTriggers,
IResourceManager resourceManager, EventTriggerClickListener eventTriggerClickListener) {
this.context = context;
Expand All @@ -55,129 +45,61 @@ void setEventTriggers(@NonNull List<EventTrigger> eventTriggers) {

@NonNull
@Override
public EventTriggerVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_event, parent, false);

return new EventTriggerVH(view);
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

switch (viewType) {
case EventTrigger.CAMERA_VIDEO:
return new VideoVH(eventTriggerClickListener, context, resourceManager, parent);
case EventTrigger.CAMERA:
return new ImageVH(resourceManager, eventTriggerClickListener, parent);
case EventTrigger.MICROPHONE:
return new AudioVH(resourceManager, parent);
case EventTrigger.ACCELEROMETER:
case EventTrigger.LIGHT:
case EventTrigger.PRESSURE:
case EventTrigger.POWER:
case EventTrigger.BUMP:
return new EventTriggerVH(resourceManager, parent);
}
return new RecyclerView.ViewHolder(new View(context)) {};
}

@Override
public void onBindViewHolder(@NonNull EventTriggerVH holder, int position) {
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

final EventTrigger eventTrigger = eventTriggers.get(position);

String title = eventTrigger.getStringType(resourceManager);
String desc = eventTrigger.getTime().toLocaleString();

holder.image.setVisibility(View.GONE);
holder.video.setVisibility(View.GONE);
holder.extra.setVisibility(View.GONE);
holder.sound.setVisibility(View.GONE);


if (eventTrigger.getPath() != null)
if (eventTrigger.getPath() != null && eventTrigger.getType() != null)
{
switch (eventTrigger.getType()) {
case EventTrigger.CAMERA_VIDEO:
holder.video.setVisibility(View.VISIBLE);
BitmapDrawable bitmapD = new BitmapDrawable(context.getResources(),
ThumbnailUtils.createVideoThumbnail(eventTrigger.getPath(),
MediaStore.Video.Thumbnails.FULL_SCREEN_KIND));
holder.video.setBackground(bitmapD);
holder.video.setOnClickListener(view -> {
if (eventTriggerClickListener != null) {
eventTriggerClickListener.onVideoClick(eventTrigger);
}
});

holder.video.setOnLongClickListener(view -> {
if (eventTriggerClickListener != null) {
eventTriggerClickListener.onVideoLongClick(eventTrigger);
}
return false;
});
((VideoVH) holder).bind(eventTrigger, position);
break;
case EventTrigger.CAMERA:
holder.image.setVisibility(View.VISIBLE);

/**
Uri fileUri = FileProvider.getUriForFile(
context,
AUTHORITY,
new File(eventTrigger.getPath()));
holder.image.setImageURI(fileUri);
**/

Uri fileUri = Uri.parse("file://" + eventTrigger.getPath());
holder.image.setImageURI(fileUri);


holder.image.setOnClickListener(view -> {
if (eventTriggerClickListener != null)
eventTriggerClickListener.onImageClick(eventTrigger);
});

holder.image.setOnLongClickListener(view -> {
if (eventTriggerClickListener != null)
eventTriggerClickListener.onImageLongClick(eventTrigger);
return false;
});
((ImageVH) holder).bind(eventTrigger, position);
break;
case EventTrigger.MICROPHONE:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

holder.sound.setVisibility(View.VISIBLE);
final File fileSound = new File(eventTrigger.getPath());
try {
final SoundFile soundFile = SoundFile.create(fileSound.getPath(), new SoundFile.ProgressListener() {
int lastProgress = 0;

@Override
public boolean reportProgress(double fractionComplete) {
final int progress = (int) (fractionComplete * 100);
if (lastProgress == progress) {
return true;
}
lastProgress = progress;

return true;
}
});
holder.sound.setAudioFile(soundFile);
holder.sound.invalidate();
} catch (Exception e) {
}

holder.extra.setVisibility(View.VISIBLE);
holder.extra.removeAllViews();

AudioWife audioWife = new AudioWife();
audioWife.init(context, Uri.fromFile(fileSound))
.useDefaultUi(holder.extra, inflater);

((AudioVH) holder).bind(eventTrigger, context, position);
break;
case EventTrigger.ACCELEROMETER:
desc += "\n" + resourceManager.getString(R.string.data_speed) + ": " + eventTrigger.getPath();

case EventTrigger.BUMP:
((EventTriggerVH) holder)
.bind(eventTrigger, resourceManager.getString(R.string.data_speed), position);
break;
case EventTrigger.LIGHT:
desc += "\n" + resourceManager.getString(R.string.data_light) + ": " + eventTrigger.getPath();

((EventTriggerVH) holder)
.bind(eventTrigger, resourceManager.getString(R.string.data_light), position);
break;
case EventTrigger.PRESSURE:
desc += "\n" + resourceManager.getString(R.string.data_pressure) + ": " + eventTrigger.getPath();
((EventTriggerVH) holder)
.bind(eventTrigger, resourceManager.getString(R.string.data_pressure), position);
break;
case EventTrigger.POWER:
desc += "\n" + resourceManager.getString(R.string.data_power) + ": " + eventTrigger.getPath();
((EventTriggerVH) holder)
.bind(eventTrigger, resourceManager.getString(R.string.data_power), position);
break;
}

}

holder.title.setText(title);
holder.note.setText(desc);


}

@Override
Expand All @@ -192,32 +114,11 @@ public int getItemCount() {
return eventTriggers.size();
}

class EventTriggerVH extends RecyclerView.ViewHolder {
TextView title, note;
ImageView image;
VideoView video;
ViewGroup extra;
SimpleWaveformView sound;
EventTriggerVH(View itemView) {
super(itemView);

title = itemView.findViewById(R.id.event_item_title);
note = itemView.findViewById(R.id.event_item_desc);
image = itemView.findViewById(R.id.event_item_image);
video = itemView.findViewById(R.id.event_item_video);
extra = itemView.findViewById(R.id.event_item_extra);
sound = itemView.findViewById(R.id.event_item_sound);
}
}

public interface EventTriggerClickListener {
void onVideoClick(EventTrigger eventTrigger);

void onVideoLongClick(EventTrigger eventTrigger);

void onImageClick(EventTrigger eventTrigger);

void onImageLongClick(EventTrigger eventTrigger);
@Override
public int getItemViewType(int position) {
return eventTriggers.get(position).getType();
}

public interface EventTriggerClickListener extends VideoVH.VideoClickListener,
ImageVH.ImageClickListener {}
}
Loading

0 comments on commit 5f31227

Please sign in to comment.