mirror of https://github.com/FongMi/TV.git
parent
95a9fa0129
commit
e834e01d83
@ -0,0 +1,81 @@ |
||||
package com.fongmi.android.tv.player; |
||||
|
||||
import android.os.CountDownTimer; |
||||
|
||||
import com.fongmi.android.tv.event.ActionEvent; |
||||
|
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
public class Timer { |
||||
|
||||
private CountDownTimer timer; |
||||
private Callback callback; |
||||
private long tick; |
||||
|
||||
private static class Loader { |
||||
static volatile Timer INSTANCE = new Timer(); |
||||
} |
||||
|
||||
public static Timer get() { |
||||
return Loader.INSTANCE; |
||||
} |
||||
|
||||
public boolean isRunning() { |
||||
return timer != null; |
||||
} |
||||
|
||||
public void setCallback(Callback callback) { |
||||
this.callback = callback; |
||||
} |
||||
|
||||
public long getTick() { |
||||
return tick; |
||||
} |
||||
|
||||
public void set(long t) { |
||||
timer = new CountDownTimer(t, 1000) { |
||||
@Override |
||||
public void onTick(long tick) { |
||||
Timer.this.onTick(tick); |
||||
} |
||||
|
||||
@Override |
||||
public void onFinish() { |
||||
Timer.this.onFinish(); |
||||
} |
||||
}.start(); |
||||
} |
||||
|
||||
private void onTick(long tick) { |
||||
this.tick = tick; |
||||
if (callback != null) callback.onTick(tick); |
||||
} |
||||
|
||||
private void onFinish() { |
||||
if (callback != null) callback.onFinish(); |
||||
ActionEvent.pause(); |
||||
reset(); |
||||
} |
||||
|
||||
public void delay() { |
||||
cancel(); |
||||
set(TimeUnit.MINUTES.toMillis(5) + tick); |
||||
} |
||||
|
||||
public void reset() { |
||||
tick = 0; |
||||
cancel(); |
||||
} |
||||
|
||||
public void cancel() { |
||||
if (timer != null) timer.cancel(); |
||||
timer = null; |
||||
} |
||||
|
||||
public interface Callback { |
||||
|
||||
void onTick(long tick); |
||||
|
||||
void onFinish(); |
||||
} |
||||
} |
||||
@ -0,0 +1,95 @@ |
||||
package com.fongmi.android.tv.ui.dialog; |
||||
|
||||
import android.view.LayoutInflater; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.annotation.Nullable; |
||||
import androidx.fragment.app.Fragment; |
||||
import androidx.fragment.app.FragmentActivity; |
||||
import androidx.viewbinding.ViewBinding; |
||||
|
||||
import com.fongmi.android.tv.databinding.DialogTimerBinding; |
||||
import com.fongmi.android.tv.player.Timer; |
||||
import com.fongmi.android.tv.utils.Util; |
||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment; |
||||
|
||||
import java.util.Formatter; |
||||
import java.util.Locale; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
public class TimerDialog extends BaseDialog implements Timer.Callback { |
||||
|
||||
private DialogTimerBinding binding; |
||||
private StringBuilder builder; |
||||
private Formatter formatter; |
||||
|
||||
public static TimerDialog create() { |
||||
return new TimerDialog(); |
||||
} |
||||
|
||||
public TimerDialog() { |
||||
builder = new StringBuilder(); |
||||
formatter = new Formatter(builder, Locale.getDefault()); |
||||
} |
||||
|
||||
public void show(FragmentActivity activity) { |
||||
for (Fragment f : activity.getSupportFragmentManager().getFragments()) if (f instanceof BottomSheetDialogFragment) return; |
||||
show(activity.getSupportFragmentManager(), null); |
||||
} |
||||
|
||||
@Override |
||||
protected ViewBinding getBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) { |
||||
return binding = DialogTimerBinding.inflate(inflater, container, false); |
||||
} |
||||
|
||||
@Override |
||||
protected void initView() { |
||||
onTick(Timer.get().getTick()); |
||||
binding.list.setVisibility(Timer.get().isRunning() ? View.GONE : View.VISIBLE); |
||||
binding.timer.setVisibility(Timer.get().isRunning() ? View.VISIBLE : View.GONE); |
||||
} |
||||
|
||||
@Override |
||||
protected void initEvent() { |
||||
Timer.get().setCallback(this); |
||||
binding.delay.setOnClickListener(this::onDelay); |
||||
binding.reset.setOnClickListener(this::onReset); |
||||
binding.time1.setOnClickListener(this::setTimer); |
||||
binding.time2.setOnClickListener(this::setTimer); |
||||
binding.time3.setOnClickListener(this::setTimer); |
||||
binding.time4.setOnClickListener(this::setTimer); |
||||
} |
||||
|
||||
private void setTimer(View view) { |
||||
int minutes = Integer.parseInt(view.getTag().toString()); |
||||
Timer.get().set(TimeUnit.MINUTES.toMillis(minutes)); |
||||
dismiss(); |
||||
} |
||||
|
||||
private void onDelay(View view) { |
||||
Timer.get().delay(); |
||||
} |
||||
|
||||
private void onReset(View view) { |
||||
Timer.get().reset(); |
||||
dismiss(); |
||||
} |
||||
|
||||
@Override |
||||
public void onTick(long tick) { |
||||
binding.tick.setText(Util.format(builder, formatter, tick)); |
||||
} |
||||
|
||||
@Override |
||||
public void onFinish() { |
||||
dismiss(); |
||||
} |
||||
|
||||
@Override |
||||
public void dismiss() { |
||||
Timer.get().setCallback(null); |
||||
super.dismiss(); |
||||
} |
||||
} |
||||
@ -0,0 +1,120 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="vertical"> |
||||
|
||||
<TextView |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginStart="16dp" |
||||
android:layout_marginTop="16dp" |
||||
android:text="@string/play_timer" |
||||
android:textColor="?android:attr/textColorPrimary" |
||||
android:textSize="16sp" /> |
||||
|
||||
<LinearLayout |
||||
android:id="@+id/list" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="vertical" |
||||
android:paddingStart="16dp" |
||||
android:paddingTop="8dp" |
||||
android:paddingEnd="16dp" |
||||
android:paddingBottom="8dp" |
||||
android:visibility="gone" |
||||
tools:visibility="visible"> |
||||
|
||||
<TextView |
||||
android:id="@+id/time1" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginTop="8dp" |
||||
android:layout_marginBottom="8dp" |
||||
android:background="@drawable/shape_accent" |
||||
android:tag="5" |
||||
android:text="@string/timer_5" |
||||
android:textColor="?android:attr/textColorPrimary" |
||||
android:textSize="14sp" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/time2" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginTop="8dp" |
||||
android:layout_marginBottom="8dp" |
||||
android:background="@drawable/shape_accent" |
||||
android:tag="15" |
||||
android:text="@string/timer_15" |
||||
android:textColor="?android:attr/textColorPrimary" |
||||
android:textSize="14sp" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/time3" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginTop="8dp" |
||||
android:layout_marginBottom="8dp" |
||||
android:background="@drawable/shape_accent" |
||||
android:tag="30" |
||||
android:text="@string/timer_30" |
||||
android:textColor="?android:attr/textColorPrimary" |
||||
android:textSize="14sp" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/time4" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginTop="8dp" |
||||
android:layout_marginBottom="8dp" |
||||
android:background="@drawable/shape_accent" |
||||
android:tag="60" |
||||
android:text="@string/timer_60" |
||||
android:textColor="?android:attr/textColorPrimary" |
||||
android:textSize="14sp" /> |
||||
|
||||
</LinearLayout> |
||||
|
||||
<LinearLayout |
||||
android:id="@+id/timer" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:gravity="center" |
||||
android:orientation="vertical" |
||||
android:visibility="gone" |
||||
tools:visibility="visible"> |
||||
|
||||
<TextView |
||||
android:id="@+id/tick" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginTop="24dp" |
||||
android:layout_marginBottom="24dp" |
||||
android:textColor="?android:attr/textColorPrimary" |
||||
android:textSize="48sp" |
||||
android:textStyle="bold" |
||||
tools:text="5:00" /> |
||||
|
||||
<com.google.android.material.button.MaterialButton |
||||
android:id="@+id/delay" |
||||
style="?attr/materialButtonStyle" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginBottom="8dp" |
||||
android:singleLine="true" |
||||
android:text="@string/timer_delay" |
||||
android:textColor="@color/white" /> |
||||
|
||||
<com.google.android.material.button.MaterialButton |
||||
android:id="@+id/reset" |
||||
style="?attr/materialButtonOutlinedStyle" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginBottom="16dp" |
||||
android:singleLine="true" |
||||
android:text="@string/timer_cancel" |
||||
android:textColor="?android:attr/textColorPrimary" /> |
||||
|
||||
</LinearLayout> |
||||
</LinearLayout> |
||||
Loading…
Reference in new issue