Skip to content
This repository has been archived by the owner on Oct 18, 2020. It is now read-only.

Commit

Permalink
Added swipe to play/download, added number filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusjdev committed Jul 20, 2019
1 parent 5d0c93f commit a9d81f9
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 47 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.d3coding.gmusicapi"
minSdkVersion 28
targetSdkVersion 28
versionCode 28
versionName "0.24.2"
versionCode 30
versionName "0.25"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
Expand All @@ -28,6 +29,7 @@
import androidx.fragment.app.Fragment;
import androidx.palette.graphics.Palette;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

Expand All @@ -37,6 +39,7 @@
import com.d3coding.gmusicapi.gmusic.Download;
import com.d3coding.gmusicapi.items.MusicAdapter;
import com.d3coding.gmusicapi.items.MusicItem;
import com.d3coding.gmusicapi.items.MusicSwipe;

import java.io.File;
import java.util.ArrayList;
Expand All @@ -52,6 +55,7 @@ public class HomeFragment extends Fragment {

private int sort = 0;
private int sortOnline = 0;
private boolean desc = false;

private String filterText = "";

Expand Down Expand Up @@ -80,11 +84,24 @@ public void onViewCreated(@NonNull View layoutView, @Nullable Bundle savedInstan
mDownload = new Download(getContext());
mAdapter = new MusicAdapter(ConvertList);

mAdapter.setOnDownloadItem((UUID) -> {
if (mDownload.scan(UUID))
Toast.makeText(getContext(), "Music already downloaded", Toast.LENGTH_SHORT).show();
else
downloadQueueService.execute(() -> mDownload.getQueue(UUID));
});

mAdapter.setOnPlayItem((UUID) -> {
if (!openFile(UUID))
Toast.makeText(getContext(), "Music isn't downloaded", Toast.LENGTH_SHORT).show();
});

LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(getContext(), R.anim.layout_animation_fall_down);
recyclerView.setLayoutAnimation(animation);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
(new ItemTouchHelper(new MusicSwipe(mAdapter, getContext()))).attachToRecyclerView(recyclerView);

if (mOnScrollListener != null)
recyclerView.addOnScrollListener(mOnScrollListener);
Expand Down Expand Up @@ -200,21 +217,8 @@ public void onViewCreated(@NonNull View layoutView, @Nullable Bundle savedInstan
builder.setOnCancelListener((dialog) -> exec.shutdown());
}


vView.findViewById(R.id.opt_bt_play).setOnClickListener((view2) -> {
Uri uri = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".fileprovider",
new File(new File(Environment.getExternalStorageDirectory(), "Gmusicapi"), musicItem.getUUID() + ".mp3"));

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, getContext().getContentResolver().getType(uri))
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);

});

vView.findViewById(R.id.opt_bt_open).setOnClickListener((view2) ->
startActivity(new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.parse(Environment.getExternalStorageDirectory() + "/Gmusicapi/"), "resource/folder")));

vView.findViewById(R.id.opt_bt_play).setOnClickListener((view2) -> openFile(musicItem.getUUID()));
vView.findViewById(R.id.opt_bt_open).setOnClickListener((view2) -> openFolder());
vView.findViewById(R.id.opt_bt_delete).setOnClickListener((view2) -> Toast.makeText(getContext(), getString(R.string.null_description), Toast.LENGTH_SHORT).show());

builder.setView(vView).create().show();
Expand All @@ -229,6 +233,24 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
return inflater.inflate(R.layout.frag_items, container, false);
}

boolean openFile(String UUID) {
if (mDownload.scan(UUID)) {
Uri uri = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".fileprovider",
new File(new File(Environment.getExternalStorageDirectory(), "Gmusicapi"), UUID + ".mp3"));

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, getContext().getContentResolver().getType(uri))
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
return true;
} else
return false;
}

void openFolder() {
startActivity(new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.parse(Environment.getExternalStorageDirectory() + "/Gmusicapi/"), "resource/folder"));
}

void initThreads() {
if (downloadQueueService == null)
downloadQueueService = (ThreadPoolExecutor) Executors.newFixedThreadPool(Config.SIMULTANEOUS_DOWNLOAD);
Expand All @@ -246,25 +268,28 @@ public int getNumItems() {
}

public void showFilter() {
{
ViewGroup vView = (ViewGroup) getLayoutInflater().inflate(R.layout.ad_filter, null);
Spinner spinner_organize = vView.findViewById(R.id.filter_organize);
Spinner spinner_filter = vView.findViewById(R.id.filter_filter);
ArrayAdapter<CharSequence> adapter_organize = ArrayAdapter.createFromResource(getContext(), R.array.organize_by, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter_filter = ArrayAdapter.createFromResource(getContext(), R.array.filter_by, android.R.layout.simple_spinner_item);
adapter_organize.setDropDownViewResource(R.layout.spinner_simple_text_box);
adapter_filter.setDropDownViewResource(R.layout.spinner_simple_text_box);
spinner_organize.setAdapter(adapter_organize);
spinner_filter.setAdapter(adapter_filter);
spinner_organize.setSelection(sort);
spinner_filter.setSelection(sortOnline);
new AlertDialog.Builder(getContext(), R.style.AppTheme_AlertDialog).setPositiveButton(R.string.act_icon_filter, (dialog, which) -> {
sort = spinner_organize.getSelectedItemPosition();
sortOnline = spinner_filter.getSelectedItemPosition();
updateList();
}).setView(vView).create().show();

}

ViewGroup vView = (ViewGroup) getLayoutInflater().inflate(R.layout.ad_filter, null);
Spinner spinner_organize = vView.findViewById(R.id.filter_organize);
Spinner spinner_filter = vView.findViewById(R.id.filter_filter);
ArrayAdapter<CharSequence> adapter_organize = ArrayAdapter.createFromResource(getContext(), R.array.organize_by, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter_filter = ArrayAdapter.createFromResource(getContext(), R.array.filter_by, android.R.layout.simple_spinner_item);
adapter_organize.setDropDownViewResource(R.layout.spinner_simple_text_box);
adapter_filter.setDropDownViewResource(R.layout.spinner_simple_text_box);
spinner_organize.setAdapter(adapter_organize);
spinner_filter.setAdapter(adapter_filter);
spinner_organize.setSelection(sort);
spinner_filter.setSelection(sortOnline);

CheckBox checkBox = vView.findViewById(R.id.checkbox_ascend);
checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> desc = isChecked);

new AlertDialog.Builder(getContext(), R.style.AppTheme_AlertDialog).setPositiveButton(R.string.act_icon_filter, (dialog, which) -> {
sort = spinner_organize.getSelectedItemPosition();
sortOnline = spinner_filter.getSelectedItemPosition();
updateList();
}).setView(vView).create().show();


}

Expand All @@ -279,7 +304,7 @@ void updateList() {
db = new Database(getContext());

ConvertList.clear();
ConvertList.addAll(db.getMusicItems(sort, sortOnline, filterText, false));
ConvertList.addAll(db.getMusicItems(sort, sortOnline, filterText, desc));
mAdapter.notifyDataSetChanged();

recyclerView.startLayoutAnimation();
Expand Down
23 changes: 19 additions & 4 deletions app/src/main/java/com/d3coding/gmusicapi/gmusic/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Database extends SQLiteOpenHelper {
downloadsColumn.id.name() + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
downloadsColumn.uuid.name() + " TEXT NOT NULL, " +
downloadsColumn.downloadTimestamp.name() + " INTEGER );";
private static final int DATABASE_VERSION = 6;
private static final int DATABASE_VERSION = 7;
private static final String TYPE_TEXT = " TEXT, ";
private static final String TYPE_INTEGER = " INTEGER, ";
private static final String SQL_CREATE_TABLE_TRACKS = "CREATE TABLE " + TABLE_TRACKS + " ( " +
Expand All @@ -41,6 +41,7 @@ public class Database extends SQLiteOpenHelper {
column.albumId.name() + TYPE_TEXT +
column.artistId.name() + TYPE_TEXT +
column.comment.name() + TYPE_TEXT +
column.creationTimestamp.name() + TYPE_TEXT +
column.totalTrackCount.name() + " INTEGER );";
private static final String SQL_DELETE_POSTS = "DROP TABLE IF EXISTS ";

Expand Down Expand Up @@ -80,6 +81,7 @@ public void insertByTrackMetadata(List<TrackMetadata> trackMetadata) {
values.put(column.artistId.name(), track.artistId);
values.put(column.comment.name(), track.comment);
values.put(column.totalTrackCount.name(), track.totalTrackCount);
values.put(column.creationTimestamp.name(), track.creationTimestamp);

db.insert(TABLE_TRACKS, null, values);
}
Expand Down Expand Up @@ -159,14 +161,23 @@ public List<MusicItem> getMusicItems(int order, int sortOnline, String filterTit

StringBuilder selection = new StringBuilder();

int num = 5000;

if (!filterTitle.equals("")) {
if (filterTitle.startsWith("AR:"))
selection.append(column.artist.name()).append(" LIKE \'%").append(filterTitle.replace("AR:", "")).append("%\'");
else if (filterTitle.startsWith("AL:"))
selection.append(column.album.name()).append(" LIKE \'%").append(filterTitle.replace("AL:", "")).append("%\'");
else if (filterTitle.startsWith("GE:"))
selection.append(column.genre.name()).append(" LIKE \'%").append(filterTitle.replace("GE:", "")).append("%\'");
else
else if (filterTitle.startsWith("NU:")) {
if (filterTitle.length() == 5)
try {
num = Integer.parseInt(String.valueOf(filterTitle.charAt(3)) + filterTitle.charAt(4));
} catch (NumberFormatException e) {
e.printStackTrace();
}
} else
selection.append(column.title.name()).append(" LIKE \'%").append(filterTitle).append("%\'");
}

Expand All @@ -180,13 +191,15 @@ else if (filterTitle.startsWith("GE:"))
selection.append(column.uuid.name()).append(" NOT IN (SELECT ").append(downloadsColumn.uuid.name()).append(" FROM ").append(TABLE_DOWNLOAD).append(")");
}

String orderBy = null;
String orderBy;
if (order == 1)
orderBy = column.artist.name();
else if (order == 2)
orderBy = column.album.name();
else if (order == 3)
orderBy = column.genre.name();
else if (order == 4)
orderBy = column.creationTimestamp.name();
else
orderBy = column.title.name();

Expand All @@ -198,12 +211,14 @@ else if (order == 3)
Cursor cursor = db.query(TABLE_TRACKS, columns, selection.toString(), null, null, null, orderBy);

if (cursor != null && cursor.moveToFirst()) {
int x = 0;
do {
Long milliseconds = Long.parseLong(cursor.getString(4));
ret.add(new MusicItem(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3),
String.format("%02d:%02d ", TimeUnit.MILLISECONDS.toMinutes(milliseconds), TimeUnit.MILLISECONDS.toSeconds(milliseconds) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds)))));
} while (cursor.moveToNext());
++x;
} while (cursor.moveToNext() && x < num);
}

try {
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/d3coding/gmusicapi/gmusic/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ protected Void doInBackground(String... strings) {
else
trackMetadata.totalTrackCount = 0;

//Timestamp
Optional<String> optionalCreationTimestamp = track.getCreationTimestamp();
if (optionalComment.isPresent())
trackMetadata.creationTimestamp = optionalCreationTimestamp.get();
else
trackMetadata.creationTimestamp = "";

chunkList.add(trackMetadata);
}

Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/d3coding/gmusicapi/items/MusicAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.MyViewHolder
private Download mDownload;
private OnItemClickListener clickListener;
private OnItemLongClickListener longListener;
public OnDownloadItemListener downloadItem;
public OnPlayItemListener playItem;

public MusicAdapter(List<MusicItem> value) {
this.convertList = value;
Expand All @@ -44,6 +46,23 @@ public void setOnItemLongClickListener(OnItemLongClickListener listener) {
this.longListener = listener;
}

public void setOnDownloadItem(OnDownloadItemListener mOnDownloadItem) {
this.downloadItem = mOnDownloadItem;
}

public void setOnPlayItem(OnPlayItemListener mOnPlayItem) {
this.playItem = mOnPlayItem;
}

String getItemUUID(int position) {
return convertList.get(position).getUUID();
}

public interface OnDownloadItemListener {
void OnDownloadItem(String UUID);
}


@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
MusicItem musicItems = convertList.get(position);
Expand Down Expand Up @@ -74,6 +93,10 @@ public int getItemCount() {
return convertList.size();
}

public interface OnPlayItemListener {
void OnPlayItem(String UUID);
}

public interface OnItemClickListener {
void onItemClick(View itemView, int position);
}
Expand Down
Loading

0 comments on commit a9d81f9

Please sign in to comment.