package com.morrmat.radiowl;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.morrmat.radiowl.player.PlaybackStatus;
import com.morrmat.radiowl.player.RadioManager;
import com.morrmat.radiowl.util.Shoutcast;
import com.morrmat.radiowl.util.ShoutcastHelper;
import com.morrmat.radiowl.util.ShoutcastListAdapter;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.concurrent.TimeUnit;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;
public class MainActivity extends AppCompatActivity {
//@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.playTrigger)
ImageButton trigger;
@BindView(R.id.listview)
ListView listView;
//@BindView(R.id.name)
TextView textView;
//@BindView(R.id.timertext)
// TextView timerText;
// @BindView(R.id.sub_player)
View subPlayer;
RadioManager radioManager;
//fix start missing subplayer
public static String radioName = null;
String streamURL;
int flag = 0;
boolean isTimer;
String oldurl = null;
private long backPressedTime;
private Toast backToast;
int time=0;
//CountDownTimer texttimer;
boolean playingcheck;
int set = 0;
private Chronometer chronometer;
private long pauseOffset;
private boolean running;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
toolbar = findViewById(R.id.toolbar);
textView = findViewById(R.id.name);
subPlayer = findViewById(R.id.sub_player);
setSupportActionBar(toolbar);
radioManager = RadioManager.with(this);
listView.setAdapter(new ShoutcastListAdapter(this, ShoutcastHelper.retrieveShoutcasts(this)));
/*
texttimer = new CountDownTimer (1000000000 * 10000, 1000) {
public void onTick(long millisUntilFinished) {
String hms = String.format("%02d:%02d:%02d", TimeUnit.SECONDS.toHours(time),
TimeUnit.SECONDS.toMinutes(time) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(time)),
TimeUnit.SECONDS.toSeconds(time) - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(time)));
timerText.setText(hms);
time++;
}
public void onFinish() {
}
public void stoptimer (){
texttimer.cancel();
}
}; */
chronometer = findViewById(R.id.chronometer);
chronometer.setFormat("%s");
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
/* if ((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 100000000) {
chronometer.setBase(SystemClock.elapsedRealtime());
//Toast.makeText(MainActivity.this, "Bing!", Toast.LENGTH_SHORT).show();
}*/
}
});
}
public void startChronometer() {
if (!running) {
chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
chronometer.start();
running = true;
}
}
public void pauseChronometer() {
if (running) {
chronometer.stop();
pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
running = false;
}
}
public void resetChronometer() {
chronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;
}
public String checkDigit(int number) {
return number <= 9 ? "0" + number : String.valueOf(number);
}
public void open(View view) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.morrmat.com"));
startActivity(browserIntent);
}
//craig's code for 3 dots
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent browser = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.morrmat.com"));
startActivity(browser);
}
return true;
}
//craig's code for 3 dots ends
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onPause() {
startChronometer();
super.onPause();
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
//texttimer.cancel();
pauseChronometer();
}
@Override
protected void onDestroy() {
radioManager.unbind();
super.onDestroy();
Runtime.getRuntime().gc();
finish();
finishAffinity();
System.exit(0);
}
@Override
protected void onResume() {
super.onResume();
radioManager.bind();
}
@Override
public void onBackPressed() {
//finish();
if (backPressedTime + 2000 > System.currentTimeMillis()) {
backToast.cancel();
super.onBackPressed();
finish();
return;
} else {
backToast = Toast.makeText(getBaseContext(), "Press BACK again to exit", Toast.LENGTH_SHORT);
backToast.show();
}
backPressedTime = System.currentTimeMillis();
}
@Subscribe
public void onEvent(String status){
switch (status){
case PlaybackStatus.LOADING:
//texttimer.cancel();
pauseChronometer();
playingcheck = false;
// loading
break;
case PlaybackStatus.PLAYING:
//texttimer.cancel();
pauseChronometer();
playingcheck =true;
set =1;
startChronometer();
// texttimer.start();
break;
case PlaybackStatus.ERROR:
//texttimer.cancel();
pauseChronometer();
Toast.makeText(this, "-UNABLE TO STREAM-" + "
"+" STATION DOWN or NO INTERNET", Toast.LENGTH_LONG).show();
playingcheck = false;
time = 0;
resetChronometer();
break;
case PlaybackStatus.STOPPED:
//texttimer.cancel();
pauseChronometer();
playingcheck = false;
break;
case PlaybackStatus.PAUSED:
//texttimer.cancel();
pauseChronometer();
playingcheck =false;
break;
case PlaybackStatus.IDLE:
//texttimer.cancel();
pauseChronometer();
playingcheck =false;
break;
}
trigger.setImageResource(status.equals(PlaybackStatus.PLAYING)
? R.drawable.ic_pause_black
: R.drawable.ic_play_arrow_black);
}
@OnClick(R.id.playTrigger)
public void onClicked(){
if(TextUtils.isEmpty(streamURL)) return;
radioManager.playOrPause(streamURL);
if(set==1){
if (playingcheck =true){
if (oldurl == streamURL){
//texttimer.cancel();
pauseChronometer();
playingcheck = false;
} else {
//texttimer.cancel();
pauseChronometer();
time = 0;
resetChronometer();
//texttimer.start();
startChronometer();
playingcheck = true;
}
} else {
if (!(oldurl == streamURL)){
//texttimer.cancel();
pauseChronometer();
time = 0;
resetChronometer();
//texttimer.start();
startChronometer();
playingcheck = true;
set =0;
} else {
//texttimer.cancel();
pauseChronometer();
//texttimer.start();
startChronometer();
playingcheck = true;
}
}}
}
@RequiresApi(api = Build.VERSION_CODES.M)
@OnItemClick(R.id.listview)
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Shoutcast shoutcast = (Shoutcast) parent.getItemAtPosition(position);
if (shoutcast == null) {
return;
}
view.setPressed(true);
radioName = shoutcast.getName();
textView.setText(shoutcast.getName());
trigger.setVisibility(View.VISIBLE);
if (streamURL==null) {playingcheck=false; }
streamURL = shoutcast.getUrl();
if (oldurl != streamURL){Toast.makeText(this, "Loading " + radioName + "
" + " please wait.", Toast.LENGTH_SHORT).show();}
radioManager.playOrPause(streamURL);
if(set==1){
if (playingcheck =true){
if (oldurl == streamURL){
//texttimer.cancel();
pauseChronometer();
playingcheck = false;
} else {
//texttimer.cancel();
pauseChronometer();
time = 0;
resetChronometer();
//texttimer.start();
startChronometer();
playingcheck = true;
}
} else {
if (!(oldurl == streamURL)){
//texttimer.cancel();
pauseChronometer();
time = 0;
resetChronometer();
//texttimer.start();
startChronometer();
playingcheck = true;
set =0;
} else {
//texttimer.cancel();
pauseChronometer();
//texttimer.start();
startChronometer();
playingcheck = true;
}
}}
oldurl = streamURL;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…