Skip to content

Commit

Permalink
fix_convention
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhquang252 committed Oct 30, 2018
1 parent 42437c5 commit e1617f1
Show file tree
Hide file tree
Showing 16 changed files with 402 additions and 204 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".View.MainActivity">
<activity android:name=".view.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".View.ServicePlayMusic"
android:name=".view.ServicePlayMusic"
android:exported="false" />
</application>

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

36 changes: 36 additions & 0 deletions app/src/main/java/com/example/quangtran/listenmusic/util/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.quangtran.listenmusic.util;

import java.text.SimpleDateFormat;
import java.util.Locale;

public class Util {
private static final int MILLITIME = 1000;
private static final int HOURS_TO_SECONDS = 3600;
private static final int HUNDRED = 100;
private static final int ONE_HOUR = 60;
private static final String HOUR_DEFAULT = "00:00";
private static final String TIME_FORMAT = "mm:ss";

public String TotalTime(long milliseconds) {
SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT, Locale.US);
return dateFormat.format(milliseconds);
}

//update progressbar
public static int getProgressPercentage(long currentDuration, long totalDuration) {
Double percentage = (double) 0;
long currentSeconds = (int) (currentDuration / MILLITIME);
long totalSeconds = (int) (totalDuration / MILLITIME);
double phantram = (double) (currentSeconds * HUNDRED / totalSeconds);
percentage = ((double) (currentSeconds * HUNDRED / totalSeconds));
return percentage.intValue();
}

//progress tracking then song stop here
public static int progressToTimer(int progress, int totalDuration) {
int currentDuration = 0;
totalDuration = totalDuration / MILLITIME;
currentDuration = (int) ((((double) progress) / HUNDRED) * totalDuration);
return currentDuration * MILLITIME;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package com.example.quangtran.listenmusic.view;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.quangtran.listenmusic.R;
import com.example.quangtran.listenmusic.util.Util;

public class MainActivity extends AppCompatActivity implements View.OnClickListener
, SeekBar.OnSeekBarChangeListener {
private static final String TITLE_NOTIFICATION = "Media Artist";
private static final String CONTENT_NOTIFICATION = "thuong thuc am nhac";
private static final int REQUEST_CODE = 1;
private static final int NOTIFICATION_ID = 1;
private static final int PROGRESS_START = 0;
private static final int PROGRESS_MAX = 100;
private SeekBar mProgressMusic;
private ImageButton mImagePlay, mImageNext, mImageBack;
private TextView mCurrentTime, mTotalTime;
private Util mUtil = new Util();
private android.os.Handler mHandler = new android.os.Handler();
private ServicePlayMusic mServicePlayMusic;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageBack = findViewById(R.id.image_back);
mImagePlay = findViewById(R.id.image_play);
mImageNext = findViewById(R.id.image_next);
mImagePlay.setOnClickListener(this);
mImageBack.setOnClickListener(this);
mImageNext.setOnClickListener(this);
mCurrentTime = findViewById(R.id.text_current_time);
mTotalTime = findViewById(R.id.text_total_time);
mProgressMusic = findViewById(R.id.seek_bar_music);
mProgressMusic.setProgress(PROGRESS_START);
mProgressMusic.setMax(PROGRESS_MAX);
mProgressMusic.setOnSeekBarChangeListener(this);
startService();
buildNotification();
}

private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
ServicePlayMusic.LocalBinder binder = (ServicePlayMusic.LocalBinder) iBinder;
mServicePlayMusic = binder.getService();
updateUI();
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
unbindService(mConnection);
}
};

private void startService() {
Intent startService = new Intent(this, ServicePlayMusic.class);
bindService(startService, mConnection, BIND_AUTO_CREATE);
startService(startService);
}

private void buildNotification() {
Intent intentNotification = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent
.getService(getApplicationContext(), REQUEST_CODE, intentNotification, 0);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.music_icon)
.setContentTitle(TITLE_NOTIFICATION)
.setContentText(CONTENT_NOTIFICATION)
.setDeleteIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}

private void updateUI() {
if (mServicePlayMusic.mediaPlayer != null) {
updateProgressBar();
if (mServicePlayMusic.mediaPlayer.isPlaying()) {
mImagePlay.setImageResource(R.drawable.ic_pause);
} else {
mImagePlay.setImageResource(R.drawable.ic_play_button);
}
}
}

private Runnable mUpdateTimes = (new Runnable() {
@Override
public void run() {
long currentTime = mServicePlayMusic.mediaPlayer.getCurrentPosition();
long totalTime = mServicePlayMusic.mediaPlayer.getDuration();
mCurrentTime.setText(mUtil.TotalTime(currentTime));
mTotalTime.setText(mUtil.TotalTime(totalTime));
//update progressbar
int progress = (int) (Util.getProgressPercentage(currentTime, totalTime));
mProgressMusic.setProgress(progress);
if (mProgressMusic.getProgress() == mProgressMusic.getMax()) {
mServicePlayMusic.nextSong();
}
mHandler.postDelayed(this, PROGRESS_MAX);//de quy
}
});

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.image_play:
updatePlay();
break;
case R.id.image_back:
mServicePlayMusic.backSong();
mImagePlay.setImageResource(R.drawable.ic_pause);
break;

case R.id.image_next:
mServicePlayMusic.nextSong();
mImagePlay.setImageResource(R.drawable.ic_pause);
break;
default:
break;
}
}

private void updatePlay() {
if (mServicePlayMusic.mediaPlayer.isPlaying()) {
mServicePlayMusic.play();
mImagePlay.setImageResource(R.drawable.ic_play_button);
} else {
mServicePlayMusic.play();
mImagePlay.setImageResource(R.drawable.ic_pause);
}
}

private void updateProgressBar() {
mHandler.postDelayed(mUpdateTimes, PROGRESS_MAX);
}

@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mHandler.removeCallbacks(mUpdateTimes);
if (mServicePlayMusic.mediaPlayer != null) {
int totalDuration = mServicePlayMusic.mediaPlayer.getDuration();
int progress = mProgressMusic.getProgress();
int current = Util.progressToTimer(progress, totalDuration);
mServicePlayMusic.mediaPlayer.seekTo(current);
updateProgressBar();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}
}
Loading

0 comments on commit e1617f1

Please sign in to comment.