290 lines
11 KiB
Java
290 lines
11 KiB
Java
package com.xxpatx.os.alarm;
|
||
|
||
import android.content.Intent;
|
||
import android.graphics.Bitmap;
|
||
import android.media.AudioAttributes;
|
||
import android.media.MediaPlayer;
|
||
import android.text.TextUtils;
|
||
import android.util.Log;
|
||
import android.view.LayoutInflater;
|
||
import android.view.View;
|
||
import android.view.ViewGroup;
|
||
import android.widget.ImageView;
|
||
import android.widget.TextView;
|
||
|
||
import androidx.annotation.NonNull;
|
||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||
import androidx.fragment.app.FragmentActivity;
|
||
import androidx.recyclerview.widget.RecyclerView;
|
||
|
||
import com.bumptech.glide.Glide;
|
||
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
|
||
import com.bumptech.glide.request.RequestOptions;
|
||
import com.xxpatx.os.R;
|
||
import com.xxpatx.os.activity.alarm.edit.AlarmEditActivity;
|
||
import com.xxpatx.os.utils.FFmpegUtils;
|
||
import com.xxpatx.os.utils.ScreenUtils;
|
||
import com.xxpatx.os.utils.Utils;
|
||
import com.xxpatx.os.view.JzvdStdRound;
|
||
import com.xxpatx.os.view.ToggleButton;
|
||
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.text.ParseException;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.Date;
|
||
import java.util.List;
|
||
import java.util.regex.Pattern;
|
||
|
||
import io.reactivex.rxjava3.core.Observer;
|
||
import io.reactivex.rxjava3.disposables.Disposable;
|
||
|
||
public class AlarmAdapter extends RecyclerView.Adapter<AlarmAdapter.holder> {
|
||
|
||
private FragmentActivity mContext;
|
||
private List<AlarmClockData> mAlarmClockData;
|
||
private OnLongClickListener mOnLongClickListener;
|
||
|
||
public void setAlarmClockData(List<AlarmClockData> alarmClockData) {
|
||
this.mAlarmClockData = alarmClockData;
|
||
notifyDataSetChanged();
|
||
}
|
||
|
||
public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
|
||
this.mOnLongClickListener = onLongClickListener;
|
||
}
|
||
|
||
public interface OnLongClickListener {
|
||
void onLongClick(AlarmClockData alarmClockData);
|
||
}
|
||
|
||
@NonNull
|
||
@Override
|
||
public holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||
mContext = (FragmentActivity) parent.getContext();
|
||
return new AlarmAdapter.holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_alarm_port, parent, false));
|
||
}
|
||
|
||
@Override
|
||
public void onBindViewHolder(@NonNull holder holder, int position) {
|
||
AlarmClockData alarmClockData = mAlarmClockData.get(position);
|
||
switch (alarmClockData.getType()) {
|
||
case AlarmUtils.ONCE:
|
||
try {
|
||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||
Date date = sdf.parse(alarmClockData.getTime());
|
||
SimpleDateFormat hours = new SimpleDateFormat("HH:mm");
|
||
String time = hours.format(date);
|
||
holder.tv_time.setText(time);
|
||
} catch (ParseException e) {
|
||
e.printStackTrace();
|
||
}
|
||
holder.tv_remind_type.setText("一次");
|
||
holder.tv_remind_type.setBackgroundResource(R.drawable.tv_times_bg_once);
|
||
break;
|
||
case AlarmUtils.LOOP:
|
||
holder.tv_time.setText(alarmClockData.getTime());
|
||
holder.tv_remind_type.setText("每天");
|
||
holder.tv_remind_type.setBackgroundResource(R.drawable.tv_times_bg_loop);
|
||
break;
|
||
case AlarmUtils.WORKING_DAY:
|
||
holder.tv_time.setText(alarmClockData.getTime());
|
||
holder.tv_remind_type.setText("周一至周五");
|
||
holder.tv_remind_type.setBackgroundResource(R.drawable.tv_times_bg_work);
|
||
break;
|
||
case AlarmUtils.OFF_DAY:
|
||
holder.tv_time.setText(alarmClockData.getTime());
|
||
holder.tv_remind_type.setText("周六至周日");
|
||
holder.tv_remind_type.setBackgroundResource(R.drawable.tv_times_bg_offday);
|
||
break;
|
||
default:
|
||
}
|
||
String title = alarmClockData.getTitle();
|
||
if (TextUtils.isEmpty(title)) {
|
||
holder.tv_title.setText("无标题");
|
||
} else {
|
||
holder.tv_title.setText(title);
|
||
}
|
||
int is_onoff = alarmClockData.getIs_onoff();
|
||
holder.toggleButton2.setDisable(true);
|
||
if (is_onoff == 1) {
|
||
holder.tv_status.setText("已开启");
|
||
holder.toggleButton2.setToggleOn(false);
|
||
} else {
|
||
holder.tv_status.setText("已关闭");
|
||
holder.toggleButton2.setToggleOff(false);
|
||
}
|
||
int type = alarmClockData.getClazz();
|
||
switch (type){
|
||
case 1:
|
||
default:
|
||
holder.iv_type.setImageDrawable(mContext.getDrawable(R.drawable.icon_alarm_medicine_pressed));
|
||
break;
|
||
case 2:
|
||
holder.iv_type.setImageDrawable(mContext.getDrawable(R.drawable.icon_alarm_look_pressed));
|
||
break;
|
||
case 3:
|
||
holder.iv_type.setImageDrawable(mContext.getDrawable(R.drawable.icon_alarm_reserve_pressed));
|
||
break;
|
||
}
|
||
String voice = alarmClockData.getVoice();
|
||
if (TextUtils.isEmpty(voice)) {
|
||
holder.cl_voice.setVisibility(View.GONE);
|
||
} else {
|
||
holder.cl_voice.setVisibility(View.VISIBLE);
|
||
MediaPlayer mMediaPlayer = new MediaPlayer();
|
||
mMediaPlayer.setAudioAttributes(
|
||
new AudioAttributes.Builder()
|
||
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
|
||
.build()
|
||
);
|
||
mMediaPlayer.setOnCompletionListener(mp -> Log.e("setOnCompletionListener", "onCompletion: "));
|
||
mMediaPlayer.setOnPreparedListener(mp -> Log.e("setOnPreparedListener", "onPrepared: "));
|
||
mMediaPlayer.setOnErrorListener((mp, what, extra) -> false);
|
||
//设置音频文件到MediaPlayer对象中
|
||
try {
|
||
mMediaPlayer.setDataSource(voice);
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
//让MediaPlayer对象准备,用这个方法防止加载时耗时导致anr
|
||
mMediaPlayer.prepareAsync();
|
||
FFmpegUtils.getDurationInMilliseconds(voice, new Observer<Integer>() {
|
||
@Override
|
||
public void onSubscribe(@NonNull Disposable d) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onNext(@NonNull Integer integer) {
|
||
holder.tv_voice.setText(integer + "秒");
|
||
}
|
||
|
||
@Override
|
||
public void onError(@NonNull Throwable e) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onComplete() {
|
||
|
||
}
|
||
});
|
||
holder.cl_voice.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
mMediaPlayer.start();
|
||
}
|
||
});
|
||
}
|
||
String fileUrl = alarmClockData.getFile();
|
||
if (TextUtils.isEmpty(fileUrl)) {
|
||
holder.cl_vp.setVisibility(View.GONE);
|
||
} else {
|
||
holder.cl_vp.setVisibility(View.VISIBLE);
|
||
if (isImgUrl(fileUrl)) {
|
||
holder.imageView.setVisibility(View.VISIBLE);
|
||
holder.jz_video.setVisibility(View.GONE);
|
||
if (!mContext.isDestroyed()) {
|
||
RequestOptions options = new RequestOptions().transform(new RoundedCorners(ScreenUtils.dip2px(mContext, 16F)));
|
||
Glide.with(mContext).load(fileUrl).apply(options).into(holder.imageView);
|
||
}
|
||
} else {
|
||
holder.imageView.setVisibility(View.GONE);
|
||
holder.jz_video.setVisibility(View.VISIBLE);
|
||
String fileName = Utils.getFileNamefromURL(fileUrl);
|
||
File file = new File(Utils.getDownLoadPath(mContext) + fileName);
|
||
String path;
|
||
if (file.exists() && !file.isDirectory()) {
|
||
path = file.getAbsolutePath();
|
||
} else {
|
||
path = fileUrl;
|
||
}
|
||
holder.jz_video.setUp(path, "");
|
||
holder.jz_video.startButton.setImageDrawable(mContext.getDrawable(R.drawable.play));
|
||
FFmpegUtils.loadVideoScreenshot(path, new Observer<Bitmap>() {
|
||
@Override
|
||
public void onSubscribe(@NonNull Disposable d) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onNext(@NonNull Bitmap bitmap) {
|
||
if (!mContext.isDestroyed()) {
|
||
RequestOptions options = new RequestOptions().transform(new RoundedCorners(ScreenUtils.dip2px(mContext, 16F)));
|
||
Glide.with(mContext).load(bitmap).apply(options).into(holder.jz_video.posterImageView);
|
||
// Glide.with(mContext).load(bitmap).into(holder.imageView);
|
||
}
|
||
holder.jz_video.startButton.setImageDrawable(mContext.getDrawable(R.drawable.play));
|
||
}
|
||
|
||
@Override
|
||
public void onError(@NonNull Throwable e) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onComplete() {
|
||
|
||
}
|
||
});
|
||
|
||
}
|
||
}
|
||
holder.root.setOnLongClickListener(new View.OnLongClickListener() {
|
||
@Override
|
||
public boolean onLongClick(View view) {
|
||
mOnLongClickListener.onLongClick(alarmClockData);
|
||
return false;
|
||
}
|
||
});
|
||
holder.root.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View view) {
|
||
Intent intent = new Intent(mContext, AlarmEditActivity.class);
|
||
intent.putExtra("AlarmClockData", alarmClockData);
|
||
mContext.startActivity(intent);
|
||
}
|
||
});
|
||
|
||
}
|
||
|
||
private Pattern mCompile = Pattern.compile(".*?(gif|jpeg|png|jpg|bmp|webp)");
|
||
|
||
private boolean isImgUrl(String url) {
|
||
if (TextUtils.isEmpty(url) || url.trim().length() == 0) {
|
||
return false;
|
||
}
|
||
return mCompile.matcher(url).matches();
|
||
}
|
||
|
||
@Override
|
||
public int getItemCount() {
|
||
return mAlarmClockData == null ? 0 : mAlarmClockData.size();
|
||
}
|
||
|
||
class holder extends RecyclerView.ViewHolder {
|
||
TextView tv_time, tv_remind_type, tv_title, tv_voice, tv_status;
|
||
ConstraintLayout cl_voice, cl_vp, root;
|
||
JzvdStdRound jz_video;
|
||
ImageView imageView,iv_type;
|
||
ToggleButton toggleButton2;
|
||
|
||
public holder(@NonNull View itemView) {
|
||
super(itemView);
|
||
tv_time = itemView.findViewById(R.id.tv_time);
|
||
tv_remind_type = itemView.findViewById(R.id.tv_remind_type);
|
||
tv_title = itemView.findViewById(R.id.tv_title);
|
||
tv_voice = itemView.findViewById(R.id.tv_voice);
|
||
cl_voice = itemView.findViewById(R.id.cl_voice);
|
||
cl_vp = itemView.findViewById(R.id.cl_vp);
|
||
root = itemView.findViewById(R.id.root);
|
||
jz_video = itemView.findViewById(R.id.jz_video);
|
||
imageView = itemView.findViewById(R.id.imageView);
|
||
iv_type = itemView.findViewById(R.id.iv_type);
|
||
tv_status = itemView.findViewById(R.id.tv_status);
|
||
toggleButton2 = itemView.findViewById(R.id.toggleButton2);
|
||
}
|
||
}
|
||
}
|