Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved issue of Home screen widget #2

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 31 additions & 15 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
xmlns:tools="http://schemas.android.com/tools">


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Expand All @@ -14,29 +15,44 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MusicPlayer"
tools:targetApi="31" >
tools:targetApi="31">


<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- PlaySong activity -->
<activity
android:name=".PlaySong"
android:exported="false"
android:parentActivityName="com.example.musicplayer.MainActivity">

<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.musicplayer.MainActivity"/>
</activity>
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
<!-- Music Widget Receiver -->
<receiver
android:name=".MusicWidgetProvider"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
android:name="android.appwidget.provider"
android:resource="@xml/music_widget_info" />
</receiver>

</manifest>

<service
android:name=".MusicService"
android:exported="true" />

</application>
</manifest>
7 changes: 3 additions & 4 deletions app/src/main/java/com/example/musicplayer/CustomAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ViewHolder(View view) {
super(view);
view.setOnClickListener(this);
textView = (TextView) view.findViewById(R.id.textView);
// Define click listener for the ViewHolder's View


}

Expand Down Expand Up @@ -75,12 +75,11 @@ public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {

// Get element from your dataset at this position and replace the
// contents of the view with that element

viewHolder.getTextView().setText(localdataset.get(position).getName());
}

// Return the size of your dataset (invoked by the layout manager)

@Override
public int getItemCount() {
return localdataset.size();
Expand Down
88 changes: 9 additions & 79 deletions app/src/main/java/com/example/musicplayer/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
package com.example.musicplayer;

import androidx.annotation.NonNull;


import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.karumi.dexter.Dexter;
import com.karumi.dexter.DexterBuilder;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
Expand All @@ -31,26 +19,21 @@

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
//public static HashMap<String,String> mp;
public static ArrayList<File> Songs;

//Method that returns an ArrayList of Files whose name ends with .mp3 and does not start with a .
public static ArrayList<File> getSongs(File file){
// Method that returns an ArrayList of Files whose name ends with .mp3 and does not start with a .
public static ArrayList<File> getSongs(File file) {
ArrayList<File> ans = new ArrayList<>();
File[] listFiles = file.listFiles();
if(listFiles!=null){
for(File curr:listFiles){
if(!curr.isHidden() && curr.isDirectory()){
if (listFiles != null) {
for (File curr : listFiles) {
if (!curr.isHidden() && curr.isDirectory()) {
ans.addAll(getSongs(curr));
}
else{
if(curr.getName().endsWith(".mp3") && !curr.getName().startsWith(".")){
} else {
if (curr.getName().endsWith(".mp3") && !curr.getName().startsWith(".")) {
ans.add(curr);
}
}
Expand All @@ -65,74 +48,30 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);

//Permission from User for External Storage Read Access using Dexter Library
// Permission from User for External Storage Read Access using Dexter Library
Dexter.withContext(this)
.withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
//If Permission granted


@Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {


// StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE);
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// List<StorageVolume> volumes = storageManager.getStorageVolumes();
// int i=0;
// while(i<volumes.size()){
// Log.d("MyLog",volumes.get(i).toString());
// i++;
// }
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// if (volumes.get(0).getDirectory()==Environment.getExternalStorageDirectory()){
// Log.d("MyLog","0Internal");
// }
// else{
// Log.d("MyLog","0External");
// }
// }
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// if (volumes.get(1).getDirectory()==Environment.getExternalStorageDirectory()){
// Log.d("MyLog","1Internal");
// }
// else{
// Log.d("MyLog","1External");
// }
// }
// }

//StorageManager storageManager = (StorageManager)getSystemService(STORAGE_SERVICE);
//List<StorageVolume> vol = storageManager.getStorageVolumes();



//Getting a List of Songs in the form of a String
//songs = getSongs(vol.get(1).getDirectory());
Songs = getSongs(Environment.getExternalStorageDirectory());


//Setting Recycler View
// Setting Recycler View
LinearLayoutManager llm = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(llm);
CustomAdapter adapter = new CustomAdapter(Songs);
recyclerView.setAdapter(adapter);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
llm.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
//recyclerView.se



}

//If Permission is not granted
@Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
Toast.makeText(MainActivity.this, "This Permission is required to proceed further", Toast.LENGTH_SHORT).show();
}

//Permission not granted and App reopened
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
Expand All @@ -141,32 +80,23 @@ public void onPermissionRationaleShouldBeShown(PermissionRequest permissionReque
.check();
}



@Override
protected void onPause() {
super.onPause();

}

@Override
protected void onResume() {
super.onResume();
// Intent backToMain = getIntent();
// int song_position = backToMain.getIntExtra("com.example.musicplayer.PlaySong.SongPosition", 0);
}

@Override
protected void onRestart() {
super.onRestart();
}


@Override
protected void onDestroy() {
super.onDestroy();
}
}



64 changes: 64 additions & 0 deletions app/src/main/java/com/example/musicplayer/MusicService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.example.musicplayer;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MusicService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

if (intent != null && intent.getAction() != null) {
switch (intent.getAction()) {
case "PLAY_PAUSE":
handlePlayPause();
break;
case "NEXT":
handleNext();
break;
case "PREVIOUS":
handlePrevious();
break;
case "FORWARD":
handleForward();
break;
case "REWIND":
handleRewind();
break;
}
}
return START_STICKY;
}

private void handlePlayPause() {

Toast.makeText(this, "Play/Pause clicked", Toast.LENGTH_SHORT).show();
}

private void handleNext() {

Toast.makeText(this, "Next clicked", Toast.LENGTH_SHORT).show();
}

private void handlePrevious() {

Toast.makeText(this, "Previous clicked", Toast.LENGTH_SHORT).show();
}

private void handleForward() {

Toast.makeText(this, "Forward clicked", Toast.LENGTH_SHORT).show();
}

private void handleRewind() {

Toast.makeText(this, "Rewind clicked", Toast.LENGTH_SHORT).show();
}

@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Loading