Untitled
unknown
plain_text
2 years ago
26 kB
6
Indexable
package com.animescout.apps;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.appbar.AppBarLayout;
import android.app.*;
import android.media.AudioAttributes;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.*;
import android.graphics.*;
import android.util.*;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.BaseAdapter;
import android.widget.ScrollView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.HorizontalScrollView;
import android.widget.SeekBar;
import android.media.MediaPlayer;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.GenericTypeIndicator;
import com.google.firebase.database.ChildEventListener;
import android.widget.AdapterView;
import android.view.View;
import android.graphics.Typeface;
import androidx.media.app.NotificationCompat.MediaStyle;
public class MusicActivity extends AppCompatActivity {
private Timer _timer = new Timer();
private FirebaseDatabase _firebase = FirebaseDatabase.getInstance();
private Toolbar _toolbar;
private AppBarLayout _app_bar;
private CoordinatorLayout _coordinator;
private String url_string = "";
private double pos = 0;
private boolean isPlaying = false;
private boolean isChanging = false;
private boolean isExpanded = false;
private boolean isAdmin = false;
private double profile_num = 0;
private String fontName = "";
private String typeace = "";
private String textTitle = "";
private String textAlbum = "";
private ArrayList<HashMap<String, Object>> upload_list = new ArrayList<>();
private ArrayList<String> childkey = new ArrayList<>();
private LinearLayout dasar;
private LinearLayout linear2;
private ListView listview1;
private ScrollView vscroll1;
private LinearLayout linear3;
private ProgressBar progressbar1;
private TextView textview1;
private LinearLayout linear4;
private LinearLayout linear5;
private LinearLayout linear9;
private LinearLayout linear6;
private LinearLayout linear7;
private ImageView image_expand;
private HorizontalScrollView hscroll1;
private TextView text_title;
private TextView text_current;
private SeekBar seekbar1;
private TextView text_max;
private LinearLayout linear8;
private ImageView image_previous;
private ImageView image_play;
private ImageView image_next;
private MediaPlayer musik;
private TimerTask timer;
private Calendar current = Calendar.getInstance();
private Calendar duration = Calendar.getInstance();
private TimerTask delay;
private DatabaseReference upload_text = _firebase.getReference("upload/text");
private ChildEventListener _upload_text_child_listener;
@Override
protected void onCreate(Bundle _savedInstanceState) {
super.onCreate(_savedInstanceState);
setContentView(R.layout.music);
initialize(_savedInstanceState);
com.google.firebase.FirebaseApp.initializeApp(this);
initializeLogic();
}
private void initialize(Bundle _savedInstanceState) {
_app_bar = (AppBarLayout) findViewById(R.id._app_bar);
_coordinator = (CoordinatorLayout) findViewById(R.id._coordinator);
_toolbar = (Toolbar) findViewById(R.id._toolbar);
setSupportActionBar(_toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View _v) {
onBackPressed();
}
});
dasar = (LinearLayout) findViewById(R.id.dasar);
linear2 = (LinearLayout) findViewById(R.id.linear2);
listview1 = (ListView) findViewById(R.id.listview1);
vscroll1 = (ScrollView) findViewById(R.id.vscroll1);
linear3 = (LinearLayout) findViewById(R.id.linear3);
progressbar1 = (ProgressBar) findViewById(R.id.progressbar1);
textview1 = (TextView) findViewById(R.id.textview1);
linear4 = (LinearLayout) findViewById(R.id.linear4);
linear5 = (LinearLayout) findViewById(R.id.linear5);
linear9 = (LinearLayout) findViewById(R.id.linear9);
linear6 = (LinearLayout) findViewById(R.id.linear6);
linear7 = (LinearLayout) findViewById(R.id.linear7);
image_expand = (ImageView) findViewById(R.id.image_expand);
hscroll1 = (HorizontalScrollView) findViewById(R.id.hscroll1);
text_title = (TextView) findViewById(R.id.text_title);
text_current = (TextView) findViewById(R.id.text_current);
seekbar1 = (SeekBar) findViewById(R.id.seekbar1);
text_max = (TextView) findViewById(R.id.text_max);
linear8 = (LinearLayout) findViewById(R.id.linear8);
image_previous = (ImageView) findViewById(R.id.image_previous);
image_play = (ImageView) findViewById(R.id.image_play);
image_next = (ImageView) findViewById(R.id.image_next);
listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> _param1, View _param2, int _param3, long _param4) {
final int _position = _param3;
text_title.setText(upload_list.get((int)_position).get("name").toString());
url_string = upload_list.get((int)_position).get("url").toString();
String musicName = upload_list.get((int)_position).get("name").toString();
String musicUrl = upload_list.get((int)_position).get("url").toString();
textAlbum = "You're playing a song";
//Bitmap para recoger la imagen de drawable
Bitmap img01 = BitmapFactory.decodeResource(getResources(), R.drawable.imagem);
// Set up the media player
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build());
try {
mediaPlayer.setDataSource(musicUrl);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
//intent para abrir la aplicaciΓ³n al tocar la notificaciΓ³n
Intent in = new Intent(MusicActivity.this, MusicActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(MusicActivity.this, 0, in, 0);
//Notification
androidx.core.app.NotificationCompat.Builder builder = new androidx.core.app.NotificationCompat.Builder(MusicActivity.this, "id 1")
.setPriority(androidx.core.app.NotificationCompat.PRIORITY_LOW)
.setSmallIcon(R.drawable.ic_audiotrack_white)
.setContentTitle(musicName)
.setContentText(textAlbum)
.setStyle(new MediaStyle())
.setLargeIcon(img01)
.setAutoCancel(false)
.setOngoing(true)
.setOnlyAlertOnce(false)
.setContentIntent(pendingIntent);
//Eksekusi Notification
androidx.core.app.NotificationManagerCompat notificationManager = androidx.core.app.NotificationManagerCompat.from(MusicActivity.this);
notificationManager.notify(1, builder.build());
if (isPlaying) {
timer.cancel();
if (musik.isPlaying()) {
musik.stop();
}
}
else {
isPlaying = true;
}
linear4.setVisibility(View.GONE);
musik = new MediaPlayer();
musik.setAudioStreamType(3);
try {
musik.setDataSource(url_string);
}catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "Unknown URL!", 1).show();
} catch (SecurityException e2) {
Toast.makeText(getApplicationContext(), "Unknown URL!", 1).show(); }
catch (IllegalStateException e3) {
Toast.makeText(getApplicationContext(), "Unknown URL!", 1).show(); }
catch (java.io.IOException e4)
{
e4.printStackTrace();
}
try
{
musik.prepare();
}
catch (IllegalStateException e5)
{
Toast.makeText(getApplicationContext(), "Unknown URL!", 1).show();
}
catch (java.io.IOException e6)
{
Toast.makeText(getApplicationContext(), "Unknown URL!", 1).show();
}
musik.start();
linear4.setVisibility(View.VISIBLE);
pos = _position;
duration.setTimeInMillis((long)(musik.getDuration()));
text_max.setText(new SimpleDateFormat("mm:ss").format(duration.getTime()));
seekbar1.setMax((int)musik.getDuration() / 1000);
timer = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (musik.isPlaying()) {
seekbar1.setProgress((int)musik.getCurrentPosition() / 1000);
image_play.setImageResource(R.drawable.ic_pause_white);
}
else {
image_play.setImageResource(R.drawable.ic_play_arrow_white);
}
}
});
}
};
_timer.scheduleAtFixedRate(timer, (int)(0), (int)(1));
musik.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer theMediaPlayer){
timer.cancel();
pos++;
if (upload_list.size() > pos) {
_play(pos);
}
else {
timer.cancel();
linear4.setVisibility(View.GONE);
isPlaying = false;
}
}
});
}
});
image_expand.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View _view) {
if (isExpanded) {
isExpanded = false;
_autoTransitionScroll(vscroll1);
linear6.setVisibility(View.GONE);
linear7.setVisibility(View.GONE);
image_expand.setImageResource(R.drawable.ic_expand_less_white);
}
else {
isExpanded = true;
_autoTransitionScroll(vscroll1);
linear6.setVisibility(View.VISIBLE);
linear7.setVisibility(View.VISIBLE);
image_expand.setImageResource(R.drawable.ic_expand_more_white);
}
}
});
seekbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged (SeekBar _param1, int _param2, boolean _param3) {
final int _progressValue = _param2;
if (isChanging) {
current.setTimeInMillis((long)(_progressValue * 1000));
text_current.setText(new SimpleDateFormat("mm:ss").format(current.getTime()));
musik.seekTo((int)(_progressValue * 1000));
}
else {
current.setTimeInMillis((long)(musik.getCurrentPosition()));
text_current.setText(new SimpleDateFormat("mm:ss").format(current.getTime()));
}
}
@Override
public void onStartTrackingTouch(SeekBar _param1) {
timer.cancel();
musik.pause();
isChanging = true;
image_play.setImageResource(R.drawable.ic_play_arrow_white);
}
@Override
public void onStopTrackingTouch(SeekBar _param2) {
isChanging = false;
timer = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (musik.isPlaying()) {
seekbar1.setProgress((int)musik.getCurrentPosition() / 1000);
image_play.setImageResource(R.drawable.ic_pause_white);
}
else {
image_play.setImageResource(R.drawable.ic_play_arrow_white);
}
}
});
}
};
_timer.scheduleAtFixedRate(timer, (int)(0), (int)(1));
musik.start();
}
});
image_previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View _view) {
if ((pos - 1) > -1) {
timer.cancel();
musik.stop();
pos--;
_play(pos);
}
}
});
image_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View _view) {
if (musik.isPlaying()) {
musik.pause();
}
else {
musik.start();
}
}
});
image_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View _view) {
if (upload_list.size() > (pos + 1)) {
timer.cancel();
musik.stop();
pos++;
_play(pos);
}
}
});
_upload_text_child_listener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot _param1, String _param2) {
GenericTypeIndicator<HashMap<String, Object>> _ind = new GenericTypeIndicator<HashMap<String, Object>>() {};
final String _childKey = _param1.getKey();
final HashMap<String, Object> _childValue = _param1.getValue(_ind);
childkey.add(_childKey);
upload_text.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot _dataSnapshot) {
upload_list = new ArrayList<>();
try {
GenericTypeIndicator<HashMap<String, Object>> _ind = new GenericTypeIndicator<HashMap<String, Object>>() {};
for (DataSnapshot _data : _dataSnapshot.getChildren()) {
HashMap<String, Object> _map = _data.getValue(_ind);
upload_list.add(_map);
}
}
catch (Exception _e) {
_e.printStackTrace();
}
listview1.setAdapter(new Listview1Adapter(upload_list));
}
@Override
public void onCancelled(DatabaseError _databaseError) {
}
});
}
@Override
public void onChildChanged(DataSnapshot _param1, String _param2) {
GenericTypeIndicator<HashMap<String, Object>> _ind = new GenericTypeIndicator<HashMap<String, Object>>() {};
final String _childKey = _param1.getKey();
final HashMap<String, Object> _childValue = _param1.getValue(_ind);
}
@Override
public void onChildMoved(DataSnapshot _param1, String _param2) {
}
@Override
public void onChildRemoved(DataSnapshot _param1) {
GenericTypeIndicator<HashMap<String, Object>> _ind = new GenericTypeIndicator<HashMap<String, Object>>() {};
final String _childKey = _param1.getKey();
final HashMap<String, Object> _childValue = _param1.getValue(_ind);
}
@Override
public void onCancelled(DatabaseError _param1) {
final int _errorCode = _param1.getCode();
final String _errorMessage = _param1.getMessage();
}
};
upload_text.addChildEventListener(_upload_text_child_listener);
}
private void initializeLogic() {
_createChannel();
setTitle("Music Box");
_changeActivityFont("googlesans");
int[] colors = {Color.rgb(138,41,81),Color.rgb(41,53,158)};
android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable(android.graphics.drawable.GradientDrawable.Orientation.BR_TL, colors);
gd.setStroke(0,Color.WHITE);
if(android.os.Build.VERSION.SDK_INT >= 16) {dasar.setBackground(gd); } else {dasar.setBackgroundDrawable(gd);}
_Card_View(linear3, 35, "FFFFFF", 10, 0, "");
textview1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/googlesans.ttf"), 0);
linear6.setVisibility(View.GONE);
linear7.setVisibility(View.GONE);
linear4.setVisibility(View.GONE);
upload_text.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot _dataSnapshot) {
upload_list = new ArrayList<>();
try {
GenericTypeIndicator<HashMap<String, Object>> _ind = new GenericTypeIndicator<HashMap<String, Object>>() {};
for (DataSnapshot _data : _dataSnapshot.getChildren()) {
HashMap<String, Object> _map = _data.getValue(_ind);
upload_list.add(_map);
}
}
catch (Exception _e) {
_e.printStackTrace();
}
linear2.setVisibility(View.GONE);
_RippleEffect(image_expand);
_RippleEffect(image_previous);
_RippleEffect(image_play);
_RippleEffect(image_next);
text_title.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/googlesans.ttf"), 0);
text_current.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/googlesans.ttf"), 0);
text_max.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/googlesans.ttf"), 0);
}
@Override
public void onCancelled(DatabaseError _databaseError) {
}
});
}
@Override
protected void onActivityResult(int _requestCode, int _resultCode, Intent _data) {
super.onActivityResult(_requestCode, _resultCode, _data);
switch (_requestCode) {
default:
break;
}
}
@Override
public void onBackPressed() {
if (isPlaying) {
musik.reset();
finish();
}
else {
finish();
}
}
@Override
public void onDestroy() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancelAll();
super.onDestroy();
}
public void _play (final double _pos) {
text_title.setText(upload_list.get((int)_pos).get("name").toString());
url_string = upload_list.get((int)_pos).get("url").toString();
linear4.setVisibility(View.GONE);
musik = new MediaPlayer();
musik.setAudioStreamType(3);
try {
musik.setDataSource(url_string);
}catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "Unknown URL!", 1).show();
} catch (SecurityException e2) {
Toast.makeText(getApplicationContext(), "Unknown URL!", 1).show(); }
catch (IllegalStateException e3) {
Toast.makeText(getApplicationContext(), "Unknown URL!", 1).show(); }
catch (java.io.IOException e4)
{
e4.printStackTrace();
}
try
{
musik.prepare();
}
catch (IllegalStateException e5)
{
Toast.makeText(getApplicationContext(), "Unknown URL!", 1).show();
}
catch (java.io.IOException e6)
{
Toast.makeText(getApplicationContext(), "Unknown URL!", 1).show();
}
musik.start();
linear4.setVisibility(View.VISIBLE);
pos = _pos;
seekbar1.setMax((int)musik.getDuration() / 1000);
timer = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (musik.isPlaying()) {
seekbar1.setProgress((int)musik.getCurrentPosition() / 1000);
image_play.setImageResource(R.drawable.ic_pause_white);
}
else {
image_play.setImageResource(R.drawable.ic_play_arrow_white);
}
}
});
}
};
_timer.scheduleAtFixedRate(timer, (int)(0), (int)(1));
musik.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer theMediaPlayer){
timer.cancel();
pos++;
if (upload_list.size() > pos) {
_play(pos);
}
else {
timer.cancel();
linear4.setVisibility(View.GONE);
isPlaying = false;
}
}
});
}
public void _createSnackBar (final String _message) {
ViewGroup parentLayout = (ViewGroup) ((ViewGroup) this .findViewById(android.R.id.content)).getChildAt(0);
com.google.android.material.snackbar.Snackbar.make(parentLayout, _message, com.google.android.material.snackbar.Snackbar.LENGTH_LONG).show();
}
public void _RippleEffect (final View _v) {
int[] attrs = new int[]{android.R.attr.selectableItemBackgroundBorderless}; android.content.res.TypedArray typedArray = this.obtainStyledAttributes(attrs); int backgroundResource = typedArray.getResourceId(0, 0); _v.setBackgroundResource(backgroundResource); _v.setClickable(true);
}
public void _Card_View (final View _view, final double _cornerradius, final String _bgcolor, final double _elevation, final double _stroke, final String _strokecolor) {
if (_stroke == 0) {
//ππ₯π¨ππ€ ππ«πππππ ππ² π-6π’π±
android.graphics.drawable.GradientDrawable cv = new android.graphics.drawable.GradientDrawable();
float cornerradius = (float) _cornerradius;
cv.setCornerRadius(cornerradius);
cv.setColor(Color.parseColor("#" + _bgcolor.replace("#", "")));
_view.setBackground(cv);
float elevation = (float) _elevation;
_view.setElevation(elevation);
}
else {
android.graphics.drawable.GradientDrawable cv = new android.graphics.drawable.GradientDrawable();
float cornerradius = (float) _cornerradius;
cv.setStroke((int)_stroke, Color.parseColor("#" + _strokecolor.replace("#", "")));
cv.setCornerRadius(cornerradius);
cv.setColor(Color.parseColor("#" + _bgcolor.replace("#", "")));
_view.setBackground(cv);
float elevation = (float) _elevation;
_view.setElevation(elevation);
}
}
public void _autoTransitionScroll (final View _scroll) {
android.transition.TransitionManager.beginDelayedTransition((ScrollView)_scroll, new android.transition.AutoTransition());
}
public void _changeActivityFont (final String _fontname) {
fontName = "fonts/".concat(_fontname.concat(".ttf"));
overrideFonts(this,getWindow().getDecorView());
}
private void overrideFonts(final android.content.Context context, final View v) {
try {
Typeface
typeace = Typeface.createFromAsset(getAssets(), fontName);;
if ((v instanceof ViewGroup)) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0;
i < vg.getChildCount();
i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
}
else {
if ((v instanceof TextView)) {
((TextView) v).setTypeface(typeace);
}
else {
if ((v instanceof EditText )) {
((EditText) v).setTypeface(typeace);
}
else {
if ((v instanceof Button)) {
((Button) v).setTypeface(typeace);
}
}
}
}
}
catch(Exception e)
{
JavaUtil.showMessage(getApplicationContext(), "Error Loading Font");
};
}
public void _createChannel () {
//More block ( createChannel )
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Channel name 1";
String description = "Notification channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("id 1", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
public class Listview1Adapter extends BaseAdapter {
ArrayList<HashMap<String, Object>> _data;
public Listview1Adapter(ArrayList<HashMap<String, Object>> _arr) {
_data = _arr;
}
@Override
public int getCount() {
return _data.size();
}
@Override
public HashMap<String, Object> getItem(int _index) {
return _data.get(_index);
}
@Override
public long getItemId(int _index) {
return _index;
}
@Override
public View getView(final int _position, View _v, ViewGroup _container) {
LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.list, null);
}
final LinearLayout linear2 = (LinearLayout) _view.findViewById(R.id.linear2);
final TextView textview1 = (TextView) _view.findViewById(R.id.textview1);
textview1.setText(upload_list.get((int)_position).get("name").toString());
textview1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/googlesans.ttf"), 0);
return _view;
}
}
@Deprecated
public void showMessage(String _s) {
Toast.makeText(getApplicationContext(), _s, Toast.LENGTH_SHORT).show();
}
@Deprecated
public int getLocationX(View _v) {
int _location[] = new int[2];
_v.getLocationInWindow(_location);
return _location[0];
}
@Deprecated
public int getLocationY(View _v) {
int _location[] = new int[2];
_v.getLocationInWindow(_location);
return _location[1];
}
@Deprecated
public int getRandom(int _min, int _max) {
Random random = new Random();
return random.nextInt(_max - _min + 1) + _min;
}
@Deprecated
public ArrayList<Double> getCheckedItemPositionsToArray(ListView _list) {
ArrayList<Double> _result = new ArrayList<Double>();
SparseBooleanArray _arr = _list.getCheckedItemPositions();
for (int _iIdx = 0; _iIdx < _arr.size(); _iIdx++) {
if (_arr.valueAt(_iIdx))
_result.add((double)_arr.keyAt(_iIdx));
}
return _result;
}
@Deprecated
public float getDip(int _input) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, _input, getResources().getDisplayMetrics());
}
@Deprecated
public int getDisplayWidthPixels() {
return getResources().getDisplayMetrics().widthPixels;
}
@Deprecated
public int getDisplayHeightPixels() {
return getResources().getDisplayMetrics().heightPixels;
}
}
Editor is loading...